Crates.io | practical-astronomy-rust |
lib.rs | practical-astronomy-rust |
version | 0.2.4 |
source | src |
created_at | 2024-04-07 13:28:03.321975 |
updated_at | 2024-04-09 01:32:26.456524 |
description | Algorithms from Practical Astronomy, implemented in Rust |
homepage | |
repository | https://github.com/jfcarr/practical-astronomy-rust |
max_upload_size | |
id | 1199145 |
size | 371,283 |
Algorithms from "Practical Astronomy with your Calculator or Spreadsheet" by Peter Duffett-Smith, implemented in Rust. API documentation is published here.
If you're interested in this topic, please buy the book! It provides far more detail and context.
We'll calculate the circumstances of the April 8 solar eclipse for West Alexandria, Ohio, using the published crate. (You can tweak the inputs, if you like)
First, open a terminal and create a binary application:
cargo new pa_solar_test
Switch to the new project directory, and add a reference to the Practical Astronomy crate:
cargo add practical-astronomy-rust
Then, edit main.rs
and update it to look like this:
use practical_astronomy_rust::eclipses as ECL;
use practical_astronomy_rust::util as UTIL;
fn main() {
// Input values
let local_date_day: f64 = 8.0;
let local_date_month: u32 = 4;
let local_date_year: u32 = 2024;
let is_daylight_saving: bool = true;
let zone_correction_hours: i32 = 5;
let geog_longitude_deg: f64 = -84.53639;
let geog_latitude_deg: f64 = 39.74722;
// Calculate the circumstances of the eclipse
let (
solar_eclipse_certain_date_day,
solar_eclipse_certain_date_month,
solar_eclipse_certain_date_year,
ut_first_contact_hour,
ut_first_contact_minutes,
ut_mid_eclipse_hour,
ut_mid_eclipse_minutes,
ut_last_contact_hour,
ut_last_contact_minutes,
eclipse_magnitude,
) = ECL::solar_eclipse_circumstances(
local_date_day,
local_date_month,
local_date_year,
is_daylight_saving,
zone_correction_hours,
geog_longitude_deg,
geog_latitude_deg,
);
// Results are in Universal Time, so lets adjust them for local
let ut_first_contact_hour_adj: f64 = UTIL::get_local_hour_from_ut(
ut_first_contact_hour,
is_daylight_saving,
zone_correction_hours,
);
let ut_mid_eclipse_hour_adj: f64 = UTIL::get_local_hour_from_ut(
ut_mid_eclipse_hour,
is_daylight_saving,
zone_correction_hours,
);
let ut_last_contact_hour_adj: f64 = UTIL::get_local_hour_from_ut(
ut_last_contact_hour,
is_daylight_saving,
zone_correction_hours,
);
// Display the results
println!("Solar eclipse circumstances:\n\t[Local Date] {}/{}/{}\n\t[DST?] {}\n\t[Zone Correction] {} hours\n\t[Geographical Longitude/Latitude] {} degrees / {} degrees\n\t=\n\t[Certain Date] {}/{}/{}\n\t[First Contact] {}:{}\n\t[Mid Eclipse] {}:{}\n\t[Last Contact] {}:{}\n\t[Magnitude] {}", local_date_month, local_date_day, local_date_year, is_daylight_saving, zone_correction_hours, geog_longitude_deg, geog_latitude_deg, solar_eclipse_certain_date_month, solar_eclipse_certain_date_day, solar_eclipse_certain_date_year, ut_first_contact_hour_adj, ut_first_contact_minutes, ut_mid_eclipse_hour_adj, ut_mid_eclipse_minutes, ut_last_contact_hour_adj, ut_last_contact_minutes, eclipse_magnitude);
}
Save the file, and run it:
cargo run
You should see this:
Solar eclipse circumstances:
[Local Date] 4/8/2024
[DST?] true
[Zone Correction] 5 hours
[Geographical Longitude/Latitude] -84.53639 degrees / 39.74722 degrees
=
[Certain Date] 4/8/2024
[First Contact] 13:55
[Mid Eclipse] 15:11
[Last Contact] 16:27
[Magnitude] 1.006