Add num_hrs_to_fetch var
[infoex-autowx.git] / infoex-autowx.py
index c3144a4dfa5922b9767241b81eb3c4676fffdbb2..2b7880eaf9865acad39b83c6a56d8b5e1a55f92c 100755 (executable)
@@ -43,7 +43,7 @@ import zeep
 import zeep.cache
 import zeep.transports
 
-__version__ = '3.2.3'
+__version__ = '3.2.4'
 
 LOG = logging.getLogger(__name__)
 LOG.setLevel(logging.NOTSET)
@@ -128,9 +128,45 @@ def setup_config(config):
             LOG.critical("%s is not a valid timezone", tz)
             sys.exit(1)
 
+        # By default, fetch three hours of data
+        #
+        # If user wants hn24 or wind averaging, then
+        # we need more.
+        station['num_hrs_to_fetch'] = 3
+
+        # HN24
+        if 'hn24' in config['station']:
+            if config['station']['hn24'] not in ['true', 'false']:
+                raise ValueError("hn24 must be either 'true' or 'false'")
+
+            if config['station']['hn24'] == "true":
+                station['hn24'] = True
+                station['num_hrs_to_fetch'] = 24
+            else:
+                station['hn24'] = False
+        else:
+            # default to False
+            station['hn24'] = False
+
+        # Wind mode
+        if 'wind_mode' in config['station']:
+            if config['station']['wind_mode'] not in ['normal', 'average']:
+                raise ValueError("wind_mode must be either 'normal' or 'average'")
+
+            station['wind_mode'] = config['station']['wind_mode']
+
+            if station['wind_mode'] == "average":
+                station['num_hrs_to_fetch'] = 24
+        else:
+            # default to False
+            station['wind_mode'] = "normal"
+
     except KeyError as err:
         LOG.critical("%s not defined in configuration file", err)
         sys.exit(1)
+    except ValueError as err:
+        LOG.critical("%s", err)
+        sys.exit(1)
 
     # all sections/values present in config file, final sanity check
     try:
@@ -606,7 +642,7 @@ def setup_time_values(station):
     end_date = date_time - datetime.timedelta(minutes=date_time.minute % 60,
                                               seconds=date_time.second,
                                               microseconds=date_time.microsecond)
-    begin_date = end_date - datetime.timedelta(hours=3)
+    begin_date = end_date - datetime.timedelta(hours=station['num_hrs_to_fetch'])
     return (begin_date, end_date)
 
 def f_to_c(f):