Skip to content

Commit

Permalink
fix: make Time and TimeScale pickable
Browse files Browse the repository at this point in the history
  • Loading branch information
helgee committed Feb 11, 2025
1 parent 2d1e3f2 commit 3bf494d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
9 changes: 4 additions & 5 deletions crates/lox-orbits/src/ground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

use std::f64::consts::FRAC_PI_2;

use crate::frames::{DynFrame, Iau, Icrf, TryRotateTo};
use crate::propagators::Propagator;
use crate::states::{DynState, State};
use crate::trajectories::{DynTrajectory, Trajectory, TrajectoryError};
use glam::{DMat3, DVec3};
use lox_bodies::{DynOrigin, RotationalElements, Spheroid, TrySpheroid};
use lox_math::types::units::Radians;
Expand All @@ -17,11 +21,6 @@ use lox_time::ut1::DeltaUt1TaiProvider;
use lox_time::{DynTime, Time};
use thiserror::Error;

use crate::frames::{DynFrame, Iau, Icrf, TryRotateTo};
use crate::propagators::Propagator;
use crate::states::{DynState, State};
use crate::trajectories::{DynTrajectory, Trajectory, TrajectoryError};

#[derive(Clone, Debug)]
pub struct Observables {
azimuth: Radians,
Expand Down
7 changes: 6 additions & 1 deletion crates/lox-space/lox_space.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ class GroundLocation:
def longitude(self) -> float: ...
def latitude(self) -> float: ...
def altitude(self) -> float: ...
def observables(self) -> Observables: ...
def observables(
self,
state: State,
provider: UT1Provider | None = None,
frame: Frame | None = Frame("IAU_EARTH"),
) -> Observables: ...
def rotation_to_topocentric(self) -> np.ndarray: ...

class GroundPropagator:
Expand Down
22 changes: 22 additions & 0 deletions crates/lox-space/tests/test_ground.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import lox_space as lox
import numpy as np
import pytest


def test_observables():
longitude = np.radians(-4)
latitude = np.radians(41)
location = lox.GroundLocation(lox.Origin("Earth"), longitude, latitude, 0)
position = (3359.927, -2398.072, 5153.0)
velocity = (5.0657, 5.485, -0.744)
time = lox.Time("TDB", 2012, 7, 1)
state = lox.State(time, position, velocity, frame=lox.Frame("IAU_EARTH"))
observables = location.observables(state)
expected_range = 2707.7
expected_range_rate = -7.16
expected_azimuth = np.radians(-53.418)
expected_elevation = np.radians(-7.077)
assert observables.range() == pytest.approx(expected_range, rel=1e-2)
assert observables.range_rate() == pytest.approx(expected_range_rate, rel=1e-2)
assert observables.azimuth() == pytest.approx(expected_azimuth, rel=1e-2)
assert observables.elevation() == pytest.approx(expected_elevation, rel=1e-2)
9 changes: 4 additions & 5 deletions crates/lox-space/tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
@pytest.mark.parametrize(
"obj",
[
# lox.Sun(),
# lox.Barycenter("Solar System Barycenter"),
# lox.Planet("Earth"),
# lox.Satellite("Moon"),
# lox.MinorBody("Ceres"),
lox.Origin("Earth"),
lox.Frame("ICRF"),
lox.ElevationMask.fixed(0.0),
lox.TimeScale("TAI"),
lox.Time("TAI", 2000, 1, 1),
],
)
def test_pickle(obj):
Expand Down
17 changes: 16 additions & 1 deletion crates/lox-time/src/python/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::str::FromStr;
use pyo3::basic::CompareOp;
use pyo3::exceptions::{PyTypeError, PyValueError};
use pyo3::types::{PyAnyMethods, PyType};
use pyo3::{pyclass, pymethods, Bound, PyAny, PyErr, PyObject, PyResult, Python};
use pyo3::{pyclass, pymethods, Bound, IntoPyObjectExt, PyAny, PyErr, PyObject, PyResult, Python};

use lox_math::is_close::IsClose;

Expand Down Expand Up @@ -94,6 +94,21 @@ impl PyTime {
Ok(PyTime(time))
}

fn __getnewargs__<'py>(
&self,
py: Python<'py>,
) -> (Bound<'py, PyAny>, i64, u8, u8, u8, u8, f64) {
(
self.scale().into_bound_py_any(py).unwrap(),
self.0.year(),
self.0.month(),
self.0.day(),
self.0.hour(),
self.0.minute(),
self.0.decimal_seconds(),
)
}

#[classmethod]
#[pyo3(signature = (scale, jd, epoch = "jd"))]
pub fn from_julian_date(
Expand Down
5 changes: 4 additions & 1 deletion crates/lox-time/src/python/time_scales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl From<Ut1Error> for PyErr {
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[pyclass(name = "TimeScale", module = "lox_space", frozen)]
#[pyclass(name = "TimeScale", module = "lox_space", frozen, eq)]
pub struct PyTimeScale(pub DynTimeScale);

#[pymethods]
Expand All @@ -36,6 +36,9 @@ impl PyTimeScale {
pub fn new(abbreviation: &str) -> PyResult<Self> {
Ok(PyTimeScale(abbreviation.parse()?))
}
fn __getnewargs__(&self) -> (String,) {
(self.abbreviation(),)
}

pub fn __repr__(&self) -> String {
format!("TimeScale(\"{}\")", self.0)
Expand Down

0 comments on commit 3bf494d

Please sign in to comment.