-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_masks_for_variogram_multiprocessing.py
More file actions
292 lines (253 loc) · 13.2 KB
/
save_masks_for_variogram_multiprocessing.py
File metadata and controls
292 lines (253 loc) · 13.2 KB
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
import numpy as np
import matplotlib.pyplot as plt
import sys
import glob
import os
from multiprocessing import Pool, cpu_count
import time
import h5py
import hdf5plugin
import pandas as pd
import xarray as xr
from netCDF4 import Dataset
import cartopy.crs as crs
import random
plt.style.use('ggplot')
from pprint import pprint
import seaborn as sns
import matplotlib.ticker as ticker
import read_vars_WRF_RAMS
def get_time_from_RAMS_file(INPUT_FILE):
cur_time = os.path.split(INPUT_FILE)[1][4:21] # Grab time string from RAMS file
pd_time = pd.to_datetime(cur_time[0:10]+' '+cur_time[11:13]+":"+cur_time[13:15]+":"+cur_time[15:17])
return pd_time.strftime('%Y-%m-%d %H:%M:%S'), pd_time.strftime('%Y%m%d%H%M%S')
def read_head(headfile,h5file):
# Function that reads header files from RAMS
# Inputs:
# headfile: header file including full path in str format
# h5file: h5 datafile including full path in str format
# Returns:
# zmn: height levels for momentum values (i.e., grid box upper and lower levels)
# ztn: height levels for thermodynaic values (i.e., grid box centers)
# nx:: the number of x points for the domain associated with the h5file
# ny: the number of y points for the domain associated with the h5file
# npa: the number of surface patches
dom_num = h5file[h5file.index('.h5')-1] # Find index of .h5 to determine position showing which nest domain to use
with open(headfile) as f:
contents = f.readlines()
idx_zmn = contents.index('__zmn0'+dom_num+'\n')
nz_m = int(contents[idx_zmn+1])
zmn = np.zeros(nz_m)
for i in np.arange(0,nz_m):
zmn[i] = float(contents[idx_zmn+2+i])
idx_ztn = contents.index('__ztn0'+dom_num+'\n')
nz_t = int(contents[idx_ztn+1])
ztn = np.zeros(nz_t)
for i in np.arange(0,nz_t):
ztn[i] = float(contents[idx_ztn+2+i])
ztop = np.max(ztn) # Model domain top (m)
# Grad the size of the horizontal grid spacing
idx_dxy = contents.index('__deltaxn\n')
dxy = float(contents[idx_dxy+1+int(dom_num)].strip())
idx_npatch = contents.index('__npatch\n')
npa = int(contents[idx_npatch+2])
idx_ny = contents.index('__nnyp\n')
idx_nx = contents.index('__nnxp\n')
ny = np.ones(int(contents[idx_ny+1]))
nx = np.ones(int(contents[idx_ny+1]))
for i in np.arange(0,len(ny)):
nx[i] = int(contents[idx_nx+2+i])
ny[i] = int(contents[idx_ny+2+i])
ny_out = ny[int(dom_num)-1]
nx_out = nx[int(dom_num)-1]
return zmn, ztn, nx_out, ny_out, dxy, npa
simulations=['USA1.1-R','ARG1.1-R_old','PHI1.1-R','PHI2.1-R','WPO1.1-R','BRA1.1-R','DRC1.1-R','AUS1.1-R']
domain='3'
def save_storm_mask_ITC(simulation_name):
Cp=1004.
Rd=287.0
p00 = 100000.0
path = '/monsoon/MODEL/LES_MODEL_DATA/'+simulation_name+'/G3/out_30s/'
# Grab all the rams files
h5filepath = path+'a-L*g3.h5'
h5files1 = sorted(glob.glob(h5filepath))
hefilepath = path+'a-L*head.txt'
hefiles1 = sorted(glob.glob(hefilepath))
print(' first file: ',h5files1[0])
print(' last file: ',h5files1[-1])
middle_file = h5files1[int(len(h5files1)/2)]
timestr = get_time_from_RAMS_file(middle_file)[1]
da=xr.open_dataset(middle_file,engine='h5netcdf', phony_dims='sort')
domain_z_dim,domain_y_dim,domain_x_dim=np.shape(da.WP)
print(' ',domain_z_dim)
print(' ',domain_y_dim)
print(' ',domain_x_dim)
zm, zt, nx, ny, dxy, npa = read_head(hefiles1[0],h5files1[0])
print(' calculating ITC...')
total_condensate = da['RTP']-da['RV']
# Load variables needed to calculate density
th = da['THETA']
nx = np.shape(th)[2]
ny = np.shape(th)[1]
pi = da['PI']
rv = da['RV']
# Convert RAMS native variables to temperature and pressure
pres = np.power((pi/Cp),Cp/Rd)*p00
temp = th*(pi/Cp)
del(th,pi)
# Calculate atmospheric density
dens = pres/(Rd*temp*(1+0.61*rv))
del(pres,temp,rv)
# Difference in heights (dz)
diff_zt_3D = np.tile(np.diff(zt),(int(ny),int(nx),1))
diff_zt_3D = np.moveaxis(diff_zt_3D,2,0)
itc = np.nansum(total_condensate[1:,:,:]*dens[1:,:,:]*diff_zt_3D,axis=0)
output_var = itc/997.0*1000 # integrated total frozen condensate in mm
output_var_mod = np.where(output_var > 2, 1 , np.nan)
plt.imshow(output_var_mod,cmap='GnBu')
plt.colorbar()
print(' done estimating ITC for ',simulation_name)
output_var_mask = np.where(output_var > 2.0, 1.0 , np.nan)
storm_mask_filename = 'storm_mask_'+simulation_name+'_'+timestr+'.npy'
print(' saving the storm mask to ',storm_mask_filename)
with open(storm_mask_filename, 'wb') as f:
np.save(f, output_var_mask)
return
def save_storm_mask_surface_precip_w(WHICH_TIME, SIMULATIONS, DOMAIN, PLOT):
print('creating a storm mask')
if DOMAIN=='1':
dx = 1.6
if DOMAIN=='2':
dx=0.4
if DOMAIN=='3':
dx=0.1
for ii,simulation in enumerate(SIMULATIONS):
print(' working on simulation: ',simulation)
if DOMAIN=='1' or DOMAIN =='2':
rams_files=sorted(glob.glob('/monsoon/MODEL/LES_MODEL_DATA/'+simulation+'/G'+DOMAIN+'/out/'+'a-A-*g'+DOMAIN+'.h5'))# CSU machine
if DOMAIN=='3':
rams_files=sorted(glob.glob('/monsoon/MODEL/LES_MODEL_DATA/'+simulation+'/G'+DOMAIN+'/out_30s/'+'a-L-*g3.h5'))# CSU machine
print(' total # files = ',len(rams_files))
print(' first file is ',rams_files[0])
print(' last file is ',rams_files[-1])
if WHICH_TIME=='start':
rams_fil = rams_files[0]
if WHICH_TIME=='middle':
rams_fil = rams_files[int(len(rams_files)/2)]
if WHICH_TIME=='end':
rams_fil = rams_files[-1]
print(' choosing the middle file: ',rams_fil)
da = xr.open_dataset(rams_fil,engine='h5netcdf', phony_dims='sort')
precip = da['PCPRR'].values # kgm**-2
#print(' shape of precip rate is ',np.shape(z))
w = da['WP'].max(dim='phony_dim_3')
print(' precip rate min = ',np.nanmin(precip),' max = ',np.nanmax(precip))
#print(' max w is ',np.nanmax(w))
# create a mask
output_var_mask = np.where(precip > 0.0001, 1.0 , np.nan)#*np.where(qtc > 0.0001, 1.0 , np.nan)
storm_mask_filename = 'storm_mask_precip_'+simulation+'_'+get_time_from_RAMS_file(rams_fil)[1]+'.npy'
print(' saving the storm mask to ',storm_mask_filename)
with open(storm_mask_filename, 'wb') as f:
np.save(f, output_var_mask)
if PLOT:
fig = plt.figure(figsize=(8,8))
mask_contours = plt.imshow(output_var_mask)#,cmap='GnBu')
plt.contour(w,levels=np.arange(5,60,10),colors='k')
timestep_string = get_time_from_RAMS_file(rams_fil)[0]
#timestep_string = pd.to_datetime(z_time,format='%Y%m%d%H%M%S').strftime('%Y-%m-%d %H:%M:%S')
plt.title('storm_mask_surface_precip'+simulation+'\n'+timestep_string)
plt.colorbar(mask_contours)
plt.savefig('storm_mask_surface_precip'+simulation+'_'+get_time_from_RAMS_file(rams_fil)[1]+'.png')
def save_storm_mask_upper_level_precip_w_qtc(WHICH_TIME, LEVEL, LEVEL_TYPE, SIMULATIONS, DOMAIN, PLOT, MASK_CRITERIA):
print('creating a storm mask for ',LEVEL_TYPE,' level ',LEVEL)
if DOMAIN=='1':
dx = 1.6
if DOMAIN=='2':
dx=0.4
if DOMAIN=='3':
dx=0.1
for ii,simulation in enumerate(SIMULATIONS):
print(' working on simulation: ',simulation)
if DOMAIN=='1' or DOMAIN =='2':
rams_files=sorted(glob.glob('/monsoon/MODEL/LES_MODEL_DATA/'+simulation+'/G'+DOMAIN+'/out/'+'a-A-*g'+DOMAIN+'.h5'))# CSU machine
if DOMAIN=='3':
rams_files=sorted(glob.glob('/monsoon/MODEL/LES_MODEL_DATA/'+simulation+'/G'+DOMAIN+'/out_30s/'+'a-L-*g3.h5'))# CSU machine
print(' total # files = ',len(rams_files))
print(' first file is ',rams_files[0])
print(' last file is ',rams_files[-1])
if WHICH_TIME=='start':
rams_fil = rams_files[0]
if WHICH_TIME=='middle':
rams_fil = rams_files[int(len(rams_files)/2)]
if WHICH_TIME=='end':
rams_fil = rams_files[-1]
print(' choosing the ',WHICH_TIME,' file: ',rams_fil)
w, w_name, w_units, w_time = read_vars_WRF_RAMS.read_variable(rams_fil,'W','RAMS',output_height=False,interpolate=True,level=LEVEL,interptype=LEVEL_TYPE)
qtc, qtc_name, qtc_units, qtc_time = read_vars_WRF_RAMS.read_variable(rams_fil,'QTC','RAMS',output_height=False,interpolate=True,level=LEVEL,interptype=LEVEL_TYPE)
#precip, precip_name, precip_units, precip_time = read_vars_WRF_RAMS.read_variable(rams_fil,'PCP_RATE_3D','RAMS',output_height=False,interpolate=True,level=LEVEL,interptype=LEVEL_TYPE)
print(' w min = ',np.nanmin(w),' max = ',np.nanmax(w))
print(' qtc min = ',np.nanmin(qtc),' max = ',np.nanmax(qtc))
#print(' precip min = ',np.nanmin(precip),' max = ',np.nanmax(precip))
# create a mask
if MASK_CRITERIA=='qtc_0.00001_w_2':
output_var_mask = np.where(qtc > 0.00001, 1.0 , np.nan)*np.where(w > 2.0, 1.0 , np.nan)
storm_mask_filename = 'storm_mask_'+MASK_CRITERIA+'_'+LEVEL_TYPE+'_level_'+str(int(LEVEL))+'_'+simulation+'_'+get_time_from_RAMS_file(rams_fil)[1]+'.npy'
print(' saving the storm mask to ',storm_mask_filename)
with open(storm_mask_filename, 'wb') as f:
np.save(f, output_var_mask)
if MASK_CRITERIA=='qtc_0.00001_w_1':
output_var_mask = np.where(qtc > 0.00001, 1.0 , np.nan)*np.where(w > 1.0, 1.0 , np.nan)
storm_mask_filename = 'storm_mask_'+MASK_CRITERIA+'_'+LEVEL_TYPE+'_level_'+str(int(LEVEL))+'_'+simulation+'_'+get_time_from_RAMS_file(rams_fil)[1]+'.npy'
print(' saving the storm mask to ',storm_mask_filename)
with open(storm_mask_filename, 'wb') as g:
np.save(g, output_var_mask)
if MASK_CRITERIA=='qtc_0.00001':
output_var_mask = np.where(qtc > 0.00001, 1.0 , np.nan)
storm_mask_filename = 'storm_mask_'+MASK_CRITERIA+'_'+LEVEL_TYPE+'_level_'+str(int(LEVEL))+'_'+simulation+'_'+get_time_from_RAMS_file(rams_fil)[1]+'.npy'
print(' saving the storm mask to ',storm_mask_filename)
with open(storm_mask_filename, 'wb') as h:
np.save(h, output_var_mask)
if PLOT:
fig = plt.figure(figsize=(8,8))
mask_contours = plt.imshow(output_var_mask)#,cmap='GnBu')
plt.contour(w,levels=np.arange(5,60,10),colors='k')
timestep_string = get_time_from_RAMS_file(rams_fil)[0]
#timestep_string = pd.to_datetime(z_time,format='%Y%m%d%H%M%S').strftime('%Y-%m-%d %H:%M:%S')
plt.title('storm_mask: '+MASK_CRITERIA+' ;'+simulation+'\n'+timestep_string)
plt.savefig('storm_mask_'+MASK_CRITERIA+'_'+simulation+'_'+w_time+'.png',dpi=150)
plt.colorbar(mask_contours)
#save_storm_mask_surface_precip_w('end',simulations,domain,False)
# for pressure_lev in [750,500,200]:
# #for mask_type in ['qtc_0.00001_w_2','qtc_0.00001_w_1','qtc_0.00001']:
# for mask_criteria in ['qtc_0.00001']:
# for sim_time in ['start','end']:
# save_storm_mask_upper_level_precip_w_qtc('start', pressure_lev, 'pressure', simulations, domain, True, mask_criteria)
# print('-----------\n\n')
#save_storm_mask_surface_precip_w('middle', simulations, domain, True)
# sims=['RSA1.1-R']
# for sim in sims:
# print('working on simulation: ',sim)
# save_storm_mask(sim)
#save_variogram_masked_domain('not_near_storm','middle',variables[0], simulations, sample_size, domain, nsamples, colors, True)
print('working on domain' ,domain)
#Running on the terminal in parallel
argument = []
for sim_time in ['start','middle','end']:
argument = argument + [(sim_time, simulations, domain, False)]
print('length of argument is: ',len(argument))
# # ############################### FIRST OF ALL ################################
cpu_count1 = 37 #cpu_count()
print('number of cpus: ',cpu_count1)
# # #############################################################################
def main(FUNCTION, ARGUMENT):
start_time = time.perf_counter()
with Pool(processes = (cpu_count1-1)) as pool:
data = pool.starmap(FUNCTION, ARGUMENT)
finish_time = time.perf_counter()
print(f"Program finished in {finish_time-start_time} seconds")
#df_all = pd.concat(data, ignore_index=True)
#thermo_indices_data_csv_file = csv_folder+'thermodynamic_indices_' + DOMAIN + '_comb_track_filt_01_02_50_02_sr5017_setpos.csv'
#print('saving thermodynamic indices to the file: ',thermo_indices_data_csv_file)
#df_all.to_csv(thermo_indices_data_csv_file) # sounding data
if __name__ == "__main__":
main(save_storm_mask_surface_precip_w, argument)