-
Notifications
You must be signed in to change notification settings - Fork 2
/
calc_fdsi_opendap_lat-lon.txt
336 lines (235 loc) · 8.76 KB
/
calc_fdsi_opendap_lat-lon.txt
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
; calculate spatial FDSI timeseries from a gridded downscaled climate projections data - MACAv2-metdata
; FDSI calculations are based on Williams et al. 2013. Temperature as a potent driver of regional forest drought stress and tree mortality. NatCC, 3(3), pp.292-297. https://www.nature.com/articles/nclimate1693
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"
begin
;----------------
; ENTER MODEL INFO (INPUT REQUIRED)
; Climate model information could be accessed at: https://climate.northwestknowledge.net/MACA/GCMs.php
modi = "BNU-ESM" ; enter GCM name
rcpi = "rcp45" ; enter RCP (options include rcp45 or rcp85)
runi = "r1i1p1" ; enter model experiment ID (r1i1p1 for all GCMs except CCSM4 for which it is r6i1p1)
;----------------
; ENTER LOCATION INFO (INPUT REQUIRED)
regi = "Rocky Mountain National Park" ; enter region's name
latS = 39.9 ; enter southern-most latitude value in degrees north
latN = 40.5 ; enter northern-most latitude value in degrees north
lonW_ti = 105.8 ; enter western-most longitude value in degrees west
lonE_ti = 105.5 ; enter eastern-most longitude value in degrees west
lonW = 360-lonW_ti
lonE = 360-lonE_ti
;----------------
; NO INPUT REQUIRED BEYOND THIS
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
varo = "fdsi"
varo_ti = "FDSI"
timi = "1951-2099"
datai = "MACAv2-metdata"
vari1 = "pr"
vari2 = "vpd"
;----------------
; Plotting Routine
outfile = "FDSI_timeseries_"+regi+"_"+timi+"_"+modi+"_"+runi+"_"+rcpi+"_"+datai
; Get output selector from command line.
formats = (/ "pdf", "eps", "png", "x11" /) ; output selector table
output = formats(2) ; get first match in list
; setting plotting resources
wks = gsn_open_wks(output, outfile)
;----------------
; OPeNDAP MACA data access
diri_p1 = "http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_"+vari1+"_"+modi+"_"+runi+"_historical_1950_2005_CONUS_monthly.nc"
diri_p2 = "http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_"+vari1+"_"+modi+"_"+runi+"_"+rcpi+"_2006_2099_CONUS_monthly.nc"
diri_v1 = "http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_"+vari2+"_"+modi+"_"+runi+"_historical_1950_2005_CONUS_monthly.nc"
diri_v2 = "http://thredds.northwestknowledge.net:8080/thredds/dodsC/agg_macav2metdata_"+vari2+"_"+modi+"_"+runi+"_"+rcpi+"_2006_2099_CONUS_monthly.nc"
print(diri_p1)
print(diri_v2)
;----------------
;----------------
;----------------
; precipitation file access
f_p1 = addfile(diri_p1,"r")
x_p1 = f_p1->precipitation(:,{latS:latN},{lonW:lonE})
printVarSummary(x_p1)
printMinMax(x_p1,True)
lat = x_p1&lat
lon = x_p1&lon
printVarSummary(lon)
;----------------
f_p2 = addfile(diri_p2,"r")
x_p2 = f_p2->precipitation(:,{latS:latN},{lonW:lonE})
printVarSummary(x_p2)
printMinMax(x_p2,True)
x_p= array_append_record (x_p1, x_p2, 0)
printVarSummary(x_p)
printMinMax(x_p,True)
dims = dimsizes(x_p(:,0,0))
print(dims)
;----------------
; spatial averaging
; calc NDJFM total
x_p_seas = x_p(14:dims-1:12,:,:)
x_p_seas = (x_p(10:dims-13:12,:,:)+x_p(11:dims-13:12,:,:)+x_p(12:dims-1:12,:,:)+x_p(13:dims-1:12,:,:)+x_p(14:dims-1:12,:,:))
printVarSummary(x_p_seas)
printMinMax(x_p_seas,1)
;----------------
; calc historical (1950-2005) mean and std. dev.
x_pm = dim_avg_n_Wrap(x_p_seas(0:54,:,:),0) ; mean
printVarSummary(x_pm)
printMinMax(x_pm,1)
x_ps = dim_stddev_n_Wrap(x_p_seas(0:54,:,:),0) ; sd
printVarSummary(x_ps)
printMinMax(x_ps,1)
;----------------
; standardizing
dims2 = dimsizes(x_p_seas(:,0,0))
print(dims2)
x_p_seas_ln = x_p_seas
x_p_seas_ln = log(x_p_seas)
printVarSummary(x_p_seas_ln)
x_pm_ln = x_pm
x_pm_ln = log(x_pm)
printVarSummary(x_pm_ln)
x_ps_ln = x_ps
x_ps_ln = log(x_ps)
printVarSummary(x_ps_ln)
e1 = x_p_seas_ln
do i = 0,dims2-1
e1(i,:,:) = (x_p_seas_ln(i,:,:)-x_pm_ln)/x_ps_ln
end do
printVarSummary(e1)
;----------------
;----------------
;----------------
; vapor pressure deficit file access
f_v1 = addfile(diri_v1,"r")
x_v1 = f_v1->vpd(:,{latS:latN},{lonW:lonE})
printVarSummary(x_v1)
printMinMax(x_v1,True)
lat = x_v1&lat
lon = x_v1&lon
printVarSummary(lon)
;----------------
f_v2 = addfile(diri_v2,"r")
x_v2 = f_v2->vpd(:,{latS:latN},{lonW:lonE})
printVarSummary(x_v2)
printMinMax(x_v2,True)
x_v= array_append_record (x_v1, x_v2, 0)
printVarSummary(x_v)
printMinMax(x_v,True)
; calc sum of mean ASO (Aug-Oct previous year) and mean MJJ (May-Jul current year)
x_v_aso = month_to_season(x_v(0:dims-13,:,:), "ASO")
x_v_mjj = month_to_season(x_v(12:dims-1,:,:), "MJJ")
x_v_seas = x_v_aso
x_v_seas = (x_v_aso+x_v_mjj)/2
printVarSummary(x_v_seas)
printMinMax(x_v_seas,1)
;----------------
; calc historical (1950-2005) mean and std. dev.
x_vm = dim_avg_n_Wrap(x_v_seas(0:54,:,:),0) ; mean
printVarSummary(x_vm)
printMinMax(x_vm,1)
x_vs = dim_stddev_n_Wrap(x_v_seas(0:54,:,:),0) ; sd
printVarSummary(x_vs)
printMinMax(x_vs,1)
;----------------
; standardizing
e2 = x_v_seas
do i = 0,dims2-1
do i = 0,dims2-1
denom = where(x_vs.eq.0,1,x_vs)
numer = where(x_vs.eq.0,0,x_v_seas(i,:,:)-x_vm)
e2(i,:,:) = numer/denom
end do
end do
printVarSummary(e2)
;----------------
;----------------
;----------------
;fdsi estimation
fdsi = e1
fdsi = (0.44*e1)-(0.56*e2)
fdsi@long_name = "Forest Drought Stress Index"
fdsi@units = "standarized index"
printVarSummary(fdsi)
;----------------
;----------------
;----------------
;----------------
; print output FDSI timeseries data
nyrs = ispan(1951,2099,1)
printVarSummary(nyrs)
fdsi_avg = dim_avg_n_Wrap(dim_avg_n_Wrap(fdsi,2),1)
printVarSummary(fdsi_avg)
printMinMax(fdsi_avg,True)
print("Model = "+modi)
print("Year FDSI")
print(nyrs+" "+fdsi_avg)
;----------------
; write table
header = (/"Source: North Central Climate Adaptation Science Center", \
"--------------------------------", \
"Year FDSI", \
"--------------------------------"/)
alist = [/nyrs, fdsi_avg/]
hlist = [/header/]
fname = "FDSI_timeseries_"+regi+"_"+timi+"_"+modi+"_"+runi+"_"+rcpi+"_"+datai+".txt"
write_table(fname, "w", hlist, "%s")
write_table(fname, "a", alist, "%d%16.2f%s%d%ld")
;----------------
;plot output FDSI timeseries data
res = True
res@gsnScale = True
; these four resources allow the user to stretch the plot size, and
; decide exactly where on the page to draw it.
res@vpXF = 0.10 ; In page coordinates, where to start
res@vpYF = 0.80 ; the plot
res@vpHeightF = 0.50 ; Changes the aspect ratio
res@vpWidthF = 0.85
res@gsnMaximize = True
res@gsnFrame = False
res@gsnDraw = False
res@trYMinF = -4.5 ; min value on y-axis
res@trYMaxF = 4.5 ; max value on y-axis
res@trXMinF = 1950
res@trXMaxF = 2100
res@tmYLMode = "Manual"
res@tmYLTickStartF = -5
res@tmYLTickEndF = 5
res@tmYLTickSpacingF= 1
res@tmYLMaxTicks = 15
res@tmXTOn = False
res@tmYROn = False
res@tmXBMinorOn = False
res@tmYLMinorOn = False
res@tiYAxisString = ""
res@tmXBMode = "Manual"
res@tmXBTickStartF = 1950
res@tmXBTickEndF = 2100
res@tmXBTickSpacingF= 20
res@tiMainString = varo_ti+": "+regi+" ("+latS+"-"+latN+"N, "+lonE_ti+"-"+lonW_ti+"W) ~C~ ~Z75~ "+modi+"_"+runi+"_"+rcpi+"_"+datai ;plot title
res@tiMainFontHeightF = 0.025
res@gsnYRefLine = 0 ; reference line
res@gsnXYBarChart = True ; create bar chart
res@gsnAboveYRefLineColor = "slateblue4" ; above ref line fill red
res@gsnBelowYRefLineColor = "orangered3" ; below ref line fill blue
plot = gsn_csm_xy (wks,nyrs,fdsi_avg,res)
draw(plot)
;--------------------
; legend
txres = True
;txres@txFont = 22
txres@txFontHeightF = 0.010
xtxt = (/0.88,0.8/) ; text
ytxt = (/0.16,0.14/) ; strings.
text1 = "FDSI data and plot generated by:"
text2 = "North Central Climate Adaptation Science Center (NC CASC)"
gsn_text_ndc(wks,text1,xtxt(0),ytxt(0),txres)
gsn_text_ndc(wks,text2,xtxt(1),ytxt(1),txres)
;--------------------
frame(wks)
end