-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskew_t_plot.py
274 lines (243 loc) · 21.1 KB
/
skew_t_plot.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
from metpy.units import units
from metpy import calc as mpcalc
import numpy as np
import holoviews as hv
from bokeh.models import Range1d, WheelZoomTool, ColumnDataSource, HArea, HoverTool
from bokeh.models.annotations import BoxAnnotation, Label
from functools import reduce
hv.extension('bokeh')
plot_width = 800
plot_height = 800
class skew_t_plot:
def __init__(self, profileData):
self.profileData = profileData
self.skew_t = self.plotSkewT()
def hook_limit_pan(self, plot, element):
profileData = self.profileData
plot.state.select_one(WheelZoomTool).maintain_focus = False
xlimmin = 10*(np.nanmin(profileData.TEMP.data.to(units.degC).magnitude) // 10)+10
xlimmax = (10*np.nanmax(profileData.TEMP.data.to(units.degC).magnitude) // 10)+10
if xlimmax - xlimmin < 110:
xlimmin = xlimmax - 110
ylimbot = np.max([np.nanmax(profileData.LEVEL.data.magnitude+5), 1000])
ylimtop = np.min([np.nanmin(profileData.LEVEL.data.magnitude-5), 100])
plot.state.x_range = Range1d(xlimmin, xlimmax, bounds=(xlimmin, xlimmax))
plot.state.y_range = Range1d(ylimbot, ylimtop, bounds='auto')
def hook_height_label(self, plot, element):
kmToInterp = np.arange(0, 15001, 1000)
interpPressures = np.interp(kmToInterp, self.profileData.AGL, self.profileData.LEVEL)
colorsAndKilometers = {
(0, 1): "fuchsia",
(1, 3): "firebrick",
(3, 6): "limegreen",
(6, 9): "goldenrod",
(9, 12): "darkturquoise",
(12, 15): "darkturquoise"
}
for indices, color in reversed(colorsAndKilometers.items()):
bot, top = indices
plot.state.add_layout(BoxAnnotation(left=0, right=plot_width/25, bottom=interpPressures[bot], top=interpPressures[top], fill_alpha=0.5, fill_color=color, line_color=color, left_units='screen', right_units='screen'))
for i in [0, 1, 2, 3, 4, 5, 6, 9, 12, 15]:
if np.nanmax(self.profileData.AGL.data) < i * units.km:
plot.state.add_layout(Label(x=1, y=np.nanmin(interpPressures), text=f"{np.nanmax(self.profileData.AGL.data).to(units.km).magnitude:.3f} km: {np.nanmin(self.profileData.LEVEL.data).to(units.hPa).magnitude:.1f} hPa",x_units='screen', text_baseline='middle', text_font_size='10px'))
break
if i == 0:
plot.state.add_layout(Label(x=1, y=interpPressures[i], text=f"SFC: {interpPressures[i]:.1f} hPa", x_units='screen', text_baseline='bottom', text_font_size='10px'))
else:
plot.state.add_layout(Label(x=1, y=interpPressures[i], text=f"{str(int(i))} km: {interpPressures[i]:.1f} hPa", x_units='screen', text_baseline='bottom', text_font_size='10px'))
def hook_inflow_layer(self, plot, element):
EIL_box = BoxAnnotation(left=plot_width/25, right=3*plot_width/50, bottom=self.profileData.inflowBottom.to(units.hPa).magnitude, top=self.profileData.inflowTop.to(units.hPa).magnitude, fill_alpha=0.2, fill_color='teal', line_color='teal', left_units='screen', right_units='screen')
plot.state.add_layout(EIL_box)
if self.profileData.inflowBottom < self.profileData.LEVEL[0]:
plot.state.add_layout(Label(x=5*plot_width/50, y=self.profileData.inflowBottom.to(units.hPa).magnitude, text=f"EIL: {self.profileData.inflowBottom.to(units.hPa).magnitude:.1f} hPa", x_units='screen', text_baseline='middle', text_color='teal', text_font_size='10px'))
plot.state.add_layout(Label(x=5*plot_width/50, y=self.profileData.inflowTop.to(units.hPa).magnitude, text=f"EIL: {self.profileData.inflowTop.to(units.hPa).magnitude:.1f} hPa", x_units='screen', text_baseline='middle', text_color='teal', text_font_size='10px'))
eilData = self.profileData.where((self.profileData.LEVEL <= self.profileData.inflowBottom) & (self.profileData.LEVEL >= self.profileData.inflowTop), drop=True)
eilRH = int(eilData.RH.mean() * 100)
new_hover = [('Effective Inflow Layer', ''), ('Pressure', f"{self.profileData.inflowBottom.to(units.hPa).magnitude:.1f} - {self.profileData.inflowTop.to(units.hPa).magnitude:.1f} hPa"), ('Height (AGL)', f"{self.profileData.inflowBottom_agl.to(units.meter).magnitude:.1f} - {self.profileData.inflowTop_agl.to(units.meter).magnitude:.1f} m"), ('Relative Humidity', f"{eilRH}%")]
for hover in plot.state.select(HoverTool):
if len(hover.tooltips) == 3 and hover.tooltips[2][0] == 'eil_override':
hover.tooltips = new_hover
break
# skew.ax.text(0.16, profileData.inflowTop.to(units.hPa).magnitude, f"Effective Inflow Layer\n{profileData.inflowBottom.to(units.hPa).magnitude:.1f} - {profileData.inflowTop.to(units.hPa).magnitude:.1f} hPa\nAGL: {int(profileData.inflowBottom_agl.to(units.meter).magnitude)} - {int(profileData.inflowTop_agl.to(units.meter).magnitude)} m\nRH: {eilRH}%", color="teal", ha="left", va="top", path_effects=[withStroke(linewidth=3, foreground="white")], fontsize=8, clip_on=True, zorder=7, transform=skew.ax.get_yaxis_transform(), alpha=0.7)
def hook_hover_fix(self, plot, element):
hovers = plot.state.select(HoverTool)
for hover in hovers:
tooltips = []
for tooltip in hover.tooltips:
if tooltip[0] == 'Pressure':
if 'hPa' not in tooltip[1]:
tooltips.append((tooltip[0], f'{tooltip[1]} hPa'))
elif 'Potential' in tooltip[0]:
if 'K' not in tooltip[1]:
tooltips.append((tooltip[0], tooltip[1]+'{0.1f} K'))
elif 'Temperature' in tooltip[0] or tooltip[0] == 'Dew Point':
if '°C' not in tooltip[1]:
tooltips.append((tooltip[0], tooltip[1]+'{0.1f} °C'))
elif 'Humidity' in tooltip[0]:
if '%' not in tooltip[1]:
tooltips.append((tooltip[0], tooltip[1]+'{0.1f} %'))
elif 'override' in tooltip[0]:
tooltips.append((tooltip[0], tooltip[1]))
if len(tooltips) > 0:
hover.tooltips = tooltips
def hook_dgz_area(self, plot, element):
dgzData = self.profileData.where((self.profileData.LEVEL <= element['DGZ_bottom'][0]*units.hPa), drop=True).where(
(self.profileData.LEVEL >= element['Pressure'][0]*units.hPa), drop=True)
x1 = (dgzData.DWPT.data.to(units.degC)+dgzData.skewt_offset.data).magnitude
x2 = (dgzData.TEMP.data.to(units.degC)+dgzData.skewt_offset.data).magnitude
y = dgzData.LEVEL.data.to(units.hPa).magnitude
src = ColumnDataSource(data=dict(x1=x1, x2=x2, y=y))
dgz_shade = HArea(x1='x1', x2='x2', y='y', fill_color='blue', fill_alpha=0.2)
renderer = plot.state.add_glyph(src, dgz_shade)
# TODO: this won't work with multiple DGZs
plot.state.add_tools(
HoverTool(
renderers=[renderer],
tooltips=[
('Dendritic Growth Zone', ''),
('Pressure', f"{dgzData.LEVEL[0].data.to(units.hPa).magnitude:.1f} - {dgzData.LEVEL[-1].data.to(units.hPa).magnitude:.1f} hPa"),
('Height (AGL)', f"{dgzData.AGL[0].data.to(units.meter).magnitude:.1f} - {dgzData.AGL[-1].data.to(units.meter).magnitude:.1f} m"),
('Relative Humidity', f"{int(dgzData.RH.data.mean().magnitude*100)}%")
],
toggleable=False
)
)
def hook_lcl_label(self, plot, element):
plot.state.add_layout(Label(x=5*plot_width/50, y=self.profileData.attrs[self.parcelType+'LCL'].to(units.hPa).magnitude, text=f"LCL: {self.profileData.attrs[self.parcelType+'LCL'].to(units.hPa).magnitude:.1f} hPa", x_units='screen', text_baseline='middle', text_color='mediumseagreen', text_font_size='10px'))
new_hover = [('Lifted Condensation Level', ''), ('Pressure', f"{self.profileData.attrs[self.parcelType+'LCL'].to(units.hPa).magnitude:.1f} hPa"), ('Height (AGL)', f"{self.profileData.attrs[self.parcelType+'LCL_agl'].to(units.meters).magnitude:.1f} m")]
for hover in plot.state.select(HoverTool):
if len(hover.tooltips) == 3 and hover.tooltips[2][0] == 'lcl_override':
hover.tooltips = new_hover
break
def hook_lfc_label(self, plot, element):
plot.state.add_layout(Label(x=5*plot_width/50, y=self.profileData.attrs[self.parcelType+'LFC'].to(units.hPa).magnitude, text=f"LFC: {self.profileData.attrs[self.parcelType+'LFC'].to(units.hPa).magnitude:.1f} hPa", x_units='screen', text_baseline='middle', text_color='darkgoldenrod', text_font_size='10px'))
new_hover = [('Level of Free Convection', ''), ('Pressure', f"{self.profileData.attrs[self.parcelType+'LFC'].to(units.hPa).magnitude:.1f} hPa"), ('Height (AGL)', f"{self.profileData.attrs[self.parcelType+'LFC_agl'].to(units.meters).magnitude:.1f} m")]
for hover in plot.state.select(HoverTool):
if len(hover.tooltips) == 3 and hover.tooltips[2][0] == 'lfc_override':
hover.tooltips = new_hover
break
def hook_el_label(self, plot, element):
plot.state.add_layout(Label(x=5*plot_width/50, y=self.profileData.attrs[self.parcelType+'EL'].to(units.hPa).magnitude, text=f"EL: {self.profileData.attrs[self.parcelType+'EL'].to(units.hPa).magnitude:.1f} hPa", x_units='screen', text_baseline='middle', text_color='mediumpurple', text_font_size='10px'))
new_hover = [('Equilibrium Level', ''), ('Pressure', f"{self.profileData.attrs[self.parcelType+'EL'].to(units.hPa).magnitude:.1f} hPa"), ('Height (AGL)', f"{self.profileData.attrs[self.parcelType+'EL_agl'].to(units.meters).magnitude:.1f} m")]
for hover in plot.state.select(HoverTool):
if len(hover.tooltips) == 3 and hover.tooltips[2][0] == 'el_override':
hover.tooltips = new_hover
break
def hook_remove_bokeh(self, plot, element):
plot.state.toolbar.logo = None
for tool in plot.state.toolbar.tools:
if isinstance(tool, HoverTool):
tool.toggleable = False
def plotSkewT(self, parcelType="sb"):
self.parcelType = parcelType
sounding_opts = {
'ylim': (1000, 100),
'xlabel' : 'Temperature (°C)',
'ylabel' : 'Pressure (hPa)',
'logy':True,
'width':plot_width,
'height':plot_height,
'hooks':[self.hook_limit_pan, self.hook_remove_bokeh, self.hook_hover_fix]
}
# Plot data
skew_t_offset = self.profileData.skewt_offset.data
temp_curve = hv.Curve((self.profileData.TEMP.data.to(units.degC)+skew_t_offset, self.profileData.LEVEL.data, self.profileData.TEMP.data.to(units.degC)),
kdims=['Skewed_T'], vdims=['Pressure', 'Temperature']).opts(
color='red', tools=['hover'], **sounding_opts)
temp_curve = temp_curve.opts(hooks=[self.hook_height_label])
# https://docs.bokeh.org/en/latest/docs/reference/models/tools.html#bokeh.models.HoverTool
dew_curve = hv.Curve((self.profileData.DWPT.data.to(units.degC)+skew_t_offset, self.profileData.LEVEL.data, self.profileData.DWPT.data.to(units.degC), self.profileData.RH.data.magnitude*100),
kdims=['Skewed_T'], vdims=['Pressure', 'Dew Point', 'Relative Humidity']).opts(
color='lime', tools=['hover'], **sounding_opts)
virt_curve = hv.Curve((self.profileData.virtT.data.to(units.degC)+skew_t_offset, self.profileData.LEVEL.data, self.profileData.virtT.data.to(units.degC)),
kdims=['Skewed_T'], vdims=['Pressure', 'Virtual Temperature']).opts(
color='red', line_dash='dotted', tools=['hover'], **sounding_opts)
wetbulb_curve = hv.Curve((self.profileData.wetbulb.data.to(units.degC)+skew_t_offset, self.profileData.LEVEL.data, self.profileData.wetbulb.data.to(units.degC)),
kdims=['Skewed_T'], vdims=['Pressure', 'Wet Bulb Temperature']).opts(
color='cyan', line_width=0.5, tools=['hover'], **sounding_opts)
theta_curve = hv.Curve((self.profileData.potential_temperature.data.to(units.degC)+skew_t_offset, self.profileData.LEVEL.data,
self.profileData.potential_temperature.data.to(units.K)), kdims=['Skewed_T'], vdims=['Pressure', 'Potential Temperature']).opts(
color='mediumslateblue', line_width=0.5, tools=['hover'], **sounding_opts)
theta_e_curve = hv.Curve((self.profileData.equivalent_potential_temperature.data.to(units.degC)+skew_t_offset, self.profileData.LEVEL.data,
self.profileData.equivalent_potential_temperature.data.to(units.K)), kdims=['Skewed_T'], vdims=['Pressure', 'Equivalent Potential Temperature']).opts(
color='indigo', line_width=0.5, tools=['hover'], **sounding_opts)
isotherm_min = 10*(np.nanmin(self.profileData.DWPT.data.to(units.degC).magnitude - np.nanmax(skew_t_offset.magnitude)) // 10)+10
isotherm_max = (10*np.nanmax(self.profileData.TEMP.data.to(units.degC).magnitude) // 10)+30
isotherms_to_draw = np.arange(isotherm_min, isotherm_max+1, 10)
isotherms = []
dry_adiabats = []
moist_adiabats = []
for isotherm in isotherms_to_draw:
isotherm_line = hv.Curve((np.full_like(self.profileData.LEVEL.data, isotherm)*units.degC+skew_t_offset, self.profileData.LEVEL.data), kdims=['Skewed_T'], vdims=['Pressure']).opts(
color='gray', line_width=0.5, **sounding_opts)
isotherms.append(isotherm_line)
this_dry_adiabat = mpcalc.dry_lapse(self.profileData.LEVEL, isotherm*units.degC).to(units.degC)
dry_daiabat_line = hv.Curve((this_dry_adiabat+skew_t_offset, self.profileData.LEVEL.data), kdims=['Skewed_T'], vdims=['Pressure']).opts(
color='red', line_width=0.1, **sounding_opts)
dry_adiabats.append(dry_daiabat_line)
this_moist_adiabat = mpcalc.moist_lapse(self.profileData.LEVEL, isotherm*units.degC).to(units.degC)
moist_daiabat_line = hv.Curve((this_moist_adiabat+skew_t_offset, self.profileData.LEVEL.data), kdims=['Skewed_T'], vdims=['Pressure']).opts(
color='blue', line_width=0.1, **sounding_opts)
moist_adiabats.append(moist_daiabat_line)
isotherms = reduce(lambda x, y: x*y, isotherms)
dry_adiabats = reduce(lambda x, y: x*y, dry_adiabats)
moist_adiabats = reduce(lambda x, y: x*y, moist_adiabats)
dgz_isotherms = [hv.Curve((np.full_like(self.profileData.LEVEL.data, isotherm)*units.degC+skew_t_offset, self.profileData.LEVEL.data), kdims=['Skewed_T'], vdims=['Pressure']).opts(
color='blue', line_dash='dashed', line_width=0.5, **sounding_opts) for isotherm in [-12, -17]]
dgz_isotherms = reduce(lambda x, y: x*y, dgz_isotherms)
sfc_dew_label = hv.Text((self.profileData.DWPT.data[0].to(units.degC)+skew_t_offset[0]).magnitude, self.profileData.LEVEL.data[0].magnitude,
f"{int((self.profileData.DWPT.data[0]).to(units.degF).magnitude)}°F").opts(color='lime', text_align='right', text_baseline='bottom', text_font_size='12px')
sfc_wetbulb_label = hv.Text((self.profileData.wetbulb.data[0].to(units.degC)+skew_t_offset[0]).magnitude, self.profileData.LEVEL.data[0].magnitude,
f"{int((self.profileData.wetbulb.data[0]).to(units.degF).magnitude)}°F").opts(color='cyan', text_align='center', text_baseline='bottom', text_font_size='12px')
sfc_temp_label = hv.Text((self.profileData.TEMP.data[0].to(units.degC)+skew_t_offset[0]).magnitude, self.profileData.LEVEL.data[0].magnitude,
f"{int((self.profileData.TEMP.data[0]).to(units.degF).magnitude)}°F").opts(color='red', text_align='left', text_baseline='bottom', text_font_size='12px')
# TODO: add selector for parcel type
sb_parcel_curve = hv.Curve((self.profileData.sbParcelPath.data.to(units.degC)+skew_t_offset, self.profileData.LEVEL.data,
self.profileData.sbParcelPath.data.to(units.degC)), kdims=['Skewed_T'], vdims=['Pressure', 'Parcel Temperature']).opts(
color='dimgray', line_dash='dashdot', tools=['hover'], **sounding_opts)
dcape_curve = hv.Curve((self.profileData.dcape_profile.data.to(units.degC)+skew_t_offset, self.profileData.LEVEL.data,
self.profileData.dcape_profile.data.to(units.degC)), kdims=['Skewed_T'], vdims=['Pressure', 'Downdraft Parcel Temperature']).opts(
color='rebeccapurple', line_dash='dashdot', tools=['hover'], **sounding_opts)
skewt = temp_curve * dew_curve * virt_curve * wetbulb_curve * sb_parcel_curve * isotherms * dry_adiabats * moist_adiabats * dgz_isotherms * sfc_wetbulb_label * sfc_dew_label * sfc_temp_label * theta_curve * theta_e_curve * dcape_curve
if hasattr(self.profileData, parcelType+'LCL') and not np.isnan(self.profileData.attrs[parcelType+'LCL']):
LCL_line = hv.Curve((np.linspace(isotherm_min, isotherm_max, 10, endpoint=True), np.full((10), self.profileData.attrs[parcelType+'LCL'].to(units.hPa).magnitude), np.zeros((10))),
kdims=['Skewed_T'], vdims=['Pressure', 'lcl_override']).opts(color='mediumseagreen', alpha=0.2, tools=['hover'], hooks=[self.hook_lcl_label])
skewt = LCL_line * skewt
if hasattr(self.profileData, parcelType+'LFC') and not np.isnan(self.profileData.attrs[parcelType+'LFC']):
LFC_line = hv.Curve((np.linspace(isotherm_min, isotherm_max, 10, endpoint=True), np.full((10), self.profileData.attrs[parcelType+'LFC'].to(units.hPa).magnitude), np.zeros((10))),
kdims=['Skewed_T'], vdims=['Pressure', 'lfc_override']).opts(color='darkgoldenrod', alpha=0.2, tools=['hover'], hooks=[self.hook_lfc_label])
skewt = LFC_line * skewt
if hasattr(self.profileData, parcelType+'EL') and not np.isnan(self.profileData.attrs[parcelType+'EL']):
EL_line = hv.Curve((np.linspace(isotherm_min, isotherm_max, 10, endpoint=True), np.full((10), self.profileData.attrs[parcelType+'EL'].to(units.hPa).magnitude), np.zeros((10))),
kdims=['Skewed_T'], vdims=['Pressure', 'el_override']).opts(color='mediumpurple', alpha=0.2, tools=['hover'], hooks=[self.hook_el_label])
skewt = EL_line * skewt
dgzsData = (self.profileData.TEMP.data <= -12*units.degC) & (self.profileData.TEMP.data >= -17*units.degC)
dgzsData = dgzsData.nonzero()[0]
if len(dgzsData) > 0:
dgz_bounds = []
listOfDGZs = []
dgzBottom = self.profileData.LEVEL.data[dgzsData[0]]
for i in range(1, len(dgzsData)):
if dgzsData[i-1] - dgzsData[i] >= 2:
dgzTop = self.profileData.LEVEL.data[dgzsData[i-1]]
listOfDGZs.append((dgzBottom, dgzTop))
dgzBottom = self.profileData.LEVEL.data[dgzsData[i]]
dgzTop = self.profileData.LEVEL.data[dgzsData[-1]]
listOfDGZs.append((dgzBottom, dgzTop))
for dgz in listOfDGZs:
dgzData = self.profileData.where((self.profileData.LEVEL <= dgz[0]), drop=True).where((self.profileData.LEVEL >= dgz[1]), drop=True)
upper_line = hv.Curve(([isotherm_min, isotherm_max], [dgzData.LEVEL[-1].data.to(units.hPa).magnitude, dgzData.LEVEL[-1].data.to(units.hPa).magnitude], [dgzData.LEVEL[0].data.to(units.hPa).magnitude, dgzData.LEVEL[0].data.to(units.hPa).magnitude]),
kdims=['Skewed_T'], vdims=['Pressure', 'DGZ_bottom']).opts(color='blue', alpha=0.2, tools=['hover'], hooks=[self.hook_dgz_area])
lower_line = hv.Curve(([isotherm_min, isotherm_max], [dgzData.LEVEL[0].data.to(units.hPa).magnitude, dgzData.LEVEL[0].data.to(units.hPa).magnitude]),
kdims=['Skewed_T'], vdims=['Pressure']).opts(color='blue', alpha=0.2)
dgz_bounds.append(upper_line * lower_line)
skewt = skewt * reduce(lambda x, y: x*y, dgz_bounds)
if not np.isnan(self.profileData.inflowBottom):
if self.profileData.inflowBottom < self.profileData.LEVEL[0]:
EIL_bottom_line = hv.Curve((np.linspace(isotherm_min, isotherm_max, 10, endpoint=True), np.full((10), self.profileData.inflowBottom.to(units.hPa).magnitude), np.zeros((10))),
kdims=['Skewed_T'], vdims=['Pressure', 'eil_override']).opts(color='teal', alpha=0.2, tools=['hover'], hooks=[self.hook_inflow_layer])
skewt = EIL_bottom_line * skewt
EIL_top_line = hv.Curve((np.linspace(isotherm_min, isotherm_max, 10, endpoint=True), np.full((10), self.profileData.inflowTop.to(units.hPa).magnitude), np.zeros((10))),
kdims=['Skewed_T'], vdims=['Pressure', 'eil_override']).opts(color='teal', alpha=0.2, tools=['hover'], hooks=[self.hook_inflow_layer])
skewt = EIL_top_line * skewt
return skewt