Skip to content

Commit 004d2d5

Browse files
committed
make matlab engine code reusable
1 parent 9e70059 commit 004d2d5

File tree

5 files changed

+72
-23
lines changed

5 files changed

+72
-23
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- "**.py"
77
- .github/workflows/ci.yml
88
- "!scripts/**"
9+
- "!examples/**"
910

1011
jobs:
1112

.github/workflows/ci_stdlib_only.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- "**.py"
77
- .github/workflows/ci_stdlib_only.yml
88
- "!scripts/**"
9+
- "!examples/**"
910

1011
jobs:
1112

Examples/astropy_geodetic2ecef.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@
99

1010
from astropy.time import Time
1111
import astropy
12+
import numpy as np
13+
1214
import pymap3d as pm
1315

16+
try:
17+
from pymap3d.tests.matlab_engine import matlab_ecef2eci, matlab_engine, has_matmap3d
18+
except ImportError:
19+
matlab_engine = None
20+
1421
print("Python version:", sys.version)
1522
print("AstroPy version:", astropy.__version__)
1623

@@ -28,12 +35,44 @@
2835
print("Julian Date (TT):", astropy_time.tt.jd)
2936
print("GMST:", astropy_time.sidereal_time("mean", "greenwich"))
3037

38+
np.set_printoptions(precision=3, suppress=True)
39+
3140
# %% 1. Geodetic to ECEF
3241
ecef = pm.geodetic2ecef(lat, lon, alt)
3342
print("\nECEF Coordinates (meters):")
34-
print(f"X: {ecef[0]:.8f}, Y: {ecef[1]:.8f}, Z: {ecef[2]:.8f}")
43+
print(np.array(ecef))
44+
45+
# %% AstroPy ECEF to ECI (J2000)
46+
astropy_eci = pm.ecef2eci(ecef[0], ecef[1], ecef[2], dt)
47+
astropy_eci = np.array(astropy_eci)
48+
print("\nAstroPy: ECI Coordinates (meters):")
49+
print(astropy_eci)
50+
51+
numpy_eci = pm.ecef2eci(ecef[0], ecef[1], ecef[2], dt, force_non_astropy=True)
52+
numpy_eci = np.array(numpy_eci)
53+
print("\nNumpy: ECI Coordinates (meters):")
54+
print(numpy_eci)
55+
56+
print("\nAstroPy - Numpy Difference (ECI meters):", astropy_eci - numpy_eci)
57+
58+
# %% Matlab comparison
59+
if matlab_engine is not None:
60+
eng = matlab_engine()
61+
eci_matlab_aerospace = matlab_ecef2eci(eng, False, dt, ecef)
62+
print("\nMatlab Aerospace Toolbox: ECI Coordinates (meters):")
63+
print(np.array(eci_matlab_aerospace))
64+
print(
65+
"\nAstroPy - Matlab Aerospace Toolbox Difference (ECI meters):",
66+
astropy_eci - eci_matlab_aerospace,
67+
)
68+
print(
69+
"Numpy - Matlab Aerospace Toolbox Difference (ECI meters):",
70+
numpy_eci - eci_matlab_aerospace,
71+
)
3572

36-
# %% 2. ECEF to ECI (J2000)
37-
eci = pm.ecef2eci(ecef[0], ecef[1], ecef[2], dt)
38-
print("\nECI Coordinates (meters):")
39-
print(f"X: {eci[0]}, Y: {eci[1]}, Z: {eci[2]}")
73+
if has_matmap3d(eng):
74+
eci_matmap3d = matlab_ecef2eci(eng, True, dt, ecef)
75+
print("\nMatlab Matmap3D: ECI Coordinates (meters):")
76+
print(np.array(eci_matmap3d))
77+
print("\nAstroPy - Matlab Matmap3D Difference (ECI meters):", astropy_eci - eci_matmap3d)
78+
print("Numpy - Matlab Matmap3D Difference (ECI meters):", numpy_eci - eci_matmap3d)

src/pymap3d/tests/matlab_engine.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import functools
22
from pathlib import Path
3+
from datetime import datetime
4+
5+
import numpy as np
36

47
import matlab.engine
58

@@ -36,3 +39,17 @@ def has_aerospace(eng) -> bool:
3639
@functools.cache
3740
def has_mapping(eng) -> bool:
3841
return eng.get_matlab_toolboxes()["mapping"]
42+
43+
44+
def matlab_ecef2eci(eng, matmap3d: bool, utc: datetime, ecef):
45+
if matmap3d:
46+
return eng.matmap3d.ecef2eci(utc, *ecef, nargout=3)
47+
48+
return np.array(eng.ecef2eci(utc, np.asarray(ecef), nargout=1)).squeeze()
49+
50+
51+
def matlab_eci2ecef(eng, matmap3d: bool, utc: datetime, eci):
52+
if matmap3d:
53+
return eng.matmap3d.eci2ecef(utc, *eci, nargout=3)
54+
55+
return np.array(eng.eci2ecef(utc, np.asarray(eci), nargout=1)).squeeze()

src/pymap3d/tests/test_matlab_ecef2eci.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,19 @@
1111
import pymap3d
1212

1313
try:
14-
import numpy as np
15-
from .matlab_engine import matlab_engine, has_aerospace, has_matmap3d
14+
from .matlab_engine import (
15+
matlab_engine,
16+
has_aerospace,
17+
has_matmap3d,
18+
matlab_ecef2eci,
19+
matlab_eci2ecef,
20+
)
1621
except ImportError:
1722
pytest.skip("Matlab Engine not found", allow_module_level=True)
1823
except RuntimeError:
1924
pytest.skip("Matlab Engine configuration error", allow_module_level=True)
2025

2126

22-
def ecef2eci(eng, matmap3d: bool, utc: datetime, ecef):
23-
if matmap3d:
24-
return eng.matmap3d.ecef2eci(utc, *ecef, nargout=3)
25-
26-
return np.array(eng.ecef2eci(utc, np.asarray(ecef), nargout=1)).squeeze()
27-
28-
29-
def eci2ecef(eng, matmap3d: bool, utc_m, eci):
30-
if matmap3d:
31-
return eng.matmap3d.eci2ecef(utc_m, *eci, nargout=3)
32-
33-
return np.array(eng.eci2ecef(utc_m, np.asarray(eci), nargout=1)).squeeze()
34-
35-
3627
@pytest.mark.parametrize("matmap3d", [False, True])
3728
def test_compare_ecef2eci(matmap3d):
3829
eng = matlab_engine()
@@ -52,7 +43,7 @@ def test_compare_ecef2eci(matmap3d):
5243

5344
eci_py = pymap3d.ecef2eci(ecef[0], ecef[1], ecef[2], utc)
5445

55-
eci_m = ecef2eci(eng, matmap3d, utc, ecef)
46+
eci_m = matlab_ecef2eci(eng, matmap3d, utc, ecef)
5647

5748
assert eci_py == approx(eci_m, rel=rtol)
5849

@@ -76,6 +67,6 @@ def test_compare_eci2ecef(matmap3d):
7667

7768
ecef_py = pymap3d.eci2ecef(eci[0], eci[1], eci[2], utc)
7869

79-
ecef_m = eci2ecef(eng, matmap3d, utc, eci)
70+
ecef_m = matlab_eci2ecef(eng, matmap3d, utc, eci)
8071

8172
assert ecef_py == approx(ecef_m, rel=rtol)

0 commit comments

Comments
 (0)