Skip to content

Commit

Permalink
Merge pull request #2319 from obspy/backport-2280
Browse files Browse the repository at this point in the history
Backport 2280
  • Loading branch information
krischer committed Feb 14, 2019
2 parents 1f2a9e9 + 4ca5696 commit 4160369
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions obspy/taup/tau_branch.py
Expand Up @@ -8,6 +8,8 @@
from future.builtins import * # NOQA
from future.utils import native_str

import warnings

import numpy as np

from .c_wrappers import clibtau
Expand Down Expand Up @@ -153,15 +155,16 @@ def insert(self, ray_param, s_mod, index):
def shift_branch(self, index):
new_size = len(self.dist) + 1

self.time.resize(new_size)
self.time = self._robust_resize(self.time, new_size)

self.time[index + 1:] = self.time[index:-1]
self.time[index] = 0

self.dist.resize(new_size)
self.dist = self._robust_resize(self.dist, new_size)
self.dist[index + 1:] = self.dist[index:-1]
self.dist[index] = 0

self.tau.resize(new_size)
self.tau = self._robust_resize(self.tau, new_size)
self.tau[index + 1:] = self.tau[index:-1]
self.tau[index] = 0

Expand Down Expand Up @@ -477,3 +480,19 @@ def _from_array(arr):
arr_ = arr_[()]
setattr(branch, key, arr_)
return branch

@staticmethod
def _robust_resize(arr, new_size):
"""
Try to resize an array inplace. If an error is raised use numpy
resize function to create a new array. Return the array.
"""
try:
arr.resize(new_size)
except ValueError:
msg = ('Resizing a TauP array inplace failed due to the existence'
' of other references to the array, creating a new array. '
'See Obspy #2280.')
warnings.warn(msg)
arr = np.resize(arr, new_size)
return arr

0 comments on commit 4160369

Please sign in to comment.