Skip to content
Snippets Groups Projects
Commit c6af413e authored by Hal Simpson's avatar Hal Simpson
Browse files

refactored argument parser to use mutually exclusive groups for input and...

refactored argument parser to use mutually exclusive groups for input and output. Also put algorithms in their own group.
parent d3698a09
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,7 @@ import sys ...@@ -5,7 +5,7 @@ import sys
# ensure geomag is on the path before importing # ensure geomag is on the path before importing
try: try:
import geomagio # noqa import geomagio # noqa (tells linter to ignor this line.)
except: except:
from os import path from os import path
script_dir = path.dirname(path.abspath(__file__)) script_dir = path.dirname(path.abspath(__file__))
...@@ -16,7 +16,6 @@ import geomagio.edge as edge ...@@ -16,7 +16,6 @@ import geomagio.edge as edge
from geomagio.Algorithm import Algorithm from geomagio.Algorithm import Algorithm
from geomagio.XYZAlgorithm import XYZAlgorithm from geomagio.XYZAlgorithm import XYZAlgorithm
from geomagio.Controller import Controller from geomagio.Controller import Controller
from geomagio.iaga2002.IAGA2002Factory import IAGA_FILE_PATTERN
from obspy.core.utcdatetime import UTCDateTime from obspy.core.utcdatetime import UTCDateTime
...@@ -35,66 +34,71 @@ def main(): ...@@ -35,66 +34,71 @@ def main():
args = parse_args() args = parse_args()
if args.input == 'iaga': # Input Factory
if args.input_iaga_magweb: if args.input_edge is not None:
inputfactory = iaga2002.MagWebFactory( if len(args.input_edge) != 2:
observatory=args.observatory, print >> sys.stderr, \
type=args.type, '--input-edge requires 2 parameters Host and Port'
interval=args.interval) inputfactory = edge.EdgeFactory(
elif args.input_iaga_url is not None: host=args.input_edge[0],
inputfactory = iaga2002.IAGA2002Factory( port=int(args.input_edge[1]),
urlTemplate=_get_iaga_input_url(args), observatory=args.observatory,
observatory=args.observatory, type=args.type,
type=args.type, interval=args.interval)
interval=args.interval) elif args.input_iaga_magweb:
elif args.input_iaga_file is not None or args.input_iaga_stdin: inputfactory = iaga2002.MagWebFactory(
if args.input_iaga_file is not None: observatory=args.observatory,
iagaFile = open(args.input_iaga_file, 'r').read() type=args.type,
else: interval=args.interval)
print >> sys.stderr, "Iaga Input waiting for data from stdin" elif args.input_iaga_url is not None:
iagaFile = sys.stdin.read() inputfactory = iaga2002.IAGA2002Factory(
inputfactory = iaga2002.StreamIAGA2002Factory( urlTemplate=args.input_iaga_url,
stream=iagaFile, observatory=args.observatory,
type=args.type,
interval=args.interval)
elif args.input_iaga_file is not None:
inputfactory = iaga2002.StreamIAGA2002Factory(
stream=open(args.input_iaga_file, 'r').read(),
observatory=args.observatory, observatory=args.observatory,
type=args.type, type=args.type,
interval=args.interval) interval=args.interval)
else: elif args.input_iaga_stdin:
print >> sys.stderr, "Iaga Input was missing needed arguments" inputfactory = iaga2002.StreamIAGA2002Factory(
stream=sys.stdin.read(),
elif args.input == 'edge':
inputfactory = edge.EdgeFactory(
host=args.input_edge_host,
port=args.input_edge_port,
observatory=args.observatory, observatory=args.observatory,
type=args.type, type=args.type,
interval=args.interval) interval=args.interval)
else:
print >> sys.stderr, 'Missing required input directive.'
if args.output == 'iaga': # Output Factory
if args.output_iaga_url is not None: if args.output_iaga_url is not None:
outputfactory = iaga2002.IAGA2002Factory( outputfactory = iaga2002.IAGA2002Factory(
urlTemplate=_get_iaga_output_url(args), urlTemplate=args.output_iaga_url,
observatory=args.observatory, observatory=args.observatory,
type=args.type, type=args.type,
interval=args.interval) interval=args.interval)
elif args.output_iaga_file is not None: elif args.output_iaga_file is not None:
iagaFile = open(args.output_iaga_file, 'w') outputfactory = iaga2002.StreamIAGA2002Factory(
outputfactory = iaga2002.StreamIAGA2002Factory( stream=open(args.output_iaga_file, 'w'),
stream=iagaFile, observatory=args.observatory,
type=args.type,
interval=args.interval)
elif args.output_iaga_stdout:
outputfactory = iaga2002.StreamIAGA2002Factory(
stream=sys.stdout,
observatory=args.observatory, observatory=args.observatory,
type=args.type, type=args.type,
interval=args.interval) interval=args.interval)
elif args.output_iaga_stdout: else:
iagaFile = sys.stdout print >> sys.stderr, "Missing required output directive"
outputfactory = iaga2002.StreamIAGA2002Factory(
stream=iagaFile, if args.xyz is not None:
observatory=args.observatory, if len(args.xyz) != 2:
type=args.type, print >> sys.stderr, \
interval=args.interval) '--xyz requires 2 parameters Informat and Outformat'
else: algorithm = XYZAlgorithm(informat=args.xyz[0],
print >> sys.stderr, "Iaga Output was missing needed arguments" outformat=args.xyz[1])
if args.algorithm == 'xyz':
algorithm = XYZAlgorithm(args.xyz_informat, args.xyz_outformat)
else: else:
algorithm = Algorithm(inchannels=args.inchannels, algorithm = Algorithm(inchannels=args.inchannels,
outchannels=args.outchannels) outchannels=args.outchannels)
...@@ -118,12 +122,6 @@ def parse_args(): ...@@ -118,12 +122,6 @@ def parse_args():
description='Use @ to read commands from a file.', description='Use @ to read commands from a file.',
fromfile_prefix_chars='@',) fromfile_prefix_chars='@',)
parser.add_argument('--input', choices=['iaga', 'edge'],
help='Input type.', required=True)
parser.add_argument('--output', choices=['iaga'],
help='Input type.', required=True)
parser.add_argument('--starttime', default=UTCDateTime(), parser.add_argument('--starttime', default=UTCDateTime(),
help='UTC date YYYY-MM-DD HH:MM:SS') help='UTC date YYYY-MM-DD HH:MM:SS')
parser.add_argument('--endtime', default=UTCDateTime(), parser.add_argument('--endtime', default=UTCDateTime(),
...@@ -140,98 +138,42 @@ def parse_args(): ...@@ -140,98 +138,42 @@ def parse_args():
parser.add_argument('--interval', default='minute', parser.add_argument('--interval', default='minute',
choices=['minute', 'second']) choices=['minute', 'second'])
parser.add_argument('--algorithm', choices=['xyz', ]) # Input group
input_group = parser.add_mutually_exclusive_group(required=True)
# iaga specific args. input_group.add_argument('--input-edge', nargs=2,
parser.add_argument('--input-iaga-file', metavar=('Host', 'Port'),
help='Iaga2002 filename') help='Requires Host IP # and Port #')
parser.add_argument('--input-iaga-magweb', input_group.add_argument('--input-iaga-url',
action="store_true", default=False, help='Example: file://./%%(obs)s%%(ymd)s%%(t)s%%(i)s.%%(i)s')
input_group.add_argument('--input-iaga-file',
help='Reads from the specified file.')
input_group.add_argument('--input-iaga-stdin',
action='store_true', default=False,
help='Pass in an iaga file using redirection from stdin.')
input_group.add_argument('--input-iaga-magweb',
action='store_true', default=False,
help='Indicates iaga2002 files will be read from \ help='Indicates iaga2002 files will be read from \
http://magweb.cr.usgs.gov/data/magnetometer/') http://magweb.cr.usgs.gov/data/magnetometer/')
parser.add_argument('--input-iaga-stdin',
action="store_true", default=False,
help='Indicates file will be redirected from stdin')
parser.add_argument('--input-iaga-url',
help='Url or Directory where Iaga2002 files can be read from')
parser.add_argument('--input-iaga-urltemplate',
help='Template for directory matching')
parser.add_argument('--input-iaga-filetemplate',
help='Template for iaga filenames')
parser.add_argument('--output-iaga-url',
help='Url or Directory where IAGA2002 files should be written to')
parser.add_argument('--output-iaga-stdout',
action="store_true", default=False,
help='Indicates file will be directed to stdout')
parser.add_argument('--output-iaga-urltemplate',
help='Template for subdirectories')
parser.add_argument('--output-iaga-filetemplate',
help='Template for iaga filenames')
parser.add_argument('--output-iaga-file',
help='Output file name for single iaga file.')
# edge specific args
parser.add_argument('--input-edge-host',
help='ip address of the edge input server')
parser.add_argument('--input-edge-port', type=int,
help='port number of the edge input server')
# XYZ Algorithm specific args
parser.add_argument('--xyz-informat',
choices=['geo', 'mag', 'obs', 'obsd'])
parser.add_argument('--xyz-outformat',
choices=['geo', 'mag', 'obs', 'obsd'])
return parser.parse_args()
def _get_iaga_input_url(args):
"""get iaga input url
Parameters # Output group
---------- output_group = parser.add_mutually_exclusive_group(required=True)
args: argparse.Namespace output_group.add_argument('--output-iaga-url',
all the arguments passed to geomag.py help='Example: file://./%%(obs)s%%(ymd)s%%(t)s%%(i)s.%%(i)s')
input_iaga_url: string output_group.add_argument('--output-iaga-file',
the start of the url to read from help='Write to a single iaga file')
input_iaga_urltemplate: string output_group.add_argument('--output-iaga-stdout',
the template for the subdirectories to be read from action='store_true', default=False,
input_iaga_filetemplate:string help='Write to stdout')
the template for the file
# Algorithms group
algorithm_group = parser.add_mutually_exclusive_group()
algorithm_group.add_argument('--xyz', nargs=2,
choices=['geo', 'mag', 'obs', 'obsd'],
help='Enter the geomagnetic orientation(s) you want to read from' +
' and to respectfully.')
Returns return parser.parse_args()
-------
complete template for the input url
"""
url = args.input_iaga_url or 'file://./'
urltemplate = args.input_iaga_urltemplate or ''
filetemplate = args.input_iaga_filetemplate or IAGA_FILE_PATTERN
return url + urltemplate + filetemplate
def _get_iaga_output_url(args):
"""get iaga input url
Parameters
----------
args: argparse.Namespace
all the arguments passed to geomag.py
output_iaga_url: string
the start of the url to read from
output_iaga_urltemplate: string
the template for the subdirectories to be read from
output_iaga_filetemplate:string
the template for the file
Returns
-------
complete template for the output url
"""
url = args.output_iaga_url or 'file://./'
urltemplate = args.output_iaga_urltemplate or ''
filetemplate = args.output_iaga_filetemplate or IAGA_FILE_PATTERN
return url + urltemplate + filetemplate
if __name__ == '__main__': if __name__ == '__main__':
main() main()
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