forked from remindmodel/remind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.R
274 lines (244 loc) · 10.5 KB
/
output.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# | (C) 2006-2019 Potsdam Institute for Climate Impact Research (PIK)
# | authors, and contributors see CITATION.cff file. This file is part
# | of REMIND and licensed under AGPL-3.0-or-later. Under Section 7 of
# | AGPL-3.0, you are granted additional permissions described in the
# | REMIND License Exception, version 1.0 (see LICENSE file).
# | Contact: remind@pik-potsdam.de
##########################################################
#### REMIND Output Generation ####
##########################################################
# Version 1.0
# Type "Rscript output.R" to start the script in the command line
# Based on the Version 2.2 of same file in the MAgPIE main folder
#########################################################################################
#Write dump file when error occurs, see help to dump.frames for more information
options(error=quote({dump.frames(to.file=TRUE); traceback(); q()}))
#load landuse library
library(lucode)
###Define arguments that can be read from command line
if(!exists("source_include")) {
# if this script is not being sourced by another script but called from the command line via Rscript read the command line arguments and let the user choose the slurm options
readArgs("outputdir","output","comp","remind_dir")
}
#Setting relevant paths
if(file.exists('/iplex/01/landuse')) { #run is performed on the cluster
pythonpath <- '/iplex/01/landuse/bin/python/bin/'
latexpath <- '/iplex/01/sys/applications/texlive/bin/x86_64-linux/'
} else {
pythonpath <- ''
latexpath <- NA
}
get_line <- function(){
# gets characters (line) from the terminal of from a connection
# and stores it in the return object
if(interactive()){
s <- readline()
} else {
con <- file("stdin")
s <- readLines(con, 1, warn=FALSE)
on.exit(close(con))
}
return(s);
}
choose_folder <- function(folder,title="Please choose a folder") {
dirs <- NULL
# Detect all output folders containing fulldata.gdx
# For coupled runs please use the outcommented text block below
dirs <- sub("/fulldata.gdx","",sub("./output/","",Sys.glob(file.path(folder,"*","fulldata.gdx"))))
# DK: The following outcommented lines are specially made for listing results of coupled runs
#runs <- findCoupledruns(folder)
#dirs <- findIterations(runs,modelpath=folder,latest=TRUE)
#dirs <- sub("./output/","",dirs)
dirs <- c("all",dirs)
cat("\n\n",title,":\n\n")
cat(paste(1:length(dirs), dirs, sep=": " ),sep="\n")
cat(paste(length(dirs)+1, "Search by the pattern.\n", sep=": "))
cat("\nNumber: ")
identifier <- get_line()
identifier <- strsplit(identifier,",")[[1]]
tmp <- NULL
for (i in 1:length(identifier)) {
if (length(strsplit(identifier,":")[[i]]) > 1) tmp <- c(tmp,as.numeric(strsplit(identifier,":")[[i]])[1]:as.numeric(strsplit(identifier,":")[[i]])[2])
else tmp <- c(tmp,as.numeric(identifier[i]))
}
identifier <- tmp
# PATTERN
if(length(identifier==1) && identifier==(length(dirs)+1)){
cat("\nInsert the search pattern or the regular expression: ")
pattern <- get_line()
id <- grep(pattern=pattern, dirs[-1])
# lists all chosen directories and ask for the confirmation of the made choice
cat("\n\nYou have chosen the following directories:\n")
cat(paste(1:length(id), dirs[id+1], sep=": "), sep="\n")
cat("\nAre you sure these are the right directories?(y/n): ")
answer <- get_line()
if(answer=="y"){
return(dirs[id+1])
} else choose_folder(folder,title)
#
} else if(any(dirs[identifier] == "all")){
identifier <- 2:length(dirs)
return(dirs[identifier])
} else return(dirs[identifier])
}
choose_module <- function(Rfolder,title="Please choose an outputmodule") {
module <- gsub("\\.R$","",grep("\\.R$",list.files(Rfolder), value=TRUE))
cat("\n\n",title,":\n\n")
cat(paste(1: length(module), module, sep=": " ),sep="\n")
cat("\nNumber: ")
identifier <- get_line()
identifier <- as.numeric(strsplit(identifier,",")[[1]])
if (any(!(identifier %in% 1:length(module)))) stop("This choice (",identifier,") is not possible. Please type in a number between 1 and ",length(module))
return(module[identifier])
}
choose_mode <- function(title="Please choose the output mode") {
modes <- c("Output for single run ","Comparison across runs")
cat("\n\n",title,":\n\n")
cat(paste(1:length(modes), modes, sep=": " ),sep="\n")
cat("\nNumber: ")
identifier <- get_line()
identifier <- as.numeric(strsplit(identifier,",")[[1]])
if (identifier==1) {
comp<-FALSE
} else if (identifier==2) {
comp<-TRUE
} else {
stop("This mode is invalid. Please choose a valid mode")
}
return(comp)
}
if(exists("source_include")) {
comp <- FALSE
} else {
if(!exists("comp")) {
comp<-choose_mode("Please choose the output mode")
}
}
if (comp==TRUE) {
print("comparison")
# Select output modules if not defined by readArgs
if(!exists("output")) {
output <- choose_module("./scripts/output/comparison","Please choose the output module to be used for output generation")
}
# Select output directories if not defined by readArgs
if (!exists("outputdir")) {
if (!exists("remind_dir")) {
temp <- choose_folder("./output","Please choose the runs to be used for output generation")
outputdirs <- temp
for (i in 1:length(temp)) outputdirs[i] <- path("output",temp[i])
} else {
temp <- choose_folder(remind_dir,"Please choose the runs to be used for output generation")
outputdirs <- temp
for (i in 1:length(temp)) {
last_iteration <- max(as.numeric(sub("magpie_","",grep("magpie_",list.dirs(path(remind_dir,temp[i],"data/results")),value=T))))
outputdirs[i] <- path(remind_dir,temp[i],"data/results/",paste("magpie_",last_iteration,sep=""))
}
}
} else outputdirs <- outputdir
#Set value source_include so that loaded scripts know, that they are
#included as source (instead of a load from command line)
source_include <- TRUE
# Execute output scripts over all choosen folders
for(rout in output){
name<-paste(rout,".R",sep="")
if(file.exists(paste("scripts/output/comparison/",name,sep=""))){
print(paste("Executing",name))
tmp.env <- new.env()
tmp.error <- try(sys.source(paste("scripts/output/comparison/",name,sep=""),envir=tmp.env))
rm(tmp.env)
gc()
if(!is.null(tmp.error)) warning("Script ",name," was stopped by an error and not executed properly!")
}
}
} else {
# Select an output directory if not defined by readArgs
if(!exists("outputdir")) {
if (!exists("remind_dir")) {
temp <- choose_folder("./output","Please choose the run(s) to be used for output generation")
outputdirs <- temp
for (i in 1:length(temp)) outputdirs[i] <- path("output",temp[i])
} else {
temp <- choose_folder(remind_dir,"Please choose the runs to be used for output generation")
outputdirs <- temp
for (i in 1:length(temp)) {
last_iteration <- max(as.numeric(sub("magpie_","",grep("magpie_",list.dirs(path(remind_dir,temp[i],"data/results")),value=T))))
outputdirs[i] <- path(remind_dir,temp[i],"data/results/",paste("magpie_",last_iteration,sep=""))
}
}
} else outputdirs <- outputdir
# define slurm class or direct execution
if(!exists("source_include")) {
# if this script is not being sourced by another script but called from the command line via Rscript let the user choose the slurm options
source("scripts/start/choose_slurmConfig.R")
slurmConfig <- choose_slurmConfig()
} else {
# if this script is being sourced by another script exectue the output scripts directly without sending them to the cluster
slurmConfig <- "direct"
}
#Execute outputscripts for all choosen folders
for (outputdir in outputdirs) {
# Select an output module if not defined by readArgs
if(!exists("output")) {
output <- choose_module("./scripts/output/single","Please choose the output module to be used for output generation")
}
if(exists("cfg")) {
title <- cfg$title
gms <- cfg$gms
input <- cfg$input
revision <- cfg$revision
magpie_folder <- cfg$magpie_folder
}
# Get values of config if output.R is called standalone
if(!exists("source_include")) {
magpie_folder <- getwd()
print(path(outputdir,"config.Rdata"))
if(file.exists(path(outputdir,"config.Rdata"))) {
load(path(outputdir,"config.Rdata"))
title <- cfg$title
gms <- cfg$gms
input <- cfg$input
revision <- cfg$revision
} else {
config <- grep("\\.cfg$",list.files(outputdir), value=TRUE)
l<-readLines(path(outputdir,config))
title <- strsplit(grep("(cfg\\$|)title +<-",l,value=TRUE),"\"")[[1]][2]
gms <- list()
gms$scenarios <- strsplit(grep("(cfg\\$|)gms\\$scenarios +<-",l,value=TRUE),"\"")[[1]][2]
input <- strsplit(grep("(cfg\\$|)input +<-",l,value=TRUE),"\"")[[1]][2]
revision <- as.numeric(unlist(strsplit(grep("(cfg\\$|)revision +<-",l,value=TRUE),"<-[ \t]*"))[2])
}
}
#Set value source_include so that loaded scripts know, that they are
#included as source (instead of a load from command line)
source_include <- TRUE
cat(paste("\nStarting output generation for",outputdir,"\n\n"))
###################################################################################
# Execute R scripts
###################################################################################
for(rout in output){
name<-paste(rout,".R",sep="")
if(file.exists(paste0("scripts/output/single/",name))){
if (slurmConfig == "direct") {
# execute output script directly (without sending it to slurm)
print(paste("Executing",name))
tmp.env <- new.env()
tmp.error <- try(sys.source(paste0("scripts/output/single/",name),envir=tmp.env))
# rm(list=ls(tmp.env),envir=tmp.env)
rm(tmp.env)
gc()
if(!is.null(tmp.error)) warning("Script ",name," was stopped by an error and not executed properly!")
} else {
# send the output script to slurm
slurmcmd <- paste0("sbatch ",slurmConfig," --job-name=",outputdir," --output=",outputdir,".txt --mail-type=END --comment=REMIND --wrap=\"Rscript scripts/output/single/",rout,".R outputdir=",outputdir,"\"")
cat("Sending to slurm: ",name,"\n")
system(slurmcmd)
Sys.sleep(1)
}
}
}
# finished
cat(paste("\nFinished output generation for",outputdir,"!\n\n"))
rm(source_include)
if(!is.null(warnings())) print(warnings())
}
}