Skip to content
Snippets Groups Projects
Controller.py 45 KiB
Newer Older
"""Controller class for geomag algorithms"""
from io import BytesIO
import sys
from typing import List, Optional, Tuple, Union

from obspy.core import Stream, UTCDateTime
from .algorithm import Algorithm, algorithms, AlgorithmException
from .DerivedTimeseriesFactory import DerivedTimeseriesFactory
Yash Shah's avatar
Yash Shah committed
from .PlotTimeseriesFactory import PlotTimeseriesFactory
from .StreamTimeseriesFactory import StreamTimeseriesFactory
from . import TimeseriesUtility, Util
# factory packages
Yash Shah's avatar
Yash Shah committed
from . import binlog
from . import edge
from . import iaga2002
Yash Shah's avatar
Yash Shah committed
from . import pcdcp
from . import imfv122
from . import imfv283
from . import temperature
from . import vbf
class Controller(object):
    """Controller for geomag algorithms.

    Parameters
    ----------
Hal Simpson's avatar
Hal Simpson committed
    inputFactory: TimeseriesFactory
        the factory that will read in timeseries data
Hal Simpson's avatar
Hal Simpson committed
    outputFactory: TimeseriesFactory
        the factory that will output the timeseries data
    algorithm: Algorithm
        the algorithm(s) that will procees the timeseries data

    Notes
    -----
Hal Simpson's avatar
Hal Simpson committed
    Has 2 basic modes.
    Run simply sends all the data in a stream to edge. If a startime/endtime is
        provided, it will send the data from the stream that is within that
        time span.
    Update will update any data that has changed between the source, and
Hal Simpson's avatar
Hal Simpson committed
        the target during a given timeframe. It will also attempt to
        recursively backup so it can update all missing data.
    def __init__(
        self,
        inputFactory,
        outputFactory,
        algorithm: Optional[Algorithm] = None,
        inputInterval: Optional[str] = None,
        outputInterval: Optional[str] = None,
    ):
Hal Simpson's avatar
Hal Simpson committed
        self._algorithm = algorithm
        self._inputFactory = inputFactory
        self._inputInterval = inputInterval
Hal Simpson's avatar
Hal Simpson committed
        self._outputFactory = outputFactory
        self._outputInterval = outputInterval
    def _get_input_timeseries(
        self,
        observatory,
        channels,
        starttime,
        endtime,
        algorithm=None,
        interval=None,
        """Get timeseries from the input factory for requested options.

        Parameters
        ----------
        observatory : array_like
            observatories to request.
        channels : array_like
            channels to request.
        starttime : obspy.core.UTCDateTime
            time of first sample to request.
        endtime : obspy.core.UTCDateTime
            time of last sample to request.
        renames : array_like
            list of channels to rename
            each list item should be array_like:
                the first element is the channel to rename,
                the last element is the new channel name

        Returns
        -------
        timeseries : obspy.core.Stream
        """
        algorithm = algorithm or self._algorithm
        for obs in observatory:
            # get input interval for observatory
            # do this per observatory in case an
            # algorithm needs different amounts of data
            input_start, input_end = algorithm.get_input_interval(
                start=starttime, end=endtime, observatory=obs, channels=channels
            )
            if input_start is None or input_end is None:
                continue
            timeseries += self._inputFactory.get_timeseries(
                observatory=obs,
                starttime=input_start,
                endtime=input_end,
                channels=channels,
                interval=interval or self._inputInterval,
        return timeseries

    def _rename_channels(self, timeseries, renames):
        """Rename trace channel names.

        Parameters
        ----------
        timeseries : obspy.core.Stream
            stream with channels to rename
        renames : array_like
            list of channels to rename
            each list item should be array_like:
                the first element is the channel to rename,
                the last element is the new channel name

        Returns
        -------
        timeseries : obspy.core.Stream
        """
        for r in renames:
            from_name, to_name = r[0], r[-1]
            for t in timeseries.select(channel=from_name):
                t.stats.channel = to_name
        return timeseries

    def _get_output_timeseries(
        self,
        observatory,
        channels,
        starttime,
        endtime,
        interval=None,
    ):
        """Get timeseries from the output factory for requested options.

        Parameters
        ----------
        observatory : array_like
            observatories to request.
        channels : array_like
            channels to request.
        starttime : obspy.core.UTCDateTime
            time of first sample to request.
        endtime : obspy.core.UTCDateTime
            time of last sample to request.

        Returns
        -------
        timeseries : obspy.core.Stream
        """
        timeseries = Stream()
        for obs in observatory:
            timeseries += self._outputFactory.get_timeseries(
                observatory=obs,
                starttime=starttime,
                endtime=endtime,
                channels=channels,
                interval=interval or self._outputInterval,
    def _run(self, options, input_timeseries=None):
        Parameters
        ----------
Hal Simpson's avatar
Hal Simpson committed
        options: dictionary
            The dictionary of all the command line arguments. Could in theory
            contain other options passed in by the controller.
        input_timeseries : obspy.core.Stream
            Used by run_as_update to save a double input read, since it has
            already read the input to confirm data can be produced.
        self.run(
            observatory=options.observatory,
            starttime=options.starttime,
            endtime=options.endtime,
            input_channels=options.inchannels,
            input_timeseries=input_timeseries,
            output_channels=options.outchannels,
            input_interval=options.input_interval or options.interval,
            output_interval=options.output_interval or options.interval,
            no_trim=options.no_trim,
            rename_input_channel=options.rename_input_channel,
            rename_output_channel=options.rename_output_channel,
            realtime=options.realtime,
        )

    def _run_as_update(self, options, update_count=0):
        """Updates data.
        Parameters
        ----------
        options: dictionary
            The dictionary of all the command line arguments. Could in theory
            contain other options passed in by the controller.

        Notes
        -----
        Finds gaps in the target data, and if there's new data in the input
            source, calls run with the start/end time of a given gap to fill
            in.
        It checks the start of the target data, and if it's missing, and
            there's new data available, it backs up the starttime/endtime,
            and recursively calls itself, to check the previous period, to see
            if new data is available there as well. Calls run for each new
            period, oldest to newest.
        """
        self.run_as_update(
            observatory=options.observatory,
            output_observatory=options.output_observatory,
            starttime=options.starttime,
            endtime=options.endtime,
            input_channels=options.inchannels,
            output_channels=options.outchannels,
            input_interval=options.input_interval or options.interval,
            output_interval=options.output_interval or options.interval,
            no_trim=options.no_trim,
            realtime=options.realtime,
            rename_input_channel=options.rename_input_channel,
            rename_output_channel=options.rename_output_channel,
            update_limit=options.update_limit,
        )

    def run(
        self,
        observatory: List[str],
        starttime: UTCDateTime,
        endtime: UTCDateTime,
        algorithm: Optional[Algorithm] = None,
        input_channels: Optional[List[str]] = None,
        input_timeseries: Optional[Stream] = None,
        output_channels: Optional[List[str]] = None,
        input_interval: Optional[str] = None,
        output_interval: Optional[str] = None,
        no_trim: bool = False,
        realtime: Union[bool, int] = False,
        rename_input_channel: Optional[List[List[str]]] = None,
        rename_output_channel: Optional[List[List[str]]] = None,
    ):
        """Run algorithm for a specific time range.

        Parameters
        ----------
        observatory: the observatory or list of observatories for processing
        starttime: time of first data
        endtime: time of last data
        input_channels: list of channels to read
        input_timeseries: used by run_as_update, which has already read input.
        output_channels: list of channels to write
        input_interval: input data interval
        output_interval: output data interval
        no_trim: whether to trim output to starttime/endtime interval
        realtime: number of seconds in realtime interval
        rename_input_channel: list of input channel renames
        rename_output_channel: list of output channel renames
        """
        # ensure realtime is a valid value:
        if realtime <= 0:
            realtime = False
        algorithm = algorithm or self._algorithm
        input_channels = input_channels or algorithm.get_input_channels()
        output_channels = output_channels or algorithm.get_output_channels()
        input_interval = input_interval or self._inputInterval
        output_interval = output_interval or self._outputInterval
        next_starttime = algorithm.get_next_starttime()
        starttime = next_starttime or starttime
        timeseries = input_timeseries or self._get_input_timeseries(
            observatory=observatory,
            starttime=starttime,
            endtime=endtime,
            channels=input_channels,
            interval=input_interval,
        if next_starttime and realtime:
            # when running a stateful algorithms with the realtime option
            # pad/trim timeseries to the interval:
            # [next_starttime, max(timeseries.endtime, now-options.realtime)]
            input_start, input_end = TimeseriesUtility.get_stream_start_end_times(
                timeseries, without_gaps=True
            )
            realtime_gap = endtime - realtime
            if input_end < realtime_gap:
                input_end = realtime_gap
            # pad to the start of the "realtime gap"
            TimeseriesUtility.pad_timeseries(timeseries, next_starttime, input_end)
        if rename_input_channel:
            timeseries = self._rename_channels(
                timeseries=timeseries, renames=rename_input_channel
        processed = algorithm.process(timeseries)
Cain, Payton David's avatar
Cain, Payton David committed
        # trim if --no-trim is not set
            processed.trim(starttime=starttime, endtime=endtime)
Loading
Loading full blame...