-
Notifications
You must be signed in to change notification settings - Fork 0
/
radar.py
214 lines (172 loc) · 6.59 KB
/
radar.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
"""
radar.py: download and perform image tasks on radar imagery.
The entire class needs to be re-factored, because NWS has changed how they
deliver radar imagery in mid-Dec 2020. I have started a GitHub bug issue.
"""
from __future__ import print_function
import os
import logging
import requests
import gzip
from PIL import Image
class Radar(object):
"""
download and merge radar imagery and layers.
radar station abbr: data['radar_station']
radar url: data['radar_url']
names of all the files needed to overlay: data['graphics_list']
"""
def __init__(self,
data=''):
self.data = data
self.defaults = data['defaults']
self.radar_url = data['defaults']['radar_url']
self.station = data['radar_station']
self.assets_url = data['defaults']['weather_url_root']
self.warnings_url = data['defaults']['warnings_url']
self.problem = False
def get_radar(self):
"""
Using the NWS radar station abbreviation, retrieve the current radar image
and world file from the NWS.
"""
url_path = self.radar_url.format(station=self.station, image='N0R_0.gfw')
logging.debug('Making request to: %s', url_path)
response1 = requests.get(url_path, verify=True, timeout=10)
if response1.status_code != 200:
logging.error('Response from server was not OK: %s', response1.status_code)
self.problem = True
return False
logging.debug('Server response: %s', response1.status_code)
cur1 = open(os.path.join(self.data['output_dir'], 'current_image.gfw'), 'w')
cur1.write(response1.text)
cur1.close()
url_path = self.radar_url.format(station=self.station, image='N0R_0.gif')
logging.debug('Making request to: %s', url_path)
response2 = requests.get(url_path, verify=True, timeout=10)
if response2.status_code != 200:
logging.error('Response from server was not OK: %s', response2.status_code)
self.problem = True
return False
logging.debug('Server response: %s', response2.status_code)
cur2 = open(os.path.join(self.data['output_dir'], 'current_image.gif'), 'wb')
cur2.write(response2.content)
cur2.close()
return True
def get_warnings_box(self):
"""
Retrieve the severe weather graphics boxes (suitable for overlaying)
from the NWS for the specified locale.
"""
warnings = 'Warnings'
url_path = self.warnings_url.format(station=self.station, warnings=warnings)
logging.debug('Making request to: %s', url_path)
response = requests.get(url_path, verify=True, timeout=10)
try:
cur = open(os.path.join(self.data['output_dir'], 'current_warnings.gif'), 'wb')
cur.write(response.content)
cur.close()
return True
except Exception as exc:
logging.error('Exception: %s', exc)
self.problem = True
return False
def check_assets(self):
"""
Confirm that layer images from NWS already exist where they should be.
"""
for asset in self.data['radar_layers']:
file_url_dir = '{0}'.format(asset.format(r_abbr=self.station))
filename = file_url_dir.split('/')[-1]
logging.debug('Local file path: %s', os.path.join(self.data['output_dir'], filename))
if not self._check_asset(self.data['output_dir'],
filename=filename,
url_dir=file_url_dir,
url=self.assets_url):
self.problem = True
file_url_dir = '{0}'.format(self.defaults['legend_file'].format(radar=self.station))
filename = file_url_dir.split('/')[-1]
logging.debug('Local legend file path: %s', os.path.join(self.data['output_dir'], filename))
if not self._check_asset(self.data['output_dir'],
filename=filename,
url_dir=file_url_dir,
url=self.defaults['legend_url_root']):
self.problem = True
return False
return True
def _check_asset(self, outputdir, filename, url_dir, url):
"""
Confirm that a file exists, then go get it remotely if needed.
"""
localpath = os.path.join(outputdir, filename)
if os.path.isfile(localpath) is False:
logging.info('Retrieving %s from %s', localpath, os.path.join(url, url_dir))
if self._retrieve_asset(file_url_dir=url_dir, url=url, filename=filename):
logging.debug('Got it.')
else:
logging.error('Unable to get the file. Returning False.')
self.problem = True
return False
else:
logging.debug('%s is where it should be (in %s)', filename, outputdir)
return True
def _retrieve_asset(self, file_url_dir, url, filename):
"""
Retrieve a file from a URL.
"""
graphic = requests.get(os.path.join(url, file_url_dir),
verify=True, timeout=10)
with open(os.path.join(self.data['output_dir'], filename), 'wb') as output:
output.write(graphic.content)
output.close()
return True
def _overlay_composite(self, resultfile='bkg2.gif'):
"""
Take list of images in output_dir and overlay them as needed to make
the proper radar overlay image.
"""
blist = ['highways', 'ring', 'lrg_cities', 'counties']
im1 = self._open_transparent('highways')
for layer in blist[1:]:
temp = self._open_transparent(layer)
if temp:
im1.paste(temp, (0, 0), temp)
im1.save(os.path.join(self.data['output_dir'], resultfile),
transparency=0)
return im1
def _open_transparent(self, layer):
"""
Open the image and enable transparent layers.
"""
rad_l = self.data['radar_layers']
try:
imagename = rad_l[layer].format(r_abbr=rad_l['r_abbr']).split('/')[-1]
imagepath = os.path.join(self.data['output_dir'], imagename)
if not os.path.exists(imagepath):
return None
except Exception as exc:
logging.error('Exception: %s', exc)
return None
try:
return Image.open(imagepath).convert('RGBA')
except Exception as exc:
logging.error('Exception: %s', exc)
return None
def retrieve_image(self, directory, imagename):
"""
Stub to replace the outdated get_radar() method, above, with a means to
retrieve RIDGEII images.
"""
result = requests.get(os.path.join(directory, imagename))
if result.status_code == 200:
print('Server returned OK.')
return result
return None
def unzip_and_write(self, payload, filename):
"""
Stub to unzip a gzipped binary file (here, always a NWS GeoTIFF) and
write it locally.
"""
with open(filename, 'wb') as binary_output:
binary_output.write(gzip.decompress(payload))
return True