2b7880eaf9865acad39b83c6a56d8b5e1a55f92c
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
35 from ftplib
import FTP
36 from argparse
import ArgumentParser
44 import zeep
.transports
48 LOG
= logging
.getLogger(__name__
)
49 LOG
.setLevel(logging
.NOTSET
)
51 urllib3
.disable_warnings()
54 """Return OptionParser for this program"""
55 parser
= ArgumentParser()
57 parser
.add_argument("--version",
61 parser
.add_argument("--config",
64 help="location of config file")
66 parser
.add_argument("--log-level",
69 help="set the log level (debug, info, warning)")
71 parser
.add_argument("--dry-run",
75 help="fetch data but don't upload to InfoEx")
79 def setup_config(config
):
80 """Setup config variable based on values specified in the ini file"""
83 'host': config
['infoex']['host'],
84 'uuid': config
['infoex']['uuid'],
85 'api_key': config
['infoex']['api_key'],
86 'csv_filename': config
['infoex']['csv_filename'],
87 'location_uuid': config
['infoex']['location_uuid'],
88 'wx_data': {}, # placeholder key, values to come later
92 station
['provider'] = config
['station']['type']
94 if station
['provider'] not in ['nrcs', 'mesowest', 'python']:
95 print("Please specify either nrcs or mesowest as the station type.")
98 if station
['provider'] == 'nrcs':
99 station
['source'] = 'https://www.wcc.nrcs.usda.gov/awdbWebService/services?WSDL'
100 station
['station_id'] = config
['station']['station_id']
101 station
['desired_data'] = config
['station']['desired_data'].split(',')
102 station
['units'] = config
['station']['units']
104 if station
['provider'] == 'mesowest':
105 station
['source'] = 'https://api.synopticdata.com/v2/stations/timeseries'
106 station
['station_id'] = config
['station']['station_id']
107 station
['units'] = config
['station']['units']
108 station
['desired_data'] = config
['station']['desired_data']
110 # construct full API URL (sans start/end time, added later)
111 station
['source'] = station
['source'] + '?token=' + \
112 config
['station']['token'] + \
113 '&within=60&units=' + station
['units'] + \
114 '&stid=' + station
['station_id'] + \
115 '&vars=' + station
['desired_data']
117 if station
['provider'] == 'python':
118 station
['path'] = config
['station']['path']
120 tz
= 'America/Los_Angeles'
122 if 'tz' in config
['station']:
123 tz
= config
['station']['tz']
126 station
['tz'] = pytz
.timezone(tz
)
127 except pytz
.exceptions
.UnknownTimeZoneError
:
128 LOG
.critical("%s is not a valid timezone", tz
)
131 # By default, fetch three hours of data
133 # If user wants hn24 or wind averaging, then
135 station
['num_hrs_to_fetch'] = 3
138 if 'hn24' in config
['station']:
139 if config
['station']['hn24'] not in ['true', 'false']:
140 raise ValueError("hn24 must be either 'true' or 'false'")
142 if config
['station']['hn24'] == "true":
143 station
['hn24'] = True
144 station
['num_hrs_to_fetch'] = 24
146 station
['hn24'] = False
149 station
['hn24'] = False
152 if 'wind_mode' in config
['station']:
153 if config
['station']['wind_mode'] not in ['normal', 'average']:
154 raise ValueError("wind_mode must be either 'normal' or 'average'")
156 station
['wind_mode'] = config
['station']['wind_mode']
158 if station
['wind_mode'] == "average":
159 station
['num_hrs_to_fetch'] = 24
162 station
['wind_mode'] = "normal"
164 except KeyError as err
:
165 LOG
.critical("%s not defined in configuration file", err
)
167 except ValueError as err
:
168 LOG
.critical("%s", err
)
171 # all sections/values present in config file, final sanity check
173 for key
in config
.sections():
174 for subkey
in config
[key
]:
175 if not config
[key
][subkey
]:
178 LOG
.critical("Config value '%s.%s' is empty", key
, subkey
)
181 return (infoex
, station
)
183 def setup_logging(log_level
):
184 """Setup our logging infrastructure"""
186 from systemd
.journal
import JournalHandler
187 LOG
.addHandler(JournalHandler())
189 ## fallback to syslog
190 #import logging.handlers
191 #LOG.addHandler(logging.handlers.SysLogHandler())
193 handler
= logging
.StreamHandler(sys
.stdout
)
194 formatter
= logging
.Formatter('%(asctime)s.%(msecs)03d '
195 '%(levelname)s %(module)s - '
196 '%(funcName)s: %(message)s',
198 handler
.setFormatter(formatter
)
199 LOG
.addHandler(handler
)
202 if log_level
in [None, 'debug', 'info', 'warning']:
203 if log_level
== 'debug':
204 LOG
.setLevel(logging
.DEBUG
)
205 elif log_level
== 'info':
206 LOG
.setLevel(logging
.INFO
)
207 elif log_level
== 'warning':
208 LOG
.setLevel(logging
.WARNING
)
210 LOG
.setLevel(logging
.NOTSET
)
217 """Main routine: sort through args, decide what to do, then do it"""
218 parser
= get_parser()
219 options
= parser
.parse_args()
221 config
= configparser
.ConfigParser(allow_no_value
=False)
223 if not options
.config
:
225 print("\nPlease specify a configuration file via --config.")
228 config
.read(options
.config
)
230 if not setup_logging(options
.log_level
):
232 print("\nPlease select an appropriate log level or remove the switch (--log-level).")
235 (infoex
, station
) = setup_config(config
)
237 LOG
.debug('Config parsed, starting up')
240 (fmap
, final_data
) = setup_infoex_fields_mapping(infoex
['location_uuid'])
241 iemap
= setup_infoex_counterparts_mapping(station
['provider'])
243 # override units if user selected metric
244 if station
['provider'] != 'python' and station
['units'] == 'metric':
245 final_data
= switch_units_to_metric(final_data
, fmap
)
247 (begin_date
, end_date
) = setup_time_values(station
)
249 if station
['provider'] == 'python':
250 LOG
.debug("Getting custom data from external Python program")
252 LOG
.debug("Getting %s data from %s to %s (%s)",
253 str(station
['desired_data']),
254 str(begin_date
), str(end_date
), end_date
.tzinfo
.zone
)
256 time_all_elements
= time
.time()
259 if station
['provider'] == 'nrcs':
260 infoex
['wx_data'] = get_nrcs_data(begin_date
, end_date
, station
)
261 elif station
['provider'] == 'mesowest':
262 infoex
['wx_data'] = get_mesowest_data(begin_date
, end_date
,
264 elif station
['provider'] == 'python':
266 spec
= importlib
.util
.spec_from_file_location('custom_wx',
268 mod
= importlib
.util
.module_from_spec(spec
)
269 spec
.loader
.exec_module(mod
)
273 infoex
['wx_data'] = mod
.get_custom_data()
275 if infoex
['wx_data'] is None:
276 infoex
['wx_data'] = []
277 except Exception as exc
:
278 LOG
.error("Python program for custom Wx data failed in "
279 "execution: %s", str(exc
))
282 LOG
.info("Successfully executed external Python program")
284 LOG
.error("Please upgrade to Python 3.3 or later")
286 except FileNotFoundError
:
287 LOG
.error("Specified Python program for custom Wx data "
290 except Exception as exc
:
291 LOG
.error("A problem was encountered when attempting to "
292 "load your custom Wx program: %s", str(exc
))
295 LOG
.info("Time taken to get all data : %.3f sec", time
.time() -
298 LOG
.debug("infoex[wx_data]: %s", str(infoex
['wx_data']))
301 final_end_date
= end_date
.astimezone(station
['tz'])
303 # Now we only need to add in what we want to change thanks to that
304 # abomination of a variable declaration earlier
305 final_data
[fmap
['Location UUID']] = infoex
['location_uuid']
306 final_data
[fmap
['obDate']] = final_end_date
.strftime('%m/%d/%Y')
307 final_data
[fmap
['obTime']] = final_end_date
.strftime('%H:%M')
308 final_data
[fmap
['timeZone']] = station
['tz'].zone
310 for element_cd
in infoex
['wx_data']:
311 if element_cd
not in iemap
:
312 LOG
.warning("BAD KEY wx_data['%s']", element_cd
)
315 if infoex
['wx_data'][element_cd
] is None:
318 # do the conversion before the rounding
319 if station
['provider'] == 'nrcs' and station
['units'] == 'metric':
320 infoex
['wx_data'][element_cd
] = convert_nrcs_units_to_metric(element_cd
, infoex
['wx_data'][element_cd
])
322 # Massage precision of certain values to fit InfoEx's
325 # 0 decimal places: relative humidity, wind speed, wind
326 # direction, wind gust, snow depth
327 # 1 decimal place: air temp, baro
328 # Avoid transforming None values
329 if element_cd
in ['wind_speed', 'WSPD', 'wind_direction',
330 'RHUM', 'relative_humidity', 'WDIR',
331 'wind_gust', 'SNWD', 'snow_depth']:
332 infoex
['wx_data'][element_cd
] = round(infoex
['wx_data'][element_cd
])
333 elif element_cd
in ['TOBS', 'air_temp', 'PRES', 'pressure']:
334 infoex
['wx_data'][element_cd
] = round(infoex
['wx_data'][element_cd
], 1)
335 elif element_cd
in ['PREC', 'precip_accum']:
336 infoex
['wx_data'][element_cd
] = round(infoex
['wx_data'][element_cd
], 2)
338 # CONSIDER: Casting every value to Float() -- need to investigate if
339 # any possible elementCds we may want are any other data
342 # Another possibility is to query the API with
343 # getStationElements and temporarily store the
344 # storedUnitCd. But that's pretty network-intensive and
345 # may not even be worth it if there's only e.g. one or two
346 # exceptions to any otherwise uniformly Float value set.
347 final_data
[fmap
[iemap
[element_cd
]]] = infoex
['wx_data'][element_cd
]
349 LOG
.debug("final_data: %s", str(final_data
))
351 if infoex
['wx_data']:
352 if not write_local_csv(infoex
['csv_filename'], final_data
):
353 LOG
.warning('Could not write local CSV file: %s',
354 infoex
['csv_filename'])
357 if not options
.dry_run
:
358 upload_csv(infoex
['csv_filename'], infoex
)
363 # data structure operations
364 def setup_infoex_fields_mapping(location_uuid
):
366 Create a mapping of InfoEx fields to the local data's indexing scheme.
370 This won't earn style points in Python, but here we establish a couple
371 of helpful mappings variables. The reason this is helpful is that the
372 end result is simply an ordered set, the CSV file. But we still may
373 want to manipulate the values arbitrarily before writing that file.
375 Also note that the current Auto Wx InfoEx documentation shows these
376 keys in a graphical table with the "index" beginning at 1, but here we
377 sanely index beginning at 0.
379 # pylint: disable=too-many-statements,multiple-statements,bad-whitespace
380 fmap
= {} ; final_data
= [None] * 29
381 fmap
['Location UUID'] = 0 ; final_data
[0] = location_uuid
382 fmap
['obDate'] = 1 ; final_data
[1] = None
383 fmap
['obTime'] = 2 ; final_data
[2] = None
384 fmap
['timeZone'] = 3 ; final_data
[3] = 'Pacific'
385 fmap
['tempMaxHour'] = 4 ; final_data
[4] = None
386 fmap
['tempMaxHourUnit'] = 5 ; final_data
[5] = 'F'
387 fmap
['tempMinHour'] = 6 ; final_data
[6] = None
388 fmap
['tempMinHourUnit'] = 7 ; final_data
[7] = 'F'
389 fmap
['tempPres'] = 8 ; final_data
[8] = None
390 fmap
['tempPresUnit'] = 9 ; final_data
[9] = 'F'
391 fmap
['precipitationGauge'] = 10 ; final_data
[10] = None
392 fmap
['precipitationGaugeUnit'] = 11 ; final_data
[11] = 'in'
393 fmap
['windSpeedNum'] = 12 ; final_data
[12] = None
394 fmap
['windSpeedUnit'] = 13 ; final_data
[13] = 'mph'
395 fmap
['windDirectionNum'] = 14 ; final_data
[14] = None
396 fmap
['hS'] = 15 ; final_data
[15] = None
397 fmap
['hsUnit'] = 16 ; final_data
[16] = 'in'
398 fmap
['baro'] = 17 ; final_data
[17] = None
399 fmap
['baroUnit'] = 18 ; final_data
[18] = 'inHg'
400 fmap
['rH'] = 19 ; final_data
[19] = None
401 fmap
['windGustSpeedNum'] = 20 ; final_data
[20] = None
402 fmap
['windGustSpeedNumUnit'] = 21 ; final_data
[21] = 'mph'
403 fmap
['windGustDirNum'] = 22 ; final_data
[22] = None
404 fmap
['dewPoint'] = 23 ; final_data
[23] = None
405 fmap
['dewPointUnit'] = 24 ; final_data
[24] = 'F'
406 fmap
['hn24Auto'] = 25 ; final_data
[25] = None
407 fmap
['hn24AutoUnit'] = 26 ; final_data
[26] = 'in'
408 fmap
['hstAuto'] = 27 ; final_data
[27] = None
409 fmap
['hstAutoUnit'] = 28 ; final_data
[28] = 'in'
411 return (fmap
, final_data
)
413 def setup_infoex_counterparts_mapping(provider
):
415 Create a mapping of the NRCS/MesoWest fields that this program supports to
416 their InfoEx counterparts
420 if provider
== 'nrcs':
421 iemap
['PREC'] = 'precipitationGauge'
422 iemap
['TOBS'] = 'tempPres'
423 iemap
['TMAX'] = 'tempMaxHour'
424 iemap
['TMIN'] = 'tempMinHour'
426 iemap
['PRES'] = 'baro'
428 iemap
['WSPD'] = 'windSpeedNum'
429 iemap
['WDIR'] = 'windDirectionNum'
430 # unsupported by NRCS:
432 elif provider
== 'mesowest':
433 iemap
['precip_accum'] = 'precipitationGauge'
434 iemap
['air_temp'] = 'tempPres'
435 iemap
['air_temp_high_24_hour'] = 'tempMaxHour'
436 iemap
['air_temp_low_24_hour'] = 'tempMinHour'
437 iemap
['snow_depth'] = 'hS'
438 iemap
['pressure'] = 'baro'
439 iemap
['relative_humidity'] = 'rH'
440 iemap
['wind_speed'] = 'windSpeedNum'
441 iemap
['wind_direction'] = 'windDirectionNum'
442 iemap
['wind_gust'] = 'windGustSpeedNum'
443 elif provider
== 'python':
444 # we expect Python programs to use the InfoEx data type names
445 iemap
['precipitationGauge'] = 'precipitationGauge'
446 iemap
['tempPres'] = 'tempPres'
447 iemap
['tempMaxHour'] = 'tempMaxHour'
448 iemap
['tempMinHour'] = 'tempMinHour'
450 iemap
['baro'] = 'baro'
452 iemap
['windSpeedNum'] = 'windSpeedNum'
453 iemap
['windDirectionNum'] = 'windDirectionNum'
454 iemap
['windGustSpeedNum'] = 'windGustSpeedNum'
458 # provider-specific operations
459 def get_nrcs_data(begin
, end
, station
):
460 """get the data we're after from the NRCS WSDL"""
461 transport
= zeep
.transports
.Transport(cache
=zeep
.cache
.SqliteCache())
462 transport
.session
.verify
= False
463 client
= zeep
.Client(wsdl
=station
['source'], transport
=transport
)
466 # massage begin/end date format
467 begin_date_str
= begin
.strftime('%Y-%m-%d %H:%M:00')
468 end_date_str
= end
.strftime('%Y-%m-%d %H:%M:00')
470 for element_cd
in station
['desired_data']:
471 time_element
= time
.time()
473 # get the last three hours of data for this elementCd/element_cd
474 tmp
= client
.service
.getHourlyData(
475 stationTriplets
=[station
['station_id']],
476 elementCd
=element_cd
,
478 beginDate
=begin_date_str
,
479 endDate
=end_date_str
)
481 LOG
.info("Time to get NRCS elementCd '%s': %.3f sec", element_cd
,
482 time
.time() - time_element
)
484 values
= tmp
[0]['values']
486 # sort and isolate the most recent
488 # NOTE: we do this because sometimes there are gaps in hourly data
489 # in NRCS; yes, we may end up with slightly inaccurate data,
490 # so perhaps this decision will be re-evaluated in the future
492 ordered
= sorted(values
, key
=lambda t
: t
['dateTime'], reverse
=True)
493 remote_data
[element_cd
] = ordered
[0]['value']
495 remote_data
[element_cd
] = None
499 def get_mesowest_data(begin
, end
, station
):
500 """get the data we're after from the MesoWest/Synoptic API"""
503 # massage begin/end date format
504 begin_date_str
= begin
.strftime('%Y%m%d%H%M')
505 end_date_str
= end
.strftime('%Y%m%d%H%M')
507 # construct final, completed API URL
508 api_req_url
= station
['source'] + '&start=' + begin_date_str
+ '&end=' + end_date_str
511 req
= requests
.get(api_req_url
)
512 except requests
.exceptions
.ConnectionError
:
513 LOG
.error("Could not connect to '%s'", api_req_url
)
519 LOG
.error("Bad JSON in MesoWest response")
523 observations
= json
['STATION'][0]['OBSERVATIONS']
524 except KeyError as exc
:
525 LOG
.error("Unexpected JSON in MesoWest response: '%s'", exc
)
527 except IndexError as exc
:
528 LOG
.error("Unexpected JSON in MesoWest response: '%s'", exc
)
530 LOG
.error("Detailed MesoWest response: '%s'",
531 json
['SUMMARY']['RESPONSE_MESSAGE'])
535 except ValueError as exc
:
536 LOG
.error("Bad JSON in MesoWest response: '%s'", exc
)
539 pos
= len(observations
['date_time']) - 1
541 for element_cd
in station
['desired_data'].split(','):
542 # sort and isolate the most recent, see note above in NRCS for how and
545 # NOTE: Unlike in the NRCS case, the MesoWest API response contains all
546 # data (whereas with NRCS, we have to make a separate request for
547 # each element we want). This is nice for network efficiency but
548 # it means we have to handle this part differently for each.
550 # NOTE: Also unlike NRCS, MesoWest provides more granular data; NRCS
551 # provides hourly data, but MesoWest can often provide data every
552 # 10 minutes -- though this provides more opportunity for
555 # we may not have the data at all
556 key_name
= element_cd
+ '_set_1'
558 if key_name
in observations
:
559 if observations
[key_name
][pos
]:
560 remote_data
[element_cd
] = observations
[key_name
][pos
]
562 # mesowest by default provides wind_speed in m/s, but
563 # we specify 'english' units in the request; either way,
565 if element_cd
in ('wind_speed', 'wind_gust'):
566 remote_data
[element_cd
] = kn_to_mph(remote_data
[element_cd
])
568 # mesowest provides HS in mm, not cm; we want cm
569 if element_cd
== 'snow_depth' and station
['units'] == 'metric':
570 remote_data
[element_cd
] = mm_to_cm(remote_data
[element_cd
])
572 remote_data
[element_cd
] = None
574 remote_data
[element_cd
] = None
578 def switch_units_to_metric(data_map
, mapping
):
579 """replace units with metric counterparts"""
581 # NOTE: to update this, use the fmap<->final_data mapping laid out
582 # in setup_infoex_fields_mapping ()
583 data_map
[mapping
['tempMaxHourUnit']] = 'C'
584 data_map
[mapping
['tempMinHourUnit']] = 'C'
585 data_map
[mapping
['tempPresUnit']] = 'C'
586 data_map
[mapping
['precipitationGaugeUnit']] = 'mm'
587 data_map
[mapping
['hsUnit']] = 'cm'
588 data_map
[mapping
['windSpeedUnit']] = 'm/s'
589 data_map
[mapping
['windGustSpeedNumUnit']] = 'm/s'
590 data_map
[mapping
['dewPointUnit']] = 'C'
591 data_map
[mapping
['hn24AutoUnit']] = 'cm'
592 data_map
[mapping
['hstAutoUnit']] = 'cm'
596 def convert_nrcs_units_to_metric(element_cd
, value
):
597 """convert NRCS values from English to metric"""
598 if element_cd
== 'TOBS':
599 value
= f_to_c(value
)
600 elif element_cd
== 'SNWD':
601 value
= in_to_cm(value
)
602 elif element_cd
== 'PREC':
603 value
= in_to_mm(value
)
607 def write_local_csv(path_to_file
, data
):
608 """Write the specified CSV file to disk"""
609 with
open(path_to_file
, 'w') as file_object
:
610 # The requirement is that empty values are represented in the CSV
611 # file as "", csv.QUOTE_NONNUMERIC achieves that
612 LOG
.debug("writing CSV file '%s'", path_to_file
)
613 writer
= csv
.writer(file_object
, quoting
=csv
.QUOTE_NONNUMERIC
)
614 writer
.writerow(data
)
618 def upload_csv(path_to_file
, infoex_data
):
619 """Upload the specified CSV file to InfoEx FTP and remove the file"""
620 with
open(path_to_file
, 'rb') as file_object
:
621 LOG
.debug("uploading FTP file '%s'", infoex_data
['host'])
622 ftp
= FTP(infoex_data
['host'], infoex_data
['uuid'],
623 infoex_data
['api_key'])
624 ftp
.storlines('STOR ' + path_to_file
, file_object
)
627 os
.remove(path_to_file
)
629 # other miscellaneous routines
630 def setup_time_values(station
):
631 """establish time bounds of data request(s)"""
633 # default timezone to UTC (for MesoWest)
636 # but for NRCS, use the config-specified timezone
637 if station
['provider'] == 'nrcs':
640 # floor time to nearest hour
641 date_time
= datetime
.datetime
.now(tz
=tz
)
642 end_date
= date_time
- datetime
.timedelta(minutes
=date_time
.minute
% 60,
643 seconds
=date_time
.second
,
644 microseconds
=date_time
.microsecond
)
645 begin_date
= end_date
- datetime
.timedelta(hours
=station
['num_hrs_to_fetch'])
646 return (begin_date
, end_date
)
649 """convert Fahrenheit to Celsius"""
650 return (float(f
) - 32) * 5.0/9.0
652 def in_to_cm(inches
):
653 """convert inches to centimetrs"""
654 return float(inches
) * 2.54
656 def in_to_mm(inches
):
657 """convert inches to millimeters"""
658 return (float(inches
) * 2.54) * 10.0
661 """convert meters per second to miles per hour"""
665 """convert knots to miles per hour"""
669 """convert millimeters to centimetrs"""
672 if __name__
== "__main__":