def test_exceptions():
    homedir = os.path.dirname(
        os.path.abspath(__file__))  # where is this script?
    datafile_v2 = os.path.join(homedir, '..', 'data', 'geonet',
                               '20161113_110259_WTMC_20.V2A')
    stream_v2 = read_geonet(datafile_v2)
    stream1 = stream_v2.select(channel="HN1")
    try:
        StationSummary.from_stream(stream1, ['gmrotd50'], ['pga'])
        sucess = True
    except PGMException:
        sucess = False
    assert sucess == False

    for trace in stream_v2:
        stream1.append(trace)
    try:
        StationSummary.from_stream(stream1, ['gmrotd50'], ['pga'])
        sucess = True
    except PGMException:
        sucess = False
    assert sucess == False

    stream2 = Stream([
        stream_v2.select(channel="HN1")[0],
        Trace(data=np.asarray([]), header={"channel": "HN2"})
    ])
    try:
        StationSummary.from_stream(stream2, ['gmrotd50'], ['pga'])
        sucess = True
    except PGMException:
        sucess = False
    assert sucess == False
def test_gmrotd():
    homedir = os.path.dirname(
        os.path.abspath(__file__))  # where is this script?
    datafile_v2 = os.path.join(homedir, '..', 'data', 'geonet',
                               '20161113_110259_WTMC_20.V2A')
    stream_v2 = read_geonet(datafile_v2)
    station_summary = StationSummary.from_stream(
        stream_v2, ['gmrotd0', 'gmrotd50', 'gmrotd100'], ['pga'])
Exemple #3
0
def test_greater_of_two_horizontals():
    homedir = os.path.dirname(
        os.path.abspath(__file__))  # where is this script?
    datafile_v2 = os.path.join(homedir, '..', 'data', 'geonet',
                               '20161113_110259_WTMC_20.V2A')
    stream_v2 = read_geonet(datafile_v2)
    station_summary = StationSummary.from_stream(
        stream_v2, ['greater_of_two_horizontals'], ['pga'])
    station_dict = station_summary.pgms['PGA']
    greater = station_dict['GREATER_OF_TWO_HORIZONTALS']
    np.testing.assert_almost_equal(greater, 99.3173469387755, decimal=1)
def test():
    homedir = os.path.dirname(
        os.path.abspath(__file__))  # where is this script?
    datadir = os.path.join(homedir, '..', 'data', 'cwb')
    datafiles = glob.glob(os.path.join(datadir, '*.dat'))
    streams = []
    for datafile in datafiles:
        stream = read_cwb(datafile)
        streams.append(stream)
    df, _ = streams_to_dataframe(streams)
    pgasum = df['BN2']['PGA'].sum()
    np.testing.assert_almost_equal(pgasum, 1.7209452756509136, decimal=1)

    # Test for channel grouping with three unique channels
    streams = []
    datadir = os.path.join(homedir, '..', 'data', 'knet')
    for ext in ['.EW', '.NS', '.UD']:
        filename = 'AOM0031801241951' + ext
        datafile = os.path.join(homedir, '..', 'data', 'knet', filename)
        streams += [read_knet(datafile)]
    grouped_streams = group_channels(streams)
    assert len(grouped_streams) == 1
    assert grouped_streams[0].count() == 3

    # Test for channel grouping with three more duplicate channels
    for ext in ['.EW', '.NS', '.UD']:
        filename = 'AOM0031801241951' + ext
        datafile = os.path.join(homedir, '..', 'data', 'knet', filename)
        streams += [read_knet(datafile)]
    grouped_streams = group_channels(streams)
    assert len(grouped_streams) == 1
    assert grouped_streams[0].count() == 3

    # Test for channel grouping with more file types
    filename = '20161113_110313_THZ_20.V2A'
    datafile = os.path.join(homedir, '..', 'data', 'geonet', filename)
    streams += [read_geonet(datafile)]
    grouped_streams = group_channels(streams)
    assert len(grouped_streams) == 2
    assert grouped_streams[0].count() == 3
    assert grouped_streams[1].count() == 3

    # Test for warning for one channel streams
    filename = 'AOM0071801241951.UD'
    datafile = os.path.join(homedir, '..', 'data', 'knet', filename)
    streams += [read_knet(datafile)]
    with warnings.catch_warnings(record=True) as w:
        grouped_streams = group_channels(streams)
        assert issubclass(w[-1].category, Warning)
        assert "One channel stream:" in str(w[-1].message)
    assert len(grouped_streams) == 3
    assert grouped_streams[0].count() == 3
    assert grouped_streams[1].count() == 3
    assert grouped_streams[2].count() == 1
def test_spectral():
    # where is this script?
    homedir = os.path.dirname(os.path.abspath(__file__))
    datafile_v2 = os.path.join(homedir, '..', 'data', 'geonet',
                               '20161113_110259_WTMC_20.V2A')
    stream_v2 = read_geonet(datafile_v2)
    df2, _ = streams_to_dataframe([stream_v2])

    assert df2['HN1']['SA(0.3)'].iloc[0] / 323.8532 >= 0.95
    assert df2['HN1']['SA(1.0)'].iloc[0] / 136.6972 >= 0.95
    assert df2['HN1']['SA(3.0)'].iloc[0] / 17.9511 >= 0.95
def test_sa():
    homedir = os.path.dirname(
        os.path.abspath(__file__))  # where is this script?
    datafile_v2 = os.path.join(homedir, '..', 'data', 'geonet',
                               '20161113_110259_WTMC_20.V2A')
    stream_v2 = read_geonet(datafile_v2)
    sa_target = {}
    for trace in stream_v2:
        vtrace = trace.copy()
        vtrace.integrate()
        sa_target[vtrace.stats['channel']] = np.abs(vtrace.max())
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        station_summary = StationSummary.from_stream(
            stream_v2, ['greater_of_two_horizontals', 'gmrotd50', 'channels'],
            ['sa1.0', 'saincorrect'])
    assert 'SA1.0' in station_summary.pgms
Exemple #7
0
def test_channels():
    homedir = os.path.dirname(
        os.path.abspath(__file__))  # where is this script?
    datafile_v2 = os.path.join(homedir, '..', 'data', 'geonet',
                               '20161113_110259_WTMC_20.V2A')
    stream_v2 = read_geonet(datafile_v2)
    station_summary = StationSummary.from_stream(stream_v2, ['channels'],
                                                 ['pga'])
    station_dict = station_summary.pgms['PGA']
    np.testing.assert_almost_equal(station_dict['HN2'],
                                   81.28979591836733,
                                   decimal=1)
    np.testing.assert_almost_equal(station_dict['HN1'],
                                   99.3173469387755,
                                   decimal=1)
    np.testing.assert_almost_equal(station_dict['HNZ'],
                                   183.89693877551022,
                                   decimal=1)
def test_pga():
    homedir = os.path.dirname(os.path.abspath(
        __file__))  # where is this script?
    datafile_v2 = os.path.join(homedir, '..', 'data', 'geonet',
                               '20161113_110259_WTMC_20.V2A')
    stream_v2 = read_geonet(datafile_v2)
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        station_summary = StationSummary.from_stream(stream_v2,
                ['channels', 'greater_of_two_horizontals', 'gmrotd50',
                'gmrotd100', 'gmrotd0'],
                ['pga', 'sa1.0', 'saincorrect'])
    station_dict = station_summary.pgms['PGA']
    greater = station_dict['GREATER_OF_TWO_HORIZONTALS']
    np.testing.assert_almost_equal(station_dict['HN2'], 81.28979591836733, decimal=1)
    np.testing.assert_almost_equal(station_dict['HN1'], 99.3173469387755, decimal=1)
    np.testing.assert_almost_equal(station_dict['HNZ'], 183.89693877551022, decimal=1)
    np.testing.assert_almost_equal(greater, 99.3173469387755, decimal=1)
def test_stationsummary():
    homedir = os.path.dirname(
        os.path.abspath(__file__))  # where is this script?
    datafile = os.path.join(homedir, '..', 'data', 'geonet',
                            '20161113_110259_WTMC_20.V2A')
    target_imcs = np.sort(
        np.asarray([
            'GREATER_OF_TWO_HORIZONTALS', 'HN1', 'HN2', 'HNZ', 'ROTD50.0',
            'ROTD100.0'
        ]))
    target_imts = np.sort(np.asarray(['SA1.0', 'PGA', 'PGV']))
    stream = read_geonet(datafile)
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        stream_summary = StationSummary.from_stream(stream, [
            'greater_of_two_horizontals', 'channels', 'rotd50', 'rotd100',
            'invalid'
        ], ['sa1.0', 'PGA', 'pgv', 'invalid'])
        original_stream = stream_summary.stream
        stream_summary.stream = []
        final_stream = stream_summary.stream
        assert original_stream == final_stream
        original_code = stream_summary.station_code
        stream_summary.station_code = ''
        final_code = stream_summary.station_code
        assert original_code == final_code
        original_oscillators = stream_summary.oscillators
        final_oscillators = stream_summary.oscillators
        assert original_oscillators == final_oscillators
        np.testing.assert_array_equal(np.sort(stream_summary.components),
                                      target_imcs)
        np.testing.assert_array_equal(np.sort(stream_summary.imts),
                                      target_imts)
        np.testing.assert_almost_equal(stream_summary.get_pgm('PGA', 'HN1'),
                                       99.3173469387755,
                                       decimal=1)
        target_available = np.sort(
            np.asarray([
                'calculate_greater_of_two_horizontals', 'calculate_channels',
                'calculate_gmrotd', 'calculate_rotd'
            ]))
        imcs = stream_summary.available_imcs
        np.testing.assert_array_equal(np.sort(imcs), target_available)
        target_available = np.sort(
            np.asarray([
                'calculate_pga', 'calculate_pgv', 'calculate_sa',
                'calculate_arias'
            ]))
        imts = stream_summary.available_imts
        np.testing.assert_array_equal(np.sort(imts), target_available)
    test_pgms = {
        'PGV': {
            'ROTD100.0': 114.24894584734818,
            'ROTD50.0': 81.55436750525355,
            'HNZ': 37.47740000000001,
            'HN1': 100.81460000000004,
            'HN2': 68.4354,
            'GREATER_OF_TWO_HORIZONTALS': 100.81460000000004
        },
        'PGA': {
            'ROTD100.0': 100.73875535385548,
            'ROTD50.0': 91.40178541935455,
            'HNZ': 183.7722361866693,
            'HN1': 99.24999872535474,
            'HN2': 81.23467239067368,
            'GREATER_OF_TWO_HORIZONTALS': 99.24999872535474
        },
        'SA1.0': {
            'ROTD100.0': 146.9023350124098,
            'ROTD50.0': 106.03202302692158,
            'HNZ': 27.74118995438756,
            'HN1': 136.25041187387063,
            'HN2': 84.69296738413021,
            'GREATER_OF_TWO_HORIZONTALS': 136.25041187387063
        }
    }
    datafile = os.path.join(homedir, '..', 'data', 'geonet',
                            '20161113_110313_THZ_20.V2A')
    invalid_stream = read_geonet(datafile)
    station_code = 'WTMC'
    pgm_summary = StationSummary.from_pgms(station_code, test_pgms)
    # assert pgm_summary.pgms == stream_summary.pgms
    adict = pgm_summary.pgms
    bdict = stream_summary.pgms
    cmp_dicts(adict, bdict)

    # oscillators cannot be calculated without a stream
    try:
        pgm_summary.generate_oscillators(pgm_summary.imts, 0.05)
        success = True
    except Exception:
        success = False
    assert success is False
    # Invalid stream inputs should be rejected
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        pgm_summary.stream = []
        assert pgm_summary.stream is None
        pgm_summary.stream = invalid_stream
        assert pgm_summary.stream is None
        pgm_summary.stream = stream
        assert pgm_summary.stream == stream
Exemple #10
0
def test():
    homedir = os.path.dirname(
        os.path.abspath(__file__))  #where is this script?
    datadir = os.path.join(homedir, '..', '..', '..', 'data', 'geonet')

    # first test a non-geonet file
    try:
        assert is_geonet(os.path.abspath(__file__))
    except AssertionError:
        assert 1 == 1

    # loop over some events that test different properties
    comps = [('20161113_110259_WTMC_20.V1A', 'V1 file w/ remainder row',
              -1102.6, 922.9, 3154.1),
             ('20161113_110259_WTMC_20.V2A', 'V2 file w/ remainder row',
              -973.31, 796.64, 1802.19),
             ('20161113_110313_THZ_20.V1A', 'V1 file w/out remainder row',
              39.97, 48.46, -24.91),
             ('20180212_211557_WPWS_20.V2A', 'V2 file w/out remainder row',
              -4.16, -19.40, -2.73)]

    for comp in comps:
        fname = comp[0]
        desc = comp[1]
        test_vals = comp[2:]
        print('Testing %s, %s...' % (fname, desc))
        geonet_file = os.path.join(datadir, fname)
        assert is_geonet(geonet_file)
        stream = read_geonet(geonet_file)
        np.testing.assert_almost_equal(stream[0].max(),
                                       test_vals[0],
                                       decimal=1)
        np.testing.assert_almost_equal(stream[1].max(),
                                       test_vals[1],
                                       decimal=1)
        np.testing.assert_almost_equal(stream[2].max(),
                                       test_vals[2],
                                       decimal=1)

    # test the velocity values from one of the V2 files
    comps = [('20180212_211557_WPWS_20.V2A', 0.165, 0.509, -0.091)]
    for comp in comps:
        geonet_file = os.path.join(datadir, comp[0])
        stream = read_geonet(geonet_file)
        traces = []
        for trace in stream:
            vtrace = trace.copy()
            vtrace.detrend('linear')
            vtrace.detrend('demean')
            vtrace.taper(max_percentage=0.05, type='cosine')
            vtrace.filter('highpass',
                          freq=FILTER_FREQ,
                          zerophase=True,
                          corners=CORNERS)
            vtrace.detrend('linear')
            vtrace.detrend('demean')
            vtrace.integrate()
            traces.append(vtrace)

        assert traces[0].max() / comp[1] >= 0.95
        assert traces[1].max() / comp[2] >= 0.95
        assert traces[2].max() / comp[3] >= 0.95