X-Git-Url: https://wylark.com/src/infoex-autowx.git/blobdiff_plain/e5c785f4ece402e97e1b3a2bad5dbde1732a52dc..87aca64501da63577349e9f810a3f1521094fbd0:/infoex-autowx.py?ds=inline diff --git a/infoex-autowx.py b/infoex-autowx.py index bb93eef..a16b7e6 100755 --- a/infoex-autowx.py +++ b/infoex-autowx.py @@ -39,7 +39,7 @@ import zeep import zeep.cache import zeep.transports -__version__ = '2.0.0' +__version__ = '2.0.1' LOG = logging.getLogger(__name__) LOG.setLevel(logging.NOTSET) @@ -88,16 +88,7 @@ def setup_config(config): if station['provider'] == 'nrcs': station['source'] = 'https://www.wcc.nrcs.usda.gov/awdbWebService/services?WSDL' station['station_id'] = config['station']['station_id'] - - try: - station['desired_data'] = config['station']['desired_data'].split(',') - except: - # desired_data malformed or missing, setting default - station['desired_data'] = [ - 'TOBS', # AIR TEMPERATURE OBSERVED (degF) - 'SNWD', # SNOW DEPTH (in) - 'PREC' # PRECIPITATION ACCUMULATION (in) - ] + station['desired_data'] = config['station']['desired_data'].split(',') # XXX: For NRCS, we're manually overriding units for now! Once # unit conversion is supported for NRCS, REMOVE THIS! @@ -108,12 +99,7 @@ def setup_config(config): station['source'] = 'https://api.synopticdata.com/v2/stations/timeseries' station['station_id'] = config['station']['station_id'] station['units'] = config['station']['units'] - - try: - station['desired_data'] = config['station']['desired_data'] - except: - # desired_data malformed or missing, setting default - station['desired_data'] = 'air_temp,snow_depth' + station['desired_data'] = config['station']['desired_data'] # construct full API URL (sans start/end time, added later) station['source'] = station['source'] + '?token=' + config['station']['token'] + '&within=60&units=' + station['units'] + '&stid=' + station['station_id'] + '&vars=' + station['desired_data'] @@ -193,25 +179,10 @@ def main(): iemap = setup_infoex_counterparts_mapping(station['provider']) # override units if user selected metric - # - # NOTE: to update this, use the fmap<->final_data mapping laid out above - # - # NOTE: this only 'works' with MesoWest for now, as the MesoWest API - # itself handles the unit conversion; in the future, we will also - # support NRCS unit conversion, but this must be done by this - # program. if station['units'] == 'metric': - final_data[fmap['tempPresUnit']] = 'C' - final_data[fmap['hsUnit']] = 'm' - final_data[fmap['windSpeedUnit']] = 'm/s' - final_data[fmap['windGustSpeedNumUnit']] = 'm/s' + final_data = switch_units_to_metric(final_data, fmap) - # floor time to nearest hour - dt = datetime.datetime.now() - end_date = dt - datetime.timedelta(minutes=dt.minute % 60, - seconds=dt.second, - microseconds=dt.microsecond) - begin_date = end_date - datetime.timedelta(hours=3) + (begin_date, end_date) = setup_time_values() # get the data LOG.debug("Getting %s data from %s to %s" % (str(station['desired_data']), @@ -432,6 +403,23 @@ def get_mesowest_data(begin, end, station): return remote_data +def switch_units_to_metric(data_map, mapping): + """replace units with metric counterparts""" + + # NOTE: to update this, use the fmap<->final_data mapping laid out + # in setup_infoex_fields_mapping () + # + # NOTE: this only 'works' with MesoWest for now, as the MesoWest API + # itself handles the unit conversion; in the future, we will also + # support NRCS unit conversion, but this must be done by this + # program. + data_map[mapping['tempPresUnit']] = 'C' + data_map[mapping['hsUnit']] = 'm' + data_map[mapping['windSpeedUnit']] = 'm/s' + data_map[mapping['windGustSpeedNumUnit']] = 'm/s' + + return data_map + # CSV operations def write_local_csv(path_to_file, data): """Write the specified CSV file to disk""" @@ -455,5 +443,16 @@ def upload_csv(path_to_file, infoex_data): file_object.close() os.remove(path_to_file) +# other miscellaneous routines +def setup_time_values(): + """establish time bounds of data request(s)""" + # floor time to nearest hour + dt = datetime.datetime.now() + end_date = dt - datetime.timedelta(minutes=dt.minute % 60, + seconds=dt.second, + microseconds=dt.microsecond) + begin_date = end_date - datetime.timedelta(hours=3) + return (begin_date, end_date) + if __name__ == "__main__": sys.exit(main())