From 3506b7fd49a32deff31f1376b024f2d2e2842c24 Mon Sep 17 00:00:00 2001 From: scivision Date: Wed, 8 Jan 2025 00:43:42 -0500 Subject: [PATCH] azel*radec: test, functionalize Vallado non-Astropy --- src/pymap3d/__init__.py | 2 +- src/pymap3d/azelradec.py | 50 ++++++++++++++++++++++-------- src/pymap3d/sidereal.py | 4 +-- src/pymap3d/tests/test_sidereal.py | 3 +- src/pymap3d/tests/test_sky.py | 19 ++++++++++++ src/pymap3d/vallado.py | 2 +- 6 files changed, 62 insertions(+), 18 deletions(-) diff --git a/src/pymap3d/__init__.py b/src/pymap3d/__init__.py index 0544080..7541de8 100644 --- a/src/pymap3d/__init__.py +++ b/src/pymap3d/__init__.py @@ -29,7 +29,7 @@ * Fortran: [Maptran3D](https://github.com/geospace-code/maptran3d) """ -__version__ = "3.1.0" +__version__ = "3.1.1" from .aer import aer2ecef, aer2geodetic, ecef2aer, geodetic2aer from .ecef import ( diff --git a/src/pymap3d/azelradec.py b/src/pymap3d/azelradec.py index 7448bbb..1c6598f 100644 --- a/src/pymap3d/azelradec.py +++ b/src/pymap3d/azelradec.py @@ -52,17 +52,24 @@ def azel2radec( """ try: - obs = EarthLocation(lat=lat_deg * u.deg, lon=lon_deg * u.deg) + return azel2radec_astropy(az_deg, el_deg, lat_deg, lon_deg, time) + except NameError: + return vazel2radec(az_deg, el_deg, lat_deg, lon_deg, time) - direc = AltAz( - location=obs, obstime=Time(str2dt(time)), az=az_deg * u.deg, alt=el_deg * u.deg - ) - sky = SkyCoord(direc.transform_to(ICRS())) +def azel2radec_astropy( + az_deg: float, el_deg: float, lat_deg: float, lon_deg: float, time: datetime +) -> tuple[float, float]: + """azel2radec using Astropy + see azel2radec() for description + """ + obs = EarthLocation(lat=lat_deg * u.deg, lon=lon_deg * u.deg) - return sky.ra.deg, sky.dec.deg - except NameError: - return vazel2radec(az_deg, el_deg, lat_deg, lon_deg, time) + direc = AltAz(location=obs, obstime=Time(str2dt(time)), az=az_deg * u.deg, alt=el_deg * u.deg) + + sky = SkyCoord(direc.transform_to(ICRS())) + + return sky.ra.deg, sky.dec.deg def radec2azel( @@ -97,10 +104,27 @@ def radec2azel( """ try: - obs = EarthLocation(lat=lat_deg * u.deg, lon=lon_deg * u.deg) - points = SkyCoord(Angle(ra_deg, unit=u.deg), Angle(dec_deg, unit=u.deg), equinox="J2000.0") - altaz = points.transform_to(AltAz(location=obs, obstime=Time(str2dt(time)))) - - return altaz.az.degree, altaz.alt.degree + return radec2azel_astropy(ra_deg, dec_deg, lat_deg, lon_deg, time) except NameError: return vradec2azel(ra_deg, dec_deg, lat_deg, lon_deg, time) + + +def radec2azel_astropy( + ra_deg: float, + dec_deg: float, + lat_deg: float, + lon_deg: float, + time: datetime, +) -> tuple[float, float]: + """ + rade2azel using Astropy + see radec2azel() for description + """ + + obs = EarthLocation(lat=lat_deg * u.deg, lon=lon_deg * u.deg) + + points = SkyCoord(Angle(ra_deg, unit=u.deg), Angle(dec_deg, unit=u.deg), equinox="J2000.0") + + altaz = points.transform_to(AltAz(location=obs, obstime=Time(str2dt(time)))) + + return altaz.az.degree, altaz.alt.degree diff --git a/src/pymap3d/sidereal.py b/src/pymap3d/sidereal.py index 42cb54b..f4b352f 100644 --- a/src/pymap3d/sidereal.py +++ b/src/pymap3d/sidereal.py @@ -44,7 +44,7 @@ def datetime2sidereal(time: datetime, lon_radians: float) -> float: def datetime2sidereal_astropy(t: datetime, lon_radians: float) -> float: - """ datetime to sidereal time using astropy + """datetime to sidereal time using astropy see datetime2sidereal() for description """ @@ -54,7 +54,7 @@ def datetime2sidereal_astropy(t: datetime, lon_radians: float) -> float: def datetime2sidereal_vallado(t: datetime, lon_radians: float) -> float: - """ datetime to sidereal time using Vallado methods + """datetime to sidereal time using Vallado methods see datetime2sidereal() for description """ diff --git a/src/pymap3d/tests/test_sidereal.py b/src/pymap3d/tests/test_sidereal.py index 6ce503b..4ef755a 100755 --- a/src/pymap3d/tests/test_sidereal.py +++ b/src/pymap3d/tests/test_sidereal.py @@ -23,12 +23,13 @@ def test_sidereal(time): def test_sidereal_astropy(): + pytest.importorskip("astropy") tsr = pmd.datetime2sidereal_astropy(t0, radians(lon)) assert tsr == approx(sra, rel=1e-5) assert isinstance(tsr, float) -def test_sidereal_valado(): +def test_sidereal_vallado(): tsr = pmd.datetime2sidereal_vallado(t0, radians(lon)) assert tsr == approx(sra, rel=1e-5) assert isinstance(tsr, float) diff --git a/src/pymap3d/tests/test_sky.py b/src/pymap3d/tests/test_sky.py index 778e2f9..5284c6c 100755 --- a/src/pymap3d/tests/test_sky.py +++ b/src/pymap3d/tests/test_sky.py @@ -1,6 +1,7 @@ from datetime import datetime import pymap3d as pm +import pymap3d.vallado as pv import pytest from pytest import approx @@ -14,6 +15,15 @@ def test_azel2radec(): radec1 = pm.azel2radec(*azel, lat, lon, t0) assert radec1 == approx(radec, rel=0.01) + assert isinstance(radec1[0], float) + assert isinstance(radec1[1], float) + + +def test_azel2radec_vallado(): + radec1 = pv.azel2radec(*azel, lat, lon, t0) + assert radec1 == approx(radec, rel=0.01) + assert isinstance(radec1[0], float) + assert isinstance(radec1[1], float) def test_numpy_azel2radec(): @@ -25,6 +35,15 @@ def test_numpy_azel2radec(): def test_radec2azel(): azel1 = pm.radec2azel(*radec, lat, lon, t0) assert azel1 == approx(azel, rel=0.01) + assert isinstance(azel1[0], float) + assert isinstance(azel1[1], float) + + +def test_radec2azel_vallado(): + azel1 = pv.radec2azel(*radec, lat, lon, t0) + assert azel1 == approx(azel, rel=0.01) + assert isinstance(azel1[0], float) + assert isinstance(azel1[1], float) def test_numpy_radec2azel(): diff --git a/src/pymap3d/vallado.py b/src/pymap3d/vallado.py index 4286b37..7205359 100644 --- a/src/pymap3d/vallado.py +++ b/src/pymap3d/vallado.py @@ -1,7 +1,7 @@ """ converts right ascension, declination to azimuth, elevation and vice versa. Normally do this via AstroPy. -These functions are fallbacks for those wihtout AstroPy. +These functions are fallbacks for those without AstroPy. Michael Hirsch implementation of algorithms from D. Vallado """