micropython-samples/astronomy
Peter Hinch d29fb19eb4 astronomy/sun_moon.py: Beta version and docs. 2023-11-29 12:13:28 +00:00
..
README.md astronomy/sun_moon.py: Beta version and docs. 2023-11-29 12:13:28 +00:00
sun_moon.py astronomy/sun_moon.py: Beta version and docs. 2023-11-29 12:13:28 +00:00

README.md

Astronomical calculations in MicroPython

This module enables sun and moon rise and set times to be determined at any geographical location. Times are in seconds from midnight and refer to any event in a 24 hour period starting at midnight. The midnight datum is defined in local time. The start is a day being the current day plus an offset in days.

A moonphase function is also provided enabling the moon phase to be determined for any date.

The code was ported from C/C++ as presented in "Astronomy on the Personal Computer" by Montenbruck and Pfleger, with mathematical improvements contributed by Raul Kompaß and Marcus Mendenhall.

Caveat. I am not an astronomer. If there are errors in the fundamental algorithms I am unlikely to be able to offer an opinion, still less a fix.

The code is currently under development: the API may change.

The RiSet class

Constructor

Args (float):

  • lat=LAT Latitude in degrees. Defaults are my location. :)
  • long=LONG Longitude in degrees (-ve is West).
  • lto=0 Local time offset in hours (-ve is West).

Methods:

  • set_day(day: int = 0) The arg is the offset from the current system date. Calling this with a changed arg causes the rise and set times to be updated.
  • sunrise(variant: int = 0) See below for details and the variant arg.
  • sunset(variant: int = 0)
  • moonrise(variant: int = 0)
  • moonset(variant: int = 0)
  • moonphase() Return current phase as a float: 0.0 <= result < 1.0. 0.0 is new moon, 0.5 is full.

The return value of the rise and set method is determined by the variant arg. In all cases rise and set events are identified which occur in the current 24 hour period. Note that a given event may be absent in the period: this can occur with the moon at most locations, and with the sun in polar regions.

Variants:

  • 0 Return integer seconds since midnight local time (or None if no event).
  • 1 Return integer seconds since since epoch of the MicroPython platform (or None).
  • 2 Return text of form hh:mm:ss (or --:--:--) being local time.

The moonphase function

This is a simple function whose provenance is uncertain. I have a lunar clock which uses a C version of this which has run for 14 years without issue, but I can't vouch for its absolute accuracy over long time intervals. The Montenbruck and Pfleger version is very much more involved but they claim accuracy over centuries.

Args (all integers):

  • year 4-digit year
  • month 1..12
  • day Day of month 1..31
  • hour 0..23

Return value:
A float in range 0.0 <= result < 1.0, 0 being new moon, 0.5 being full moon.