Newer
Older
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
# | (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
############################# LOAD LIBRARIES #############################
library(magclass, quietly = TRUE,warn.conflicts =FALSE)
library(luplot, quietly = TRUE,warn.conflicts =FALSE)
library(lusweave, quietly = TRUE,warn.conflicts =FALSE)
library(lucode, quietly = TRUE,warn.conflicts =FALSE)
library(gdx, quietly = TRUE,warn.conflicts =FALSE)
library(magpie, quietly = TRUE,warn.conflicts =FALSE)
library(remind, quietly = TRUE,warn.conflicts =FALSE)
library(gtools, quietly = TRUE,warn.conflicts =FALSE)
############################# BASIC CONFIGURATION #############################
runs <- NULL
readArgs("runs")
###############################################################################
############################# DEFINE FUNCTIONS ###########################
readfuelex <- function(gdx,enty) {
out <- readGDX(gdx,name="vm_fuExtr", format="first_found", field="l")[,,enty]
out <- collapseNames(out)
return(out)
}
readprodPE <- function(gdx,enty) {
out <- readGDX(gdx,name="vm_prodPe", format="first_found", field="l")[,,enty]
out <- collapseNames(out)
return(out)
}
readshift <- function(gdx) {
out <- readGDX(gdx,name="p30_pebiolc_pricshift", format="first_found")
getNames(out) <- "pebiolc_priceshift" # hab ich mir ausgedacht, kann natuerlich alles andere sein, wird durch collapseNames sowieso geloescht
out <- collapseNames(out)
return(out)
}
readbioprice <- function(gdx,name) {
out <- readGDX(gdx, name, format="first_found")
getNames(out) <- "pebiolc_pricemag" # hab ich mir ausgedacht, kann natuerlich alles andere sein, wird durch collapseNames sowieso geloescht
out <- collapseNames(out)
return(out)
}
# function to read parameter from gdx file
readpar <- function(gdx,name) {
out <- readGDX(gdx, name, format="first_found")
#getNames(out) <- "dummy" # something has to be here, will be removed by collapseNames anyway
#out <- collapseNames(out)
return(out)
}
# function to read variable from gdx file
readvar <- function(gdx,name,enty=NULL) {
if (is.null(enty)) {
out <- readGDX(gdx,name, format="first_found", field="l")
getNames(out) <- "dummy" # something has to be here, will be removed by collapseNames anyway
} else {
out <- readGDX(gdx,name=name, format="first_found", field="l")[,,enty]
}
out <- collapseNames(out)
return(out)
}
# aufblasen auf alle jahre mit in die obige funktion, sodass man immer mult und shift mit vollen dimensionen zurueckbekommt.
# diese dann fuer jede region ueber der zeit fuer alle iterationen plotten
# If input (x) is not defined for years copy single value for all given years
fillyears <- function (x,years) {
if (fulldim(x)[[1]][2]==1){
a<-new.magpie(getRegions(x),years,getNames(x))
for (i in years) {
for (r in getRegions(a)) {
a[r,i,]<-as.vector(x[r,1,])
}
}
x<-a
}
return(x)
}
# The main plot function
plot_iterations <- function(runname) {
############################ FIND GDX FILES #################################
cat(paste0("Searching for reportings for ",runname,"\n"))
gdx_path <- Sys.glob(paste0(runname,"-rem-*/fulldata.gdx"))
gdx_path <- rev(mixedsort(gdx_path)) # sort runs from 1,10,2,20 to 1,2,10,20
cat("The following reportings were found:\n")
print(gdx_path)
# Read runnames and use them to name the rows of gdx_path
outputdirs <- sub("/fulldata.gdx","",gdx_path)
if(is.null(outputdirs)) {
return("No gdx files found")
}
scenNames_path <- path(outputdirs,"config.Rdata")
scenNames <- c()
for (i in scenNames_path) {
load(i)
scenNames[i] <- cfg$title
}
names(gdx_path) <- scenNames
######################### COMMON SETTINGS ############################
TWa2EJ <- 31.5576 # TWa to EJ (1 a = 365.25*24*3600 s = 31557600 s)
txtsiz <- 10
r <- c("SSA","CHA","EUR","NEU","IND","JPN","LAM","MEA","OAS","CAZ","REF","USA","GLO")
y <- paste0("y",2000+10*(1:10))
years <- c("y2005","y2010","y2015","y2020","y2025","y2030","y2035","y2040","y2045","y2050","y2055","y2060","y2070","y2080","y2090","y2100") # used for fillyears
r_sub <- setdiff(c("SSA","CHA","EUR","IND","LAM","OAS","CAZ","REF","USA"),"GLO")
sm_tdptwyr2dpgj <- 31.71 # multipl. factor to convert (TerraDollar per TWyear) to (Dollar per GJoule)
######################### IMPORT AND PLOT DATA #######################
### PRICES (MAgPIE) OF PURPOSE GROWN BIOENERGY ###
price <- read_all(gdx_path,readbioprice,name="p30_pebiolc_pricemag",as.list=FALSE)
price <- price / TWa2EJ * 1000
price <- mbind(price,new.magpie("GLO",getYears(price),getNames(price),fill=c(0)))
getNames(price) <- gsub(".*rem-","",getNames(price))
v <- paste(runname,"Price|Biomass|MAgPIE (US$2005/GJ)",sep="\n")
p_price_mag <- magpie2ggplot2(price[r,y,],scenario=1,
group=NULL,ylab="$/GJ",color="Scenario",facet_x="Region",show_grid=TRUE,title=v,
scales="free_y",text_size=10,ncol=4,pointwidth=1,linewidth=1,
legend_position="right")
### LUC EMISSIONS (MAgPIE) ###
emi <- read_all(gdx_path,readpar,name="p_macBaseMagpie",as.list=FALSE)[,,"co2luc"]*1000*44/12
emi <- mbind(emi,dimSums(emi,dim=1))
getNames(emi) <- gsub(".*rem-","",getNames(emi))
v <- paste(runname,"Emissions|CO2|Land Use (Mt CO2/yr)",sep="\n")
p_emi_mag <- magpie2ggplot2(emi[r,y,],scenario=1,
group=NULL,ylab="Mt CO2/yr",color="Scenario",facet_x="Region",show_grid=TRUE,title=v,
scales="free_y",text_size=10,ncol=4,pointwidth=1,linewidth=1,
legend_position="right")
### DEMAND FOR PURPOSE GROWN BIOENERGY ###
fuelex <- read_all(gdx_path,readfuelex,enty="pebiolc",as.list=FALSE)
fuelex_bio <- collapseNames(fuelex[,,"1"]) * TWa2EJ
fuelex_bio <- mbind(fuelex_bio,dimSums(fuelex_bio,dim=1))
getNames(fuelex_bio) <- gsub(".*rem-","",getNames(fuelex_bio))
v <- paste(runname,"Primary Energy Production|Biomass|Energy Crops (EJ/yr)",sep="\n")
p_fuelex <- magpie2ggplot2(fuelex_bio[r,y,],scenario=1,
group=NULL,ylab="EJ/yr",color="Scenario",facet_x="Region",show_grid=TRUE,title=v,
scales="free_y",text_size=10,ncol=4,pointwidth=1,linewidth=1,
legend_position="right")
p_it_fuelex <- magpie2ggplot2(fuelex_bio[r,y,],scenario=1,group="Year",ylab="EJ/yr",color="Year",xaxis="Scenario",facet_x="Region",show_grid=TRUE,title=v,
scales="free_y",text_size=10,ncol=4,pointwidth=1,linewidth=1,asDate=FALSE,legend_position="right")
p_it_fuelex_fix <- magpie2ggplot2(fuelex_bio[r,y,]["GLO",,,invert=TRUE],scenario=1,group="Year",ylab="EJ/yr",color="Year",xaxis="Scenario",facet_x="Region",show_grid=TRUE,title=v,
scales="fixed",text_size=10,ncol=4,pointwidth=1,linewidth=1,asDate=FALSE,legend_position="right")
p_it_fuelex_2060 <- magpie2ggplot2(fuelex_bio[r,"y2060",],scenario=1,
geom="bar",fill="Data1",stack=T,facet_x="Region",xaxis="Scenario",ylab="EJ/yr",
title=paste0(v," in 2060"),xlab="Scenario",ncol=4)
### DEMAND
fuelex <- read_all(gdx_path,readprodPE,enty="pebiolc",as.list=FALSE)
fuelex_bio <- collapseNames(fuelex) * TWa2EJ
fuelex_bio <- mbind(fuelex_bio,dimSums(fuelex_bio,dim=1))
getNames(fuelex_bio) <- gsub(".*rem-","",getNames(fuelex_bio))
v <- paste(runname,"PE|Biomass|Modern (EJ/yr)",sep="\n")
p_demPE <- magpie2ggplot2(fuelex_bio[r,y,],scenario=1,
group=NULL,ylab="EJ/yr",color="Scenario",facet_x="Region",show_grid=TRUE,title=v,
scales="free_y",text_size=10,ncol=4,pointwidth=1,linewidth=1,
legend_position="right")
p_it_demPE <- magpie2ggplot2(fuelex_bio[r,y,],scenario=1,group="Year",ylab="EJ/yr",color="Year",xaxis="Scenario",facet_x="Region",show_grid=TRUE,title=v,
scales="free_y",text_size=10,ncol=4,pointwidth=1,linewidth=1,asDate=FALSE,legend_position="right")
### PRICE SHIFT FACTOR IN 2060 ###
shift <- read_all(gdx_path,readshift,as.list=FALSE)* sm_tdptwyr2dpgj
shift <- mbind(shift,new.magpie("GLO",getYears(shift),getNames(shift),fill=c(0)))
shift <- fillyears(shift,years)
getNames(shift) <- gsub(".*rem-","",getNames(shift))
v <- paste(runname,"Price|Biomass|Shiftfactor",sep="\n")
p_shift_2060 <- magpie2ggplot2(shift[r,"y2060",],scenario=1,
geom="bar",fill="Data1",stack=T,facet_x="Region",xaxis="Scenario",ylab="[-]",
title=paste0(v," in 2060"),xlab="Scenario",ncol=4)
### Price shift and mult factor over time ###
v_shift <- read_all(gdx_path,readvar,name="v30_priceshift",as.list=FALSE) * sm_tdptwyr2dpgj
v_shift <- fillyears(v_shift,years) # If there is no year dimension add it
getNames(v_shift) <- gsub(".*rem-","",getNames(v_shift))
p_shift <- magpie2ggplot2(v_shift[r_sub,years,],geom='line',group=NULL,
ylab='$/GJ',color='Data1',#linetype="Data2",
scales='free',show_grid=TRUE,ncol=3,text_size=txtsiz,#ylim=y_limreg,
title=paste(runname,"Price shift",sep="\n"))
v_mult <- read_all(gdx_path,readvar,name="v30_pricemult",as.list=FALSE)
v_mult <- fillyears(v_mult,years) # If there is no year dimension add it
#y_limreg <- c(0,max(v_mult[,years,]))
getNames(v_mult) <- gsub(".*rem-","",getNames(v_mult))
p_mult <- magpie2ggplot2(v_mult[r_sub,years,],geom='line',group=NULL,
ylab='',color='Data1',#linetype="Data2",
scales='free_y',show_grid=TRUE,ncol=3,text_size=txtsiz,#ylim=y_limreg,
title=paste(runname,"Price mult factor",sep="\n"))
######################### PRINT TO PDF ################################
out<-swopen(template="/home/dklein/scripts/template.tex")
swfigure(out,print,p_price_mag,sw_option="height=9,width=16")
swfigure(out,print,p_fuelex,sw_option="height=9,width=16")
swfigure(out,print,p_it_fuelex,sw_option="height=9,width=16")
swfigure(out,print,p_it_fuelex_fix,sw_option="height=9,width=16")
swfigure(out,print,p_it_demPE,sw_option="height=9,width=16")
swfigure(out,print,p_it_fuelex_2060,sw_option="height=9,width=16")
swfigure(out,print,p_emi_mag,sw_option="height=9,width=16")
swfigure(out,print,p_shift,sw_option="height=9,width=16")
swfigure(out,print,p_shift_2060,sw_option="height=9,width=16")
swfigure(out,print,p_mult,sw_option="height=9,width=16")
filename <- paste0(runname,"-",length(scenNames))
swclose(out,outfile=filename,clean_output=TRUE,save_stream=FALSE)
file.remove(paste0(filename,c(".log",".out")))
return("Done\n")
}
wdnow <- getwd()
setwd("../../../output/")
# Searching for runs to plot iterations for
if (is.null(runs)) {
cat("\nNo run specified by user. Searching for all runs available in this folder:\n")
# Find which runs were performed by searching for all files that contain "-rem-"
runs <- Sys.glob("*-rem-*/fulldata.gdx")
# keep directories only (filter out files)
#runs <- runs[file.info(runs)[,"isdir"]]
# Remove "-rem-*" from the folder names and remove remaining double elements to yield the pure runname
runs <- unique(sub("-rem-[0-9]+/fulldata.gdx","",runs))
print(runs)
cat("\n\n")
}
# Plot iterations
for (runname in runs) {
cat(paste0("##################### ",runname," #################################\n"))
ret <- plot_iterations(runname)
cat(ret)
}
setwd(wdnow)