예제 #1
0
def test_1dassociater(random_filename):
    db_assoc = random_filename(ext='.db')
    db_tt_test = random_filename(ext='.db')

    # Our SQLite databases are:
    db_assoc = 'sqlite:///' + db_assoc
    shutil.copy(db_tt, db_tt_test)
    db_tt_test = 'sqlite:///' + db_tt_test
    # Traveltime database
    # Connect to our databases
    engine_assoc = create_engine(db_assoc, echo=False)
    # # Create the tables required to run the 1D associator
    tables1D.Base.metadata.create_all(engine_assoc)
    Session = sessionmaker(bind=engine_assoc)
    session = Session()
    # Find all waveform data in the data directory
    file_list = glob.glob(os.path.join(EX_DATA, '*.msd'))

    # Define our picker instance
    picker = fbpicker.FBPicker(t_long=5,
                               freqmin=0.5,
                               mode='rms',
                               t_ma=20,
                               nsigma=3,
                               t_up=0.78,
                               nr_len=2,
                               nr_coeff=2,
                               pol_len=10,
                               pol_coeff=10,
                               uncert_coeff=3)

    st = Stream()

    for f in file_list:
        st += obspy_read(f)

    # Pick the waveforms
    for s in st:
        # st.merge()  # merge will cause issues if there is a data gap
        s.detrend('linear')
        scnl, picks, polarity, snr, uncert = picker.picks(s)
        t_create = datetime.utcnow()  # Record the time we made the picks
        # Add each pick to the database
        for i in range(len(picks)):
            log.debug('st = {} Pick = {} {} {} scnl = {}'.format(
                s, i, picks[i], picks[i].datetime, scnl))

            new_pick = tables1D.Pick(scnl, picks[i].datetime, polarity[i],
                                     snr[i], uncert[i], t_create)
            session.add(new_pick)  # Add pick i to the database
        session.commit()  # Commit the pick to the database
        log.debug('Wrote picks')

    # Define the associator
    assocOK = assoc1D.LocalAssociator(db_assoc,
                                      db_tt_test,
                                      max_km=350,
                                      aggregation=1,
                                      aggr_norm='L2',
                                      cutoff_outlier=30,
                                      assoc_ot_uncert=7,
                                      nsta_declare=3,
                                      loc_uncert_thresh=0.2)
    # Identify candidate events (Pick Aggregation)
    assocOK.id_candidate_events()
    # Associate events
    assocOK.associate_candidates()

    print("Unit Testing for 1Dassociator ...............")

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #   Function Testing rms sort list
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    radius = [
        ('SIO', -137.26, 68.992, 83.5, 0.7514, 0),
        ('U32A', -137.26, 68.992, 203.0, 1.8268, 1),
        ('W35A', -137.26, 68.992, 42.5, 0.3825, 2),
        ('OKCFA', -137.26, 68.992, 33.0, 0.297, 3),
        ('X34A', -137.26, 68.992, 122.0, 1.0979, 4),
        ('FNO', -137.26, 68.992, 36.5, 0.3285, 5),
    ]
    lon = -137.26
    lat = 68.992
    st_declare = 3  # number of station requires to monitor earth quake
    rms_sort = []
    rms_sort, cb = assocOK._LocalAssociator__accumulate_rms_sort(
        radius, lon, lat, st_declare)

    print("")
    print('rms                   = {}'.format(rms_sort))
    print('Combinations Stations = {}'.format(cb))
    print("")
    assert len(rms_sort) > 0

    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #   Function Testing for radius paremeters
    # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    #    candis_arr = [Candidate Event <2013-06-16T15:38:50.150000 U32A 203.00 1.83 18 19>]
    #    radius=[]
    #    radius, lon, lat = assocOK.accumulate_radius(candis_arr)
    #    print ("")
    #    print ("Radius    = {}".format(radius))
    #    print ("Lon       = {}".format(lon))
    #    print ("Lat       = {}".format(lat))

    # Add singles stations to events
    assocOK.single_phase()

    events = assocOK.assoc_db.query(Associated).all()
    assert len(events) == 1
    event = events[0]
    assert event.nsta == 3
    print(event.longitude)
    print(event.latitude)

    print('event.longitude = ', event.longitude)
    print('event.latitude = ', event.latitude)
db_tt = 'sqlite:///' + os.path.join(DATA, 'tt_stations_1D.db')

# Connect to our databases
engine_assoc = create_engine(db_assoc, echo=False)
# Create the tables required to run the 1D associator
tables1D.Base.metadata.create_all(engine_assoc)
Session = sessionmaker(bind=engine_assoc)
session = Session()

# Define our picker instance
picker = fbpicker.FBPicker(t_long=5,
                           freqmin=10,
                           mode='rms',
                           t_ma=20,
                           nsigma=6,
                           t_up=0.78,
                           nr_len=2,
                           nr_coeff=1.8,
                           pol_len=10,
                           pol_coeff=10,
                           uncert_coeff=3)

# Pick the waveforms
st = read(mseed)
# st.merge()  # merge will cause issues if there is a data gap
for tr in st:
    tr.detrend('linear')
    scnl, picks, polarity, snr, uncert = picker.picks(tr)
    t_create = datetime.utcnow()  # Record the time we made the picks
    # Add each pick to the database
    for i in range(len(picks)):
def get_picks(tr, picker="aic", show_plot=False):
    if picker == "kt":  # PhasePApy Kurtosis Picker

        picker = ktpicker.KTPicker(t_win=0.04,
                                   t_ma=0.12,
                                   nsigma=6,
                                   t_up=0.02,
                                   nr_len=0.06,
                                   nr_coeff=2,
                                   pol_len=10,
                                   pol_coeff=10,
                                   uncert_coeff=3)
        scnl, picks, polarity, snr, uncert = picker.picks(tr)
        if show_plot:
            print('scnl:', scnl)
            print('picks:', picks)
            print('polarity:', polarity)
            print('signal to noise ratio:', snr)
            print('uncertainty:', uncert)
            summary = ktpicker.KTSummary(picker, tr)
            summary.plot_summary()
            summary.plot_picks()

    elif picker == "fb":  # PhasePApy FBPicker

        picker = fbpicker.FBPicker(t_long=0.05,
                                   freqmin=10,
                                   mode='rms',
                                   t_ma=0.12,
                                   nsigma=6,
                                   t_up=0.02,
                                   nr_len=2,
                                   nr_coeff=2,
                                   pol_len=10,
                                   pol_coeff=10,
                                   uncert_coeff=3)
        scnl, picks, polarity, snr, uncert = picker.picks(tr)
        if show_plot:
            print('scnl:', scnl)
            print('picks:', picks)
            print('polarity:', polarity)
            print('signal to noise ratio:', snr)
            print('uncertainty:', uncert)
            summary = fbpicker.FBSummary(picker, tr)
            summary.plot_bandfilter()
            summary.plot_statistics()
            summary.plot_summary()

    elif picker == "aic":  # PhasePApy AIC differentiation picker

        picker = aicdpicker.AICDPicker(t_ma=0.12,
                                       nsigma=6,
                                       t_up=0.02,
                                       nr_len=0.06,
                                       nr_coeff=2,
                                       pol_len=10,
                                       pol_coeff=10,
                                       uncert_coeff=3)
        scnl, picks, polarity, snr, uncert = picker.picks(tr)
        if show_plot:
            print('scnl:', scnl)
            print('picks:', picks)
            print('polarity:', polarity)
            print('signal to noise ratio:', snr)
            print('uncertainty:', uncert)
            summary = aicdpicker.AICDSummary(picker, tr)
            summary.plot_summary()
            summary.plot_picks()
    else:
        raise ValueError("Unrecognized picker option")


#    print("Found %d picks." % len(picks))
    return scnl, picks, polarity, snr, uncert