def test_source(): fault_text = """30.979788 103.454422 1 31.691615 104.419160 1 31.723569 104.374760 1 32.532213 105.220821 1 32.641450 105.135050 20 31.846790 104.246202 20 31.942158 104.205286 20 31.290105 103.284388 20 30.979788 103.454422 1""" event_text = """<?xml version="1.0" encoding="US-ASCII" standalone="yes"?> <earthquake id="2008ryan" lat="30.9858" lon="103.3639" mag="7.9" year="2008" month="05" day="12" hour="06" minute="28" second="01" timezone="GMT" depth="19.0" locstring="EASTERN SICHUAN, CHINA" created="1211173621" otime="1210573681" type="" /> """ source_text = "mech=RS" ffile = io.StringIO(fault_text) efile = io.StringIO(event_text) sfile = io.StringIO(source_text) source = Source.fromFile(efile, faultfile=ffile, sourcefile=sfile) gmpe = abrahamson_2014.AbrahamsonEtAl2014() rupture = source.getRuptureContext([gmpe]) testdict = { "mag": 7.9, "strike": -133.083550974, "dip": 49.8524115024, "rake": 45.0, "ztor": 0.999999999995, "hypo_lon": 103.3639, "hypo_lat": 30.9858, "hypo_depth": 19.0, "width": 27.8623813381, } for key in testdict.keys(): value = eval("rupture.%s" % key) np.testing.assert_almost_equal(testdict[key], value) mech = "RS" exp_dip = 40 exp_rake = 90 source.setMechanism(mech) assert source.getEventParam("dip") == exp_dip assert source.getEventParam("rake") == exp_rake source.setMechanism("ALL", dip=45, rake=315) assert source.getEventParam("rake") == -45 # this should raise an exception with pytest.raises(Exception) as e_info: source.setMechanism("ALL", dip=110) with pytest.raises(Exception) as e_info: source.setMechanism("ALL", rake=620)
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, source, and fault files and produce a Source object # inputfile = os.path.join(datadir, "stationlist_dat.xml") dyfifile = os.path.join(datadir, "ciim3_dat.xml") eventfile = os.path.join(datadir, "event.xml") faultfile = os.path.join(datadir, "wei_fault.txt") source_obj = Source.fromFile(eventfile, faultfile=faultfile) rx = source_obj.getRuptureContext([gmpe]) 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, source_obj, lats, lons, depths) 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 = source_obj.getRuptureContext([gmpe]) dobj = Distance(gmpe, source_obj, lats, lons, depths) 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)
def test_station(tmpdir): homedir = os.path.dirname(os.path.abspath(__file__)) datadir = os.path.abspath(os.path.join(homedir, '..', 'data', 'eventdata', 'Calexico', 'input')) # # Read the event, source, and rupture files and produce a Source object # 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') source_obj = Source.fromFile(eventfile, rupturefile=rupturefile) # # Set up the GMPE, IPE, and GMICE # gmpe_cy14 = ChiouYoungs2014() gmpe = MultiGMPE.from_list([gmpe_cy14], [1.0]) gmice = WGRW12() ipe = AllenEtAl2012() # # # rupture_ctx = source_obj.getRuptureContext([gmpe]) 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( rupture_ctx.hypo_lon, rupture_ctx.hypo_lat, lonspan, latspan, smdx, smdy, defaultVs30=760.0, vs30File=vs30filename, vs30measured_grid=None, padding=False, resample=False ) xmlfiles = [inputfile, dyfifile] # dbfile = str(tmpdir.join('stations.db')) dbfile = os.path.join(str(tmpdir), 'stations.db') stations = StationList.fromXML(xmlfiles, dbfile, source_obj, sites_obj_grid, gmpe, ipe, gmice) df1 = stations.getStationDataframe(1, sort=True) df2 = stations.getStationDataframe(0, sort=True) # # In case the test starts failing because of some minor change # in one of the prediction or conversion equations (or roundoff # or whatever), but the code is running correctly, uncomment # these lines and re-run the test. Then, copy the new stations.db # file into tests/data/eventdata/Calexico/database/. Then # recomment these lines and rerun the test. It should succeed. # #shutil.copy(dbfile,'./stations.db') #print(os.getcwd()) # # We should probably check these dataframes against some established # set, and also check the database against a known database. # ref_dbfile = os.path.join(datadir, '..', 'database', 'stations.db') stations2 = StationList(ref_dbfile) ref_df1 = stations2.getStationDataframe(1, sort=True) ref_df2 = stations2.getStationDataframe(0, sort=True) # assert ref_df1.equals(df1) # assert ref_df2.equals(df2) pdt.assert_frame_equal(df1, ref_df1) pdt.assert_frame_equal(df2, ref_df2)