-
Notifications
You must be signed in to change notification settings - Fork 7
/
plot.py
311 lines (239 loc) · 12.1 KB
/
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
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
from __future__ import print_function
import numpy as np
import matplotlib as mpl
mpl.use('agg')
import pylab
from matplotlib.patches import Circle
from matplotlib.lines import Line2D
import json
from datetime import datetime, timedelta
from params import vec2comp
_seg_hghts = [0, 3, 6, 9, 12, 18]
_seg_colors = ['r', '#00ff00', '#008800', '#993399', 'c']
def _total_seconds(td):
return td.days * 24 * 3600 + td.seconds + td.microseconds * 1e-6
def _fmt_timedelta(td):
seconds = int(_total_seconds(td))
periods = [
('dy', 60*60*24),
('hr', 60*60),
('min', 60),
('sec', 1)
]
strings=[]
for period_name,period_seconds in periods:
if seconds > period_seconds:
period_value, seconds = divmod(seconds,period_seconds)
strings.append("%s %s" % (period_value, period_name))
return " ".join(strings)
def _plot_param_table(parameters, web=False):
storm_dir, storm_spd = parameters['storm_motion']
trans = pylab.gca().transAxes
line_space = 0.028
start_x = 1.02
start_y = 1.0 - line_space
line_y = start_y
kwargs = {'color':'k', 'fontsize':10, 'clip_on':False, 'transform':trans}
pylab.text(start_x + 0.175, start_y, "Parameter Table", ha='center', fontweight='bold', **kwargs)
spacer = Line2D([start_x, start_x + 0.361], [line_y - line_space * 0.48] * 2, color='k', linestyle='-', transform=trans, clip_on=False)
pylab.gca().add_line(spacer)
line_y -= line_space * 1.5
pylab.text(start_x + 0.095, line_y - 0.0025, "BWD (kts)", fontweight='bold', **kwargs)
if not web:
pylab.text(start_x + 0.22, line_y - 0.0025, "SRH (m$^2$s$^{-2}$)", fontweight='bold', **kwargs)
else:
# Awful, awful hack for matplotlib without a LaTeX distribution
pylab.text(start_x + 0.22, line_y - 0.0025, "SRH (m s )", fontweight='bold', **kwargs)
pylab.text(start_x + 0.305, line_y + 0.009, "2 -2", fontweight='bold', color='k', fontsize=6, clip_on=False, transform=trans)
line_y -= line_space
pylab.text(start_x, line_y, "0-1 km", fontweight='bold', **kwargs)
val = "--" if np.isnan(parameters['shear_mag_1000m']) else "%d" % int(parameters['shear_mag_1000m'])
pylab.text(start_x + 0.095, line_y, val, **kwargs)
val = "--" if np.isnan(parameters['srh_1000m']) else "%d" % int(parameters['srh_1000m'])
pylab.text(start_x + 0.22, line_y, val, **kwargs)
line_y -= line_space
pylab.text(start_x, line_y, "0-3 km", fontweight='bold', **kwargs)
val = "--" if np.isnan(parameters['shear_mag_3000m']) else "%d" % int(parameters['shear_mag_3000m'])
pylab.text(start_x + 0.095, line_y, val, **kwargs)
val = "--" if np.isnan(parameters['srh_3000m']) else "%d" % int(parameters['srh_3000m'])
pylab.text(start_x + 0.22, line_y, val, **kwargs)
line_y -= line_space
pylab.text(start_x, line_y, "0-6 km", fontweight='bold', **kwargs)
val = "--" if np.isnan(parameters['shear_mag_6000m']) else "%d" % int(parameters['shear_mag_6000m'])
pylab.text(start_x + 0.095, line_y, val, **kwargs)
spacer = Line2D([start_x, start_x + 0.361], [line_y - line_space * 0.48] * 2, color='k', linestyle='-', transform=trans, clip_on=False)
pylab.gca().add_line(spacer)
line_y -= 1.5 * line_space
pylab.text(start_x, line_y, "Storm Motion:", fontweight='bold', **kwargs)
val = "--" if np.isnan(parameters['storm_motion']).any() else "%03d/%02d kts" % (storm_dir, storm_spd)
pylab.text(start_x + 0.26, line_y + 0.001, val, **kwargs)
line_y -= line_space
bl_dir, bl_spd = parameters['bunkers_left']
pylab.text(start_x, line_y, "Bunkers Left Mover:", fontweight='bold', **kwargs)
val = "--" if np.isnan(parameters['bunkers_left']).any() else "%03d/%02d kts" % (bl_dir, bl_spd)
pylab.text(start_x + 0.26, line_y + 0.001, val, **kwargs)
line_y -= line_space
br_dir, br_spd = parameters['bunkers_right']
if not web:
pylab.text(start_x, line_y, "Bunkers Right Mover:", fontweight='bold', **kwargs)
else:
pylab.text(start_x, line_y - 0.005, "Bunkers Right Mover:", fontweight='bold', **kwargs)
val = "--" if np.isnan(parameters['bunkers_right']).any() else "%03d/%02d kts" % (br_dir, br_spd)
if not web:
pylab.text(start_x + 0.26, line_y + 0.001, val, **kwargs)
else:
pylab.text(start_x + 0.26, line_y - 0.001, val, **kwargs)
line_y -= line_space
mn_dir, mn_spd = parameters['mean_wind']
pylab.text(start_x, line_y, "0-6 km Mean Wind:", fontweight='bold', **kwargs)
val = "--" if np.isnan(parameters['mean_wind']).any() else "%03d/%02d kts" % (mn_dir, mn_spd)
pylab.text(start_x + 0.26, line_y + 0.001, val, **kwargs)
spacer = Line2D([start_x, start_x + 0.361], [line_y - line_space * 0.48] * 2, color='k', linestyle='-', transform=trans, clip_on=False)
pylab.gca().add_line(spacer)
line_y -= 1.5 * line_space
if not web:
pylab.text(start_x, line_y, "Critical Angle:", fontweight='bold', **kwargs)
val = "--" if np.isnan(parameters['critical']) else "%d$^{\circ}$" % int(parameters['critical'])
pylab.text(start_x + 0.18, line_y - 0.0025, val, **kwargs)
else:
pylab.text(start_x, line_y - 0.0075, "Critical Angle:", fontweight='bold', **kwargs)
val = "--" if np.isnan(parameters['critical']) else "%d deg" % int(parameters['critical'])
pylab.text(start_x + 0.18, line_y - 0.0075, val, **kwargs)
def _plot_data(data, parameters):
storm_dir, storm_spd = parameters['storm_motion']
bl_dir, bl_spd = parameters['bunkers_left']
br_dir, br_spd = parameters['bunkers_right']
mn_dir, mn_spd = parameters['mean_wind']
u, v = vec2comp(data['wind_dir'], data['wind_spd'])
alt = data['altitude']
storm_u, storm_v = vec2comp(storm_dir, storm_spd)
bl_u, bl_v = vec2comp(bl_dir, bl_spd)
br_u, br_v = vec2comp(br_dir, br_spd)
mn_u, mn_v = vec2comp(mn_dir, mn_spd)
seg_idxs = np.searchsorted(alt, _seg_hghts)
try:
seg_u = np.interp(_seg_hghts, alt, u, left=np.nan, right=np.nan)
seg_v = np.interp(_seg_hghts, alt, v, left=np.nan, right=np.nan)
ca_u = np.interp(0.5, alt, u, left=np.nan, right=np.nan)
ca_v = np.interp(0.5, alt, v, left=np.nan, right=np.nan)
except ValueError:
seg_u = np.nan * np.array(_seg_hghts)
seg_v = np.nan * np.array(_seg_hghts)
ca_u = np.nan
ca_v = np.nan
mkr_z = np.arange(16)
try:
mkr_u = np.interp(mkr_z, alt, u, left=np.nan, right=np.nan)
mkr_v = np.interp(mkr_z, alt, v, left=np.nan, right=np.nan)
except ValueError:
mkr_u = np.nan * mkr_z
mkr_v = np.nan * mkr_z
for idx in range(len(_seg_hghts) - 1):
idx_start = seg_idxs[idx]
idx_end = seg_idxs[idx + 1]
if not np.isnan(seg_u[idx]):
pylab.plot([seg_u[idx], u[idx_start]], [seg_v[idx], v[idx_start]], '-', color=_seg_colors[idx], linewidth=1.5)
if idx_start < len(data['rms_error']) and data['rms_error'][idx_start] == 0.:
# The first segment is to the surface wind, draw it in a dashed line
pylab.plot(u[idx_start:(idx_start + 2)], v[idx_start:(idx_start + 2)], '--', color=_seg_colors[idx], linewidth=1.5)
pylab.plot(u[(idx_start + 1):idx_end], v[(idx_start + 1):idx_end], '-', color=_seg_colors[idx], linewidth=1.5)
else:
pylab.plot(u[idx_start:idx_end], v[idx_start:idx_end], '-', color=_seg_colors[idx], linewidth=1.5)
if not np.isnan(seg_u[idx + 1]):
pylab.plot([u[idx_end - 1], seg_u[idx + 1]], [v[idx_end - 1], seg_v[idx + 1]], '-', color=_seg_colors[idx], linewidth=1.5)
for upt, vpt, rms in list(zip(u, v, data['rms_error']))[idx_start:idx_end]:
rad = np.sqrt(2) * rms
circ = Circle((upt, vpt), rad, color=_seg_colors[idx], alpha=0.05)
pylab.gca().add_patch(circ)
pylab.plot(mkr_u, mkr_v, 'ko', ms=10)
for um, vm, zm in zip(mkr_u, mkr_v, mkr_z):
if not np.isnan(um):
pylab.text(um, vm - 0.1, str(zm), va='center', ha='center', color='white', size=6.5, fontweight='bold')
try:
pylab.plot([storm_u, u[0]], [storm_v, v[0]], 'c-', linewidth=0.75)
pylab.plot([u[0], ca_u], [v[0], ca_v], 'm-', linewidth=0.75)
except IndexError:
pass
if not (np.isnan(bl_u) or np.isnan(bl_v)):
pylab.plot(bl_u, bl_v, 'ko', markersize=5, mfc='none')
pylab.text(bl_u + 0.5, bl_v - 0.5, "LM", ha='left', va='top', color='k', fontsize=10)
if not (np.isnan(br_u) or np.isnan(br_v)):
pylab.plot(br_u, br_v, 'ko', markersize=5, mfc='none')
pylab.text(br_u + 0.5, br_v - 0.5, "RM", ha='left', va='top', color='k', fontsize=10)
if not (np.isnan(mn_u) or np.isnan(mn_v)):
pylab.plot(mn_u, mn_v, 's', color='#a04000', markersize=5, mfc='none')
pylab.text(mn_u + 0.6, mn_v - 0.6, "MEAN", ha='left', va='top', color='#a04000', fontsize=10)
smv_is_brm = (storm_u == br_u and storm_v == br_v)
smv_is_blm = (storm_u == bl_u and storm_v == bl_v)
smv_is_mnw = (storm_u == mn_u and storm_v == mn_v)
if not (np.isnan(storm_u) or np.isnan(storm_v)) and not (smv_is_brm or smv_is_blm or smv_is_mnw):
pylab.plot(storm_u, storm_v, 'k+', markersize=6)
pylab.text(storm_u + 0.5, storm_v - 0.5, "SM", ha='left', va='top', color='k', fontsize=10)
def _plot_background(min_u, max_u, min_v, max_v):
max_ring = int(np.ceil(max(
np.hypot(min_u, min_v),
np.hypot(min_u, max_v),
np.hypot(max_u, min_v),
np.hypot(max_u, max_v)
)))
pylab.axvline(x=0, linestyle='-', color='#999999')
pylab.axhline(y=0, linestyle='-', color='#999999')
for irng in range(10, max_ring, 10):
ring = Circle((0., 0.), irng, linestyle='dashed', fc='none', ec='#999999')
pylab.gca().add_patch(ring)
if irng <= max_u - 10:
rng_str = "%d kts" % irng if max_u - 20 < irng <= max_u - 10 else "%d" % irng
pylab.text(irng + 0.5, -0.5, rng_str, ha='left', va='top', fontsize=9, color='#999999', clip_on=True, clip_box=pylab.gca().get_clip_box())
def plot_hodograph(data, parameters, fname=None, web=False, fixed=False, archive=False):
img_title = "%s VWP valid %s" % (data.rid, data['time'].strftime("%d %b %Y %H%M UTC"))
if fname is not None:
img_file_name = fname
else:
img_file_name = "%s_vad.png" % data.rid
u, v = vec2comp(data['wind_dir'], data['wind_spd'])
sat_age = 6 * 3600
if fixed or len(u) == 0:
ctr_u, ctr_v = 20, 20
size = 120
else:
ctr_u = u.mean()
ctr_v = v.mean()
size = max(u.max() - u.min(), v.max() - v.min()) + 20
size = max(120, size)
min_u = ctr_u - size / 2
max_u = ctr_u + size / 2
min_v = ctr_v - size / 2
max_v = ctr_v + size / 2
now = datetime.utcnow()
img_age = now - data['time']
age_cstop = min(_total_seconds(img_age) / sat_age, 1) * 0.4
age_color = mpl.cm.get_cmap('hot')(age_cstop)[:-1]
age_str = "Image created on %s (%s old)" % (now.strftime("%d %b %Y %H%M UTC"), _fmt_timedelta(img_age))
pylab.figure(figsize=(10, 7.5), dpi=150)
fig_wid, fig_hght = pylab.gcf().get_size_inches()
fig_aspect = fig_wid / fig_hght
axes_left = 0.05
axes_bot = 0.05
axes_hght = 0.9
axes_wid = axes_hght / fig_aspect
pylab.axes((axes_left, axes_bot, axes_wid, axes_hght))
_plot_background(min_u, max_u, min_v, max_v)
_plot_data(data, parameters)
_plot_param_table(parameters, web=web)
pylab.xlim(min_u, max_u)
pylab.ylim(min_v, max_v)
pylab.xticks([])
pylab.yticks([])
if not archive:
pylab.title(img_title, color=age_color)
pylab.text(0., -0.01, age_str, transform=pylab.gca().transAxes, ha='left', va='top', fontsize=9, color=age_color)
else:
pylab.title(img_title)
if web:
web_brand = "http://www.autumnsky.us/vad/"
pylab.text(1.0, -0.01, web_brand, transform=pylab.gca().transAxes, ha='right', va='top', fontsize=9)
pylab.savefig(img_file_name, dpi=pylab.gcf().dpi)
pylab.close()
if web:
bounds = {'min_u':min_u, 'max_u':max_u, 'min_v':min_v, 'max_v':max_v}
print(json.dumps(bounds))