-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMPLboundarylayer.py
More file actions
370 lines (324 loc) · 13.6 KB
/
MPLboundarylayer.py
File metadata and controls
370 lines (324 loc) · 13.6 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
# -*- coding: utf-8 -*-
"""
Created on Tue May 19 12:50:39 2015
@author: User
"""
import os,sys,glob
import numpy as np
import MPLtools as mtools
import MPLprocesstools as mproc
import MPLfileproc as mfile
import pandas as pan
import datetime
import pytz
import ephem
from scipy import signal
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
import seaborn as sns
def PBLanalyze(**kwargs):
"""
Program used to find and hsitogram PBL heights and capping layers
"""
filedir=kwargs.get('filedir',[])
location=kwargs.get('location',(48.9167,-125.5333))
altrange = kwargs.get('altrange',np.arange(0.150,15.030,0.030))
timestep = kwargs.get('timestep','60S')
molthresh=kwargs.get('molthresh',1.0)
layernoisethresh=kwargs.get('layernoisethresh',1.0)
bg_alt=kwargs.get('bg_alt',None)
datatype=kwargs.get('datatype','NRB')
winsize=kwargs.get('winsize',5)
wavelet=kwargs.get('wavelet',signal.ricker)
noisethresh=kwargs.get('noisethresh',0.4)
cloudthresh=kwargs.get('cloudthresh',(1.0,0.20))
CWTwidth=kwargs.get('CWTwidth',2)
minwidth=kwargs.get('minwidth',4)
layerCWTrange=kwargs.get('layerCWTrange',np.arange(2,5))
PBLwavelet=kwargs.get('PBLwavelet',mproc.dog)
PBLCWTrange=kwargs.get('PBLCWTrange',np.arange(2,10))
PBLwidth=kwargs.get('PBLwidth',7)
sigma0=kwargs.get('sigma0',0.01)
waterthresh=kwargs.get('waterthresh',0.10)
icethresh=kwargs.get('icethresh',0.35)
smokethresh=kwargs.get('smokethresh',0.10)
dustthresh=kwargs.get('dustthresh',0.20)
verbose=kwargs.get('verbose',False)
if not filedir:
filedir=mtools.set_dir('Select file directory to process')
olddir=os.getcwd()
os.chdir(filedir)
rawfiles = glob.glob('*.mpl')
rawfiles.sort()
[path,startfile] = os.path.split(rawfiles[0])
[path,endfile] = os.path.split(rawfiles[-1])
startdate = kwargs.get('startdate',mfile.MPLtodatetime(startfile))
enddate = kwargs.get('enddate',mfile.MPLtodatetime(endfile))
mpllist=[]
datelist=[]
PBL=pan.DataFrame()
caplayer=pan.DataFrame()
clearcap=pan.DataFrame()
layerkwargs={'altrange':altrange,'timestep':timestep,'bg_alt':bg_alt,'datatype':datatype,
'molthresh':molthresh,'winsize':winsize,'layernoisethresh':layernoisethresh,
'wavelet':wavelet,'noisethresh':noisethresh,'cloudthresh':cloudthresh,
'CWTwidth':CWTwidth,'minwidth':minwidth,'layerCWTrange':layerCWTrange,
'PBLwavelet':PBLwavelet,'PBLCWTrange':PBLCWTrange,'PBLwidth':PBLwidth,'sigma0':sigma0,
'waterthresh':waterthresh,'icethresh':icethresh,'smokethresh':smokethresh,
'dustthresh':dustthresh}
filedates=[mfile.MPLtodatetime(os.path.split(f)[1]) for f in rawfiles]
filteredfiles=[(r,t) for r,t in zip(rawfiles,filedates) if (t>=startdate) and (t<=enddate)]
oldday=filedates[0]
tempephem,tempperiods=get_ephemera(location,oldday)
tempmpl=[]
tempdate=[]
ephemera=[]
periods=[]
for f,d in filteredfiles:
newday=d
if newday.date()==oldday.date():
tempmpl.append(f)
tempdate.append(d)
else:
mpllist.append(tempmpl)
datelist.append(tempdate)
ephemera.append(tempephem)
periods.append(tempperiods)
tempmpl=[f]
tempdate=[d]
tempephem,tempperiods=get_ephemera(location,newday)
if f==filteredfiles[-1][0]:
mpllist.append(tempmpl)
datelist.append(tempdate)
ephemera.append(tempephem)
periods.append(tempperiods)
oldday=newday
repnum=1
for mplgrp,datgrp,e,p in zip(mpllist,datelist,ephemera,periods):
if verbose:
print 'Processing Group {0} of {1}'.format(repnum,len(mplgrp))
repnum+=1
for n in range(len(e)-1):
tempmpl=[m for m,t in zip(mplgrp,datgrp) if (t>=e[n])and(t<e[n+1])]
if len(tempmpl)==0:
continue
else:
templayerdict=get_layers(tempmpl,**layerkwargs)
tempcap,tempclear,tempPBL=find_capper(templayerdict,p[n])
PBL=PBL.append(tempPBL)
caplayer=caplayer.append(tempcap)
clearcap=clearcap.append(tempclear)
os.chdir(olddir)
return PBL,caplayer,clearcap
def get_ephemera(loc,datein,localzone=pytz.timezone('US/Pacific')):
daystart=datetime.datetime(datein.year,datein.month,datein.day,0,0)
daystart_local=localzone.localize(daystart)
daystart_utc=daystart_local.astimezone(pytz.utc)
dayend=daystart+datetime.timedelta(days=1)
o=ephem.Observer()
o.lat=str(loc[0])
o.long=str(loc[1])
sun=ephem.Sun()
sunrise=ephem.localtime(o.next_rising(sun,start=daystart_utc))
solarnoon=ephem.localtime(o.next_transit(sun,start=daystart_utc))
sunset=ephem.localtime(o.next_setting(sun,start=daystart_utc))
solarmidnight=ephem.localtime(o.next_antitransit(sun,start=daystart_utc))
if solarmidnight>sunrise:
periods=['Night','Morning','Afternoon','Evening','Night']
ephemera=[daystart,sunrise,solarnoon,sunset,solarmidnight,dayend]
else:
periods=['Evening','Night','Morning','Afternoon','Evening']
ephemera=[daystart,solarmidnight,sunrise,solarnoon,sunset,dayend]
return ephemera,periods
def get_layers(rawfiles,**kwargs):
altrange = kwargs.get('altrange',np.arange(0.150,15.03,0.030))
timestep = kwargs.get('timestep','60S')
molthresh=kwargs.get('molthresh',1.0)
layernoisethresh=kwargs.get('layernoisethresh',1.0)
bg_alt=kwargs.get('bg_alt',None)
datatype=kwargs.get('datatype','NRB')
winsize=kwargs.get('winsize',5)
wavelet=kwargs.get('wavelet',signal.ricker)
noisethresh=kwargs.get('noisethresh',0.4)
cloudthresh=kwargs.get('cloudthresh',(1.0,0.20))
CWTwidth=kwargs.get('CWTwidth',2)
minwidth=kwargs.get('minwidth',4)
layerCWTrange=kwargs.get('layerCWTrange',np.arange(2,5))
PBLwavelet=kwargs.get('PBLwavelet',mproc.dog)
PBLCWTrange=kwargs.get('PBLCWTrange',np.arange(2,10))
sigma0=kwargs.get('sigma0',0.01)
waterthresh=kwargs.get('waterthresh',0.10)
icethresh=kwargs.get('icethresh',0.35)
smokethresh=kwargs.get('smokethresh',0.10)
dustthresh=kwargs.get('dustthresh',0.20)
starttime=kwargs.get('starttime',datetime.datetime(1970,1,1))
endtime=kwargs.get('endtime',datetime.datetime.today())
# MPLdat_event=mtools.MPL()
for r in rawfiles:
[path,tempname] = os.path.split(r)
if starttime <= mfile.MPLtodatetime(tempname) <= endtime:
MPLdat_temp = mtools.MPL()
MPLdat_temp.fromMPL(tempname)
MPLdat_temp.alt_resample(altrange)
try:
MPLdat_event.append(MPLdat_temp)
except NameError:
MPLdat_event = MPLdat_temp
#sort by index to make certain data is in order then set date ranges to match
MPLdat_event.header.sort_index()
for n in range(MPLdat_event.header['numchans'][0]):
data = MPLdat_event.data[n]
data.sort_index()
MPLdat_event.time_resample(timestep)
MPLdat_event.calc_all()
layerkwargs={'timestep':timestep,'bg_alt':bg_alt,'datatype':datatype,
'molthresh':molthresh,'winsize':winsize,'layernoisethresh':layernoisethresh,
'wavelet':wavelet,'noisethresh':noisethresh,'cloudthresh':cloudthresh,
'CWTwidth':CWTwidth,'minwidth':minwidth,'layerCWTrange':layerCWTrange,
'PBLwavelet':PBLwavelet,'PBLCWTrange':PBLCWTrange,'sigma0':sigma0,
'waterthresh':waterthresh,'icethresh':icethresh,'smokethresh':smokethresh,
'dustthresh':dustthresh}
layerdict=mproc.findalllayers(mplin=MPLdat_event,**layerkwargs)
return layerdict
def find_capper(layerdict,dayperiod,typemode='Sub-Type'):
maxalt=layerdict['mpl'].NRB[0].columns[-1]
mplindex=layerdict['mpl'].NRB[0].index
try:
molalt=layerdict['molecular']['Layer0']['Base']
except KeyError:
molalt=pan.series(data=maxalt,index=mplindex)
try:
layeralt=layerdict['layers']['Layer0']['Base']
layertype=layerdict['layers']['Layer0'][typemode]
except KeyError:
layeralt=pan.Series(data=maxalt,index=mplindex)
layertype=pan.Series(data=np.nan,index=mplindex)
PBLalt=layerdict['pbl']
molalt.fillna(maxalt,inplace=True)
layeralt.fillna(maxalt,inplace=True)
captype=pan.DataFrame(index=mplindex,columns=[dayperiod])
clearcap=pan.DataFrame(index=mplindex,columns=[dayperiod])
PBL=pan.DataFrame(index=mplindex,columns=[dayperiod])
for i in captype.index:
if layeralt.ix[i]<molalt.ix[i]:
captype.ix[i]=layertype.ix[i]
PBL.ix[i]=PBLalt.ix[i]
clearcap.ix[i]='Other'
else:
captype.ix[i]=np.nan
PBL.ix[i]=np.nan
clearcap.ix[i]='Clear Air'
return captype,clearcap,PBL
#def pieplot(dfin,**kwargs):
#
# numfigs=len(plt.get_fignums())
# if type(dfin.values[0,0])==str:
# dfplot=dfin.apply(pan.value_counts)
# else:
# dfplot=dfin
#
# labels=dfplot.index.values
# fig=plt.figure(numfigs+1)
# subplot_counter=1
# for c in dfplot.columns:
# subplot_label=c
# try:
# ax=fig.add_subplot(layout[0],layout[1],subplot_counter)
# except NameError:
# ax=fig.add_subplot(1,len(dfplot.columns)+1,subplot_counter)
#
# ax.pie(dfplot[c].values,autopct='%1.1f%%',radius=2,pctdistance=2.2)
# ax.set_title(c)
# ax.axis('equal')
# subplot_counter+=1
#
# return fig
def barplot(dfin,**kwargs):
scaled=kwargs.get('scaled',True)
stacked=kwargs.get('stacked',True)
numfigs=len(plt.get_fignums())
if scaled==True:
dfplot=100.0*dfin.div(dfin.sum(axis=1), axis=0)
fmt='%.0f%%'
else:
dfplot=dfin
fmt='%.00f%'
sns.set_palette("Set3", len(dfplot.columns))
sns.set_context('poster')
sns.set_style('darkgrid',{'legend.frameon':True})
ax=dfplot.plot(kind='barh',stacked=stacked)
xticks=mtick.FormatStrFormatter(fmt)
ax.xaxis.set_major_formatter(xticks)
def violinplot(dfin,**kwargs):
sns.violinplot(dfin)
# dfplot = [v.dropna() for k, v in dfin.iteritems()]
# ax=sns.violinplot(dfplot)
#
# xlabels=dfin.columns.values
# xpos=range(len(xlabels))
# plt.xticks(xpos,xlabels)
# ax.set_axis_labels(xlabels)
if __name__=='__main__':
# os.chdir('K:\\All_MPL_Data')
# os.chdir('/data/lv1/pcottle/MPLData/Raw_Files')
timestep='240S'
print 'Processing UBC1 files'
altrange=np.arange(0.150,2.0,0.03)
startdate=datetime.datetime(2013,8,27,0)
enddate=datetime.datetime(2014,1,6,23)
location=(48.0256,-123.25)
PBL,caplayer,clearcap=PBLanalyze(timestep=timestep,altrange=altrange,
startdate=startdate,enddate=enddate,location=location,
filedir='/data/lv1/pcottle/MPLData/Raw_Files',verbose=True)
# store=pan.HDFStore('K:\\All_MPL_Stats\\Boundary Layer\\UBC1_Boundary_Layer_stats_all.h5')
store=pan.HDFStore('/data/lv1/pcottle/MPLStats/UBC1_Boundary_Layer_stats_all.h5')
store['PBL']=PBL
store['caplayer']=caplayer
store['clearcap']=clearcap
store.close()
del PBL,caplayer,clearcap
print 'Processing Whistler files'
startdate=datetime.datetime(2014,1,7,0)
enddate=datetime.datetime(2014,4,10,23)
location=(50.1447,-122.1606)
PBL,caplayer,clearcap=PBLanalyze(timestep=timestep,altrange=altrange,
startdate=startdate,enddate=enddate,location=location,
filedir='/data/lv1/pcottle/MPLData/Raw_Files',verbose=True)
store=pan.HDFStore('/data/lv1/pcottle/MPLStats/Whistler_Boundary_Layer_stats_all.h5')
store['PBL']=PBL
store['caplayer']=caplayer
store['clearcap']=clearcap
store.close()
del PBL,caplayer,clearcap
print 'Processing Ucluelet files'
startdate=datetime.datetime(2014,4,11,0)
enddate=datetime.datetime(2014,7,22,23)
location=(48.9167,-125.5333)
PBL,caplayer,clearcap=PBLanalyze(timestep=timestep,altrange=altrange,
startdate=startdate,enddate=enddate,location=location,
filedir='/data/lv1/pcottle/MPLData/Raw_Files',verbose=True)
store=pan.HDFStore('/data/lv1/pcottle/MPLStats/Ucluelet_Boundary_Layer_stats_all.h5')
store['PBL']=PBL
store['caplayer']=caplayer
store['clearcap']=clearcap
store.close()
del PBL,caplayer,clearcap
print 'Processing UBC2 files'
startdate=datetime.datetime(2015,5,5,0)
enddate=datetime.datetime(2015,12,31,23)
location=(48.0256,-123.25)
PBL,caplayer,clearcap=PBLanalyze(timestep=timestep,altrange=altrange,
startdate=startdate,enddate=enddate,location=location,
filedir='/data/lv1/pcottle/MPLData/Raw_Files',verbose=True)
store=pan.HDFStore('/data/lv1/pcottle/MPLStats/UBC2_Boundary_Layer_stats_all.h5')
store['PBL']=PBL
store['caplayer']=caplayer
store['clearcap']=clearcap
store.close()
# violinplot(PBL)
#
# capplot=caplayer.apply(pan.value_counts).transpose()
# barplot(capplot)
# clearplot=clearcap.apply(pan.value_counts).transpose()
# barplot(clearplot)
# piefig=pieplot(caplayer)