-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfunction_nn_getfail_fluxnet2015.R
329 lines (275 loc) · 12.7 KB
/
function_nn_getfail_fluxnet2015.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
nn_getfail_fluxnet <- function( sitename, code=NA, recalc=TRUE, nam_target="lue_obs_evi", use_weights=ifelse( nam_target=="lue_obs_evi", TRUE, FALSE ), use_fapar=FALSE, testprofile=FALSE, makepdf=TRUE, verbose=FALSE ){
# ## XXX debug----------------
# sitename = "AU-Dry"
# nam_target = "lue_obs_evi"
# use_weights= FALSE
# use_fapar = FALSE
# recalc = TRUE
# testprofile= TRUE
# code = 1
# makepdf = FALSE
# verbose = TRUE
# ##--------------------------
require(dplyr)
source( "analyse_modobs.R" )
source( "plot_panel_nn.R" )
nn_fluxnet <- list()
## check and override if necessary
if ( nam_target=="lue_obs" || nam_target=="lue_obs_evi" || nam_target=="lue_obs_fpar" ){
plotlue <- TRUE
if (nam_target=="lue_obs_evi"){
fapar_data <- "evi"
} else if (nam_target=="lue_obs_fpar"){
fapar_data <- "fpar"
}
if (use_fapar){
print("WARNING: setting use_fapar to FALSE")
use_fapar <- FALSE
}
}
## identifier for output files
if (use_fapar){
if (nam_target=="lue_obs_evi"){
char_fapar <- "_withEVI"
} else if (nam_target=="lue_obs_fpar"){
char_fapar <- "_withFPAR"
} else {
print("ERROR: PROVIDE VALID FAPAR DATA!")
}
} else {
char_fapar <- ""
}
if (use_weights){
char_wgt <- "_wgt"
} else {
char_wgt <- ""
}
filn <- paste( "nn_fluxnet2015_", sitename, "_", nam_target, char_wgt, char_fapar, ".Rdata", sep="" )
if (testprofile){
dir <- paste( "./data/fvar/", sep="" )
} else {
dir <- paste( myhome, "/data/nn_fluxnet/fvar/", sep="" )
}
avl <- TRUE
failed_before <- FALSE
path <- paste( dir, filn, sep="" )
if (verbose) print( paste( "reading file", path ) )
if (file.exists( path ) ){
load( path )
} else {
avl <- FALSE
}
if (is.na(code)) code <- 9999
if ( avl && code !=0 ){
##------------------------------------------------
## "detatch"
##------------------------------------------------
nice <- nn_fluxnet[[ sitename ]]$nice
minmax <- nn_fluxnet[[ sitename ]]$minmax
droughts <- nn_fluxnet[[ sitename ]]$droughts
out_evianomalies <- nn_fluxnet[[ sitename ]]$out_evianomalies
cutoff <- nn_fluxnet[[ sitename ]]$cutoff
varnams_swc <- nn_fluxnet[[ sitename ]]$varnams_swc
varnams_swc_obs <- nn_fluxnet[[ sitename ]]$varnams_swc_obs
if (recalc){
##------------------------------------------------
## Determine wether algorithm failed
## Classify as failed depending on the following criteria:
## * during droughts NNgood is lower than NNall
## * R2 of NNall vs. observed
## * R2 of NNgood vs. observed during good days
## * RMSE of NNgood vs. NNall during good days
##------------------------------------------------
droughtdays <- nice %>% dplyr::filter( is_drought_byvar_recalc )
nondrgtdays <- nice %>% dplyr::filter( !is_drought_byvar_recalc )
# ## * during droughts NNgood is lower than NNall
# if ( nrow(droughtdays) > 0 ){
# if ( mean( droughtdays$var_nn_pot, na.rm=TRUE ) < mean( droughtdays$var_nn_act, na.rm=TRUE ) ) failed <- TRUE
# }
## * R2 of NNall
mod <- nice$var_nn_act * nice$ppfd * nice$evi
obs <- nice[[ nam_target ]] * nice$ppfd * nice$evi
out_NNall <- analyse_modobs(
mod,
obs,
do.plot=TRUE,
plot.title=paste( "NN all", sitename ),
nrcol=1,
plot.fil=paste( "fig_nn_fluxnet2015/modobs_alldays/modobs_alldays", sitename, ".pdf", sep="")
)
# if ( out_NNall$prmse > 60 || out_NNall$rsq < 0.3 ) failed <- TRUE
## * R2 of NNgood vs. observed during good days
mod <- nondrgtdays$var_nn_pot * nondrgtdays$ppfd * nondrgtdays$evi
obs <- nondrgtdays[[ nam_target ]] * nondrgtdays$ppfd * nondrgtdays$evi
out_NNgood <- analyse_modobs(
mod,
obs,
do.plot=TRUE,
plot.title=paste( "NN good, good days", sitename ),
nrcol=1,
plot.fil=paste( "fig_nn_fluxnet2015/modobs_gooddays/modobs_gooddays", sitename, ".pdf", sep="")
)
# if ( out_NNgood$prmse > 60 || out_NNgood$rsq < 0.3 ) failed <- TRUE
## * R2 of NNall during bad days has no big bias
if ( nrow(droughtdays) > 0 && nrow(droughtdays)>0.02*nrow(nondrgtdays) ){
mod <- droughtdays$var_nn_act * droughtdays$ppfd * droughtdays$evi
obs <- droughtdays[[ nam_target ]] * droughtdays$ppfd * droughtdays$evi
out_NNallbad <- analyse_modobs(
mod,
obs,
do.plot=TRUE,
plot.title=paste( "NN all", sitename ),
nrcol=1,
plot.fil=paste( "fig_nn_fluxnet2015/modobs_alldays/modobs_alldays_bad", sitename, ".pdf", sep="")
)
# if ( out_NNallbad$prmse > 70 || out_NNallbad$rsq < 0.3 ) failed <- TRUE
} else {
out_NNallbad <- list( prmse=NA, rmse=NA, rsq=NA, ptoohi=NA, pbias=NA )
}
## * RMSE of NNgood vs. NNall during good days
out_NNNN <- analyse_modobs(
nondrgtdays$var_nn_act * nondrgtdays$ppfd * nondrgtdays$evi,
nondrgtdays$var_nn_pot * nondrgtdays$ppfd * nondrgtdays$evi,
do.plot=TRUE,
plot.title=paste( "NN pot vs NN act, good days", sitename ),
nrcol=1,
plot.fil=paste( "fig_nn_fluxnet2015/pot_vs_act_NN/pot_vs_act_NN", sitename, ".pdf", sep="")
)
# if ( out_NNNN$prmse > 15 ) failed <- TRUE
stats <- list( out_NNall=out_NNall, out_NNgood=out_NNgood, out_NNNN=out_NNNN, out_NNallbad=out_NNallbad )
} else {
stats <- nn_fluxnet[[ sitename ]]$stats
}
##------------------------------------------------
## Determine wether algorithm failed
## Classify as failed depending on the following criteria:
## * during droughts NNgood is lower than NNall
## * R2 of NNall vs. observed
## * R2 of NNgood vs. observed during good days
## * RMSE of NNgood vs. NNall during good days
##------------------------------------------------
failed <- FALSE
sc_lev <- NA
ndays <- nice %>% dplyr::filter( !is.na(fvar) ) %>% nrow()
droughtdays <- nice %>% dplyr::filter( is_drought_byvar_recalc )
nondrgtdays <- nice %>% dplyr::filter( !is_drought_byvar_recalc )
## * during droughts NNgood is lower than NNall
if ( nrow(droughtdays) > 0 ){
if ( mean( droughtdays$var_nn_pot, na.rm=TRUE ) < mean( droughtdays$var_nn_act, na.rm=TRUE ) ) { failed <- TRUE; sc_lev <- 2 }
}
## * R2 of NNall
if ( stats$out_NNall$rmse > 2.8 || stats$out_NNall$rsq < 0.5 ) { failed <- TRUE; sc_lev <- 2 }
## * R2 of NNgood vs. observed during good days
if ( stats$out_NNgood$prmse > 80 || stats$out_NNgood$rsq < 0.3 ) { failed <- TRUE; sc_lev <- 2 }
## * R2 of NNall during bad days has no big bias
if ( nrow(droughtdays)>0.02*nrow(nondrgtdays) ){
# if ( stats$out_NNallbad$ptoohi > 0.7 || stats$out_NNallbad$rsq < 0.2 ) failed <- TRUE
if ( stats$out_NNallbad$rsq < 0.17 || stats$out_NNallbad$ptoohi > 0.88 || stats$out_NNallbad$prmse > 76 ) { failed <- TRUE; sc_lev <- 3 }
}
## * RMSE of NNgood vs. NNall during good days
if ( stats$out_NNNN$rsq < 0.5 || stats$out_NNNN$rmse > 1.6 ) { failed <- TRUE; sc_lev <- 3 }
# ## delete panel file
# dir <- paste( myhome, "sofun/utils_sofun/analysis_sofun/fluxnet2015/fig_nn_fluxnet2015/panel_potentialgpp/", sep="")
# filn <- paste( "panel_crude_potentialgpp_", sitename, "_", nam_target, char_wgt, char_fapar, ".pdf", sep="" )
# path <- paste( dir, filn, sep="" )
# if (file.exists( path )) {
# system( paste( "rm", path ) )
# } else {
# path <- paste( dir, "failed/", filn, sep="" )
# system( paste( "rm", path ) )
# }
# ## move NN fvar data file if it's re-classified now
# dir <- paste( myhome, "data/nn_fluxnet/fvar/", sep="")
# filn <- paste( "nn_fluxnet2015_", sitename, "_", nam_target, char_wgt, char_fapar, ".Rdata", sep="" )
# path <- paste( dir, filn, sep="" )
# if ( file.exists( path ) && failed ) {
# system( paste( "mv", path, paste( dir, "failed/" ) ) )
# } else if ( !file.exists( path ) && !failed ){
# system( paste( "mv", paste( dir, "failed/", filn, sep="" ), dir ) )
# }
##------------------------------------------------
## Return code for success
##------------------------------------------------
if (failed || ndays < 500){
successcode <- 3
} else if ( sum(droughts$len) < 0.02 * nrow(nice) ){
successcode <- 2
} else {
successcode <- 1
}
if (ndays < 500) { sc_lev <- 1 }
out <- data.frame(
mysitename=sitename,
successcode=successcode,
ndays=ndays,
sc_lev=sc_lev,
NNallbad_prmse=stats$out_NNallbad$prmse,
NNallbad_rmse=stats$out_NNallbad$rmse,
NNallbad_rsq=stats$out_NNallbad$rsq,
NNallbad_ptoohi=stats$out_NNallbad$ptoohi,
NNallbad_pbias=stats$out_NNallbad$pbias,
NNgood_prmse=stats$out_NNgood$prmse,
NNgood_rmse=stats$out_NNgood$rmse,
NNgood_rsq=stats$out_NNgood$rsq,
NNgood_ptoohi=stats$out_NNgood$ptoohi,
NNgood_pbias=stats$out_NNgood$pbias,
NNall_prmse=stats$out_NNall$prmse,
NNall_rmse=stats$out_NNall$rmse,
NNall_rsq=stats$out_NNall$rsq,
NNall_ptoohi=stats$out_NNall$ptoohi,
NNall_pbias=stats$out_NNall$pbias,
NNNN_prmse=stats$out_NNNN$prmse,
NNNN_rmse=stats$out_NNNN$rmse,
NNNN_rsq=stats$out_NNNN$rsq,
NNNN_ptoohi=stats$out_NNNN$ptoohi,
NNNN_pbias=stats$out_NNNN$pbias
)
## create new panel file and put to failed folder if necessary
dir <- paste( "fig_nn_fluxnet2015/panel_potentialgpp/", paste( "level_", as.character(successcode), sep="" ), sep="" )
if (!file.exists( dir ) ) system( paste( "mkdir", dir ) )
panelfiln <- paste( dir, "/panel_crude_potentialgpp_", sitename, "_", nam_target, char_wgt, char_fapar, ".pdf", sep="")
# system( paste( "rm ", "/fig_nn_fluxnet2015/panel_potentialgpp/level_*/*", sep="" ) )
##------------------------------------------------
## plot time series with identified droughts
##------------------------------------------------
plot_panel_nn(
sitename, nice, out_nn_act$nn,
minmax = minmax,
dolek = FALSE,
filename = panelfiln,
droughts = droughts,
cutoff = cutoff,
fapar_extremes = out_evianomalies$extremes,
nam_target = nam_target,
makepdf = makepdf
)
} else {
out <- data.frame(
mysitename=sitename,
successcode=0,
sc_lev=NA,
ndays=NA,
NNallbad_prmse=NA,
NNallbad_rmse=NA,
NNallbad_rsq=NA,
NNallbad_ptoohi=NA,
NNallbad_pbias=NA,
NNgood_prmse=NA,
NNgood_rmse=NA,
NNgood_rsq=NA,
NNgood_ptoohi=NA,
NNgood_pbias=NA,
NNall_prmse=NA,
NNall_rmse=NA,
NNall_rsq=NA,
NNall_ptoohi=NA,
NNall_pbias=NA,
NNNN_prmse=NA,
NNNN_rmse=NA,
NNNN_rsq=NA,
NNNN_ptoohi=NA,
NNNN_pbias=NA
)
}
return( out )
}