Repository: obspy/obspy

Issue:
RuntimeWarning when using own model in obspy.taup
Dear Developer:

I try to use my own model in obspy.taup. But I got some problems.

This is my own model named ```new_model.nd```:
```
0 3.5 3 2.7 1456.0 600.0
1.9100000000000001 3.7 3 2.7 1456.0 600.0
2.9000000000000004 4.2 3 2.7 1456.0 600.0
4.9 4.5 3 2.7 1456.0 600.0
6.9 5.0 3 2.7 1456.0 600.0
19.7 6.5 3 2.7 1456.0 600.0
22.7 6.8 3 2.7 1456.0 600.0
32.7 8.0 3 2.7 1456.0 600.0
mantle
   32.7     8.11061   4.49094   3.38076    1446.0     600.0
   40.00     8.10119   4.48486   3.37906    1446.0     600.0
   60.00     8.08907   4.47715   3.37688    1447.0     600.0
   80.00     8.07688   4.46953   3.37471     195.0      80.0
  115.00     8.05540   4.45643   3.37091     195.0      80.0
  150.00     8.03370   4.44361   3.36710     195.0      80.0
  185.00     8.01180   4.43108   3.36330     195.0      80.0
  220.00     7.98970   4.41885   3.35950     195.0      80.0
  220.00     8.55896   4.64391   3.43578     362.0     143.0
  265.00     8.64552   4.67540   3.46264     365.0     143.0
  310.00     8.73209   4.70690   3.48951     367.0     143.0
  355.00     8.81867   4.73840   3.51639     370.0     143.0
  400.00     8.90522   4.76989   3.54325     372.0     143.0
  400.00     9.13397   4.93259   3.72378     366.0     143.0
  450.00     9.38990   5.07842   3.78678     365.0     143.0
  500.00     9.64588   5.22428   3.84980     364.0     143.0
  550.00     9.90185   5.37014   3.91282     363.0     143.0
  600.00    10.15782   5.51602   3.97584     362.0     143.0
  635.00    10.21203   5.54311   3.98399     362.0     143.0
  670.00    10.26622   5.57020   3.99214     362.0     143.0
  670.00    10.75131   5.94508   4.38071     759.0     312.0
  721.00    10.91005   6.09418   4.41241     744.0     312.0
  771.00    11.06557   6.24046   4.44317     730.0     312.0
  871.00    11.24490   6.31091   4.50372     737.0     312.0
  971.00    11.41560   6.37813   4.56307     743.0     312.0
 1071.00    11.57828   6.44232   4.62129     750.0     312.0
 1171.00    11.73357   6.50370   4.67844     755.0     312.0
 1271.00    11.88209   6.56250   4.73460     761.0     312.0
 1371.00    12.02445   6.61891   4.78983     766.0     312.0
 1471.00    12.16126   6.67317   4.84422     770.0     312.0
 1571.00    12.29316   6.72548   4.89783     775.0     312.0
 1671.00    12.42075   6.77606   4.95073     779.0     312.0
 1771.00    12.54466   6.82512   5.00299     784.0     312.0
 1871.00    12.66550   6.87289   5.05469     788.0     312.0
 1971.00    12.78389   6.91957   5.10590     792.0     312.0
 2071.00    12.90045   6.96538   5.15669     795.0     312.0
 2171.00    13.01579   7.01053   5.20713     799.0     312.0
 2271.00    13.13055   7.05525   5.25729     803.0     312.0
 2371.00    13.24532   7.09974   5.30724     807.0     312.0
 2471.00    13.36074   7.14423   5.35706     811.0     312.0
 2571.00    13.47742   7.18892   5.40681     815.0     312.0
 2671.00    13.59597   7.23403   5.45657     819.0     312.0
 2741.00    13.68041   7.26597   5.49145     822.0     312.0
 2771.00    13.68753   7.26575   5.50642     823.0     312.0
 2871.00    13.71168   7.26486   5.55641     826.0     312.0
 2891.00    13.71660   7.26466   5.56645     826.0     312.0
outer-core
 2891.00     8.06482   0.00000   9.90349   57822.0       0.0
 2971.00     8.19939   0.00000  10.02940   57822.0       0.0
 3071.00     8.36019   0.00000  10.18134   57822.0       0.0
 3171.00     8.51298   0.00000  10.32726   57822.0       0.0
 3271.00     8.65805   0.00000  10.46727   57822.0       0.0
 3371.00     8.79573   0.00000  10.60152   57822.0       0.0
 3471.00     8.92632   0.00000  10.73012   57822.0       0.0
 3571.00     9.05015   0.00000  10.85321   57822.0       0.0
 3671.00     9.16752   0.00000  10.97091   57822.0       0.0
 3771.00     9.27867   0.00000  11.08335   57822.0       0.0
 3871.00     9.38418   0.00000  11.19067   57822.0       0.0
 3971.00     9.48409   0.00000  11.29298   57822.0       0.0
 4071.00     9.57881   0.00000  11.39042   57822.0       0.0
 4171.00     9.66865   0.00000  11.48311   57822.0       0.0
 4271.00     9.75393   0.00000  11.57119   57822.0       0.0
 4371.00     9.83496   0.00000  11.65478   57822.0       0.0
 4471.00     9.91206   0.00000  11.73401   57822.0       0.0
 4571.00     9.98554   0.00000  11.80900   57822.0       0.0
 4671.00    10.05572   0.00000  11.87990   57822.0       0.0
 4771.00    10.12291   0.00000  11.94682   57822.0       0.0
 4871.00    10.18743   0.00000  12.00989   57822.0       0.0
 4971.00    10.24959   0.00000  12.06924   57822.0       0.0
 5071.00    10.30971   0.00000  12.12500   57822.0       0.0
 5149.50    10.35568   0.00000  12.16634   57822.0       0.0
inner-core
 5149.50    11.02827   3.50432  12.76360     445.0      85.0
 5171.00    11.03643   3.51002  12.77493     445.0      85.0
 5271.00    11.07249   3.53522  12.82501     443.0      85.0
 5371.00    11.10542   3.55823  12.87073     440.0      85.0
 5471.00    11.13521   3.57905  12.91211     439.0      85.0
 5571.00    11.16186   3.59767  12.94912     437.0      85.0
 5671.00    11.18538   3.61411  12.98178     436.0      85.0
 5771.00    11.20576   3.62835  13.01009     434.0      85.0
 5871.00    11.22301   3.64041  13.03404     433.0      85.0
 5971.00    11.23712   3.65027  13.05364     432.0      85.0
 6071.00    11.24809   3.65794  13.06888     432.0      85.0
 6171.00    11.25593   3.66342  13.07977     431.0      85.0
 6271.00    11.26064   3.66670  13.08630     431.0      85.0
 6371.00    11.26220   3.66780  13.08848     431.0      85.0
```

Below the MOHO it is same with PREM.

When I try following scripts:
```
import obspy.taup
from obspy.taup import TauPyModel
obspy.taup.taup_create.build_taup_model("./new_model.nd",".")
model = TauPyModel( model="./new_model.npz" )
arrivals = model.get_travel_times(source_depth_in_km=0.01,distance_in_degree=0.01,phase_list=["P", "PSPSPS"])
```
some warnings appear:
```
/usr/local/anaconda3/lib/python3.6/site-packages/obspy/taup/slowness_layer.py:273: RuntimeWarning: overflow encountered in double_scalars
  a_denominator = pow((radius_of_planet - top_depth), b)
/usr/local/anaconda3/lib/python3.6/site-packages/obspy/taup/slowness_layer.py:275: RuntimeWarning: overflow encountered in double_scalars
  answer = a * pow((radius_of_planet - depth), b)
/usr/local/anaconda3/lib/python3.6/site-packages/obspy/taup/slowness_layer.py:275: RuntimeWarning: invalid value encountered in double_scalars
  answer = a * pow((radius_of_planet - depth), b)
```

With these warning messages, I do not know whether the travel time results are still right. Could you please check what causes these warnings? Many thanks.


Discussion:

Resolving patch:
diff --git a/obspy/taup/slowness_layer.py b/obspy/taup/slowness_layer.py
--- a/obspy/taup/slowness_layer.py
+++ b/obspy/taup/slowness_layer.py
@@ -267,12 +267,19 @@ def evaluate_at_bullen(layer, depth, radius_of_planet):
     elif depth == bot_depth:
         return bot_p
     else:
-        b = np.divide(math.log(np.divide(top_p, bot_p)),
-                      math.log(np.divide((radius_of_planet - top_depth),
-                                         (radius_of_planet - bot_depth))))
-        a_denominator = pow((radius_of_planet - top_depth), b)
-        a = top_p / a_denominator
-        answer = a * pow((radius_of_planet - depth), b)
+        try:
+            # The power law calculation has some stability issues with very
+            # small layers. Catch them here to trigger the fallback
+            # computations later on.
+            with np.errstate(all="raise"):
+                b = math.log(top_p / bot_p) / \
+                    math.log((radius_of_planet - top_depth) /
+                             (radius_of_planet - bot_depth))
+                a_denominator = pow((radius_of_planet - top_depth), b)
+                a = top_p / a_denominator
+                answer = a * pow((radius_of_planet - depth), b)
+        except FloatingPointError:
+            answer = math.nan
         if answer < 0 or math.isnan(answer) or math.isinf(answer):
             # numerical instability in power law calculation???
             # try a linear interpolation if the layer is small ( <2 km)


Test patch: