def readANTELOPE(database, station=None, channel=None, starttime=None, endtime=None): """ Reads a portion of a Antelope wfdisc table to a Stream. Attempts to return one Trace per line of the 'wfdisc' view passed. Additionally, will filter and cut with respect to any of the fields in the primary key IF specified. (sta chan time::endtime) NOTE: Currently MUST have both times (start/end) or neither. the returned Traces will have a new attribute, 'db' :type database: string or antelope.datascope.Dbptr :param database: Antelope database name or pointer :type station: string :param station: Station expression to subset :type channel: string :param channel: Channel expression to subset :type starttime: :class: `~obspy.core.utcdatetime.UTCDateTime` :param starttime: Desired start time :type endtime: :class: `~obspy.core.utcdatetime.UTCDateTime` :param endtime: Desired end time :rtype: :class: `~obspy.core.stream.Stream' :return: Stream with one Trace for each row of the database view .. rubric:: Example >>> st = readANTELOPE('/opt/antelope/example/db', station='TOLO', channel='LH.', starttime=UTCDateTime(2008,6,13), endtime=UTCDateTime(2008,6,14)) >>> print(st) 6 Trace(s) in Stream: XA.TOL0..LHE | 2008-06-12T23:59:59.640000Z - 2008-06-13T00:04:11.640000Z | 1.0 Hz, 253 samples XA.TOL0..LHE | 2008-06-13T00:04:12.640000Z - 2008-06-13T23:59:59.640000Z | 1.0 Hz, 86148 samples XA.TOL0..LHN | 2008-06-12T23:59:59.640000Z - 2008-06-13T00:04:11.640000Z | 1.0 Hz, 253 samples XA.TOL0..LHN | 2008-06-13T00:04:12.640000Z - 2008-06-13T23:59:59.640000Z | 1.0 Hz, 86148 samples XA.TOL0..LHZ | 2008-06-12T23:59:59.640000Z - 2008-06-13T00:04:21.640000Z | 1.0 Hz, 263 samples XA.TOL0..LHZ | 2008-06-13T00:04:22.640000Z - 2008-06-13T23:59:59.640000Z | 1.0 Hz, 86138 samples Also adds a Dbrecord as an attribute of the Trace >>> st[0].db Dbrecord('View43' -> TOL0 LHE 1213229044.64::1213315451.64) """ from obspy.core import read, Stream, UTCDateTime if isinstance(database, Dbptr): db = Dbptr(database) db = db.lookup(table="wfdisc") else: raise TypeError("Must input a string or pointer to a valid database") if station is not None: db = db.subset("sta=~/{0}/".format(station)) if channel is not None: db = db.subset("chan=~/{0}/".format(channel)) if starttime is not None and endtime is not None: ts = starttime.timestamp te = endtime.timestamp db = db.subset("endtime > {0} && time < {1}".format(ts, te)) else: ts = starttime te = endtime assert db.nrecs() is not 0, "No records for given time period" st = Stream() for db.record in range(db.nrecs()): fname = db.filename() dbr = RowPointerDict(db) t0 = UTCDateTime(dbr["time"]) t1 = UTCDateTime(dbr["endtime"]) if dbr["time"] < ts: t0 = starttime if dbr["endtime"] > te: t1 = endtime if os.path.exists(fname): _st = read(fname, starttime=t0, endtime=t1) # add format? _st = _st.select(station=dbr["sta"], channel=dbr["chan"]) # not location aware # _st[0].db = dbr if dbr["calib"] < 0: _st[0].data *= -1 st += _st # Close what we opened, BUT garbage collection may take care of this: # if you have an open pointer but pass db name as a string, global # use of your pointer won't work if this is uncommented: # # if isinstance(database,str): # db.close() return st
def main(): from argparse import ArgumentParser # Get command line args parser = ArgumentParser() parser.add_argument("dbin", help="Input database") parser.add_argument("dbout", help="Output database", nargs='?') parser.add_argument("-p", "--plot", help="Plot result", action='store_true') parser.add_argument("-l", "--loc", help="dbloc2 mode", action='store_true') parser.add_argument("-i", "--image", help="Save image with db", action='store_true') parser.add_argument("--pf", help="Parameter file") group = parser.add_mutually_exclusive_group() #required=True) group.add_argument("--evid", help="Event ID", type=int) group.add_argument("--orid", help="Origin ID", type=int) args = parser.parse_args() # Special 'dbloc2' settings if args.loc: from antelope.datascope import Dbptr # alter args b/c dbloc2 passes a db and a row number args.dbin = args.dbin.rstrip('.origin') db = Dbptr(args.dbin) db = db.lookup(table='origin') db.record = int(args.dbout) args.orid = db.getv('orid')[0] args.dbout = dbloc_source_db(args.dbin, pointer=False) args.plot = True # force plot args.image = True # force saving image to db folder # Now that we have a save location from command line args, # make a function to save to that database. The plotter is I/O # agnostic, it will accept a function to save anything anyhow anywhichway # def save_plot_to_db(fmplotter, dbname=args.dbout, dump_bitmap=args.image): focal_mech = fmplotter.event.focal_mechanisms[fmplotter._fm_index] if focal_mech is not fmplotter.event.preferred_focal_mechanism(): fmplotter.event.preferred_focal_mechanism_id = focal_mech.resource_id.resource_id # Save to db eventfocalmech2db(event=fmplotter.event, database=dbname) if dump_bitmap: vers = fmplotter.event.preferred_origin().creation_info.version dbdir = os.path.dirname(dbname) _dump_bitmap(figure=fmplotter.fig, directory=dbdir, uid=vers) # Run HASH hp = dbhash_run(args.dbin, orid=args.orid, pf=args.pf) # Launch plotter or spit out solution if args.plot: from hashpy.plotting.focalmechplotter import FocalMechPlotter ev = hp.output(format="OBSPY") p = FocalMechPlotter(ev, save=save_plot_to_db) else: # quick orid/strike/dip/rake line print hp.output() p = 0 if args.dbout: db = hp.output(format="ANTELOPE", dbout=args.dbout) # Done, return HashPype and/or FocalMechPlotter for debugging return hp, p
def readANTELOPE(database, station=None, channel=None, starttime=None, endtime=None): """ Reads a portion of a Antelope wfdisc table to a Stream. Attempts to return one Trace per line of the 'wfdisc' view passed. Additionally, will filter and cut with respect to any of the fields in the primary key IF specified. (sta chan time::endtime) NOTE: Currently MUST have both times (start/end) or neither. the returned Traces will have a new attribute, 'db' :type database: string or antelope.datascope.Dbptr :param database: Antelope database name or pointer :type station: string :param station: Station expression to subset :type channel: string :param channel: Channel expression to subset :type starttime: :class: `~obspy.core.utcdatetime.UTCDateTime` :param starttime: Desired start time :type endtime: :class: `~obspy.core.utcdatetime.UTCDateTime` :param endtime: Desired end time :rtype: :class: `~obspy.core.stream.Stream' :return: Stream with one Trace for each row of the database view .. rubric:: Example >>> st = readANTELOPE('/opt/antelope/example/db', station='TOLO', channel='LH.', starttime=UTCDateTime(2008,6,13), endtime=UTCDateTime(2008,6,14)) >>> print(st) 6 Trace(s) in Stream: XA.TOL0..LHE | 2008-06-12T23:59:59.640000Z - 2008-06-13T00:04:11.640000Z | 1.0 Hz, 253 samples XA.TOL0..LHE | 2008-06-13T00:04:12.640000Z - 2008-06-13T23:59:59.640000Z | 1.0 Hz, 86148 samples XA.TOL0..LHN | 2008-06-12T23:59:59.640000Z - 2008-06-13T00:04:11.640000Z | 1.0 Hz, 253 samples XA.TOL0..LHN | 2008-06-13T00:04:12.640000Z - 2008-06-13T23:59:59.640000Z | 1.0 Hz, 86148 samples XA.TOL0..LHZ | 2008-06-12T23:59:59.640000Z - 2008-06-13T00:04:21.640000Z | 1.0 Hz, 263 samples XA.TOL0..LHZ | 2008-06-13T00:04:22.640000Z - 2008-06-13T23:59:59.640000Z | 1.0 Hz, 86138 samples Also adds a Dbrecord as an attribute of the Trace >>> st[0].db Dbrecord('View43' -> TOL0 LHE 1213229044.64::1213315451.64) """ from obspy.core import read, Stream, UTCDateTime if isinstance(database,Dbptr): db = Dbptr(database) db = db.lookup(table='wfdisc') else: raise TypeError("Must input a string or pointer to a valid database") if station is not None: db = db.subset('sta=~/{0}/'.format(station)) if channel is not None: db = db.subset('chan=~/{0}/'.format(channel)) if starttime is not None and endtime is not None: ts = starttime.timestamp te = endtime.timestamp db = db.subset('endtime > {0} && time < {1}'.format(ts,te) ) else: ts = starttime te = endtime assert db.nrecs() is not 0, "No records for given time period" st = Stream() for db.record in range(db.nrecs() ): fname = db.filename() dbr = RowPointerDict(db) t0 = UTCDateTime(dbr['time']) t1 = UTCDateTime(dbr['endtime']) if dbr['time'] < ts: t0 = starttime if dbr['endtime'] > te: t1 = endtime if os.path.exists(fname): _st = read(fname, starttime=t0, endtime=t1) # add format? _st = _st.select(station=dbr['sta'], channel=dbr['chan']) #not location aware #_st[0].db = dbr if dbr['calib'] < 0: _st[0].data *= -1 st += _st # Close what we opened, BUT garbage collection may take care of this: # if you have an open pointer but pass db name as a string, global # use of your pointer won't work if this is uncommented: # #if isinstance(database,str): # db.close() return st