diff --git a/geomagio/Controller.py b/geomagio/Controller.py index d1dd90e9934905ab2f9b3a09f379c1c3d33cf3e5..2f7b3bf38eb84075b023fc830aa435e8afbc0330 100644 --- a/geomagio/Controller.py +++ b/geomagio/Controller.py @@ -1,6 +1,8 @@ """Controller class for geomag algorithms""" -import TimeseriesUtilities +import TimeseriesUtilities as TUtils +import numpy +import obspy class Controller(object): @@ -15,11 +17,14 @@ class Controller(object): algorithm: the algorithm(s) that will take procees the timeseries data """ - def __init__(self, inputFactory, outputFactory, algorithm, update=False): + def __init__(self, inputFactory, outputFactory, algorithm, update=False, + interval='minute', update_realtime=False): self._inputFactory = inputFactory self._algorithm = algorithm self._outputFactory = outputFactory self._update = update + self._interval = interval + self._update_realtime = update_realtime def run(self, starttime, endtime): """run an algorithm as setup up by the main script. @@ -39,24 +44,60 @@ class Controller(object): def run_as_update(self, starttime, endtime): input_channels = self._algorithm.get_input_channels() output_channels = self._algorithm.get_output_channels() + seconds_of_interval = TUtils.get_seconds_of_interval(self._interval) + backup_start = starttime + backup_end = endtime + backup = self._update_realtime - timeseries_in = self._inputFactory.get_timeseries(starttime, + # get new input_channel data, and last output data. + timeseries_in = self._inputFactory.get_timeseries(backup_start, endtime, channels=input_channels) - timeseries_out = self._inputFactory.get_timeseries(starttime, + timeseries_update = self._outputFactory.get_timeseries(backup_start, endtime, channels=output_channels) - - #TODO get input gaps - - output_gaps = TimeseriesUtilities.get_timeseries_gaps( - timeseries_out, output_channels, starttime, endtime) - - output_merged_gaps = TimeseriesUtilities.get_merged_gaps(output_gaps, - output_channels) - # TODO compare gaps. - # if there is new data, run algorithm over entire time. - # save any new data. - - #TODO iterate is starttime of gaps is starttime and new data found + # repeat until ouput is succesfully backfilled. + while backup: + first_in_exists = self._first_value_exists(timeseries_in, + input_channels) + first_out_exists = self._first_value_exists(timeseries_update, + output_channels) + if first_in_exists and not first_out_exists: + new_backup_end = backup_start - seconds_of_interval + backup_start = backup_start - (backup_end - backup_start) + backup_end = new_backup_end + timeseries_in += self._inputFactory.get_timeseries( + backup_start, backup_end, channels=input_channels) + timeseries_update += self._outputFactory.get_timeseries( + backup_start, backup_end, channels=output_channels) + timeseries_in.merge() + timeseries_update.merge() + else: + backup = False + print backup_start, backup_end + starttime = backup_start + + input_gaps = TUtils.get_timeseries_gaps( + timeseries_in, input_channels, starttime, endtime) + output_gaps = TUtils.get_timeseries_gaps( + timeseries_update, output_channels, starttime, endtime) + + input_merged_gaps = TUtils.get_merged_gaps( + input_gaps, input_channels) + output_merged_gaps = TUtils.get_merged_gaps( + output_gaps, output_channels) + + if TUtils.is_new_data(input_merged_gaps, output_merged_gaps): + pass + + # TODO call algorithm + + # TODO call output + + def _first_value_exists(self, timeseries, channels): + for channel in channels: + stream = timeseries.select(channel=channel) + if len(stream[0].data) and not numpy.isnan(stream[0].data[0]): + return True + return False def run_as_timeseries(self, starttime, endtime):