Skip to content

Commit

Permalink
Merge pull request nasa-gcn#1878 from jak574/across-api-ephem-unit-tests
Browse files Browse the repository at this point in the history
ACROSS API: Add unit tests for Ephemeris calculations and others
  • Loading branch information
swyatt7 authored Feb 7, 2024
2 parents 42df852 + 2b627cb commit 47994df
Show file tree
Hide file tree
Showing 13 changed files with 578 additions and 7 deletions.
3 changes: 3 additions & 0 deletions python/across_api/base/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class BaseSchema(BaseModel):

model_config = ConfigDict(from_attributes=True, arbitrary_types_allowed=True)

def __hash__(self):
return hash((type(self),) + tuple(self.__dict__.values()))


class DateRangeSchema(BaseSchema):
"""
Expand Down
11 changes: 9 additions & 2 deletions python/across_api/base/tle.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class TLEBase(ACROSSAPIBase):
# Return values
error: Optional[str]

def __init__(self, epoch: Time):
def __init__(self, epoch: Time, tle: Optional[TLEEntry] = None):
"""
Initialize a TLE object with the given epoch.
Expand All @@ -98,7 +98,10 @@ def __init__(self, epoch: Time):
The epoch of the TLE object.
"""
self.epoch = epoch
self.tles = []
if tle is not None:
self.tles = [tle]
else:
self.tles = []
if self.validate_get():
self.get()

Expand Down Expand Up @@ -342,6 +345,10 @@ def get(self) -> bool:
elif self.epoch > Time.now().utc:
self.epoch = Time.now().utc

# Check if a TLE is loaded manually
if self.tle is not None:
return True

# Fetch TLE from the TLE database
if self.read_tle_db() is True:
if self.tle is not None:
Expand Down
15 changes: 13 additions & 2 deletions python/across_api/burstcube/ephem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.

from typing import Optional
import astropy.units as u # type: ignore
from astropy.time import Time # type: ignore
from cachetools import TTLCache, cached

from ..base.schema import TLEEntry
from ..base.common import ACROSSAPIBase
from ..base.ephem import EphemBase
from ..burstcube.tle import BurstCubeTLE
Expand All @@ -21,7 +23,16 @@ class BurstCubeEphem(EphemBase, ACROSSAPIBase):
# Configuration options
earth_radius = 70 * u.deg # Fix 70 degree Earth radius

def __init__(self, begin: Time, end: Time, stepsize: u.Quantity = 60 * u.s):
self.tle = BurstCubeTLE(begin).tle
def __init__(
self,
begin: Time,
end: Time,
stepsize: u.Quantity = 60 * u.s,
tle: Optional[TLEEntry] = None,
):
# Load TLE data
self.tle = tle
if self.tle is None:
self.tle = BurstCubeTLE(begin).tle
if self.tle is not None:
super().__init__(begin=begin, end=end, stepsize=stepsize)
15 changes: 13 additions & 2 deletions python/across_api/swift/ephem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.

from typing import Optional
import astropy.units as u # type: ignore
from astropy.time import Time # type: ignore
from cachetools import TTLCache, cached

from ..base.common import ACROSSAPIBase
from ..base.ephem import EphemBase
from ..base.schema import TLEEntry
from .tle import SwiftTLE


Expand All @@ -18,7 +20,16 @@ class SwiftEphem(EphemBase, ACROSSAPIBase):
Satellite from TLE.
"""

def __init__(self, begin: Time, end: Time, stepsize: u.Quantity = 60 * u.s):
self.tle = SwiftTLE(begin).tle
def __init__(
self,
begin: Time,
end: Time,
stepsize: u.Quantity = 60 * u.s,
tle: Optional[TLEEntry] = None,
):
# Load TLE data
self.tle = tle
if self.tle is None:
self.tle = SwiftTLE(begin).tle
if self.tle is not None:
super().__init__(begin=begin, end=end, stepsize=stepsize)
Empty file added python/tests/across/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions python/tests/across/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright © 2023 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.

import pytest


class ExpectedResolve:
def __init__(self, name: str, ra: float, dec: float, resolver: str):
self.name = name
self.ra = ra
self.dec = dec
self.resolver = resolver


@pytest.fixture
def expected_resolve_crab():
return ExpectedResolve(name="Crab", ra=83.6287, dec=22.0147, resolver="CDS")


@pytest.fixture
def expected_resolve_ztf():
return ExpectedResolve(
name="ZTF17aabwgbz",
ra=95.85514670221599,
dec=-12.322666705084146,
resolver="ANTARES",
)
19 changes: 19 additions & 0 deletions python/tests/across/test_across_resolve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright © 2023 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.

from across_api.across.resolve import Resolve


def test_resolve_cds(expected_resolve_crab):
resolve = Resolve(expected_resolve_crab.name)
assert abs(resolve.ra - expected_resolve_crab.ra) < 0.1 / 3600
assert abs(resolve.dec - expected_resolve_crab.dec) < 0.1 / 3600
assert resolve.resolver == expected_resolve_crab.resolver


def test_resolve_antares(expected_resolve_ztf):
resolve = Resolve(expected_resolve_ztf.name)
assert abs(resolve.ra - expected_resolve_ztf.ra) < 0.1 / 3600
assert abs(resolve.dec - expected_resolve_ztf.dec) < 0.1 / 3600
assert resolve.resolver == expected_resolve_ztf.resolver
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.


from env import feature, get_features


Expand Down
Empty file added python/tests/ephem/__init__.py
Empty file.
Loading

0 comments on commit 47994df

Please sign in to comment.