diff --git a/geomagio/Controller.py b/geomagio/Controller.py index 177f61b325e6dc9b95f450d9d1548426714538ce..146207d6a2f8850393fa0a0ad385c04a0bec3d03 100644 --- a/geomagio/Controller.py +++ b/geomagio/Controller.py @@ -13,7 +13,7 @@ import iaga2002 import pcdcp import imfv283 -# DCS 20160326 -- factories for new filetypes +# factories for new filetypes import temperature import vbf @@ -378,29 +378,27 @@ def main(args): locationCode=locationcode, tag=args.output_edge_tag, forceout=args.output_edge_forceout) - - # DCS 20160326 -- new factories elif args.output_temperature_file is not None: outputfactory = temperature.StreamTEMPFactory( stream=open(args.output_temperature_file, 'wb'), observatory=args.observatory, type=args.type, interval=args.interval) - # DCS 20160401 -- param list for StreamVBF includes flag for vbf vs binlog + # VBF includes flag for writing vbf file vs bin-change log elif args.output_vbf_file is not None: outputfactory = vbf.StreamVBFFactory( stream=open(args.output_vbf_file, 'wb'), observatory=args.observatory, type=args.type, interval=args.interval, - output = 'vbf') + output='vbf') elif args.output_binlog_file is not None: outputfactory = vbf.StreamVBFFactory( stream=open(args.output_binlog_file, 'wb'), observatory=args.observatory, type=args.type, interval=args.interval, - output = 'binlog') + output='binlog') elif args.output_plot: outputfactory = PlotTimeseriesFactory() @@ -585,14 +583,12 @@ def parse_args(args): # Output group output_group = parser.add_mutually_exclusive_group(required=True) - # DCS -- options for new output factories output_group.add_argument('--output-temperature-file', help='Write to a single temperature/battery file.') output_group.add_argument('--output-vbf-file', help='Write to a single voltage-bin file.') output_group.add_argument('--output-binlog-file', help='Write to a single bin-change log file.') - output_group.add_argument('--output-iaga-file', help='Write to a single iaga file.') output_group.add_argument('--output-iaga-stdout', diff --git a/geomagio/edge/EdgeFactory.py b/geomagio/edge/EdgeFactory.py index 12f5a5af01349559c9e9943a9716d4e3889a1c82..e9e43c274abc66a75973e81c58de512b33abe740 100644 --- a/geomagio/edge/EdgeFactory.py +++ b/geomagio/edge/EdgeFactory.py @@ -354,8 +354,8 @@ class EdgeFactory(TimeseriesFactory): edge_interval_code = self._get_interval_code(interval) edge_channel = None - # DCS 20160403 -- if form is chan.loc, return chan portion - # Allows specific chan/loc selection in geomag.py + # If form is chan.loc, return chan (left) portion. + # Allows specific chan/loc selection. if channel.find('.') >= 0: tmplist = channel.split('.') return tmplist[0].strip() @@ -404,8 +404,8 @@ class EdgeFactory(TimeseriesFactory): """ location = None - # DCS 20160403 -- if form is chan.loc, return loc portion - # Allows specific chan/loc selection in geomag.py + # If form is chan.loc, return loc (right) portion + # Allows specific chan/loc selection. if channel.find('.') >= 0: tmplist = channel.split('.') return tmplist[1].strip() diff --git a/geomagio/pcdcp/PCDCPFactory.py b/geomagio/pcdcp/PCDCPFactory.py index f094d8c2262f0e633a14b56266aca08b2df388e0..4760b94a297a10065db4e1a3fd9717af6aca4014 100644 --- a/geomagio/pcdcp/PCDCPFactory.py +++ b/geomagio/pcdcp/PCDCPFactory.py @@ -103,22 +103,20 @@ class PCDCPFactory(TimeseriesFactory): parser = PCDCPParser() parser.parse(pcdcpString) - year = parser.header['year'] - yearday = parser.header['yearday'] + yr = int(parser.header['year']) + yrday = int(parser.header['yearday']) begin = int(parser.times[0]) - startHour = str(int(begin / 60.0)) - startMinute = str(int(begin % 60.0)) + startHour = int(begin / 60.0) + startMinute = int(begin % 60.0) ending = int(parser.times[-1]) - endHour = str(int(ending / 60.0)) - endMinute = str(int(ending % 60.0)) + endHour = int(ending / 60.0) + endMinute = int(ending % 60.0) - start = year + yearday + "T" + startHour + ":" + \ - startMinute + ":" + "00.0" - end = year + yearday + "T" + endHour + ":" + endMinute + ":" + "00.0" - - starttime = obspy.core.UTCDateTime(start) - endtime = obspy.core.UTCDateTime(end) + starttime = obspy.core.UTCDateTime(year=yr, julday=yrday, + hour=startHour, minute=startMinute) + endtime = obspy.core.UTCDateTime(year=yr, julday=yrday, hour=endHour, + minute=endMinute) data = parser.data length = len(data[data.keys()[0]]) diff --git a/geomagio/pcdcp/PCDCPWriter.py b/geomagio/pcdcp/PCDCPWriter.py index 4080fa50eb9e4a2eead8f8601ee3682987ce3449..352bb95cfdc994c6a12734a7572affb14a5b8f1e 100644 --- a/geomagio/pcdcp/PCDCPWriter.py +++ b/geomagio/pcdcp/PCDCPWriter.py @@ -34,14 +34,13 @@ class PCDCPWriter(object): (channel, str(TimeseriesUtility.get_channels(timeseries)))) stats = timeseries[0].stats - # DCS 20160328 -- set dead val for 1-sec data - # won't work if input is IAGA2002: stats missing data_interval + # Set dead val for 1-sec data. + # Won't work if input is IAGA2002: stats missing data_interval if stats.data_interval == "second": self.empty_value = PCDCPParser.NINES_RAW out.write(self._format_header(stats)) - # DCS 20160325 -- pass stats to _format_data out.write(self._format_data(timeseries, channels, stats)) def _format_header(self, stats): @@ -64,7 +63,7 @@ class PCDCPWriter(object): yearday = str(stats.starttime.julday).zfill(3) date = stats.starttime.strftime("%d-%b-%y") - # DCS 20160325 -- 1-sec vs 1-min headers + # Choose resolution for 1-sec vs 1-min header. resolution = "0.01nT" # won't work if input is IAGA2002: stats missing data_interval if stats.data_interval == "second": @@ -73,10 +72,8 @@ class PCDCPWriter(object): buf.append(observatory + ' ' + year + ' ' + yearday + ' ' + date + ' HEZF ' + resolution + ' File Version 2.00\n') - return ''.join(buf) - # DCS 20160325 -- include stats in parms list def _format_data(self, timeseries, channels, stats): """Format all data lines. @@ -114,16 +111,13 @@ class PCDCPWriter(object): starttime = float(traces[0].stats.starttime) delta = traces[0].stats.delta - # DCS 20160325 -- pass stats to _format_values for i in xrange(len(traces[0].data)): buf.append(self._format_values( datetime.utcfromtimestamp(starttime + i * delta), - (t.data[i] for t in traces), stats - )) + (t.data[i] for t in traces), stats)) return ''.join(buf) - # DCS 20160325 -- include stats in parms list def _format_values(self, time, values, stats): """Format one line of data values. @@ -140,8 +134,8 @@ class PCDCPWriter(object): unicode Formatted line containing values. """ - # DCS 20160325 -- 1-sec and 1-min data have different formats - # won't work if input is IAGA2002: stats missing data_interval + # 1-sec and 1-min data have different formats. + # Won't work if input is IAGA2002: stats missing data_interval. time_width = 4 data_width = 8 data_multiplier = 100 @@ -158,11 +152,11 @@ class PCDCPWriter(object): tt = time.timetuple() - totalMinutes = int(tt.tm_hour * hr_multiplier + \ + totalMinutes = int(tt.tm_hour * hr_multiplier + tt.tm_min * mn_multiplier + tt.tm_sec * sc_multiplier) - return '{0:0>{tw}d} {1: >{dw}d} {2: >{dw}d} {3: >{dw}d} {4: >{dw}d}\n'.\ - format( totalMinutes, tw=time_width, + return '{0:0>{tw}d} {1: >{dw}d} {2: >{dw}d} {3: >{dw}d}' \ + ' {4: >{dw}d}\n'.format(totalMinutes, tw=time_width, *[self.empty_value if numpy.isnan(val) else int(round( val * data_multiplier)) for val in values], dw=data_width) diff --git a/geomagio/temperature/TEMPFactory.py b/geomagio/temperature/TEMPFactory.py index 52b4071de12d634bba769faec76632aeec16db45..fb7649a3697e3afa8cab4ff3a188abd62608530a 100644 --- a/geomagio/temperature/TEMPFactory.py +++ b/geomagio/temperature/TEMPFactory.py @@ -5,7 +5,6 @@ from .. import ChannelConverter from ..TimeseriesFactory import TimeseriesFactory from ..TimeseriesFactoryException import TimeseriesFactoryException from ..Util import read_url -from TEMPParser import TEMPParser from TEMPWriter import TEMPWriter @@ -103,22 +102,20 @@ class TEMPFactory(TimeseriesFactory): parser = TEMPParser() parser.parse(tempString) - year = parser.header['year'] - yearday = parser.header['yearday'] + yr = int(parser.header['year']) + yrday = int(parser.header['yearday']) begin = int(parser.times[0]) - startHour = str(int(begin / 60.0)) - startMinute = str(int(begin % 60.0)) + startHour = int(begin / 60.0) + startMinute = int(begin % 60.0) ending = int(parser.times[-1]) - endHour = str(int(ending / 60.0)) - endMinute = str(int(ending % 60.0)) + endHour = int(ending / 60.0) + endMinute = int(ending % 60.0) - start = year + yearday + "T" + startHour + ":" + \ - startMinute + ":" + "00.0" - end = year + yearday + "T" + endHour + ":" + endMinute + ":" + "00.0" - - starttime = obspy.core.UTCDateTime(start) - endtime = obspy.core.UTCDateTime(end) + starttime = obspy.core.UTCDateTime(year=yr, julday=yrday, + hour=startHour, minute=startMinute) + endtime = obspy.core.UTCDateTime(year=yr, julday=yrday, hour=endHour, + minute=endMinute) data = parser.data length = len(data[data.keys()[0]]) diff --git a/geomagio/temperature/TEMPWriter.py b/geomagio/temperature/TEMPWriter.py index f5280f9e9128cada7e53775df00cee1f41592b44..5df1b252a7e9bfbfd5d5bb184892711e96a1026d 100644 --- a/geomagio/temperature/TEMPWriter.py +++ b/geomagio/temperature/TEMPWriter.py @@ -1,9 +1,8 @@ import numpy -import TEMPParser from cStringIO import StringIO from datetime import datetime -from .. import ChannelConverter, TimeseriesUtility +from .. import TimeseriesUtility from ..TimeseriesFactoryException import TimeseriesFactoryException from obspy.core import Stream @@ -12,7 +11,7 @@ class TEMPWriter(object): """TEMP writer. """ - def __init__(self, empty_value=TEMPParser.NINES_DEG): + def __init__(self, empty_value=numpy.int('9999')): self.empty_value = empty_value def write(self, out, timeseries, channels): @@ -121,8 +120,8 @@ class TEMPWriter(object): tt = time.timetuple() totalMinutes = int(tt.tm_hour * 60 + tt.tm_min) - return '{0:0>4d} {1: >5d} {2: >5d} {3: >5d} {4: >5d} {5: >5d}\n'.format( - totalMinutes, + return '{0:0>4d} {1: >5d} {2: >5d} {3: >5d} {4: >5d}' \ + ' {5: >5d}\n'.format(totalMinutes, *[self.empty_value if numpy.isnan(val) else int(round( val * 10)) for val in values]) diff --git a/geomagio/temperature/__init__.py b/geomagio/temperature/__init__.py index 2d5067b57917e80308e76a5030f9d815309600de..81046e6acc8201f3fde0fb30a132506636722c2e 100644 --- a/geomagio/temperature/__init__.py +++ b/geomagio/temperature/__init__.py @@ -3,13 +3,11 @@ from TEMPFactory import TEMPFactory from StreamTEMPFactory import StreamTEMPFactory -from TEMPParser import TEMPParser from TEMPWriter import TEMPWriter __all__ = [ 'TEMPFactory', 'StreamTEMPFactory', - 'TEMPParser', 'TEMPWriter' ] diff --git a/geomagio/vbf/StreamVBFFactory.py b/geomagio/vbf/StreamVBFFactory.py index ebe4b73a19507d8b1ac9af1606bb66e738c338e2..1be360341850c474d6cd99156ca0847c3ab74cf4 100644 --- a/geomagio/vbf/StreamVBFFactory.py +++ b/geomagio/vbf/StreamVBFFactory.py @@ -18,8 +18,7 @@ class StreamVBFFactory(VBFFactory): Timeseriesfactory """ - # DCS 20160401 -- 'output' flag added to parm list. - # If 'vbf' then write a vbf file, if 'binlog' then make a bin change log + # Flag "output" used for vbf file versus bin-change log. def __init__(self, stream, observatory=None, channels=None, type=None, interval=None, output='vbf'): VBFFactory.__init__(self, None, observatory, channels, diff --git a/geomagio/vbf/VBFFactory.py b/geomagio/vbf/VBFFactory.py index adc0d80f39e5cf2f4a48937860bf1691f9f43dee..767b243821f4443b8352a0de69a0f8ab7d102092 100644 --- a/geomagio/vbf/VBFFactory.py +++ b/geomagio/vbf/VBFFactory.py @@ -5,7 +5,6 @@ from .. import ChannelConverter from ..TimeseriesFactory import TimeseriesFactory from ..TimeseriesFactoryException import TimeseriesFactoryException from ..Util import read_url -from VBFParser import VBFParser from VBFWriter import VBFWriter @@ -35,7 +34,7 @@ class VBFFactory(TimeseriesFactory): VBFParser """ - # DCS -- 20160401 -- output flag added to parm list for vbf vs binlog + # Flag "output" used for vbf file vs bin-change log. def __init__(self, urlTemplate, observatory=None, channels=None, type=None, interval=None, output='vbf'): TimeseriesFactory.__init__(self, observatory, channels, type, @@ -105,22 +104,20 @@ class VBFFactory(TimeseriesFactory): parser = VBFParser() parser.parse(vbfString) - year = parser.header['year'] - yearday = parser.header['yearday'] + yr = int(parser.header['year']) + yrday = int(parser.header['yearday']) begin = int(parser.times[0]) - startHour = str(int(begin / 60.0)) - startMinute = str(int(begin % 60.0)) + startHour = int(begin / 60.0) + startMinute = int(begin % 60.0) ending = int(parser.times[-1]) - endHour = str(int(ending / 60.0)) - endMinute = str(int(ending % 60.0)) + endHour = int(ending / 60.0) + endMinute = int(ending % 60.0) - start = year + yearday + "T" + startHour + ":" + \ - startMinute + ":" + "00.0" - end = year + yearday + "T" + endHour + ":" + endMinute + ":" + "00.0" - - starttime = obspy.core.UTCDateTime(start) - endtime = obspy.core.UTCDateTime(end) + starttime = obspy.core.UTCDateTime(year=yr, julday=yrday, + hour=startHour, minute=startMinute) + endtime = obspy.core.UTCDateTime(year=yr, julday=yrday, hour=endHour, + minute=endMinute) data = parser.data length = len(data[data.keys()[0]]) @@ -192,8 +189,6 @@ class VBFFactory(TimeseriesFactory): list of channels to store """ - # DCS 20160401 -- if making a bin change log, call the _change_ - # version of 'write'. Otherwise, call the usual 'write' if self.output == 'binlog': VBFWriter().write_change_log(fh, timeseries, channels) else: diff --git a/geomagio/vbf/VBFWriter.py b/geomagio/vbf/VBFWriter.py index b4f185318f0fdcee87ebdd0f38371062ea35194e..9b3cfbff3eec6d2ad2bcd6ffe59ca08a3ff213ff 100644 --- a/geomagio/vbf/VBFWriter.py +++ b/geomagio/vbf/VBFWriter.py @@ -1,6 +1,5 @@ import numpy -import VBFParser from cStringIO import StringIO from datetime import datetime from .. import ChannelConverter, TimeseriesUtility @@ -8,20 +7,21 @@ from ..TimeseriesFactoryException import TimeseriesFactoryException from obspy.core import Stream -# DCS 20160328 -- for a binlog, need to save previous time and volts +# For a binlog, need to save previous time and volts. h_prev = [99.999999, 999] e_prev = [99.999999, 999] z_prev = [99.999999, 999] -# DCS 20160328 -- use seperate HEZ buffers to group binlog output by component +# Use seperate HEZ buffers to group binlog output by component. Hbuf = [] Ebuf = [] Zbuf = [] + class VBFWriter(object): """VBF writer. """ - def __init__(self, empty_value=VBFParser.NINES): + def __init__(self, empty_value=numpy.int('9999999')): self.empty_value = empty_value def write(self, out, timeseries, channels): @@ -135,10 +135,10 @@ class VBFWriter(object): tt = time.timetuple() totalMinutes = int(tt.tm_hour * 3600 + tt.tm_min * 60 + tt.tm_sec) - # DCS 20160328 -- init volt/bin vals to dead + # Init the volt/bin vals to deads. vdead = 99.999999 bdead = 999 - vblist =[vdead, bdead, vdead, bdead, vdead, bdead] + vblist = [vdead, bdead, vdead, bdead, vdead, bdead] # now "un-dead" the good vals, format volts as float, bins as int for idx, valx in enumerate(values): @@ -148,18 +148,14 @@ class VBFWriter(object): else: vblist[idx] = int(valx) - return '{0:0>5d} {1: >10.6f} {2: >4d} {3: >10.6f} {4: >4d} ' \ '{5: >10.6f} {6: >4d}\n'.format(totalMinutes, *vblist) - - - - #=============================================== + # =============================================== # CODE BELOW IS FOR MAKING A BIN CHANGE LOG. # VBFFactory calls the '_change_' version of the # procedures rather than the "usual" procedures - #=============================================== + # =============================================== def write_change_log(self, out, timeseries, channels): """Write timeseries to vbf file. @@ -180,13 +176,13 @@ class VBFWriter(object): (channel, str(TimeseriesUtility.get_channels(timeseries)))) stats = timeseries[0].stats - out.write(self._format_change_header(stats)) self._format_change_data(timeseries, channels) if (len(Hbuf) + len(Ebuf) + len(Zbuf)) > 0: - out.write(' C Date Time DaySec Bin change Voltage change\n') + out.write(' C Date Time DaySec Bin change' + ' Voltage change\n') out.write(''.join(Hbuf)) out.write('\n') out.write(''.join(Ebuf)) @@ -219,7 +215,6 @@ class VBFWriter(object): return ''.join(buf) - def _format_change_data(self, timeseries, channels): """Format all data lines. @@ -235,8 +230,6 @@ class VBFWriter(object): str A string formatted to be the data lines in a VBF file. """ - buf = [] - # create new stream timeseriesLocal = Stream() @@ -290,11 +283,10 @@ class VBFWriter(object): ' ({1:0>5d})'. \ format(tt, totalMinutes) - # init volt/bin vals to dead vdead = 99.999999 bdead = 999 - vblist =[vdead, bdead, vdead, bdead, vdead, bdead] + vblist = [vdead, bdead, vdead, bdead, vdead, bdead] # now "un-dead" the non-nans, format volts as float, bins as int for idx, valx in enumerate(values): @@ -304,22 +296,21 @@ class VBFWriter(object): else: vblist[idx] = int(valx) - if vblist[1] != 999 and h_prev[1] != 999 and vblist[1] != h_prev[1]: - Hbuf.append('{0: >3s} {1:>s} ' \ - '{2: >4d} to {3: >4d} {4: >10.6f} to {5: >10.6f}\n'. \ + Hbuf.append('{0: >3s} {1:>s} ' + '{2: >4d} to {3: >4d} {4: >10.6f} to {5: >10.6f}\n'. format('(H)', timestr, h_prev[1], vblist[1], h_prev[0], vblist[0])) if vblist[3] != 999 and e_prev[1] != 999 and vblist[3] != e_prev[1]: - Ebuf.append('{0: >3s} {1:>s} ' \ - '{2: >4d} to {3: >4d} {4: >10.6f} to {5: >10.6f}\n'. \ + Ebuf.append('{0: >3s} {1:>s} ' + '{2: >4d} to {3: >4d} {4: >10.6f} to {5: >10.6f}\n'. format('(E)', timestr, e_prev[1], vblist[3], e_prev[0], vblist[2])) if vblist[5] != 999 and z_prev[1] != 999 and vblist[5] != z_prev[1]: - Zbuf.append('{0: >3s} {1:>s} ' \ - '{2: >4d} to {3: >4d} {4: >10.6f} to {5: >10.6f}\n'. \ + Zbuf.append('{0: >3s} {1:>s} ' + '{2: >4d} to {3: >4d} {4: >10.6f} to {5: >10.6f}\n'. format('(Z)', timestr, z_prev[1], vblist[5], z_prev[0], vblist[4])) @@ -332,10 +323,8 @@ class VBFWriter(object): z_prev[0] = vblist[4] z_prev[1] = vblist[5] - return - @classmethod def format(self, timeseries, channels): """Get an VBF formatted string. diff --git a/geomagio/vbf/__init__.py b/geomagio/vbf/__init__.py index a4b28ccf257ca251aad8e400d307cef59b9c57c8..13d26cbef4a01539e82dae3116435ad983b878f8 100644 --- a/geomagio/vbf/__init__.py +++ b/geomagio/vbf/__init__.py @@ -3,13 +3,11 @@ from VBFFactory import VBFFactory from StreamVBFFactory import StreamVBFFactory -from VBFParser import VBFParser from VBFWriter import VBFWriter __all__ = [ 'VBFFactory', 'StreamVBFFactory', - 'VBFParser', 'VBFWriter' ]