forked from NCAR/CESM_postprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregridclimo.ncl
419 lines (373 loc) · 14.3 KB
/
regridclimo.ncl
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
;***************************************************************
; NCL script
; regridclimo.ncl
; John Truesdale, May 2012
;***************************************************************
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/esmf/ESMF_regridding.ncl"
begin
ingrid = getenv("INGRID")
outgrid = getenv("OUTGRID")
map_method = getenv("INTERP_METHOD")
srcFileName = getenv("TEST_INPUT") ; case1 input filename
dstFileName = getenv("TEST_PLOTVARS") ; case1 output filename
WGT_dir = getenv("MAP_DATA")
;print("Beginning to regrid "+srcFileName+" from "+ingrid+" to "+outgrid)
; --- Define grid
grid_type = "unknown"
nx=-999
ny=-999
if (outgrid.eq."0.23x0.31") then
nx=1152
ny=768
grid_type = "degree"
end if
if (outgrid.eq."0.47x0.63") then
nx=576
ny=384
grid_type = "degree"
end if
if (outgrid.eq."0.9x1.25") then
nx=288
ny=192
grid_type = "degree"
end if
if (outgrid.eq."1.9x2.5") then
nx=144
ny=96
grid_type = "degree"
end if
if (outgrid.eq."2x2.5") then
nx=144
ny=91
grid_type = "degree"
end if
if (outgrid.eq."4x5") then
nx=72
ny=46
grid_type = "degree"
end if
if (outgrid.eq."10x15") then
nx=24
ny=19
grid_type = "degree"
end if
if (outgrid.eq."gx1v6") then
nx=320
ny=384
grid_type = "degree"
end if
if (outgrid.eq."gx3v7") then
nx=100
ny=116
grid_type = "degree"
end if
if (outgrid.eq."gx3v5") then
nx=100
ny=116
grid_type = "degree"
end if
if (outgrid.eq."x0.1v2") then
nx=3600
ny=2400
grid_type = "degree"
end if
if (outgrid.eq."tx1v1") then
nx=360
ny=240
grid_type = "degree"
end if
if (nx.eq.-999) then
;---Check for "1x1", "2x3", "0.25x0.25" format
if(isStrSubset(outgrid,"x")) then
str = str_split(outgrid,"x")
if(dimsizes(str).ne.2) then
print("regridclimo: invalid format for grid type")
exit
end if
dlat = tofloat(str(0))
dlon = tofloat(str(1))
if(ismissing(dlat).or.ismissing(dlon)) then
print("regridclimo: invalid format for grid type")
exit
end if
grid_type = "degree"
delete(str)
else if(isStrSubset(outgrid,"deg")) then
;---Check for "1deg", "0.25 deg" format
str = str_split(outgrid,"deg")
if(dimsizes(str).ne.1) then
print("regridclimo: invalid format for grid type")
exit
end if
dlat = tofloat(str(0))
if(ismissing(dlat)) then
print("regridclimo: invalid format for grid type")
exit
end if
dlon = dlat
grid_type = "degree"
delete(str)
else if(isStrSubset(outgrid,"G")) then
;---Check for "G64", "G 128" format
str = str_split(outgrid,"G")
if(dimsizes(str).ne.1) then
print("regridclimo: invalid format for grid type")
exit
end if
nlon = tointeger(str(0))
if(ismissing(nlon)) then
print("regridclimo: invalid value for gaussian grid")
exit
end if
if((nlon%2).ne.0) then
print("regridclimo: invalid value for gaussian grid")
exit
end if
nlat = nlon/2
grid_type = "gaussian"
delete(str)
else
print("regrid: invalid format for output grid type "+outgrid)
exit
end if
end if
end if
ny=floattointeger(180./dlat)
nx=floattointeger(360./dlon)
end if
; 0. Set the source/destination file names,
; open the source file for reading,
; create a destination file for regridded data.
;------------------------------------------------
print(" ")
print("Regridding: ")
print("SRC:"+srcFileName)
print("DST:"+dstFileName)
if(isfilepresent(dstFileName)) then
system("rm "+dstFileName)
end if
src_file=addfile(srcFileName,"r")
dst_file=addfile(dstFileName,"c")
;
; 1. Generate a description file for the source grid (SE).
;---------------------------------------------------
srcGridName=WGT_dir+"/ne"+ingrid+".nc"
if(isfilepresent(srcGridName)) then
print("Found srcGrid description "+srcGridName)
else
print("Creating srcGrid description "+srcGridName)
Opt =True
Opt@ForceOverWrite=True
Opt@Title ="SE Grid"
Opt@Debug =False ; True
Opt@PrintTimings =False ; True
unstructured_to_ESMF(srcGridName,src_file->lat,src_file->lon,Opt)
delete(Opt)
end if
;
; 2. Generate a description file for the destination grid (FV).
;-----------------------------------------------------
dstGridName = WGT_dir+"/"+outgrid+".nc"
if(isfilepresent(dstGridName)) then
print("Found dstGrid description "+dstGridName)
else
print("Creating dstGrid description "+dstGridName)
Opt =True
Opt@ForceOverWrite=True
Opt@Debug =False ; True
Opt@PrintTimings =False ; True
lats =latGlobeF(ny,"lat","latitude" ,"degrees_north")
lons =lonGlobeF(nx,"lon","longitude","degrees_east" )
rectilinear_to_SCRIP(dstGridName,lats,lons,Opt)
delete(lats)
delete(lons)
delete(Opt)
end if
;
; 3. Generate the weights file, using the source and
; destination files created in #1 and #2.
;-----------------------------------------------------
wgtFileName = WGT_dir+"/map_ne"+ingrid+"_to_"+outgrid+"_"+map_method+".nc"
if(isfilepresent(wgtFileName)) then
print("Found WeightFile "+wgtFileName)
else
print("Creating WeightFile "+wgtFileName)
Opt = True
Opt@InterpMethod =map_method ;"bilinear" "patch", "conserve"
Opt@ForceOverWrite=True
Opt@SrcESMF =True
Opt@Debug =False ; True
Opt@PrintTimings =False ; True
ESMF_regrid_gen_weights(srcGridName,dstGridName,wgtFileName,Opt)
delete(Opt)
end if
;--- Specify a list of 1D variables on the *source SE file* that should NOT be copied
var_in_exclude = (/"lat", "lon", "area", "date_written", "time_written"/)
;--- Specify a list of variables on the source SE file that should be directly copied
var_in_copy = (/"time_bnds","cosp_ht_bnds","cosp_sr_bnds","cosp_prs_bnds","cosp_tau_bnds"/)
;--- Specify a list of variables to be regridded
var_out = "All_Variables" ; to be regridded
;---Read from the weight file the method used to derive the remap weights
wgt_file = addfile(wgtFileName, "r")
dst_grid_dims = wgt_file->dst_grid_dims
dst_mlon = dst_grid_dims(0)
dst_nlat = dst_grid_dims(1)
dst_lat = wgt_file->yc_b(::dst_mlon)
dst_lon = wgt_file->xc_b(:dst_mlon-1)
;---Generate or read the latitudinal weights dir the FV grid
gw = latRegWgt(dst_lat, "double", 0)
gw!0= "lat"
;---Use the destination (FV) grid info on the weight file to create lat/lon
lat = dst_lat ; get from weight file
lat@long_name = "latitude"
lat!0 = "lat"
lat@units = "degrees_north"
lat&lat = lat
nlat = dimsizes(lat) ; same as dst_nlat
lon = dst_lon
lon@long_name = "longitude"
lon!0 = "lon"
lon@units = "degrees_east"
lon&lon = lon
mlon = dimsizes(lon) ; same as dst_mlon
;---Get all variables on the SE file
var_in = getfilevarnames( src_file )
nvar_in = dimsizes(var_in)
Opt_RGRD = True
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; PREDEFINE MODE
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
setfileoption(dst_file,"DefineMode",True)
;===================================================================
; create global attributes of the netCDF file containing regridded data
;===================================================================
global = True
global@separator2= "---------------------------------------"
copy_VarAtts(src_file, global)
global@separator1= "------- SOURCE FILE ATTRIBUTES --------"
if (isatt(src_file,"title")) then
global@TITLE = "REMAPPED: "+src_file@title
end if
global@remap = "NCL: ESMF_regrid_with_weights"
global@map_method = map_method
global@creation_date = systemfunc("date")
fileattdef( dst_file, global ) ; copy file attributes to netCDF file
;===================================================================
; predefine the coordinate dimension names and their sizes
;===================================================================
dNames = getvardims(src_file) ; get level info from source file
dSizes = getfiledimsizes(src_file)
ia = ind(dNames.eq."lev" ) ; # of levels
ib = ind(dNames.eq."ilev")
klev = dSizes(ia)
klevi = dSizes(ib)
; cosp dimensions
if (any("cosp_scol".eq.dNames))
iscol = ind(dNames.eq."cosp_scol")
nscol = dSizes(iscol)
itau = ind(dNames.eq."cosp_tau")
ntau = dSizes(itau)
iprs = ind(dNames.eq."cosp_prs")
nprs = dSizes(iprs)
isr = ind(dNames.eq."cosp_sr")
nsr = dSizes(isr)
isza = ind(dNames.eq."cosp_sza")
nsza = dSizes(isza)
iht = ind(dNames.eq."cosp_ht")
nht = dSizes(iht)
; CAM dimensions plus cosp
dimNames = (/"time", "lat", "lon", "lev", "ilev", "nbnd", "chars","cosp_scol","cosp_prs","cosp_sr","cosp_tau","cosp_sza","cosp_ht"/)
dimSizes = (/ -1 , nlat , mlon , klev, klevi , 2 , 8 ,nscol ,nprs ,nsr ,ntau ,nsza ,nht /)
dimUnlim = (/ True , False, False, False, False , False , False ,False ,False ,False ,False ,False ,False /)
filedimdef(dst_file,dimNames,dimSizes,dimUnlim)
else
; standard CAM dimensions
dimNames = (/"time", "lat", "lon", "lev", "ilev", "nbnd", "chars"/)
dimSizes = (/ -1 , nlat , mlon , klev, klevi , 2 , 8 /)
dimUnlim = (/ True , False, False, False, False , False , False /)
filedimdef(dst_file,dimNames,dimSizes,dimUnlim)
end if
;--- The following are explicitly added because they are 'special'
filevardef(dst_file, "lat", typeof(lat), getvardims(lat))
filevarattdef(dst_file,"lat" ,lat)
filevardef(dst_file, "lon", typeof(lon), getvardims(lon))
filevarattdef(dst_file,"lon" ,lon)
filevardef(dst_file, "gw", typeof(gw), getvardims(gw))
filevarattdef(dst_file,"gw" ,gw)
;--- Loop over all variables and predfine meta data
do nv=0,nvar_in-1
if (.not.any(var_in(nv).eq.var_in_exclude)) then
if(var_out(0).eq."All_Variables" .or. \
any(var_in(nv).eq.var_out) .or. \
any(var_in(nv).eq.var_in_copy) ) then
rank_in = dimsizes(getfilevardimsizes(src_file, var_in(nv)))
if (rank_in .eq.1 .or. any(var_in(nv).eq.var_in_copy) ) then
filevardef(dst_file, var_in(nv), getfilevartypes(src_file,var_in(nv)) \
, getfilevardims(src_file,var_in(nv)) )
;
end if
if (rank_in .eq.2 .and. .not.any(var_in(nv).eq.var_in_copy)) then
filevardef(dst_file, var_in(nv), getfilevartypes(src_file,var_in(nv)) \
, (/ "time", "lat", "lon" /) )
end if
if (rank_in .eq.3) then
vdims = getfilevardims(src_file, var_in(nv))
filevardef(dst_file, var_in(nv), getfilevartypes(src_file,var_in(nv)) \
, (/ "time", vdims(1), "lat", "lon" /) )
delete(vdims)
end if
if (rank_in .eq.4.and.var_in(nv).eq."FISCCP1_COSP") then
vdims = getfilevardims(src_file, var_in(nv))
filevardef(dst_file, var_in(nv), getfilevartypes(src_file,var_in(nv)) \
, (/ "time", "cosp_prs", "cosp_tau","lat", "lon" /) )
delete(vdims)
end if
dumAtts = new( 1, getfilevartypes(src_file,var_in(nv)))
varAtts = getfilevaratts(src_file, var_in(nv))
if (.not.ismissing(varAtts(0))) then
nAtts = dimsizes(varAtts)
do na=0,nAtts-1
dumAtts@$varAtts(na)$ = src_file->$var_in(nv)$@$varAtts(na)$
end do
filevarattdef(dst_file, var_in(nv) , dumAtts)
end if
delete([/varAtts, dumAtts/]) ; may change size next iteration
end if
end if
end do ; nv
;===================================================================
; explicitly exit file definition mode. **NOT REQUIRED in NCL**
;===================================================================
setfileoption(dst_file,"DefineMode",False)
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Write the basic and regridded data values to the predefined structures
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
dst_file->lat = (/ lat /)
dst_file->lon = (/ lon /)
dst_file->gw = (/ gw /)
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Write the basic and regridded data values to the predefined structures
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
dst_file->lat = (/ lat /)
dst_file->lon = (/ lon /)
dst_file->gw = (/ gw /)
do nv=0,nvar_in-1
if (.not.any(var_in(nv).eq.var_in_exclude)) then
if(var_out(0).eq."All_Variables" .or. \
any(var_in(nv).eq.var_out) .or. \
any(var_in(nv).eq.var_in_copy) ) then
rank_in = dimsizes(getfilevardimsizes(src_file, var_in(nv)))
if (rank_in .eq.1 .or. any(var_in(nv).eq.var_in_copy) ) then
dst_file->$var_in(nv)$ = (/ src_file->$var_in(nv)$ /)
end if
if (rank_in .ge.2 .and. .not.any(var_in(nv).eq.var_in_copy)) then
dst_file->$var_in(nv)$ = (/ ESMF_regrid_with_weights(src_file->$var_in(nv)$,wgtFileName,Opt_RGRD) /)
end if
delete(rank_in)
end if
end if
end do ; nv
end