Skip to content

Commit

Permalink
Handle timezone aware datetime objects
Browse files Browse the repository at this point in the history
Signed-off-by: Adam.Dybbroe <[email protected]>
  • Loading branch information
Adam.Dybbroe committed Jul 19, 2024
1 parent 84e57b0 commit 26ce23d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
18 changes: 17 additions & 1 deletion pyorbital/orbital.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import logging
import warnings
from datetime import datetime, timedelta
import pytz

import numpy as np
from scipy import optimize
Expand Down Expand Up @@ -169,7 +170,7 @@ def get_last_an_time(self, utc_time):
"""Calculate time of last ascending node relative to the specified time."""
# Propagate backwards to ascending node
dt = np.timedelta64(10, 'm')
t_old = np.datetime64(utc_time)
t_old = np.datetime64(_get_tz_unaware_utctime(utc_time))
t_new = t_old - dt
pos0, vel0 = self.get_position(t_old, normalize=False)
pos1, vel1 = self.get_position(t_new, normalize=False)
Expand Down Expand Up @@ -920,6 +921,21 @@ def propagate(self, utc_time):
return kep


def _get_tz_unaware_utctime(utc_time):
"""Return timzone unaware datetime object.
The input *utc_time* is either a timezone unaware object assumed to be in
UTC, or a timezone aware datetime object in UTC.
"""
if not hasattr(utc_time, 'tzinfo') or utc_time.tzinfo is None:
return utc_time

if utc_time.tzinfo != pytz.utc:
raise AttributeError("UTC time expected! Parsing a timezone aware datetime object requires it to be UTC!")

return utc_time.replace(tzinfo=None)


def kep2xyz(kep):
"""Keppler to cartesian coordinates conversion.
Expand Down
33 changes: 25 additions & 8 deletions pyorbital/tests/test_orbital.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import unittest
from unittest import mock
from datetime import datetime, timedelta
import pytz
import numpy as np
from pyorbital import orbital

Expand Down Expand Up @@ -417,21 +418,37 @@ def test_63(self):
warnings.filterwarnings('default')


@pytest.mark.parametrize('dtime, expected',
[(datetime(2024, 6, 25, 11, 0, 18),
np.datetime64('2024-06-25T10:44:18.234375')),
(datetime(2024, 6, 25, 11, 5, 0),
np.datetime64('2024-06-25T10:44:18.234375')),
(np.datetime64('2024-06-25T11:10:00.000000'),
np.datetime64('2024-06-25T10:44:18.234375')),
@pytest.mark.parametrize('dtime',
[datetime(2024, 6, 25, 11, 0, 18),
datetime(2024, 6, 25, 11, 5, 0, 0, pytz.UTC),
np.datetime64('2024-06-25T11:10:00.000000')
]
)
def test_get_last_an_time_scalar_input(dtime, expected):
def test_get_last_an_time_scalar_input(dtime):
"""Test getting the time of the last ascending node - input time is a scalar."""
from pyorbital.orbital import Orbital
orb = Orbital("NOAA-20",
line1='1 43013U 17073A 24176.73674251 .00000000 00000+0 11066-3 0 00014',
line2='2 43013 98.7060 114.5340 0001454 139.3958 190.7541 14.19599847341971')

expected = np.datetime64('2024-06-25T10:44:18.234375')
result = orb.get_last_an_time(dtime)
assert abs(expected - result) < np.timedelta64(1, 's')


@pytest.mark.parametrize('dtime',
[datetime(2024, 6, 25, 11, 5, 0, 0, pytz.timezone('Europe/Stockholm')),
]
)
def test_get_last_an_time_wrong_input(dtime):
"""Test getting the time of the last ascending node - wrong input."""
from pyorbital.orbital import Orbital
orb = Orbital("NOAA-20",
line1='1 43013U 17073A 24176.73674251 .00000000 00000+0 11066-3 0 00014',
line2='2 43013 98.7060 114.5340 0001454 139.3958 190.7541 14.19599847341971')

with pytest.raises(AttributeError) as exec_info:
_ = orb.get_last_an_time(dtime)

expected = "UTC time expected! Parsing a timezone aware datetime object requires it to be UTC!"
assert str(exec_info.value) == expected

0 comments on commit 26ce23d

Please sign in to comment.