Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add get_night_phase() to celerity/night module. #108

Merged
merged 1 commit into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading