def _get_origtime_delays(self, vel_mod, target_depth, max_dist=1., num_vals=20): """ :param max_dist: maximum distance (degrees) at which to calculate :param num_vals: number of distances (from 0 to max_distance) at which to calculate delats :returns: dict with keys 'op' and 'ps' corresponding to sorted lists of delay times from origin-to-p and p-to-s """ try: model = TauPyModel(model=vel_mod) except Exception: build_taup_model(vel_mod) # converts from np or tvel to npz model = TauModel.from_file(vel_mod) # reads npz delays = {'op': [0], 'ps': [0]} # ps_delays, p_delays = [0], [0] step = max_dist / num_vals for distance in np.arange(step, max_dist + .001, step): arrivals = model.get_travel_times(target_depth, distance, phase_list=["P", "S"]) p_delay = [x.time for x in arrivals if x.phase == "P"] s_delay = [x.time for x in arrivals if x.phase == "S"] ps_delay = s_delay - p_delay if len(ps_delay) == 1 and len(p_delay) == 1: delays['ps'].append(ps_delay[0]) delays['op'].append(p_delay[0]) else: log( 'Could not calculate delays for dist={:.2g} degrees'. format(distance), 'warning') return delays
def taumodel(model): """Returns a TauModel from string, or the argument in case of TypeError, assuming the latter is already a TauModel """ try: return TauModel.from_file(model) # NOTE from_file has an argument cache that we ignore because of a bug (reported) # in obspy 1.0.2 if cache is False! except TypeError: return model # ok, we assume the argument is already a TaupModel then
def test_split(self): depth = 110 tau_model = TauModel.from_file('iasp91') split_t_mod = tau_model.split_branch(depth) self.assertEqual(tau_model.tau_branches.shape[1] + 1, split_t_mod.tau_branches.shape[1]) self.assertEqual(len(tau_model.ray_params) + 2, len(split_t_mod.ray_params)) branch_count = tau_model.tau_branches.shape[1] split_branch_index = tau_model.find_branch(depth) new_p_ray_param = split_t_mod.s_mod.get_slowness_layer( split_t_mod.s_mod.layer_number_above(depth, True), True)['bot_p'] new_s_ray_param = split_t_mod.s_mod.get_slowness_layer( split_t_mod.s_mod.layer_number_above(depth, False), False)['bot_p'] p_index = s_index = len(split_t_mod.ray_params) ray_params = split_t_mod.ray_params for j in range(len(ray_params)): if new_p_ray_param == ray_params[j]: p_index = j if new_s_ray_param == ray_params[j]: s_index = j self.assertTrue(p_index == len(split_t_mod.ray_params) or s_index < p_index) for b in range(branch_count): orig = tau_model.get_tau_branch(b, True) if b < split_branch_index: depth_branch = split_t_mod.get_tau_branch(b, True) self.assertGreater(depth_branch.dist[p_index], 0) self.assertGreater(depth_branch.time[p_index], 0) elif b > split_branch_index: depth_branch = split_t_mod.get_tau_branch(b + 1, True) self.assertAlmostEqual(depth_branch.dist[p_index], 0, delta=0.00000001) self.assertAlmostEqual(depth_branch.time[p_index], 0, delta=0.00000001) else: # the split one continue np.testing.assert_allclose(orig.dist[0:s_index], depth_branch.dist[0:s_index], atol=0.00000001) np.testing.assert_allclose(orig.time[0:s_index], depth_branch.time[0:s_index], atol=0.00000001) orig_len = len(orig.dist) if s_index < orig_len: self.assertEqual(orig_len + 2, len(depth_branch.dist)) np.testing.assert_allclose( orig.dist[s_index:p_index - 1], depth_branch.dist[s_index + 1:p_index], atol=0.00000001) np.testing.assert_allclose( orig.time[s_index:p_index - 1], depth_branch.time[s_index + 1:p_index], atol=0.00000001) np.testing.assert_allclose( orig.dist[p_index:orig_len-s_index-2], depth_branch.dist[p_index+2:orig_len-s_index], atol=0.00000001) np.testing.assert_allclose( orig.time[p_index:orig_len-s_index-2], depth_branch.time[p_index+2:orig_len-s_index], atol=0.00000001) # now check branch split orig = tau_model.get_tau_branch(split_branch_index, True) above = split_t_mod.get_tau_branch(split_branch_index, True) below = split_t_mod.get_tau_branch(split_branch_index + 1, True) self.assertAlmostEqual(above.min_ray_param, below.max_ray_param, 8) for i in range(len(above.dist)): if i < s_index: self.assertAlmostEqual(orig.dist[i], above.dist[i], delta=0.000000001) elif i == s_index: # new value should be close to average of values to either side self.assertAlmostEqual((orig.dist[i - 1] + orig.dist[i]) / 2, above.dist[i], delta=0.00001) elif i > s_index and i < p_index: self.assertAlmostEqual(orig.dist[i - 1], above.dist[i] + below.dist[i], delta=0.000000001) elif i == p_index: # new value should be close to average of values to either side self.assertAlmostEqual( (orig.dist[i - 2] + orig.dist[i - 1]) / 2, above.dist[i] + below.dist[i], delta=0.0001) else: self.assertAlmostEqual(orig.dist[i - 2], above.dist[i] + below.dist[i], delta=0.000000001)
def setUp(self): self.depth = 119 self.tMod = TauModel.from_file("iasp91").depth_correct(self.depth)
def setUp(self): self.depth = 119 self.tau_model = TauModel.from_file('iasp91').depth_correct(self.depth)
def test_split(self): depth = 110 tau_model = TauModel.from_file('iasp91') split_t_mod = tau_model.split_branch(depth) shape1 = tau_model.tau_branches.shape[1] + 1 shape2 = split_t_mod.tau_branches.shape[1] assert shape1 == shape2 assert len(tau_model.ray_params) + 2 == len(split_t_mod.ray_params) branch_count = tau_model.tau_branches.shape[1] split_branch_index = tau_model.find_branch(depth) new_p_ray_param = split_t_mod.s_mod.get_slowness_layer( split_t_mod.s_mod.layer_number_above(depth, True), True)['bot_p'] new_s_ray_param = split_t_mod.s_mod.get_slowness_layer( split_t_mod.s_mod.layer_number_above(depth, False), False)['bot_p'] p_index = s_index = len(split_t_mod.ray_params) ray_params = split_t_mod.ray_params for j in range(len(ray_params)): if new_p_ray_param == ray_params[j]: p_index = j if new_s_ray_param == ray_params[j]: s_index = j assert p_index == len(split_t_mod.ray_params) or s_index < p_index for b in range(branch_count): orig = tau_model.get_tau_branch(b, True) if b < split_branch_index: depth_branch = split_t_mod.get_tau_branch(b, True) assert depth_branch.dist[p_index] > 0 assert depth_branch.time[p_index] > 0 elif b > split_branch_index: depth_branch = split_t_mod.get_tau_branch(b + 1, True) assert np.isclose(depth_branch.dist[p_index], 0) assert np.isclose(depth_branch.time[p_index], 0) else: # the split one continue np.testing.assert_allclose(orig.dist[0:s_index], depth_branch.dist[0:s_index], atol=0.00000001) np.testing.assert_allclose(orig.time[0:s_index], depth_branch.time[0:s_index], atol=0.00000001) orig_len = len(orig.dist) if s_index < orig_len: assert orig_len + 2 == len(depth_branch.dist) np.testing.assert_allclose( orig.dist[s_index:p_index - 1], depth_branch.dist[s_index + 1:p_index], atol=0.00000001) np.testing.assert_allclose( orig.time[s_index:p_index - 1], depth_branch.time[s_index + 1:p_index], atol=0.00000001) np.testing.assert_allclose( orig.dist[p_index:orig_len - s_index - 2], depth_branch.dist[p_index + 2:orig_len - s_index], atol=0.00000001) np.testing.assert_allclose( orig.time[p_index:orig_len - s_index - 2], depth_branch.time[p_index + 2:orig_len - s_index], atol=0.00000001) # now check branch split orig = tau_model.get_tau_branch(split_branch_index, True) above = split_t_mod.get_tau_branch(split_branch_index, True) below = split_t_mod.get_tau_branch(split_branch_index + 1, True) assert np.isclose(above.min_ray_param, below.max_ray_param) for i in range(len(above.dist)): if i < s_index: assert np.isclose(orig.dist[i], above.dist[i]) elif i == s_index: # new value should be close to average of values to either side assert np.isclose((orig.dist[i - 1] + orig.dist[i]) / 2, above.dist[i], atol=0.00001) elif s_index < i < p_index: assert np.isclose(orig.dist[i - 1], above.dist[i] + below.dist[i]) elif i == p_index: # new value should be close to average of values to either side val1 = (orig.dist[i - 2] + orig.dist[i - 1]) / 2 val2 = above.dist[i] + below.dist[i] assert np.isclose(val1, val2, atol=0.0001) else: val1 = orig.dist[i - 2] val2 = above.dist[i] + below.dist[i] assert np.isclose(val1, val2, atol=0.000000001)
def test_split(self): depth = 110 tau_model = TauModel.from_file('iasp91') split_t_mod = tau_model.split_branch(depth) self.assertEqual(tau_model.tau_branches.shape[1] + 1, split_t_mod.tau_branches.shape[1]) self.assertEqual( len(tau_model.ray_params) + 2, len(split_t_mod.ray_params)) branch_count = tau_model.tau_branches.shape[1] split_branch_index = tau_model.find_branch(depth) new_p_ray_param = split_t_mod.s_mod.get_slowness_layer( split_t_mod.s_mod.layer_number_above(depth, True), True)['bot_p'] new_s_ray_param = split_t_mod.s_mod.get_slowness_layer( split_t_mod.s_mod.layer_number_above(depth, False), False)['bot_p'] p_index = s_index = len(split_t_mod.ray_params) ray_params = split_t_mod.ray_params for j in range(len(ray_params)): if new_p_ray_param == ray_params[j]: p_index = j if new_s_ray_param == ray_params[j]: s_index = j self.assertTrue(p_index == len(split_t_mod.ray_params) or s_index < p_index) for b in range(branch_count): orig = tau_model.get_tau_branch(b, True) if b < split_branch_index: depth_branch = split_t_mod.get_tau_branch(b, True) self.assertGreater(depth_branch.dist[p_index], 0) self.assertGreater(depth_branch.time[p_index], 0) elif b > split_branch_index: depth_branch = split_t_mod.get_tau_branch(b + 1, True) self.assertAlmostEqual(depth_branch.dist[p_index], 0, delta=0.00000001) self.assertAlmostEqual(depth_branch.time[p_index], 0, delta=0.00000001) else: # the split one continue np.testing.assert_allclose(orig.dist[0:s_index], depth_branch.dist[0:s_index], atol=0.00000001) np.testing.assert_allclose(orig.time[0:s_index], depth_branch.time[0:s_index], atol=0.00000001) orig_len = len(orig.dist) if s_index < orig_len: self.assertEqual(orig_len + 2, len(depth_branch.dist)) np.testing.assert_allclose(orig.dist[s_index:p_index - 1], depth_branch.dist[s_index + 1:p_index], atol=0.00000001) np.testing.assert_allclose(orig.time[s_index:p_index - 1], depth_branch.time[s_index + 1:p_index], atol=0.00000001) np.testing.assert_allclose( orig.dist[p_index:orig_len - s_index - 2], depth_branch.dist[p_index + 2:orig_len - s_index], atol=0.00000001) np.testing.assert_allclose( orig.time[p_index:orig_len - s_index - 2], depth_branch.time[p_index + 2:orig_len - s_index], atol=0.00000001) # now check branch split orig = tau_model.get_tau_branch(split_branch_index, True) above = split_t_mod.get_tau_branch(split_branch_index, True) below = split_t_mod.get_tau_branch(split_branch_index + 1, True) self.assertAlmostEqual(above.min_ray_param, below.max_ray_param, 8) for i in range(len(above.dist)): if i < s_index: self.assertAlmostEqual(orig.dist[i], above.dist[i], delta=0.000000001) elif i == s_index: # new value should be close to average of values to either side self.assertAlmostEqual((orig.dist[i - 1] + orig.dist[i]) / 2, above.dist[i], delta=0.00001) elif i > s_index and i < p_index: self.assertAlmostEqual(orig.dist[i - 1], above.dist[i] + below.dist[i], delta=0.000000001) elif i == p_index: # new value should be close to average of values to either side self.assertAlmostEqual( (orig.dist[i - 2] + orig.dist[i - 1]) / 2, above.dist[i] + below.dist[i], delta=0.0001) else: self.assertAlmostEqual(orig.dist[i - 2], above.dist[i] + below.dist[i], delta=0.000000001)
def tau_model(self): """Return the tau model for testing.""" return TauModel.from_file('iasp91').depth_correct(self.depth)