-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_moguntia_new.py
358 lines (333 loc) · 13.8 KB
/
plot_moguntia_new.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
from pylab import *
import glob,os,sys
from datetime import *
try:
from ipywidgets import *
except:
from IPython.html.widgets import *
from IPython.display import display,clear_output
from mpl_toolkits.basemap import Basemap
matplotlib.rcParams.update({'font.size': 12})
class plot_moguntia_new:
def __init__(self,xlf):
self.xlf = xlf
self.automatic = True
self.nlev = 10
self.conv = 1.0
self.grid = True
self.overplot = False
self.xmax = 1e10
self.xmin = 0.0
self.tmin = datetime(1900,1,1,0,0,0)
self.tmax = datetime(2020,1,1,0,0,0)
self.stations = False
# to support python2.7 widgets, set up two types of windows:
# 1. windows that allow selection of multiple files (SelectMultiple)
# select output file, select overplot file, select station
# 2. widgets that can be passed through the interact call.
# grid (T/F), overplot (T/F), coneversion (ToggleButto
#
if len(xlf.outputfiles) == 0:
print("==============================================================")
print("No outputfiles generated, bailing out")
print("Please add STATION and/or OUTPUT statements in your input file")
print("==============================================================")
sys.exit(1)
options = []
for ii,filen in enumerate(xlf.outputfiles):
filen = filen[:-1]
options.append(filen)
self.wof = SelectMultiple(description="Output",options=options)
self.wof.width=200
self.wof.value=(options[0],)
self.wdoit = ToggleButton(description="Make Plots",value=False)
# if stations files is present, read station info:
for ioutput in options:
if (ioutput.endswith('stations')):
self.stations = True
ofile = open(os.path.join('OUTPUT',ioutput),'r')
lines = ofile.readlines()
ofile.close()
self.nstat = int(lines[0].split()[0])
self.navg = int(lines[0].split()[1])
self.station_names = []
data = []
for line in lines[self.nstat+1:]:
y = [float(x) for x in line.split()]
data.extend(y)
data = array(data)
self.xlen = shape(data)[0]
self.nrec = int(self.xlen/self.nstat)
self.data = data.reshape((self.nrec,self.nstat))
for i in range(self.nstat):
self.station_names.append(lines[i+1].split()[0])
# only create widget if station output:
self.wstat = SelectMultiple(description='Stations',options=self.station_names)
self.wstat.width=200
self.wstat.value=(self.station_names[0],)
#display(self.wstat)
# also create the overplot window:
os.chdir('MEASUREMENTS')
oplotfiles = glob.glob(self.xlf.name.upper()+'_*')
oplotfiles.sort()
os.chdir('..')
self.wo = SelectMultiple(description='Measurement Stations',options=oplotfiles)
self.wo.width=200
self.wo.visible = False
self.woplot=ToggleButton(Description='Overplot',value=False)
#display(self.wo)
if not self.stations:
self.station_names = ['no station']
self.wstat = SelectMultiple(description='',options=self.station_names)
self.wstat.width=200
self.wstat.value=(self.station_names[0],)
oplotfiles = ['no station']
self.wo = SelectMultiple(description='',options=oplotfiles)
self.wo.width=200
self.wo.visible = False
self.woplot=ToggleButton(Description='Overplot',value=False)
# start the main interaction window:
wmain = interact(self.plot_file,out=self.wof,stat=self.wstat,op=self.wo,
conversion=ToggleButtons(options=['mol/mol','ppm','ppb','ppt']),
overplot = self.woplot,
grid = self.grid,
automatic = self.automatic,
doit = self.wdoit)
# this is the main routine that will perform the plotting:
def plot_file(self,
out = SelectMultiple(),
stat = SelectMultiple(),
op = SelectMultiple(),
conversion=ToggleButtons(),
overplot = ToggleButton(),
grid = True,
automatic = True,
doit = ToggleButton):
self.wo.visible=overplot
self.grid = grid
self.conversion=conversion
if conversion=='mol/mol':
self.conv = 1.0
elif conversion =='ppm':
self.conv = 1e6
elif conversion == 'ppb':
self.conv = 1e9
elif conversion == 'ppt':
self.conv = 1e12
#for ioutput in self.wof.value:
if (not automatic) and (self.automatic):
self.automatic = False
self.wlev = IntSlider(min=5,max=30,value=self.nlev,description='nlev')
self.wmax = BoundedFloatText(value=self.xmax,max=10*self.xmax,min=self.xmin,description="Max")
self.wmin = BoundedFloatText(value=self.xmin,max=self.xmax,min=0.0,description="Min")
tmin = self.convert_datetime(self.tmin)
tmax = self.convert_datetime(self.tmax)
self.wtmax = BoundedFloatText(value=tmax,max=10*tmax,min=tmin,description="Tmax")
self.wtmin = BoundedFloatText(value=tmin,max=tmax,min=0.0,description="Tmin")
self.wi = interact(self.get_levels,
nlev=self.wlev,
Min=self.wmin,
Max=self.wmax,
Tmin=self.wtmin,
Tmax=self.wtmax)
elif automatic:
self.automatic = True
try:
self.wi.widget.close()
except:
None
else:
None
station_out = False
za_out = False
ll_out = False
for ioutput in out:
if (ioutput.endswith('stations')):
station_out = True
elif (ioutput.find('za.') != -1):
za_out = True
elif (ioutput.find('ll.') != -1):
ll_out = True
# conditional visibility:
if (not za_out) and (not ll_out):
try:
self.wlev.visible=False
except:
None
else:
try:
self.wlev.visible=True
except:
None
if station_out:
self.wstat.visible=True
self.woplot.visible=True
try:
self.wtmin.visible=True
self.wtmax.visible=True
except:
None
else:
self.wstat.visible=False
self.wo.visible=False
self.woplot.visible=False
try:
self.wtmin.visible=False
self.wtmax.visible=False
except:
None
if doit:
for ioutput in out:
if (ioutput.endswith('stations')):
self.plot_station()
elif (ioutput.find('za.') != -1):
self.plot_za(ioutput)
elif (ioutput.find('ll.') != -1):
self.plot_ll(ioutput)
self.wdoit.value=False
def get_levels(self, nlev = 10, Min = 0.0,Max = 10.0, Tmin=0.0, Tmax=2015.0):
self.tmin = self.convert_partial_year(Tmin)
self.tmax = self.convert_partial_year(Tmax)
def plot_station(self):
# set up time:
start_date = [int(self.xlf.start_date[0:4]),int(self.xlf.start_date[4:6]),int(self.xlf.start_date[6:8])]
xtime = self.navg*(arange(self.nrec)+0.5)/(12.0*360.0)+\
start_date[0]+(start_date[1]-1)/12.0+(start_date[2]-1)/360.0
idate = []
for itime in xtime:
idate.append(self.convert_partial_year(itime))
f,ax = subplots()
f.set_figheight(7)
f.set_figwidth(10)
f.autofmt_xdate()
stat = self.wstat.value
for istat,name in enumerate(self.station_names):
if name in self.wstat.value:
ax.plot(idate,self.data[:,istat]*self.conv,label=name)
if self.wo.visible:
for ostat in self.wo.value:
opl = open(os.path.join('MEASUREMENTS',ostat),'r')
ov = []
ot = []
for line in opl.readlines():
xx = line.split()
year = int(xx[0])
mnth = float(xx[1])
if mnth >= 13.0:
year+=1
mnth-=12.
imnth=int(mnth)
day = 30*(mnth-imnth)
if day <= 0: day = 15
if day < 1: day = 1
day = int(day)
ot.append(datetime(year,imnth,day,0,0,0))
ov.append(xx[2])
opl.close()
ax.plot(ot,ov,linestyle='dashed',label=ostat)
ax.set_ylabel(self.xlf.name + ' ('+self.conversion+')')
ax.set_xlabel('Time')
ax.legend(loc="best")
ax.grid(self.grid)
if self.automatic:
self.xmin = ax.get_ylim()[0]
self.xmax = ax.get_ylim()[1]
self.tmin = num2date(ax.get_xlim()[0])
self.tmax = num2date(ax.get_xlim()[1])
else:
ax.set_ylim((self.wmin.value,self.wmax.value))
ax.set_xlim((self.tmin,self.tmax))
f.show()
def convert_partial_year(self,number):
year = int(number)
d = timedelta(days=(number - year)*365)
day_one = datetime(year,1,1)
date = d + day_one
return date
def convert_datetime(self,date):
number = date.year + min((date.month*30 + date.day)/365.0,0.999)
return number
def plot_za(self,ioutput):
from copy import deepcopy
self.zaname=ioutput
if self.automatic:
self.nlev=10
else:
self.nlev=self.wlev.value
with open(os.path.join('OUTPUT',ioutput),'r') as ofile:
lines = ofile.readlines()
self.zafield = zeros((10,18))
test = array([float(x) for x in lines[0].split()])
self.zafield = test.reshape((10,18))
self.zafield*=self.conv
if self.automatic:
self.xmin = self.zafield.min()
self.xmax = self.zafield.max()
#self.xmin=Min
#self.xmax=Max
x = arange(18)*10. - 85.
y = 1000.0 - arange(10)*100.0
X,Y = meshgrid(x,y)
if self.automatic:
v = self.xmin + arange(self.nlev)*(self.xmax-self.xmin)/(self.nlev-1)
else:
v = self.wmin.value + arange(self.nlev)*(self.wmax.value-self.wmin.value)/(self.nlev-1)
pf = deepcopy(self.zafield)
pf = pf[:,::-1]
f,ax = subplots()
f.set_figheight(7)
f.set_figwidth(10)
ax1 = ax.contourf(X,Y,pf,v)
ax.set_title('Zonally-averaged concentration '+self.zaname)
ax.set_ylim([1000,100])
ax.set_ylabel('Pressure (hPa)')
ax.set_xlabel('Latitude')
ax.grid(self.grid)
cbar = colorbar(mappable=ax1,orientation='horizontal')
cbar.set_label(self.xlf.name + ' ('+self.conversion+')')
def plot_ll(self,ioutput):
from copy import deepcopy
self.llname=ioutput
if self.automatic:
self.nlev=10
else:
self.nlev=self.wlev.value
x = arange(36)*10. - 175.
y = arange(18)*10. - 85.
X,Y = meshgrid(x,y)
if self.automatic:
v = self.xmin + arange(self.nlev)*(self.xmax-self.xmin)/(self.nlev-1)
else:
v = self.wmin.value + arange(self.nlev)*(self.wmax.value-self.wmin.value)/(self.nlev-1)
with open(os.path.join('OUTPUT',ioutput),'r') as ofile:
lines = ofile.readlines()
nll = int(lines[0])
self.field = zeros((18,36,nll))
self.levels = []
i = 1
for l in range(nll):
self.levels.append(int(lines[i]))
test= array([float(x) for x in lines[i+1].split()])
self.field[:,:,l] = test.reshape((18,36))
i+=2
self.field*=self.conv
if self.automatic:
self.xmin = self.field.min()
self.xmax = self.field.max()
v = self.xmin + arange(self.nlev)*(self.xmax-self.xmin)/(self.nlev-1)
else:
v = self.wmin.value + arange(self.nlev)*(self.wmax.value-self.wmin.value)/(self.nlev-1)
for il,level in enumerate(self.levels):
f,ax = subplots()
f.set_figheight(7)
f.set_figwidth(10)
height = '%5i hPa'%(1100-level*100)
pf = deepcopy(self.field[:,:,il])
pf = roll(pf,18,axis=1)
pf = pf[::-1,:]
xmap = Basemap(projection='cyl',llcrnrlat=-90.,urcrnrlat=90.,llcrnrlon=-180.,urcrnrlon=180.,resolution='c')
xmap.drawcoastlines(linewidth=0.5,color='0.25')
ax1 = xmap.contourf(X,Y,pf,v)
ax.set_title('Concentration at '+height+' '+self.llname)
cbar = colorbar(mappable=ax1,orientation='horizontal')
cbar.set_label(self.xlf.name + ' ('+self.conversion+')')