Skip to content
Snippets Groups Projects
Commit 793a40f6 authored by David Klein's avatar David Klein
Browse files

Reorganize start scripts: move code that prepares input data into lock block

parent 4753946a
No related branches found
No related tags found
1 merge request!94Reorganize start scripts: move code that prepares input data into loc…
...@@ -12,41 +12,35 @@ prepare_NDC2018<-function(gdx){ ...@@ -12,41 +12,35 @@ prepare_NDC2018<-function(gdx){
library(lucode,quietly=TRUE,warn.conflicts =FALSE) library(lucode,quietly=TRUE,warn.conflicts =FALSE)
require(remind,quietly = TRUE,warn.conflicts =FALSE) require(remind,quietly = TRUE,warn.conflicts =FALSE)
############################# BASIC CONFIGURATION #############################
#Define arguments that can be read from command line
# gdx <- "fulldata.gdx"
# readArgs("fulldata.gdx")
###############################################################################
if (file.exists(gdx)) { if (file.exists(gdx)) {
emi <- reportEmi(gdx) emi <- reportEmi(gdx)
} else { } else {
stop("No gdx file found - please provide gdx from reference BAU run") stop("No gdx file found - please provide gdx from reference BAU run")
} }
regs <- setdiff(getRegions(emi),"GLO") regs <- setdiff(getRegions(emi),"GLO")
p45_BAU_reg_emi_wo_LU_bunkers <- emi[regs,seq(2005,2050,5),"Emi|Kyoto Gases excl Land-Use Change|w/o Bunkers (Mt CO2-equiv/yr)"] p45_BAU_reg_emi_wo_LU_bunkers <- emi[regs,seq(2005,2050,5),"Emi|Kyoto Gases excl Land-Use Change|w/o Bunkers (Mt CO2-equiv/yr)"]
getNames(p45_BAU_reg_emi_wo_LU_bunkers) <- NULL getNames(p45_BAU_reg_emi_wo_LU_bunkers) <- NULL
write.magpie(p45_BAU_reg_emi_wo_LU_bunkers,"modules/45_carbonprice/NDC2018/input/p45_BAU_reg_emi_wo_LU_bunkers.cs4r", comment="** description: Regional GHG emi (excl. LU and bunkers) in BAU scenario \n*** unit: Mt CO2eq/yr \n*** file created with scripts/input/create_BAU_reg_emi_wo_LU_bunkers.R")
# write.magpie(p45_BAU_reg_emi_wo_LU_bunkers,"modules/45_carbonprice/NDC2018/input/p45_BAU_reg_emi_wo_LU_bunkers.cs4r", comment="** description: Regional GHG emi (excl. LU and bunkers) in BAU scenario \n*** unit: Mt CO2eq/yr \n*** file created with scripts/input/create_BAU_reg_emi_wo_LU_bunkers.R")
shares <- as.magpie(read.csv("modules/45_carbonprice/NDC2018/input/p45_2005share_target.cs4r",skip=4,header = F))
r2025 <- NULL
r2030 <- NULL
for(r in getRegions(shares)){
## depending on which target has the higher share of emissions, assign region to group with either 2025 or 2030 target year
if(as.numeric(shares[r,2025,])>as.numeric(shares[r,2030,])){
r2025 <- c(r2025,r)
} else {
r2030 <- c(r2030,r)
}
}
# to be done: add up the shares from both time-steps to the dominant time step and copy the target value to the other time step
# (assuming that the target is similar for the other time step - likely the minor error in comparison to not considering country at all)
write.table(r2025,"modules/45_carbonprice/NDC2018/input/set_regi2025.cs4r", row.names = F,col.names=F,quote = F) shares <- as.magpie(read.csv("modules/45_carbonprice/NDC2018/input/p45_2005share_target.cs4r",skip=4,header = F))
write.table(r2030,"modules/45_carbonprice/NDC2018/input/set_regi2030.cs4r", row.names = F,col.names=F,quote = F) r2025 <- NULL
r2030 <- NULL
for(r in getRegions(shares)){
## depending on which target has the higher share of emissions, assign region to group with either 2025 or 2030 target year
if(as.numeric(shares[r,2025,])>as.numeric(shares[r,2030,])){
r2025 <- c(r2025,r)
} else {
r2030 <- c(r2030,r)
}
}
# to be done: add up the shares from both time-steps to the dominant time step and copy the target value to the other time step
# (assuming that the target is similar for the other time step - likely the minor error in comparison to not considering country at all)
write.table(r2025,"modules/45_carbonprice/NDC2018/input/set_regi2025.cs4r", row.names = F,col.names=F,quote = F)
write.table(r2030,"modules/45_carbonprice/NDC2018/input/set_regi2030.cs4r", row.names = F,col.names=F,quote = F)
} }
...@@ -176,24 +176,12 @@ prepare_and_run <- function() { ...@@ -176,24 +176,12 @@ prepare_and_run <- function() {
# Check configuration for consistency # Check configuration for consistency
cfg <- check_config(cfg, reference_file="config/default.cfg", settings_config = "config/settings_config.csv") cfg <- check_config(cfg, reference_file="config/default.cfg", settings_config = "config/settings_config.csv")
# Check for compatibility with subsidizeLearning
###-------- do update of input files based on previous runs if applicable ------###
if(!is.null(cfg$gms$carbonprice) && (cfg$gms$carbonprice == "NDC2018")){
source("scripts/input/prepare_NDC2018.R")
prepare_NDC2018(as.character(cfg$files2export$start["input_ref.gdx"]))
}
## the following is outcommented because by now it has to be done by hand ( currently only one gdx is handed to the next run, so it is impossible to fix to one run and use the tax from another run)
## Update CO2 tax information for exogenous carbon price runs with the same CO2 price as a previous run
#if(!is.null(cfg$gms$carbonprice) && (cfg$gms$carbonprice == "ExogSameAsPrevious")){
# source("scripts/input/create_ExogSameAsPrevious_CO2price_file.R")
# create_ExogSameAsPrevious_CO2price_file(as.character(cfg$files2export$start["input_ref.gdx"]))
#}
#AJS
if ( (cfg$gms$optimization != 'nash') & (cfg$gms$subsidizeLearning == 'globallyOptimal') ) { if ( (cfg$gms$optimization != 'nash') & (cfg$gms$subsidizeLearning == 'globallyOptimal') ) {
cat("Only optimization='nash' is compatible with subsudizeLearning='globallyOptimal'. Switching subsidizeLearning to 'off' now. \n") cat("Only optimization='nash' is compatible with subsudizeLearning='globallyOptimal'. Switching subsidizeLearning to 'off' now. \n")
cfg$gms$subsidizeLearning = 'off' cfg$gms$subsidizeLearning = 'off'
} }
# reportCEScalib only works with the calibrate module # reportCEScalib only works with the calibrate module
if ( cfg$gms$CES_parameters != "calibrate" ) cfg$output <- setdiff(cfg$output,"reportCEScalib") if ( cfg$gms$CES_parameters != "calibrate" ) cfg$output <- setdiff(cfg$output,"reportCEScalib")
...@@ -202,21 +190,6 @@ prepare_and_run <- function() { ...@@ -202,21 +190,6 @@ prepare_and_run <- function() {
stop("This title is too long or the name contains dots - GAMS would not tolerate this, and quit working at a point where you least expect it. Stopping now. ") stop("This title is too long or the name contains dots - GAMS would not tolerate this, and quit working at a point where you least expect it. Stopping now. ")
} }
# Calculate CES configuration string
cfg$gms$cm_CES_configuration <- paste0("stat_",cfg$gms$stationary,"-",
"indu_",cfg$gms$industry,"-",
"buil_",cfg$gms$buildings,"-",
"tran_",cfg$gms$transport,"-",
"POP_", cfg$gms$cm_POPscen, "-",
"GDP_", cfg$gms$cm_GDPscen, "-",
"Kap_", cfg$gms$capitalMarket, "-",
"Reg_", substr(regionscode(cfg$regionmapping),1,10))
# write name of corresponding CES file to datainput.gms
replace_in_file(file = "./modules/29_CES_parameters/load/datainput.gms",
content = paste0('$include "./modules/29_CES_parameters/load/input/',cfg$gms$cm_CES_configuration,'.inc"'),
subject = "CES INPUT")
# adjust GDPpcScen based on GDPscen # adjust GDPpcScen based on GDPscen
cfg$gms$c_GDPpcScen <- gsub("gdp_","",cfg$gms$cm_GDPscen) cfg$gms$c_GDPpcScen <- gsub("gdp_","",cfg$gms$cm_GDPscen)
...@@ -228,7 +201,7 @@ prepare_and_run <- function() { ...@@ -228,7 +201,7 @@ prepare_and_run <- function() {
system("find ./core/magicc/ -type f | xargs dos2unix -q") system("find ./core/magicc/ -type f | xargs dos2unix -q")
# Set source_include so that loaded scripts know they are included as # Set source_include so that loaded scripts know they are included as
# source (instead a load from command line) # source (instead of being executed from the command line)
source_include <- TRUE source_include <- TRUE
################## M O D E L L O C K ################################### ################## M O D E L L O C K ###################################
...@@ -237,8 +210,40 @@ prepare_and_run <- function() { ...@@ -237,8 +210,40 @@ prepare_and_run <- function() {
on.exit(model_unlock(lock_id, oncluster=on_cluster)) on.exit(model_unlock(lock_id, oncluster=on_cluster))
################## M O D E L L O C K ################################### ################## M O D E L L O C K ###################################
# If report and scenname are supplied the data of this scenario in the report will be converted to REMIND input ###########################################################
# Used for REMIND-MAgPIE coupling ### PROCESSING INPUT DATA ###################### START ####
###########################################################
# update input files based on previous runs if applicable
# ATTENTION: modifying gms files
if(!is.null(cfg$gms$carbonprice) && (cfg$gms$carbonprice == "NDC2018")){
source("scripts/input/prepare_NDC2018.R")
prepare_NDC2018(as.character(cfg$files2export$start["input_ref.gdx"]))
}
## the following is outcommented because by now it has to be done by hand ( currently only one gdx is handed to the next run, so it is impossible to fix to one run and use the tax from another run)
## Update CO2 tax information for exogenous carbon price runs with the same CO2 price as a previous run
#if(!is.null(cfg$gms$carbonprice) && (cfg$gms$carbonprice == "ExogSameAsPrevious")){
# source("scripts/input/create_ExogSameAsPrevious_CO2price_file.R")
# create_ExogSameAsPrevious_CO2price_file(as.character(cfg$files2export$start["input_ref.gdx"]))
#}
# Calculate CES configuration string
cfg$gms$cm_CES_configuration <- paste0("stat_",cfg$gms$stationary,"-",
"indu_",cfg$gms$industry,"-",
"buil_",cfg$gms$buildings,"-",
"tran_",cfg$gms$transport,"-",
"POP_", cfg$gms$cm_POPscen, "-",
"GDP_", cfg$gms$cm_GDPscen, "-",
"Kap_", cfg$gms$capitalMarket, "-",
"Reg_", substr(regionscode(cfg$regionmapping),1,10))
# write name of corresponding CES file to datainput.gms
replace_in_file(file = "./modules/29_CES_parameters/load/datainput.gms",
content = paste0('$include "./modules/29_CES_parameters/load/input/',cfg$gms$cm_CES_configuration,'.inc"'),
subject = "CES INPUT")
# If a path to a MAgPIE report is supplied use it as REMIND intput (used for REMIND-MAgPIE coupling)
# ATTENTION: modifying gms files
if (!is.null(cfg$pathToMagpieReport)) { if (!is.null(cfg$pathToMagpieReport)) {
getReportData(path_to_report = cfg$pathToMagpieReport,inputpath_mag=cfg$gms$biomass,inputpath_acc=cfg$gms$agCosts) getReportData(path_to_report = cfg$pathToMagpieReport,inputpath_mag=cfg$gms$biomass,inputpath_acc=cfg$gms$agCosts)
} }
...@@ -255,12 +260,7 @@ prepare_and_run <- function() { ...@@ -255,12 +260,7 @@ prepare_and_run <- function() {
if(is.null(cfg$model)) cfg$model <- "main.gms" if(is.null(cfg$model)) cfg$model <- "main.gms"
manipulateConfig(cfg$model, cfg$gms) manipulateConfig(cfg$model, cfg$gms)
########################################################################################################### ######## declare functions for updating information ####
############# PROCESSING INPUT DATA ###################### START ##########################################
###########################################################################################################
########## declare functions for updating information ################
update_info <- function(regionscode,revision) { update_info <- function(regionscode,revision) {
subject <- 'VERSION INFO' subject <- 'VERSION INFO'
...@@ -324,7 +324,7 @@ prepare_and_run <- function() { ...@@ -324,7 +324,7 @@ prepare_and_run <- function() {
replace_in_file('core/sets.gms',content,"SETS",comment="***") replace_in_file('core/sets.gms',content,"SETS",comment="***")
} }
############### download and distribute input data ################### ############ download and distribute input data ########
# check wheather the regional resolution and input data revision are outdated and update data if needed # check wheather the regional resolution and input data revision are outdated and update data if needed
if(file.exists("input/source_files.log")) { if(file.exists("input/source_files.log")) {
input_old <- readLines("input/source_files.log")[1] input_old <- readLines("input/source_files.log")[1]
...@@ -341,7 +341,7 @@ prepare_and_run <- function() { ...@@ -341,7 +341,7 @@ prepare_and_run <- function() {
debug = FALSE) debug = FALSE)
} }
##################### update information ############################# ############ update information ########################
# update_info, which regional resolution and input data revision in cfg$model # update_info, which regional resolution and input data revision in cfg$model
update_info(regionscode(cfg$regionmapping),cfg$revision) update_info(regionscode(cfg$regionmapping),cfg$revision)
# update_sets, which is updating the region-depending sets in core/sets.gms # update_sets, which is updating the region-depending sets in core/sets.gms
...@@ -349,11 +349,11 @@ prepare_and_run <- function() { ...@@ -349,11 +349,11 @@ prepare_and_run <- function() {
map <- read.csv(cfg$regionmapping,sep=";") map <- read.csv(cfg$regionmapping,sep=";")
update_sets(map) update_sets(map)
########################################################################################################### ########################################################
############# PROCESSING INPUT DATA ###################### END ############################################ ### PROCESSING INPUT DATA ###################### END ###
########################################################################################################### ########################################################
############# ADD MODULE INFO IN SETS ###################### START ####################################### ### ADD MODULE INFO IN SETS ############# START #######
content <- NULL content <- NULL
modification_warning <- c( modification_warning <- c(
'*** THIS CODE IS CREATED AUTOMATICALLY, DO NOT MODIFY THESE LINES DIRECTLY', '*** THIS CODE IS CREATED AUTOMATICALLY, DO NOT MODIFY THESE LINES DIRECTLY',
...@@ -366,7 +366,7 @@ prepare_and_run <- function() { ...@@ -366,7 +366,7 @@ prepare_and_run <- function() {
content <- c(content,paste0(" ",getModules("modules/")[,"name"]," . %",getModules("modules/")[,"name"],"%")) content <- c(content,paste0(" ",getModules("modules/")[,"name"]," . %",getModules("modules/")[,"name"],"%"))
content <- c(content,' /',';') content <- c(content,' /',';')
replace_in_file('core/sets.gms',content,"MODULES",comment="***") replace_in_file('core/sets.gms',content,"MODULES",comment="***")
############# ADD MODULE INFO IN SETS ###################### END ######################################### ### ADD MODULE INFO IN SETS ############# END #########
# choose which conopt files to copy # choose which conopt files to copy
cfg$files2export$start <- sub("conopt3",cfg$gms$cm_conoptv,cfg$files2export$start) cfg$files2export$start <- sub("conopt3",cfg$gms$cm_conoptv,cfg$files2export$start)
...@@ -400,9 +400,6 @@ prepare_and_run <- function() { ...@@ -400,9 +400,6 @@ prepare_and_run <- function() {
################## M O D E L U N L O C K ################################### ################## M O D E L U N L O C K ###################################
setwd(cfg$results_folder) setwd(cfg$results_folder)
###########################################################################################################################################################
#################################################### Copied from submit.R #################################################################################
###########################################################################################################################################################
# Function to create the levs.gms, fixings.gms, and margs.gms files, used in # Function to create the levs.gms, fixings.gms, and margs.gms files, used in
# delay scenarios. # delay scenarios.
...@@ -797,10 +794,6 @@ prepare_and_run <- function() { ...@@ -797,10 +794,6 @@ prepare_and_run <- function() {
outputdir <- cfg$results_folder outputdir <- cfg$results_folder
sys.source("output.R",envir=new.env()) sys.source("output.R",envir=new.env())
###########################################################################################################################################################
###########################################################################################################################################################
###########################################################################################################################################################
end_time <- Sys.time() end_time <- Sys.time()
# Save run statistics to local file # Save run statistics to local file
cat("Saving start_time and end_time to runstatistics.rda\n") cat("Saving start_time and end_time to runstatistics.rda\n")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment