diff --git a/bin/xyz.py b/bin/xyz.py index 52a5f0ba5b45e549f5d5dc9e0420b2f2f252fc90..2c222cd35aaf7651c8cc1af871fe527ebb4e9583 100755 --- a/bin/xyz.py +++ b/bin/xyz.py @@ -1,22 +1,44 @@ #! /usr/bin/env python +"""Converts iaga2002 files from one coordinate system to another. + + Inputs + ------ + informat: string + The input format/coordinate system of the input file. + geo: geographic coordinate system (xyzf) + mag: magnetic north coordinate system (hdzf) + obs: observatory coordinate system (hezf or hdzf) + outformat: string + The ouput format/coordinate system of the output file. + geo: geographic coordinate system (xyzf) + mag: magnetic north coordinate system (hdzf) + obs: observatory coordinate system (hezf or hdzf) + infile: string + the filename of the Iaga2002 file to be read from + outfile: string + the filename of a new Iaga2002 file to be read to + d_out: boolean + a flag indicating whether the output for the observatory coordinate + system should have the e coordinate instead of d. +""" + import argparse from os import path -import os -#ensure geomag is on the path before importing -script_dir = path.dirname(path.abspath(__file__)) +# ensure geomag is on the path before importing if __file__ != 'main.py': import sys + script_dir = path.dirname(path.abspath(__file__)) sys.path.append(path.normpath(path.join(script_dir, '..'))) import geomagio.iaga2002 as iaga2002 -from geomagio.iaga2002.IAGA2002Factory import read_url import geomagio.StreamConverter as StreamConverter # static containing the standard output types for iaga2002 files. CHANNELS = { 'geo': ['X', 'Y', 'Z', 'F'], 'mag': ['H', 'D', 'Z', 'F'], + 'obsd': ['H', 'D', 'Z', 'F'], 'obs': ['H', 'E', 'Z', 'F'] } @@ -44,7 +66,7 @@ def get_out_channels(args): format = 'mag' return CHANNELS[format] -def convert_stream(args, timeseries): +def convert_stream(timeseries, informat, outformat): """converts a timeseries stream into a different coordinate system Parameters @@ -58,91 +80,67 @@ def convert_stream(args, timeseries): new stream object containing the converted coordinates. """ out_stream = None - if args.outformat == 'geo' and args.informat == 'mag': + if outformat == 'geo' and informat == 'mag': out_stream = StreamConverter.get_geo_from_mag(timeseries) - if args.outformat == 'geo' and args.informat == 'obs': + elif outformat == 'geo' and informat == 'obs': out_stream = StreamConverter.get_geo_from_obs(timeseries) - if args.outformat == 'mag' and args.informat == 'obs': + elif outformat == 'mag' and informat == 'obs': out_stream = StreamConverter.get_mag_from_obs(timeseries) - if args.outformat == 'mag' and args.informat == 'geo': + elif outformat == 'mag' and informat == 'geo': out_stream = StreamConverter.get_mag_from_geo(timeseries) - if args.outformat == 'obs' and args.informat == 'mag': + elif outformat == 'obs' and informat == 'mag': out_stream = StreamConverter.get_obs_from_mag(timeseries) - if args.outformat == 'obs' and args.informat == 'geo': - out_stream = StreamConverter.get_obs_from_geo(timeseries, args.d_out) + elif outformat == 'obs' and informat == 'geo': + out_stream = StreamConverter.get_obs_from_geo(timeseries) - if args.outformat == 'obs' and args.informat == 'obs': + elif outformat == 'obsd' and informat == 'geo': + out_stream = StreamConverter.get_obs_from_geo(timeseries, True) + + elif outformat == 'obs' and informat == 'obs': out_stream = StreamConverter.get_obs_from_obs(timeseries, - True, args.d_out) + True, False) + + elif outformat == 'obsd' and informat =='obs': + out_stream = StreamConverter.get_obs_from_obs(timeseries, + False, True) return out_stream def main(): - """Converts iaga2002 files from one coordinate system to another. - - Inputs - ------ - informat: string - The input format/coordinate system of the input file. - geo: geographic coordinate system (xyzf) - mag: magnetic north coordinate system (hdzf) - obs: observatory coordinate system (hezf or hdzf) - outformat: string - The ouput format/coordinate system of the output file. - geo: geographic coordinate system (xyzf) - mag: magnetic north coordinate system (hdzf) - obs: observatory coordinate system (hezf or hdzf) - infile: string - the filename of the Iaga2002 file to be read from - outfile: string - the filename of a new Iaga2002 file to be read to - d_out: boolean - a flag indicating whether the output for the observatory coordinate - system should have the e coordinate instead of d. - """ parser = argparse.ArgumentParser( description='Use @ to read commands from a file.', fromfile_prefix_chars='@') - parser.add_argument('--informat', choices=['geo', 'mag', 'obs'], - default='obs') - parser.add_argument('--outformat', choices=['geo', 'mag', 'obs'], - default='geo') + parser.add_argument('--informat', choices=['geo', 'mag', 'obs']) + parser.add_argument('--outformat', choices=['geo', 'mag', 'obs', 'obsd']) parser.add_argument('--infile', help='iaga2002 input file') parser.add_argument('--outfile', help='iaga2002 out file') - parser.add_argument('--d_out', - help='output D in the IAGA2002 file instead of E for obs coordinates', - type=bool, - default=False) args = parser.parse_args() iagaFile = '' if args.infile != None: - file_name = 'file://' + path.join(os.getcwd(), args.infile) - iagaFile = read_url(file_name) + iagaFile = open(args.infile, 'r').read() else: - for line in sys.stdin: - iagaFile += line + iagaFile = sys.stdin.read() factory = iaga2002.IAGA2002Factory(None) - timeseries = factory.parse_file(iagaFile) - out_stream = convert_stream(args, timeseries) - channels = get_out_channels(args) - print channels + timeseries = factory.parse_string(iagaFile) + out_stream = convert_stream(timeseries, args.informat, args.outformat) + channels = CHANNELS[args.outformat] if args.outfile != None: fh = open(args.outfile, 'w') else: fh = sys.stdout - iaga2002.IAGA2002Writer().write(fh, out_stream, channels) + factory.write_string(fh, out_stream, channels) if __name__ == '__main__': diff --git a/geomagio/iaga2002/IAGA2002Factory.py b/geomagio/iaga2002/IAGA2002Factory.py index 32e70c16df367ba30de6f03c4b44812d1629a8e7..bbb87847e195e0b0f12428757e0a7b32e0f00a8c 100644 --- a/geomagio/iaga2002/IAGA2002Factory.py +++ b/geomagio/iaga2002/IAGA2002Factory.py @@ -105,20 +105,20 @@ class IAGA2002Factory(TimeseriesFactory): for day in days: url = self._get_url(observatory, day, type, interval) iagaFile = read_url(url) - timeseries += self.parse_file(iagaFile) + timeseries += self.parse_string(iagaFile) # merge channel traces for multiple days timeseries.merge() # trim to requested start/end time timeseries.trim(starttime, endtime) return timeseries - def parse_file(self, iagaFile): - """Parse the contents of a url to an IAGA2002 file. + def parse_string(self, iaga2002String): + """Parse the contents of a string in the format of an IAGA2002 file. Parameters ---------- - url : str - url containing IAGA2002 content. + iaga2002String : str + string containing IAGA2002 content. Returns ------- @@ -126,7 +126,7 @@ class IAGA2002Factory(TimeseriesFactory): parsed data. """ parser = IAGA2002Parser() - parser.parse(iagaFile) + parser.parse(iaga2002String) headers = parser.headers station = headers['IAGA CODE'] comments = tuple(parser.comments) @@ -339,6 +339,19 @@ class IAGA2002Factory(TimeseriesFactory): day = obspy.core.UTCDateTime(day.timestamp + 86400) return days + def write_string(self, fh, timeseries, channels): + """writes timeseries data to the given file object. + + Parameters + ---------- + fh: file object + timeseries : obspy.core.Stream + stream containing traces to store. + channels : array_like + list of channels to store + """ + IAGA2002Writer().write(fh, timeseries, channels) + def put_timeseries(self, timeseries, starttime=None, endtime=None, channels=None, type=None, interval=None): """Store timeseries data. @@ -378,7 +391,7 @@ class IAGA2002Factory(TimeseriesFactory): self._get_url(observatory, day, type, interval)) day_timeseries = self._get_slice(timeseries, day) with open(day_filename, 'w') as fh: - IAGA2002Writer().write(fh, day_timeseries, channels) + self.write_string(fh, day_timeseries, channels) def _get_file_from_url(self, url): """Get a file for writing.