-
Notifications
You must be signed in to change notification settings - Fork 0
/
gapflow.py
337 lines (264 loc) · 10.9 KB
/
gapflow.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
'''
Analysis of gap flows as appear in
Valenzuela & Kingmisll (2014)
Raul Valenzuela
raul.valenzuela@colorado.edu
January, 2016
'''
import Meteoframes as mf
import numpy as np
import matplotlib.pyplot as plt
# import matplotlib.patches as patches
import pandas as pd
#import os
import Thermodyn as tm
from datetime import datetime
from matplotlib.path import Path
def run(case, tta=None, plot_theory=False, grid=True, ax=None, homedir=None,
color_surf=(0, 0, 0.5, 0.5),color_wp=(0, 0.5, 0, 0.5),
add_date=False, wprof_hgt=None):
" process gapflow analysis"
" first index is mesowest file, second index is \
BBY (1 or more files) "
f = get_filenames(case,homedir)
" parse mesowest data "
meso = mf.parse_mesowest_excel(f[0])
t = get_times(case)
mpress = meso.loc[t[0]: t[1]]['PMSL'].values
mesoidx = meso.loc[t[0]: t[1]].index
" parse surface BBY data "
if len(f[1]) > 1:
' more than one day of obs '
surf = mf.parse_surface(f[1][0])
for ff in f[1][1:]:
surf = surf.append(mf.parse_surface(ff))
else:
' only one day '
surf = mf.parse_surface(f[1][0])
" resample to 1min so we can find \
mesowest index "
surf = surf.resample('1T').interpolate()
" adjust bias is mesowest in case 1 and 2 "
if case in [1, 2]:
bias = 9
else:
bias = 0
spress = surf.loc[mesoidx]['press'].values - bias
" BBY and mesowest pressure difference "
pressDiff = spress - mpress
" BBY surface winds "
swspd = surf.loc[mesoidx]['wspd'].values
swdir = surf.loc[mesoidx]['wdir'].values
ucomp = -swspd*np.sin(np.radians(swdir))
" gapflow dataframe "
d = {'ucomp': ucomp, 'wspd': swspd, 'wdir': swdir,
'pdiff': pressDiff, 'Bpress': spress, 'Kpress': mpress}
gapflow = pd.DataFrame(data=d, index=mesoidx)
" removes rows with NaN "
gapflow = gapflow[np.isfinite(gapflow['Kpress'])]
" add wind profiler data at target altitude "
out = get_windprof(case,
gapflow_time=gapflow.index,
top_hgt_km=wprof_hgt,
homedir=homedir)
wp_wspd, wp_wdir = out
wp_ucomp = -wp_wspd*np.sin(np.radians(wp_wdir))
gapflow['wp_ws'] = wp_wspd
gapflow['wp_wd'] = wp_wdir
gapflow['wp_ucomp'] = wp_ucomp
" Mass etal 95 equation "
# blh = get_BLH(case)
blh = 500 #[m]
massPa, massU = mass_eq(air_density=1.24, BLH=blh)
path = make_polygon(massPa, massU)
gapflow = check_polygon(gapflow, path)
# gapflow['gapflow'] = ((gapflow.poly is True) & (gapflow.wdir <= 120))
# sub = gapflow[(gapflow.poly is True) & (gapflow.wdir <= 120)]
" theoretical lines "
if plot_theory:
if ax is None:
fig, ax = plt.subplots(figsize=(8, 7))
ax.set_xlabel('Pressure difference, BBY-SCK [hPa]')
ax.set_ylabel('BBY zonal wind [m s-1]')
# ax.scatter(gapflow['pdiff'], gapflow['ucomp'],
# color=color_surf, label='surf')
# ax.scatter(gapflow['pdiff'], gapflow['wp_ucomp'],
# color=color_wp, label='wp')
# ax.scatter(sub['pdiff'], sub['ucomp'], color='r')
ax.plot(massPa/100, massU[0], marker=None)
ax.plot(massPa/100, massU[1], linestyle='--', color='r')
ax.plot(massPa/100, massU[2], linestyle='--', color='r')
if grid:
ax.grid(True)
ax.set_xlim([-12, 1])
ax.set_ylim([-20, 15])
ini = mesoidx[0]
end = mesoidx[-1]
date = ini.strftime('%b-%Y ')
beg = ini.strftime('%d')
end = end.strftime('%d')
# ax.text(0.03, 0.76, timetxt.format(str(case).zfill(2),
# date, beg, end),
if add_date is True:
if beg == end:
ax.text(0.03, 0.85,'{} {}'.format(beg,date),
fontsize=12,
transform=ax.transAxes)
else:
ax.text(0.03, 0.85,'{}-{} {}'.format(beg,end,date),
fontsize=12,
transform=ax.transAxes)
return gapflow
def get_windprof(case, gapflow_time=None,
top_hgt_km=None,
homedir=None):
" return layer average values; layer is defined \
by top_hgt_km"
import Windprof2 as wp
from scipy.interpolate import interp1d
from datetime import timedelta
out = wp.make_arrays(resolution='coarse',
surface=False, case=str(case), period=False,
homedir=homedir)
wspd, wdir, time, hgt = out
time = np.array(time)
' match surface obs timestamp'
time2 = time - timedelta(minutes=5)
' get corresponding target time index '
index_dict = dict((value, idx) for idx, value in enumerate(time2))
target_idx = [index_dict[x] for x in gapflow_time]
' wp time equivalent to gapflow time '
target_time = time2[target_idx] + timedelta(minutes=5)
wspd_target = []
wdir_target = []
for tt in target_time:
idx = np.where(time == tt)[0]
# ws = np.squeeze(wspd[:, idx])
# wd = np.squeeze(wdir[:, idx])
# ' interpolate at target altitude '
# fws = interp1d(hgt, ws)
# fwd = interp1d(hgt, wd)
# new_ws = fws(target_hgt_km)
# new_wd = fwd(target_hgt_km)
hidx = np.where(hgt <= top_hgt_km)[0]
ws = np.squeeze(wspd[hidx, idx])
wd = np.squeeze(wdir[hidx, idx])
u = -ws*np.sin(np.radians(wd))
v = -ws*np.cos(np.radians(wd))
ubar = np.nanmean(u)
vbar = np.nanmean(v)
new_ws = np.sqrt(ubar**2+vbar**2)
new_wd = 270-np.arctan2(vbar,ubar)*180/np.pi
if new_wd > 360:
new_wd -= 360
wspd_target.append(new_ws)
wdir_target.append(new_wd)
return np.array(wspd_target), np.array(wdir_target)
def check_polygon(df, path):
x = df['pdiff'].values
y = df['ucomp'].values
coords = zip(x, y)
poly = []
for c in coords:
poly.append(path.contains_point(c))
df['poly'] = pd.Series(poly, index=df.index)
return df
def make_polygon(X, Y):
x = np.concatenate((X, X[::-1]))/100.
y = np.concatenate((Y[2], Y[1][::-1]))
vertices = zip(x, y)
npoints = len(vertices)
codes = [Path.MOVETO]+[Path.LINETO]*(npoints-2)+[Path.CLOSEPOLY]
path = Path(vertices, codes)
# patch=patches.PathPatch(path,facecolor='orange')
return path
def get_BLH(case):
'''
retrieve a boundary layer height based on
a subjective analysis of vertical directional
shear in wind profiler
'''
case = str(case)
blh = {'1': 100,
'2': 100,
'3': 500,
'4': 500,
'5': 500,
'6': 500,
'7': 500,
'8': 100,
'9': 200,
'10': 150,
'11': 500,
'12': 200,
'13': 450,
'14': 500}
return blh[case]
def get_filenames(casenum,basedir):
# basedir = os.path.expanduser('~')
b = basedir+'/SURFACE'
surf_files = {1: [b+'/case01/KSCK.xls', [b+'/case01/bby98018.met', b+'/case01/bby98019.met']],
2: [b+'/case02/KSCK.xls', [b+'/case02/bby98026.met', b+'/case02/bby98027.met']],
3: [b+'/case03/KSCK.xls', [b+'/case03/bby01023.met', b+'/case03/bby01024.met']],
4: [b+'/case04/KSCK.xls', [b+'/case04/bby01025.met', b+'/case04/bby01026.met']],
5: [b+'/case05/KSCK.xls', [b+'/case05/bby01040.met', b+'/case05/bby01041.met']],
6: [b+'/case06/KSCK.xls', [b+'/case06/bby01042.met']],
7: [b+'/case07/KSCK.xls', [b+'/case07/bby01048.met']],
8: [b+'/case08/KSCK.xls', [b+'/case08/bby03012.met', b+'/case08/bby03013.met', b+'/case08/bby03014.met']],
9: [b+'/case09/KSCK.xls', [b+'/case09/bby03021.met', b+'/case09/bby03022.met', b+'/case09/bby03023.met']],
10: [b+'/case10/KSCK.xls', [b+'/case10/bby03046.met', b+'/case10/bby03047.met']],
11: [b+'/case11/KSCK.xls', [b+'/case11/bby04009.met']],
12: [b+'/case12/KSCK.xls', [b+'/case12/bby04033.met']],
13: [b+'/case13/KSCK.xls', [b+'/case13/bby04047.met', b+'/case13/bby04048.met', b+'/case13/bby04049.met']],
14: [b+'/case14/KSCK.xls', [b+'/case14/bby04056.met']]
}
return surf_files[casenum]
def get_times(casenum):
" storm times "
slice_times = {1: [datetime(1998, 1, 18, 0, 56), datetime(1998, 1, 18, 23, 56)],
2: [datetime(1998, 1, 26, 0, 56), datetime(1998, 1, 27, 3, 56)],
3: [datetime(2001, 1, 23, 0, 0), datetime(2001, 1, 25, 0, 0)],
4: [datetime(2001, 1, 25, 0, 0), datetime(2001, 1, 27, 0, 0)],
5: [datetime(2001, 2, 9, 0, 0), datetime(2001, 2, 11, 0, 0)],
6: [datetime(2001, 2, 11, 0, 0), datetime(2001, 2, 12, 0, 0)],
7: [datetime(2001, 2, 17, 0, 0), datetime(2001, 2, 18, 0, 0)],
8: [datetime(2003, 1, 12, 0, 0), datetime(2003, 1, 15, 0, 0)],
9: [datetime(2003, 1, 21, 0, 0), datetime(2003, 1, 24, 0, 0)],
10: [datetime(2003, 2, 15, 0, 0), datetime(2003, 2, 17, 0, 0)],
11: [datetime(2004, 1, 9, 0, 0), datetime(2004, 1, 10, 0, 0)],
12: [datetime(2004, 2, 2, 0, 0), datetime(2004, 2, 3, 0, 0)],
13: [datetime(2004, 2, 16, 0, 0), datetime(2004, 2, 19, 0, 0)],
# 13: [datetime(2004, 2, 16, 0, 0), datetime(2004, 2, 17, 6, 0)],
14: [datetime(2004, 2, 25, 0, 0), datetime(2004, 2, 26, 0, 0)]}
return slice_times[casenum]
def mass_eq(air_density=1.24, BLH=500):
' Gap Flow based on Mass et al (1995) MWR '
H = BLH # [m] height of well-mixed boundary layer
BLcoeff = 2.8 # boundary layer coef (Deardorff 1972)
npoints = 100
delPa = np.linspace(0, -1200, npoints) # [Pa]
delX = 100000 # [m] distance btwn gap entrance and BBY
rho = air_density # [kg m-3] average air density
PGF = -(1/rho)*(delPa/delX)
Cd = np.array([7.5e-3, 0.5*7.5e-3, 1.5*7.5e-3]) # drag coefficient
umass = []
for c in Cd:
K = BLcoeff*c/H
U2 = (PGF/K) * (1-np.exp(-2*K*delX))
umass.append(-np.real(np.sqrt(U2)))
return [delPa, umass]
def air_density():
Rd = 287. # [J K-1 kg-1]
# density values do not vary significantly
Tv = tm.virtual_temperature(C=stemp, mixing_ratio=smixr/1000.)+273.15
air_density1 = (spress*100.)/(Rd*Tv)
Tv = tm.virtual_temperature(C=mtemp, mixing_ratio=smixr/1000.)+273.15
air_density2 = (mpress*100.)/(Rd*Tv)
def get_tta_dates(years, param):
import tta_analysis3 as tta
out = tta.preprocess(years=years, layer=param['wdir_layer'])
# print out
result = tta.analyis(out, param)
precip = result['precip']
dates = precip[precip.tta]
return dates