From aee62ec656abe7a69ba79f19fca47b75c0120819 Mon Sep 17 00:00:00 2001
From: David Klein <dklein@pik-potsdam.de>
Date: Tue, 14 Apr 2020 10:16:31 +0200
Subject: [PATCH] Fix bug in rds_report.R Plot supplycurves for all years not
 just one year.

---
 scripts/output/comparison/supplycurves.R | 13 +++++++------
 scripts/output/single/rds_report.R       | 22 ++++++++++++++++------
 2 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/scripts/output/comparison/supplycurves.R b/scripts/output/comparison/supplycurves.R
index 236f5e4..0ca5704 100644
--- a/scripts/output/comparison/supplycurves.R
+++ b/scripts/output/comparison/supplycurves.R
@@ -14,22 +14,23 @@ x <- readSupplycurveBio(outputdirs)
 years <- getYears(x$supplycurve)
 years <- years[years>="y2005" & years<="y2100"]
 
-years <- "y2080"
+#years <- "y2080"
 regions <- sort(getRegions(x$supplycurve))
 
 out<-swopen(template="david")
 
-  title <- paste0(years) 
-  dat <- gginput(x$supplycurve[regions,years,],scatter="type")
+for (year in years) {
+  title <- paste0(year) 
+  dat <- gginput(x$supplycurve[regions,year,],scatter="type")
   dat$year<-factor(dat$year)
 
   p <- ggplot(dat, aes(x=.value.x,y=.value.y)) +
     geom_line(aes(colour=scenario, linetype=curve)) + #geom_line(size=0.5) + 
-    geom_point(data=gginput(x$rem_point[regions,years,],scatter = "variable"),aes(x=.value.x,y=.value.y,colour=scenario)) +
-    geom_point(data=gginput(x$mag_point[regions,years,],scatter = "variable"),aes(x=.value.x,y=.value.y,colour=scenario),shape=5) +
+    geom_point(data=gginput(x$rem_point[regions,year,],scatter = "variable"),aes(x=.value.x,y=.value.y,colour=scenario)) +
+    geom_point(data=gginput(x$mag_point[regions,year,],scatter = "variable"),aes(x=.value.x,y=.value.y,colour=scenario),shape=5) +
     facet_wrap(~region) +
     ggtitle(title) + ylab("$/GJ") + xlab("EJ") + coord_cartesian(xlim=c(0,80),ylim=c(0,30))
 
   swfigure(out,print,p,sw_option="height=9,width=12")
-
+}
 swclose(out,outfile=paste0("supplycurves.pdf"),clean_output=TRUE,save_stream=FALSE)
diff --git a/scripts/output/single/rds_report.R b/scripts/output/single/rds_report.R
index 1d02912..f602fd1 100644
--- a/scripts/output/single/rds_report.R
+++ b/scripts/output/single/rds_report.R
@@ -41,11 +41,21 @@ saveRDS(q,file=rds)
 if(file.exists(runstatistics) & dir.exists(resultsarchive)) {
   stats <- list()
   load(runstatistics)
-  if(!is.null(stats$id)) {
-    saveRDS(q,file=paste0(resultsarchive,"/",stats$id,".rds"))
-    cwd <- getwd()
-    setwd(resultsarchive)
-    system("ls 1*.rds > files")
-    setwd(cwd)
+  if(is.null(stats$id)) {
+    # create an id if it does not exist (which means that statistics have not 
+    # been saved to the archive before) and save statistics to the archive
+    message("No id found in runstatistics.rda. Calling lucode::runstatistics() to create one.") 
+    stats <- lucode::runstatistics(file = runstatistics, submit = cfg$runstatistics)
+    message("Created the id ",stats$id)
+    # save stats locally (including id) otherwise it would generate a new id (and 
+    # resubmit the results and the statistics) next time rds_report is executed
+    save(stats, file=runstatistics, compress="xz")
   }
+  
+  # Save report to results archive
+  saveRDS(q,file=paste0(resultsarchive,"/",stats$id,".rds"))
+  cwd <- getwd()
+  setwd(resultsarchive)
+  system("ls 1*.rds > files")
+  setwd(cwd)
 }
-- 
GitLab