-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcold_ens_generation.R
280 lines (180 loc) · 10.4 KB
/
cold_ens_generation.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
##############################
#-- Variables
#-- you must set these
#-- dim/gridcellsize correspond
#-- to GEOSCHEM run to be used
#-- fileout is where bias ens
#-- is stored.
##############################
nugget.var.perc.prior = 0
var.mult.prior = 0.2^2
origdim.x_2x2.5 = 144
origdim.y_2x2.5 = 91
grid.x_2x2.5 = 2.5
grid.y_2x2.5 = 2
origdim.x_4x5 = 72
origdim.y_4x5 = 46
grid.x_4x5 = 5
grid.y_4x5 = 4
ens.size = 1000
infl.factor.mean = 0.15
infl.factor.var = 0.03^2
obslandbias_H_mean = 0
obslandbias_H_sd = 0.5
obslandbias_M_mean = 0
obslandbias_M_sd = 0.5
obsoceanbias_mean = 0
obsoceanbias_sd = 0.5
b1_offset_mean = 0.5
b1_offset_sd = 0.25
s32_mean = 34
s32_sd = 8
albedo_2_H_mean = 0
albedo_2_H_sd = 5
albedo_2_M_mean = 0
albedo_2_M_sd = 5
dp_cld_mean = 0
dp_cld_sd = 0.1
#fileout = "/user1/aschuh/temp/betas.061113.nc"
#fileout = "~/Desktop/betas.061113.nc"
fileout = "/user1/aschuh/temp/betas.112513.nc"
###############################
#-- Libraries needed
require(fields)
require(geoR)
require(mnormt)
#require(matrixcalc)
#-- COLD START, generation of initial ensemble
grid.pr_2x2.5 = expand.grid(x=seq(-180,180-grid.x_2x2.5,by=grid.x_2x2.5)+0.5*grid.x_2x2.5,
y=c(-90+0.5*(0.5*grid.y_2x2.5),seq(-90+0.5*grid.y_2x2.5,
90-1.5*grid.y_2x2.5,by=grid.y_2x2.5)+0.5*grid.y_2x2.5,90-0.5*(0.5*grid.y_2x2.5)))
dist.pr_2x2.5 = rdist.earth(grid.pr_2x2.5,miles=FALSE)
ltriang.dist.pr_2x2.5 = lower.tri(dist.pr_2x2.5)
grid.pr_4x5 = expand.grid(x=seq(-180,180-grid.x_4x5,by=grid.x_4x5)+0.5*grid.x_4x5,
y=c(-90+0.5*(0.5*grid.y_4x5),seq(-90+0.5*grid.y_4x5,
90-1.5*grid.y_4x5,by=grid.y_4x5)+0.5*grid.y_4x5,90-0.5*(0.5*grid.y_4x5)))
dist.pr_4x5 = rdist.earth(grid.pr_4x5,miles=FALSE)
ltriang.dist.pr_4x5 = lower.tri(dist.pr_4x5)
#-- FOR 2x2.5, LAND
VV.land_2x2.5 = varcov.spatial(dists.lowertri=dist.pr_2x2.5[lower.tri(dist.pr_2x2.5)],cov.model="exponential",
cov.pars=c(var.mult.prior,800))
ensembs.land_2x2.5 = rmnorm(ens.size*2,mean=rep(0,grid.x_2x2.5*grid.y_2x2.5),varcov=VV.land_2x2.5$varcov)
ensembs.land.gpp_2x2.5 = ensembs.land_2x2.5[1:ens.size,]
ensembs.land.resp_2x2.5 = ensembs.land_2x2.5[(ens.size+1):(ens.size*2),]
rm(VV.land_2x2.5)
#-- FOR 4x5, LAND
VV.land_4x5 = varcov.spatial(dists.lowertri=dist.pr_4x5[lower.tri(dist.pr_4x5)],cov.model="exponential",
cov.pars=c(var.mult.prior,800))
ensembs.land_4x5 = rmnorm(ens.size*2,mean=rep(0,grid.x_4x5*grid.y_4x5),varcov=VV.land_4x5$varcov)
ensembs.land.gpp_4x5 = ensembs.land_4x5[1:ens.size,]
ensembs.land.resp_4x5 = ensembs.land_4x5[(ens.size+1):(ens.size*2),]
rm(VV.land_4x5)
#-- Write out ensemble reals to netcdf
#-- FOR 2x2.5
VV.ocean_2x2.5 = varcov.spatial(dists.lowertri = dist.pr_2x2.5[lower.tri(dist.pr_2x2.5)],cov.model="exponential",cov.pars=c(0.1^2,1600))
ensembs.ocean_2x2.5 = rmnorm(ens.size,mean=rep(0,grid.x_2x2.5*grid.y_2x2.5),varcov=VV.ocean_2x2.5$varcov)
rm(VV.ocean_2x2.5)
#-- FOR 4x5
VV.ocean_4x5 = varcov.spatial(dists.lowertri = dist.pr_4x5[lower.tri(dist.pr_4x5)],cov.model="exponential",cov.pars=c(0.1^2,1600))
ensembs.ocean_4x5 = rmnorm(ens.size,mean=rep(0,grid.x_4x5*grid.y_4x5),varcov=VV.ocean_4x5$varcov)
rm(VV.ocean_4x5)
#-- Write out ensemble reals to netcdf, 2x2.5
ens = list(ensembs.land.gpp_2x2.5=ensembs.land.gpp_2x2.5,
ensembs.land.resp_2x2.5=ensembs.land.resp_2x2.5,
ensembs.ocean_2x2.5=ensembs.ocean_2x2.5)
#-- This sets the first ens member to the "control", just 0 in this case because
#-- GEOSCHEM is expecting the add '1' to this, ie. newGPP = GPP (1 + beta)
ens$ensembs.land.gpp[1,] = 0
ens$ensembs.land.resp[1,] = 0
ens$ensembs.ocean[1,] = 0
#save(ens,file="/user1/aschuh/temp/ens.040912.rda")
#load("/user1/aschuh/temp/ens.050112.rda")
#-- This begins to build the netcdf file
require(ncdf4)
x = ncdim_def( "lon", "degrees_east", as.double(seq(-180,180-grid.x_2x2.5,by=grid.x_2x2.5)))
y = ncdim_def( "lat", "degrees_north", as.double(c(-90+0.25*grid.y_2x2.5,seq((-90+0.25*grid.y_2x2.5)+0.5*grid.y_2x2.5,
90-0.75*grid.y_2x2.5,by=grid.y_2x2.5),90-0.25*grid.y_2x2.5)))
nens = ncdim_def( "ensemble", units="integer", as.double(seq(1:ens.size)))
t = ncdim_def( "time", "hours since 1900-01-01", 1, unlim=TRUE)
varlist = list()
varlist[[1]] <- ncvar_def(name="BETAGPP",units="",
dim=list(x,y,nens,t), missval=NA,prec="float")
varlist[[2]] <- ncvar_def(name="BETARESP",units="",
dim=list(x,y,nens,t), missval=NA,prec="float")
varlist[[3]] <- ncvar_def(name="BETAOCEAN",units="",
dim=list(x,y,nens,t), missval=NA,prec="float")
#varlist[[4]] <- ncvar_def(name="BETAGPP_INFL_MEAN",units="",
# dim=list(x,y,t), missval=NA,prec="float")
#varlist[[5]] <- ncvar_def(name="BETAGPP_INFL_VAR",units="",
# dim=list(x,y,t), missval=NA,prec="float")
#varlist[[6]] <- ncvar_def(name="BETARESP_INFL_MEAN",units="",
# dim=list(x,y,t), missval=NA,prec="float")
#varlist[[7]] <- ncvar_def(name="BETARESP_INFL_VAR",units="",
# dim=list(x,y,t), missval=NA,prec="float")
#varlist[[8]] <- ncvar_def(name="BETAOCEAN_INFL_MEAN",units="",
# dim=list(x,y,t), missval=NA,prec="float")
#varlist[[9]] <- ncvar_def(name="BETAOCEAN_INFL_VAR",units="",
# dim=list(x,y,t), missval=NA,prec="float")
#varlist[[1]] <- ncvar_def(name="OBSB1_OFFSETCOEF",units="",
# dim=list(nens,t), missval=NA,prec="float")
#varlist[[2]] <- ncvar_def(name="OBSS32_COEF",units="",
# dim=list(nens,t), missval=NA,prec="float")
#varlist[[3]] <- ncvar_def(name="OBSALBEDO_2_M_COEF",units="",
# dim=list(nens,t), missval=NA,prec="float")
#varlist[[4]] <- ncvar_def(name="OBSALBEDO_2_H_COEF",units="",
# dim=list(nens,t), missval=NA,prec="float")
#varlist[[5]] <- ncvar_def(name="OBSDP_CLD_COEF",units="",
# dim=list(nens,t), missval=NA,prec="float")
#varlist[[6]] <- ncvar_def(name="OBSLANDHBIAS",units="",
# dim=list(nens,t), missval=NA,prec="float")
#varlist[[7]] <- ncvar_def(name="OBSLANDMBIAS",units="",
# dim=list(nens,t), missval=NA,prec="float")
#varlist[[8]] <- ncvar_def(name="OBSOCEANBIAS",units="",
# dim=list(nens,t), missval=NA,prec="float")
#fileout = "/discover/nobackup/aschuh/data/betas/betas.041514_2x2.5.nc"
ncnew <- nc_create(fileout,varlist)
ncvar_put(ncnew, varlist[[1]],t(ens$ensembs.land.gpp))
ncvar_put(ncnew, varlist[[2]],t(ens$ensembs.land.resp))
ncvar_put(ncnew, varlist[[3]],t(ens$ensembs.ocean))
#ncvar_put(ncnew, varlist[[4]],rep(infl.factor.mean,length(x$vals)*length(y$vals)))
#ncvar_put(ncnew, varlist[[5]],rep(infl.factor.var,length(x$vals)*length(y$vals)))
#ncvar_put(ncnew, varlist[[6]],rep(infl.factor.mean,length(x$vals)*length(y$vals)))
#ncvar_put(ncnew, varlist[[7]],rep(infl.factor.var,length(x$vals)*length(y$vals)))
#ncvar_put(ncnew, varlist[[8]],rep(infl.factor.mean,length(x$vals)*length(y$vals)))
#ncvar_put(ncnew, varlist[[9]],rep(infl.factor.var,length(x$vals)*length(y$vals)))
#ncvar_put(ncnew, varlist[[1]],rnorm(ens.size,b1_offset_mean,b1_offset_sd))
#ncvar_put(ncnew, varlist[[2]],rnorm(ens.size,s32_mean,s32_sd))
#ncvar_put(ncnew, varlist[[3]],rnorm(ens.size,albedo_2_M_mean,albedo_2_M_sd))
#ncvar_put(ncnew, varlist[[4]],rnorm(ens.size,albedo_2_H_mean,albedo_2_H_sd))
#ncvar_put(ncnew, varlist[[5]],rnorm(ens.size,dp_cld_mean,dp_cld_sd))
#ncvar_put(ncnew, varlist[[6]],rnorm(ens.size,obslandbias_H_mean,obslandbias_H_sd))
#ncvar_put(ncnew, varlist[[7]],rnorm(ens.size,obslandbias_M_mean,obslandbias_M_sd))
#ncvar_put(ncnew, varlist[[8]],rnorm(ens.size,obsoceanbias_mean,obsoceanbias_sd))
nc_close(ncnew)
#-- Write out ensemble reals to netcdf, 4x5
ens = list(ensembs.land.gpp_4x5=ensembs.land.gpp_4x5,
ensembs.land.resp_4x5=ensembs.land.resp_4x5,
ensembs.ocean_4x5=ensembs.ocean_4x5)
ens$ensembs.land.gpp[1,] = 0
ens$ensembs.land.resp[1,] = 0
ens$ensembs.ocean[1,] = 0
#-- This begins to build the netcdf file
require(ncdf4)
x = ncdim_def( "lon", "degrees_east", as.double(seq(-180,180-grid.x_4x5,by=grid.x_4x5)))
y = ncdim_def( "lat", "degrees_north", as.double(c(-90+0.25*grid.y_4x5,seq((-90+0.25*grid.y_4x5)+0.5*grid.y_4x5,
90-0.75*grid.y_4x5,by=grid.y_4x5),90-0.25*grid.y_4x5)))
nens = ncdim_def( "ensemble", units="integer", as.double(seq(1:ens.size)))
t = ncdim_def( "time", "hours since 1900-01-01", 1, unlim=TRUE)
varlist = list()
varlist[[1]] <- ncvar_def(name="BETAGPP",units="",
dim=list(x,y,nens,t), missval=NA,prec="float")
varlist[[2]] <- ncvar_def(name="BETARESP",units="",
dim=list(x,y,nens,t), missval=NA,prec="float")
varlist[[3]] <- ncvar_def(name="BETAOCEAN",units="",
dim=list(x,y,nens,t), missval=NA,prec="float")
#fileout = "/discover/nobackup/aschuh/data/betas/betas.041514_4x5.nc"
ncnew <- nc_create(fileout,varlist)
ncvar_put(ncnew, varlist[[1]],t(ens$ensembs.land.gpp))
ncvar_put(ncnew, varlist[[2]],t(ens$ensembs.land.resp))
ncvar_put(ncnew, varlist[[3]],t(ens$ensembs.ocean))
nc_close(ncnew)