Skip to content

Commit

Permalink
Reduced code duplication by using generics. Updated Examples to use n…
Browse files Browse the repository at this point in the history
…ew Unit defintions.

Already prepared generics for the use with f64.

Co-Authored-By: Dominic <git@msrd0.de>
  • Loading branch information
NiklasVousten and msrd0 committed Oct 31, 2023
1 parent 45e3249 commit a8edeca
Show file tree
Hide file tree
Showing 12 changed files with 293 additions and 339 deletions.
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ serde = { version = "1.0.143", features = ["derive"] }
thiserror = "1.0.32"
tokio = { version = "1.20.1", features = ["time"] }
uom = { version = "0.35.0", optional = true}
num-traits = "0.2.17"

[dev-dependencies]
env_logger = "0.10.0"
tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }

[features]
si = ["dep:uom"]
si = ["dep:uom"]

[[example]]
name = "ephemeris_orbital_elements_si"
required-features = ["si"]
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use chrono::{Duration, Utc};
use rhorizons::{ephemeris_orbital_elements, major_bodies};
use rhorizons::{
ephemeris_orbital_elements_si, major_bodies, EphemerisOrbitalElementsItem, SiUnits,
};

#[tokio::main]
async fn main() {
Expand All @@ -22,10 +24,12 @@ async fn main() {
start_time, stop_time
);

for elements in ephemeris_orbital_elements(earth.id, start_time, stop_time).await {
let elements: Vec<EphemerisOrbitalElementsItem<f32, SiUnits>> =
ephemeris_orbital_elements_si(earth.id, start_time, stop_time).await;
for item in elements {
println!(
"Eccentricity: {:?}, Semi-major axis: {:?}, Inclination: {:?}, Longitude of ascending node: {:?}, Argument of perifocus: {:?}, Mean anomaly: {:?}",
elements.eccentricity, elements.semi_major_axis, elements.inclination, elements.longitude_of_ascending_node, elements.argument_of_perifocus, elements.mean_anomaly
item.eccentricity, item.semi_major_axis, item.inclination, item.longitude_of_ascending_node, item.argument_of_perifocus, item.mean_anomaly
);
}
}
9 changes: 6 additions & 3 deletions examples/ephemeris_vector.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use chrono::{Duration, Utc};
use rhorizons::{ephemeris_vector, major_bodies};
use rhorizons::{ephemeris_vector, major_bodies, DefaultUnits, EphemerisVectorItem};

#[tokio::main]
async fn main() {
Expand All @@ -22,10 +22,13 @@ async fn main() {
start_time, stop_time
);

for vectors in ephemeris_vector(earth.id, start_time, stop_time).await {
let vectors: Vec<EphemerisVectorItem<f32, DefaultUnits>> =
ephemeris_vector(earth.id, start_time, stop_time).await;

for item in vectors {
println!(
"position: {:?}, velocity: {:?}",
vectors.position, vectors.velocity
item.position, item.velocity
);
}
}
40 changes: 38 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub async fn ephemeris_vector(
id: i32,
start_time: DateTime<Utc>,
stop_time: DateTime<Utc>,
) -> Vec<EphemerisVectorItem> {
) -> Vec<EphemerisVectorItem<f32, crate::units::DefaultUnits>> {
let result = query_with_retries(&[
("COMMAND", id.to_string().as_str()),
// Select Sun as a observer. Note that Solar System Barycenter is in a
Expand Down Expand Up @@ -106,7 +106,7 @@ pub async fn ephemeris_orbital_elements(
id: i32,
start_time: DateTime<Utc>,
stop_time: DateTime<Utc>,
) -> Vec<EphemerisOrbitalElementsItem> {
) -> Vec<EphemerisOrbitalElementsItem<f32, crate::units::DefaultUnits>> {
let result = query_with_retries(&[
("COMMAND", id.to_string().as_str()),
// Select Sun as a observer. Note that Solar System Barycenter is in a
Expand All @@ -128,3 +128,39 @@ pub async fn ephemeris_orbital_elements(

EphemerisOrbitalElementsParser::parse(result.iter().map(String::as_str)).collect()
}

#[cfg(feature = "si")]
/// Get vector ephemeris (position and velocity) of a major body in SI-units. Coordinates are
/// relative to the Sun's center.
/// Needs the `si` feature to be enabled
///
/// SI-units from the crate *uom*: <https://docs.rs/uom/0.35.0/uom/>
pub async fn ephemeris_vector_si(
id: i32,
start_time: DateTime<Utc>,
stop_time: DateTime<Utc>,
) -> Vec<EphemerisVectorItem<f32, crate::units::SiUnits>> {
crate::ephemeris_vector(id, start_time, stop_time)
.await
.into_iter()
.map(EphemerisVectorItem::from)
.collect()
}

#[cfg(feature = "si")]
/// Get orbital element ephemeris (e.g. eccentricity, semi-major axis, ...) of a
/// major body relative to the Sun's center in SI-units.
/// Needs the `si` feature to be enabled
///
/// SI-units from the crate *uom*: <https://docs.rs/uom/0.35.0/uom/>
pub async fn ephemeris_orbital_elements_si(
id: i32,
start_time: DateTime<Utc>,
stop_time: DateTime<Utc>,
) -> Vec<EphemerisOrbitalElementsItem<f32, crate::units::SiUnits>> {
crate::ephemeris_orbital_elements(id, start_time, stop_time)
.await
.into_iter()
.map(EphemerisOrbitalElementsItem::from)
.collect()
}
Loading

0 comments on commit a8edeca

Please sign in to comment.