Current section
Files
Jump to
Current section
Files
lib/astro.ex
defmodule Astro do
@moduledoc """
High-level API for common astronomical observations.
This module is the primary public interface for the Astro library.
It provides functions for sunrise/sunset, moonrise/moonset,
equinoxes and solstices, lunar phases, and sun/moon position.
All functions accept standard Elixir `Date` or `DateTime` structs
and return `{:ok, value}` or `{:error, reason}` tuples.
For lower-level access see `Astro.Solar`, `Astro.Lunar`,
`Astro.Time`, `Astro.Earth` and `Astro.Ephemeris`.
## Specifying a location
Location is specified as a `{longitude, latitude}` tuple (note
the order, matching `Geo.Point`), a `Geo.Point.t` struct, or a
`Geo.PointZ.t` struct that includes elevation in meters.
* Longitude is `+` for east, `-` for west, in degrees.
* Latitude is `+` for north, `-` for south, in degrees.
## Time zone resolution
Rise/set functions (`sunrise/3`, `sunset/3`, `moonrise/3`,
`moonset/3`) return a `DateTime` in the local time zone of the
given location. By default the time zone is resolved via
`TzWorld` (if configured). This can be overridden with the
following options:
* `:time_zone` — a zone name string, `:utc`, or `:default`
(resolve from coordinates via `TzWorld`).
* `:time_zone_database` — the time zone database module
(e.g. Tz.TimeZoneDatabase).
* `:time_zone_resolver` — a custom 1-arity function
`(%Geo.Point{}) → {:ok, String.t()}`.
## Function groups
### Solar
* `sunrise/3`, `sunset/3` — local sunrise and sunset times
* `solar_noon/2` — UTC solar noon for a location and date
* `hours_of_daylight/2` — duration of daylight
* `sun_position_at/1` — right ascension, declination and distance
* `sun_azimuth_elevation/2` — azimuth and altitude at a datetime
* `sun_apparent_longitude/1` — apparent ecliptic longitude
### Lunar
* `moonrise/3`, `moonset/3` — local moonrise and moonset times
* `moon_position_at/1` — right ascension, declination and distance
* `illuminated_fraction_of_moon_at/1` — fraction of the Moon illuminated
* `lunar_phase_at/1` — phase angle (0–360°)
* `lunar_phase_emoji/1` — Unicode emoji for a phase angle
### New moon search
* `date_time_new_moon_before/1`, `date_time_new_moon_at_or_after/1`
* `date_time_new_moon_nearest/1`
### Phase search
* `date_time_lunar_phase_at_or_before/2`
* `date_time_lunar_phase_at_or_after/2`
### Crescent visibility
* `new_visible_crescent/3` — predict visibility of the new crescent moon
### Equinoxes and solstices
* `equinox/2` — March or September equinox
* `solstice/2` — June or December solstice
"""
alias Astro.{Solar, Lunar, Location, Time, Math, Guards}
import Astro.Math,
only: [
sin: 1,
cos: 1,
mod: 2,
to_degrees: 1
]
@type longitude :: float()
@type latitude :: float()
@type altitude :: float()
@type degrees :: float()
@type radians :: float()
@type angle() :: number()
@type meters() :: number()
@type astronomical_units() :: number()
@type kilometers() :: number()
@type phase() :: angle()
@type location :: {longitude, latitude} | Geo.Point.t() | Geo.PointZ.t()
@type date :: Calendar.date() | Calendar.datetime()
@type options :: keyword()
@seconds_per_day 86_400
# Selects the preferred time zone database at compile time based on which
# optional dependency (`:tzdata` or `:tz`) is available. The `:elixir`
# `:time_zone_database` application config still takes precedence at
# runtime — see `default_options/0`.
@compile_time_time_zone_db (cond do
Code.ensure_loaded?(Tzdata.TimeZoneDatabase) ->
Tzdata.TimeZoneDatabase
Code.ensure_loaded?(Tz.TimeZoneDatabase) ->
Tz.TimeZoneDatabase
true ->
nil
end)
defguard is_lunar_phase(phase) when phase >= 0.0 and phase <= 360.0
@doc """
Returns a tuple `{azimuth, altitude}` for a given
date time and location.
### Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired sunrise
azimuth and altitude. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation
* a `Geo.PointZ.t` struct to represent a location and elevation
* `date_time` is a `t:DateTime.t/0` or a `t:Date.t/0` or
any struct that meets the requirements of
`t:Calendar.date/0` or `t:Calendar.datetime/0`.
### Returns
* a tuple of the format `{azimith, altitude}` which are
expressed in float degrees.
### Example
iex> {:ok, date_time} = DateTime.new(~D[2023-05-17], ~T[12:47:00], "Australia/Sydney")
iex> location = {151.1637781, -33.5145852}
iex> {_azimuth, _altitude} = Astro.sun_azimuth_elevation(location, date_time)
"""
# Use https://midcdmz.nrel.gov/solpos/spa.html for validation
# current implementation is approx 1 degree at variance with
# that calculator.
@doc since: "0.11.0"
@spec sun_azimuth_elevation(location(), Calendar.datetime()) ::
{azimuth :: float, altitude :: float}
def sun_azimuth_elevation(location, unquote(Guards.datetime()) = date_time) do
_ = calendar
%Geo.PointZ{coordinates: {right_ascension, declination, _distance}} =
sun_position_at(date_time)
%Geo.PointZ{coordinates: {_longitude, latitude, _altitude}} =
Location.normalize_location(location)
local_sidereal_time =
Time.local_sidereal_time(location, date_time)
hour_angle =
mod(local_sidereal_time - right_ascension, 360.0)
altitude =
:math.asin(
sin(declination) * sin(latitude) + cos(declination) * cos(latitude) * cos(hour_angle)
)
|> to_degrees
a =
:math.acos(
(sin(declination) - sin(altitude) * sin(latitude)) / (cos(altitude) * cos(latitude))
)
|> to_degrees()
azimuth =
if sin(hour_angle) < 0.0, do: a, else: 360.0 - a
{azimuth, altitude}
end
@doc """
Returns a `t:Geo.PointZ` containing
the right ascension and declination of
the sun at a given date or date time.
### Arguments
* `date_time` is a `t:DateTime.t/0` or a `t:Date/0` or
any struct that meets the requirements of
`t:Calendar.date/0` or `t:Calendar.datetime/0`.
### Returns
* a `t:Geo.PointZ.t/0` struct with coordinates
`{right_ascension, declination, distance}` with properties
`%{reference: :celestial, object: :sun}`.
`distance` is in meters.
### Example
iex> Astro.sun_position_at(~D[1992-10-13])
%Geo.PointZ{
coordinates: {-161.61854343627374, -7.785324796344723, 149169604737.93973},
properties: %{object: :sun, reference: :celestial},
srid: nil
}
"""
@doc since: "0.6.0"
@spec sun_position_at(date()) :: Geo.PointZ.t()
def sun_position_at(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Solar.solar_position()
|> convert_distance_to_m()
|> Location.normalize_location()
|> Map.put(:properties, %{reference: :celestial, object: :sun})
end
def sun_position_at(unquote(Guards.date()) = date) do
_ = calendar
date
|> Time.date_time_to_moment()
|> Solar.solar_position()
|> convert_distance_to_m()
|> Location.normalize_location()
|> Map.put(:properties, %{reference: :celestial, object: :sun})
end
defp convert_distance_to_m({lng, lat, alt}) do
{lng, lat, Math.au_to_m(alt)}
end
@doc """
Returns a `t:Geo.PointZ` containing
the right ascension and declination of
the moon at a given date or date time.
### Arguments
* `date_time` is a `t:DateTime.t/0` or a `t:Date.t/0` or
any struct that meets the requirements of
`t:Calendar.date/0` or `t:Calendar.datetime/0`.
### Returns
* a `t:Geo.PointZ` struct with coordinates
`{right_ascension, declination, distance}` with properties
`%{reference: :celestial, object: :moon}`
`distance` is in meters.
### Example
iex> Astro.moon_position_at(~D[1992-04-12]) |> Astro.Location.round(6)
%Geo.PointZ{
coordinates: {134.69343, 13.766512, 368409007.322444},
properties: %{object: :moon, reference: :celestial},
srid: nil
}
"""
@doc since: "0.6.0"
@spec moon_position_at(date()) :: Geo.PointZ.t()
def moon_position_at(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> moon_position_at_moment()
end
def moon_position_at(unquote(Guards.date()) = date) do
_ = calendar
date
|> Time.date_time_to_moment()
|> moon_position_at_moment()
end
defp moon_position_at_moment(moment) do
moment
|> Lunar.lunar_position()
# |> convert_distance_to_m()
|> Location.normalize_location()
|> Map.put(:properties, %{reference: :celestial, object: :moon})
end
@doc """
Returns the illumination of the moon
as a float for a given date or date time.
### Arguments
* `date_time` is a `t:DateTime.t/0` or a `t:Date.t/0` or
any struct that meets the requirements of
`t:Calendar.date/0` or `t:Calendar.datetime/0`.
### Returns
* a `float` value between `0.0` and `1.0`
representing the fractional illumination of
the moon.
### Example
iex> fraction = Astro.illuminated_fraction_of_moon_at(~D[2017-03-16])
iex> Float.round(fraction, 4)
0.8884
iex> fraction = Astro.illuminated_fraction_of_moon_at(~D[1992-04-12])
iex> Float.round(fraction, 4)
0.6786
"""
@doc since: "0.6.0"
@spec illuminated_fraction_of_moon_at(date()) :: number()
def illuminated_fraction_of_moon_at(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.illuminated_fraction_of_moon()
end
def illuminated_fraction_of_moon_at(unquote(Guards.date()) = date) do
_ = calendar
date
|> Time.date_time_to_moment()
|> Lunar.illuminated_fraction_of_moon()
end
@doc """
Returns the date time of the new
moon before a given date or date time.
### Arguments
* `date_time` is a `t:DateTime.t/0` or a `t:Date.t/0` or
any struct that meets the requirements of
`t:Calendar.date/0` or `t:Calendar.datetime/0`.
### Returns
* `{:ok, date_time}` at which the new moon occurs or
* `{:error, {module, reason}}`
### Example
iex> Astro.date_time_new_moon_before(~D[2021-08-23])
{:ok, ~U[2021-08-08 13:50:07.634598Z]}
"""
@doc since: "0.5.0"
@spec(
date_time_new_moon_before(date()) ::
{:ok, Calendar.datetime()},
{:error, {module(), String.t()}}
)
def date_time_new_moon_at_or_before(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.date_time_new_moon_before()
|> Time.date_time_from_moment()
end
def date_time_new_moon_before(unquote(Guards.date()) = date) do
_ = calendar
date
|> Time.date_time_to_moment()
|> Lunar.date_time_new_moon_before()
|> Time.date_time_from_moment()
end
@doc """
Returns the date time of the new
moon nearest to a given date or date time.
### Arguments
* `date_time` is a `t:DateTime.t/0` or a `t:Date.t/0` or
any struct that meets the requirements of
`t:Calendar.date/0` or `t:Calendar.datetime/0`.
### Returns
* `{:ok, date_time}` at which the new moon occurs or
* `{:error, {module, reason}}`
### Example
iex> Astro.date_time_new_moon_nearest(~D[2021-08-23])
{:ok, ~U[2021-08-08 13:50:07.490242Z]}
"""
@doc since: "2.0.0"
@spec(
date_time_new_moon_nearest(date()) ::
{:ok, Calendar.datetime()},
{:error, {module(), String.t()}}
)
def date_time_new_moon_nearest(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.date_time_new_moon_nearest()
|> Time.date_time_from_moment()
end
def date_time_new_moon_nearest(unquote(Guards.date()) = date) do
_ = calendar
date
|> Time.date_time_to_moment()
|> Lunar.date_time_new_moon_nearest()
|> Time.date_time_from_moment()
end
@doc """
Returns the date time of the new
moon at or after a given date or
date time.
### Arguments
* `date_time` is a `DateTime` or a `Date` or
any struct that meets the requirements of
`t:Calendar.date` or `t:Calendar.datetime`.
### Returns
* `{:ok, date_time}` at which the new moon occurs or
* `{:error, {module, reason}}`
### Example
iex> Astro.date_time_new_moon_at_or_after(~D[2021-08-23])
{:ok, ~U[2021-09-07 00:51:44.267320Z]}
"""
@doc since: "0.5.0"
@spec(
date_time_new_moon_at_or_after(date) ::
{:ok, Calendar.datetime()},
{:error, {module(), String.t()}}
)
def date_time_new_moon_at_or_after(unquote(Guards.datetime()) = datetime) do
_ = calendar
datetime
|> Time.date_time_to_moment()
|> Lunar.date_time_new_moon_at_or_after()
|> Time.date_time_from_moment()
end
def date_time_new_moon_at_or_after(unquote(Guards.date()) = date) do
_ = calendar
date
|> Time.date_time_to_moment()
|> Lunar.date_time_new_moon_at_or_after()
|> Time.date_time_from_moment()
end
@doc """
Returns the lunar phase as a
float number of degrees at a given
date or date time.
### Arguments
* `date_time` is a `t:DateTime.t/0` or a `t:Date.t/0` or
any struct that meets the requirements of
`t:Calendar.date/0` or `t:Calendar.datetime/0`.
### Returns
* the lunar phase as a float number of
degrees.
### Example
iex> Astro.lunar_phase_at ~U[2021-08-22 12:02:02.816534Z]
180.00004404669988
iex> Astro.lunar_phase_at(~U[2021-07-10 01:16:34.022607Z])
3.6600909621461326e-6
"""
@doc since: "0.5.0"
@spec lunar_phase_at(date()) :: phase()
def lunar_phase_at(unquote(Guards.datetime()) = date_time) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.lunar_phase_at()
end
def lunar_phase_at(unquote(Guards.date()) = date) do
_ = calendar
date
|> Time.date_time_to_moment()
|> Lunar.lunar_phase_at()
end
@doc """
Returns the moon phase as a UTF8 binary
representing an emoji of the moon phase.
### Arguments
* `phase` is a moon phase between `0.0` and `360.0`.
### Returns
* A single grapheme string representing the [Unicode
moon phase emoji](https://unicode-table.com/en/sets/moon/).
### Examples
iex> Astro.lunar_phase_emoji 0
"🌑"
iex> Astro.lunar_phase_emoji 45
"🌒"
iex> Astro.lunar_phase_emoji 90
"🌓"
iex> Astro.lunar_phase_emoji 135
"🌔"
iex> Astro.lunar_phase_emoji 180
"🌕"
iex> Astro.lunar_phase_emoji 245
"🌖"
iex> Astro.lunar_phase_emoji 270
"🌗"
iex> Astro.lunar_phase_emoji 320
"🌘"
iex> Astro.lunar_phase_emoji 360
"🌑"
iex> ~U[2021-08-22 12:02:02.816534Z]
...> |> Astro.lunar_phase_at()
...> |> Astro.lunar_phase_emoji()
"🌕"
"""
@emoji_base 0x1F310
@emoji_phase_count 8
@emoji_phase 360.0 / @emoji_phase_count
@spec lunar_phase_emoji(phase()) :: String.t()
def lunar_phase_emoji(360) do
lunar_phase_emoji(0)
end
def lunar_phase_emoji(phase) when is_lunar_phase(phase) do
offset = ceil(phase / @emoji_phase + 0.5)
:unicode.characters_to_binary([offset + @emoji_base])
end
@doc """
Returns the date time of a given
lunar phase at or before a given
date time or date.
### Arguments
* `date_time` is a `t:DateTime.t/0` or a `t:Date.t/0` or
any struct that meets the requirements of
`t:Calendar.date/0` or `t:Calendar.datetime/0`.
* `phase` is the required lunar phase expressed
as a float number of degrees between `0.0` and
`3660.0`.
### Returns
* `{:ok, date_time}` at which the phase occurs or
* `{:error, {module, reason}}`
### Example
iex> Astro.date_time_lunar_phase_at_or_before(~D[2021-08-01], Astro.Lunar.new_moon_phase())
{:ok, ~U[2021-07-10 01:16:34.022607Z]}
"""
@doc since: "0.5.0"
@spec(
date_time_lunar_phase_at_or_before(date(), Astro.phase()) ::
{:ok, Calendar.datetime()},
{:error, {module(), String.t()}}
)
def date_time_lunar_phase_at_or_before(unquote(Guards.datetime()) = date_time, phase) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.date_time_lunar_phase_at_or_before(phase)
|> Time.date_time_from_moment()
end
def date_time_lunar_phase_at_or_before(unquote(Guards.date()) = date, phase) do
_ = calendar
date
|> Time.date_time_to_moment()
|> Lunar.date_time_lunar_phase_at_or_before(phase)
|> Time.date_time_from_moment()
end
@doc """
Returns the date time of a given
lunar phase at or after a given
date time or date.
### Arguments
* `date_time` is a `t:DateTime.t/0` or a `t:Date.t/0` or
any struct that meets the requirements of
`t:Calendar.date/0` or `t:Calendar.datetime/0`.
* `phase` is the required lunar phase expressed
as a float number of degrees between `0.0` and
`360.0`.
### Returns
* `{:ok, date_time}` at which the phase occurs or
* `{:error, {module, reason}}`
### Example
iex> Astro.date_time_lunar_phase_at_or_after(~D[2021-08-01], Astro.Lunar.full_moon_phase())
{:ok, ~U[2021-08-22 12:02:02.816534Z]}
"""
@doc since: "0.5.0"
@spec(
date_time_lunar_phase_at_or_after(date(), Astro.phase()) ::
{:ok, Calendar.datetime()},
{:error, {module(), String.t()}}
)
def date_time_lunar_phase_at_or_after(unquote(Guards.datetime()) = date_time, phase) do
_ = calendar
date_time
|> Time.date_time_to_moment()
|> Lunar.date_time_lunar_phase_at_or_after(phase)
|> Time.date_time_from_moment()
end
def date_time_lunar_phase_at_or_after(unquote(Guards.date()) = date, phase) do
_ = calendar
date
|> Time.date_time_to_moment()
|> Lunar.date_time_lunar_phase_at_or_after(phase)
|> Time.date_time_from_moment()
end
@doc """
Calculates the sunrise for a given location and date.
Sunrise is the moment when the upper limb of
the sun appears on the horizon in the morning.
### Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired sunrise
time. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `t:Geo.Point.t/0` struct to represent a location without elevation
* a `t:Geo.PointZ.t/0` struct to represent a location and elevation
* `date` is a `t:Date.t/0`, `t:NaiveDateTime.t/0` or `t:DateTime.t/0`
to indicate the date of the year in which
the sunrise time is required.
* `options` is a keyword list of options.
### Options
* `solar_elevation` represents the type of sunrise
required. The default is `:geometric` which equates to
a solar elevation of 90°. In this case the calculation
also accounts for refraction and elevation to return a
result which accords with the eye's perception. Other
solar elevations are:
* `:civil` representing a solar elevation of 96.0°. At this
point the sun is just below the horizon so there is
generally enough natural light to carry out most
outdoor activities.
* `:nautical` representing a solar elevation of 102.0°
This is the point at which the horizon is just barely visible
and the moon and stars can still be used for navigation.
* `:astronomical` representing a solar elevation of 108.0°.
This is the point beyond which astronomical observation
becomes impractical.
* Any floating point number representing the desired
solar elevation.
* `:time_zone` is the time zone in which the sunrise
is requested. The default is `:default` in which
the sunrise time is reported in the time zone of
the requested location. `:utc` can be specified or any
other time zone name supported by the option
`:time_zone_database` is acceptabe.
* `:time_zone_database` represents the module that
implements the `Calendar.TimeZoneDatabase` behaviour.
The default is the configured Elixir time zone database or
one of Tzdata.TimeZoneDatabase or Tz.TimeZoneDatabase
depending upon which dependency is configured.
* `:time_zone_resolver` is a 1-arity function that resolves the
time zone name for a given location. The function will receive
a `%Geo.Point{cordinates: {lng, lat}}` struct and is expected to
return either `{:ok, time_zone_name}` or `{:error, :time_zone_not_found}`.
The default is `TzWorld.timezone_at/1` if `:tz_world` is
configured.
### Returns
* a `t:DateTime.t/0` representing the time of sunrise in the
requested time zone at the requested location.
* `{:error, :time_zone_not_found}` if the requested
time zone is unknown.
* `{:error, :time_zone_not_resolved}` if it is not possible
to resolve a time zone name from the location. This can happen
if `:tz_world` is not configured as a dependency and no
`:time_zone_resolver` option is specified.
* `{:error, :no_time}` if for the requested date
and location there is no sunrise. This can occur at
very high and very low latitudes during summer and winter.
### Notes
* If the resolved UTC date time is ambiguous because of a daylight savings
transition, the second of the two possibilities is applied. See
the `DateTime.from_naive/3` for more information.
### Examples
# Sunrise in Sydney, Australia
Astro.sunrise({151.20666584, -33.8559799094}, ~D[2019-12-04])
{:ok, #DateTime<2019-12-04 05:37:00.000000+11:00 AEDT Australia/Sydney>}
# Sunrise in Alert, Nanavut, Canada
Astro.sunrise({-62.3481, 82.5018}, ~D[2019-12-04])
{:error, :no_time}
"""
@spec sunrise(location, date, options) ::
{:ok, DateTime.t()}
| {:error, :time_zone_not_found | :time_zone_not_resolved | :no_time}
def sunrise(location, date, options \\ []) when is_list(options) do
Solar.SunRiseSet.sunrise(location, date_to_moment(date), options)
end
@doc """
Calculates the sunset for a given location and date.
Sunset is the moment when the upper limb of
the sun disappears below the horizon in the evening.
### Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired sunrise
time. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation
* a `Geo.PointZ.t` struct to represent a location and elevation
* `date` is a `t:Date.t/0`, `t:NaiveDateTime.t/0` or `t:DateTime.t/0`
to indicate the date of the year in which
the sunset time is required.
* `options` is a keyword list of options.
### Options
* `solar_elevation` represents the type of sunset
required. The default is `:geometric` which equates to
a solar elevation of 90°. In this case the calulation
also accounts for refraction and elevation to return a
result which accords with the eyes perception. Other
solar elevations are:
* `:civil` representing a solar elevation of 96.0°. At this
point the sun is just below the horizon so there is
generally enough natural light to carry out most
outdoor activities.
* `:nautical` representing a solar elevation of 102.0°
This is the point at which the horizon is just barely visible
and the moon and stars can still be used for navigation.
* `:astronomical`representing a solar elevation of 108.0°.
This is the point beyond which astronomical observation
becomes impractical.
* Any floating point number representing the desired
solar elevation.
* `:time_zone` is the time zone in which the sunset
is requested. The default is `:default` in which
the sunrise time is reported in the time zone of
the requested location. `:utc` can be specified or any
other time zone name supported by the option
`:time_zone_database` is acceptabe.
* `:time_zone_database` represents the module that
implements the `Calendar.TimeZoneDatabase` behaviour.
The default is the configured Elixir time zone database or
one of Tzdata.TimeZoneDatabase or Tz.TimeZoneDatabase
depending upon which dependency is configured.
* `:time_zone_resolver` is a 1-arity function that resolves the
time zone name for a given location. The function will receive
a `%Geo.Point{cordinates: {lng, lat}}` struct and is expected to
return either `{:ok, time_zone_name}` or `{:error, :time_zone_not_found}`.
The default is `TzWorld.timezone_at/1` if `:tz_world` is
configured.
### Returns
* a `t:DateTime.t/0` representing the time of sunset in the
requested time zone at the requested location.
* `{:error, :time_zone_not_found}` if the requested
time zone is unknown.
* `{:error, :time_zone_not_resolved}` if it is not possible
to resolve a time zone name from the location. This can happen
if `:tz_world` is not configured as a dependency and no
`:time_zone_resolver` option is specified.
* `{:error, :no_time}` if for the requested date
and location there is no sunset. This can occur at
very high and very low latitudes during summer and winter.
### Notes
* If the resolved UTC date time is ambiguous because of a daylight savings
transition, the second of the two possibilities is applied. See
the `DateTime.from_naive/3` for more information.
### Examples
# Sunset in Sydney, Australia
Astro.sunset({151.20666584, -33.8559799094}, ~D[2019-12-04])
{:ok, #DateTime<2019-12-04 19:53:00.000000+11:00 AEDT Australia/Sydney>}
# Sunset in Alert, Nanavut, Canada
Astro.sunset({-62.3481, 82.5018}, ~D[2019-12-04])
{:error, :no_time}
"""
@spec sunset(location, date, options) ::
{:ok, DateTime.t()}
| {:error, :time_zone_not_found | :time_zone_not_resolved | :no_time}
def sunset(location, date, options \\ []) when is_list(options) do
Solar.SunRiseSet.sunset(location, date_to_moment(date), options)
end
@doc """
Returns the datetime of moonrise for a given location and date.
Uses the JPL DE440s ephemeris with fully topocentric correction
to compute the Moon's altitude zero-crossing via scan-and-bisect.
### Arguments
* `location` is the location as a `{longitude, latitude}` tuple,
a `Geo.Point.t` or a `Geo.PointZ.t`.
* `date` is a `t:Date.t/0` or `t:DateTime.t/0`.
* `options` is a keyword list of options.
### Options
* `:time_zone` is the time zone in which the sunrise
is requested. The default is `:default` in which
the sunrise time is reported in the time zone of
the requested location. `:utc` can be specified or any
other time zone name supported by the option
`:time_zone_database` is acceptabe.
* `:time_zone_database` represents the module that
implements the `Calendar.TimeZoneDatabase` behaviour.
The default is the configured Elixir time zone database or
one of Tzdata.TimeZoneDatabase or Tz.TimeZoneDatabase
depending upon which dependency is configured.
* `:time_zone_resolver` is a 1-arity function that resolves the
time zone name for a given location. The function will receive
a `%Geo.Point{cordinates: {lng, lat}}` struct and is expected to
return either `{:ok, time_zone_name}` or `{:error, :time_zone_not_found}`.
The default is `TzWorld.timezone_at/1` if `:tz_world` is
configured.
### Returns
* `{:ok, date_time}` with the local time of moonrise, or
* `{:error, :moon_always_below_horizon}` if the Moon does not
rise on the given date at the given location.
"""
@doc since: "2.0.0"
@spec moonrise(location, date, options) ::
{:ok, DateTime.t()} | {:error, :moon_always_below_horizon}
def moonrise(location, date, options \\ default_options())
def moonrise(location, date, options) when is_list(options) do
Lunar.MoonRiseSet.moonrise(location, date_to_moment(date), options)
end
@doc """
Returns the datetime of moonset for a given location and date.
Uses the JPL DE440s ephemeris with fully topocentric correction
to compute the Moon's altitude zero-crossing via scan-and-bisect.
### Arguments
* `location` is the location as a `{longitude, latitude}` tuple,
a `Geo.Point.t` or a `Geo.PointZ.t`.
* `date` is a `t:Date.t/0` or `t:DateTime.t/0`.
* `options` is a keyword list of options.
### Options
* `:time_zone` is the time zone in which the sunrise
is requested. The default is `:default` in which
the sunrise time is reported in the time zone of
the requested location. `:utc` can be specified or any
other time zone name supported by the option
`:time_zone_database` is acceptabe.
* `:time_zone_database` represents the module that
implements the `Calendar.TimeZoneDatabase` behaviour.
The default is the configured Elixir time zone database or
one of Tzdata.TimeZoneDatabase or Tz.TimeZoneDatabase
depending upon which dependency is configured.
* `:time_zone_resolver` is a 1-arity function that resolves the
time zone name for a given location. The function will receive
a `%Geo.Point{cordinates: {lng, lat}}` struct and is expected to
return either `{:ok, time_zone_name}` or `{:error, :time_zone_not_found}`.
The default is `TzWorld.timezone_at/1` if `:tz_world` is
configured.
### Returns
* `{:ok, date_time}` with the local time of moonset, or
* `{:error, :moon_always_above_horizon}` if the Moon does not
set on the given date at the given location.
"""
@doc since: "2.0.0"
@spec moonset(location, date, options) ::
{:ok, DateTime.t()} | {:error, :moon_always_above_horizon}
def moonset(location, date, options \\ default_options())
def moonset(location, date, options) when is_list(options) do
Lunar.MoonRiseSet.moonset(location, date_to_moment(date), options)
end
@doc """
Predicts the visibility of the new crescent moon at a given location
on a given date using one of three published criteria.
At the optimal observation time after sunset, the function evaluates
the geometric and photometric conditions to classify the crescent
into one of five visibility categories.
### Arguments
* `location` is the latitude, longitude and optionally elevation for
the observation site. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude as floating
point numbers. **Note** the order of the arguments.
* a `t:Geo.Point.t/0` struct to represent a location without elevation.
* a `t:Geo.PointZ.t/0` struct to represent a location and elevation.
* `date` is a `t:Date.t/0` or `t:DateTime.t/0` indicating the evening
on which crescent visibility is to be evaluated.
* `method` selects the prediction criterion. Default `:odeh`.
* `:odeh` — Odeh (2006). Empirical criterion based on 737 observations.
Uses topocentric ARCV with a Danjon limit of 6.4°. The most widely
used modern criterion.
* `:yallop` — Yallop (1997). Empirical criterion based on 295
observations. Uses geocentric ARCV. The original single-parameter
approach that Odeh later refined.
* `:schaefer` — Schaefer (1988/2000). Physics-based model computing
the contrast between crescent brightness and twilight sky brightness
against the human contrast detection threshold. The best observation
time is found by scanning from sunset to moonset.
### Options
When `method` is `:schaefer`, the following options are accepted as
an optional fourth argument (a keyword list):
* `:extinction` — V-band zenith extinction coefficient. Default `0.172`
(clean sea-level site). Typical values: `0.12` (high mountain),
`0.17` (sea level), `0.25` (hazy conditions).
### Returns
* `{:ok, visibility}` where `visibility` is one of:
* `:A` — Visible to the naked eye.
* `:B` — Visible with optical aid.
* `:C` — May need optical aid.
* `:D` — Not visible with optical aid.
* `:E` — Not visible.
* `{:error, :no_sunset}` if no sunset occurs on the given date at
the given location (e.g. polar day).
* `{:error, :not_found}` if the date is outside the range covered
by the installed ephemeris.
### Method comparison
| Aspect | Yallop (1997) | Odeh (2006) | Schaefer (1988/2000) |
|--------|---------------|-------------|----------------------|
| Basis | Empirical polynomial | Empirical polynomial | Physical model |
| Observations | 295 | 737 | N/A (theory) |
| ARCV type | Geocentric | Topocentric | N/A |
| Best time | Sunset + 4/9 lag | Sunset + 4/9 lag | Scanned (max Rs) |
| Atmosphere | Not modelled | Not modelled | Extinction coefficient |
### Examples
iex> location = {-0.1275, 51.5072}
iex> Astro.new_visible_crescent(location, ~D[2025-03-31])
{:ok, :A}
iex> location = {-0.1275, 51.5072}
iex> Astro.new_visible_crescent(location, ~D[2025-03-31], :yallop)
{:ok, :A}
"""
@doc since: "2.1.0"
@type method :: :odeh | :yallop | :schaefer
@spec new_visible_crescent(location(), date(), method()) ::
{:ok, Lunar.CrescentVisibility.visibility()} | {:error, :no_sunset | :not_found}
def new_visible_crescent(location, date, method \\ :odeh)
def new_visible_crescent(location, date, :yallop) do
moment = Time.date_time_to_moment(date)
normalized = Location.normalize_location(location)
Lunar.CrescentVisibility.yallop_new_visible_crescent(normalized, moment)
end
def new_visible_crescent(location, date, :odeh) do
moment = Time.date_time_to_moment(date)
normalized = Location.normalize_location(location)
Lunar.CrescentVisibility.odeh_new_visible_crescent(normalized, moment)
end
def new_visible_crescent(location, date, :schaefer) do
moment = Time.date_time_to_moment(date)
normalized = Location.normalize_location(location)
Lunar.CrescentVisibility.schaefer_new_visible_crescent(normalized, moment)
end
@doc """
Same as `new_visible_crescent/3` with `method: :schaefer` but
accepts additional atmospheric options.
See `new_visible_crescent/3` for full documentation.
### Options
* `:extinction` — V-band zenith extinction coefficient. Default `0.172`.
### Example
iex> location = {39.8579, 21.3891}
iex> {:ok, visibility} = Astro.new_visible_crescent(location, ~D[2025-03-31], :schaefer, extinction: 0.25)
iex> visibility in [:A, :B, :C, :D, :E]
true
"""
@doc since: "2.1.0"
@spec new_visible_crescent(location(), date(), :schaefer, keyword()) ::
{:ok, Lunar.CrescentVisibility.visibility()} | {:error, :no_sunset | :not_found}
def new_visible_crescent(location, date, :schaefer, options) when is_list(options) do
moment = Time.date_time_to_moment(date)
normalized = Location.normalize_location(location)
Lunar.CrescentVisibility.schaefer_new_visible_crescent(normalized, moment, options)
end
@doc """
Returns the datetime in UTC for either the
March or September equinox.
### Arguments
* `year` is the gregorian year for which the equinox is
to be calculated.
* `event` is either `:march` or `:september` indicating
which of the two annual equinox datetimes is required.
### Returns
* `{:ok, datetime}` representing the UTC datetime of
the equinox.
* `{:error, :year_out_of_range}` if `year` is outside the
supported range of 1000 CE to 3000 CE.
### Examples
iex> {:ok, dt} = Astro.equinox 2019, :march
iex> DateTime.truncate(dt, :second)
~U[2019-03-20 21:58:28Z]
iex> {:ok, dt} = Astro.equinox 2019, :september
iex> DateTime.truncate(dt, :second)
~U[2019-09-23 07:49:52Z]
iex> Astro.equinox 900, :march
{:error, :year_out_of_range}
### Notes
This equinox calculation is expected to be accurate
to within 2 minutes for the years 1000 CE to 3000 CE.
An equinox is commonly regarded as the instant of
time when the plane of earth's equator passes through
the center of the Sun. This occurs twice each year:
around 20 March and 23 September.
In other words, it is the moment at which the
center of the visible sun is directly above the equator.
"""
@spec equinox(Calendar.year(), :march | :september) ::
{:ok, DateTime.t()} | {:error, :year_out_of_range}
def equinox(year, event) when event in [:march, :september] and year in 1000..3000 do
Solar.equinox_and_solstice(year, event)
end
# The calculation is accurate to within 2 minutes only for 1000 CE to
# 3000 CE; outside that span return an error rather than raising a
# FunctionClauseError at the caller.
def equinox(year, event) when event in [:march, :september] and is_integer(year) do
{:error, :year_out_of_range}
end
@doc """
Returns the datetime in UTC for either the
June or December solstice.
### Arguments
* `year` is the gregorian year for which the solstice is
to be calculated.
* `event` is either `:june` or `:december` indicating
which of the two annual solstice datetimes is required.
### Returns
* `{:ok, datetime}` representing the UTC datetime of
the solstice.
* `{:error, :year_out_of_range}` if `year` is outside the
supported range of 1000 CE to 3000 CE.
### Examples
iex> {:ok, dt} = Astro.solstice 2019, :december
iex> DateTime.truncate(dt, :second)
~U[2019-12-22 04:19:19Z]
iex> {:ok, dt} = Astro.solstice 2019, :june
iex> DateTime.truncate(dt, :second)
~U[2019-06-21 15:54:07Z]
iex> Astro.solstice 3500, :june
{:error, :year_out_of_range}
### Notes
This solstice calculation is expected to be accurate
to within 2 minutes for the years 1000 CE to 3000 CE.
A solstice is an event occurring when the Sun appears
to reach its most northerly or southerly excursion
relative to the celestial equator on the celestial
sphere. Two solstices occur annually, around June 21
and December 21.
The seasons of the year are determined by
reference to both the solstices and the equinoxes.
The day of a solstice in either hemisphere has either the most
sunlight of the year (summer solstice) or the least
sunlight of the year (winter solstice) for any place
other than the Equator.
Alternative terms, with no ambiguity as to which
hemisphere is the context, are "June solstice" and
"December solstice", referring to the months in
which they take place every year.
"""
@spec solstice(Calendar.year(), :june | :december) ::
{:ok, DateTime.t()} | {:error, :year_out_of_range}
def solstice(year, event) when event in [:june, :december] and year in 1000..3000 do
Solar.equinox_and_solstice(year, event)
end
# The calculation is accurate to within 2 minutes only for 1000 CE to
# 3000 CE; outside that span return an error rather than raising a
# FunctionClauseError at the caller.
def solstice(year, event) when event in [:june, :december] and is_integer(year) do
{:error, :year_out_of_range}
end
@doc """
Returns solar noon for a
given date and location as
a UTC datetime
### Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired solar noon
time. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation.
* a `Geo.PointZ.t` struct to represent a location and elevation.
* `date` is any `t:Date.t/0` in the Gregorian
calendar (for example, `Calendar.ISO`).
### Returns
* a UTC datetime representing solar noon
at the given location for the given date.
### Example
iex> Astro.solar_noon {151.20666584, -33.8559799094}, ~D[2019-12-06]
{:ok, ~U[2019-12-06 01:45:42Z]}
### Notes
Solar noon is the moment when the sun passes a
location's meridian and reaches its highest position
in the sky. In most cases, it doesn't happen at 12 o'clock.
At solar noon, the Sun reaches its
highest position in the sky as it passes the
local meridian.
"""
@spec solar_noon(Astro.location(), Calendar.date()) :: {:ok, DateTime.t()}
def solar_noon(location, date) do
%Geo.PointZ{coordinates: {longitude, _, _}} = Location.normalize_location(location)
julian_day = Time.julian_day_from_date(date)
julian_centuries = Time.julian_centuries_from_julian_day(julian_day)
julian_centuries
|> Solar.solar_noon_utc(-longitude)
|> Time.date_time_from_date_and_minutes(date)
end
@doc """
Returns solar longitude for a
given date. Solar longitude is used
to identify the seasons.
### Arguments
* `date` is any `t:Date.t/0` in the Gregorian
calendar (for example, `Calendar.ISO`).
### Returns
* a `float` number of degrees between 0 and
360 representing the solar longitude
on `date`.
### Examples
iex> Astro.sun_apparent_longitude ~D[2019-03-21]
0.08035853207991295
iex> Astro.sun_apparent_longitude ~D[2019-06-22]
90.32130455695378
iex> Astro.sun_apparent_longitude ~D[2019-09-23]
179.68691978440197
iex> Astro.sun_apparent_longitude ~D[2019-12-23]
270.83941087483504
### Notes
Solar longitude (the ecliptic longitude of the sun)
in effect describes the position of the earth in its
orbit, being zero at the moment of the March
equinox.
Since it is based on how far the earth has moved
in its orbit since the equinox, it is a measure of
what time of the tropical year (the year of seasons)
we are in, but without the inaccuracies of a calendar
date, which is perturbed by leap years and calendar
imperfections.
"""
@spec sun_apparent_longitude(Calendar.date()) :: degrees()
def sun_apparent_longitude(date) do
date
|> Time.julian_day_from_date()
|> Time.julian_centuries_from_julian_day()
|> Solar.sun_apparent_longitude()
end
@doc """
Returns the number of hours of daylight for a given
location on a given date.
On Elixir 1.17+, the function `duration_of_daylight/2`
is recommended over this function since it returns a
`t:Duration.t/0` which can represent a full 24 hours
of daylight.
### Arguments
* `location` is the latitude, longitude and
optionally elevation for the desired hours of
daylight. It can be expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation
* a `Geo.PointZ.t` struct to represent a location and elevation.
* `date` is any `t:Date.t/0` in the Gregorian
calendar (for example, `Calendar.ISO`).
### Returns
* `{:ok, time}` where `time` is a `Time.t()`. The maximum value is
`~T[23:59:59]`, representing 24 hours of daylight (a `Time.t()`
cannot hold `24:00:00`).
* `{:error, reason}` if the time zone for the location cannot be
resolved.
### Examples
iex> Astro.hours_of_daylight({151.20666584, -33.8559799094}, ~D[2019-12-10])
{:ok, ~T[14:20:51]}
# No sunset in summer
iex> Astro.hours_of_daylight({-62.3481, 82.5018}, ~D[2019-06-07])
{:ok, ~T[23:59:59]}
# No sunrise in winter
iex> Astro.hours_of_daylight({-62.3481, 82.5018}, ~D[2019-12-07])
{:ok, ~T[00:00:00]}
### Notes
Daylight is measured as the total time the Sun is above the horizon
during the local calendar day, so the result is correct regardless of
whether sunrise precedes sunset.
In latitudes above the polar circles (approximately +/- 66.5631
degrees) there will be no hours of daylight in winter and 24 hours of
daylight in summer. Just below the polar circles, near the solstice,
a single calendar day can contain a sunset (shortly after midnight)
followed by a sunrise (a few hours later); such days are handled
correctly and report close to, but less than, 24 hours.
"""
@spec hours_of_daylight(Astro.location(), Calendar.date()) ::
{:ok, Elixir.Time.t()} | {:error, atom()}
def hours_of_daylight(location, date) do
case daylight_seconds(location, date) do
{:ok, seconds} when seconds >= @seconds_per_day ->
# 24 hours of daylight (polar day). A `Time.t/0` cannot represent
# 24:00:00, so it is reported one second short. Use
# `duration_of_daylight/2` for an uncapped `Duration.t/0`.
Elixir.Time.new(23, 59, 59)
{:ok, seconds} ->
Elixir.Time.new(div(seconds, 3600), div(rem(seconds, 3600), 60), rem(seconds, 60))
{:error, reason} ->
{:error, reason}
end
end
if Code.ensure_loaded?(Duration) do
@doc """
Returns the duration of daylight for a given location on a
given date as a `t:Duration.t/0`.
This is the same calculation as `hours_of_daylight/2` but,
because a `t:Duration.t/0` is not bounded like a `t:Time.t/0`,
it can represent a full 24 hours of daylight (returned as
`%Duration{hour: 24}`) during the polar summer rather than
capping at `~T[23:59:59]`.
This function is only defined when running on Elixir 1.17 or
later, where the `Duration` module is available.
### Arguments
* `location` is the latitude, longitude and optionally
elevation for the desired duration of daylight. It can be
expressed as:
* `{lng, lat}` - a tuple with longitude and latitude
as floating point numbers. **Note** the order of the
arguments.
* a `Geo.Point.t` struct to represent a location without elevation.
* a `Geo.PointZ.t` struct to represent a location and elevation.
* `date` is any `t:Date.t/0` in the Gregorian calendar
(for example, `Calendar.ISO`).
### Returns
* `{:ok, duration}` where `duration` is a `t:Duration.t/0`
between `%Duration{}` (no daylight) and `%Duration{hour: 24}`
(24 hours of daylight).
* `{:error, reason}` if the time zone for the location cannot
be resolved.
### Examples
iex> Astro.duration_of_daylight({151.20666584, -33.8559799094}, ~D[2019-12-10])
{:ok, %Duration{hour: 14, minute: 20, second: 51}}
# 24 hours of daylight in the polar summer, uncapped
iex> Astro.duration_of_daylight({-62.3481, 82.5018}, ~D[2019-06-07])
{:ok, %Duration{hour: 24}}
# No daylight in the polar winter
iex> Astro.duration_of_daylight({-62.3481, 82.5018}, ~D[2019-12-07])
{:ok, %Duration{}}
"""
@doc since: "2.3.0"
@spec duration_of_daylight(Astro.location(), Calendar.date()) ::
{:ok, Duration.t()} | {:error, atom()}
def duration_of_daylight(location, date) do
case daylight_seconds(location, date) do
{:ok, seconds} ->
{:ok,
Duration.new!(
hour: div(seconds, 3600),
minute: div(rem(seconds, 3600), 60),
second: rem(seconds, 60)
)}
{:error, reason} ->
{:error, reason}
end
end
end
# Total seconds the Sun is above the horizon during the local calendar day,
# in the range 0..86_400. Rather than subtracting sunrise from sunset (which
# breaks at sub-polar latitudes near the solstice, where the Sun can set just
# after local midnight and rise again hours later, so sunset precedes
# sunrise), the daylight is derived from which events fall within the day:
#
# * both events present → normal day (rise then set) is set − rise; a
# reversed day (set then rise) is the whole day minus the night between.
# * only sunrise present → Sun rises and stays up to end of day.
# * only sunset present → Sun is up at midnight and sets during the day.
# * neither present → polar day (24h) or polar night (0h).
#
defp daylight_seconds(location, date) do
case {sunrise(location, date), sunset(location, date)} do
{{:ok, sunrise}, {:ok, sunset}} ->
{:ok, daylight_between(sunrise, sunset)}
{{:error, :no_time}, {:ok, sunset}} ->
{:ok, seconds_since_midnight(sunset)}
{{:ok, sunrise}, {:error, :no_time}} ->
{:ok, @seconds_per_day - seconds_since_midnight(sunrise)}
{{:error, :no_time}, {:error, :no_time}} ->
if Solar.SunRiseSet.sun_above_horizon?(location, date_to_moment(date)) do
{:ok, @seconds_per_day}
else
{:ok, 0}
end
{{:error, reason}, _} ->
{:error, reason}
{_, {:error, reason}} ->
{:error, reason}
end
end
# When sunset precedes sunrise on the same calendar day the Sun was already
# up at local midnight, set briefly, then rose again — daylight is the whole
# day less the night between the two events (`DateTime.diff` is negative).
defp daylight_between(sunrise, sunset) do
case DateTime.diff(sunset, sunrise) do
seconds when seconds >= 0 -> seconds
night -> @seconds_per_day + night
end
end
defp seconds_since_midnight(%DateTime{hour: hour, minute: minute, second: second}) do
hour * 3600 + minute * 60 + second
end
@doc false
def default_options do
default_time_zone_db =
Application.get_env(:elixir, :time_zone_database) || @compile_time_time_zone_db
[
solar_elevation: Solar.solar_elevation(:geometric),
time_zone: :default,
time_zone_database: default_time_zone_db
]
end
# Convert a Date, DateTime or NaiveDateTime to a moment
# (integer Gregorian days representing UTC midnight).
defp date_to_moment(date) do
Time.date_time_to_moment(date)
end
end