-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskewT.py
More file actions
292 lines (258 loc) · 10.3 KB
/
skewT.py
File metadata and controls
292 lines (258 loc) · 10.3 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
# import matplotlib.pyplot as plt
import plotly.graph_objects as go
import numpy as np
import thermo as thrm
def skew_transform(T, p, skew_factor):
"""
Apply skew-T log-p transform.
Parameters:
----------
T : float or np.ndarray
Temperature in Kelvin.
p : float or np.ndarray
Pressure in Pa.
Returns:
-------
float or np.ndarray
Skewed temperature in degrees Celsius.
"""
Tc = T - thrm.T0
p_hpa = p / 100.0
return Tc + skew_factor * (np.log(1000.0) - np.log(p_hpa))
class SkewT_lines:
def __init__(self, skew_factor=35, ktot=64):
"""
Calculate static lines of a skew-T diagram.
Parameters:
----------
skew_factor : float or int
Skewness factor of diagram.
ktot : int
Number of vertical levels in curved lines.
Returns:
-------
None
"""
self.ktot = ktot
self.p1 = np.geomspace(105_000, 10_000, self.ktot) # Full pressure grid.
self.p2 = np.geomspace(105_000, 50_000, self.ktot) # Low pressure grid for theta and mixing ratio.
# Linear version for straight lines.
self.p1_lin = np.array([105_000, 10_000])
self.p2_lin = np.array([105_000, 50_000])
# Start points (temperature in Celcus at 1000 hPa) of static lines.
self.x0_isotherms = np.arange(-120, 40.01, 10)
self.x0_dry_adiabats = np.arange( -40, 50.01, 10)
#self.x0_moist_adiabats = np.arange( -0, 25.01, 5)
self.x0_moist_adiabats = np.array([0, 5, 10, 12.5, 15, 17.5, 20, 22.5, 25, 27.5])
self.x0_isohumes = np.arange( -30, 30.01, 10)
self.skew_factor = skew_factor
def calc(self):
"""
Calculate default (static) lines as f(T,p).
"""
# Isotherms: lines of constant absolute temperature.
x = self.x0_isotherms + thrm.T0
T = np.broadcast_to(x[np.newaxis, :], (self.p1_lin.size, len(x)))
self.isotherms = skew_transform(T, self.p1_lin[:, np.newaxis], self.skew_factor)
# Dry adiabats: lines of constant potential temperature.
x = self.x0_dry_adiabats + thrm.T0
T = x[np.newaxis, :] * thrm.exner(self.p2[:, np.newaxis])
self.dry_adiabats = skew_transform(T, self.p2[:, np.newaxis], self.skew_factor)
# Moist adiabats: lines of constant saturated potential temperature.
x = self.x0_moist_adiabats + thrm.T0
T = thrm.calc_moist_adiabat(x, self.p1)
self.moist_adiabats = skew_transform(T, self.p1[:, np.newaxis], self.skew_factor)
# Isohumes: lines of constant specific humidity.
x = self.x0_isohumes + thrm.T0
q_0 = thrm.qsat(x, thrm.p0)
T = thrm.dewpoint(q_0[np.newaxis, :], self.p2_lin[:, np.newaxis])
self.isohumes = skew_transform(T, self.p2_lin[:, np.newaxis], self.skew_factor)
# class SkewT_mpl:
# def __init__(self, skewt_lines, mode='color'):
# """
# Plot skew-T diagram with Matplotlib.
# """
# self.stl = skewt_lines
#
# self.p_ticks = np.array([1000, 950, 900, 850, 800, 700, 600, 500, 400, 300, 200, 100]) * 100.0
# self.ylim = (1050e2, 100e2)
# self.xlim = (-40, 50)
#
# if mode == 'simple':
# self.cT = '0.8' # Isotherms
# self.cth = '0.8' # Dry adiabats
# self.cths = '0.8' # Moist adiabats
# self.cr = '0.8' # Mixing ratio
# self.lw = 0.5 # Line width
# self.ls = '--' # Line style
# elif mode == 'color':
# self.cT = '0.7'
# self.cth = 'tab:red'
# self.cths = '0.7'
# self.cr = 'tab:blue'
# self.lw = 0.5
# self.ls = '--'
# else:
# raise Exception('Invalid color mode.')
#
#
# def plot(self, figsize=(6,6)):
# """
# Create base plot with default static lines.
# """
# stl = self.stl
#
# plt.figure(figsize=figsize, layout='constrained')
#
# # Default lines diagram.
# plt.plot(stl.isotherms, stl.p1_lin, color=self.cT, linewidth=self.lw, linestyle=self.ls)
# plt.plot(stl.isohumes, stl.p2_lin, color=self.cr, linewidth=self.lw, linestyle=self.ls)
# plt.plot(stl.dry_adiabats, stl.p2, color=self.cth, linewidth=self.lw, linestyle=self.ls)
# plt.plot(stl.moist_adiabats, stl.p1, color=self.cths, linewidth=self.lw, linestyle=self.ls)
#
# # Finish diagram.
# plt.yscale('log')
# plt.gca().invert_yaxis()
# plt.yticks(self.p_ticks, (self.p_ticks / 100).astype(int))
# plt.gca().yaxis.set_minor_formatter(plt.NullFormatter())
# plt.grid(axis='y', linewidth=0.5, color='0.5')
# plt.xlim(self.xlim)
# plt.ylim(self.ylim)
# plt.ylabel('Pressure (hPa)')
# plt.xlabel('Temperature (°C)')
#
#
# def plot_sounding(self, T, p, ax=None, *args, **kwargs):
# """
# Plot observed or modelled sounding.
#
# Parameters:
# ----------
# T : ndarray
# Temperature (K)
# p : ndarray
# Pressure (Pa)
# ax : matplotlib axes, optional
# Axes to plot on. Defaults to current axes.
# *args, **kwargs :
# Passed to ax.plot().
#
# Returns:
# -------
# None
# """
#
# if ax is None:
# ax = plt.gca()
#
# ax.plot(skew_transform(T, p, self.stl.skew_factor), p, *args, **kwargs)
# ax.axhline(p[0], color='k', linewidth=0.5)
#
#
# def plot_non_entraining_parcel(self, parcel, ax=None, *args, **kwargs):
# """
# Plot non-entraining parcel
#
# Parameters:
# ----------
# parcel : dict
# Dict with parcel properties.
# ax : matplotlib axes, optional
# Axes to plot on. Defaults to current axes.
# *args, **kwargs :
# Passed to ax.plot().
#
# Returns:
# -------
# None
# """
#
# if ax is None:
# ax = plt.gca()
#
# ax.plot(skew_transform(parcel['T_isohume'], parcel['p_isohume'], self.stl.skew_factor), parcel['p_isohume'], *args, **kwargs)
# ax.plot(skew_transform(parcel['T_dry'], parcel['p_dry'], self.stl.skew_factor), parcel['p_dry'], *args, **kwargs)
# ax.plot(skew_transform(parcel['T_moist'], parcel['p_moist'], self.stl.skew_factor), parcel['p_moist'], *args, **kwargs)
class SkewT_plotly:
def __init__(self, skewt_lines):
self.stl = skewt_lines
self.p_ticks = np.array([1000, 950, 900, 850, 800, 700, 600, 500, 400, 300, 200, 100]) * 100.0
self.ylim = (1050e2, 100e2)
self.xlim = (-40, 50)
def plot(self, title=''):
stl = self.stl
fig = go.Figure()
line_kw = dict(showlegend=False, hoverinfo='skip')
# Isotherms.
for i in range(stl.isotherms.shape[1]):
fig.add_trace(go.Scatter(
x=stl.isotherms[:, i], y=stl.p1_lin,
mode='lines', line=dict(color='rgba(179,179,179,0.7)', width=1.2, dash='4px,2px'), **line_kw))
# Isohumes.
for i in range(stl.isohumes.shape[1]):
fig.add_trace(go.Scatter(
x=stl.isohumes[:, i], y=stl.p2_lin,
mode='lines', line=dict(color='rgba(31,119,180,0.7)', width=1.2, dash='4px,2px'), **line_kw))
# Dry adiabats.
for i in range(stl.dry_adiabats.shape[1]):
fig.add_trace(go.Scatter(
x=stl.dry_adiabats[:, i], y=stl.p2,
mode='lines', line=dict(color='rgba(214,39,40,0.7)', width=1.2, dash='4px,2px'), **line_kw))
# Moist adiabats.
for i in range(stl.moist_adiabats.shape[1]):
fig.add_trace(go.Scatter(
x=stl.moist_adiabats[:, i], y=stl.p1,
mode='lines', line=dict(color='rgba(179,179,179,0.7)', width=1.2, dash='4px,2px'), **line_kw))
fig.update_yaxes(
type='log', range=[np.log10(self.ylim[0]), np.log10(self.ylim[1])],
tickvals=self.p_ticks,
ticktext=(self.p_ticks / 100).astype(int),
title='Pressure (hPa)',
showgrid=True, gridwidth=0.5, gridcolor='rgba(128,128,128,0.3)',
minor=dict(showgrid=False),
)
fig.update_xaxes(
range=self.xlim,
title='Temperature (°C)',
)
fig.update_layout(
title=dict(text=f'<span style="font-weight:normal">{title}</span>', x=0.5, xanchor='center'),
height=900, width=550,
margin=dict(l=60, r=20, t=60, b=60),
plot_bgcolor='white',
)
self.fig = fig
return fig
def plot_sounding(self, T, p, name='', color='red', width=2, dash=None):
x = skew_transform(T, p, self.stl.skew_factor)
line_kw = dict(color=color, width=width)
if dash is not None:
line_kw['dash'] = dash
self.fig.add_trace(go.Scatter(
x=x, y=p, mode='lines', name=name,
line=line_kw,
))
return self.fig
def plot_non_entraining_parcel(self, parcel, name='Parcel', color='black', width=2, dash='4px,2px'):
sf = self.stl.skew_factor
for i, (key_T, key_p) in enumerate([('T_isohume', 'p_isohume'), ('T_dry', 'p_dry'), ('T_moist', 'p_moist')]):
self.fig.add_trace(go.Scatter(
x=skew_transform(parcel[key_T], parcel[key_p], sf), y=parcel[key_p],
mode='lines', name=name, line=dict(color=color, width=width, dash=dash),
showlegend=i == 0,
))
return self.fig
def plot_entraining_parcel(self, plume, name='Plume', color='black', width=2, dash='4px,2px'):
sf = self.stl.skew_factor
line_kw = dict(color=color, width=width, dash=dash)
self.fig.add_trace(go.Scatter(
x=skew_transform(plume['T'], plume['p'], sf), y=plume['p'],
mode='lines', name=name, line=line_kw,
))
below_lcl = plume['type'] == 0
self.fig.add_trace(go.Scatter(
x=skew_transform(plume['Td'][below_lcl], plume['p'][below_lcl], sf), y=plume['p'][below_lcl],
mode='lines', name=name, line=line_kw,
showlegend=False,
))
return self.fig