#date-time #moon #practical #coordinates #sun #planet #astronomy

practical-astronomy-rust

Algorithms from Practical Astronomy, implemented in Rust

5 releases

0.2.4 Apr 9, 2024
0.2.3 Apr 9, 2024
0.2.2 Apr 7, 2024
0.2.1 Apr 7, 2024
0.2.0 Apr 7, 2024

#136 in Date and time

Download history 3/week @ 2024-09-18 2/week @ 2024-09-25

123 downloads per month

MIT license

375KB
10K SLoC

Practical Astronomy in Rust

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.

Quick Start

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

Library Functions - Status

Date/Time

  • Calculate -> Date of Easter
  • Convert -> Civil Date to Day Number
  • Convert -> Civil Time <-> Decimal Hours
  • Extract -> Hour, Minutes, and Seconds parts of Decimal Hours
  • Convert -> Local Civil Time <-> Universal Time
  • Convert -> Universal Time <-> Greenwich Sidereal Time
  • Convert -> Greenwich Sidereal Time <-> Local Sidereal Time
  • Calculate -> Day of Week for Julian Date

Coordinates

  • Convert -> Angle <-> Decimal Degrees
  • Convert -> Right Ascension <-> Hour Angle
  • Convert -> Equatorial Coordinates <-> Horizon Coordinates
  • Calculate -> Obliquity of the Ecliptic
  • Convert -> Ecliptic Coordinates <-> Equatorial Coordinates
  • Convert -> Equatorial Coordinates <-> Galactic Coordinates
  • Calculate -> Angle between two objects
  • Calculate -> Rising and Setting times for an object
  • Calculate -> Precession (corrected coordinates between two epochs)
  • Calculate -> Nutation (in ecliptic longitude and obliquity) for a Greenwich date
  • Calculate -> Effects of aberration for ecliptic coordinates
  • Calculate -> RA and Declination values, corrected for atmospheric refraction
  • Calculate -> RA and Declination values, corrected for geocentric parallax
  • Calculate -> Heliographic coordinates
  • Calculate -> Carrington rotation number
  • Calculate -> Selenographic (lunar) coordinates (sub-Earth and sub-Solar)

The Sun

  • Calculate -> Approximate and precise positions of the Sun
  • Calculate -> Sun's distance and angular size
  • Calculate -> Local sunrise and sunset
  • Calculate -> Morning and evening twilight
  • Calculate -> Equation of time
  • Calculate -> Solar elongation

Planets

  • Calculate -> Approximate position of planet
  • Calculate -> Precise position of planet
  • Calculate -> Visual aspects of planet (distance, angular diameter, phase, light time, position angle of bright limb, and apparent magnitude)
  • Calculate -> Position of comet (elliptical and parabolic)
  • Calculate -> Binary star orbit data

The Moon

  • Calculate -> Approximate and precise position of Moon
  • Calculate -> Moon phase and position angle of bright limb
  • Calculate -> Times of new Moon and full Moon
  • Calculate -> Moon's distance, angular diameter, and horizontal parallax
  • Calculate -> Local moonrise and moonset

Eclipses

  • Calculate -> Lunar eclipse occurrence and circumstances
  • Calculate -> Solar eclipse occurrence and circumstances

Dependencies

~475KB