Skip to content

Commit

Permalink
Merge pull request #108 from michealroberts/feature/night/get_night_p…
Browse files Browse the repository at this point in the history
…hase

feat: add get_night_phase() to celerity/night module.
  • Loading branch information
michealroberts committed Apr 28, 2024
2 parents cf51e30 + a05b67e commit a33189a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/celerity/night.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,27 @@ def is_night(


# **************************************************************************************


def get_night_phase(altitude: float) -> NightPhase:
"""
Determine the phase of the night based on the altitude of the Sun.
:param altitude: The altitude of the Sun.
"""
if altitude > 0:
return NightPhase.DAY

if altitude > -6:
return NightPhase.CIVIL_TWILIGHT

if altitude > -12:
return NightPhase.NAUTICAL_TWILIGHT

if altitude > -18:
return NightPhase.ASTRONOMICAL_TWILIGHT

return NightPhase.NIGHT


# **************************************************************************************
35 changes: 34 additions & 1 deletion tests/test_night.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from datetime import datetime, timezone

from src.celerity.common import GeographicCoordinate
from src.celerity.night import get_night, get_solar_transit, is_night
from src.celerity.night import (
NightPhase,
get_night,
get_night_phase,
get_solar_transit,
is_night,
)

# For testing we need to specify a date because most calculations are
# differential w.r.t a time component. We set it to the author's birthday:
Expand Down Expand Up @@ -70,3 +76,30 @@ def test_is_night():
date = datetime(2021, 5, 14, 22, 0, 0, 0, tzinfo=timezone.utc)
n = is_night(date, observer)
assert n is True


def test_get_night_phase():
# If the Sun's altitude is less than -18 degrees, then it is night:
altitude = -18
n = get_night_phase(altitude)
assert n == NightPhase.NIGHT

# If the Sun's altitude is between -18 and -12 degrees, then it is
# astronomical twilight:
altitude = -12
n = get_night_phase(altitude)
assert n == NightPhase.ASTRONOMICAL_TWILIGHT

# If the Sun's altitude is between -12 and -6 degrees, then it is nautical twilight:
altitude = -6
n = get_night_phase(altitude)
assert n == NightPhase.NAUTICAL_TWILIGHT

# If the Sun's altitude is between -6 and 0 degrees, then it is civil twilight:
altitude = 0
n = get_night_phase(altitude)
assert n == NightPhase.CIVIL_TWILIGHT

# If the Sun's altitude is greater than 0 degrees, then it is day:
altitude = 1
n = get_night_phase(altitude)

0 comments on commit a33189a

Please sign in to comment.