8 import cartopy.crs
as ccrs
10 import matplotlib.pyplot
as plt
28 print(
'\n ABORT: '+message+
'\n')
34 @click.option(
'--inputfile', required=
True, help=
'NetCDF input containing Lon/Lat fields')
35 @click.option(
'--fieldname', required=
True, help=
'Name of field to plot')
36 @click.option(
'--layer', required=
False, help=
'Model layer to plot. [reqiured if 3D variable]', type=int)
37 @click.option(
'--showfig', required=
False, help=
'Display figure if set to true', default=
False)
38 def main(inputfile, fieldname, layer, showfig):
41 print(
'\nOpening ', inputfile,
'for reading')
42 ncfile = netCDF4.Dataset(inputfile, mode=
'r')
45 npx = ncfile.dimensions[
"lon"].size
46 npy = ncfile.dimensions[
"lat"].size
47 npz = ncfile.dimensions[
"lev"].size
48 lons = ncfile.variables[
"lons"][:]
49 lats = ncfile.variables[
"lats"][:]
52 print(
" Grid dimensions", npx,
'x', npy,
'x', npz)
55 units = ncfile.variables[fieldname].units
58 field = np.zeros((npy, npx))
61 if len(ncfile.variables[fieldname].shape) == 4:
65 abort(
"If plotting 3D variable user must provide layer with --layer")
68 print(
" Reading layer ", layer,
" from field ", fieldname)
69 field[:,:] = ncfile.variables[fieldname][:,layer-1,:,:]
72 title =
"Contour of "+fieldname+
" ("+units+
") for layer "+str(layer)
73 outfile = os.path.splitext(inputfile)[0]+
"_"+fieldname+
"_layer-"+str(layer)+
".png"
75 elif len(ncfile.variables[fieldname].shape) == 3:
78 print(
" Reading field ", fieldname)
79 field[:,:] = ncfile.variables[fieldname][:,:]
80 title =
"Contour of "+fieldname+
" ("+units+
")"
81 outfile = os.path.splitext(inputfile)[0]+
"_"+fieldname+
".png"
89 cmax = np.max(np.abs(field))
95 cmap =
'nipy_spectral'
97 levels = np.linspace(cmin,cmax,25)
103 projection = ccrs.PlateCarree()
106 fig = plt.figure(figsize=(10, 5))
109 ax = fig.add_subplot(1, 1, 1, projection=projection)
112 im = ax.contourf(lons, lats, field,
113 transform=projection,
121 ax.set_xticks(np.linspace(-180, 180, 5), crs=projection)
122 ax.set_yticks(np.linspace(-90, 90, 5), crs=projection)
123 ax.set_xlabel(
'Longitude')
124 ax.set_ylabel(
'Latitude')
132 print(
" Saving figure as", outfile,
"\n")
140 if __name__ ==
'__main__':