- Notifications
You must be signed in to change notification settings - Fork 397
/
Copy pathplotozone.py
108 lines (89 loc) · 3.95 KB
/
plotozone.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
from __future__ import (absolute_import, division, print_function)
# make plot of ozone concentration data on
# lambert conformal conic map projection, drawing coastlines, state and
# country boundaries, and parallels/meridians.
# the data is interpolated to the native projection grid.
frommpl_toolkits.basemapimportBasemap, shiftgrid
importnumpyasnp
importmatplotlib.pyplotasplt
importnetCDF4
plt.rcParams['text.usetex'] =False
# read in netCDF4 file. Results from CAMx v6
# test case, converted to netcdf by PseudoNetCDF
# pseudonetcdf.googlecode.com
camx=netCDF4.Dataset('camx.sample.nc')
#alternatively read directly from CAMx uamiv file
#if available
#
# from PseudoNetCDF.camxfiles.Memmaps import uamiv
# camx = uamiv('camx.bin')
# Get Ozone Variable
o3=camx.variables['O3']
# Get projection space
llcrnrx=camx.XORIG
llcrnry=camx.YORIG
urcrnrx=llcrnrx+ (o3[:].shape[-1] +1) *camx.XCELL
urcrnry=llcrnry+ (o3[:].shape[-2] +1) *camx.XCELL
# Get edge values for pcolor
xedge=np.linspace(0, urcrnrx-llcrnrx, camx.NCOLS+1)
yedge=np.linspace(0, urcrnry-llcrnry, camx.NCOLS+1)
X, Y=np.meshgrid(xedge, yedge)
# setup of basemap ('lcc' = lambert conformal conic).
# projection parameters from CAMx file
m=Basemap(projection='lcc',
lon_0=camx.P_GAM, lat_0=40.,
lat_1=camx.P_ALP, lat_2=camx.P_BET,
llcrnrx=llcrnrx, llcrnry=llcrnry,
urcrnry=urcrnry, urcrnrx=urcrnrx)
# create the figure.
fig=plt.figure(figsize=(8,8))
# add an axes.
ax=fig.add_axes([0.1,0.1,0.8,0.8])
ax.set_facecolor('lightgrey')
# associate this axes with the Basemap instance.
m.ax=ax
# plot tile plot with pcolor
# Use first time and first layer (i.e., o3[0, 0] (time, layer, row, col))
# Edge cells have precisely 0 value, and are masked
# to avoid an unnecessary color range.
# Each color bin contains precisely 10% of values
# which makes for a pretty plot.
frommatplotlib.colorsimportListedColormap
WhGrYlBu=ListedColormap(['#ffffff', '#b7f6ff', '#70edff', '#29e4ff', '#00e1fb', '#0fffc6', '#3bffa4', '#68ff82', '#94ff60', '#c0ff3e', '#edff1c', '#fff400', '#ffc700', '#ff9b00', '#ff6e00', '#ff4200', '#ff1500', '#e80000', '#bb0000', '#8f0000'])
#.from_list('WhGrYlBu', ['white', 'white', 'cyan', 'lightblue', 'lightgreen', 'green', 'yellow', 'orange', 'red', 'red'])
toplot=np.ma.masked_values(o3[0, 0], 0.) *1000.
bounds=np.percentile(toplot.compressed().ravel(), np.linspace(5, 95, 9).tolist())
ptch=m.pcolor(X, Y, toplot, cmap=WhGrYlBu, norm=plt.matplotlib.colors.BoundaryNorm(bounds, 20))
# Add a colorbar using proportional spacing, but
# colors based on 10 distinct bins
cb=m.colorbar(ptch, location='right',pad='10%', boundaries=bounds, spacing='proportional', format='%.3f', extend='both') # draw colorbar
# Add units to the colorbar
cb.ax.set_xlabel('%s*1000.'%o3.units.strip())
# plot blue dot on Houston, Baton Rouge, and Atlanta
defadd_dot(lon, lat, label):
xpt,ypt=m(lon,lat)
m.plot([xpt],[ypt],'bo')
ax.annotate(label, xy=(xpt, ypt), xytext=(xpt+1e5, ypt+1e5),
bbox=dict(boxstyle="round4", fc="w"),
arrowprops=dict(facecolor='black'),
)
add_dot(-95.361328,29.754505, 'Houston')
add_dot(-91.140320, 30.458283, 'Baton Rouge')
add_dot(-84.387982, 33.748995, 'Atlanta')
# draw coastlines and political boundaries.
m.drawcoastlines()
m.drawcountries()
m.drawstates()
# draw parallels and meridians.
# label on left, right and bottom of map.
parallels=np.arange(20.,60,10.)
m.drawparallels(parallels,labels=[1,1,0,1])
meridians=np.arange(-120., 70.,10.)
m.drawmeridians(meridians,labels=[1,1,0,1])
# set title.
ax.set_title('O$_3$ as predicted by the CAMx v6 Test-Case\neach color division has 10% of cells 5-95% and 5% in triagles')
importtextwrap
histstr='Processing: %s'%'\n'.join(textwrap.wrap(camx.history.strip(), 140))
fig.text(0.01, 0.01, histstr, horizontalalignment='left', verticalalignment='bottom', size=8)
plt.draw()
plt.show()