-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsoca_vrfy.py
executable file
·316 lines (266 loc) · 11.5 KB
/
soca_vrfy.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
#!/usr/bin/env python3
# make plots for marine analysis
import matplotlib.pyplot as plt
import xarray as xr
import cartopy
import cartopy.crs as ccrs
import numpy as np
import os
projs = {'North': ccrs.NorthPolarStereo(),
'South': ccrs.SouthPolarStereo(),
'Global': ccrs.Mollweide(central_longitude=-150)}
def plotConfig(grid_file=[],
data_file=[],
layer_file=[],
variable=[],
PDY=os.getenv('PDY'),
cyc=os.getenv('cyc'),
exp=os.getenv('PSLOT'),
levels=[],
bounds=[],
colormap=[],
max_depth=np.nan,
max_depths=[700.0, 5000.0],
vrfyout='',
comout='',
variables_horiz={},
variables_zonal={},
variables_meridional={},
lat=np.nan,
lats=np.arange(-60, 60, 10),
lon=np.nan,
lons=np.arange(-280, 80, 30),
proj='set me',
projs=['Global']):
# Map variable names to their units
variable_units = {
'ave_ssh': 'meter',
'Temp': 'deg C',
'Salt': 'psu',
'aice_h': 'unitless',
'hi_h': 'meter',
'hs_h': 'meter',
'u': 'm/s',
'v': 'm/s'
}
"""
Prepares the configuration for the plotting functions below
"""
config = {}
config['vrfyout'] = vrfyout # output directory
config['comout'] = comout # output directory
config['grid file'] = grid_file
config['fields file'] = data_file
config['layer file'] = layer_file
config['PDY'] = PDY
config['cyc'] = cyc
config['exp'] = exp
config['levels'] = [1]
config['colormap'] = colormap
config['bounds'] = bounds
config['lats'] = lats # all the lats to plot
config['lat'] = lat # the lat being currently plotted
config['lons'] = lons # all the lons to plot
config['lon'] = lon # the lon being currently plotted
config['max depths'] = max_depths # all the max depths to plot
config['max depth'] = max_depth # the max depth currently plotted
config['horiz variables'] = variables_horiz # all the vars for horiz plots
config['zonal variables'] = variables_zonal # all the vars for zonal plots
config['meridional variables'] = variables_meridional # all the vars for meridional plots
config['variable'] = variable # the variable currently plotted
config['projs'] = projs # all the projections etc.
config['proj'] = proj
# Add units to the config for each variable
config['variable_units'] = variable_units
return config
def plotHorizontalSlice(config):
"""
Contourf of a horizontal slice of an ocean field
"""
grid = xr.open_dataset(config['grid file'])
data = xr.open_dataset(config['fields file'])
dirname = os.path.join(config['vrfyout'], config['variable'])
os.makedirs(dirname, exist_ok=True)
variable = config['variable']
unit = config['variable_units'].get(config['variable'], 'unknown')
exp = config['exp']
PDY = config['PDY']
cyc = config['cyc']
if variable in ['Temp', 'Salt', 'u', 'v']:
level = config['levels'][0]
slice_data = np.squeeze(data[variable])[level, :, :]
label_colorbar = f"{variable} ({unit}) Level {level}"
figname = os.path.join(dirname, variable + '_Level_' + str(level))
title = f"{exp} {PDY} {cyc} {variable} Level {level}"
else:
slice_data = np.squeeze(data[variable])
label_colorbar = f"{variable} ({unit})"
figname = os.path.join(dirname, variable + '_' + config['proj'])
title = f"{exp} {PDY} {cyc} {variable}"
bounds = config['horiz variables'][variable]
slice_data = np.clip(slice_data, bounds[0], bounds[1])
fig, ax = plt.subplots(figsize=(8, 5), subplot_kw={'projection': projs[config['proj']]})
# Use pcolor to plot the data
pcolor_plot = ax.pcolormesh(np.squeeze(grid.lon),
np.squeeze(grid.lat),
slice_data,
vmin=bounds[0], vmax=bounds[1],
transform=ccrs.PlateCarree(),
cmap=config['colormap'],
zorder=0)
# Add colorbar for filled contours
cbar = fig.colorbar(pcolor_plot, ax=ax, shrink=0.75, orientation='horizontal')
cbar.set_label(label_colorbar)
# Add contour lines with specified linewidths
contour_levels = np.linspace(bounds[0], bounds[1], 5)
ax.contour(np.squeeze(grid.lon),
np.squeeze(grid.lat),
slice_data,
levels=contour_levels,
colors='black',
linewidths=0.1,
transform=ccrs.PlateCarree(),
zorder=2)
try:
ax.coastlines() # TODO: make this work on hpc
except Exception as e:
print(f"Warning: could not add coastlines. {e}")
ax.set_title(title)
if config['proj'] == 'South':
ax.set_extent([-180, 180, -90, -50], ccrs.PlateCarree())
if config['proj'] == 'North':
ax.set_extent([-180, 180, 50, 90], ccrs.PlateCarree())
# ax.add_feature(cartopy.feature.LAND) # TODO: make this work on hpc
plt.savefig(figname, bbox_inches='tight', dpi=300)
plt.close(fig)
def plotZonalSlice(config):
"""
Contourf of a zonal slice of an ocean field
"""
variable = config['variable']
unit = config['variable_units'].get(config['variable'], 'unknown')
exp = config['exp']
PDY = config['PDY']
cyc = config['cyc']
lat = float(config['lat'])
grid = xr.open_dataset(config['grid file'])
data = xr.open_dataset(config['fields file'])
layer = xr.open_dataset(config['layer file'])
lat_index = np.argmin(np.array(np.abs(np.squeeze(grid.lat)[:, 0] - lat)))
slice_data = np.squeeze(np.array(data[variable]))[:, lat_index, :]
depth = np.squeeze(np.array(layer['h']))[:, lat_index, :]
depth[np.where(np.abs(depth) > 10000.0)] = 0.0
depth = np.cumsum(depth, axis=0)
bounds = config['zonal variables'][variable]
slice_data = np.clip(slice_data, bounds[0], bounds[1])
x = np.tile(np.squeeze(grid.lon[:, lat_index]), (np.shape(depth)[0], 1))
fig, ax = plt.subplots(figsize=(8, 5))
# Plot the filled contours
contourf_plot = ax.contourf(x, -depth, slice_data,
levels=np.linspace(bounds[0], bounds[1], 100),
vmin=bounds[0], vmax=bounds[1],
cmap=config['colormap'])
# Add contour lines with specified linewidths
contour_levels = np.linspace(bounds[0], bounds[1], 5)
ax.contour(x, -depth, slice_data,
levels=contour_levels,
colors='black',
linewidths=0.1)
# Add colorbar for filled contours
cbar = fig.colorbar(contourf_plot, ax=ax, shrink=0.5, orientation='horizontal')
cbar.set_label(f"{config['variable']} ({unit}) Lat {lat}")
# Set the colorbar ticks
cbar.set_ticks(contour_levels)
contourf_plot.set_clim(bounds[0], bounds[1])
ax.set_ylim(-config['max depth'], 0)
title = f"{exp} {PDY} {cyc} {variable} lat {int(lat)}"
ax.set_title(title)
dirname = os.path.join(config['vrfyout'], config['variable'])
os.makedirs(dirname, exist_ok=True)
figname = os.path.join(dirname, config['variable'] +
'zonal_lat_' + str(int(lat)) + '_' + str(int(config['max depth'])) + 'm')
plt.savefig(figname, bbox_inches='tight', dpi=300)
plt.close(fig)
def plotMeridionalSlice(config):
"""
Contourf of a Meridional slice of an ocean field
"""
variable = config['variable']
unit = config['variable_units'].get(config['variable'], 'unknown')
exp = config['exp']
PDY = config['PDY']
cyc = config['cyc']
lon = float(config['lon'])
grid = xr.open_dataset(config['grid file'])
data = xr.open_dataset(config['fields file'])
layer = xr.open_dataset(config['layer file'])
lon_index = np.argmin(np.array(np.abs(np.squeeze(grid.lon)[0, :] - lon)))
slice_data = np.squeeze(np.array(data[config['variable']]))[:, :, lon_index]
depth = np.squeeze(np.array(layer['h']))[:, :, lon_index]
depth[np.where(np.abs(depth) > 10000.0)] = 0.0
depth = np.cumsum(depth, axis=0)
bounds = config['meridional variables'][variable]
slice_data = np.clip(slice_data, bounds[0], bounds[1])
y = np.tile(np.squeeze(grid.lat)[:, lon_index], (np.shape(depth)[0], 1))
fig, ax = plt.subplots(figsize=(8, 5))
# Plot the filled contours
contourf_plot = ax.contourf(y, -depth, slice_data,
levels=np.linspace(bounds[0], bounds[1], 100),
vmin=bounds[0], vmax=bounds[1],
cmap=config['colormap'])
# Add contour lines with specified linewidths
contour_levels = np.linspace(bounds[0], bounds[1], 5)
ax.contour(y, -depth, slice_data,
levels=contour_levels,
colors='black',
linewidths=0.1)
# Add colorbar for filled contours
cbar = fig.colorbar(contourf_plot, ax=ax, shrink=0.5, orientation='horizontal')
cbar.set_label(f"{config['variable']} ({unit}) Lon {lon}")
# Set the colorbar ticks
cbar.set_ticks(contour_levels)
contourf_plot.set_clim(bounds[0], bounds[1])
ax.set_ylim(-config['max depth'], 0)
title = f"{exp} {PDY} {cyc} {variable} lon {int(lon)}"
ax.set_title(title)
dirname = os.path.join(config['vrfyout'], config['variable'])
os.makedirs(dirname, exist_ok=True)
figname = os.path.join(dirname, config['variable'] +
'meridional_lon_' + str(int(lon)) + '_' + str(int(config['max depth'])) + 'm')
plt.savefig(figname, bbox_inches='tight', dpi=300)
plt.close(fig)
class statePlotter:
def __init__(self, config_dict):
self.config = config_dict
def plot(self):
# Loop over variables, slices (horiz and vertical) and projections ... and whatever else is needed
#######################################
# zonal slices
for lat in self.config['lats']:
self.config['lat'] = lat
for max_depth in self.config['max depths']:
self.config['max depth'] = max_depth
variableBounds = self.config['zonal variables']
for variable in variableBounds.keys():
bounds = variableBounds[variable]
self.config.update({'variable': variable, 'bounds': bounds})
plotZonalSlice(self.config)
#######################################
# Meridional slices
for lon in self.config['lons']:
self.config['lon'] = lon
for max_depth in self.config['max depths']:
self.config['max depth'] = max_depth
variableBounds = self.config['meridional variables']
for variable in variableBounds.keys():
bounds = variableBounds[variable]
self.config.update({'variable': variable, 'bounds': bounds})
plotMeridionalSlice(self.config)
#######################################
# Horizontal slices
for proj in self.config['projs']:
variableBounds = self.config['horiz variables']
for variable in variableBounds.keys():
bounds = variableBounds[variable]
self.config.update({'variable': variable, 'bounds': bounds, 'proj': proj})
plotHorizontalSlice(self.config)