def ttak135pf(self, phase_list=['P','Pn','Pg','p']): # travel time of first P arrivals with AK135 model model = TauPyModel(model="ak135") if self.evdp>0 and self.evdp<1.4: self.evdp = 0 arrivals = model.get_travel_times(self.evdp, self.gcarc, phase_list=phase_list) arr = arrivals[0] # self.phase = arr.name self.tak135 = arr.time
def ttak135sf(self, phase_list=['S','Sn','Sg','s']): # travel time of first S arrivals with AK135 model model = TauPyModel(model="ak135") if self.evdp>0 and self.evdp<1.4: self.evdp = 0 arrivals = model.get_travel_times(self.evdp, self.gcarc, phase_list=phase_list) # print len(arrivals), self.date, self.time, self.stla, self.stlo, self.evla, self.evlo arr = arrivals[0] # self.phase = arr.name self.tak135 = arr.time
def ttak135pf( self, phase_list=['P', 'Pn', 'Pg', 'p']): # travel time of first P arrivals with AK135 model model = TauPyModel(model="ak135") if self.evdp > 0 and self.evdp < 1.4: self.evdp = 0 arrivals = model.get_travel_times(self.evdp, self.gcarc, phase_list=phase_list) arr = arrivals[0] # self.phase = arr.name self.tak135 = arr.time
def ttak135sf( self, phase_list=['S', 'Sn', 'Sg', 's']): # travel time of first S arrivals with AK135 model model = TauPyModel(model="ak135") if self.evdp > 0 and self.evdp < 1.4: self.evdp = 0 arrivals = model.get_travel_times(self.evdp, self.gcarc, phase_list=phase_list) # print len(arrivals), self.date, self.time, self.stla, self.stlo, self.evla, self.evlo arr = arrivals[0] # self.phase = arr.name self.tak135 = arr.time
class TaupGeoTestCase(unittest.TestCase): """ Test suite for the SeismicPhase class. """ def setUp(self): self.model = TauPyModel('iasp91') @unittest.skipIf(not geodetics.GEOGRAPHICLIB_VERSION_AT_LEAST_1_34, 'Module geographiclib is not installed or too old.') def test_path_geo(self): evlat, evlon = 0., 20. evdepth = 10. stlat, stlon = 0., -80. arrivals = self.model.get_ray_paths_geo(evdepth, evlat, evlon, stlat, stlon) for arr in arrivals: stlat_path = arr.path['lat'][-1] stlon_path = arr.path['lon'][-1] self.assertAlmostEqual(stlat, stlat_path, delta=0.1) self.assertAlmostEqual(stlon, stlon_path, delta=0.1)
def setUp(self): self.model = TauPyModel('iasp91')
(http://www.gnu.org/copyleft/lesser.html) """ from __future__ import (absolute_import, division, print_function) # Basic packages import os import logging import re from pyproj import Geod from obspy.taup.tau import TauPyModel from urllib.parse import urlencode LOGGER = logging.getLogger(__name__) GEOD = Geod(ellps='WGS84') TAUP = TauPyModel() # Rough meters/degree calculation M_PER_DEG = (GEOD.inv(0, 0, 0, 1)[2] + GEOD.inv(0, 0, 1, 0)[2]) / 2 class OutputFormat(object): """ Simple output format definition. """ def __init__(self, value, label=None, extension=None): #: This is the name used by ObsPy, which we treat as the "real" value self.value = value #: This is the label used in the UI, it defaults to value self.label = label or value #: This is the file extension, it defaults to lowercased value
class TaupGeoTestCase(unittest.TestCase): """ Test suite for the SeismicPhase class. """ def setUp(self): self.model = TauPyModel('iasp91') @unittest.skipIf(not geodetics.GEOGRAPHICLIB_VERSION_AT_LEAST_1_34, 'Module geographiclib is not installed or too old.') def test_path_geo(self): evlat, evlon = 0., 20. evdepth = 10. stlat, stlon = 0., -80. arrivals = self.model.get_ray_paths_geo(evdepth, evlat, evlon, stlat, stlon) for arr in arrivals: stlat_path = arr.path['lat'][-1] stlon_path = arr.path['lon'][-1] self.assertAlmostEqual(stlat, stlat_path, delta=0.1) self.assertAlmostEqual(stlon, stlon_path, delta=0.1) @unittest.skipIf(not geodetics.GEOGRAPHICLIB_VERSION_AT_LEAST_1_34, 'Module geographiclib is not installed or too old.') def test_path_resampling(self): """ Test resampling of paths in regions that are pretty much straight in geographical coordinates and therefore only coarsely sampled by taup """ kwargs = dict(source_depth_in_km=50, source_latitude_in_deg=0., source_longitude_in_deg=0., receiver_latitude_in_deg=0., receiver_longitude_in_deg=150, phase_list=('Pdiff', )) model = self.model default = model.get_ray_paths_geo(resample=False, **kwargs)[0].path resampled = model.get_ray_paths_geo(resample=True, **kwargs)[0].path self.assertEqual(len(default), 397) self.assertEqual(len(resampled), 416) # start and end of path are unchanged expected = [ (254.3313, 0., 0.00000000e+00, 50., 0., 0.), (254.3313, 0.122393, 5.03916e-05, 50.931307, 0., 0.00288723), (254.3313, 0.820524, 3.38136e-04, 56.243394, 0., 0.01937378), (254.3313, 2.217176, 9.15375e-04, 66.870045, 0., 0.05244716), (254.3313, 3.614348, 1.49496e-03, 77.5, 0., 0.08565492)] np.testing.assert_allclose(default[:5].tolist(), expected, rtol=1e-5) np.testing.assert_allclose(resampled[:5].tolist(), expected, rtol=1e-5) expected = [(254.33137581441554, 1040.4579085173295, 2.617806788150977, 5.004051595465171, 0.0, 149.98928054174857), (254.33137581441554, 1040.9008781834862, 2.6179002698779987, 2.5047268613761844, 0.0, 149.99463665016856), (254.33137581441554, 1041.1223593199215, 2.6179469824519237, 1.2550644943312363, 0.0, 149.99731308350465), (254.33137581441554, 1041.3438379939714, 2.617993676181489, 0.005402127286288305, 0.0, 149.99998843713843), (254.33137581441554, 1041.3447954120252, 2.617993877991496, 0.0, 0.0, 150.0000000000001)] np.testing.assert_allclose(default[-5:].tolist(), expected, rtol=1e-5) np.testing.assert_allclose(resampled[-5:].tolist(), expected, rtol=1e-5) # in the middle the paths differ expected_resampled = [ (254.33137581441554, 602.8221186084121, 94.33432576321066, 2889.0, 0.0, 94.33432576321066), (254.33137581441554, 617.1726077606227, 97.56720436396148, 2889.0, 0.0, 97.56720436396148), (254.33137581441554, 631.5230969128334, 1.759293334017677, 2889.0, 0.0, 100.80008296471232), (254.33137581441554, 643.4514604207692, 103.4785176520484, 2880.9095668072578, 0.0, 103.4785176520484), (254.33137581441554, 655.379823928705, 1.8527883422049563, 2872.8191336145155, 0.0, 106.15695233938449), (254.33137581441554, 664.4057532502989, 108.1520313009955, 2856.0745668072577, 0.0, 108.1520313009955), (254.33137581441554, 673.4316825718927, 1.9224297356397198, 2839.33, 0.0, 110.14711026260652), (254.33137581441554, 688.0227541830799, 1.9774741001400498, 2799.2895131996584, 0.0, 113.30092003445516), (254.33137581441554, 691.0197687282376, 1.9886081210291868, 2789.67, 0.0, 113.93885244041321), (254.33137581441554, 692.3359202082614, 1.9934773547795543, 2785.297917906318, 0.0, 114.21783898377193)] expected_default = [ (254.33137581441554, 740.555911561264, 2.160089140534382, 2560.645140025988, 0.0, 123.76399112466144), (254.33137581441554, 743.6578548789879, 2.1698423363329122, 2542.0, 0.0, 124.32280808068195), (254.33137581441554, 751.5598436136655, 2.194109696034333, 2492.5, 0.0, 125.71322537149922), (254.33137581441554, 752.3706892487971, 2.196552592897761, 2487.2632826174126, 0.0, 125.8531930515594), (254.33137581441554, 753.7391549563343, 2.200655453715524, 2478.3607118178234, 0.0, 126.08826966034681), (254.33137581441554, 759.0587464034165, 2.2163649120816844, 2443.0, 0.0, 126.98835532316427), (254.33137581441554, 763.4244896464312, 2.2289727423907655, 2413.1126874221954, 0.0, 127.71073078869175), (254.33137581441554, 766.2313256113371, 2.2369430801261303, 2393.5, 0.0, 128.167397502222), (254.33137581441554, 773.1340678380197, 2.256093920089646, 2344.0, 0.0, 129.26465980626193), (254.33137581441554, 773.9286888315412, 2.258257579541221, 2338.1899362389727, 0.0, 129.38862836114077)] np.testing.assert_allclose(default[205:215].tolist(), expected_default, rtol=1e-5) np.testing.assert_allclose(resampled[205:215].tolist(), expected_resampled, rtol=1e-5)
class TaupGeoTestCase(unittest.TestCase): """ Test suite for the SeismicPhase class. """ def setUp(self): self.model = TauPyModel('iasp91') @unittest.skipIf(not geodetics.GEOGRAPHICLIB_VERSION_AT_LEAST_1_34, 'Module geographiclib is not installed or too old.') def test_path_geo(self): evlat, evlon = 0., 20. evdepth = 10. stlat, stlon = 0., -80. arrivals = self.model.get_ray_paths_geo(evdepth, evlat, evlon, stlat, stlon) for arr in arrivals: stlat_path = arr.path['lat'][-1] stlon_path = arr.path['lon'][-1] self.assertAlmostEqual(stlat, stlat_path, delta=0.1) self.assertAlmostEqual(stlon, stlon_path, delta=0.1) @unittest.skipIf(not geodetics.GEOGRAPHICLIB_VERSION_AT_LEAST_1_34, 'Module geographiclib is not installed or too old.') def test_path_resampling(self): """ Test resampling of paths in regions that are pretty much straight in geographical coordinates and therefore only coarsely sampled by taup """ kwargs = dict(source_depth_in_km=50, source_latitude_in_deg=0., source_longitude_in_deg=0., receiver_latitude_in_deg=0., receiver_longitude_in_deg=150, phase_list=('Pdiff', )) model = self.model default = model.get_ray_paths_geo(resample=False, **kwargs)[0].path resampled = model.get_ray_paths_geo(resample=True, **kwargs)[0].path self.assertEqual(len(default), 397) self.assertEqual(len(resampled), 416) # start and end of path are unchanged expected = [ (254.3313, 0., 0.00000000e+00, 50., 0., 0.), (254.3313, 0.122393, 5.03916e-05, 50.931307, 0., 0.00288723), (254.3313, 0.820524, 3.38136e-04, 56.243394, 0., 0.01937378), (254.3313, 2.217176, 9.15375e-04, 66.870045, 0., 0.05244716), (254.3313, 3.614348, 1.49496e-03, 77.5, 0., 0.08565492) ] np.testing.assert_allclose(default[:5].tolist(), expected, rtol=1e-5) np.testing.assert_allclose(resampled[:5].tolist(), expected, rtol=1e-5) expected = [ (254.33137581441554, 1040.4579085173295, 2.617806788150977, 5.004051595465171, 0.0, 149.98928054174857), (254.33137581441554, 1040.9008781834862, 2.6179002698779987, 2.5047268613761844, 0.0, 149.99463665016856), (254.33137581441554, 1041.1223593199215, 2.6179469824519237, 1.2550644943312363, 0.0, 149.99731308350465), (254.33137581441554, 1041.3438379939714, 2.617993676181489, 0.005402127286288305, 0.0, 149.99998843713843), (254.33137581441554, 1041.3447954120252, 2.617993877991496, 0.0, 0.0, 150.0000000000001) ] np.testing.assert_allclose(default[-5:].tolist(), expected, rtol=1e-5) np.testing.assert_allclose(resampled[-5:].tolist(), expected, rtol=1e-5) # in the middle the paths differ expected_resampled = [ (254.33137581441554, 602.8221186084121, 94.33432576321066, 2889.0, 0.0, 94.33432576321066), (254.33137581441554, 617.1726077606227, 97.56720436396148, 2889.0, 0.0, 97.56720436396148), (254.33137581441554, 631.5230969128334, 1.759293334017677, 2889.0, 0.0, 100.80008296471232), (254.33137581441554, 643.4514604207692, 103.4785176520484, 2880.9095668072578, 0.0, 103.4785176520484), (254.33137581441554, 655.379823928705, 1.8527883422049563, 2872.8191336145155, 0.0, 106.15695233938449), (254.33137581441554, 664.4057532502989, 108.1520313009955, 2856.0745668072577, 0.0, 108.1520313009955), (254.33137581441554, 673.4316825718927, 1.9224297356397198, 2839.33, 0.0, 110.14711026260652), (254.33137581441554, 688.0227541830799, 1.9774741001400498, 2799.2895131996584, 0.0, 113.30092003445516), (254.33137581441554, 691.0197687282376, 1.9886081210291868, 2789.67, 0.0, 113.93885244041321), (254.33137581441554, 692.3359202082614, 1.9934773547795543, 2785.297917906318, 0.0, 114.21783898377193) ] expected_default = [ (254.33137581441554, 740.555911561264, 2.160089140534382, 2560.645140025988, 0.0, 123.76399112466144), (254.33137581441554, 743.6578548789879, 2.1698423363329122, 2542.0, 0.0, 124.32280808068195), (254.33137581441554, 751.5598436136655, 2.194109696034333, 2492.5, 0.0, 125.71322537149922), (254.33137581441554, 752.3706892487971, 2.196552592897761, 2487.2632826174126, 0.0, 125.8531930515594), (254.33137581441554, 753.7391549563343, 2.200655453715524, 2478.3607118178234, 0.0, 126.08826966034681), (254.33137581441554, 759.0587464034165, 2.2163649120816844, 2443.0, 0.0, 126.98835532316427), (254.33137581441554, 763.4244896464312, 2.2289727423907655, 2413.1126874221954, 0.0, 127.71073078869175), (254.33137581441554, 766.2313256113371, 2.2369430801261303, 2393.5, 0.0, 128.167397502222), (254.33137581441554, 773.1340678380197, 2.256093920089646, 2344.0, 0.0, 129.26465980626193), (254.33137581441554, 773.9286888315412, 2.258257579541221, 2338.1899362389727, 0.0, 129.38862836114077) ] np.testing.assert_allclose(default[205:215].tolist(), expected_default, rtol=1e-5) np.testing.assert_allclose(resampled[205:215].tolist(), expected_resampled, rtol=1e-5)
print("Station ", ref_sta, " found in inventory ", invfile) break else: print("Warning no data found in ", invfile, " for station", ref_sta) continue eve_lat = eve_coord[0] eve_lon = eve_coord[1] eve_dep = eve_coord[2] # print "sta_lon, sta_lat, eve_lon, eve_lat==", sta_lon, # sta_lat, eve_lon, eve_lat epi_dist, az, baz = gps2dist_azimuth(eve_lat, eve_lon, slat, slon) epi_dist = epi_dist / 1000 deg = kilometer2degrees(epi_dist) model = TauPyModel(model=taup_model) arrivals = model.get_travel_times( source_depth_in_km=eve_dep, distance_in_degree=deg, phase_list=["s", "S"], ) print(arrivals) # arrP = arrivals[0] arrS = arrivals[0] # correction to be used to evaluate the origin time of event origin_time_shift = arrS.time - half_time print("OT...= ", origin_time_shift) for ii, tr in enumerate(st): Tshift[ii] = Tshift[ii] - origin_time_shift ssta = tr.stats.station
def calculate_trave_times(db, params): # set params from params lat_min = params["lat_min"] lat_max = params["lat_max"] lon_min = params["lon_min"] lon_max = params["lon_max"] step = params["step"] calculate_open = params["calculate_open"] vel_model = params["vel_model"] eq_depth = params["eq_depth"] max_dist = ((lat_max - lat_min) ** 2 + (lon_max - lon_min) ** 2) ** (1 / 2) model_name = "travel_time_d" + str(eq_depth) if calculate_open == "calculate": print("") print("----------") print("CALCULATING TRAVEL TIMES") print("----------") print("Precalculationg tt") # define velocity model model = TauPyModel(model=vel_model) # precalculate times tt_precalc = precalculate_times(max_dist, step, eq_depth, model) # connect to the database mydb = mysql.connector.connect( host=db["host"], user=db["user"], passwd=db["passwd"], database=db["db_name"], ) devices = fetch_all_devices(db) grid = make_grid( lat_min=lat_min, lat_max=lat_max, lon_min=lon_min, lon_max=lon_max, step=step, ) travel_time = {"grid_lat": grid[0], "grid_lon": grid[1], "vector": tt_precalc} print("Calculating for stations") for device in devices: sta_name = device[0] print("Station: {}".format(sta_name)) sta_lat = device[1] sta_lon = device[2] travel_time[sta_name] = calculate_tt( tt_precalc, grid, sta_lat, sta_lon, eq_depth ) with open("obj/travel_time/" + model_name + ".pkl", "wb") as f: pickle.dump(travel_time, f, pickle.HIGHEST_PROTOCOL) return travel_time elif calculate_open == "open": with open("obj/travel_time/" + model_name + ".pkl", "rb") as f: travel_time = pickle.load(f) return travel_time
print "reading tvel file:", try: f = open(tvel_file_1, 'r') s = f.readlines() f.close() except: print 'Failed to read tvel velocity model file {}, skipping.'.format( tvel_file_1) continue #get top P velocity top_vel = float(s[2].split()[1]) #create travel time calculator object taupmodel_1 = TauPyModel(model=npz_file_1) #remove data before B sac header value #tr1.data=tr1.data[-tr1.stats.sac.b/tr1.stats.delta:] try: #calculate GCARC (for some reason obspy does not read gcarc header) gcarc1 = haversineD(tr1.stats.sac.evla, tr1.stats.sac.evlo, tr1.stats.sac.stla, tr1.stats.sac.stlo) except: print "ERROR: Can\'t calculate event distance, quitting", tr1 #unlikely that any other station will have a P arrival, so break sys.exit(1) if args.verbose: print "delta=", gcarc1, "depth=", tr1.stats.sac.evdp #t1 = np.arange(0, tr1.stats.npts / tr1.stats.sampling_rate, tr1.stats.delta)