-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_gfdl_transect.py
291 lines (237 loc) · 10.6 KB
/
io_gfdl_transect.py
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
#!/usr/bin/env python
# coding: utf-8
# Output transect data for faster processing
# The notebook utilized the new zarr format for faster IO and dask array processing.
# The output is in netcdf format for easy sharing with other
#
# (1) cropped dataset based on time and transect
# (2) regrid the different variable to the same grid
import os
import dask
import xarray as xr
import numpy as np
import sys
from dask.distributed import Client
import xesmf as xe
import time as te
import warnings
warnings.simplefilter("ignore")
client = Client(n_workers=1, threads_per_worker=8, processes=False)
from mem_track import used_memory
used_memory()
#### possible input info from external text file
## OMODEL file detail
# ####################
# # standard model (interpolated to this model)
# Model_standard = 'CORE'
# syear = 1968
# fyear = 2007
# Model_name = ['CORE']
# modelin = {}
# model = Model_name[0]
# modeldir = './data/GFDL/CORE/'
# modelfile = [['CORE_uo_1948_1967.zarr',CORE_uo_1968_1992.zarr','CORE_uo.zarr'],
# ['CORE_thetao_1948_1967.zarr','CORE_thetao_1968_1992.zarr','CORE_thetao.zarr'],
# ['CORE_vo_1948_1967.zarr','CORE_vo_1968_1992.zarr','CORE_vo.zarr'],
# ['derived_field/CORE_wo_scpt/']]
# Model_varname = ['uo','thetao','vo','wo']
# Tracer_varname = 'thetao' # the variable name at the tracer point (Arakawa C grid)
# derived_field = ['wo']
# no_regrid = ['wo','thetao']
# # output transect
# transect_plane = ['yz','yz','yz','xz','xz','xz','xy']
# transect_var = [['uo','thetao'],
# ['uo','thetao'],
# ['uo','thetao'],
# ['vo','thetao'],
# ['vo','thetao'],
# ['uo','thetao','vo'],
# ['wo','thetao']]
# transect_x = [[-160],[-180],[120],[],[],[],[]]
# transect_y = [[],[],[],[-20],[20],[0],[]]
# transect_z = [[],[],[],[],[],[],[400]]
# # outputs
# modelout = {}
# modeldir_out = './data/GFDL/CORE/transect_scpt/'
# modelfile_out = ['CORE_uo_thetao',
# 'CORE_uo_thetao',
# 'CORE_uo_thetao',
# 'CORE_vo_thetao',
# 'CORE_vo_thetao',
# 'CORE_uo_thetao_vo',
# 'CORE_wo_thetao']
####################
# standard model (interpolated to this model)
Model_standard = 'JRA'
syear = 1958
fyear = 2017
Model_name = ['JRA']
modelin = {}
model = Model_name[0]
modeldir = './data/GFDL/JRA/'
modelfile = [['JRA_uo.zarr'],['JRA_thetao.zarr'],['JRA_vo.zarr'],['derived_field/JRA_wo_scpt/']]
Model_varname = ['uo','thetao','vo','wo']
Tracer_varname = 'thetao' # the variable name at the tracer point (Arakawa C grid)
derived_field = ['wo']
no_regrid = ['wo','thetao']
# output transect
transect_plane = ['yz','yz','yz','xz','xz','xz','xy']
transect_var = [['uo','thetao'],
['uo','thetao'],
['uo','thetao'],
['vo','thetao'],
['vo','thetao'],
['uo','thetao','vo'],
['wo','thetao']]
transect_x = [[-160],[-180],[120],[],[],[],[]]
transect_y = [[],[],[],[-20],[20],[0],[]]
transect_z = [[],[],[],[],[],[],[400]]
# outputs
modelout = {}
modeldir_out = './data/GFDL/JRA/transect_scpt/'
modelfile_out = ['JRA_uo_thetao',
'JRA_uo_thetao',
'JRA_uo_thetao',
'JRA_vo_thetao',
'JRA_vo_thetao',
'JRA_uo_thetao_vo',
'JRA_wo_thetao']
#####################################################################
# # Read OMODEL dataset
# input
for nmodel,model in enumerate(Model_name):
multivar = []
for file in modelfile :
if len(file) == 1 :
multivar.append([os.path.join(modeldir,file[0])])
elif len(file) > 1 :
multifile = []
for ff in file :
multifile.append(os.path.join(modeldir,ff))
multivar.append(multifile)
modelin[model] = multivar
# output
for nmodel,model in enumerate(Model_name):
multifile = []
for ntran,plane in enumerate(transect_plane):
if plane in ['yz']:
multifile.append(os.path.join(modeldir_out,modelfile_out[ntran]\
+'_%s_%i'%(transect_plane[ntran],transect_x[ntran][0])))
elif plane in ['xz']:
multifile.append(os.path.join(modeldir_out,modelfile_out[ntran]\
+'_%s_%i'%(transect_plane[ntran],transect_y[ntran][0])))
elif plane in ['xy']:
multifile.append(os.path.join(modeldir_out,modelfile_out[ntran]\
+'_%s_%i'%(transect_plane[ntran],transect_z[ntran][0])))
modelout[model] = multifile
# initialization of dict and list
nmodel = len(Model_name)
nvar = len(Model_varname)
ds_model_mlist = {}
mean_mlist = {}
season_mlist = {}
# # Removing mean and seasonal signal
for nmodel,model in enumerate(Model_name):
ds_model_list = {}
mean_list = {}
season_list = {}
for nvar,var in enumerate(Model_varname):
print('read %s %s'%(model,var))
# read input data
if var not in derived_field:
#-- single file
if len(modelin[model][nvar]) == 1 :
ds_model = xr.open_zarr(modelin[model][nvar][0])
#-- multi-file merge (same variable)
elif len(modelin[model][nvar]) > 1 :
for nf,file in enumerate(modelin[model][nvar]):
ds_model_sub = xr.open_zarr(file)
# for some old file include var: pacific
try :
ds_model_sub = ds_model_sub.drop('pacific')
except ValueError:
print('')
if nf == 0 :
ds_model = ds_model_sub
else:
ds_model = xr.concat([ds_model,ds_model_sub],dim='time',data_vars='minimal')
else:
ds_model = xr.open_mfdataset(os.path.join(modelin[model][nvar][0],'????-??.nc'),
concat_dim='time',
chunks={'z':-1,'y':100,'x':100})
# set time dimension
filenames_all = os.listdir(modelin[model][nvar][0])
filenames_all.sort()
filenames_date = [np.datetime64(file[:7]) for file in filenames_all if "._" not in file]
ds_model = ds_model.assign_coords(time = filenames_date)
# crop data (time)
da_model = ds_model[var].where((ds_model['time.year'] >= syear)&\
(ds_model['time.year'] <= fyear),drop=True)
#da_model = da_model.where((ds_model.lat >= np.min(np.array(tp_lat_region)))&\
# (ds_model.lat <= np.max(np.array(tp_lat_region))),drop=True)
# store all model data
ds_model_list[var] = da_model
# # calculate mean
# mean_list[var] = ds_model_list[var].mean(dim='time').compute()
# ds_model_list[var] = ds_model_list[var]-mean_list[var]
# # calculate seasonality
# season_list[var] = ds_model_list[var].groupby('time.month').mean(dim='time').compute()
# ds_model_list[var] = ds_model_list[var].groupby('time.month')-season_list[var]
# mean_mlist[model] = mean_list
# season_mlist[model] = season_list
ds_model_mlist[model] = ds_model_list
for nmodel,model in enumerate(Model_name):
for tt,time in enumerate(ds_model_mlist[model][Tracer_varname].time):
stime = te.time()
print('============ Time',time.values,'===========')
list_ds_transect = []
for ntran,plane in enumerate(transect_plane):
list_ds_transect.append(xr.Dataset())
# different variable
for var in Model_varname:
print('var:',var)
if var not in no_regrid:
regridder1 = xe.Regridder(ds_model_mlist[model][var],
ds_model_mlist[Model_standard][Tracer_varname],
'bilinear',
filename='%s2t_%s.nc'%(var,model),
periodic=True,
reuse_weights=True)
da_regrid = regridder1(ds_model_mlist[model][var].isel(time=tt))
used_memory()
print('regridded')
da_regrid = da_regrid.assign_coords(x = ds_model_mlist[model][Tracer_varname].x)
da_regrid = da_regrid.assign_coords(y = ds_model_mlist[model][Tracer_varname].y)
else:
da_regrid = ds_model_mlist[model][var].isel(time=tt)
# different transect
for ntran,plane in enumerate(transect_plane):
# correct the lon range
lon_mod = np.array(transect_x[ntran])
lonmin = ds_model_mlist[model][Tracer_varname].lon.min()
ind1 = np.where(lon_mod>np.float(360.+lonmin))[0]
# change Lon range to -300-60 (might be different for different model)
lon_mod[ind1] = lon_mod[ind1]-360.
transect_x[ntran] = lon_mod
if var in transect_var[ntran]:
if plane in ['yz']:
print("processing",plane,transect_x[ntran])
list_ds_transect[ntran][var] = da_regrid.sel(x=transect_x[ntran], method='nearest')
elif plane in ['xz']:
print("processing",plane,transect_y[ntran])
list_ds_transect[ntran][var] = da_regrid.sel(y=transect_y[ntran], method='nearest')
elif plane in ['xy']:
print("processing",plane,transect_z[ntran])
list_ds_transect[ntran][var] = da_regrid.sel(z=transect_z[ntran], method='nearest')
for ntran,plane in enumerate(transect_plane):
# del list_ds_transect[ntran].z.attrs['edges']
if not os.path.exists(modelout[model][ntran]):
os.makedirs(modelout[model][ntran])
try :
os.remove(modelout[model][ntran]+'/'+str(time.values)[:7]+'.nc')
list_ds_transect[ntran].to_netcdf(modelout[model][ntran]+'/'+str(time.values)[:7]+'.nc', mode='w')
except FileNotFoundError:
list_ds_transect[ntran].to_netcdf(modelout[model][ntran]+'/'+str(time.values)[:7]+'.nc', mode='w')
ftime = te.time()
used_memory()
print("calculation time:",(ftime-stime)/60.,'mins')