Skip to content

Commit

Permalink
compare ecef2eci: run all tests, print details
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Nov 20, 2023
1 parent b41016f commit 8347339
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
46 changes: 25 additions & 21 deletions scripts/compare/compare_ecef2eci.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,49 @@
#!/usr/bin/env python3
from __future__ import annotations

import logging
from datetime import datetime

from pytest import approx
import numpy as np

from pymap3d.eci import ecef2eci, eci2ecef

from matlab_aerospace import matlab_aerospace
from matlab_engine import matlab_engine


def test_ecef_eci():
ecef = [-5762640.0, -1682738.0, 3156028.0]
utc = datetime(2019, 1, 4, 12)
ecef = [-5762640.0, -1682738.0, 3156028.0]
utc = datetime(2019, 1, 4, 12)

eci = ecef2eci(*ecef, utc)
eci_py = ecef2eci(ecef[0], ecef[1], ecef[2], utc)

eng = matlab_engine()
eng = matlab_engine()

utc_matlab = eng.datetime(utc.year, utc.month, utc.day, utc.hour, utc.minute, utc.second)
if matlab_aerospace(eng):
eci_matlab = eng.ecef2eci(utc_matlab, *ecef, nargout=3)
else:
eci_matlab = eng.matmap3d.ecef2eci(utc_matlab, *ecef, nargout=3)
utc_matlab = eng.datetime(utc.year, utc.month, utc.day, utc.hour, utc.minute, utc.second)
if matlab_aerospace(eng):
eci_matlab = eng.ecef2eci(utc_matlab, np.array(ecef), nargout=3)
else:
eci_matlab = eng.matmap3d.ecef2eci(utc_matlab, *ecef, nargout=3)

assert eci == approx(eci_matlab, rel=0.01)
eci_good = np.isclose(eci_py, eci_matlab, rtol=0.01).all()

if eci_good:
print("OK: PyMap3d ecef2eci vs. Matlab ecef2eci")
else:
logging.error("eci2ecef did not match Matlab")

ecef = eci2ecef(*eci_matlab, utc)
ecef_py = eci2ecef(eci_matlab[0], eci_matlab[1], eci_matlab[2], utc)

if matlab_aerospace(eng):
ecef_matlab = eng.eci2ecef(utc_matlab, *eci_matlab, nargout=3)
else:
ecef_matlab = eng.matmap3d.eci2ecef(utc_matlab, *eci_matlab, nargout=3)
if matlab_aerospace(eng):
ecef_matlab = eng.eci2ecef(utc_matlab, *eci_matlab, nargout=3)
else:
ecef_matlab = eng.matmap3d.eci2ecef(utc_matlab, *eci_matlab, nargout=3)

assert ecef == approx(ecef_matlab, rel=0.02)
ecef_good = np.isclose(ecef_py, ecef_matlab, rtol=0.02).all()

if ecef_good:
print("OK: PyMap3d eci2ecef vs. Matlab eci2ecef")
else:
logging.error("eci2ecef did not match Matlab")


if __name__ == "__main__":
test_ecef_eci()
assert ecef_good and eci_good, "Matlab compare mismatch"
2 changes: 1 addition & 1 deletion src/pymap3d/eci.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def eci2ecef(x, y, z, time: datetime) -> tuple:
y = atleast_1d(y)
z = atleast_1d(z)
gst = atleast_1d(greenwichsrt(juliandate(time)))
assert x.shape == y.shape == z.shape
assert x.shape == y.shape == z.shape, f"shape mismatch: x: ${x.shape} y: {y.shape} z: {z.shape}"
assert x.size == gst.size

eci = column_stack((x.ravel(), y.ravel(), z.ravel()))
Expand Down

0 comments on commit 8347339

Please sign in to comment.