0755e4a1d9252340b01c0442fd092b587ac98837
2 # -*- coding: utf-8 -*-
5 InfoEx <-> NRCS/MesoWest Auto Wx implementation
7 Wylark Mountaineering LLC
9 This program fetches data from either an NRCS SNOTEL site or MesoWest
10 weather station and pushes it to InfoEx using the new automated weather
11 system implementation.
13 It is designed to be run hourly, and it asks for the last three hours
14 of data of each desired type, and selects the most recent one. This
15 lends some resiliency to the process and helps ensure that we have a
16 value to send, but it can lead to somewhat inconsistent/untruthful
17 data if e.g. the HS is from the last hour but the tempPres is from two
18 hours ago because the instrumentation had a hiccup. It's worth
19 considering if this is a bug or a feature.
21 For more information, see file: README
22 For licensing, see file: LICENSE
33 from ftplib
import FTP
34 from optparse
import OptionParser
40 import zeep
.transports
44 LOG
= logging
.getLogger(__name__
)
45 LOG
.setLevel(logging
.NOTSET
)
48 """Return OptionParser for this program"""
49 parser
= OptionParser(version
=__version__
)
51 parser
.add_option("--config",
54 help="location of config file")
56 parser
.add_option("--log-level",
59 help="set the log level (debug, info, warning)")
61 parser
.add_option("--dry-run",
65 help="fetch data but don't upload to InfoEx")
69 def setup_config(config
):
70 """Setup config variable based on values specified in the ini file"""
73 'host': config
['infoex']['host'],
74 'uuid': config
['infoex']['uuid'],
75 'api_key': config
['infoex']['api_key'],
76 'csv_filename': config
['infoex']['csv_filename'],
77 'location_uuid': config
['infoex']['location_uuid'],
78 'wx_data': {}, # placeholder key, values to come later
82 station
['provider'] = config
['station']['type']
84 if station
['provider'] not in ['nrcs', 'mesowest']:
85 print("Please specify either nrcs or mesowest as the station type.")
88 if station
['provider'] == 'nrcs':
89 station
['source'] = 'https://www.wcc.nrcs.usda.gov/awdbWebService/services?WSDL'
90 station
['station_id'] = config
['station']['station_id']
91 station
['desired_data'] = config
['station']['desired_data'].split(',')
93 # XXX: For NRCS, we're manually overriding units for now! Once
94 # unit conversion is supported for NRCS, REMOVE THIS!
95 if 'units' not in station
:
96 station
['units'] = 'imperial'
98 if station
['provider'] == 'mesowest':
99 station
['source'] = 'https://api.synopticdata.com/v2/stations/timeseries'
100 station
['station_id'] = config
['station']['station_id']
101 station
['units'] = config
['station']['units']
102 station
['desired_data'] = config
['station']['desired_data']
104 # construct full API URL (sans start/end time, added later)
105 station
['source'] = station
['source'] + '?token=' + \
106 config
['station']['token'] + \
107 '&within=60&units=' + station
['units'] + \
108 '&stid=' + station
['station_id'] + \
109 '&vars=' + station
['desired_data']
111 except KeyError as err
:
112 LOG
.critical("%s not defined in configuration file", err
)
115 # all sections/values present in config file, final sanity check
117 for key
in config
.sections():
118 for subkey
in config
[key
]:
119 if not config
[key
][subkey
]:
122 LOG
.critical("Config value '%s.%s' is empty", key
, subkey
)
125 return (infoex
, station
)
127 def setup_logging(log_level
):
128 """Setup our logging infrastructure"""
130 from systemd
.journal
import JournalHandler
131 LOG
.addHandler(JournalHandler())
133 ## fallback to syslog
134 #import logging.handlers
135 #LOG.addHandler(logging.handlers.SysLogHandler())
137 handler
= logging
.StreamHandler(sys
.stdout
)
138 LOG
.addHandler(handler
)
141 if log_level
in [None, 'debug', 'info', 'warning']:
142 if log_level
== 'debug':
143 LOG
.setLevel(logging
.DEBUG
)
144 elif log_level
== 'info':
145 LOG
.setLevel(logging
.INFO
)
146 elif log_level
== 'warning':
147 LOG
.setLevel(logging
.WARNING
)
149 LOG
.setLevel(logging
.NOTSET
)
156 """Main routine: sort through args, decide what to do, then do it"""
157 parser
= get_parser()
158 (options
, args
) = parser
.parse_args()
160 config
= configparser
.ConfigParser(allow_no_value
=False)
162 if not options
.config
:
164 print("\nPlease specify a configuration file via --config.")
167 config
.read(options
.config
)
169 if not setup_logging(options
.log_level
):
171 print("\nPlease select an appropriate log level or remove the switch (--log-level).")
174 (infoex
, station
) = setup_config(config
)
176 LOG
.debug('Config parsed, starting up')
179 (fmap
, final_data
) = setup_infoex_fields_mapping(infoex
['location_uuid'])
180 iemap
= setup_infoex_counterparts_mapping(station
['provider'])
182 # override units if user selected metric
183 if station
['units'] == 'metric':
184 final_data
= switch_units_to_metric(final_data
, fmap
)
186 (begin_date
, end_date
) = setup_time_values()
189 LOG
.debug("Getting %s data from %s to %s", str(station
['desired_data']),
190 str(begin_date
), str(end_date
))
192 time_all_elements
= time
.time()
195 if station
['provider'] == 'nrcs':
196 infoex
['wx_data'] = get_nrcs_data(begin_date
, end_date
, station
)
197 elif station
['provider'] == 'mesowest':
198 infoex
['wx_data'] = get_mesowest_data(begin_date
, end_date
,
201 LOG
.info("Time taken to get all data : %.3f sec", time
.time() -
204 LOG
.debug("infoex[wx_data]: %s", str(infoex
['wx_data']))
206 # Now we only need to add in what we want to change thanks to that
207 # abomination of a variable declaration earlier
208 final_data
[fmap
['Location UUID']] = infoex
['location_uuid']
209 final_data
[fmap
['obDate']] = end_date
.strftime('%m/%d/%Y')
210 final_data
[fmap
['obTime']] = end_date
.strftime('%H:%M')
212 for element_cd
in infoex
['wx_data']:
213 if element_cd
not in iemap
:
214 LOG
.warning("BAD KEY wx_data['%s']", element_cd
)
217 # CONSIDER: Casting every value to Float() -- need to investigate if
218 # any possible elementCds we may want are any other data
221 # Another possibility is to query the API with
222 # getStationElements and temporarily store the
223 # storedUnitCd. But that's pretty network-intensive and
224 # may not even be worth it if there's only e.g. one or two
225 # exceptions to any otherwise uniformly Float value set.
226 final_data
[fmap
[iemap
[element_cd
]]] = infoex
['wx_data'][element_cd
]
228 LOG
.debug("final_data: %s", str(final_data
))
230 if not write_local_csv(infoex
['csv_filename'], final_data
):
231 LOG
.warning('Could not write local CSV file: %s',
232 infoex
['csv_filename'])
235 if not options
.dry_run
:
236 upload_csv(infoex
['csv_filename'], infoex
)
241 # data structure operations
242 def setup_infoex_fields_mapping(location_uuid
):
244 Create a mapping of InfoEx fields to the local data's indexing scheme.
248 This won't earn style points in Python, but here we establish a couple
249 of helpful mappings variables. The reason this is helpful is that the
250 end result is simply an ordered set, the CSV file. But we still may
251 want to manipulate the values arbitrarily before writing that file.
253 Also note that the current Auto Wx InfoEx documentation shows these
254 keys in a graphical table with the "index" beginning at 1, but here we
255 sanely index beginning at 0.
257 # pylint: disable=too-many-statements,multiple-statements,bad-whitespace
258 fmap
= {} ; final_data
= [None] * 29
259 fmap
['Location UUID'] = 0 ; final_data
[0] = location_uuid
260 fmap
['obDate'] = 1 ; final_data
[1] = None
261 fmap
['obTime'] = 2 ; final_data
[2] = None
262 fmap
['timeZone'] = 3 ; final_data
[3] = 'Pacific'
263 fmap
['tempMaxHour'] = 4 ; final_data
[4] = None
264 fmap
['tempMaxHourUnit'] = 5 ; final_data
[5] = 'F'
265 fmap
['tempMinHour'] = 6 ; final_data
[6] = None
266 fmap
['tempMinHourUnit'] = 7 ; final_data
[7] = 'F'
267 fmap
['tempPres'] = 8 ; final_data
[8] = None
268 fmap
['tempPresUnit'] = 9 ; final_data
[9] = 'F'
269 fmap
['precipitationGauge'] = 10 ; final_data
[10] = None
270 fmap
['precipitationGaugeUnit'] = 11 ; final_data
[11] = 'in'
271 fmap
['windSpeedNum'] = 12 ; final_data
[12] = None
272 fmap
['windSpeedUnit'] = 13 ; final_data
[13] = 'mph'
273 fmap
['windDirectionNum'] = 14 ; final_data
[14] = None
274 fmap
['hS'] = 15 ; final_data
[15] = None
275 fmap
['hsUnit'] = 16 ; final_data
[16] = 'in'
276 fmap
['baro'] = 17 ; final_data
[17] = None
277 fmap
['baroUnit'] = 18 ; final_data
[18] = 'inHg'
278 fmap
['rH'] = 19 ; final_data
[19] = None
279 fmap
['windGustSpeedNum'] = 20 ; final_data
[20] = None
280 fmap
['windGustSpeedNumUnit'] = 21 ; final_data
[21] = 'mph'
281 fmap
['windGustDirNum'] = 22 ; final_data
[22] = None
282 fmap
['dewPoint'] = 23 ; final_data
[23] = None
283 fmap
['dewPointUnit'] = 24 ; final_data
[24] = 'F'
284 fmap
['hn24Auto'] = 25 ; final_data
[25] = None
285 fmap
['hn24AutoUnit'] = 26 ; final_data
[26] = 'in'
286 fmap
['hstAuto'] = 27 ; final_data
[27] = None
287 fmap
['hstAutoUnit'] = 28 ; final_data
[28] = 'in'
289 return (fmap
, final_data
)
291 def setup_infoex_counterparts_mapping(provider
):
293 Create a mapping of the NRCS/MesoWest fields that this program supports to
294 their InfoEx counterparts
298 if provider
== 'nrcs':
299 iemap
['PREC'] = 'precipitationGauge'
300 iemap
['TOBS'] = 'tempPres'
302 iemap
['PRES'] = 'baro'
304 iemap
['WSPD'] = 'windSpeedNum'
305 iemap
['WDIR'] = 'windDirectionNum'
306 # unsupported by NRCS:
308 elif provider
== 'mesowest':
309 iemap
['precip_accum'] = 'precipitationGauge'
310 iemap
['air_temp'] = 'tempPres'
311 iemap
['snow_depth'] = 'hS'
312 iemap
['pressure'] = 'baro'
313 iemap
['relative_humidity'] = 'rH'
314 iemap
['wind_speed'] = 'windSpeedNum'
315 iemap
['wind_direction'] = 'windDirectionNum'
316 iemap
['wind_gust'] = 'windGustSpeedNum'
320 # provider-specific operations
321 def get_nrcs_data(begin
, end
, station
):
322 """get the data we're after from the NRCS WSDL"""
323 transport
= zeep
.transports
.Transport(cache
=zeep
.cache
.SqliteCache())
324 client
= zeep
.Client(wsdl
=station
['source'], transport
=transport
)
327 for element_cd
in station
['desired_data']:
328 time_element
= time
.time()
330 # get the last three hours of data for this elementCd/element_cd
331 tmp
= client
.service
.getHourlyData(
332 stationTriplets
=[station
['station_id']],
333 elementCd
=element_cd
,
338 LOG
.info("Time to get NRCS elementCd '%s': %.3f sec", element_cd
,
339 time
.time() - time_element
)
341 values
= tmp
[0]['values']
343 # sort and isolate the most recent
345 # NOTE: we do this because sometimes there are gaps in hourly data
346 # in NRCS; yes, we may end up with slightly inaccurate data,
347 # so perhaps this decision will be re-evaluated in the future
349 ordered
= sorted(values
, key
=lambda t
: t
['dateTime'], reverse
=True)
350 remote_data
[element_cd
] = ordered
[0]['value']
352 remote_data
[element_cd
] = None
356 def get_mesowest_data(begin
, end
, station
):
357 """get the data we're after from the MesoWest/Synoptic API"""
360 # massage begin/end date format
361 begin_date_str
= begin
.strftime('%Y%m%d%H%M')
362 end_date_str
= end
.strftime('%Y%m%d%H%M')
364 # construct final, completed API URL
365 api_req_url
= station
['source'] + '&start=' + begin_date_str
+ '&end=' + end_date_str
366 req
= requests
.get(api_req_url
)
371 LOG
.error("Bad JSON in MesoWest response")
375 observations
= json
['STATION'][0]['OBSERVATIONS']
377 LOG
.error("Bad JSON in MesoWest response")
380 pos
= len(observations
['date_time']) - 1
382 for element_cd
in station
['desired_data'].split(','):
383 # sort and isolate the most recent, see note above in NRCS for how and
386 # NOTE: Unlike in the NRCS case, the MesoWest API response contains all
387 # data (whereas with NRCS, we have to make a separate request for
388 # each element we want). This is nice for network efficiency but
389 # it means we have to handle this part differently for each.
391 # NOTE: Also unlike NRCS, MesoWest provides more granular data; NRCS
392 # provides hourly data, but MesoWest can often provide data every
393 # 10 minutes -- though this provides more opportunity for
396 # we may not have the data at all
397 key_name
= element_cd
+ '_set_1'
398 if key_name
in observations
:
399 if observations
[key_name
][pos
]:
400 remote_data
[element_cd
] = observations
[key_name
][pos
]
402 remote_data
[element_cd
] = None
404 remote_data
[element_cd
] = None
408 def switch_units_to_metric(data_map
, mapping
):
409 """replace units with metric counterparts"""
411 # NOTE: to update this, use the fmap<->final_data mapping laid out
412 # in setup_infoex_fields_mapping ()
414 # NOTE: this only 'works' with MesoWest for now, as the MesoWest API
415 # itself handles the unit conversion; in the future, we will also
416 # support NRCS unit conversion, but this must be done by this
418 data_map
[mapping
['tempPresUnit']] = 'C'
419 data_map
[mapping
['hsUnit']] = 'm'
420 data_map
[mapping
['windSpeedUnit']] = 'm/s'
421 data_map
[mapping
['windGustSpeedNumUnit']] = 'm/s'
426 def write_local_csv(path_to_file
, data
):
427 """Write the specified CSV file to disk"""
428 with
open(path_to_file
, 'w') as file_object
:
429 # The requirement is that empty values are represented in the CSV
430 # file as "", csv.QUOTE_NONNUMERIC achieves that
431 LOG
.debug("writing CSV file '%s'", path_to_file
)
432 writer
= csv
.writer(file_object
, quoting
=csv
.QUOTE_NONNUMERIC
)
433 writer
.writerow(data
)
437 def upload_csv(path_to_file
, infoex_data
):
438 """Upload the specified CSV file to InfoEx FTP and remove the file"""
439 with
open(path_to_file
, 'rb') as file_object
:
440 LOG
.debug("uploading FTP file '%s'", infoex_data
['host'])
441 ftp
= FTP(infoex_data
['host'], infoex_data
['uuid'],
442 infoex_data
['api_key'])
443 ftp
.storlines('STOR ' + path_to_file
, file_object
)
446 os
.remove(path_to_file
)
448 # other miscellaneous routines
449 def setup_time_values():
450 """establish time bounds of data request(s)"""
451 # floor time to nearest hour
452 date_time
= datetime
.datetime
.now()
453 end_date
= date_time
- datetime
.timedelta(minutes
=date_time
.minute
% 60,
454 seconds
=date_time
.second
,
455 microseconds
=date_time
.microsecond
)
456 begin_date
= end_date
- datetime
.timedelta(hours
=3)
457 return (begin_date
, end_date
)
459 if __name__
== "__main__":