Skip to content
Snippets Groups Projects
Commit 7652dd95 authored by Benjamin Blanz's avatar Benjamin Blanz
Browse files

Added GRACE sector mapping

parent 9634ec12
No related branches found
No related tags found
No related merge requests found
...@@ -6,5 +6,6 @@ ...@@ -6,5 +6,6 @@
source('stockAggregation.R') source('stockAggregation.R')
source('sectorMappingNACE2GTAP.R') source('sectorMappingNACE2GTAP.R')
source('sectorMappingNACE2GRACE.R')
source('aggregateShocks.R') source('aggregateShocks.R')
source('relativizeShocks.R') source('relativizeShocks.R')
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
# aggregated data # aggregated data
# #
aggregateNUTS3ToCountry <- function(data,codes){ aggregateNUTS3ToCountry <- function(data,codes){
sectorColPattern <- 'ALL|TOTAL|^[A-Z]$|AGR|MIN|MFG|EGW|CNS|TRD|OTP|WTP|CMN|OFI|OBS|REA|PUB|OSG' sectorColPattern <- 'ALL|TOTAL|^[A-Z]$|AGR|MIN|MFG|EGW|CNS|TRD|OTP|WTP|CMN|OFI|OBS|REA|PUB|OSG|agr|coa.oil.gas|pro|ely.elc|ser|air.wtp.tran'
# identify columns with data rather than identifiers # identify columns with data rather than identifiers
sectorCols <- grep(sectorColPattern,names(data),perl = T) sectorCols <- grep(sectorColPattern,names(data),perl = T)
# convert data to numeric (deals with in import error) # convert data to numeric (deals with in import error)
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
# Benjamin Blanz 2024 # Benjamin Blanz 2024
# #
relData <- function(data,stocksNUTS3=NULL,stocksCNT=NULL){ relData <- function(data,stocksNUTS3=NULL,stocksCNT=NULL){
sectorColPattern <- 'ALL|TOTAL|^[A-Z]$|AGR|MIN|MFG|EGW|CNS|TRD|OTP|WTP|CMN|OFI|OBS|REA|PUB|OSG' sectorColPattern <- 'ALL|TOTAL|^[A-Z]$|AGR|MIN|MFG|EGW|CNS|TRD|OTP|WTP|CMN|OFI|OBS|REA|PUB|OSG|agr|coa.oil.gas|pro|ely.elc|ser|air.wtp.tran'
# Ensure the Total clumn is called TOTAL, not ALL as in some scenarios # Ensure the Total clumn is called TOTAL, not ALL as in some scenarios
names(data)[names(data)=='ALL'] <- 'TOTAL' names(data)[names(data)=='ALL'] <- 'TOTAL'
names(stocksNUTS3)[names(stocksNUTS3)=='ALL'] <- 'TOTAL' names(stocksNUTS3)[names(stocksNUTS3)=='ALL'] <- 'TOTAL'
......
No preview for this file type
...@@ -8,9 +8,11 @@ ...@@ -8,9 +8,11 @@
source('funRelData.R') source('funRelData.R')
nuts3LevelStocksNACE <- read.csv("helperData/nuts3LevelStocksNACE.csv", row.names=NULL) nuts3LevelStocksNACE <- read.csv("helperData/nuts3LevelStocksNACE.csv", row.names=NULL)
nuts3LevelStocksGTAP <- read.csv("helperData/nuts3LevelStocksGTAP.csv", row.names=NULL)
countryLevelStocksNACE <- read.csv("helperData/countryLevelStocksNACE.csv", row.names=NULL) countryLevelStocksNACE <- read.csv("helperData/countryLevelStocksNACE.csv", row.names=NULL)
nuts3LevelStocksGTAP <- read.csv("helperData/nuts3LevelStocksGTAP.csv", row.names=NULL)
countryLevelStocksGTAP <- read.csv("helperData/countryLevelStocksGTAP.csv", row.names=NULL) countryLevelStocksGTAP <- read.csv("helperData/countryLevelStocksGTAP.csv", row.names=NULL)
nuts3LevelStocksGRACE <- read.csv("helperData/nuts3LevelStocksGRACE.csv", row.names=NULL)
countryLevelStocksGRACE <- read.csv("helperData/countryLevelStocksGRACE.csv", row.names=NULL)
files <- list.files('scenarios',pattern = 'csv',recursive = T) files <- list.files('scenarios',pattern = 'csv',recursive = T)
...@@ -23,6 +25,9 @@ for(f.i in 1:length(files)){ ...@@ -23,6 +25,9 @@ for(f.i in 1:length(files)){
if(grepl('GTAP',file)){ if(grepl('GTAP',file)){
data.rel <- relData(data,nuts3LevelStocksGTAP,countryLevelStocksGTAP) data.rel <- relData(data,nuts3LevelStocksGTAP,countryLevelStocksGTAP)
write.csv(data.rel,gsub('.csv','-rel.csv',file),row.names = F) write.csv(data.rel,gsub('.csv','-rel.csv',file),row.names = F)
} else if (grepl('GRACE',file)) {
data.rel <- relData(data,nuts3LevelStocksGRACE,countryLevelStocksGRACE)
write.csv(data.rel,gsub('.csv','-rel.csv',file),row.names = F)
} else { } else {
data.rel <- relData(data,nuts3LevelStocksNACE,countryLevelStocksNACE) data.rel <- relData(data,nuts3LevelStocksNACE,countryLevelStocksNACE)
write.csv(data.rel,gsub('.csv','-rel.csv',file),row.names = F) write.csv(data.rel,gsub('.csv','-rel.csv',file),row.names = F)
......
#
# This script will map the NACE sectorsin the original
# secnarios to the GRACE sectors.
# Uses the mapping provided by Lin Ma.
#
# Benjamin Blanz 2024
#
library(readxl)
sector_mapping <- read_excel("helperData/GTAP_NACE_sector_mapping.xlsx")
sectorColPattern <- 'ALL|TOTAL|^[A-Z]$|AGR|MIN|MFG|EGW|CNS|TRD|OTP|WTP|CMN|OFI|OBS|REA|PUB|OSG|agr|coa.oil.gas|pro|ely.elc|ser|air.wtp.tran'
# NACE files for which we will map the sectors to GRACE
files <- list.files('scenarios',pattern = 'NACE.csv',recursive = T)
files <- paste0('scenarios/',files)
files[length(files)+1] <- 'helperData/countryLevelStocksNACE.csv'
files[length(files)+1] <- 'helperData/nuts3LevelStocksNACE.csv'
# Mapping of sectors
for (file in files){
cat(sprintf('GRACEifying %s...',file))
data <- read.csv(file)
names(data)[names(data)=='ALL'] <- 'TOTAL'
dataCols <- grep(sectorColPattern,names(data),perl = T)
for( i in dataCols){
data[,i] <- suppressWarnings(as.numeric(data[,i]))
}
data[is.na(data)] <- 0
dataGRACE <- data
dataGRACE[,dataCols] <- NULL
GRACEcodes <- unique(sector_mapping$`GRACE Code`)
for(GRACEcode in GRACEcodes){
dataGRACE[[GRACEcode]] <- NA
rows <- which(sector_mapping$`GRACE Code` == GRACEcode)
NACEcodes <- strsplit(paste(sector_mapping$`NACE Code`[rows],collapse = '.'),'\\.')[[1]]
if(sum(NACEcodes %in% colnames(data))==length(NACEcodes)){
if(length(NACEcodes)>1){
dataGRACE[[GRACEcode]] <- rowSums(data[,NACEcodes])
} else {
dataGRACE[[GRACEcode]] <- data[,NACEcodes]
}
}
}
write.csv(dataGRACE,gsub('NACE.csv','GRACE.csv',file),row.names = F)
cat('done\n')
}
#
# This script will map the NACE sectors in the original secnarios to the GTAP sectors.
# Uses the mapping provided by Lin Ma.
#
# Benjamin Blanz 2024
#
library(readxl)
GTAP_NACE_sector_mapping <- read_excel("helperData/GTAP_NACE_sector_mapping.xlsx")
# files for which we will map the sectors to GTAP
files <- list.files('scenarios',pattern = 'NACE.csv',recursive = T)
files <- paste0('scenarios/',files)
files[length(files)+1] <- 'helperData/countryLevelStocksNACE.csv'
files[length(files)+1] <- 'helperData/nuts3LevelStocksNACE.csv'
# Mapping of sectors
for (file in files){
cat(sprintf('GTAPifying %s...',file))
data <- read.csv(file)
names(data)[names(data)=='ALL'] <- 'TOTAL'
dataCols <- grep('ALL|TOTAL|^[A-Z]$',names(data),perl = T)
for( i in dataCols){
data[,i] <- suppressWarnings(as.numeric(data[,i]))
}
data[is.na(data)] <- 0
dataGTAP <- data
dataGTAP[,dataCols] <- NULL
for( GTAPcode.i in 1:nrow(GTAP_NACE_sector_mapping)){
dataGTAP[[GTAP_NACE_sector_mapping$`GTAP Code`[GTAPcode.i]]] <- NA
NACEcode <- GTAP_NACE_sector_mapping$`NACE Code`[GTAPcode.i]
NACEcodes <- strsplit(NACEcode,'\\.')[[1]]
if(sum(NACEcodes %in% colnames(data))==length(NACEcodes))
if(length(NACEcodes)>1){
dataGTAP[[GTAP_NACE_sector_mapping$`GTAP Code`[GTAPcode.i]]] <-
rowSums(data[,NACEcodes])
} else {
dataGTAP[[GTAP_NACE_sector_mapping$`GTAP Code`[GTAPcode.i]]] <-
data[,NACEcodes]
}
}
write.csv(dataGTAP,gsub('NACE.csv','GTAP.csv',file),row.names = F)
cat('done\n')
}
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
# Benjamin Blanz 2024 # Benjamin Blanz 2024
# #
sectorColPattern <- 'ALL|TOTAL|^[A-Z]$|AGR|MIN|MFG|EGW|CNS|TRD|OTP|WTP|CMN|OFI|OBS|REA|PUB|OSG' sectorColPattern <- 'ALL|TOTAL|^[A-Z]$|AGR|MIN|MFG|EGW|CNS|TRD|OTP|WTP|CMN|OFI|OBS|REA|PUB|OSG|agr|coa-oil-gas|pro|ely-elc|ser|air-wtp-tran'
# read scenario file with NUTS3 data #### # read scenario file with NUTS3 data ####
library(readxl) library(readxl)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment