diff --git a/geomagio/Controller.py b/geomagio/Controller.py index 8dcb3eaea1d13f13783e73729c328fd47682df2d..a698279eab185901b23ae8f823d33e0c112cf925 100644 --- a/geomagio/Controller.py +++ b/geomagio/Controller.py @@ -566,9 +566,12 @@ def parse_args(args): metavar=('FROM', 'TO'), nargs=2) parser.add_argument('--locationcode', - choices=['R0', 'R1', 'RM', 'Q0', 'D0', 'C0']) + help='EDGE location code, e.g. "R0", "R1"', + type=edge.LocationCode) parser.add_argument('--outlocationcode', - choices=['R0', 'R1', 'RM', 'Q0', 'D0', 'C0']) + help='EDGE output location code' + ' (if different from --locationcode)', + type=edge.LocationCode) parser.add_argument('--interval', default='minute', choices=['hourly', 'minute', 'second']) diff --git a/geomagio/edge/LocationCode.py b/geomagio/edge/LocationCode.py new file mode 100644 index 0000000000000000000000000000000000000000..aa5168eb80567c273618894a172d86923f768620 --- /dev/null +++ b/geomagio/edge/LocationCode.py @@ -0,0 +1,32 @@ +"""EDGE Location Code argument validation.""" + +import argparse +import re + + +def LocationCode(code): + """EDGE Location Code argument validator. + + Location Code is the last component in a channel identifier; + SNCL => Station, Network, Channel, Location Code + + Parameters + ---------- + code : str + the code to validate + + Returns + ------- + str + validated lcoation code. + + Raises + ------ + argparse.ArgumentTypeError + if the location code doesn't match the regular expression. + """ + try: + return re.match('^[A-Z0-9]{2}$', code).group(0) + except: + raise argparse.ArgumentTypeError( + 'Invalid location code, expected /^[A-Z0-9]{2}$/') diff --git a/geomagio/edge/__init__.py b/geomagio/edge/__init__.py index 1555f9579b474bd36e402082663ba1923dc3188c..ba615d36c791013dd5e9be3d29709bdb5dbecb90 100644 --- a/geomagio/edge/__init__.py +++ b/geomagio/edge/__init__.py @@ -2,9 +2,11 @@ """ from EdgeFactory import EdgeFactory +from LocationCode import LocationCode from RawInputClient import RawInputClient __all__ = [ 'EdgeFactory', + 'LocationCode', 'RawInputClient' ] diff --git a/geomagio/iaga2002/IAGA2002Parser.py b/geomagio/iaga2002/IAGA2002Parser.py index e81610d4bc6c04d96627860a4c1c991fce997fff..637e7b79c1ecf089698082ae3ae66c808209d1a8 100644 --- a/geomagio/iaga2002/IAGA2002Parser.py +++ b/geomagio/iaga2002/IAGA2002Parser.py @@ -103,10 +103,13 @@ class IAGA2002Parser(object): key = 'sensor_orientation' elif key_upper == 'DIGITAL SAMPLING': key = 'sensor_sampling_rate' - if value.find('second') != -1: - value = 1 / float(value.replace('second', '').strip()) - elif value.find('Hz') != -1: - value = float(value.replace('Hz', '').strip()) + try: + if value.find('second') != -1: + value = 1 / float(value.replace('second', '').strip()) + elif value.find('Hz') != -1: + value = float(value.replace('Hz', '').strip()) + except: + return elif key_upper == 'DATA INTERVAL TYPE': key = 'data_interval_type' elif key_upper == 'DATA TYPE': diff --git a/geomagio/iaga2002/IAGA2002Writer.py b/geomagio/iaga2002/IAGA2002Writer.py index 6737274e90becb2be42b565267d2c6d072d0dcd2..10cec07de7005b003065ed5a4301bc5c350a0619 100644 --- a/geomagio/iaga2002/IAGA2002Writer.py +++ b/geomagio/iaga2002/IAGA2002Writer.py @@ -102,7 +102,7 @@ class IAGA2002Writer(object): an array containing formatted strings of header data. """ comments = [] - if 'declination_base' in stats: + if 'declination_base' in stats and stats.declination_base is not None: comments.append('DECBAS {:<8d}' '(Baseline declination value in tenths of minutes East' ' (0-216,000)).'.format(stats.declination_base)) @@ -118,7 +118,8 @@ class IAGA2002Writer(object): ' INTERMAGNET DVD.') comments.append('Go to www.intermagnet.org for details on' + ' obtaining this product.') - if 'conditions_of_use' in stats: + if 'conditions_of_use' in stats and \ + stats.conditions_of_use is not None: comments.append('CONDITIONS OF USE: ' + stats.conditions_of_use) # generate comment output buf = [] diff --git a/geomagio/imfv122/IMFV122Parser.py b/geomagio/imfv122/IMFV122Parser.py index 9f8e943712ae072a3a52fd27bd92dd9b16421309..1c92647226e0b05b575b4b4694878b2907c76b24 100644 --- a/geomagio/imfv122/IMFV122Parser.py +++ b/geomagio/imfv122/IMFV122Parser.py @@ -140,5 +140,9 @@ class IMFV122Parser(object): data[data == int(EIGHTS)] = numpy.nan data[data == EIGHTS] = numpy.nan data[data == NINES] = numpy.nan - self.data[channel] = data / 10 + if channel == 'D': + data = data / 100 + else: + data = data / 10 + self.data[channel] = data self._parsedata = None diff --git a/test/imfv122_test/IMFV122Parser_test.py b/test/imfv122_test/IMFV122Parser_test.py index fea9af44ef32aa874d49607c81c65fc8ec07b660..573a0f4a8623a972fe92b8f2657fa9616f686132 100644 --- a/test/imfv122_test/IMFV122Parser_test.py +++ b/test/imfv122_test/IMFV122Parser_test.py @@ -66,11 +66,11 @@ def test_imfv122_post_process(): parser._post_process() assert_equals(parser.times[0], UTCDateTime('2016-01-01T02:03:00Z')) assert_equals(parser.data['H'][0], 123.4) - assert_equals(parser.data['D'][0], 567.8) + assert_equals(parser.data['D'][0], 56.78) assert_equals(parser.data['Z'][0], 910.1) assert_equals(parser.data['F'][0], 112.1) assert_equals(parser.times[1], UTCDateTime('2016-01-01T02:03:01Z')) assert_equals(parser.data['H'][1], 314.1) - assert_equals(parser.data['D'][1], 516.1) + assert_equals(parser.data['D'][1], 51.61) assert_equals(parser.data['Z'][1], 718.1) assert_equals(parser.data['F'][1], 920.2)