Skip to content

Commit 1cc1e33

Browse files
committed
better importing
1 parent d3b4f42 commit 1cc1e33

File tree

7 files changed

+28
-36
lines changed

7 files changed

+28
-36
lines changed

Examples/astropy_geodetic2ecef.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
try:
1717
from pymap3d.tests.matlab_engine import matlab_ecef2eci, matlab_engine, has_matmap3d
1818
except ImportError:
19-
matlab_engine = None
19+
pass
2020

2121
print("Python version:", sys.version)
2222
print("AstroPy version:", astropy.__version__)
@@ -43,20 +43,18 @@
4343
print(np.array(ecef))
4444

4545
# %% AstroPy ECEF to ECI (J2000)
46-
astropy_eci = pm.ecef2eci(ecef[0], ecef[1], ecef[2], dt)
47-
astropy_eci = np.array(astropy_eci)
46+
astropy_eci = np.array(pm.ecef2eci(ecef[0], ecef[1], ecef[2], dt))
4847
print("\nAstroPy: ECI Coordinates (meters):")
4948
print(astropy_eci)
5049

51-
numpy_eci = pm.ecef2eci(ecef[0], ecef[1], ecef[2], dt, force_non_astropy=True)
52-
numpy_eci = np.array(numpy_eci)
50+
numpy_eci = np.array(pm.ecef2eci(ecef[0], ecef[1], ecef[2], dt, force_non_astropy=True))
5351
print("\nNumpy: ECI Coordinates (meters):")
5452
print(numpy_eci)
5553

5654
print("\nAstroPy - Numpy Difference (ECI meters):", astropy_eci - numpy_eci)
5755

5856
# %% Matlab comparison
59-
if matlab_engine is not None:
57+
if matlab_engine in sys.modules:
6058
eng = matlab_engine()
6159
eci_matlab_aerospace = matlab_ecef2eci(eng, False, dt, ecef)
6260
print("\nMatlab Aerospace Toolbox: ECI Coordinates (meters):")

src/pymap3d/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* Fortran: [Maptran3D](https://github.com/geospace-code/maptran3d)
3030
"""
3131

32-
__version__ = "3.1.1"
32+
__version__ = "3.1.2"
3333

3434
from .aer import aer2ecef, aer2geodetic, ecef2aer, geodetic2aer
3535
from .ecef import (

src/pymap3d/ecef.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,13 @@
66

77
try:
88
from numpy import asarray, empty_like, finfo, where
9-
10-
from .eci import ecef2eci, eci2ecef
119
except ImportError:
12-
13-
def eci2ecef(x, y, z, time: datetime, force_non_astropy: bool = False) -> tuple:
14-
raise ImportError("Numpy required for eci2ecef")
15-
16-
def ecef2eci(x, y, z, time: datetime, force_non_astropy: bool = False) -> tuple:
17-
raise ImportError("Numpy required for ecef2eci")
18-
10+
pass
1911

2012
from datetime import datetime
2113
from math import pi
2214

15+
from .eci import ecef2eci, eci2ecef
2316
from .ellipsoid import Ellipsoid
2417
from .mathfun import atan, atan2, cos, degrees, hypot, isclose, radians, sin, sqrt, tan
2518

src/pymap3d/eci.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import sys
77
import logging
88

9-
import numpy
10-
119
try:
10+
import numpy
1211
import astropy.units as u
1312
from astropy.coordinates import GCRS, ITRS, CartesianRepresentation, EarthLocation
1413
except ImportError:
1514
pass
1615

16+
1717
from .sidereal import greenwichsrt, juliandate
1818

1919
__all__ = ["eci2ecef", "ecef2eci"]
@@ -48,11 +48,13 @@ def eci2ecef(x, y, z, time: datetime, force_non_astropy: bool = False) -> tuple:
4848
z ECEF coordinate
4949
"""
5050

51-
if force_non_astropy or "astropy" not in sys.modules:
52-
logging.warning(f"{__name__}: Numpy implementation has considerably less accuracy than Astropy")
51+
if "astropy" in sys.modules and not force_non_astropy:
52+
xe, ye, ze = eci2ecef_astropy(x, y, z, time)
53+
elif "numpy" in sys.modules:
54+
logging.warning(f"{__name__}: Numpy implementation has much less accuracy than Astropy")
5355
xe, ye, ze = eci2ecef_numpy(x, y, z, time)
5456
else:
55-
xe, ye, ze = eci2ecef_astropy(x, y, z, time)
57+
raise ImportError("eci2ecef requires either Numpy or Astropy")
5658

5759
return xe.squeeze()[()], ye.squeeze()[()], ze.squeeze()[()]
5860

@@ -135,12 +137,13 @@ def ecef2eci(x, y, z, time: datetime, force_non_astropy: bool = False) -> tuple:
135137
z ECI coordinate
136138
"""
137139

138-
# if astropy is imported
139-
if force_non_astropy or "astropy" not in sys.modules:
140-
logging.warning(f"{__name__}: Numpy implementation has considerably less accuracy than Astropy")
140+
if "astropy" in sys.modules and not force_non_astropy:
141+
xe, ye, ze = ecef2eci_astropy(x, y, z, time)
142+
elif "numpy" in sys.modules:
143+
logging.warning(f"{__name__}: Numpy implementation has much less accuracy than Astropy")
141144
xe, ye, ze = ecef2eci_numpy(x, y, z, time)
142145
else:
143-
xe, ye, ze = ecef2eci_astropy(x, y, z, time)
146+
raise ImportError("ecef2eci requires either Numpy or Astropy")
144147

145148
return xe, ye, ze
146149

src/pymap3d/haversine.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"""
1111

1212
import sys
13-
import logging
1413

1514
try:
1615
from astropy.coordinates import angular_separation
@@ -110,11 +109,10 @@ def anglesep(
110109
lon1 = radians(lon1)
111110
lat1 = radians(lat1)
112111

113-
if force_non_astropy or "astropy" not in sys.modules:
114-
logging.warning(f"{__name__}: Numpy implementation has considerably less accuracy than Astropy")
115-
sep_rad = anglesep_meeus(lon0, lat0, lon1, lat1, deg=False)
116-
else:
112+
if "astropy" in sys.modules and not force_non_astropy:
117113
sep_rad = angular_separation(lon0, lat0, lon1, lat1)
114+
else:
115+
sep_rad = anglesep_meeus(lon0, lat0, lon1, lat1, deg=False)
118116

119117
return degrees(sep_rad) if deg else sep_rad
120118

src/pymap3d/sidereal.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ def datetime2sidereal(time: datetime, lon_radians: float, force_non_astropy: boo
4242
if isinstance(time, (tuple, list)):
4343
return [datetime2sidereal(t, lon_radians) for t in time]
4444

45-
if force_non_astropy or "astropy" not in sys.modules:
45+
if "astropy" in sys.modules and not force_non_astropy:
46+
return datetime2sidereal_astropy(time, lon_radians)
47+
else:
4648
logging.debug(f"{__name__}: Vallado implementation")
4749
return datetime2sidereal_vallado(time, lon_radians)
48-
else:
49-
logging.debug(f"{__name__}: Astropy implementation")
50-
return datetime2sidereal_astropy(time, lon_radians)
5150

5251

5352
def datetime2sidereal_astropy(t: datetime, lon_radians: float):

src/pymap3d/tests/test_eci.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ def test_eci2ecef_astropy():
5050
assert isinstance(ecef[2], float)
5151

5252

53-
def test_ecef2eci():
53+
@pytest.mark.parametrize("force_non_astropy", [True, False])
54+
def test_ecef2eci(force_non_astropy):
5455
pytest.importorskip("numpy")
5556
# this example from Matlab ecef2eci docs
56-
eci = pm.ecef2eci(*ECEF, UTC, force_non_astropy=True)
57+
eci = pm.ecef2eci(*ECEF, UTC, force_non_astropy=force_non_astropy)
5758

5859
assert isinstance(eci[0], float)
5960
assert isinstance(eci[1], float)

0 commit comments

Comments
 (0)