-
Notifications
You must be signed in to change notification settings - Fork 0
/
wprof_cfad.py
561 lines (461 loc) · 19.1 KB
/
wprof_cfad.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 17 09:23:07 2016
@author: raul
"""
'''
Raul Valenzuela
raul.valenzuela@colorado.edu
Example:
from wprof_cfad import cfad
out=cfad(year=[1998],wdsurf=125,wdwpro=170,
rainbb=0.25,raincz=0.25,nhours=2)
out.plot('wspd',add_average=True,add_median=True)
'''
import parse_data
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
from rv_utilities import pandas2stack, add_colorbar
from tta_analysis2 import tta_analysis
class cfad:
def __init__(self,year=[],wdsurf=None, wdwpro=None,
rainbb=None, raincz=None, nhours=None):
out = processv2(year=year,wdsurf=wdsurf,
wdwpro=wdwpro,rainbb=rainbb,
raincz=raincz, nhours=nhours)
self.spd_hist = out[0]
self.dir_hist = out[1]
self.u_hist = out[2]
self.v_hist = out[3]
self.spd_cfad = out[4]
self.dir_cfad = out[5]
self.u_cfad = out[6]
self.v_cfad = out[7]
self.bins_spd = out[8]
self.bins_dir = out[9]
self.bins_u = out[10]
self.bins_v = out[11]
self.hgts = out[12]
self.wp_hours = out[13]
self.tta_hours = out[14]
self.notta_hours = out[15]
self.spd_average = out[16]
self.dir_average = out[17]
self.u_average = out[18]
self.v_average = out[19]
self.spd_median = out[20]
self.dir_median = out[21]
self.u_median = out[22]
self.v_median = out[23]
self.wdsurf = wdsurf
self.wdwpro = wdwpro
self.rainbb = rainbb
self.raincz = raincz
self.nhours = nhours
self.year = year
def plot(self,target,axes=None,pngsuffix=False, pdfsuffix=False,
contourf=True, add_median=False,add_average=False,
add_title=True, add_cbar=True,cbar_label=None,show=True,
subax_label=True,top_altitude=4000,
orientation=None):
name={'wdir':'Wind Direction',
'wspd':'Wind Speed',
'u':'u-wind',
'v':'v-wind'}
if target == 'wdir':
cfad = self.dir_cfad
median = self.dir_median
average = self.dir_average
bins = self.bins_dir
hist_xticks = np.arange(0,420,60)
hist_xlim = [0,360]
elif target == 'wspd':
cfad = self.spd_cfad
median = self.spd_median
average = self.spd_average
bins = self.bins_spd
hist_xticks = np.arange(0,40,5)
hist_xlim = [0,35]
elif target == 'u':
cfad = self.u_cfad
median = self.u_median
average = self.u_average
bins = self.bins_u
hist_xticks = np.arange(-14,22,4)
hist_xlim = [-14,20]
elif target == 'v':
cfad = self.v_cfad
median = self.v_median
average = self.v_average
bins = self.bins_v
hist_xticks = np.arange(-14,24,4)
hist_xlim = [-14,20]
if axes is None:
fig,axs = plt.subplots(1,3,sharey=True,figsize=(10,8))
ax1 = axs[0]
ax2 = axs[1]
ax3 = axs[2]
else:
ax1 = axes[0]
ax2 = axes[1]
ax3 = axes[2]
hist_wp = np.squeeze(cfad[:,:,0])
hist_wptta = np.squeeze(cfad[:,:,1])
hist_wpnotta = np.squeeze(cfad[:,:,2])
x = bins
y = self.hgts
if contourf:
X,Y = np.meshgrid(x,y)
nancol = np.zeros((40,1))+np.nan
hist_wp = np.hstack((hist_wp,nancol))
hist_wptta = np.hstack((hist_wptta,nancol))
hist_wpnotta = np.hstack((hist_wpnotta,nancol))
vmax=15
nlevels = 6
delta = int(vmax/nlevels)
v = np.arange(2,vmax+delta,delta)
cmap = cm.get_cmap('plasma')
ax1.contourf(X,Y,hist_wp,v,cmap=cmap)
p = ax2.contourf(X,Y,hist_wptta,v,cmap=cmap)
ax3.contourf(X,Y,hist_wpnotta,v,cmap=cmap)
lcolor = (0.6,0.6,0.6)
lw = 3
ax1.vlines(0,0,4000,linestyle='--',color=lcolor,lw=lw)
ax2.vlines(0,0,4000,linestyle='--',color=lcolor,lw=lw)
ax3.vlines(0,0,4000,linestyle='--',color=lcolor,lw=lw)
lw=3
if add_median:
ax1.plot(median[:,0],self.hgts,color='w',lw=lw)
ax2.plot(median[:,1],self.hgts,color='w',lw=lw)
ax3.plot(median[:,2],self.hgts,color='w',lw=lw)
if add_average:
ax1.plot(average[:,0],self.hgts,color='w',lw=lw)
ax2.plot(average[:,1],self.hgts,color='w',lw=lw)
ax3.plot(average[:,2],self.hgts,color='w',lw=lw)
else:
p = ax1.pcolormesh(x,y,hist_wp,cmap='viridis')
ax2.pcolormesh(x,y,hist_wptta,cmap='viridis')
ax3.pcolormesh(x,y,hist_wpnotta,cmap='viridis')
''' add color bar '''
if add_cbar is True:
add_colorbar(ax3,p,size='4%',loc='right',
label=cbar_label)
' --- setup ax1 --- '
ax1.set_xticks(hist_xticks)
ax1.set_xlim(hist_xlim)
ax1.set_ylim([0,top_altitude])
ax1.set_ylabel('Altitude [m] MSL')
' --- setup ax2 --- '
ax2.set_xticks(hist_xticks)
ax2.set_xlim(hist_xlim)
ax2.set_ylim([0,top_altitude])
ax2.set_xlabel(name[target])
' --- setup ax3 --- '
ax3.set_xticks(hist_xticks)
ax3.set_xlim(hist_xlim)
ax3.set_ylim([0,top_altitude])
''' add subaxis label '''
if orientation == 'horizontal':
vpos=1.05
if subax_label is True:
txt = 'All profiles (n={})'.format(self.wp_hours)
ax1.text(0.5,vpos,txt,fontsize=15,weight='bold',
transform=ax1.transAxes,va='center',ha='center')
txt = 'TTA (n={})'.format(self.tta_hours)
ax2.text(0.5,vpos,txt,fontsize=15,weight='bold',
transform=ax2.transAxes,va='center',ha='center')
txt = 'NO-TTA (n={})'.format(self.notta_hours)
ax3.text(0.5,vpos,txt,fontsize=15, weight='bold',
transform=ax3.transAxes,va='center',ha='center')
elif orientation == 'vertical':
hpos=1.05
if subax_label is True:
txt = 'All profiles (n={})'.format(self.wp_hours)
ax1.text(hpos,0.5,txt,fontsize=15,weight='bold',
transform=ax1.transAxes,va='center',
ha='center',rotation=-90)
txt = 'TTA (n={})'.format(self.tta_hours)
ax2.text(hpos,0.5,txt,fontsize=15,weight='bold',
transform=ax2.transAxes,va='center',
ha='center',rotation=-90)
txt = 'NO-TTA (n={})'.format(self.notta_hours)
ax3.text(hpos,0.5,txt,fontsize=15, weight='bold',
transform=ax3.transAxes,va='center',
ha='center',rotation=-90)
''' add title '''
if add_title is True:
title = 'Normalized frequencies of BBY wind profiles {} \n'
title += 'TTA wdir_surf:{}, wdir_wp:{}, '
title += 'rain_bby:{}, rain_czd:{}, nhours:{}'
if len(self.year) == 1:
yy = 'year {}'.format(self.year[0])
else:
yy = 'year {} to {}'.format(self.year[0],self.year[-1])
plt.suptitle(title.format(yy, self.wdsurf,
self.wdwpro, self.rainbb, self.raincz, self.nhours),
fontsize=15)
if pngsuffix:
out_name = 'wprof_{}_cfad{}.png'
plt.savefig(out_name.format(target,pngsuffix))
plt.close()
elif pdfsuffix:
out_name = 'wprof_{}_cfad{}.pdf'
plt.savefig(out_name.format(target,pdfsuffix))
plt.close()
if show is True:
plt.show()
return {'axes':[ax1,ax2,ax3],'im':p}
def processv2(year=[],wdsurf=None,
wdwpro=None,rainbb=None,
raincz=None, nhours=None):
''' v2: target loop moved into year loop '''
binss={'wdir': np.arange(0,370,10),
'wspd': np.arange(0,36,1),
'u': np.arange(-15,21,1),
'v': np.arange(-14,21,1),
}
target = ['wdir','wspd']
arrays = {}
wsp = np.empty((40,1))
wsp_tta = np.empty((40,1))
wsp_notta = np.empty((40,1))
wdr = np.empty((40,1))
wdr_tta = np.empty((40,1))
wdr_notta = np.empty((40,1))
for y in year:
print('Processing year {}'.format(y))
' tta analysis '
tta = tta_analysis(y)
tta.start_df(wdir_surf = wdsurf,
wdir_wprof = wdwpro,
rain_bby = rainbb,
rain_czd = raincz,
nhours = nhours)
' retrieve dates '
include_dates = tta.include_dates
tta_dates = tta.tta_dates
notta_dates = tta.notta_dates
' read wprof '
wprof_df = parse_data.windprof(y)
for n,t in enumerate(target):
wprof = wprof_df.dframe[t]
' wprof partition '
wprof = wprof.loc[include_dates] # all included
wprof_tta = wprof.loc[tta_dates] # only tta
wprof_notta = wprof.loc[notta_dates]# only notta
s1 = np.squeeze(pandas2stack(wprof))
if wprof_tta.size > 0:
s2 = np.squeeze(pandas2stack(wprof_tta))
ttaok = True
else:
ttaok =False
s3 = np.squeeze(pandas2stack(wprof_notta))
if t == 'wdir':
wdr = np.hstack((wdr,s1))
if ttaok is True:
if s2.ndim == 1:
s2=np.expand_dims(s2,axis=1)
wdr_tta = np.hstack((wdr_tta,s2))
wdr_notta = np.hstack((wdr_notta, s3))
else:
wsp = np.hstack((wsp,s1))
if ttaok is True:
if s2.ndim == 1:
s2=np.expand_dims(s2,axis=1)
wsp_tta = np.hstack((wsp_tta,s2))
wsp_notta = np.hstack((wsp_notta, s3))
arrays['wdir']=[wdr,wdr_tta,wdr_notta]
arrays['wspd']=[wsp,wsp_tta,wsp_notta]
uw = -wsp*np.sin(np.radians(wdr))
uw_tta = -wsp_tta*np.sin(np.radians(wdr_tta))
uw_notta = -wsp_notta*np.sin(np.radians(wdr_notta))
vw = -wsp*np.cos(np.radians(wdr))
vw_tta = -wsp_tta*np.cos(np.radians(wdr_tta))
vw_notta = -wsp_notta*np.cos(np.radians(wdr_notta))
arrays['u']=[uw,uw_tta,uw_notta]
arrays['v']=[vw,vw_tta,vw_notta]
''' total hours, first rows are empty '''
_,wp_hours = wsp.shape
_,tta_hours = wsp_tta.shape
_,notta_hours = wsp_notta.shape
wp_hours -= 1
tta_hours-= 1
notta_hours -= 1
' initialize arrays '
hist_array_spd = np.empty((40,len(binss['wspd'])-1,3))
hist_array_dir = np.empty((40,len(binss['wdir'])-1,3))
cfad_array_spd = np.empty((40,len(binss['wspd'])-1,3))
cfad_array_dir = np.empty((40,len(binss['wdir'])-1,3))
average_spd = np.empty((40,3))
average_dir = np.empty((40,3))
median_spd = np.empty((40,3))
median_dir = np.empty((40,3))
' loop for variable (wdir,wspd) '
for k,v in arrays.iteritems():
hist_array = np.empty((40,len(binss[k])-1,3))
cfad_array = np.empty((40,len(binss[k])-1,3))
average = np.empty((40,3))
median = np.empty((40,3))
' extract value'
wp = v[0]
wp_tta = v[1]
wp_notta = v[2]
' makes CFAD '
for hgt in range(wp.shape[0]):
row1 = wp[hgt,:]
row2 = wp_tta[hgt,:]
row3 = wp_notta[hgt,:]
for n,r in enumerate([row1,row2,row3]):
' following CFAD Yuter et al (1995) '
freq,bins=np.histogram(r[~np.isnan(r)],
bins=binss[k])
hist_array[hgt,:,n] = freq
cfad_array[hgt,:,n] = 100.*(freq/float(freq.sum()))
bin_middle = (bins[1:]+bins[:-1])/2.
average[hgt,n] = np.sum(freq*bin_middle)/freq.sum()
median[hgt,n] = np.percentile(r[~np.isnan(r)],50)
if k == 'wspd':
hist_array_spd = hist_array
cfad_array_spd = cfad_array
average_spd = average
median_spd = median
elif k == 'wdir':
hist_array_dir = hist_array
cfad_array_dir = cfad_array
average_dir = average
median_dir = median
elif k == 'u':
hist_array_u = hist_array
cfad_array_u = cfad_array
average_u = average
median_u = median
elif k == 'v':
hist_array_v = hist_array
cfad_array_v = cfad_array
average_v = average
median_v = median
return [hist_array_spd,
hist_array_dir,
hist_array_u,
hist_array_v,
cfad_array_spd,
cfad_array_dir,
cfad_array_u,
cfad_array_v,
binss['wspd'],
binss['wdir'],
binss['u'],
binss['v'],
wprof_df.hgt,
wp_hours,
tta_hours,
notta_hours,
average_spd,
average_dir,
average_u,
average_v,
median_spd,
median_dir,
median_u,
median_v,
]
def process(year=[],wdsurf=None,
wdwpro=None,rainbb=None,
raincz=None, nhours=None):
binss={'wdir':np.arange(0,370,10),
'wspd':np.arange(0,36,1)}
target = ['wdir','wspd']
arrays = {}
for t in target:
first = True
for y in year:
print('Processing year {}'.format(y))
' tta analysis '
tta = tta_analysis(y)
tta.start_df(wdir_surf=wdsurf,
wdir_wprof=wdwpro,
rain_bby=rainbb,
rain_czd=raincz,
nhours=nhours)
' retrieve dates '
include_dates = tta.include_dates
tta_dates = tta.tta_dates
notta_dates = tta.notta_dates
' read wprof '
wprof_df = parse_data.windprof(y)
wprof = wprof_df.dframe[t]
' wprof partition '
wprof = wprof.loc[include_dates] # all included
wprof_tta = wprof.loc[tta_dates] # only tta
wprof_notta = wprof.loc[notta_dates]# only notta
s1 = np.squeeze(pandas2stack(wprof))
s2 = np.squeeze(pandas2stack(wprof_tta))
s3 = np.squeeze(pandas2stack(wprof_notta))
if first:
wp = s1
wp_tta = s2
wp_notta = s3
first = False
else:
wp = np.hstack((wp,s1))
wp_tta = np.hstack((wp_tta,s2))
wp_notta = np.hstack((wp_notta, s3))
_,wp_hours = wp.shape
_,tta_hours = wp_tta.shape
_,notta_hours = wp_notta.shape
arrays[t]=[wp,wp_tta,wp_notta]
' makes CFAD '
hist_array_spd = np.empty((40,len(binss['wspd'])-1,3))
hist_array_dir = np.empty((40,len(binss['wdir'])-1,3))
cfad_array_spd = np.empty((40,len(binss['wspd'])-1,3))
cfad_array_dir = np.empty((40,len(binss['wdir'])-1,3))
average_spd = np.empty((40,3))
average_dir = np.empty((40,3))
median_spd = np.empty((40,3))
median_dir = np.empty((40,3))
for k,v in arrays.iteritems():
hist_array = np.empty((40,len(binss[k])-1,3))
cfad_array = np.empty((40,len(binss[k])-1,3))
average = np.empty((40,3))
median = np.empty((40,3))
wp = v[0]
wp_tta = v[1]
wp_notta = v[2]
for hgt in range(wp.shape[0]):
row1 = wp[hgt,:]
row2 = wp_tta[hgt,:]
row3 = wp_notta[hgt,:]
for n,r in enumerate([row1,row2,row3]):
' following CFAD Yuter et al (1995) '
freq,bins=np.histogram(r[~np.isnan(r)],
bins=binss[k])
hist_array[hgt,:,n] = freq
cfad_array[hgt,:,n] = 100.*(freq/float(freq.sum()))
bin_middle = (bins[1:]+bins[:-1])/2.
average[hgt,n] = np.sum(freq*bin_middle)/freq.sum()
median[hgt,n] = np.percentile(r[~np.isnan(r)],50)
if k == 'wspd':
hist_array_spd = hist_array
cfad_array_spd = cfad_array
average_spd = average
median_spd = median
else:
hist_array_dir = hist_array
cfad_array_dir = cfad_array
average_dir = average
median_dir = median
return [hist_array_spd,
hist_array_dir,
cfad_array_spd,
cfad_array_dir,
binss['wspd'],
binss['wdir'],
wprof_df.hgt,
wp_hours,
tta_hours,
notta_hours,
average_spd,
average_dir,
median_spd,
median_dir]