diff --git a/scenarioProcessing/a1ProcessScenarioData.R b/scenarioProcessing/a1ProcessScenarioData.R index 928275c69e7b732a6b9733355dca1849ff9e793d..f1ca398f3c7b1802fb6ac24a9e7e06b4df3ded19 100644 --- a/scenarioProcessing/a1ProcessScenarioData.R +++ b/scenarioProcessing/a1ProcessScenarioData.R @@ -6,5 +6,6 @@ source('stockAggregation.R') source('sectorMappingNACE2GTAP.R') +source('sectorMappingNACE2GRACE.R') source('aggregateShocks.R') source('relativizeShocks.R') diff --git a/scenarioProcessing/funAggregateNuts2CNT.R b/scenarioProcessing/funAggregateNuts2CNT.R index da9f1ee24d7b4b89eae28ce13b8ec65d74669e21..4e6f158736f5832ddc4a9f5b8d34e8233d9a5a61 100644 --- a/scenarioProcessing/funAggregateNuts2CNT.R +++ b/scenarioProcessing/funAggregateNuts2CNT.R @@ -9,7 +9,7 @@ # aggregated data # 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 sectorCols <- grep(sectorColPattern,names(data),perl = T) # convert data to numeric (deals with in import error) diff --git a/scenarioProcessing/funRelData.R b/scenarioProcessing/funRelData.R index 981c275b6433ca6d9a34f09f1010acf63443edf9..2e4fad7eb6ea1448d8ce25176d824dd0a41854eb 100644 --- a/scenarioProcessing/funRelData.R +++ b/scenarioProcessing/funRelData.R @@ -17,7 +17,7 @@ # Benjamin Blanz 2024 # 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 names(data)[names(data)=='ALL'] <- 'TOTAL' names(stocksNUTS3)[names(stocksNUTS3)=='ALL'] <- 'TOTAL' diff --git a/scenarioProcessing/helperData/GTAP_NACE_sector_mapping.xlsx b/scenarioProcessing/helperData/GTAP_NACE_sector_mapping.xlsx index d2160144f58b37fd0c309d8698b79a23b824b104..4ffdf2403eb2202ca5d820a8682e26c7ae092fbc 100644 Binary files a/scenarioProcessing/helperData/GTAP_NACE_sector_mapping.xlsx and b/scenarioProcessing/helperData/GTAP_NACE_sector_mapping.xlsx differ diff --git a/scenarioProcessing/relativizeShocks.R b/scenarioProcessing/relativizeShocks.R index e012ebd6fdaf1fa1280a1eecf90de0371c7a241e..d587eed336acf1ff52c57bac3aaa03cfdb573489 100644 --- a/scenarioProcessing/relativizeShocks.R +++ b/scenarioProcessing/relativizeShocks.R @@ -8,9 +8,11 @@ source('funRelData.R') 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) +nuts3LevelStocksGTAP <- read.csv("helperData/nuts3LevelStocksGTAP.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) @@ -23,7 +25,10 @@ for(f.i in 1:length(files)){ if(grepl('GTAP',file)){ data.rel <- relData(data,nuts3LevelStocksGTAP,countryLevelStocksGTAP) write.csv(data.rel,gsub('.csv','-rel.csv',file),row.names = F) - }else{ + } else if (grepl('GRACE',file)) { + data.rel <- relData(data,nuts3LevelStocksGRACE,countryLevelStocksGRACE) + write.csv(data.rel,gsub('.csv','-rel.csv',file),row.names = F) + } else { data.rel <- relData(data,nuts3LevelStocksNACE,countryLevelStocksNACE) write.csv(data.rel,gsub('.csv','-rel.csv',file),row.names = F) } diff --git a/scenarioProcessing/sectorMappingNACE2GRACE.R b/scenarioProcessing/sectorMappingNACE2GRACE.R new file mode 100644 index 0000000000000000000000000000000000000000..dc4b022eb5f2a923886b4380a0d144641ca79ad0 --- /dev/null +++ b/scenarioProcessing/sectorMappingNACE2GRACE.R @@ -0,0 +1,46 @@ +# +# 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') +} diff --git a/scenarioProcessing/sectorMappingNACE2GTAP.R b/scenarioProcessing/sectorMappingNACE2GTAP.R new file mode 100644 index 0000000000000000000000000000000000000000..b8c24a23bebb7b8e65ada7596a8b6d3da65f2a88 --- /dev/null +++ b/scenarioProcessing/sectorMappingNACE2GTAP.R @@ -0,0 +1,44 @@ +# +# 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') +} diff --git a/scenarioProcessing/stockAggregation.R b/scenarioProcessing/stockAggregation.R index 37e4a4e5f1d3d01d134fd2e15099af84a0aef98f..5d17b7bf5a959d5c6f7716819d627bd07abd21d2 100644 --- a/scenarioProcessing/stockAggregation.R +++ b/scenarioProcessing/stockAggregation.R @@ -7,7 +7,7 @@ # 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 #### library(readxl)