-
Notifications
You must be signed in to change notification settings - Fork 5
/
04_EnsembleForecast.Rmd
337 lines (286 loc) · 12.1 KB
/
04_EnsembleForecast.Rmd
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
333
334
335
336
337
---
title: "04_ParticleFilter"
author: "Cam Reimer"
date: "4/26/2021"
output: html_document
---
```{r}
## Package check and load
source('00C_Library+Directory_Setting.R')
```
```{r}
# load the data file [30 min Target data]
loadFilename <- sprintf("%s.Rdata","Target_LE")
loadFilename <- paste(dataPath, loadFilename, sep="", collapse = NULL)
load(file = loadFilename)
loadFilename <- sprintf("%s.Rdata","Target_NEE")
loadFilename <- paste(dataPath, loadFilename, sep="", collapse = NULL)
load(file = loadFilename)
loadFilename <- sprintf("%s.Rdata","Target_VSWC")
loadFilename <- paste(dataPath, loadFilename, sep="", collapse = NULL)
load(file = loadFilename)
loadFilename <- sprintf("%s.Rdata","Target_time")
loadFilename <- paste(dataPath, loadFilename, sep="", collapse = NULL)
load(file = loadFilename)
```
```{r}
#NOAA data load
# Download NOAA climate forecasts (hourly) and downsample to daily scale
source("00B_NOAAconversion.R")
# define site names
site_names <- c("BART","KONZ","OSBS","SRER")
####If you don't have NOAA data, run this code
#for (S in site_names){
# download_noaa_files_s3(siteID = S,
# date = "2021-03-01",
# cycle = "00",
# local_directory <- paste0(basePath,"/drivers/"))
#}
NOAA_Driver = noaa_gefs_read(paste0(basePath,"/drivers/noaa/NOAAGEFS_1hr"), "2021-03-01", "00", "KONZ")
predict_time = subset(NOAA_Driver, ensemble==1)
predict_time = predict_time$time
## Driver data preparation
#shortwave radiance
sw_driver = subset(NOAA_Driver, ensemble!=0)
sw_driver = sw_driver$surface_downwelling_shortwave_flux_in_air
sw_driver = matrix(sw_driver, nrow=30 ,byrow = TRUE)
#longwave radiance
lw_driver = subset(NOAA_Driver, ensemble!=0)
lw_driver = lw_driver$surface_downwelling_longwave_flux_in_air
lw_driver = matrix(lw_driver, nrow=30 ,byrow = TRUE)
#air temperature
temp_driver = subset(NOAA_Driver, ensemble!=0)
temp_driver = temp_driver$air_temperature
temp_driver = matrix(temp_driver, nrow=30 ,byrow = TRUE)
tmp = matrix(273.15,30,841)
temp_driver = temp_driver - tmp # conversion kelvin degree to celcius degree (-273.15)
#precipitation flux
precip_driver = subset(NOAA_Driver, ensemble!=0)
precip_driver = precip_driver$precipitation_flux
precip_driver = matrix(precip_driver, nrow=30 ,byrow = TRUE)
precip_driver = 1800 * precip_driver # unit conversion (30 min -> 1800 sec)
#storage to make 30 min interval driver data
sw_driver_gf = matrix(0, nrow=30, ncol=1679)
lw_driver_gf = matrix(0, nrow=30, ncol=1679)
temp_driver_gf = matrix(0, nrow=30, ncol=1679)
precip_driver_gf = matrix(0, nrow=30, ncol=1679)
## filling gap (interpolation using average)
for(i in 1:839){
sw_driver_gf[,2*i-1]=sw_driver[,i]
sw_driver_gf[,2*i]=(sw_driver[,i]+sw_driver[,i+1])/2
lw_driver_gf[,2*i-1]=lw_driver[,i]
lw_driver_gf[,2*i]=(lw_driver[,i]+lw_driver[,i+1])/2
temp_driver_gf[,2*i-1]=temp_driver[,i]
temp_driver_gf[,2*i]=(temp_driver[,i]+temp_driver[,i+1])/2
precip_driver_gf[,2*i-1]=precip_driver[,i]
precip_driver_gf[,2*i]=(precip_driver[,i]+precip_driver[,i+1])/2
}
sw_driver_gf[,1679]=sw_driver[,840]
lw_driver_gf[,1679]=lw_driver[,840]
temp_driver_gf[,1679]=temp_driver[,840]
precip_driver_gf[,1679]=precip_driver[,840]
```
```{r}
# load MCMC output
loadFilename <- sprintf("%s.Rdata","joint_burn")
loadFilename <- paste(dataPath, loadFilename, sep="", collapse = NULL)
load(file = loadFilename)
#SET ENSEMBLE RUNS
ne = 30 #needs to stay 30 unless we also sample (with replacement) the noaa driver ensembles
#AVERAGE CHAINS
#parsing MCMC output
beta_LE = sample(joint_out$params[[2]][,1], ne)
beta_LEI = sample(joint_out$params[[2]][,2], ne)
beta_LN = sample(joint_out$params[[2]][,3], ne)
beta_LV = sample(joint_out$params[[2]][,4], ne)
beta_NEE = sample(joint_out$params[[2]][,5], ne)
beta_NEEI = sample(joint_out$params[[2]][,6], ne)
beta_NL = sample(joint_out$params[[2]][,7], ne)
beta_NV = sample(joint_out$params[[2]][,8], ne)
beta_VL = sample(joint_out$params[[2]][,9], ne)
beta_VN = sample(joint_out$params[[2]][,10], ne)
beta_VSWC = sample(joint_out$params[[2]][,11], ne)
beta_VSWCI = sample(joint_out$params[[2]][,12], ne)
beta_lw = sample(joint_out$params[[2]][,13], ne)
beta_precip = sample(joint_out$params[[2]][,14], ne)
beta_sw1 = sample(joint_out$params[[2]][,15], ne)
beta_sw2 = sample(joint_out$params[[2]][,16], ne)
beta_temp = sample(joint_out$params[[2]][,17], ne)
tau_le_add = sample(joint_out$params[[2]][,18], ne)
tau_le_obs = sample(joint_out$params[[2]][,19], ne)
tau_nee_add = sample(joint_out$params[[2]][,20], ne)
tau_nee_obs = sample(joint_out$params[[2]][,21], ne)
tau_vswc_add = sample(joint_out$params[[2]][,22], ne)
tau_vswc_obs = sample(joint_out$params[[2]][,23], ne)
#Initial conditions: starting from last observed value <-- is this a bad idea?
qa_nee = joint_out$data$NEE_obs[!is.na(joint_out$data$NEE_obs)]
IC_NEE = rnorm(ne, mean = qa_nee[length(qa_nee)], sd = 0.1)
rm(qa_nee)
qa_le = joint_out$data$LE_obs[!is.na(joint_out$data$LE_obs)]
IC_LE = rnorm(ne, mean = qa_le[length(qa_le)], sd = 0.1)
rm(qa_le)
qa_vswc = joint_out$data$VSWC_obs[!is.na(joint_out$data$VSWC_obs)] #remember outlier
IC_VSWC = rnorm(ne, mean = qa_vswc[length(qa_vswc)], sd = 0.1)
rm(qa_vswc)
```
```{r}
ensembleforecast <- function(IC_NEE,IC_LE,IC_VSWC,
beta_NEE,beta_LE,beta_VSWC,beta_NL,beta_NV,beta_LV,beta_LN,
beta_VN,beta_VL,beta_NEEI,beta_LEI,beta_VSWCI,
beta_sw1,beta_sw2,beta_lw,beta_temp,beta_precip,
sw,lw,temp,precip){
Nprev_NEE <- IC_NEE
Nprev_LE <- IC_LE
Nprev_VSWC <- IC_VSWC
NEE = (1+beta_NEE)*Nprev_NEE+beta_NEEI+beta_NL*Nprev_LE+beta_NV*Nprev_VSWC+beta_sw1*sw +beta_temp*temp
LE = (1+beta_LE)*Nprev_LE+beta_LEI+beta_LN*Nprev_NEE+beta_LV*Nprev_VSWC+beta_sw2*sw +beta_lw*lw
VSWC = (1+beta_VSWC)*Nprev_VSWC+beta_VSWCI+beta_VN*Nprev_NEE+beta_VL*Nprev_LE+beta_precip*precip
return(cbind(NEE=NEE, LE=LE, VSWC=VSWC))
}
```
```{r}
#Initial Forecast
nt = 1679
#nt = 35 * 48 ## 35 days of 30min; production run should be nrow(inputs) *********
output = array(0.0, c(ne, nt, 3)) ## output storage [time step,ensembles,variables]
## forward ensemble simulation
for(t in 1:nt){
output[,t , ] <- ensembleforecast(IC_NEE,IC_LE,IC_VSWC,
beta_NEE,beta_LE,beta_VSWC,beta_NL,beta_NV,beta_LV,beta_LN,
beta_VN,beta_VL,beta_NEEI,beta_LEI,beta_VSWCI,
beta_sw1,beta_sw2,beta_lw,beta_temp,beta_precip,
sw_driver_gf[,t],lw_driver_gf[,t],temp_driver_gf[,t],precip_driver_gf[,t])
#reset initial conditions
IC_NEE = output[,t ,1]
IC_LE = output[,t ,2]
IC_VSWC = output[,t ,3]
#X <- output[t, , 1:3] ## set most recent prediction to be the next IC
#if((t %% 336) == 0) print(t / 336) ## counter: weeks elapsed (7*48 = 1 week)
}
```
```{r}
## Forward Simulation
### settings
N.cols <- c("red","green","blue") ## set colors
trans <- 0.8 ## set transparancy
time = 1:(length(time_KONZ)+1679) ## total time (1yr + 35 days)
time1 = 1:length(time_KONZ) ## calibration period
time2 = (length(time_KONZ)+1):(length(time_KONZ)+1679) ## forecast period
timeN_predict = length(time2)
tmp = matrix(0,1,length(time))
out <- as.matrix(joint_out$predict)
time_plot = time_KONZ
for (i in 1:1679){
time_plot[17520+i]<-time_plot[17519+i]+1800
}
time_predict = time_plot[17521:19199]
#ylim = c(-500,700)
```
```{r}
plot.run_NEE <- function(){
plot(time_plot,tmp,type='n',ylim=range(NEE_KONZ,na.rm=TRUE),ylab="NEE",main = "NEE Ensemble Forecast",
xlab = "time")
ecoforecastR::ciEnvelope(time_KONZ,ci[1,],ci[3,],col=col.alpha("lightBlue",0.6))
lines(time_KONZ,ci[2,],col="blue")
points(time_KONZ,NEE_BART)
}
```
```{r}
plot.run_NEE2 <- function(){
plot(time_plot,tmp,type='n',ylim=range(NEE_KONZ,na.rm=TRUE),ylab="NEE",main = "NEE Ensemble Forecast",
xlab = "time",xlim=c(as.POSIXct("2021-02-15",tz="UTC"),as.POSIXct("2021-04-15",tz="UTC")))
ecoforecastR::ciEnvelope(time_KONZ,ci[1,],ci[3,],col=col.alpha("lightBlue",0.6))
lines(time_KONZ,ci[2,],col="blue")
points(time_KONZ,NEE_BART)
}
```
```{r}
plot.run_LE <- function(){
plot(time_plot,tmp,type='n',ylim=range(LE_KONZ,na.rm=TRUE),ylab="LE",main = "LE Ensemble Forecast",
xlab = "time")
ecoforecastR::ciEnvelope(time_KONZ,ci[1,],ci[3,],col=col.alpha("lightBlue",0.6))
lines(time_KONZ,ci[2,],col="blue")
points(time_KONZ,NEE_BART)
}
```
```{r}
plot.run_LE2 <- function(){
plot(time_plot,tmp,type='n',ylim=range(LE_KONZ,na.rm=TRUE),ylab="LE",main = "LE Ensemble Forecast",
xlab = "time", xlim=c(as.POSIXct("2021-02-15",tz="UTC"),as.POSIXct("2021-04-15",tz="UTC")))
ecoforecastR::ciEnvelope(time_KONZ,ci[1,],ci[3,],col=col.alpha("lightBlue",0.6))
lines(time_KONZ,ci[2,],col="blue")
points(time_KONZ,NEE_BART)
}
```
```{r}
plot.run_VSWC <- function(){
plot(time_plot,tmp,type='n',ylim=range(ci,na.rm=TRUE),ylab="VSWC",main = "VSWC Ensemble Forecast",
xlab = "time")
ecoforecastR::ciEnvelope(time_KONZ,ci[1,],ci[3,],col=col.alpha("lightBlue",0.6))
lines(time_KONZ,ci[2,],col="blue")
points(time_KONZ,VSWC_BART)
}
```
```{r}
plot.run_VSWC2 <- function(){
plot(time_plot,tmp,type='n',ylim=range(ci,na.rm=TRUE),ylab="VSWC",main = "VSWC Ensemble Forecast",
xlab = "time", xlim=c(as.POSIXct("2021-02-15",tz="UTC"),as.POSIXct("2021-04-15",tz="UTC")))
ecoforecastR::ciEnvelope(time_KONZ,ci[1,],ci[3,],col=col.alpha("lightBlue",0.6))
lines(time_KONZ,ci[2,],col="blue")
points(time_KONZ,VSWC_BART)
}
```
### Plot for NEE
```{r,echo=FALSE}
x.cols <- grep("^NEE",colnames(out)) ## grab all columns that start with the letter x
ci <- apply(out[,x.cols],2,quantile,c(0.025,0.5,0.975))
plot.run_NEE()
ci = apply(output[, , 1], 2, quantile, c(0.025, 0.5, 0.975),na.rm=TRUE) ## calculate CI over ensemble members
ciEnvelope(time_predict, ci[1, ], ci[3, ], col = col.alpha(N.cols[1], 0.5)) ## plot interval
lines(time_predict,ci[2,],lwd=0.5)
```
### Plot for NEE (subset)
```{r,echo=FALSE}
x.cols <- grep("^NEE",colnames(out)) ## grab all columns that start with the letter x
ci <- apply(out[,x.cols],2,quantile,c(0.025,0.5,0.975))
plot.run_NEE2()
ci = apply(output[, , 1], 2, quantile, c(0.025, 0.5, 0.975),na.rm=TRUE) ## calculate CI over ensemble members
ciEnvelope(time_predict, ci[1, ], ci[3, ], col = col.alpha(N.cols[1], 0.5)) ## plot interval
lines(time_predict,ci[2,],lwd=0.5)
```
### Plot for NEE
```{r,echo=FALSE}
x.cols <- grep("^LE",colnames(out)) ## grab all columns that start with the letter x
ci <- apply(out[,x.cols],2,quantile,c(0.025,0.5,0.975))
plot.run_LE()
ci = apply(output[, , 2], 2, quantile, c(0.025, 0.5, 0.975),na.rm=TRUE) ## calculate CI over ensemble members
ciEnvelope(time_predict, ci[1, ], ci[3, ], col = col.alpha(N.cols[2], 0.5)) ## plot interval
lines(time_predict,ci[2,],lwd=0.5)
```
### Plot for LE (subset)
```{r,echo=FALSE}
x.cols <- grep("^LE",colnames(out)) ## grab all columns that start with the letter x
ci <- apply(out[,x.cols],2,quantile,c(0.025,0.5,0.975))
plot.run_LE2()
ci = apply(output[, , 2], 2, quantile, c(0.025, 0.5, 0.975),na.rm=TRUE) ## calculate CI over ensemble members
ciEnvelope(time_predict, ci[1, ], ci[3, ], col = col.alpha(N.cols[2], 0.5)) ## plot interval
lines(time_predict,ci[2,],lwd=0.5)
```
### Plot for VSWC
```{r,echo=FALSE}
x.cols <- grep("^VSWC",colnames(out)) ## grab all columns that start with the letter x
ci <- apply(out[,x.cols],2,quantile,c(0.025,0.5,0.975))
plot.run_VSWC()
ci = apply(output[, , 3], 2, quantile, c(0.025, 0.5, 0.975),na.rm=TRUE) ## calculate CI over ensemble members
ciEnvelope(time_predict, ci[1, ], ci[3, ], col = col.alpha(N.cols[3], 0.5)) ## plot interval
lines(time_predict,ci[2,],lwd=0.5)
```
### Plot for NEE (subset)
```{r,echo=FALSE}
x.cols <- grep("^VSWC",colnames(out)) ## grab all columns that start with the letter x
ci <- apply(out[,x.cols],2,quantile,c(0.025,0.5,0.975))
plot.run_VSWC2()
ci = apply(output[, , 3], 2, quantile, c(0.025, 0.5, 0.975),na.rm=TRUE) ## calculate CI over ensemble members
ciEnvelope(time_predict, ci[1, ], ci[3, ], col = col.alpha(N.cols[3], 0.5)) ## plot interval
lines(time_predict,ci[2,],lwd=0.5)
```