Skip to content
Snippets Groups Projects
Commit 92cea08e authored by Jeremy M Fee's avatar Jeremy M Fee
Browse files

Move algorithms to separate package, abstract algorithm arguments

parent ddbcbc71
No related branches found
No related tags found
No related merge requests found
Showing with 149 additions and 60 deletions
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
import argparse import argparse
import sys import sys
from obspy.core import UTCDateTime from obspy.core import UTCDateTime
from Algorithm import Algorithm from algorithm import algorithms
import TimeseriesUtility import TimeseriesUtility
from TimeseriesFactoryException import TimeseriesFactoryException from TimeseriesFactoryException import TimeseriesFactoryException
from Util import ObjectView from Util import ObjectView
...@@ -14,9 +14,6 @@ import iaga2002 ...@@ -14,9 +14,6 @@ import iaga2002
import pcdcp import pcdcp
import imfv283 import imfv283
from DeltaFAlgorithm import DeltaFAlgorithm
from XYZAlgorithm import XYZAlgorithm
class Controller(object): class Controller(object):
"""Controller for geomag algorithms. """Controller for geomag algorithms.
...@@ -301,17 +298,8 @@ def main(args): ...@@ -301,17 +298,8 @@ def main(args):
else: else:
print >> sys.stderr, "Missing required output directive" print >> sys.stderr, "Missing required output directive"
if args.xyz is not None: algorithm = algorithms[args.algorithm]()
algorithm = XYZAlgorithm(informat=args.xyz[0], algorithm.configure(args)
outformat=args.xyz[1])
elif args.deltaf is not None:
algorithm = DeltaFAlgorithm(informat=args.deltaf)
else:
# TODO get smarter on inchannels/outchannels since input doesn't always
# need to use the --inchannels argument, but might (as in iaga2002),
# get it from the file.
algorithm = Algorithm(inchannels=args.inchannels,
outchannels=args.outchannels or args.inchannels)
# TODO check for unused arguments. # TODO check for unused arguments.
...@@ -481,14 +469,11 @@ def parse_args(args): ...@@ -481,14 +469,11 @@ def parse_args(args):
help='Edge IP #. See --output-edge-* for other optional arguments') help='Edge IP #. See --output-edge-* for other optional arguments')
# Algorithms group # Algorithms group
algorithm_group = parser.add_mutually_exclusive_group() parser.add_argument('--algorithm',
algorithm_group.add_argument('--xyz', choices=[k for k in algorithms],
nargs=2, default='default')
choices=['geo', 'mag', 'obs', 'obsd'],
help='Enter the geomagnetic orientation(s) you want to read from' + for k in algorithms:
' and to respectfully.') algorithms[k].add_arguments(parser)
algorithm_group.add_argument('--deltaf',
choices=['geo', 'obs', 'obsd'],
help='Enter the geomagnetic orientation you want to read from')
return parser.parse_args(args) return parser.parse_args(args)
...@@ -4,19 +4,14 @@ Geomag Algorithm Module ...@@ -4,19 +4,14 @@ Geomag Algorithm Module
import ChannelConverter import ChannelConverter
import StreamConverter import StreamConverter
from Algorithm import Algorithm
from AlgorithmException import AlgorithmException
from Controller import Controller from Controller import Controller
from ObservatoryMetadata import ObservatoryMetadata from ObservatoryMetadata import ObservatoryMetadata
from TimeseriesFactory import TimeseriesFactory from TimeseriesFactory import TimeseriesFactory
from TimeseriesFactoryException import TimeseriesFactoryException from TimeseriesFactoryException import TimeseriesFactoryException
import TimeseriesUtility import TimeseriesUtility
import Util import Util
from XYZAlgorithm import XYZAlgorithm
__all__ = [ __all__ = [
'Algorithm',
'AlgorithmException',
'ChannelConverter', 'ChannelConverter',
'Controller', 'Controller',
'DeltaFAlgorithm', 'DeltaFAlgorithm',
......
"""Algorithm Interface.""" """Algorithm Interface."""
import TimeseriesUtility from .. import TimeseriesUtility
class Algorithm(object): class Algorithm(object):
...@@ -91,3 +91,25 @@ class Algorithm(object): ...@@ -91,3 +91,25 @@ class Algorithm(object):
endtime < input_gap[2]): endtime < input_gap[2]):
return False return False
return True return True
@classmethod
def add_arguments(cls, parser):
"""Add command line arguments to argparse parser.
Parameters
----------
parser: ArgumentParser
command line argument parser
"""
pass
def configure(self, arguments):
"""Configure algorithm using comand line arguments.
Parameters
----------
arguments: Namespace
parsed command line arguments
"""
self._inchannels = arguments.inchannels
self._outchannels = arguments.outchannels or arguments.inchannels
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
from Algorithm import Algorithm from Algorithm import Algorithm
from AlgorithmException import AlgorithmException from AlgorithmException import AlgorithmException
import StreamConverter as StreamConverter from .. import StreamConverter
# List of channels by geomagnetic observatory orientation. # List of channels by geomagnetic observatory orientation.
# geo represents a geographic north/south orientation # geo represents a geographic north/south orientation
...@@ -27,10 +27,10 @@ class DeltaFAlgorithm(Algorithm): ...@@ -27,10 +27,10 @@ class DeltaFAlgorithm(Algorithm):
will be converting from. will be converting from.
""" """
def __init__(self, informat=None): def __init__(self, informat='obs'):
Algorithm.__init__(self, inchannels=CHANNELS[informat], Algorithm.__init__(self, inchannels=CHANNELS[informat],
outchannels=['G']) outchannels=['G'])
self.informat = informat self._informat = informat
def check_stream(self, timeseries): def check_stream(self, timeseries):
"""checks a stream to make certain all the required channels """checks a stream to make certain all the required channels
...@@ -61,10 +61,35 @@ class DeltaFAlgorithm(Algorithm): ...@@ -61,10 +61,35 @@ class DeltaFAlgorithm(Algorithm):
""" """
self.check_stream(timeseries) self.check_stream(timeseries)
out_stream = None out_stream = None
informat = self._informat
if self.informat == 'geo': if informat == 'geo':
out_stream = StreamConverter.get_deltaf_from_geo(timeseries) out_stream = StreamConverter.get_deltaf_from_geo(timeseries)
elif self.informat == 'obs' or self.informat == 'obsd': elif informat == 'obs' or informat == 'obsd':
out_stream = StreamConverter.get_deltaf_from_obs(timeseries) out_stream = StreamConverter.get_deltaf_from_obs(timeseries)
return out_stream return out_stream
@classmethod
def add_arguments(cls, parser):
"""Add command line arguments to argparse parser.
Parameters
----------
parser: ArgumentParser
command line argument parser
"""
parser.add_argument('--deltaf-from',
choices=['geo', 'obs', 'obsd'],
default='obs',
help='Geomagnetic orientation to read from')
def configure(self, arguments):
"""Configure algorithm using comand line arguments.
Parameters
----------
arguments: Namespace
parsed command line arguments
"""
self._informat = arguments.deltaf_from
self._inchannels = CHANNELS[self._informat]
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
from Algorithm import Algorithm from Algorithm import Algorithm
from AlgorithmException import AlgorithmException from AlgorithmException import AlgorithmException
import StreamConverter as StreamConverter from .. import StreamConverter
# List of channels by geomagnetic observatory orientation. # List of channels by geomagnetic observatory orientation.
# geo represents a geographic north/south orientation # geo represents a geographic north/south orientation
...@@ -33,11 +33,11 @@ class XYZAlgorithm(Algorithm): ...@@ -33,11 +33,11 @@ class XYZAlgorithm(Algorithm):
be converting to. be converting to.
""" """
def __init__(self, informat=None, outformat=None): def __init__(self, informat='obs', outformat='geo'):
Algorithm.__init__(self, inchannels=CHANNELS[informat], Algorithm.__init__(self, inchannels=CHANNELS[informat],
outchannels=CHANNELS[outformat]) outchannels=CHANNELS[outformat])
self.informat = informat self._informat = informat
self.outformat = outformat self._outformat = outformat
def check_stream(self, timeseries): def check_stream(self, timeseries):
"""checks an stream to make certain all the required channels """checks an stream to make certain all the required channels
...@@ -69,36 +69,69 @@ class XYZAlgorithm(Algorithm): ...@@ -69,36 +69,69 @@ class XYZAlgorithm(Algorithm):
""" """
self.check_stream(timeseries) self.check_stream(timeseries)
out_stream = None out_stream = None
if self.outformat == 'geo': informat = self._informat
if self.informat == 'geo': outformat = self._outformat
if outformat == 'geo':
if informat == 'geo':
out_stream = timeseries out_stream = timeseries
elif self.informat == 'mag': elif informat == 'mag':
out_stream = StreamConverter.get_geo_from_mag(timeseries) out_stream = StreamConverter.get_geo_from_mag(timeseries)
elif self.informat == 'obs' or self.informat == 'obsd': elif informat == 'obs' or informat == 'obsd':
out_stream = StreamConverter.get_geo_from_obs(timeseries) out_stream = StreamConverter.get_geo_from_obs(timeseries)
elif self.outformat == 'mag': elif outformat == 'mag':
if self.informat == 'geo': if informat == 'geo':
out_stream = StreamConverter.get_mag_from_geo(timeseries) out_stream = StreamConverter.get_mag_from_geo(timeseries)
elif self.informat == 'mag': elif informat == 'mag':
out_stream = timeseries out_stream = timeseries
elif self.informat == 'obs' or self.informat == 'obsd': elif informat == 'obs' or informat == 'obsd':
out_stream = StreamConverter.get_mag_from_obs(timeseries) out_stream = StreamConverter.get_mag_from_obs(timeseries)
elif self.outformat == 'obs': elif outformat == 'obs':
if self.informat == 'geo': if informat == 'geo':
out_stream = StreamConverter.get_obs_from_geo(timeseries) out_stream = StreamConverter.get_obs_from_geo(timeseries)
elif self.informat == 'mag': elif informat == 'mag':
out_stream = StreamConverter.get_obs_from_mag(timeseries) out_stream = StreamConverter.get_obs_from_mag(timeseries)
elif self.informat == 'obs' or self.informat == 'obsd': elif informat == 'obs' or informat == 'obsd':
out_stream = StreamConverter.get_obs_from_obs(timeseries, out_stream = StreamConverter.get_obs_from_obs(timeseries,
include_e=True) include_e=True)
elif self.outformat == 'obsd': elif outformat == 'obsd':
if self.informat == 'geo': if informat == 'geo':
out_stream = StreamConverter.get_obs_from_geo(timeseries, out_stream = StreamConverter.get_obs_from_geo(timeseries,
include_d=True) include_d=True)
elif self.informat == 'mag': elif informat == 'mag':
out_stream = StreamConverter.get_obs_from_mag(timeseries, out_stream = StreamConverter.get_obs_from_mag(timeseries,
include_d=True) include_d=True)
elif self.informat == 'obs' or self.informat == 'obsd': elif informat == 'obs' or informat == 'obsd':
out_stream = StreamConverter.get_obs_from_obs(timeseries, out_stream = StreamConverter.get_obs_from_obs(timeseries,
include_d=True) include_d=True)
return out_stream return out_stream
@classmethod
def add_arguments(cls, parser):
"""Add command line arguments to argparse parser.
Parameters
----------
parser: ArgumentParser
command line argument parser
"""
parser.add_argument('--xyz-from',
choices=['geo', 'mag', 'obs', 'obsd'],
default='obs',
help='Geomagnetic orientation to read from')
parser.add_argument('--xyz-to',
choices=['geo', 'mag', 'obs', 'obsd'],
default='geo',
help='Geomagnetic orientation to convert to')
def configure(self, arguments):
"""Configure algorithm using comand line arguments.
Parameters
----------
arguments: Namespace
parsed command line arguments
"""
self._informat = arguments.xyz_from
self._outformat = arguments.xyz_to
self._inchannels = CHANNELS[self._informat]
self._outchannels = CHANNELS[self._outformat]
"""
Geomag Algorithms module
"""
# base classes
from Algorithm import Algorithm
from AlgorithmException import AlgorithmException
# algorithms
from DeltaFAlgorithm import DeltaFAlgorithm
from XYZAlgorithm import XYZAlgorithm
# algorithms is used by Controller to auto generate arguments
algorithms = {
'identity': Algorithm,
'deltaf': DeltaFAlgorithm,
'xyz': XYZAlgorithm
}
__all__ = [
# base classes
'Algorithm',
'AlgorithmException',
# algorithms
'DeltaFAlgorithm',
'XYZAlgorithm'
]
#! /usr/bin/env python #! /usr/bin/env python
from geomagio import Algorithm, Controller, TimeseriesFactory from geomagio import Controller, TimeseriesFactory
from geomagio.algorithm import Algorithm
from nose.tools import assert_is_instance from nose.tools import assert_is_instance
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
from obspy.core.stream import Stream from obspy.core.stream import Stream
from nose.tools import assert_equals from nose.tools import assert_equals
from nose.tools import assert_is_instance from nose.tools import assert_is_instance
from geomagio import Algorithm from geomagio.algorithm import Algorithm
def test_algorithm_process(): def test_algorithm_process():
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
from obspy.core.stream import Stream from obspy.core.stream import Stream
from nose.tools import assert_equals from nose.tools import assert_equals
from nose.tools import assert_is from nose.tools import assert_is
from geomagio import XYZAlgorithm from geomagio.algorithm import XYZAlgorithm
from StreamConverter_test import __create_trace from ..StreamConverter_test import __create_trace
def test_xyzalgorithm_process(): def test_xyzalgorithm_process():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment