Free a few more routines from main ()
authorAlexander Vasarab <alexander@wylark.com>
Thu, 2 Jul 2020 18:33:39 +0000 (11:33 -0700)
committerAlexander Vasarab <alexander@wylark.com>
Thu, 2 Jul 2020 18:33:39 +0000 (11:33 -0700)
infoex-autowx.py

index bb93eef0d78f3cce14feba38ea7e2f6ed077e736..48a9063ff4137ce25087dca97ab1a25aa328472c 100755 (executable)
@@ -193,25 +193,10 @@ def main():
     iemap = setup_infoex_counterparts_mapping(station['provider'])
 
     # override units if user selected metric
     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':
     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']),
 
     # get the data
     LOG.debug("Getting %s data from %s to %s" % (str(station['desired_data']),
@@ -432,6 +417,23 @@ def get_mesowest_data(begin, end, station):
 
     return remote_data
 
 
     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"""
 # CSV operations
 def write_local_csv(path_to_file, data):
     """Write the specified CSV file to disk"""
@@ -455,5 +457,16 @@ def upload_csv(path_to_file, infoex_data):
         file_object.close()
     os.remove(path_to_file)
 
         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())
 if __name__ == "__main__":
     sys.exit(main())