Ejemplo n.º 1
0
def test_EdgeRupture():

    # Rupture requires an origin even when not used:
    origin = Origin({'id': 'test',
                     'lon': 0, 'lat': 0,
                     'depth': 5.0, 'mag': 7.0, 'netid': 'us',
                     'network': '', 'locstring': '',
                     'time': HistoricTime.utcfromtimestamp(time.time())})

    file = os.path.join(homedir, 'rupture_data/cascadia.json')
    rup = get_rupture(origin, file)
    np.testing.assert_allclose(rup.getArea(), 105635.92827547337)

    # Force read Northridge as EdgeRupture
    file = os.path.join(homedir, 'rupture_data/northridge_fault.txt')
    d = text_to_json(file, new_format=True)
    rupt = EdgeRupture(d, origin)
    strike = rupt.getStrike()
    np.testing.assert_allclose(strike, 121.97, atol=0.01)
    dip = rupt.getDip()
    np.testing.assert_allclose(dip, 40.12, atol=0.01)
    L = rupt.getLength()
    np.testing.assert_allclose(L, 17.99, atol=0.01)
    W = rupt.getWidth()
    np.testing.assert_allclose(W, 23.92, atol=0.01)
    ztor = rupt.getDepthToTop()
    np.testing.assert_allclose(ztor, 5, atol=0.01)

    # And again for the same vertices but reversed order
    file = os.path.join(homedir, 'rupture_data/northridge_fixed_fault.txt')
    d = text_to_json(file, new_format=True)
    rupt = EdgeRupture(d, origin)
    strike = rupt.getStrike()
    np.testing.assert_allclose(strike, 121.97, atol=0.01)
    dip = rupt.getDip()
    np.testing.assert_allclose(dip, 40.12, atol=0.01)
    L = rupt.getLength()
    np.testing.assert_allclose(L, 17.99, atol=0.01)
    W = rupt.getWidth()
    np.testing.assert_allclose(W, 23.92, atol=0.01)
    ztor = rupt.getDepthToTop()
    np.testing.assert_allclose(ztor, 5, atol=0.01)

    # Test for fromArrays method
    toplats = np.array([37.0, 38.0])
    toplons = np.array([-120.0, -120.0])
    topdeps = np.array([0.0, 0.0])
    botlats = copy.copy(toplats)
    botlons = copy.copy(toplons)
    botdeps = np.array([10.0, 10.0])
    erup = EdgeRupture.fromArrays(toplons, toplats, topdeps, botlons, botlats,
                                  botdeps, origin)
    # Error: array lengths differ
    with pytest.raises(ShakeLibException) as e:
        qrup = QuadRupture.fromVertices(
            [toplons[0]], [toplats[0]], [topdeps[0]],
            [toplons[1]], [toplats[1]], [topdeps[1]],
            [botlons[1]], [botlats[1]], [botdeps[1]],
            [botlons[0]], [botlats[0]], [botdeps[0]][:-1],
            origin)
    print(str(e))

    # Error: group index too long
    with pytest.raises(ShakeLibException) as e:
        qrup = QuadRupture.fromVertices(
            [toplons[0]], [toplats[0]], [topdeps[0]],
            [toplons[1]], [toplats[1]], [topdeps[1]],
            [botlons[1]], [botlats[1]], [botdeps[1]],
            [botlons[0]], [botlats[0]], [botdeps[0]],
            origin, group_index=[0, 0, 0, 0, 0, 0])
    print(str(e))

    qrup = QuadRupture.fromVertices(
        [toplons[0]], [toplats[0]], [topdeps[0]],
        [toplons[1]], [toplats[1]], [topdeps[1]],
        [botlons[1]], [botlats[1]], [botdeps[1]],
        [botlons[0]], [botlats[0]], [botdeps[0]],
        origin)
    np.testing.assert_allclose(erup.getArea(), 1108.9414759967776)
    np.testing.assert_allclose(erup.getDepthToTop(), 0)
    np.testing.assert_allclose(erup.getLength(), 111.19492664455889)
    np.testing.assert_allclose(
        erup.lats, np.array([37.,  38.,  38.,  37.,  37.,  np.nan]))
    np.testing.assert_allclose(
        erup.lons, np.array([-120., -120., -120., -120., -120.,  np.nan]))
    np.testing.assert_allclose(
        erup.depths, np.array([0.,   0.,  10.,  10.,   0.,  np.nan]))
    np.testing.assert_allclose(
        erup._getGroupIndex(), np.array([0.,   0.]))
    quads = erup.getQuadrilaterals()
    np.testing.assert_allclose(quads[0][0].x, -120.0)

    # Need to also test the distances with EdgeRupture
    lons = np.linspace(-120.1, -121.0, 10)
    lats = np.linspace(37.0, 38, 10)
    deps = np.zeros_like(lons)
    rrup1, _ = qrup.computeRrup(lons, lats, deps)
    rrup2, _ = erup.computeRrup(lons, lats, deps)
    np.testing.assert_allclose(rrup1, rrup2, atol=2e-2)
    rjb1, _ = qrup.computeRjb(lons, lats, deps)
    rjb2, _ = erup.computeRjb(lons, lats, deps)
    np.testing.assert_allclose(rjb1, rjb2, atol=2e-2)
    gc2 = erup.computeGC2(lons, lats, deps)
    targetRy0 = np.array(
        [0., 0.,  0., 0.,  0.,
         0., 0.,  0., 0.,  0.67335931])
    targetRx = np.array(
        [-8.88024949, -17.73390996, -26.56167797, -35.3634266,
         -44.13902929, -52.88835984, -61.61129242, -70.30770154,
         -78.97746209, -87.6204493])
    np.testing.assert_allclose(gc2['ry0'], targetRy0)
    np.testing.assert_allclose(gc2['rx'], targetRx)
Ejemplo n.º 2
0
def test_text_to_json():
    sm_data = '''#Oglesby, D. D., D. S. Dreger, R. A. Harris, \
N. Ratchkovski, and R. Hansen (2004). Inverse kinematic and forward dynamic \
models of the 2002 Denali fault earthquake, Alaska, Bull. Seism. Soc. Am. 94, \
S214-S233.
>
-147.807 63.434        0.000
-147.210 63.472        0.000
-147.267 63.650        22.294
-147.864 63.613        22.294
-147.807 63.434        0.000
>
-146.951 63.551        0.000
-147.551 63.518        0.000
-147.551 63.518        30.000
-146.951 63.551        30.000
-146.951 63.551        0.000
>
-145.968 63.453        0.000
-146.952 63.547        0.000
-146.952 63.547        30.000
-145.968 63.453        30.000
-145.968 63.453        0.000
>
-143.586 62.872        0.000
-145.996 63.427        0.000
-145.996 63.427        30.000
-143.586 62.872        30.000
-143.586 62.872        0.000
>
-142.500 62.114        0.000
-143.669 62.831        0.000
-143.669 62.831        30.000
-142.500 62.114        30.000
-142.500 62.114        0.000'''
    stringio = io.StringIO(sm_data)
    jdict = text_to_json(stringio)
    refcmp = '''Oglesby, D. D., D. S. Dreger, R. A. Harris, \
N. Ratchkovski, and R. Hansen (2004). Inverse kinematic and forward dynamic \
models of the 2002 Denali fault earthquake, Alaska, Bull. Seism. Soc. Am. 94, \
S214-S233.'''
#    assert jdict['metadata']['reference'] == refcmp.replace('\n', ' ')
    assert jdict['metadata']['reference'] == refcmp
    cstart = [[-147.807, 63.434, 0.0],
              [-147.21,  63.472, 0.0],
              [-147.267, 63.65,  22.294],
              [-147.864, 63.613, 22.294],
              [-147.807, 63.434, 0.0]]
    cend = [[-142.5,   62.114, 0.0],
            [-143.669, 62.831, 0.0],
            [-143.669, 62.831, 30.0],
            [-142.5,   62.114, 30.0],
            [-142.5,   62.114, 0.0]]

    assert len(jdict['features'][0]['geometry']['coordinates'][0]) == 5
    assert jdict['features'][0]['geometry']['coordinates'][0][0] == cstart
    assert jdict['features'][0]['geometry']['coordinates'][0][-1] == cend

    sm_data2 = '''#Source: NEIC (2004) based on aftershock distribution
2.98 94.45 10.0
5.0  94.0 10.0
6.0  93.5 10.0
7.0  93.2 10.0
8.0  93.1 10.0
10.0 92.5 10.0
12.0 92.5 10.0
12.0 93.5 40.0
10.0 93.5 40.0
8.0  94.2 40.0
7.0  94.2 40.0
6.0  95.0 40.0
5.0  95.2 40.0
3.30 95.78 40.0
2.98 94.45 10.0'''
    stringio = io.StringIO(sm_data2)
    jdict = text_to_json(stringio, new_format=False)
    coords = [[94.45, 2.98, 10.0],
              [94.0,  5.0,  10.0],
              [93.5,  6.0,  10.0],
              [93.2,  7.0,  10.0],
              [93.1,  8.0,  10.0],
              [92.5, 10.0,  10.0],
              [92.5, 12.0,  10.0],
              [93.5, 12.0,  40.0],
              [93.5, 10.0,  40.0],
              [94.2,  8.0,  40.0],
              [94.2,  7.0,  40.0],
              [95.0,  6.0,  40.0],
              [95.2,  5.0,  40.0],
              [95.78, 3.30, 40.0],
              [94.45, 2.98, 10.0]]
    assert len(jdict['features'][0]['geometry']['coordinates']) == 1
    assert jdict['features'][0]['geometry']['coordinates'][0][0] == coords
Ejemplo n.º 3
0
def test_EdgeRupture():
    # Rupture requires an origin even when not used:
    origin = Origin({'eventsourcecode': 'test', 'lat': 0, 'lon': 0,
                     'depth': 5.0, 'mag': 7.0})

    file = os.path.join(homedir, 'rupture_data/cascadia.json')
    rup = get_rupture(origin, file)
    np.testing.assert_allclose(rup.getArea(), 105635.92827547337)

    # Force read Northridge as EdgeRupture
    file = os.path.join(homedir, 'rupture_data/northridge_fault.txt')
    d = text_to_json(file)
    rupt = EdgeRupture(d, origin)
    strike = rupt.getStrike()
    np.testing.assert_allclose(strike, 121.97, atol=0.01)
    dip = rupt.getDip()
    np.testing.assert_allclose(dip, 40.12, atol=0.01)
    L = rupt.getLength()
    np.testing.assert_allclose(L, 17.99, atol=0.01)
    W = rupt.getWidth()
    np.testing.assert_allclose(W, 23.92, atol=0.01)
    ztor = rupt.getDepthToTop()
    np.testing.assert_allclose(ztor, 5, atol=0.01)

    # And again for the same vertices but reversed order
    file = os.path.join(homedir, 'rupture_data/northridge_fixed_fault.txt')
    d = text_to_json(file)
    rupt = EdgeRupture(d, origin)
    strike = rupt.getStrike()
    np.testing.assert_allclose(strike, 121.97, atol=0.01)
    dip = rupt.getDip()
    np.testing.assert_allclose(dip, 40.12, atol=0.01)
    L = rupt.getLength()
    np.testing.assert_allclose(L, 17.99, atol=0.01)
    W = rupt.getWidth()
    np.testing.assert_allclose(W, 23.92, atol=0.01)
    ztor = rupt.getDepthToTop()
    np.testing.assert_allclose(ztor, 5, atol=0.01)

    # Test for fromArrays method
    toplats = np.array([37.0, 38.0])
    toplons = np.array([-120.0, -120.0])
    topdeps = np.array([0.0, 0.0])
    botlats = copy.copy(toplats)
    botlons = copy.copy(toplons)
    botdeps = np.array([10.0, 10.0])
    erup = EdgeRupture.fromArrays(toplons, toplats, topdeps, botlons, botlats,
                                  botdeps, origin)
    qrup = QuadRupture.fromVertices(
        [toplons[0]], [toplats[0]], [topdeps[0]],
        [toplons[1]], [toplats[1]], [topdeps[1]],
        [botlons[1]], [botlats[1]], [botdeps[1]],
        [botlons[0]], [botlats[0]], [botdeps[0]],
        origin)
    np.testing.assert_allclose(erup.getArea(), 1108.9414759967776)
    np.testing.assert_allclose(erup.getDepthToTop(), 0)
    np.testing.assert_allclose(erup.getLength(), 111.19492664455889)
    np.testing.assert_allclose(
        erup.lats, np.array([37.,  38.,  38.,  37.,  37.,  np.nan]))
    np.testing.assert_allclose(
        erup.lons, np.array([-120., -120., -120., -120., -120.,  np.nan]))
    np.testing.assert_allclose(
        erup.depths, np.array([0.,   0.,  10.,  10.,   0.,  np.nan]))
    np.testing.assert_allclose(
        erup._getGroupIndex(), np.array([0.,   0.]))
    quads = erup.getQuadrilaterals()
    np.testing.assert_allclose(quads[0][0].x, -120.0)

    # Need to also test the distances with EdgeRupture
    lons = np.linspace(-120.1, -121.0, 10)
    lats = np.linspace(37.0, 38, 10)
    deps = np.zeros_like(lons)
    rrup1 = qrup.computeRrup(lons, lats, deps)
    rrup2 = erup.computeRrup(lons, lats, deps)
    np.testing.assert_allclose(rrup1, rrup2, atol=2e-2)
    rjb1 = qrup.computeRjb(lons, lats, deps)
    rjb2 = erup.computeRjb(lons, lats, deps)
    np.testing.assert_allclose(rjb1, rjb2, atol=2e-2)
    gc2 = erup.computeGC2(lons, lats, deps)
    targetRy0 = np.array(
      [0., 0.,  0., 0.,  0.,
       0., 0.,  0., 0.,  0.67335931])
    targetRx = np.array(
      [-8.88024949, -17.73390996, -26.56167797, -35.3634266,
       -44.13902929, -52.88835984, -61.61129242, -70.30770154,
       -78.97746209, -87.6204493])
    np.testing.assert_allclose(gc2['ry0'], targetRy0)
    np.testing.assert_allclose(gc2['rx'], targetRx)