-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_forcing_SURFEX_nc.HCLIM.py
executable file
·475 lines (314 loc) · 11.5 KB
/
create_forcing_SURFEX_nc.HCLIM.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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
import numpy as np
import os
import yaml
import pandas as pd
import math
from netCDF4 import Dataset
from tqdm import tqdm
from yaml.loader import SafeLoader
# ------------------------------------------------------------------------------
def create_forcing_SURFEX_nc():
# 1 - Read namelist
namelist = read_namelist('../nam/namelist_create_forcing_SURFEX_nc.HCLIM.yaml')
# 2 - Read variables info
var = read_dataframe('../nam/namelist_variables_SURFEX_forcing.HCLIM')
for pgw_case in namelist['PGW']:
print('3 - Read grid info')
namelist = rewrite_output_file(namelist, pgw_case)
# 4 - Read grid info
print('4 - Read grid info')
time, longitude, latitude = read_grid_info_time(namelist)
# 5 - Save grid information
print('5 - Save grid information')
save_grid_info(namelist, pgw_case, time, longitude, latitude)
# 6 - Include time step
print('6 - Include time step')
include_timestep(namelist)
# 7 - Loop over all variables
#for varname in tqdm(var.SFXname.values, desc='Variables:', leave=True):
for varname in var.SFXname.values:
include_var(namelist, var[var.SFXname==varname], pgw_case)
def windspeed(u,v):
"""
Compute windspeed from u and v
Parameters:
u(float) : zonal wind (m/s)
v(float) : meridional wind (m/s)
Returns:
float: wind speed (m/s)
"""
field = np.sqrt(np.square(u) + np.square(v))
np.where(field < 0.005, field, 0)
return field
def winddir(u,v):
"""
Compute windspeed from u and v
Parameters:
u(float) : zonal wind (m/s)
v(float) : meridional wind (m/s)
Returns:
float: wind direction
"""
return np.mod(np.rad2deg(np.arctan2(-u, -v)) + 180, 360)
#return np.mod(np.rad2deg(np.arctan2(v, u)) + 180, 360)
#return np.rad2deg(np.arctan2(u,v))
def ppm_2_con(co2value):
"""
Tranform ppmv to kg/m3
Parameters:
co2value (float) : co2 concentration in ppmv
Returns:
float : co2 concentration in kg/m3
"""
rho_co2 = 1.87 # kg/m3
return rho_co2*co2value*1e-6
def read_namelist(filename):
"""
Read yaml namelist file
Parameters:
filename (str) : path to the namelist file
Returns:
dictionary : dictionary with all the namelist information
"""
with open(filename) as f:
namelist = yaml.load(f, Loader=SafeLoader)
return namelist
def read_dataframe(filename):
"""
Read dataframe
Parameters:
filename (str) : path to the namelist file
Returns:
dataframe : including all the information regarding the variables
"""
return pd.read_csv(filename, sep=',',skipinitialspace = True)
def rewrite_output_file(namelist, pgw_case):
"""
Read yaml namelist file
Parameters:
namelist (dict) : information regarding the domain and the year
Returns:
dictionary: updated version of the namelist
"""
namelist['forcingfile'] = 'FORCING_HCLIM_'+namelist['domain']+'_'+str(namelist['year'])+'_'+pgw_case+'.nc'
return namelist
def ref_height():
f = Dataset('/nobackup/rossby27/users/sm_aital/analysis/surfex_online_vs_offline/forcing_files/FORCING_L65_2018.nc','r')
t65 = f.variables['Tair'][:,:]
f.close()
R = 8.3144598
g = 9.81
M = 0.0289644
L = 0.0065
C1 = 0.5*(0.997039230000000+1)
C2 = M*g/(R*L)
C3 = np.exp(np.log(C1)/C2)
z65 = t65*((1/(C3)-1))/L
return np.mean(z65,0)
def save_grid_info(namelist, pgw_case, time0, longitude0, latitude0):
"""
Create file with grid information and other variables
Parameters:
namelist (dict) : dictionary containing all the variables names
pgw_case (str) : PGW case
time0 (float) : time array
longitude0 (float) : longitude array
latitude0 (float) : latitude array
"""
new_file = Dataset(namelist['out_dir']+'tmp_forcing.nc','w')
# Number of points
num_points = longitude0.shape[0]*longitude0.shape[1]
print(num_points)
# Dimensions
time = new_file.createDimension('time', None)
numpoints = new_file.createDimension('Number_of_points', num_points)
namelist['numpoints'] = num_points
# time
print('Define time')
time = new_file.createVariable('time', 'f4', ('time'))
time[:]=time0-time0[0]
time.standard_name = "time" ;
time.units = "hours since "+str(namelist['year'])+"-05-25 00:00:00" ;
#time.units = "hours since "+str(namelist['year'])+"-07-02 00:00:00" ;
time.calendar = "standard" ;
time.axis = "T" ;
namelist['numtime'] = len(time)
# Latitude / Longitude
print('Define lat/lon')
latitude = new_file.createVariable('LAT','f4',('Number_of_points'))
longitude = new_file.createVariable('LON','f4',('Number_of_points'))
latitude[:] = np.reshape(latitude0, (num_points))
latitude.long_name = "Latitude" ;
longitude[:] = np.reshape(longitude0, (num_points))
longitude.long_name = "Longitude" ;
# Reference heights
print('Define reference height')
zref = new_file.createVariable('ZREF','f4',('Number_of_points'))
uref = new_file.createVariable('UREF','f4',('Number_of_points'))
#zref[:] = ref_height()
#zref[:] = 2.0
#zref[:] = 12.5
zref[:] = 50.0
zref.long_name = "Reference_Height" ;
zref.units = "m"
#uref[:] = ref_height()
#uref[:] = 10.0
#uref[:] = 12.5
uref[:] = 50.0
uref.long_name = "Reference_Height_for_Wind" ;
uref.units = "m"
# CO2
print('Define CO2')
co2 = new_file.createVariable('CO2air','f4',('time','Number_of_points'))
co2[:,:] = ppm_2_con(namelist['CO2'][pgw_case])
co2.units = "kg/m3"
co2.long_name = "Near_Surface_CO2_Concentration"
print('Close file')
new_file.close()
def read_grid_info_time(namelist):
"""
Read time and lat/lon info from file
Parameters:
namelist (dict) : dictionary containing all the variables names
Returns:
time0 (float): time variables
longitude (float): longitude information
latitude (float): latitude information
"""
nc = Dataset(namelist['ini_file'],'r')
# Longitude, latitude and time variables
longitude0 = nc.variables['lon'][:-11,:-11]
latitude0 = nc.variables['lat'][:-11,:-11]
time0 = nc.variables['time'][:]
nc.close()
return time0, longitude0, latitude0
def include_timestep(namelist):
"""
Include timestep information into forcing file
Parameters:
namelist (dict) : dictionary containing all the variables names
"""
# Time step
timestep = 3600.0
cmd1 = "ncap2 -O -s 'FRC_TIME_STP=double("+str(timestep)+")' "+namelist['out_dir']+"tmp_forcing.nc "+namelist['out_dir']+namelist['forcingfile']
cmd2 = "ncatted -a longname,FRC_TIME_STP,o,c,'Forcing_Time_Step' "+namelist['out_dir']+namelist['forcingfile']
cmd3 = "rm -rf "+namelist['out_dir']+"tmp_forcing.nc "
os.system(cmd1)
os.system(cmd2)
os.system(cmd3)
def include_var(namelist, var, pgw_case):
"""
Includes variables into forcing file
Parameters:
namelist (dict) : dictionary containing all the information about the input files
var(dict) : dictionary containing all the information regarding the variables
"""
if var.dim.values == 1:
include_var_1D(namelist, var)
elif var.dim.values == 2:
include_var_2D(namelist, var, pgw_case)
def extract_rawdata(filename, var1, var2):
"""
Extract data from input netcdf file
Parameters:
filename (str) : path to file
var1 (str) : name of the main variable
var2 (str) : name of secondary variable if needed
Returns
float : 1/2-D fields
"""
ncfile = Dataset(filename,'r')
try:
var1_nc = ncfile.variables[var1][:,:-11,:-11].data
except:
var1_nc = ncfile.variables[var1][:-11,:-11].data
if var2 is not np.nan:
var2_nc = ncfile.variables[var2][:,:-11,:-11].data
else:
var2_nc = None
ncfile.close()
return var1_nc, var2_nc
def reshape_var(namelist,var,dim):
"""
Reduce dimensions by reshaping
Parameters:
namelist (dict) : dictionary containing all the variables names
var (float) : variable to be reduced
dim (integer) : dimension of the out field
Returns
float : 1/2-D field
"""
if dim == 1:
return np.reshape(var[:,:], (namelist['numpoints']))
elif dim == 2:
return np.reshape(var, (var.shape[0],namelist['numpoints']))
def extract_data(namelist, var1, var2, fun, accu, sub, dim):
"""
Extract data from input netcdf file
Parameters:
namelist (dict) : dictionary containing all the variables names
var1 (str) : name of the main variable
var2 (str) : name of secondary variable if needed
fun (str) : function to be applied
accu (logical) : accumulated field
sub (logical) : subtract between var1 and var2
dim (integer) : dimension of the out field
Returns
float : 1/2-D field
"""
filename = namelist['ini_file']
var1_nc, var2_nc = extract_rawdata(filename, var1[0], var2[0])
var1_nc = reshape_var(namelist, var1_nc, dim)
if var2_nc is not None: var2_nc = reshape_var(namelist, var2_nc, dim)
if sub[0]: var1_nc = var1_nc - var2_nc
var_out = apply_fun(fun[0],var1_nc, var2_nc, sub[0])
return var_out
def apply_fun(fun, var1, var2, sub = False):
"""
Apply function to variable
Parameters:
fun (str) : name of function
var1 (float) : main variable
var2 (float) : secondary variable
sub(logical) : is it a variable that has been substracted?
Returns
float : 1/2-D field
"""
if fun is np.nan:
return var1
elif var2 is None or sub:
return eval(fun+"(var1)")
else:
return eval(fun+"(var1, var2)")
def include_var_1D(namelist, var):
"""
Include 1D variables into forcing file
Parameters:
namelist (dict) : dictionary containing all the information about the input files
var(dict) : dictionary containing all the information regarding the variables
"""
var_values = extract_data(namelist, var.var1.values, var.var2.values, var.function.values, var.accu.values, var.subst.values, 1)
new_file = Dataset(namelist['out_dir']+namelist['forcingfile'],'r+')
nc_var = new_file.createVariable(var.SFXname.values[0],'f4',('Number_of_points'))
var_values[np.where(var_values<0)] = 0.
nc_var[:] = var_values
nc_var.long_name = var.long_name.values[0].strip()
nc_var.units = var.units.values[0].strip()
new_file.close()
def include_var_2D(namelist, var, pgw_case):
"""
Include 2D variables into forcing file
Parameters:
namelist (dict) : dictionary containing all the information about the input files
var(dict) : dictionary containing all the information regarding the variables
"""
var_values = extract_data(namelist, var.var1.values, var.var2.values, var.function.values, var.accu.values, var.subst.values, 2)
new_file = Dataset(namelist['out_dir']+namelist['forcingfile'],'r+')
nc_var = new_file.createVariable(var.SFXname.values[0],'f4',('time','Number_of_points'))
var_values[np.where(var_values<0)] = 0.
nc_var[:,:] = var_values
nc_var.long_name = var.long_name.values[0].strip()
nc_var.units = var.units.values[0].strip()
new_file.close()
# ------------------------------------------------------------------------------
create_forcing_SURFEX_nc()