def test_parse_srf_file(tmpdir): """ Tests parsing from a .srf file. """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) assert finitesource.npointsources == 10 longitudes = np.array([0.0, 0.99925, 1.99849, 2.99774, 3.99698, 4.99623, 5.99548, 6.99472, 7.99397, 8.99322]) for isrc, src in enumerate(finitesource): src_params = np.array([src.latitude, src.longitude, src.depth_in_m, src.m_rr, src.m_tt, src.m_pp, src.m_rt, src.m_rp, src.m_tp], dtype="float64") src_params_ref = np.array([ 0.00000000e+00, longitudes[isrc], 5.00000000e+04, 0.00000000e+00, -3.91886976e+03, 3.91886976e+03, -1.19980783e-13, 1.95943488e+03, 3.20000000e+19]) np.testing.assert_allclose(src_params, src_params_ref) # Try parsing it again, but this time with a couple of empty lines in # front of it. filename = os.path.join(tmpdir.strpath, "temp.srf") with io.open(SRF_FILE, "rt") as fh_1, io.open(filename, "wt") as fh_2: fh_2.write(u"\n\n\n\n") fh_2.write(fh_1.read()) finitesource_2 = FiniteSource.from_srf_file(filename, True) assert finitesource_2.npointsources == 10
def test_parse_usgs_param_file_from_bytes_io_and_open_files(): """ Tests parsing a USGS file from a BytesIO stream and open files.. """ with io.open(USGS_PARAM_FILE1, "rb") as fh: finitesource = FiniteSource.from_usgs_param_file(fh) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.9374427577095901) assert finitesource.npointsources == 121 with io.open(USGS_PARAM_FILE1, "rb") as fh: with io.BytesIO(fh.read()) as buf: finitesource = FiniteSource.from_usgs_param_file(buf) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.9374427577095901) assert finitesource.npointsources == 121 with io.open(USGS_PARAM_FILE2, "rb") as fh: finitesource = FiniteSource.from_usgs_param_file(fh) np.testing.assert_almost_equal(finitesource.moment_magnitude, 8.26413197488) assert finitesource.npointsources == 400 with io.open(USGS_PARAM_FILE2, "rb") as fh: with io.BytesIO(fh.read()) as buf: finitesource = FiniteSource.from_usgs_param_file(buf) np.testing.assert_almost_equal(finitesource.moment_magnitude, 8.26413197488) assert finitesource.npointsources == 400
def on_open_srf_file_button_released(self): pwd = os.getcwd() self.finite_src_file = str( QtGui.QFileDialog.getOpenFileName( self, "Choose *.srf or *.param File", pwd, "Standard Rupture Format (*.srf);;" "USGS finite source files (*.param)", )) if not self.finite_src_file: return if self.finite_src_file.endswith(".srf"): self.finite_source = FiniteSource.from_srf_file( self.finite_src_file, normalize=True) elif self.finite_src_file.endswith(".param"): self.finite_source = FiniteSource.from_usgs_param_file( self.finite_src_file) else: raise IOError("unknown file type *.%s" % self.finite_src_file.split(".")[-1]) self._setup_finite_source() self.update() self.set_info()
def test_parse_usgs_param_file_from_bytes_io_and_open_files(): """ Tests parsing a USGS file from a BytesIO stream and open files.. """ with io.open(USGS_PARAM_FILE1, "rb") as fh: finitesource = FiniteSource.from_usgs_param_file(fh) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.8707760910429236) assert finitesource.npointsources == 121 with io.open(USGS_PARAM_FILE1, "rb") as fh: with io.BytesIO(fh.read()) as buf: finitesource = FiniteSource.from_usgs_param_file(buf) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.87077609) assert finitesource.npointsources == 121 with io.open(USGS_PARAM_FILE2, "rb") as fh: finitesource = FiniteSource.from_usgs_param_file(fh) np.testing.assert_almost_equal(finitesource.moment_magnitude, 8.1974653082088) assert finitesource.npointsources == 400 with io.open(USGS_PARAM_FILE2, "rb") as fh: with io.BytesIO(fh.read()) as buf: finitesource = FiniteSource.from_usgs_param_file(buf) np.testing.assert_almost_equal(finitesource.moment_magnitude, 8.19746530820887) assert finitesource.npointsources == 400
def test_haskell(): """ Tests Haskell source. """ latitude, longitude, depth_in_m = 89.9999, 0., 10000. strike, dip, rake = 90., 90., 0. M0 = 1e20 # NOQA fault_length, fault_width = 1000e3, 200. rupture_velocity = 1000. nl, nw = 3, 3 finitesource = FiniteSource.from_Haskell( latitude, longitude, depth_in_m, strike, dip, rake, M0, fault_length, fault_width, rupture_velocity, nl=nl, nw=nw) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.26666666666) # Should raise an error if above ground. with pytest.raises(ValueError) as err: FiniteSource.from_Haskell( latitude, longitude, -100.0, strike, dip, rake, M0, fault_length, fault_width, rupture_velocity, nl=nl, nw=nw) assert err.value.args[0].startswith("At least one source point outside") # Manually settings trise and tfall. finitesource = FiniteSource.from_Haskell( latitude, longitude, depth_in_m, strike, dip, rake, M0, fault_length, fault_width, rupture_velocity, nl=nl, nw=nw, trise=1.0, tfall=1.0) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.26666666666666666)
def test_Haskell(): """ Tests Haskell source. """ latitude, longitude, depth_in_m = 89.9999, 0., 10000. strike, dip, rake = 90., 90., 0. M0 = 1e20 fault_length, fault_width = 1000e3, 200. rupture_velocity = 1000. nl, nw = 3, 3 finitesource = FiniteSource.from_Haskell( latitude, longitude, depth_in_m, strike, dip, rake, M0, fault_length, fault_width, rupture_velocity, nl=nl, nw=nw) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.33333333333333) # Should raise an error if above ground. with pytest.raises(ValueError) as err: FiniteSource.from_Haskell( latitude, longitude, -100.0, strike, dip, rake, M0, fault_length, fault_width, rupture_velocity, nl=nl, nw=nw) assert err.value.args[0].startswith("At least one source point outside") # Manually settings trise and tfall. finitesource = FiniteSource.from_Haskell( latitude, longitude, depth_in_m, strike, dip, rake, M0, fault_length, fault_width, rupture_velocity, nl=nl, nw=nw, trise=1.0, tfall=1.0) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.33333333333333)
def test_parsing_empty_usgs_file(): """ Parsing an empty USGS file should fail. """ with pytest.raises(USGSParamFileParsingException) as e: FiniteSource.from_usgs_param_file(USGS_PARAM_FILE_EMPTY) assert e.value.args[0] == 'No point sources found in the file.'
def test_parse_usgs_param_file(): """ Tests parsing from a .param file. """ # single segment file finitesource = FiniteSource.from_usgs_param_file(USGS_PARAM_FILE1) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.87077609) assert finitesource.npointsources == 121 # multi segment file finitesource = FiniteSource.from_usgs_param_file(USGS_PARAM_FILE2) np.testing.assert_almost_equal(finitesource.moment_magnitude, 8.1974653082088) assert finitesource.npointsources == 400
def test_parse_usgs_param_file(): """ Tests parsing from a .param file. """ # single segment file finitesource = FiniteSource.from_usgs_param_file(USGS_PARAM_FILE1) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.9374427577095901) assert finitesource.npointsources == 121 # multi segment file finitesource = FiniteSource.from_usgs_param_file(USGS_PARAM_FILE2) np.testing.assert_almost_equal(finitesource.moment_magnitude, 8.26413197488) assert finitesource.npointsources == 400
def test_reading_finite_source_with_slip_along_u2_axis(): """ Tests SRF files with slips along the u2 axis with a constructed file. """ # Constructed file with known properties. filename = os.path.join(DATA, "strike_slip_eq_2pts.srf") finitesource = FiniteSource.from_srf_file(filename, True) assert finitesource.npointsources == 3
def test_M0_finite_source(): """ Tests computation of scalar Moment. """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) finitesource.find_hypocenter() np.testing.assert_allclose(np.array([finitesource.M0]), np.array([3.2e20]))
def test_m0_finite_source(): """ Tests computation of scalar Moment. """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) finitesource.find_hypocenter() np.testing.assert_allclose(np.array([finitesource.M0]), np.array([3.2e20]))
def test_finite_source_iteration_over_empty_fs(): """ Raises a ValueError when trying to iterate over an empty finite source object. This is safe-guard against obvious errors so in this particular case its better to be explicit instead of just not looping. """ fs = FiniteSource() with pytest.raises(ValueError) as err: [_i for _i in fs] assert err.value.args[0] == "FiniteSource not Initialized"
def on_open_srf_file_button_released(self): pwd = os.getcwd() self.finite_src_file = str(QtGui.QFileDialog.getOpenFileName( self, "Choose *.srf or *.param File", pwd, "Standard Rupture Format (*.srf);;" "USGS finite source files (*.param)")) if not self.finite_src_file: return if self.finite_src_file.endswith('.srf'): self.finite_source = FiniteSource.from_srf_file( self.finite_src_file, normalize=True) elif self.finite_src_file.endswith('.param'): self.finite_source = FiniteSource.from_usgs_param_file( self.finite_src_file) else: raise IOError('unknown file type *.%s' % self.finite_src_file.split('.')[-1]) self._setup_finite_source() self.update() self.set_info()
def test_hypocenter(): """ Tests finding the hypocenter """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) finitesource.find_hypocenter() assert finitesource.hypocenter_longitude == 0.0 assert finitesource.hypocenter_latitude == 0.0 assert finitesource.hypocenter_depth_in_m == 50e3 assert finitesource.epicenter_longitude == 0.0 assert finitesource.epicenter_latitude == 0.0
def on_open_srf_file_button_released(self): self.srf_file = str(QtGui.QFileDialog.getOpenFileName( self, "Choose *.srf File", os.path.expanduser("~"))) if not self.srf_file: return self.finite_source = FiniteSource.from_srf_file(self.srf_file) if self.instaseis_db is not None: self.finite_source.resample_sliprate( dt=self.instaseis_db.dt, nsamp=self.instaseis_db.ndumps) self.update() self.set_info()
def test_resample_stf(): """ Tests resampling sliprates """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) finitesource.resample_sliprate(dt=1., nsamp=10) stf_ref = np.array([ 0.00000000e+00, 1.60000000e-05, 3.20000000e-05, 4.80000000e-05, 6.40000000e-05, 8.00000000e-05, 1.84000000e-04, 2.88000000e-04, 3.92000000e-04, 4.96000000e-04]) for isrc, src in enumerate(finitesource): np.testing.assert_allclose(stf_ref, src.sliprate)
def test_min_max_functions(): """ Tests the min/max convenience functions """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) finitesource.find_hypocenter() assert finitesource.min_depth_in_m == 50e3 assert finitesource.max_depth_in_m == 50e3 assert finitesource.min_longitude == 0.0 assert finitesource.max_longitude == 8.99322 assert finitesource.min_latitude == 0.0 assert finitesource.max_latitude == 0.0
def test_Haskell(): """ Tests Haskell source. """ latitude, longitude, depth_in_m = 89.9999, 0., 10000. strike, dip, rake = 90., 90., 0. M0 = 1e20 fault_length, fault_width = 1000e3, 200. rupture_velocity = 1000. nl, nw = 3, 3 finitesource = FiniteSource.from_Haskell( latitude, longitude, depth_in_m, strike, dip, rake, M0, fault_length, fault_width, rupture_velocity, nl=nl, nw=nw) np.testing.assert_almost_equal(finitesource.moment_magnitude, 7.33333333333333)
def test_CMT_finite_source(): """ Tests computation of CMT solution """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) finitesource.compute_centroid() np.testing.assert_allclose( np.array([-3.91886976e+04, 3.90905071e+04, 9.81905182e+01, 1.94225428e+04, -4.09600000e+03, 3.19598660e+20]), finitesource.CMT.tensor_voigt) np.testing.assert_allclose(np.array([finitesource.CMT.latitude]), np.array([0.0])) np.testing.assert_allclose(np.array([finitesource.CMT.longitude]), np.array([4.496608]))
def test_cmt_finite_source(): """ Tests computation of CMT solution """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) finitesource.compute_centroid() np.testing.assert_allclose( np.array([-3.918870e+04, 3.909051e+04, 9.819052e+01, 1.942254e+04, -5.476000e+03, 3.195987e+20]), finitesource.CMT.tensor_voigt, rtol=1E-5) np.testing.assert_allclose(np.array([finitesource.CMT.latitude]), np.array([0.0])) np.testing.assert_allclose(np.array([finitesource.CMT.longitude]), np.array([4.496608]))
def test_CMT_finite_source(): """ Tests computation of CMT solution """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) finitesource.compute_centroid() np.testing.assert_allclose( np.array([-3.918870e+04, 3.909051e+04, 9.819052e+01, 1.942254e+04, -5.476000e+03, 3.195987e+20]), finitesource.CMT.tensor_voigt, rtol=1E-5) np.testing.assert_allclose(np.array([finitesource.CMT.latitude]), np.array([0.0])) np.testing.assert_allclose(np.array([finitesource.CMT.longitude]), np.array([4.496608]))
def test_sliprate_convenience_methods_finite_source(): """ Tests some convenience methods of sliprates for finite sources. """ src = Source(latitude=0.0, longitude=90.0) fs = FiniteSource(pointsources=[src]) fs.set_sliprate_dirac(2.0, 5) np.testing.assert_allclose(np.array([0.5, 0, 0, 0, 0]), src.sliprate) src = Source(latitude=0.0, longitude=90.0) fs = FiniteSource(pointsources=[src]) fs.set_sliprate_lp(2.0, 5, 0.1) np.testing.assert_allclose(np.array( [0.023291, 0.111382, 0.211022, 0.186723, 0.045481]), src.sliprate, rtol=1E-3) src = Source(latitude=0.0, longitude=90.0) src.sliprate = np.ones(5) src.dt = 0.25 fs = FiniteSource(pointsources=[src]) fs.normalize_sliprate() np.testing.assert_allclose(np.ones(5), src.sliprate)
def test_str_method_of_finite_source(): finitesource = FiniteSource.from_srf_file(SRF_FILE, True) assert str(finitesource) == ("Instaseis Finite Source:\n" "\tMoment Magnitude : 7.67\n" "\tScalar Moment : 3.20e+20 Nm\n" "\t#Point Sources : 10\n" "\tRupture Duration : 222.2 s\n" "\tTime Shift : 0.0 s\n" "\tMin Depth : 50000.0 m\n" "\tMax Depth : 50000.0 m\n" "\tHypocenter Depth : 50000.0 m\n" "\tMin Latitude : 0.0 deg\n" "\tMax Latitude : 0.0 deg\n" "\tHypocenter Latitude : 0.0 deg\n" "\tMin Longitude : 0.0 deg\n" "\tMax Longitude : 9.0 deg\n" "\tHypocenter Longitude : 0.0 deg\n")
def test_min_max_functions(): """ Tests the min/max convenience functions """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) finitesource.find_hypocenter() assert finitesource.min_depth_in_m == 50e3 assert finitesource.max_depth_in_m == 50e3 assert finitesource.min_longitude == 0.0 assert finitesource.max_longitude == 8.99322 assert finitesource.min_latitude == 0.0 assert finitesource.max_latitude == 0.0 assert finitesource.rupture_duration == 222.22222
def test_str_method_of_finite_source(): finitesource = FiniteSource.from_srf_file(SRF_FILE, True) assert str(finitesource) == ( "Instaseis Finite Source:\n" "\tMoment Magnitude : 7.60\n" "\tScalar Moment : 3.20e+20 Nm\n" "\t#Point Sources : 10\n" "\tRupture Duration : 222.2 s\n" "\tTime Shift : 0.0 s\n" "\tMin Depth : 50000.0 m\n" "\tMax Depth : 50000.0 m\n" "\tHypocenter Depth : 50000.0 m\n" "\tMin Latitude : 0.0 deg\n" "\tMax Latitude : 0.0 deg\n" "\tHypocenter Latitude : 0.0 deg\n" "\tMin Longitude : 0.0 deg\n" "\tMax Longitude : 9.0 deg\n" "\tHypocenter Longitude : 0.0 deg\n" )
def test_parse_srf_file(): """ Tests parsing from a .srf file. """ finitesource = FiniteSource.from_srf_file(SRF_FILE, True) assert finitesource.npointsources == 10 longitudes = np.array([0.0, 0.99925, 1.99849, 2.99774, 3.99698, 4.99623, 5.99548, 6.99472, 7.99397, 8.99322]) for isrc, src in enumerate(finitesource): src_params = np.array([src.latitude, src.longitude, src.depth_in_m, src.m_rr, src.m_tt, src.m_pp, src.m_rt, src.m_rp, src.m_tp], dtype="float64") src_params_ref = np.array([ 0.00000000e+00, longitudes[isrc], 5.00000000e+04, 0.00000000e+00, -3.91886976e+03, 3.91886976e+03, -1.19980783e-13, 1.95943488e+03, 3.20000000e+19]) np.testing.assert_allclose(src_params, src_params_ref)
from kesmarag.spf import SinglePlanarFault from instaseis import FiniteSource top_center = (38.0, 20.0, 3.0) dims = (30, 20) angles = (50, 60) ngrid = (60, 40) hyp_idx = (24, 14) vr = 2.4 dt = 0.01 mu = [(30, 20), (33, 17)] sigma = [2.0, 1.0] dmax = [10.0, 5.0] rake = [50.0, 40.0] t_acc = [0.1, 0.2] t_eff = [1.2, 1.0] fault = SinglePlanarFault(top_center, dims, angles, ngrid, hyp_idx, vr, dt, mu, sigma, dmax, rake, t_acc, t_eff) fault.animation_slip_velocity('./animations/sim_earthquake_1.mp4', 700) fault.create_srf('./srf_files/sim_earthquake_1.srf') source = FiniteSource.from_srf_file('./srf_files/sim_earthquake_1.srf') print(source)