|
5 | 5 | import warnings
|
6 | 6 |
|
7 | 7 | try:
|
8 |
| - from numpy import asarray, finfo, where |
| 8 | + from numpy import asarray, finfo, full_like, logical_and, logical_or, ndarray, where |
9 | 9 |
|
10 | 10 | from .eci import ecef2eci, eci2ecef
|
11 | 11 | except ImportError:
|
@@ -144,16 +144,24 @@ def ecef2geodetic(
|
144 | 144 |
|
145 | 145 | # eqn. 4b
|
146 | 146 | try:
|
| 147 | + calculated_idx = full_like(z, False) |
| 148 | + Beta = ndarray(z.shape) |
147 | 149 | with warnings.catch_warnings(record=True):
|
148 | 150 | warnings.simplefilter("error")
|
149 |
| - Beta = atan(huE / u * z / hypot(x, y)) |
| 151 | + # Preempt any possible divide-by-zero errors by only calculating values that will succeed. Then, below, use |
| 152 | + # the fallback Beta values for any values that would have failed. |
| 153 | + xy_hypot = hypot(x, y) |
| 154 | + calculated_idx = ~logical_or(isclose(u, 0), isclose(xy_hypot, 0)) |
| 155 | + Beta[calculated_idx] = atan(huE[calculated_idx] / u[calculated_idx] * z[calculated_idx] / |
| 156 | + xy_hypot[calculated_idx]) |
150 | 157 | except (ArithmeticError, RuntimeWarning):
|
151 |
| - if any(isclose(z, 0)): |
152 |
| - Beta = 0 |
153 |
| - elif z > 0: |
154 |
| - Beta = pi / 2 |
155 |
| - else: |
156 |
| - Beta = -pi / 2 |
| 158 | + pass |
| 159 | + |
| 160 | + if not all(calculated_idx): |
| 161 | + not_calculated_idx = ~calculated_idx |
| 162 | + Beta[logical_and(not_calculated_idx, z > 0)] = pi / 2 |
| 163 | + Beta[logical_and(not_calculated_idx, z < 0)] = -pi / 2 |
| 164 | + Beta[logical_and(not_calculated_idx, isclose(z, 0))] = 0 |
157 | 165 |
|
158 | 166 | # eqn. 13
|
159 | 167 | dBeta = ((ell.semiminor_axis * u - ell.semimajor_axis * huE + E**2) * sin(Beta)) / (
|
|
0 commit comments