/
level3.py
218 lines (160 loc) · 6.59 KB
/
level3.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
#######################################################
# #
# PYTHON NEXRAD PLOT GENERATION #
# #
# Level-3, Single Site #
# #
# Warren Pettee (@wpettee) #
# #
# #
#######################################################
import argparse
import sys
import time
from datetime import datetime, timedelta
import threading
from threading import Thread
from siphon.radarserver import RadarServer
from siphon.cdmr import Dataset
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.animation as animation
mpl.rcParams['toolbar'] = 'None'
import cartopy
from metpy.plots import ctables
import validation
#------------------------------------------------------
def update(frame):
global SITE
global PRODUCT
print("Building Frame:",frame)
#What is the age of this frame?
frameIndex = frame % 9
frameAge = frameIndex * 10 # minutes old
# WHAT TIME WILL THE FRAME BE??
date = datetime.utcnow() - timedelta(minutes=frameAge)
year = date.year
month = date.month
day = date.day
hour = date.hour
minute = date.minute
# What type of radar site is this?..
siteType = validation.checkRadarType(SITE)
ncfVar = validation.checkProduct(PRODUCT)
colorTable = validation.checkColorTable(PRODUCT)
if siteType=='88D':
rs = RadarServer('http://thredds.ucar.edu/thredds/radarServer/nexrad/level3/IDD/')
elif siteType=='TDWR':
rs = RadarServer('http://thredds.ucar.edu/thredds/radarServer/terminal/level3/IDD/')
else:
print('INVALID SITE IDENTIFIER')
sys.exit()
# ACQUIRE DATA ----------------------------------------
query = rs.query()
query.stations(args.site).time(datetime(year,month,day,hour,minute)).variables(args.product)
rs.validate_query(query)
catalog = rs.get_catalog(query)
catalog.datasets
ds = list(catalog.datasets.values())[0]
ds.access_urls
# READ DATA ------------------------------------------
data = Dataset(ds.access_urls['CdmRemote'])
rng = data.variables['gate'][:]
az = data.variables['azimuth'][:]
ref = data.variables[ncfVar][:]
x = (rng * np.sin(np.deg2rad(az))[:, None])
y = (rng * np.cos(np.deg2rad(az))[:, None])
ref = np.ma.array(ref, mask=np.isnan(ref))
plot = ax.pcolormesh(x, y, ref, cmap=cmap, norm=norm, zorder=2)
title_line1 = '%s %s - %i:%i' % (args.site,args.product,hour,minute)
plt.title(title_line1,color='k',fontsize=18,fontweight='bold',style='italic')
return plot
# Command Line Functions:
parser = argparse.ArgumentParser(description='NEXRAD/TDWR Site Information')
parser.add_argument('site',
help='3-Letter Site Identifier (WSR-88D or TDWR)')
parser.add_argument('product',
help='3-Letter Product Identifier (See RPCCDS list at NWS)\nExample:Reflectivity(N0R or TR0)\nVelocity(N0V or TV0)')
parser.add_argument('animate',
help='Build animated image? "true" or "false"')
args = parser.parse_args()
SITE=args.site
PRODUCT=args.product
# WHAT TIME IS IT? ------------------------------------
date = datetime.utcnow()
year = datetime.utcnow().year
month = datetime.utcnow().month
day = datetime.utcnow().day
hour = datetime.utcnow().hour
minute = datetime.utcnow().minute
# What type of radar site is this?..
siteType = validation.checkRadarType(args.site)
ncfVar = validation.checkProduct(args.product)
colorTable = validation.checkColorTable(args.product)
if siteType=='88D':
rs = RadarServer('http://thredds.ucar.edu/thredds/radarServer/nexrad/level3/IDD/')
elif siteType=='TDWR':
rs = RadarServer('http://thredds.ucar.edu/thredds/radarServer/terminal/level3/IDD/')
else:
print('INVALID SITE IDENTIFIER')
sys.exit()
# ACQUIRE DATA ----------------------------------------
query = rs.query()
query.stations(args.site).time(datetime(year,month,day,hour,minute)).variables(args.product)
rs.validate_query(query)
catalog = rs.get_catalog(query)
catalog.datasets
ds = list(catalog.datasets.values())[0]
ds.access_urls
# READ DATA ------------------------------------------
print("--- AQUIRING DATA ---")
data = Dataset(ds.access_urls['CdmRemote'])
print("Aquisition Complete!!")
#print (data.variables) ### DEBUG
rng = data.variables['gate'][:]
az = data.variables['azimuth'][:]
ref = data.variables[ncfVar][:]
x = (rng * np.sin(np.deg2rad(az))[:, None])
y = (rng * np.cos(np.deg2rad(az))[:, None])
ref = np.ma.array(ref, mask=np.isnan(ref))
# BEGIN PLOTTING --------------------------------------------------
### TODO: Losing loading time in county rendering.. fix me
# Create projection centered on the radar. This allows us to use x
# and y relative to the radar.
proj = cartopy.crs.LambertConformal(central_longitude=data.RadarLongitude,
central_latitude=data.RadarLatitude)
# New figure with specified projection
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(1, 1, 1, projection=proj)
print("Loading geography overlays...")
# Grab state borders
#state_borders = cartopy.feature.NaturalEarthFeature(
# category='cultural', name='admin_1_states_provinces_lines',
# scale='50m', facecolor='none')
#ax.add_feature(state_borders, edgecolor='black', linewidth=2, zorder=2)
# Counties
counties = cartopy.io.shapereader.Reader('data/counties')
ax.add_geometries(counties.geometries(), cartopy.crs.PlateCarree(),
facecolor='#C2A385', edgecolor='grey', zorder=1)
# Interstates
interstate = cartopy.io.shapereader.Reader('data/interstates')
ax.add_geometries(interstate.geometries(), cartopy.crs.PlateCarree(),
facecolor='none', edgecolor='#B20000', zorder=1)
# Hydrography
#hydro = cartopy.io.shapereader.Reader('data/hydro')
#ax.add_geometries(hydro.geometries(), cartopy.crs.PlateCarree(),
# facecolor='none', edgecolor='#ADD6FF', zorder=1)
# Set limits in lat/lon space
# LonW, LonE, LatN, LatS
#ax.set_extent([-81.8, -80, 36, 34.5])
print("Building Figure...")
norm, cmap = ctables.registry.get_with_steps(colorTable, 5, 5)
plot = ax.pcolormesh(x, y, ref, cmap=cmap, norm=norm, zorder=2)
#ax.contourf(x, y, ref, cmap=cmap, norm=norm, zorder=2)
title_line1 = '%s %s - %i:%i' % (args.site,args.product,hour,minute)
plt.title(title_line1,color='k',fontsize=18,fontweight='bold',style='italic')
if args.animate == 'true':
animation = animation.FuncAnimation(fig, update, interval=15, blit=False, frames=10)
animation.save('nexpy.gif', writer='imagemagick', fps=15, dpi=40)
plt.show()