-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathplot_panel_nn.R
333 lines (281 loc) · 13.6 KB
/
plot_panel_nn.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
plot_panel_nn <- function( sitename, df, minmax=NULL, nn, dolek, filename, droughts=NULL, markdays=NULL, markdays2=NULL, markdays3=NULL, cutoff=NULL, fapar_extremes=NULL, nam_target=NULL, makepdf=TRUE ){
# library(NeuralNetTools)
library(stats)
source( "add_alpha.R" )
source( "loess_range.R" )
cols <- c( "royalblue3", "tomato" )
if (nam_target=="lue_obs_evi"||nam_target=="lue_obs_fpar"){
varnam <- "fLUE"
descr_outputset <- c(
"NN, good days (LUE ~ temperature + VPD + PPFD)",
"NN, all days (LUE ~ temperature + VPD + PPFD + soil moisture)"
)
lue <- TRUE
if (nam_target=="lue_obs_evi"){
data_fapar <- "evi"
} else {
data_fapar <- "fpar"
}
} else {
varnam <- "fGPP"
descr_outputset <- c(
"NN, good days (GPP ~ temperature + VPD + PPFD)",
"NN, all days (GPP ~ temperature + VPD + PPFD + soil moisture)"
)
lue <- FALSE
}
# print(paste("site", sitename))
##---------------------------------------------------------
## plot daily time series
##---------------------------------------------------------
magn <- 3.5
if (dolek){
ncols <- 4
order <- c(1,1,1,1,2,2,2,2)
} else {
ncols <- 2
order <- c(1,1,2,2)
}
nrows <- 2
heights <- c(1,1)*magn
widths <- rep(1.6,ncols)*magn
if (makepdf) pdf( filename, width=sum(widths), height=sum(heights) )
panel <- layout(
# matrix(c(1:(nrows*ncols)),nrows,ncols,byrow=TRUE),
matrix( order, nrows, ncols, byrow=TRUE ),
widths=widths,
heights=heights,
TRUE
)
# layout.show( panel )
##---------------------------------------------------------
## plot time series: observed and modelled GPP
##---------------------------------------------------------
par( las=1, mar=c(4,4.4,3,2) )
if (lue){
ylim <- c( 0, max( df[[ nam_target ]], df$var_nn_pot * df$ppfd * df[[ data_fapar ]], na.rm=TRUE ) )
} else {
ylim <- c( 0, max( df[[ nam_target ]], df$var_nn_pot, na.rm=TRUE ) )
}
ylab <- expression(paste("GPP (gC m"^{-2}, " d"^{-1}, ")"))
# if (lue){
# ylab <- expression(paste("LUE (gC mol"^{-1}, ")"))
# } else {
# ylab <- expression(paste("GPP (gC m"^{-2}, " d"^{-1}, ")"))
# }
plot( df$year_dec, df[[ nam_target ]],
ylim=ylim,
main=paste( sitename,sep=""),
type="n",
xlab="year", ylab=ylab, axes=FALSE,
)
axis( 1, at=unique(floor(df$year_dec)), labels=FALSE )
axis( 1, at=unique(floor(df$year_dec))+0.5, , labels=unique(floor(df$year_dec)), tck=0.0 )
axis( 2 )
box()
##----------------------------------------------------------------------------------------
## daily values
##----------------------------------------------------------------------------------------
## observed
if (lue){
lines( df$year_dec, df[[ nam_target ]] * df$ppfd * df[[ data_fapar ]] , col=add_alpha("snow4", 1.0))
# tmp <- approx( df$year_dec, df[[ nam_target ]] * df$ppfd * df[[ data_fapar ]], xout=df$year_dec)$y
# idxs <- which( !is.na(tmp) )
# lines( df$year_dec[idxs], runmed( tmp[idxs], 9), lwd=1 )
} else {
lines( df$year_dec, df[[ nam_target ]], col=add_alpha("snow4", 1.0))
}
## potential GPP = modelled without soil moisture limitation
if (lue){
lines( df$year_dec, df$var_nn_pot * df$ppfd * df[[ data_fapar ]], col=add_alpha(cols[1], 1.0), lwd=1 )
# tmp <- approx( df$year_dec, df$var_nn_pot * df$ppfd * df[[ data_fapar ]], xout=df$year_dec)$y
# idxs <- which( !is.na(tmp) )
# lines( df$year_dec[idxs], runmed( tmp[idxs], 9), col=cols[1], lwd=1 )
} else {
lines( df$year_dec, df$var_nn_pot, col=add_alpha(cols[1], 1.0), lwd=1 )
}
mtext( descr_outputset[1], col=cols[1], side=3, line=(1-1)*0.75, cex=0.6, adj=0 )
## actual GPP
if (lue){
lines( df$year_dec, df$var_nn_act * df$ppfd * df[[ data_fapar ]], col=add_alpha(cols[2], 1.0), lwd=1 )
# tmp <- approx( df$year_dec, df$var_nn_act * df$ppfd * df[[ data_fapar ]], xout=df$year_dec)$y
# idxs <- which( !is.na(tmp) )
# lines( df$year_dec[idxs], runmed( tmp[idxs], 9), col=cols[2], lwd=1 )
} else {
lines( df$year_dec, df$var_nn_act, col=add_alpha(cols[2], 1.0), lwd=1 )
}
mtext( descr_outputset[2], col=cols[2], side=3, line=(2-1)*0.75, cex=0.6, adj=0 )
##----------------------------------------------------------------------------------------
## Grey bands for droughts
##----------------------------------------------------------------------------------------
if (!is.null(droughts) && nrow(droughts)>0 ){
rect( df$year_dec[droughts$idx_start], rep( -99, nrow(droughts) ), df$year_dec[(droughts$idx_start+droughts$len-1)], rep( 99, nrow(droughts) ), col=rgb(0,0,0,0.2), border=NA )
}
if (!is.null(fapar_extremes)){
# abline( v=fapar_extremes$year_dec_start, col='red' )
# abline( v=fapar_extremes$year_dec_end, col='red' )
for (idx in 1:nrow(fapar_extremes)){
idx_start <- which.min( abs(df$year_dec-fapar_extremes$year_dec_start[idx]) )
idx_end <- which.min( abs(df$year_dec-fapar_extremes$year_dec_end[idx]) )
if (idx_start!=1 && idx_end!=1 ){
rect( df$year_dec[ idx_start ], ylim[1], df$year_dec[ idx_end ], ylim[2], col=NA, border='red', lwd=2 )
}
}
}
##---------------------------------------------------------
## plot time series: fGPP_filled or fLUE_filled (fraction of potential)
##---------------------------------------------------------
ylim <- c( 0, 1.5 )
par( las=1, mar=c(4,4.4,3,2) )
plot( df$year_dec, df[[ nam_target ]],
ylim=ylim,
type="n",
xlab="year", ylab=varnam, axes=FALSE,
)
axis( 1, at=unique(floor(df$year_dec)), labels=FALSE )
axis( 1, at=unique(floor(df$year_dec))+0.5, , labels=unique(floor(df$year_dec)), tck=0.0 )
axis( 2 )
box()
## solid line for fvar_smooth
# lines( df$year_dec, df$fvar_filled, col=add_alpha(cols[2],0.3), lwd=1 )
lines( df$year_dec, df$fvar_smooth, col=cols[2], lwd=1 ) ### this is shown in panels
# lines( df$year_dec, df$fvar, col=cols[2], lwd=1 ) ### test
## Uncertainty range
polygon( c( minmax$year_dec, rev(minmax$year_dec) ), c( smooth.spline( minmax$year_dec, minmax$fvar_min_filled, spar=0.01 )$y, rev( smooth.spline( minmax$year_dec, minmax$fvar_max_filled, spar=0.01 )$y ) ), border=NA, col=add_alpha(cols[2],0.5) )
# polygon( c( minmax$year_dec, rev(minmax$year_dec) ), c( minmax$fvar_min_filled, rev( minmax$fvar_max_filled ) ), border=NA, col=add_alpha(cols[2],0.5) )
# ## fvar based on observational soilmoisture
# if (any(!is.na(df$fvar_obs))){
# lines( df$year_dec, df$fvar_obs, col=cols[2], lwd=1, lty=2 )
# }
# tmp <- approx( df$year_dec, df$fvar_filled, xout=df$year_dec)$y
# idxs <- which( !is.na(tmp) )
# lines( df$year_dec[idxs], runmed( tmp[idxs], 5), col=cols[2], lwd=1 )
abline( h=1.0 )
if (!is.null(cutoff)) { abline( h=cutoff, lty=2 )}
mtext( varnam, col=cols[2], side=3, line=0, cex=0.6, adj=0 )
if (!is.null(droughts) && nrow(droughts)>0 ){
rect( df$year_dec[droughts$idx_start], rep( -99, nrow(droughts) ), df$year_dec[(droughts$idx_start+droughts$len-1)], rep( 99, nrow(droughts) ), col=rgb(0,0,0,0.2), border=NA )
}
## Plot periods of EVI extremes as red boxes
if (!is.null(fapar_extremes)){
# abline( v=fapar_extremes$year_dec_start, col='red' )
# abline( v=fapar_extremes$year_dec_end, col='red' )
for (idx in 1:nrow(fapar_extremes)){
idx_start <- which.min( abs(df$year_dec-fapar_extremes$year_dec_start[idx]) )
idx_end <- which.min( abs(df$year_dec-fapar_extremes$year_dec_end[idx]) )
if (idx_start!=1 && idx_end!=1 ){
rect( df$year_dec[ idx_start ], ylim[1], df$year_dec[ idx_end ], ylim[2], col=add_alpha('darkgoldenrod', 0.3), border=NA, lwd=2 )
}
}
}
## Plot time series of EVI
lines( df$year_dec, df[[ data_fapar ]], col="palegreen3", lwd=2 )
if (is.element("fet", names(data))){
##---------------------------------------------------------
## plot time series: observed and modelled ET
##---------------------------------------------------------
par( las=1, mar=c(4,4.4,3,2) )
plot( df$year_dec, df$et_obs*1e-6,
ylim=c( 0, max( df$et_obs*1e-6, df$et_nn_pot*1e-6, na.rm=TRUE ) ),
main=paste( sitename,sep=""),
type="n",
xlab="year", ylab=expression(paste("ET (MJ m"^{-2}, " d"^{-1}, ")")), axes=FALSE,
)
axis( 1, at=unique(floor(df$year_dec)), labels=FALSE )
axis( 1, at=unique(floor(df$year_dec))+0.5, , labels=unique(floor(df$year_dec)), tck=0.0 )
axis( 2 )
box()
## observed
lines( df$year_dec, df$et_obs*1e-6, col=add_alpha("snow4", 0.5))
## potential et = modelled without temp or soil moisture limitation
# if (!is.null(df$et_nn_pot)){
lines( df$year_dec, df$et_nn_pot*1e-6, col=cols[1], lwd=1 )
mtext( descr_outputset[1], col=cols[1], side=3, line=(1-1)*0.75, cex=0.6, adj=0 )
# }
## NN-predicted et
lines( df$year_dec, df$et_nn_act*1e-6, col=cols[2], lwd=1 )
mtext( descr_outputset[2], col=cols[2], side=3, line=(2-1)*0.75, cex=0.6, adj=0 )
if (!is.null(droughts)&& nrow(droughts)>0 ){
rect( df$year_dec[droughts$idx_start], rep( -99, nrow(droughts) ), df$year_dec[(droughts$idx_start+droughts$len-1)], rep( 99, nrow(droughts) ), col=rgb(1,0,0,0.2), border=NA )
}
##---------------------------------------------------------
## plot time series: Normalised ET (fraction of potential)
##---------------------------------------------------------
par( las=1, mar=c(4,4.4,3,2) )
plot( df$year_dec, df$et_obs,
ylim=c( 0, 1.5 ),
type="n",
xlab="year", ylab=expression(paste("act.-et / pot.-et")), axes=FALSE,
)
axis( 1, at=unique(floor(df$year_dec)), labels=FALSE )
axis( 1, at=unique(floor(df$year_dec))+0.5, , labels=unique(floor(df$year_dec)), tck=0.0 )
axis( 2 )
box()
## NN-predicted et
lines( df$year_dec, df$fet_filled, col=add_alpha(cols[2],0.3), lwd=1 )
lines( df$year_dec, df$fet_smooth, col=cols[2], lwd=1 )
abline( h=1.0 )
if (!is.null(cutoff)) { abline( h=cutoff, lty=2 )}
mtext( "act.-ET / pot.-ET", col=cols[2], side=3, line=0, cex=0.6, adj=0 )
if (!is.null(droughts)&& nrow(droughts)>0 ){
rect( df$year_dec[droughts$idx_start], rep( -99, nrow(droughts) ), df$year_dec[(droughts$idx_start+droughts$len-1)], rep( 99, nrow(droughts) ), col=rgb(1,0,0,0.2), border=NA )
}
}
if (is.element("wue_obs", names(data)) && is.element("wue_spline", names(data))){
##---------------------------------------------------------
## plot time series: observed and modelled wue
##---------------------------------------------------------
par( las=1, mar=c(4,4.4,3,2) )
plot( df$year_dec, df$wue_spline,
ylim=c( 0, max( df$wue_spline, na.rm=TRUE ) ),
main=paste( sitename,sep=""),
type="n",
xlab="year", ylab=expression(paste("WUE (gC MJ"^{-1}, ")")), axes=FALSE,
)
axis( 1, at=unique(floor(df$year_dec)), labels=FALSE )
axis( 1, at=unique(floor(df$year_dec))+0.5, , labels=unique(floor(df$year_dec)), tck=0.0 )
axis( 2 )
box()
## observed
lines( df$year_dec, df$wue_obs, col=add_alpha(cols[1],0.3) )
## splined observed
lines( df$year_dec, df$wue_spline, col=cols[1], lwd=2 )
# ## potential wue = modelled without temp or soil moisture limitation
# # if (!is.null(df$wue_nn_pot)){
# lines( df$year_dec, df$wue_nn_pot, col=cols[1], lwd=1 )
# mtext( descr_outputset[1], col=cols[1], side=3, line=(1-1)*0.75, cex=0.6, adj=0 )
# # }
# ## NN-predicted et
# lines( df$year_dec, df$wue_nn_act, col=cols[2], lwd=1 )
# mtext( descr_outputset[2], col=cols[2], side=3, line=(2-1)*0.75, cex=0.6, adj=0 )
if (!is.null(droughts)&& nrow(droughts)>0 ){
rect( df$year_dec[droughts$idx_start], rep( -99, nrow(droughts) ), df$year_dec[(droughts$idx_start+droughts$len-1)], rep( 99, nrow(droughts) ), col=rgb(1,0,0,0.2), border=NA )
}
if (is.element("fwue", names(data))){
##---------------------------------------------------------
## plot time series: Normalised wue (fraction of potential)
##---------------------------------------------------------
par( las=1, mar=c(4,4.4,3,2) )
plot( df$year_dec, df$fwue,
ylim=c( 0, 1.5 ),
type="n",
xlab="year", ylab=expression(paste("fWUE")), axes=FALSE,
)
axis( 1, at=unique(floor(df$year_dec)), labels=FALSE )
axis( 1, at=unique(floor(df$year_dec))+0.5, , labels=unique(floor(df$year_dec)), tck=0.0 )
axis( 2 )
box()
## NN-predicted wue
# lines( df$year_dec, df$fwue_filled, col=add_alpha(cols[2],0.3), lwd=1 )
lines( df$year_dec, df$fwue_smooth, col=add_alpha(cols[2],0.3), lwd=1 )
lines( df$year_dec, df$fwue_spline, col=cols[2], lwd=2 )
abline( h=1.0 )
if (!is.null(cutoff)) { abline( h=cutoff, lty=2 )}
mtext( "fWUE", col=cols[2], side=3, line=0, cex=0.6, adj=0 )
if (!is.null(droughts)&& nrow(droughts)>0 ){
rect( df$year_dec[droughts$idx_start], rep( -99, nrow(droughts) ), df$year_dec[(droughts$idx_start+droughts$len-1)], rep( 99, nrow(droughts) ), col=rgb(1,0,0,0.2), border=NA )
}
}
}
if (makepdf) dev.off()
}