예제 #1
0
def db2stream(dbname, orid, t_pre=T_PRE, t_post=T_POST):
    """
    Return obspy.Stream of waveforms around p-arrival for a given orid
    """
    DBPROCESS_CMDS = ('process', [('dbopen assoc', 
                      'dbjoin -o arrival', 
                      'dbsubset orid=={0}'.format(orid),
                      'dbsubset iphase=~/P.*/',
                      'dbjoin wfdisc sta chan',
                      'dbsubset arrival.time <= wfdisc.endtime && arrival.time >= wfdisc.time',
                      'dbgroup sta chan time delta dir dfile',
                      'dbsort -r delta',
                      )])
    
    st = obspy.core.Stream()
    dbpath = os.path.abspath(os.path.dirname(dbname))
    with dbapi2.connect(dbname) as conn:
        curs = conn.cursor(CONVERT_NULL=True, row_factory=OrderedDictRow)
        nrecs = curs.execute(*DBPROCESS_CMDS)
        LOG.debug('Number of picks/waveforms: {0}'.format(nrecs))
        for c in curs:
            fpath = os.path.join(dbpath, c['dir'], c['dfile'])
            t0 = obspy.core.UTCDateTime(c['time']-t_pre)
            t1 = obspy.core.UTCDateTime(c['time']+t_post)
            _st = obspy.core.read(fpath, starttime=t0, endtime=t1)
            if len(_st) > 1:
                _st.merge()
            st += _st
    return st
    def __init__(self, database, perm='r', **kwargs):
        """
        Initialize converter and connect to database
        
        Inputs
        ======
        database : str name of database
        **kwargs : extra keyword args

        kwargs
        ------
        perm : str of permissions ('r')
        pf : str name of pf file containing settings ('db2quakeml')

        """
        # Load config using Antelope-style param file
        if 'pf' in kwargs:
            _pf = kwargs.pop('pf')
        else:
            _pf = 'db2quakeml'
        self.load_pf(_pf)
        
        super(AntelopeToEventConverter, self).__init__(**kwargs)
        self.connection = connect(database, perm, row_factory=OrderedDictRow)
        self.connection.CONVERT_NULL = True
    def get_nearest_event_description(self, latitude, longitude, database=None):
        """
        Get the nearest place to a lat/lon from a db with a 'places' table

        Inputs
        ------
        database  : str of database with 'places' table
        latitude  : float of latitude
        longitude : float of longitude
        
        Returns : string of the distance and compass azimuth to a place

        """
        if database is None:
            database = self.place_db
        try:
            curs = connect(database).cursor(row_factory=OrderedDictRow)
            nrecs = curs.execute.lookup(table='places')
            stats = array([gps2DistAzimuth(latitude, longitude, r['lat'], r['lon']) for r in curs])
            ind = stats.argmin(0)[0]
            minstats = stats[ind]
            curs.scroll(int(ind), 'absolute')
            minrec = curs.fetchone()
            dist, azi, backazi = minstats
            compass = azimuth2compass(backazi)
            place_info = {'distance': dist/1000., 'direction': compass, 'city': minrec['place'], 'state': minrec['state']}
            curs.close()
            s = "{distance:0.1f} km {direction} of {city}, {state}".format(**place_info)
            return self._nearest_cities_description(s)
        except:
            return None
예제 #4
0
    def get_event(self,
                  dsn,
                  orid=None,
                  evid=None,
                  origin=True,
                  magnitude=True,
                  pick=False,
                  focal_mechanism=False,
                  anss=False):
        # pylint: disable=unused-argument, too-many-arguments
        """
        Run conversion with config
        """
        # IF REGULAR EVENT, USE DATABASE
        #######################################################################
        # Make db Connection -- wrap in context
        # with connect(dsn, row_factory=OrderedDictRow,
        #              CONVERT_NULL=True) as conn:

        with connect(dsn) as conn:
            db = DatabaseConverter(conn, self._conv)
            event = db.extract_origin(orid,
                                      origin=origin,
                                      magnitude=magnitude,
                                      pick=pick,
                                      focal_mechanism=focal_mechanism,
                                      anss=anss)

        # Set preferreds. The extract method should return in reversed time
        # order, so always choosing the first origin, mag, should be an OK
        # default. Need to use an algorithm for a preferred mag type. For
        # focalmechs, should be mt solutions in reversed time order, then
        # first motions in reveresed time order. This means that a default
        # would be any latest MT, then any latest FM. Or writecustom algorithm.

        try:
            event['preferredOriginID'] = event['origin'][0]['@publicID']
            event['preferredMagnitudeID'] = qml.find_preferred_mag(
                event['magnitude'][::-1], self.preferred_magtypes)
            if event.get('focalMechanism'):
                event['preferredFocalMechanismID'] = \
                    event['focalMechanism'][0]['@publicID']
        except Exception as ex:
            self.logger.exception(ex)

        #
        # Try the nearest places thing...
        #
        if self.placesdb and os.path.exists(self.placesdb):
            try:
                orig = event['origin'][0]
                ncd = get_nearest_place(
                    self.placesdb,
                    (orig['longitude']['value'], orig['latitude']['value']))
                event['description'] = self._conv.description(ncd)
            except Exception as ex:
                self.logger.exception(ex)

        return event
예제 #5
0
    def test_connection_context(self):
        """Test for Connection context manager methods"""

        with connect(self.dsn) as conn:
            dbptr = Dbptr(getattr(conn, '_dbptr'))
            self.assertNotEqual( dbptr.query('dbDATABASE_COUNT'), 0 )
            
        self.assertEqual( dbptr.query('dbDATABASE_COUNT'), 0 )
예제 #6
0
def get_nearest_place(dsn, coords):
    # pylint: disable=too-many-locals
    """
    Return dict of QML nearest_cities given a data source and coordinates
    dsn : str of (database name of places12 schema for now)
    coords : tuple of (x, y)

    Relies on Antelope procedure calls...
    """
    compass = ('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW',
               'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW')
    wedge = 360. / len(compass)

    with connect(dsn, row_factory=OrderedDictRow, CONVERT_NULL=True) as conn:
        coord = {'elat': coords[1], 'elon': coords[0]}
        curs = conn.cursor()
        curs.execute.lookup(table='places')
        cmd = "deg2km(distance({elat}, {elon}, lat, lon))"
        distances = [
            curs.execute.ex_eval(cmd.format(**coord))
            for curs._record in range(curs.rowcount)
        ]
        cmd = "azimuth(lat, lon, {elat}, {elon})"
        backazis = [
            curs.execute.ex_eval(cmd.format(**coord))
            for curs._record in range(curs.rowcount)
        ]
        # Find the record with the min distance
        ind = min(xrange(len(distances)), key=distances.__getitem__)
        dist = distances[ind]
        backazi = backazis[ind]
        curs.scroll(int(ind), 'absolute')
        minrec = curs.fetchone()
        shift_azi = (backazi + wedge / 2) - (360 *
                                             (int(backazi + wedge / 2) / 360))
        needle = compass[int(math.floor(shift_azi / wedge))]
        place_info = {
            'distance': dist,
            'direction': needle,
            'city': minrec['place'],
            'state': minrec['state']
        }
        string = "{distance:0.1f} km {direction} of {city}, {state}".format(
            **place_info)
        return string
예제 #7
0
    def get_deleted_event(self, dsn, orid=None, evid=None, anss=False):
        """
        Return a stub event set to "not existing"

        Notes
        -----
        Maybe not the place for this method, but best place for now
        """
        try:
            with connect(dsn) as conn:
                db = DatabaseConverter(conn, self._conv)
                event = db.get_event(orid=orid, evid=evid, anss=anss)
            if event is None:
                raise ValueError("Event not found")
        except DbprocessError:
            event = self._conv.map_event({'evid': evid}, anss=anss)
        finally:
            event['type'] = "not existing"
        return event
예제 #8
0
 def setUp(self):
     self.dsn = '/opt/antelope/data/db/demo/demo'
     self.NRECS_ORIGIN = 1351
     self.conn = connect(self.dsn)
     self.curs = self.conn.cursor()
예제 #9
0
 def test_connection_cursor(self):
     """Test for Connection cursor function"""
     with connect(self.dsn) as conn:
         self.assertIsInstance( conn.cursor(), Cursor )
예제 #10
0
 def test_connect(self):
     """Test for connect function"""
     
     conn = connect(self.dsn)
     self.assertIsInstance(conn, Connection)
     conn.close()