def test_QuadRupture(): # Rupture requires an origin even when not used: origin = Origin({'id':'test','lat':0,'lon':0,'depth':5.0,'mag':7.0}) # First with json file file = 'tests/data/izmit.json' rupj = read_rupture_file(origin, file) # Then with text file: file = 'tests/data/Barkaetal02_fault.txt' rupt = read_rupture_file(origin, file) np.testing.assert_allclose(rupj.lats, rupt.lats, atol=1e-5) np.testing.assert_allclose(rupj.lons, rupt.lons, atol=1e-5) np.testing.assert_allclose(rupj._depth, rupt._depth, atol=1e-5)
def test_incorrect(): rupture_text = """# Source: Ji, C., D. V. Helmberger, D. J. Wald, and K.-F. Ma (2003). Slip history and dynamic implications of the 1999 Chi-Chi, Taiwan, earthquake, J. Geophys. Res. 108, 2412, doi:10.1029/2002JB001764. 24.27980 120.72300 0 24.05000 121.00000 17 24.07190 121.09300 17 24.33120 121.04300 17 24.33120 121.04300 17 24.27980 120.72300 0 > 24.27980 120.72300 0 23.70000 120.68000 0 23.60400 120.97200 17 24.05000 121.00000 17 24.27980 120.72300 0 > 23.60400 120.97200 17 23.70000 120.68000 0 23.58850 120.58600 0 23.40240 120.78900 17 23.60400 120.97200 17""" # Rupture requires an origin even when not used: origin = Origin({'id':'test','lat':0,'lon':0,'depth':5.0,'mag':7.0}) cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): rupture = read_rupture_file(origin, cbuf)
def test_plot_rupture_wire3d(): ff = os.path.join(shakedir, "tests/data/eventdata/hayward_RC_HN_HS_HE_Shaw09Mod_GEOL.txt") flt = read_rupture_file(ff) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') plot_rupture_wire3d(flt, ax) return fig
def test_map_EdgeRupture(): origin = Origin({'id':'','lat':0,'lon':0,'depth':0,'mag':0}) # Cascadia ff = os.path.join(shakedir, "tests/data/cascadia.json") rup = read_rupture_file(origin, ff) fig = plt.figure() map_rupture(rup) return fig
def test_map_QuadRupture(): origin = Origin({'id':'','lat':0,'lon':0,'depth':0,'mag':0}) # Ismit ff = os.path.join(shakedir, "tests/data/izmit.json") rup = read_rupture_file(origin, ff) fig = plt.figure() map_rupture(rup) return fig
def test_plot_rupture_wire3d_EdgeRupture(): origin = Origin({'id':'','lat':0,'lon':0,'depth':0,'mag':0}) # Cascadia ff = os.path.join(shakedir, "tests/data/cascadia.json") rup = read_rupture_file(origin, ff) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') plot_rupture_wire3d(rup, ax) return fig
def test_plot_rupture_wire3d_QuadRupture(): origin = Origin({'id':'','lat':0,'lon':0,'depth':0,'mag':0}) # A relatively complicated QuadRupture ff = os.path.join(shakedir, "tests/data/Barkaetal02_fault.txt") rup = read_rupture_file(origin, ff) fig = plt.figure() ax = fig.add_subplot(111, projection='3d') plot_rupture_wire3d(rup, ax) return fig
def test_northridge(): rupture_text = """ # Source: Wald, D. J., T. H. Heaton, and K. W. Hudnut (1996). The Slip History of the 1994 Northridge, California, Earthquake Determined from Strong-Motion, Teleseismic, GPS, and Leveling Data, Bull. Seism. Soc. Am. 86, S49-S70. 34.315 -118.421 5.000 34.401 -118.587 5.000 34.261 -118.693 20.427 34.175 -118.527 20.427 34.315 -118.421 5.000 """ # Rupture requires an origin even when not used: origin = Origin({'id':'test','lat':0,'lon':0,'depth':5.0,'mag':7.0}) cbuf = io.StringIO(rupture_text) rupture = read_rupture_file(origin, cbuf) strike = rupture.getStrike() np.testing.assert_allclose(strike, 122.06, atol=0.01) dip = rupture.getDip() np.testing.assert_allclose(dip, 40.21, atol=0.01) L = rupture.getLength() np.testing.assert_allclose(L, 17.99, atol=0.01) W = rupture.getWidth() np.testing.assert_allclose(W, 23.94, atol=0.01) nq = rupture.getNumQuads() np.testing.assert_allclose(nq, 1) ng = rupture.getNumGroups() np.testing.assert_allclose(ng, 1) sind = rupture._getGroupIndex() np.testing.assert_allclose(sind, [0]) ztor = rupture.getDepthToTop() np.testing.assert_allclose(ztor, 5, atol=0.01) itl = rupture.getIndividualTopLengths() np.testing.assert_allclose(itl, 17.99, atol=0.01) iw = rupture.getIndividualWidths() np.testing.assert_allclose(iw, 23.94, atol=0.01) lats = rupture.lats lats_d = np.array([34.401, 34.315, 34.175, 34.261, 34.401, np.nan]) np.testing.assert_allclose(lats, lats_d, atol=0.01) lons = rupture.lons lons_d = np.array([-118.587, -118.421, -118.527, -118.693, -118.587, np.nan]) np.testing.assert_allclose(lons, lons_d, atol=0.01)
def test_incorrect(): rupture_text = """# Source: Ji, C., D. V. Helmberger, D. J. Wald, and K.-F. Ma (2003). Slip history and dynamic implications of the 1999 Chi-Chi, Taiwan, earthquake, J. Geophys. Res. 108, 2412, doi:10.1029/2002JB001764. 24.27980 120.72300 0 24.05000 121.00000 17 24.07190 121.09300 17 24.33120 121.04300 17 24.27980 120.72300 0 > 24.27980 120.72300 0 23.70000 120.68000 0 23.60400 120.97200 17 24.05000 121.00000 17 24.27980 120.72300 0 > 23.60400 120.97200 17 23.70000 120.68000 0 23.58850 120.58600 0 23.40240 120.78900 17 23.60400 120.97200 17""" cbuf = io.StringIO(rupture_text) with pytest.raises(Exception): rupture = read_rupture_file(cbuf)
def test_northridge(): rupture_text = """ # Source: Wald, D. J., T. H. Heaton, and K. W. Hudnut (1996). The Slip History of the 1994 Northridge, California, Earthquake Determined from Strong-Motion, Teleseismic, GPS, and Leveling Data, Bull. Seism. Soc. Am. 86, S49-S70. 34.315 -118.421 5.000 34.401 -118.587 5.000 34.261 -118.693 20.427 34.175 -118.527 20.427 34.315 -118.421 5.000 """ cbuf = io.StringIO(rupture_text) rupture = read_rupture_file(cbuf) strike = rupture.getStrike() np.testing.assert_allclose(strike, 122.06408, atol=0.001) dip = rupture.getDip() np.testing.assert_allclose(dip, 40.20979, atol=0.001) L = rupture.getRuptureLength() np.testing.assert_allclose(L, 17.99198, atol=0.001) W = rupture.getWidth() np.testing.assert_allclose(W, 23.93699, atol=0.001) nq = rupture.getNumQuads() np.testing.assert_allclose(nq, 1) ns = rupture.getNumSegments() np.testing.assert_allclose(ns, 1) sind = rupture._getSegmentIndex() np.testing.assert_allclose(sind, [0]) ztor = rupture.getTopOfRupture() np.testing.assert_allclose(ztor, 5, atol=0.001) itl = rupture.getIndividualTopLengths() np.testing.assert_allclose(itl, 17.9919846, atol=0.001) iw = rupture.getIndividualWidths() np.testing.assert_allclose(iw, 23.93699668, atol=0.001) lats = rupture.getLats() lats_d = np.array([34.315, 34.401, 34.261, 34.175, 34.315]) np.testing.assert_allclose(lats, lats_d, atol=0.001) lons = rupture.getLons() lons_d = np.array([-118.421, -118.587, -118.693, -118.527, -118.421]) np.testing.assert_allclose(lons, lons_d, atol=0.001)
def test_EdgeRupture(): # Rupture requires an origin even when not used: origin = Origin({'id':'test','lat':0,'lon':0,'depth':5.0,'mag':7.0}) file = 'tests/data/cascadia.json' rup = read_rupture_file(origin, file) # Force read Northridge as EdgeRupture file = 'tests/data/eventdata/northridge/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 = 'tests/data/eventdata/northridge/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)
def test_EdgeRupture(): file = 'tests/data/cascadia.json' casc = read_rupture_file(file)
def parse_complicated_rupture(): rupture_text = """#SOURCE: Barka, A., H. S. Akyz, E. Altunel, G. Sunal, Z. Akir, A. Dikbas, B. Yerli, R. Armijo, B. Meyer, J. B. d. Chabalier, T. Rockwell, J. R. Dolan, R. Hartleb, T. Dawson, S. Christofferson, A. Tucker, T. Fumal, R. Langridge, H. Stenner, W. Lettis, J. Bachhuber, and W. Page (2002). The Surface Rupture and Slip Distribution of the 17 August 1999 Izmit Earthquake (M 7.4), North Anatolian Fault, Bull. Seism. Soc. Am. 92, 43-60. 40.70985 29.33760 0 40.72733 29.51528 0 40.72933 29.51528 20 40.71185 29.33760 20 40.70985 29.33760 0 > 40.70513 29.61152 0 40.74903 29.87519 0 40.75103 29.87519 20 40.70713 29.61152 20 40.70513 29.61152 0 > 40.72582 29.88662 0 40.72336 30.11126 0 40.73432 30.19265 0 40.73632 30.19265 20 40.72536 30.11126 20 40.72782 29.88662 20 40.72582 29.88662 0 > 40.71210 30.30494 0 40.71081 30.46540 0 40.70739 30.56511 0 40.70939 30.56511 20 40.71281 30.46540 20 40.71410 30.30494 20 40.71210 30.30494 0 > 40.71621 30.57658 0 40.70068 30.63731 0 40.70268 30.63731 20 40.71821 30.57658 20 40.71621 30.57658 0 > 40.69947 30.72900 0 40.79654 30.93655 0 40.79854 30.93655 20 40.70147 30.72900 20 40.69947 30.72900 0 > 40.80199 30.94688 0 40.84501 31.01799 0 40.84701 31.01799 20 40.80399 30.94688 20 40.80199 30.94688 0""" cbuf = io.StringIO(rupture_text) rupture = read_rupture_file(cbuf) strike = rupture.getStrike() np.testing.assert_allclose(strike, -100.464330, atol=0.001) dip = rupture.getDip() np.testing.assert_allclose(dip, 89.3985, atol=0.001) L = rupture.getRuptureLength() np.testing.assert_allclose(L, 119.5578, atol=0.001) W = rupture.getWidth() np.testing.assert_allclose(W, 20.001, atol=0.001) nq = rupture.getNumQuads() np.testing.assert_allclose(nq, 9) ns = rupture.getNumSegments() np.testing.assert_allclose(ns, 7) sind = rupture._getSegmentIndex() np.testing.assert_allclose(sind, [0, 1, 2, 2, 3, 3, 4, 5, 6]) ztor = rupture.getTopOfRupture() np.testing.assert_allclose(ztor, 0, atol=0.001) itl = rupture.getIndividualTopLengths() itl_d = np.array([15.13750778, 22.80237887, 18.98053425, 6.98263853, 13.55978731, 8.43444811, 5.41399812, 20.57788056, 7.66869463]) np.testing.assert_allclose(itl, itl_d, atol=0.001) iw = rupture.getIndividualWidths() iw_d = np.array([20.00122876, 20.00122608, 20.00120173, 20.00121028, 20.00121513, 20.00121568, 20.00107293, 20.00105498, 20.00083348]) np.testing.assert_allclose(iw, iw_d, atol=0.001) lats = rupture.getLats() lats_d = np.array([40.70985, 40.72733, 40.72933, 40.71185, 40.70985, np.nan, 40.70513, 40.74903, 40.75103, 40.70713, 40.70513, np.nan, 40.72582, 40.72336, 40.73432, 40.73632, 40.72536, 40.72782, 40.72582, np.nan, 40.7121, 40.71081, 40.70739, 40.70939, 40.71281, 40.7141, 40.7121, np.nan, 40.71621, 40.70068, 40.70268, 40.71821, 40.71621, np.nan, 40.69947, 40.79654, 40.79854, 40.70147, 40.69947, np.nan, 40.80199, 40.84501, 40.84701, 40.80399, 40.80199]) np.testing.assert_allclose(lats, lats_d, atol=0.001) lons = rupture.getLons() lons_d = np.array([29.3376, 29.51528, 29.51528, 29.3376, 29.3376, np.nan, 29.61152, 29.87519, 29.87519, 29.61152, 29.61152, np.nan, 29.88662, 30.11126, 30.19265, 30.19265, 30.11126, 29.88662, 29.88662, np.nan, 30.30494, 30.4654, 30.56511, 30.56511, 30.4654, 30.30494, 30.30494, np.nan, 30.57658, 30.63731, 30.63731, 30.57658, 30.57658, np.nan, 30.729, 30.93655, 30.93655, 30.729, 30.729, np.nan, 30.94688, 31.01799, 31.01799, 30.94688, 30.94688]) np.testing.assert_allclose(lons, lons_d, atol=0.001)
def test_virtualipe(): # # Set up the GMPE, IPE, and GMICE # gmpe_cy14 = ChiouYoungs2014() gmpe = MultiGMPE.from_list([gmpe_cy14], [1.0]) gmice = WGRW12() ipe = VirtualIPE.fromFuncs(gmpe, gmice) # # Use the Calexico event info # homedir = os.path.dirname(os.path.abspath(__file__)) datadir = os.path.abspath(os.path.join(homedir, '..', 'data', 'eventdata', 'Calexico', 'input')) # # Read the event, origin, and rupture files and produce Rupture and Origin # objects # inputfile = os.path.join(datadir, 'stationlist_dat.xml') dyfifile = os.path.join(datadir, 'ciim3_dat.xml') eventfile = os.path.join(datadir, 'event.xml') rupturefile = os.path.join(datadir, 'wei_fault.txt') origin_obj = Origin.fromFile(eventfile) rupture_obj = read_rupture_file(origin_obj, rupturefile) rx = rupture_obj.getRuptureContext([gmpe]) rx.rake = 45. smdx = 0.0083333333 smdy = 0.0083333333 lonspan = 6.0 latspan = 4.0 vs30filename = os.path.join(datadir, '..', 'vs30', 'vs30.grd') sites_obj_grid = Sites.fromCenter( rx.hypo_lon, rx.hypo_lat, lonspan, latspan, smdx, smdy, defaultVs30=760.0, vs30File=vs30filename, vs30measured_grid=None, padding=False, resample=False ) npts = 200 lats = np.empty(npts) lons = np.empty(npts) depths = np.zeros(npts) for i in range(npts): lats[i] = rx.hypo_lat lons[i] = rx.hypo_lon + i * 0.01 lldict = {'lats': lats, 'lons': lons} sx = sites_obj_grid.getSitesContext(lldict=lldict, rock_vs30=760.0) dobj = Distance(gmpe, lons, lats, depths, rupture_obj) dx = dobj.getDistanceContext() sd_types = [oqconst.StdDev.TOTAL] mmi_const_vs30, mmi_sd_const_vs30 = \ ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types) # These prints are just so a human can examine the outputs # print(mmi_const_vs30) # print(mmi_sd_const_vs30) sx = sites_obj_grid.getSitesContext(lldict=lldict) mmi_variable_vs30, mmi_sd_variable_vs30 = \ ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types) # print(mmi_variable_vs30) # print(mmi_sd_variable_vs30) sd_types = [oqconst.StdDev.TOTAL, oqconst.StdDev.INTRA_EVENT, oqconst.StdDev.INTER_EVENT] mmi_variable_vs30_intra, mmi_sd_variable_vs30_intra = \ ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types) # print(mmi_variable_vs30_intra) # print(mmi_sd_variable_vs30_intra) # assert(0) # Assert causes test to fail and prints to be displayed # # Try with PGA # gmpe.DEFINED_FOR_INTENSITY_MEASURE_TYPES.remove(PGV) gmpe.ALL_GMPES_HAVE_PGV = False ipe = VirtualIPE.fromFuncs(gmpe, gmice) mmi_pga, mmi_sd_pga = \ ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types) # # Try with SA(1.0) # gmpe.DEFINED_FOR_INTENSITY_MEASURE_TYPES.remove(PGA) ipe = VirtualIPE.fromFuncs(gmpe, gmice) mmi_psa, mmi_sd_psa = \ ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types) # # This should raise an exception because the IMT isn't MMI # with pytest.raises(ValueError) as e: mmi_psa, mmi_sd_psa = \ ipe.get_mean_and_stddevs(sx, rx, dx, PGA(), sd_types) # # This should raise an exception because no valid IMTs are available # gmpe.DEFINED_FOR_INTENSITY_MEASURE_TYPES.remove(SA) with pytest.raises(ShakeMapException) as e: ipe = VirtualIPE.fromFuncs(gmpe, gmice) # # Now do a GMPE that uses Rjb instead of Rrup # gmpe_ba14 = BooreEtAl2014() gmpe = MultiGMPE.from_list([gmpe_ba14], [1.0]) ipe = VirtualIPE.fromFuncs(gmpe, gmice) rx = rupture_obj.getRuptureContext([gmpe]) rx.rake = 45. dobj = Distance(gmpe, lons, lats, depths, rupture_obj) dx = dobj.getDistanceContext() mmi_rjb, mmi_sd_rjb = \ ipe.get_mean_and_stddevs(sx, rx, dx, MMI(), sd_types) # # Test the results against a known standard # savefile = os.path.abspath(os.path.join(homedir, '..', 'data', 'eventdata', 'Calexico', 'virtualipe_test', 'savefile.npz')) # # If things change, set remake_save to True, and it will rebuild the # saved data file against which the comparisons are done # Remember to set this back to False once you've remade the test datafile # remake_save = False if remake_save: np.savez_compressed(savefile, mmi_const_vs30 = mmi_const_vs30, mmi_sd_const_vs30 = mmi_sd_const_vs30[0], mmi_variable_vs30 = mmi_variable_vs30, mmi_sd_variable_vs30 = mmi_sd_variable_vs30[0], mmi_variable_vs30_intra = mmi_variable_vs30_intra, mmi_sd_variable_vs30_total = mmi_sd_variable_vs30_intra[0], mmi_sd_variable_vs30_intra = mmi_sd_variable_vs30_intra[1], mmi_sd_variable_vs30_inter = mmi_sd_variable_vs30_intra[2], mmi_pga = mmi_pga, mmi_sd_pga = mmi_sd_pga[0], mmi_psa = mmi_psa, mmi_sd_psa = mmi_sd_psa[0], mmi_rjb = mmi_rjb, mmi_sd_rjb = mmi_sd_rjb[0]) td = np.load(savefile) assert(np.allclose(td['mmi_const_vs30'], mmi_const_vs30)) assert(np.allclose(td['mmi_sd_const_vs30'], mmi_sd_const_vs30[0])) assert(np.allclose(td['mmi_variable_vs30'], mmi_variable_vs30)) assert(np.allclose(td['mmi_sd_variable_vs30'], mmi_sd_variable_vs30[0])) assert(np.allclose(td['mmi_variable_vs30_intra'], mmi_variable_vs30_intra)) assert(np.allclose(td['mmi_sd_variable_vs30_total'], mmi_sd_variable_vs30_intra[0])) assert(np.allclose(td['mmi_sd_variable_vs30_intra'], mmi_sd_variable_vs30_intra[1])) assert(np.allclose(td['mmi_sd_variable_vs30_inter'], mmi_sd_variable_vs30_intra[2])) assert(np.allclose(td['mmi_pga'], mmi_pga)) assert(np.allclose(td['mmi_sd_pga'], mmi_sd_pga[0])) assert(np.allclose(td['mmi_psa'], mmi_psa)) assert(np.allclose(td['mmi_sd_psa'], mmi_sd_psa[0])) assert(np.allclose(td['mmi_rjb'], mmi_rjb)) assert(np.allclose(td['mmi_sd_rjb'], mmi_sd_rjb[0])) # The total uncertainties should be greater than the intra-event assert(np.all(mmi_sd_variable_vs30[0] > mmi_sd_variable_vs30_intra[1])) # The combined intra and inter-event uncertainty should be equal # to the total tot = np.sqrt(mmi_sd_variable_vs30_intra[1]**2 + mmi_sd_variable_vs30_intra[2]**2) assert(np.allclose(tot, mmi_sd_variable_vs30_intra[0], rtol=1e-2))