Skip to content

Commit

Permalink
fix: amend UTC datetime.astimezone() across repository in @observerly…
Browse files Browse the repository at this point in the history
…/celerity.

fix: amend UTC datetime.astimezone() across repository in @observerly/celerity.
  • Loading branch information
michealroberts committed Apr 24, 2024
1 parent c637ad0 commit 3f3f93a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 58 deletions.
8 changes: 2 additions & 6 deletions src/celerity/equinox.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def get_spring_equinox(year: int) -> datetime:
)

# Convert the Julian date to a datetime UTC object:
return datetime.utcfromtimestamp((JD - 2440587.5) * 86400).astimezone(
tz=timezone.utc
)
return datetime.fromtimestamp((JD - 2440587.5) * 86400).astimezone(tz=timezone.utc)


# **************************************************************************************
Expand All @@ -49,9 +47,7 @@ def get_autumn_equinox(year: int) -> datetime:
)

# Convert the Julian date to a datetime UTC object:
return datetime.utcfromtimestamp((JD - 2440587.5) * 86400).astimezone(
tz=timezone.utc
)
return datetime.fromtimestamp((JD - 2440587.5) * 86400).astimezone(tz=timezone.utc)


# **************************************************************************************
8 changes: 2 additions & 6 deletions src/celerity/solstice.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def get_summer_solstice(year: int) -> datetime:
JD = 1721233.2486 + 365.2417284 * year - 0.053018 * pow(T, 2) + 0.009332 * pow(T, 3)

# Convert the Julian date to a datetime UTC object:
return datetime.utcfromtimestamp((JD - 2440587.5) * 86400).astimezone(
tz=timezone.utc
)
return datetime.fromtimestamp((JD - 2440587.5) * 86400).astimezone(tz=timezone.utc)


# **************************************************************************************
Expand All @@ -47,9 +45,7 @@ def get_winter_solstice(year: int) -> datetime:
)

# Convert the Julian date to a datetime UTC object:
return datetime.utcfromtimestamp((JD - 2440587.5) * 86400).astimezone(
tz=timezone.utc
)
return datetime.fromtimestamp((JD - 2440587.5) * 86400).astimezone(tz=timezone.utc)


# **************************************************************************************
7 changes: 2 additions & 5 deletions src/celerity/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,8 @@ def get_greenwhich_sidereal_time(date: datetime) -> float:
if T_0 < 0:
T_0 += 24

# Ensure that the date is in UTC
d = date.astimezone(tz=timezone.utc)

# Convert the UTC time to a decimal fraction of hours:
UTC = d.microsecond / 1e-6 + d.second / 60 + d.minute / 60 + d.hour
UTC = date.microsecond / 1e-6 + date.second / 60 + date.minute / 60 + date.hour

A = UTC * 1.002737909

Expand All @@ -101,7 +98,7 @@ def get_local_sidereal_time(date: datetime, longitude: float) -> float:
:param longitude: The longitude of the observer.
:return: The Local Sidereal Time (LST) of the given date normalised to UTC.
"""
GST = get_greenwhich_sidereal_time(date.astimezone(tz=timezone.utc))
GST = get_greenwhich_sidereal_time(date)

d = (GST + longitude / 15.0) / 24.0

Expand Down
40 changes: 20 additions & 20 deletions tests/test_moon.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,81 +42,81 @@

def test_get_annual_equation_correction():
Ae = get_annual_equation_correction(date)
assert Ae == 0.1450832102519408
assert Ae == 0.1449999791711593


def test_get_avection_correction():
Ev = get_evection_correction(date)
assert Ev == -0.5580522629166632
assert Ev == -0.567491841389144


def test_get_mean_anomaly():
M = get_mean_anomaly(date)
assert M == 207.63633585681964
assert M == 208.18071056557528


def test_get_mean_anomaly_correction():
Ca = get_mean_anomaly_correction(date)
assert Ca == 206.64428333417192
assert Ca == 207.17946744096045


def test_get_mean_geometric_longitude():
l = get_mean_geometric_longitude(date)
assert l == 80.32626508452813
assert l == 80.87528160147485


def test_get_mean_ecliptic_longitude_of_the_ascending_node():
Ω = get_mean_ecliptic_longitude_of_the_ascending_node(date)
assert Ω == 71.6938262475226
assert Ω == 71.69169150872794


def test_get_mean_ecliptic_longitude():
λ = get_mean_ecliptic_longitude(date)
assert λ == 79.88317358099448
assert λ == 80.43218773254193


def test_get_true_anomaly():
ν = get_true_anomaly(date)
assert ν == 357.3514315617634
assert ν == 357.30141277111267


def test_get_true_ecliptic_longitude():
λt = get_true_ecliptic_longitude(date)
assert λt == 77.01224128076132
assert λt == 77.50887563483909


def test_get_corrected_ecliptic_longitude_of_the_ascending_node():
Ω = get_corrected_ecliptic_longitude_of_the_ascending_node(date)
assert Ω == 71.56888914504515
assert Ω == 71.56682607994762


def test_get_ecliptic_longitude():
λ = get_ecliptic_longitude(date)
assert λ == 76.99043727540315
assert λ == 77.48510137349693


def test_get_ecliptic_latitude():
β = get_ecliptic_latitude(date)
assert β == 0.4874504338736112
assert β == 0.5319563864055369


def test_get_equatorial_coordinate():
date = datetime(2015, 1, 2, 3, 0, 0, 0, tzinfo=timezone.utc)
eq = get_equatorial_coordinate(date)
assert eq["ra"] == 63.854089783072595
assert eq["dec"] == 17.246094608898062
assert eq["ra"] == 64.41820192745155
assert eq["dec"] == 17.309627600319683


def test_get_elongation():
date = datetime(2015, 1, 2, 3, 0, 0, 0, tzinfo=timezone.utc)
elongation = get_elongation(date)
assert elongation == 143.73394864456367
assert elongation == 144.2278280948863


def test_get_angular_diameter():
date = datetime(2015, 1, 2, 3, 0, 0, 0, tzinfo=timezone.utc)
d = get_angular_diameter(date)
assert d == 0.5480234986129843
assert d == 0.5480235062440527


def test_get_distance():
Expand All @@ -128,20 +128,20 @@ def test_get_distance():
def test_get_age():
date = datetime(2015, 1, 1, 0, 0, 0, 0, tzinfo=timezone.utc)
age = get_age(date)
assert age["A"] == 130.40251122256336
assert age["a"] == 10.696845549747305
assert age["A"] == 130.9120194831579
assert age["a"] == 10.73864022930373


def test_get_phase_angle():
date = datetime(2015, 1, 1, 0, 0, 0, 0, tzinfo=timezone.utc)
PA = get_phase_angle(date)
assert PA == 49.66441438659977
assert PA == 49.15946140922159


def test_get_illumination():
date = datetime(2015, 1, 1, 0, 0, 0, 0, tzinfo=timezone.utc)
K = get_illumination(date)
assert K == 82.36316687224799
assert K == 82.69780192655509


def test_get_phase():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_night.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def test_get_solar_transit():
d = get_solar_transit(date, observer)
# Rise, in UTC (BST is UTC+1 and we're expecting it to rise around 5am BST)
assert d[0] == datetime(2021, 5, 14, 4, 46, 0, 0, tzinfo=timezone.utc)
assert d[0] == datetime(2021, 5, 14, 4, 47, 0, 0, tzinfo=timezone.utc)
# Transit, in UTC:
assert d[1] == datetime(2021, 5, 14, 12, 20, 0, 0, tzinfo=timezone.utc)
# Set, in UTC (BST is UTC+1 and we're expecting it to set around 9pm BST)
Expand All @@ -28,7 +28,7 @@ def test_get_solar_transit():
# Civil twilight is when the Sun is 6 degrees below the horizon:
d = get_solar_transit(date, observer, -6)
# Rise, in UTC (BST is UTC+1 and we're expecting it to rise around 5am BST)
assert d[0] == datetime(2021, 5, 14, 4, 1, 0, 0, tzinfo=timezone.utc)
assert d[0] == datetime(2021, 5, 14, 4, 2, 0, 0, tzinfo=timezone.utc)
# Transit, in UTC:
assert d[1] == datetime(2021, 5, 14, 12, 20, 0, 0, tzinfo=timezone.utc)
# Set, in UTC (BST is UTC+1 and we're expecting it to set around 9pm BST)
Expand Down
25 changes: 15 additions & 10 deletions tests/test_sun.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime, timezone

from src.celerity.common import EquatorialCoordinate, GeographicCoordinate
from src.celerity.coordinates import convert_equatorial_to_horizontal
from src.celerity.sun import (
get_angular_diameter,
get_distance,
Expand Down Expand Up @@ -30,49 +31,53 @@

def test_get_mean_anomaly():
M = get_mean_anomaly(date)
assert M == 128.66090142411576
assert M == 128.70196810229208


def test_get_mean_geometric_longitude():
L = get_mean_geometric_longitude(date)
assert L == 51.96564888161811
assert L == 52.006717521619976


def test_get_equation_of_center():
C = get_equation_of_center(date)
assert C == 1.4754839423594455
assert C == 1.4746206625294058


def test_get_true_anomaly():
ν = get_true_anomaly(date)
assert ν == 130.1363853664752
assert ν == 130.1765887648215


def test_get_true_geometric_longitude():
L = get_true_geometric_longitude(date)
assert L == 53.441132823977554
assert L == 53.48133818414938


def test_get_ecliptic_longitude():
date = datetime(2015, 2, 5, 12, 0, 0, 0, tzinfo=timezone.utc)
λ = get_ecliptic_longitude(date)
assert λ == 316.10388080739784
assert λ == 316.14612163988977


def test_get_equatorial_coordinate():
date = datetime(2015, 2, 5, 12, 0, 0, 0, tzinfo=timezone.utc)
eq = get_equatorial_coordinate(date)
assert eq["ra"] == 318.5617376411268
assert eq["dec"] == -16.008394691469505
assert eq["ra"] == 318.60368091333004
assert eq["dec"] == -15.995795336804742

hz = convert_equatorial_to_horizontal(date, observer, eq)
assert hz["alt"] == -69.43822112215857
assert hz["az"] == 82.71313543737287


def test_get_angular_diameter():
date = datetime(2015, 2, 15, 0, 0, 0, 0, tzinfo=timezone.utc)
θ = get_angular_diameter(date)
assert θ == 0.5398164296031396
assert θ == 0.5398119814926151


def test_get_distance():
date = datetime(2015, 2, 15, 0, 0, 0, 0, tzinfo=timezone.utc)
d = get_distance(date)
assert d == 147744945752.45538
assert d == 147746163187.17465
2 changes: 1 addition & 1 deletion tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_get_local_sidereal_time():


def test_universal_time():
assert get_universal_time(date) == 0.000028334646537240785
assert get_universal_time(date) == 23.000038327339603


def test_convert_local_sidereal_time_to_greenwhich_sidereal_time():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_timezone():

def test_universal_time():
# Assert UT is close to 0:
assert isclose(T.UT, 0.0, abs_tol=0.0001)
assert isclose(T.UT, 23.000038327339603, abs_tol=0.0001)


def test_get_julian_date():
Expand Down
14 changes: 7 additions & 7 deletions tests/test_transit.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_get_does_object_rise_or_set():

d = get_does_object_rise_or_set({"lat": 80, "lon": 0}, betelgeuse)
assert d["Ar"] == 0.7424083500051002
assert d["H1"] == 0.7372819131688116
assert d["H1"] == 0.7372819131688118

d = get_does_object_rise_or_set({"lat": -85, "lon": 0}, betelgeuse)
assert d == False
Expand Down Expand Up @@ -86,7 +86,7 @@ def test_get_transit():
T = get_transit(observer, betelgeuse)
assert T["LSTr"] == 23.740485646638913
assert T["LSTs"] == 12.098575460027751
assert T["R"] == 82.12362992591511
assert T["R"] == 82.1236299259151
assert T["S"] == 277.8763700740849


Expand All @@ -97,8 +97,8 @@ def test_get_next_rise():
r = get_next_rise(date, observer, betelgeuse, 0)
assert r["LST"] == 23.740485646638913
assert r["GST"] == 10.105025246638917
assert r["az"] == 82.12362992591511
assert r["date"] == datetime(2021, 5, 15, 18, 31, 28, 716561, tzinfo=timezone.utc)
assert r["az"] == 82.1236299259151
assert r["date"] == datetime(2021, 11, 1, 7, 22, 54, 274701, tzinfo=timezone.utc)

# Test where we know the object is circumpolar for the observer:
r = get_next_rise(date, observer, polaris, 0)
Expand All @@ -107,7 +107,7 @@ def test_get_next_rise():
# Test where we known the object only rises at specific times of the year:
date = datetime(2021, 1, 12, 0, 0, 0, 0)
r = get_next_rise(date, {"lat": 20.2437, "lon": observer["lon"]}, LMC, 0)
assert r["date"] == datetime(2021, 1, 12, 8, 16, 12, 950867, tzinfo=timezone.utc)
assert r["date"] == datetime(2021, 1, 12, 8, 16, 3, 121302, tzinfo=timezone.utc)
assert r["LST"] == 5.375729797576824
assert r["GST"] == 15.740269397576824
assert r["az"] == 179.91065185064514
Expand All @@ -120,7 +120,7 @@ def test_get_next_set():
assert s["LST"] == 12.098575460027751
assert s["GST"] == 22.463115060027754
assert s["az"] == 277.8763700740849
assert s["date"] == datetime(2021, 5, 15, 6, 54, 52, 256582, tzinfo=timezone.utc)
assert s["date"] == datetime(2021, 5, 14, 6, 58, 48, 166063, tzinfo=timezone.utc)

# Test where we know the object is circumpolar for the observer:
s = get_next_set(date, observer, polaris, 0)
Expand All @@ -129,7 +129,7 @@ def test_get_next_set():
# Test where we known the object only rises at specific times of the year:
date = datetime(2021, 1, 12, 0, 0, 0, 0)
s = get_next_set(date, {"lat": 20.2437, "lon": observer["lon"]}, LMC, 0)
assert s["date"] == datetime(2021, 1, 12, 8, 18, 16, 557970, tzinfo=timezone.utc)
assert s["date"] == datetime(2021, 1, 12, 8, 18, 6, 728404, tzinfo=timezone.utc)
assert s["LST"] == 5.410159095756511
assert s["GST"] == 15.774698695756513
assert s["az"] == 180.08934814935486

0 comments on commit 3f3f93a

Please sign in to comment.