-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvrfy_script.py
282 lines (254 loc) · 13.5 KB
/
vrfy_script.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
import os
import numpy as np
import gen_eva_obs_yaml
import marine_eva_post
from multiprocessing import Process
from soca_vrfy import statePlotter, plotConfig
import subprocess
import glob
comout = os.getenv('COM_OCEAN_ANALYSIS')
# resolve the comout path since it may contain wild cards
matching_paths = glob.glob(comout)
if matching_paths:
comout = matching_paths[0] # Assuming you want the first match
print(comout)
else:
print(comout)
print("No matching paths found")
exit(1)
del matching_paths
com_ice_history = os.getenv('COM_ICE_HISTORY_PREV')
# resolve the comout path since it may contain wild cards
matching_paths = glob.glob(com_ice_history)
if matching_paths:
com_ice_history = matching_paths[0] # Assuming you want the first match
print(com_ice_history)
else:
print(com_ice_history)
print("No matching paths found")
exit(1)
del matching_paths
com_ocean_history = os.getenv('COM_OCEAN_HISTORY_PREV')
# resolve the comout path since it may contain wild cards
matching_paths = glob.glob(com_ocean_history)
if matching_paths:
com_ocean_history = matching_paths[0] # Assuming you want the first match
print(com_ocean_history)
else:
print(com_ocean_history)
print("No matching paths found")
exit(1)
del matching_paths
cyc = os.getenv('cyc')
RUN = os.getenv('RUN')
bcyc = str((int(cyc) - 3) % 24).zfill(2)
gcyc = str((int(cyc) - 6) % 24).zfill(2)
grid_file = os.path.join(comout, f'{RUN}.t'+bcyc+'z.ocngrid.nc')
layer_file = os.path.join(comout, f'{RUN}.t'+cyc+'z.ocninc.nc')
# Check if the file exists, then decide on grid_file
if not os.path.exists(grid_file):
# TODO: Make this work on other HPC
grid_file = '/scratch1/NCEPDEV/da/common/validation/vrfy/gdas.t21z.ocngrid.nc'
# for eva
diagdir = os.path.join(comout, 'diags')
HOMEgdas = os.getenv('HOMEgdas')
# Get flags from environment variables (set in the bash driver)
plot_ensemble_b = os.getenv('PLOT_ENSEMBLE_B', 'OFF').upper() == 'ON'
plot_parametric_b = os.getenv('PLOT_PARAMETRIC_B', 'OFF').upper() == 'ON'
plot_background = os.getenv('PLOT_BACKGROUND', 'OFF').upper() == 'ON'
plot_increment = os.getenv('PLOT_INCREMENT', 'OFF').upper() == 'ON'
plot_analysis = os.getenv('PLOT_ANALYSIS', 'OFF').upper() == 'ON'
eva_plots = os.getenv('EVA_PLOTS', 'OFF').upper() == 'ON'
# output directory
vrfyout = os.getenv('VRFYOUT', './vrfyout')
print('------------------------- vrfyout:', vrfyout)
# Initialize an empty list for the main config
configs = []
# Analysis plotting configuration
if plot_analysis:
configs_ana = [plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, f'{RUN}.t'+cyc+'z.ocnana.nc'),
variables_horiz={'ave_ssh': [-1.8, 1.3],
'Temp': [-1.8, 34.0],
'Salt': [32, 40]},
colormap='nipy_spectral',
vrfyout=os.path.join(vrfyout, 'vrfy', 'ana')), # ocean surface analysis
plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, f'{RUN}.t'+cyc+'z.iceana.nc'),
variables_horiz={'aice_h': [0.0, 1.0],
'hi_h': [0.0, 4.0],
'hs_h': [0.0, 0.5]},
colormap='jet',
projs=['North', 'South', 'Global'],
vrfyout=os.path.join(vrfyout, 'vrfy', 'ana'))] # sea ice analysis
configs.extend(configs_ana)
# Ensemble B plotting configuration
if plot_ensemble_b:
config_ens = [plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, f'{RUN}.t{cyc}z.ocn.recentering_error.nc'),
variables_horiz={'ave_ssh': [-1, 1]},
colormap='seismic',
vrfyout=os.path.join(vrfyout, 'vrfy', 'recentering_error')), # recentering error
plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, f'{RUN}.t{cyc}z.ocn.ssh_steric_stddev.nc'),
variables_horiz={'ave_ssh': [0, 0.8]},
colormap='gist_ncar',
vrfyout=os.path.join(vrfyout, 'vrfy', 'bkgerr', 'ssh_steric_stddev')), # ssh steric stddev
plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, f'{RUN}.t{cyc}z.ocn.ssh_unbal_stddev.nc'),
variables_horiz={'ave_ssh': [0, 0.8]},
colormap='gist_ncar',
vrfyout=os.path.join(vrfyout, 'vrfy', 'bkgerr', 'ssh_unbal_stddev')), # ssh unbal stddev
plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, f'{RUN}.t{cyc}z.ocn.ssh_total_stddev.nc'),
variables_horiz={'ave_ssh': [0, 0.8]},
colormap='gist_ncar',
vrfyout=os.path.join(vrfyout, 'vrfy', 'bkgerr', 'ssh_total_stddev')), # ssh total stddev
plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, f'{RUN}.t{cyc}z.ocn.steric_explained_variance.nc'),
variables_horiz={'ave_ssh': [0, 1]},
colormap='seismic',
vrfyout=os.path.join(vrfyout, 'vrfy', 'bkgerr', 'steric_explained_variance'))] # steric explained variance
configs.extend(config_ens)
# Parametric B plotting configuration
if plot_parametric_b:
config_bkgerr = [plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, os.path.pardir, os.path.pardir,
'bmatrix', 'ice', f'{RUN}.t'+cyc+'z.ice.bkgerr_stddev.nc'),
variables_horiz={'aice_h': [0.0, 0.5],
'hi_h': [0.0, 2.0],
'hs_h': [0.0, 0.2]},
colormap='jet',
projs=['North', 'South', 'Global'],
vrfyout=os.path.join(vrfyout, 'vrfy', 'bkgerr')), # sea ice bkgerr stddev
plotConfig(grid_file=grid_file,
layer_file=layer_file,
data_file=os.path.join(comout, os.path.pardir, os.path.pardir,
'bmatrix', 'ocean', f'{RUN}.t'+cyc+'z.ocean.bkgerr_stddev.nc'),
lats=np.arange(-60, 60, 10),
lons=np.arange(-280, 80, 30),
variables_zonal={'Temp': [0, 2],
'Salt': [0, 0.2],
'u': [0, 0.5],
'v': [0, 0.5]},
variables_meridional={'Temp': [0, 2],
'Salt': [0, 0.2],
'u': [0, 0.5],
'v': [0, 0.5]},
variables_horiz={'Temp': [0, 2],
'Salt': [0, 0.2],
'u': [0, 0.5],
'v': [0, 0.5],
'ave_ssh': [0, 0.1]},
colormap='jet',
vrfyout=os.path.join(vrfyout, 'vrfy', 'bkgerr'))] # ocn bkgerr stddev
configs.extend(config_bkgerr)
# Background plotting configuration
if plot_background:
config_bkg = [plotConfig(grid_file=grid_file,
data_file=os.path.join(com_ice_history, f'{RUN}.ice.t{gcyc}z.inst.f006.nc'),
variables_horiz={'aice_h': [0.0, 1.0],
'hi_h': [0.0, 4.0],
'hs_h': [0.0, 0.5]},
colormap='jet',
projs=['North', 'South', 'Global'],
vrfyout=os.path.join(vrfyout, 'vrfy', 'bkg')), # sea ice background
plotConfig(grid_file=grid_file,
layer_file=layer_file,
data_file=os.path.join(com_ocean_history, f'{RUN}.ocean.t{gcyc}z.inst.f006.nc'),
lats=np.arange(-60, 60, 10),
lons=np.arange(-280, 80, 30),
variables_zonal={'Temp': [-1.8, 34.0],
'Salt': [32, 40],
'u': [-1.0, 1.0],
'v': [-1.0, 1.0]},
variables_meridional={'Temp': [-1.8, 34.0],
'Salt': [32, 40],
'u': [-1.0, 1.0],
'v': [-1.0, 1.0]},
variables_horiz={'ave_ssh': [-1.8, 1.3],
'Temp': [-1.8, 34.0],
'Salt': [32, 40],
'u': [-1.0, 1.0],
'v': [-1.0, 1.0]},
colormap='nipy_spectral',
vrfyout=os.path.join(vrfyout, 'vrfy', 'bkg'))]
configs.extend(config_bkg)
# Increment plotting configuration
if plot_increment:
config_incr = [plotConfig(grid_file=grid_file,
layer_file=layer_file,
data_file=os.path.join(comout, f'{RUN}.t'+cyc+'z.ocninc.nc'),
lats=np.arange(-60, 60, 10),
lons=np.arange(-280, 80, 30),
variables_zonal={'Temp': [-0.5, 0.5],
'Salt': [-0.1, 0.1]},
variables_horiz={'Temp': [-0.5, 0.5],
'Salt': [-0.1, 0.1],
'ave_ssh': [-0.1, 0.1]},
variables_meridional={'Temp': [-0.5, 0.5],
'Salt': [-0.1, 0.1]},
colormap='seismic',
vrfyout=os.path.join(vrfyout, 'vrfy', 'incr')), # ocean increment
plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, f'{RUN}.t'+cyc+'z.ice.incr.nc'),
lats=np.arange(-60, 60, 10),
variables_horiz={'aice_h': [-0.2, 0.2],
'hi_h': [-0.5, 0.5],
'hs_h': [-0.1, 0.1]},
colormap='seismic',
projs=['North', 'South'],
vrfyout=os.path.join(vrfyout, 'vrfy', 'incr')), # sea ice increment
plotConfig(grid_file=grid_file,
data_file=os.path.join(comout, f'{RUN}.t'+cyc+'z.ice.incr.postproc.nc'),
lats=np.arange(-60, 60, 10),
variables_horiz={'aice_h': [-0.2, 0.2],
'hi_h': [-0.5, 0.5],
'hs_h': [-0.1, 0.1]},
colormap='seismic',
projs=['North', 'South'],
vrfyout=os.path.join(vrfyout, 'vrfy', 'incr.postproc'))] # sea ice increment after postprocessing
configs.extend(config_incr)
# Plot the marine verification figures
def plot_marine_vrfy(config):
ocnvrfyPlotter = statePlotter(config)
ocnvrfyPlotter.plot()
# Number of processes
num_processes = len(configs)
# Create a list to store the processes
processes = []
# Iterate over configs
for config in configs[:num_processes]:
process = Process(target=plot_marine_vrfy, args=(config,))
process.start()
processes.append(process)
# Wait for all processes to finish
for process in processes:
process.join()
# Run EVA
if eva_plots:
evadir = os.path.join(HOMEgdas)
marinetemplate = os.path.join(evadir, 'marine_gdas_plots.yaml')
varyaml = os.path.join(comout, 'yaml', 'var.yaml')
# it would be better to refrence the dirs explicitly with the comout path
# but eva doesn't allow for specifying output directories
os.chdir(os.path.join(vrfyout, 'vrfy'))
if not os.path.exists('preevayamls'):
os.makedirs('preevayamls')
if not os.path.exists('evayamls'):
os.makedirs('evayamls')
gen_eva_obs_yaml.gen_eva_obs_yaml(varyaml, marinetemplate, 'preevayamls')
files = os.listdir('preevayamls')
for file in files:
infile = os.path.join('preevayamls', file)
marine_eva_post.marine_eva_post(infile, 'evayamls', diagdir)
files = os.listdir('evayamls')
for file in files:
infile = os.path.join('evayamls', file)
print('running eva on', infile)
subprocess.run(['eva', infile], check=True)
#######################################
# calculate diag statistics
#######################################
# As of 11/12/2024 not working
# diag_statistics.get_diag_stats()