-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot-mean.py
200 lines (147 loc) · 5.56 KB
/
plot-mean.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
import getopt
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
from mpl_toolkits.axes_grid1 import make_axes_locatable
import cartopy.crs as ccrs
from cartopy import config
from cartopy.util import add_cyclic_point
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import cartopy.feature as cfeature
import cartopy.mpl.ticker as cticker
from netCDF4 import Dataset as netcdf_dataset
#=========================================================================
class GeneratePlot():
def __init__(self, debug=0, output=0):
self.debug = debug
self.output = output
self.set_default()
def plot(self, lons, lats, data):
#ax.coastlines(resolution='110m')
#ax.gridlines()
nrows = 1
ncols = 1
#set up the plot
proj = ccrs.PlateCarree()
fig, axs = plt.subplots(nrows=nrows,ncols=ncols,
subplot_kw=dict(projection=proj),
figsize=(11,8.5))
axs.set_global()
pvar = data
cyclic_data, cyclic_lons = add_cyclic_point(pvar, coord=lons)
cs=axs.contourf(cyclic_lons, lats, cyclic_data, transform=proj,
levels=self.clevs, extend=self.extend,
alpha=self.alpha, cmap=self.cmapname)
axs.set_extent([-180, 180, -90, 90], crs=proj)
axs.coastlines(resolution='auto', color='k')
axs.gridlines(color='lightgrey', linestyle='-', draw_labels=True)
axs.set_title(self.runname[0])
#Adjust the location of the subplots on the page to make room for the colorbar
fig.subplots_adjust(bottom=0.1, top=0.9, left=0.05, right=0.8,
wspace=0.02, hspace=0.02)
#Add a colorbar axis at the bottom of the graph
cbar_ax = fig.add_axes([0.85, 0.1, 0.05, 0.85])
#Draw the colorbar
cbar=fig.colorbar(cs, cax=cbar_ax, pad=self.pad, ticks=self.cblevs,
orientation='vertical')
cbar.set_label(self.label, rotation=90)
#Add a big title at the top
plt.suptitle(self.title)
fig.canvas.draw()
plt.tight_layout()
if(self.output):
if(self.imagename is None):
imagename = 't_aspect.png'
else:
imagename = self.imagename
plt.savefig(imagename)
plt.close()
else:
plt.show()
def set_default(self):
self.imagename = 'sample.png'
self.runname = ['Halo', 'RR', 'RR - Halo']
#cmapname = coolwarm, bwr, rainbow, jet, seismic
#self.cmapname = 'bwr'
#self.cmapname = 'coolwarm'
self.cmapname = 'rainbow'
#self.cmapname = 'jet'
self.clevs = np.arange(-0.2, 0.21, 0.01)
self.cblevs = np.arange(-0.2, 0.3, 0.1)
self.extend = 'both'
self.alpha = 0.5
self.pad = 0.1
self.orientation = 'horizontal'
self.size = 'large'
self.weight = 'bold'
self.labelsize = 'medium'
self.label = 'Unit (C)'
self.title = 'Temperature Increment'
def set_label(self, label='Unit (C)'):
self.label = label
def set_title(self, title='Temperature Increment'):
self.title = title
def set_clevs(self, clevs=[]):
self.clevs = clevs
def set_cblevs(self, cblevs=[]):
self.cblevs = cblevs
def set_imagename(self, imagename):
self.imagename = imagename
def set_cmapname(self, cmapname):
self.cmapname = cmapname
def set_runname(self, runname):
self.runname = runname
#--------------------------------------------------------------------------------
if __name__== '__main__':
debug = 1
output = 0
#datadir = '/work2/noaa/gsienkf/weihuang/jedi/develop/build/fv3-jedi/test/Data/analysis/letkf/gfs/mts'
datadir = '/work2/noaa/gsienkf/weihuang/jedi/develop/build/fv3-jedi/test'
filename = 'letkf.mid.00020201215_000000z.nc4'
opts, args = getopt.getopt(sys.argv[1:], '', ['debug=', 'output=',
'filename=', 'datadir='])
for o, a in opts:
if o in ('--debug'):
debug = int(a)
elif o in ('--output'):
output = int(a)
elif o in ('--datadir'):
datadir = a
elif o in ('--filename'):
filename = a
else:
assert False, 'unhandled option'
gp = GeneratePlot(debug=debug, output=output)
fullpath = '%s/%s' %(datadir, filename)
ncfile = netcdf_dataset(fullpath)
lats = ncfile.variables['lat'][:]
lons = ncfile.variables['lon'][:]
#-----------------------------------------------------------------------------------------
clevs = np.arange(200.0, 306.0, 1.0)
cblevs = np.arange(200.0, 310.0, 5.0)
gp.set_clevs(clevs=clevs)
gp.set_cblevs(cblevs=cblevs)
#-----------------------------------------------------------------------------------------
varlist = ['T', 'ua', 'va', 'sphum', 'delp', 'DZ', 'o3mr']
unitlist = ['Unit (C)', 'Unit (m/s)', 'Unit (m/s)',
'Unit (kg/kg)', 'Unit (Pa', 'Unit (m', 'Unit (ppm)']
#-----------------------------------------------------------------------------------------
for n in range(len(varlist)):
var = ncfile.variables[varlist[n]][0,:, :, :]
gp.set_runname([varlist[n]])
nlev, nlat, nlon = var.shape
print('var.shape = ', var.shape)
gp.set_label(unitlist[n])
for lev in range(5, nlev, 10):
data = var[lev,:,:]
title = '%s at Level %d' %(varlist[n], lev)
gp.set_title(title)
print('Plotting ', title)
print('\tdata.shape = ', data.shape)
print('\tdata.max: %f, data.min: %f' %(np.max(data), np.min(data)))
imagename = '%s_lev_%3.3d.png' %(varlist[n], lev)
gp.set_imagename(imagename)
gp.plot(lons, lats, data)
#-----------------------------------------------------------------------------------------
ncfile.close()