From cab1cbec9a9dcc72eb0df40cee89c5bd4221abd2 Mon Sep 17 00:00:00 2001
From: Hal Simpson <hasimpson@usgs.gov>
Date: Tue, 7 Jul 2015 13:04:37 -0600
Subject: [PATCH] Use starttime/endtime from the options parameter instead of
 being passed in as a seperate parameter.

---
 geomagio/Controller.py | 59 +++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 29 deletions(-)

diff --git a/geomagio/Controller.py b/geomagio/Controller.py
index 7a1ce948..ebeba5f4 100644
--- a/geomagio/Controller.py
+++ b/geomagio/Controller.py
@@ -2,6 +2,7 @@
 
 import TimeseriesUtilities as TUtils
 import TimeseriesFactoryException
+import copy
 
 
 class Controller(object):
@@ -32,21 +33,17 @@ class Controller(object):
         self._algorithm = algorithm
         self._outputFactory = outputFactory
 
-    def run(self, starttime, endtime, options):
+    def run(self, options):
         """run controller
         Parameters
         ----------
-        starttime: obspy.core.UTCDateTime
-            time of first sample. None if starttime should come from dataset
-        endtime: obspy.core.UTCDateTime
-            endtime of last sampel.  None if endtime should come from dataset
         options: dictionary
             The dictionary of all the command line arguments. Could in theory
             contain other options passed in by the controller.
         """
         input_channels = self._algorithm.get_input_channels()
         algorithm_start, algorithm_end = self._algorithm.get_input_interval(
-                starttime, endtime)
+                options.starttime, options.endtime)
 
         timeseries = self._inputFactory.get_timeseries(algorithm_start,
             algorithm_end, channels=input_channels)
@@ -58,17 +55,13 @@ class Controller(object):
                 options.outchannels)
 
         self._outputFactory.put_timeseries(timeseries=processed,
-                starttime=starttime, endtime=endtime,
+                starttime=options.starttime, endtime=options.endtime,
                 channels=output_channels)
 
-    def run_as_update(self, starttime, endtime, options):
+    def run_as_update(self, options):
         """Updates data.
         Parameters
         ----------
-        starttime: obspy.core.UTCDateTime
-            time of first sample. None if starttime should come from dataset
-        endtime: obspy.core.UTCDateTime
-            endtime of last sampel.  None if endtime should come from dataset
         options: dictionary
             The dictionary of all the command line arguments. Could in theory
             contain other options passed in by the controller.
@@ -85,36 +78,44 @@ class Controller(object):
             period, oldest to newest.
         """
         input_channels = self._algorithm.get_input_channels()
-        output_channels = self._algorithm._get_output_channels()
+        output_channels = self._algorithm.get_output_channels()
 
         output_channels = self._get_output_channels(output_channels,
                 options.outchannels)
 
-        timeseries_source = self._inputFactory.get_timeseries(starttime,
-                endtime, channels=input_channels)
-        timeseries_target = self._outputFactory.get_timeseries(starttime,
-                endtime, channels=output_channels)
+        timeseries_source = self._inputFactory.get_timeseries(
+                options.starttime, options.endtime, channels=input_channels)
+        timeseries_target = self._outputFactory.get_timeseries(
+                options.starttime, options.endtime, channels=output_channels)
 
         source_gaps = TUtils.get_timeseries_gaps(
-            timeseries_source, input_channels, starttime, endtime)
+                timeseries_source, input_channels, options.starttime,
+                options.endtime)
         target_gaps = TUtils.get_timeseries_gaps(
-            timeseries_target, output_channels, starttime, endtime)
-        source_gaps = TUtils.get_merged_gaps(
-                source_gaps, input_channels)
-        target_gaps = TUtils.get_merged_gaps(
-                target_gaps, output_channels)
+                timeseries_target, output_channels, options.starttime,
+                options.endtime)
+        source_gaps = TUtils.get_merged_gaps(source_gaps, input_channels)
+        target_gaps = TUtils.get_merged_gaps(target_gaps, output_channels)
         del timeseries_source
         del timeseries_target
+
         if ((not len(source_gaps) or
-                len(source_gaps) and source_gaps[0][0] != starttime) and
-                len(target_gaps) and target_gaps[0][0] == starttime):
-            self.run_as_update((starttime - (endtime - starttime)),
-                (starttime - TUtils.get_seconds_of_interval(options.interval)),
-                options)
+                len(source_gaps) and source_gaps[0][0] != options.starttime)
+                and
+                len(target_gaps) and target_gaps[0][0] == options.starttime):
+            new_options = copy.deepcopy(options)
+            new_options.starttime = (options.starttime -
+                    (options.endtime - options.starttime))
+            new_options.endtime = (options.starttime -
+                    TUtils.get_seconds_of_interval(options.interval))
+            self.run_as_update(new_options)
         for target_gap in target_gaps:
             if not TUtils.gap_is_new_data(source_gaps, target_gap):
                 continue
-            self.run(target_gap[0], target_gap[1], options)
+            new_options = copy.deepcopy(options)
+            new_options.starttime = target_gap[0]
+            new_options.endtime = target_gap[1]
+            self.run(new_options)
 
     def _get_output_channels(self, algorithm_channels, commandline_channels):
         """get output channels
-- 
GitLab