Small tense fix
[infoex-autowx.git] / infoex-autowx.py
index 57fef27509441b13b3db5e74ff332418d3400ca9..e4befadd20e8206f1ceb8d216a8c989eac657545 100755 (executable)
@@ -42,8 +42,10 @@ import zeep
 import zeep.cache
 import zeep.transports
 
 import zeep.cache
 import zeep.transports
 
+__version__ = '2.0.0'
+
 log = logging.getLogger(__name__)
 log = logging.getLogger(__name__)
-log.setLevel(logging.DEBUG)
+log.setLevel(logging.NOTSET)
 
 try:
     from systemd.journal import JournalHandler
 
 try:
     from systemd.journal import JournalHandler
@@ -56,13 +58,18 @@ except:
     handler = logging.StreamHandler(sys.stdout)
     log.addHandler(handler)
 
     handler = logging.StreamHandler(sys.stdout)
     log.addHandler(handler)
 
-parser = OptionParser()
+parser = OptionParser(version=__version__)
 
 parser.add_option("--config",
     dest="config",
     metavar="FILE",
     help="location of config file")
 
 
 parser.add_option("--config",
     dest="config",
     metavar="FILE",
     help="location of config file")
 
+parser.add_option("--log-level",
+    dest="log_level",
+    default=None,
+    help="set the log level (debug, info, warning)")
+
 parser.add_option("--dry-run",
     action="store_true",
     dest="dry_run",
 parser.add_option("--dry-run",
     action="store_true",
     dest="dry_run",
@@ -74,11 +81,27 @@ parser.add_option("--dry-run",
 config = configparser.ConfigParser(allow_no_value=False)
 
 if not options.config:
 config = configparser.ConfigParser(allow_no_value=False)
 
 if not options.config:
-    print("Please specify a configuration file via --config.")
+    parser.print_help()
+    print("\nPlease specify a configuration file via --config.")
     sys.exit(1)
 
 config.read(options.config)
 
     sys.exit(1)
 
 config.read(options.config)
 
+# ugly, but passable
+if options.log_level in [None, 'debug', 'info', 'warning']:
+    if options.log_level == 'debug':
+        log.setLevel(logging.DEBUG)
+    elif options.log_level == 'info':
+        log.setLevel(logging.INFO)
+    elif options.log_level == 'warning':
+        log.setLevel(logging.WARNING)
+    else:
+        log.setLevel(logging.NOTSET)
+else:
+    parser.print_help()
+    print("\nPlease select an appropriate log level or remove the switch (--log-level).")
+    sys.exit(1)
+
 log.debug('STARTING UP')
 
 try:
 log.debug('STARTING UP')
 
 try:
@@ -112,6 +135,11 @@ try:
                     'PREC'  # PRECIPITATION ACCUMULATION (in)
                     ]
 
                     'PREC'  # PRECIPITATION ACCUMULATION (in)
                     ]
 
+        # XXX: For NRCS, we're manually overriding units for now! Once
+        #      unit conversion is supported for NRCS, REMOVE THIS!
+        if 'units' not in data:
+            data['units'] = 'imperial'
+
     if data['provider'] == 'mesowest':
         data['source'] = 'https://api.synopticdata.com/v2/stations/timeseries'
         data['station_id'] = config['station']['station_id']
     if data['provider'] == 'mesowest':
         data['source'] = 'https://api.synopticdata.com/v2/stations/timeseries'
         data['station_id'] = config['station']['station_id']
@@ -192,10 +220,35 @@ if data['provider'] == 'nrcs':
     iemap['PREC'] = 'precipitationGauge'
     iemap['TOBS'] = 'tempPres'
     iemap['SNWD'] = 'hS'
     iemap['PREC'] = 'precipitationGauge'
     iemap['TOBS'] = 'tempPres'
     iemap['SNWD'] = 'hS'
+    iemap['PRES'] = 'baro'
+    iemap['RHUM'] = 'rH'
+    iemap['WSPD'] = 'windSpeedNum'
+    iemap['WDIR'] = 'windDirectionNum'
+    # unsupported by NRCS:
+    # windGustSpeedNum
 elif data['provider'] == 'mesowest':
     iemap['precip_accum'] = 'precipitationGauge'
     iemap['air_temp'] = 'tempPres'
     iemap['snow_depth'] = 'hS'
 elif data['provider'] == 'mesowest':
     iemap['precip_accum'] = 'precipitationGauge'
     iemap['air_temp'] = 'tempPres'
     iemap['snow_depth'] = 'hS'
+    iemap['pressure'] = 'baro'
+    iemap['relative_humidity'] = 'rH'
+    iemap['wind_speed'] = 'windSpeedNum'
+    iemap['wind_direction'] = 'windDirectionNum'
+    iemap['wind_gust'] = 'windGustSpeedNum'
+
+# 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 data['units'] == 'metric':
+    final_data[fmap['tempPresUnit']] = 'C'
+    final_data[fmap['hsUnit']] = 'm'
+    final_data[fmap['windSpeedUnit']] = 'm/s'
+    final_data[fmap['windGustSpeedNumUnit']] = 'm/s'
 
 # floor time to nearest hour
 dt = datetime.datetime.now()
 
 # floor time to nearest hour
 dt = datetime.datetime.now()