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

Fix datetime imports #177

Merged
merged 1 commit into from
Dec 13, 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
12 changes: 6 additions & 6 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ Computing satellite position
The orbital module enables computation of satellite position and velocity at a specific time:

>>> from pyorbital.orbital import Orbital
>>> from datetime import datetime
>>> import datetime as dt
>>> # Use current TLEs from the internet:
>>> orb = Orbital("Suomi NPP")
>>> now = datetime.utcnow()
>>> now = dt.datetime.now(dt.timezone.utc)
>>> # Get normalized position and velocity of the satellite:
>>> orb.get_position(now)
(array([-0.20015267, 0.09001458, 1.10686756]),
Expand All @@ -131,9 +131,9 @@ Use actual TLEs to increase accuracy
------------------------------------

>>> from pyorbital.orbital import Orbital
>>> from datetime import datetime
>>> import datetime as dt
>>> orb = Orbital("Suomi NPP")
>>> dtobj = datetime(2015,2,7,3,0)
>>> dtobj = dt.datetime(2015,2,7,3,0)
>>> orb.get_lonlatalt(dtobj)
(152.11564698762811, 20.475251739329622, 829.37355785502211)

Expand All @@ -158,8 +158,8 @@ Computing astronomical parameters
The astronomy module enables computation of certain parameters of interest for satellite remote sensing for instance the Sun-zenith angle:

>>> from pyorbital import astronomy
>>> from datetime import datetime
>>> utc_time = datetime(2012, 5, 15, 15, 45)
>>> import datetime as dt
>>> utc_time = dt.datetime(2012, 5, 15, 15, 45)
>>> lon, lat = 12, 56
>>> astronomy.sun_zenith_angle(utc_time, lon, lat)
62.685986438071602
Expand Down
4 changes: 2 additions & 2 deletions pyorbital/geoloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@
noaa18_tle1 = "1 28654U 05018A 11284.35271227 .00000478 00000-0 28778-3 0 9246"
noaa18_tle2 = "2 28654 99.0096 235.8581 0014859 135.4286 224.8087 14.11526826329313"

from datetime import datetime
t = datetime(2011, 10, 12, 13, 45)
import datetime as dt
t = dt.datetime(2011, 10, 12, 13, 45)

Check warning on line 278 in pyorbital/geoloc.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/geoloc.py#L277-L278

Added lines #L277 - L278 were not covered by tests

# edge and centre of an avhrr scanline
# sgeom = ScanGeometry([(-0.9664123687741623, 0),
Expand Down
4 changes: 2 additions & 2 deletions pyorbital/geoloc_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

"""Simple usage for geoloc."""

from datetime import datetime
import datetime as dt

Check warning on line 25 in pyorbital/geoloc_example.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/geoloc_example.py#L25

Added line #L25 was not covered by tests

import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -35,7 +35,7 @@
tle2 = "2 33591 098.8821 283.2036 0013384 242.4835 117.4960 14.11432063197875"

# Choosing a specific time, this should be relatively close to the issue date of the TLE
t = datetime(2012, 12, 12, 4, 16, 1, 575000)
t = dt.datetime(2012, 12, 12, 4, 16, 1, 575000)

Check warning on line 38 in pyorbital/geoloc_example.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/geoloc_example.py#L38

Added line #L38 was not covered by tests
# this is the number of full scan rotations
scans_nb = 10
# we take only every 40th point for plotting clarity
Expand Down
34 changes: 17 additions & 17 deletions pyorbital/orbital.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

"""Module for computing the orbital parameters of satellites."""

import datetime as dt
import logging
import warnings
from datetime import datetime, timedelta, timezone
from functools import partial

import numpy as np
Expand Down Expand Up @@ -353,7 +353,7 @@

"""
# every minute
times = utc_time + np.array([timedelta(minutes=minutes)
times = utc_time + np.array([dt.timedelta(minutes=minutes)
for minutes in range(length * 60)])
elev = self.get_observer_look(times, lon, lat, alt)[1] - horizon
zcs = np.where(np.diff(np.sign(elev)))[0]
Expand All @@ -364,7 +364,7 @@
elev_inv_func = partial(self._elevation_inv, utc_time, lon, lat, alt, horizon)
for guess in zcs:
horizon_mins = _get_root(elev_func, guess, guess + 1.0, tol=tol / 60.0)
horizon_time = utc_time + timedelta(minutes=horizon_mins)
horizon_time = utc_time + dt.timedelta(minutes=horizon_mins)
if elev[guess] < 0:
risetime = horizon_time
risemins = horizon_mins
Expand All @@ -377,7 +377,7 @@
int_end = min(len(elev), int(np.ceil(fallmins) + 1))
middle = int_start + np.argmax(elev[int_start:int_end])
highest = utc_time + \
timedelta(minutes=_get_max_parab(
dt.timedelta(minutes=_get_max_parab(
elev_inv_func,
max(risemins, middle - 1), min(fallmins, middle + 1),
tol=tol / 60.0
Expand All @@ -401,14 +401,14 @@
if "precision" in kwargs:
precision = kwargs["precision"]
else:
precision = timedelta(seconds=0.001)
precision = dt.timedelta(seconds=0.001)

Check warning on line 404 in pyorbital/orbital.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/orbital.py#L404

Added line #L404 was not covered by tests
if "max_iterations" in kwargs:
nmax_iter = kwargs["max_iterations"]
else:
nmax_iter = 100

sec_step = 0.5
t_step = timedelta(seconds=sec_step / 2.0)
t_step = dt.timedelta(seconds=sec_step / 2.0)

Check warning on line 411 in pyorbital/orbital.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/orbital.py#L411

Added line #L411 was not covered by tests

# Local derivative:
def fprime(timex):
Expand All @@ -418,7 +418,7 @@
obslon, obslat, 0.0)[1]
return el0, (abs(el1) - abs(el0)) / sec_step

tx0 = utc_time - timedelta(seconds=1.0)
tx0 = utc_time - dt.timedelta(seconds=1.0)

Check warning on line 421 in pyorbital/orbital.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/orbital.py#L421

Added line #L421 was not covered by tests
tx1 = utc_time
idx = 0
# eps = 500.
Expand All @@ -431,11 +431,11 @@
# var_scale = np.abs(np.sin(fpr[0] * np.pi/180.))
# var_scale = np.sqrt(var_scale)
var_scale = np.abs(fpr[0])
tx1 = tx0 - timedelta(seconds=(eps * var_scale * fpr[1]))
tx1 = tx0 - dt.timedelta(seconds=(eps * var_scale * fpr[1]))

Check warning on line 434 in pyorbital/orbital.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/orbital.py#L434

Added line #L434 was not covered by tests
idx = idx + 1
# print idx, tx0, tx1, var_scale, fpr
if abs(tx1 - utc_time) < precision and idx < 2:
tx1 = tx1 + timedelta(seconds=1.0)
tx1 = tx1 + dt.timedelta(seconds=1.0)

Check warning on line 438 in pyorbital/orbital.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/orbital.py#L438

Added line #L438 was not covered by tests

if abs(tx1 - tx0) <= precision and idx < nmax_iter:
return tx1
Expand All @@ -445,7 +445,7 @@
def utc2local(self, utc_time):
"""Convert UTC to local time."""
lon, _, _ = self.get_lonlatalt(utc_time)
return utc_time + timedelta(hours=lon * 24 / 360.0)
return utc_time + dt.timedelta(hours=lon * 24 / 360.0)

def get_equatorial_crossing_time(self, tstart, tend, node="ascending", local_time=False,
rtol=1E-9):
Expand Down Expand Up @@ -502,7 +502,7 @@
except ValueError:
# Bisection did not converge
return None
tcross = np.datetime64(int(tcross), time_unit).astype(datetime)
tcross = np.datetime64(int(tcross), time_unit).astype(dt.datetime)

# Convert UTC to local time
if local_time:
Expand All @@ -514,7 +514,7 @@
def _elevation(self, utc_time, lon, lat, alt, horizon, minutes):
"""Compute the elevation."""
return self.get_observer_look(utc_time +
timedelta(minutes=np.float64(minutes)),
dt.timedelta(minutes=np.float64(minutes)),
lon, lat, alt)[1] - horizon


Expand Down Expand Up @@ -1228,8 +1228,8 @@
The input *utc_time* is either a timezone unaware object assumed to be in
UTC, or a timezone aware datetime object in UTC.
"""
if isinstance(utc_time, datetime):
if utc_time.tzinfo and utc_time.tzinfo != timezone.utc:
if isinstance(utc_time, dt.datetime):
if utc_time.tzinfo and utc_time.tzinfo != dt.timezone.utc:
raise ValueError("UTC time expected! Parsing a timezone aware datetime object requires it to be UTC!")
return utc_time.replace(tzinfo=None)

Expand Down Expand Up @@ -1275,11 +1275,11 @@
obs_alt = 0.02
o = Orbital(satellite="METOP-B")

t_start = datetime.now()
t_stop = t_start + timedelta(minutes=20)
t_start = dt.datetime.now()
t_stop = t_start + dt.timedelta(minutes=20)

Check warning on line 1279 in pyorbital/orbital.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/orbital.py#L1278-L1279

Added lines #L1278 - L1279 were not covered by tests
t = t_start
while t < t_stop:
t += timedelta(seconds=15)
t += dt.timedelta(seconds=15)

Check warning on line 1282 in pyorbital/orbital.py

View check run for this annotation

Codecov / codecov/patch

pyorbital/orbital.py#L1282

Added line #L1282 was not covered by tests
lon, lat, alt = o.get_lonlatalt(t)
lon, lat = np.rad2deg((lon, lat))
az, el = o.get_observer_look(t, obs_lon, obs_lat, obs_alt)
Expand Down
4 changes: 2 additions & 2 deletions pyorbital/tests/test_aiaa.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
# TODO: right formal unit tests.
from __future__ import print_function, with_statement

import datetime as dt
import os
import unittest
from datetime import datetime

import numpy as np

Expand Down Expand Up @@ -63,7 +63,7 @@ def get_results(satnumber, delay):
if delay == 0:
utc_time = None
else:
utc_time = datetime.strptime(sline[-1], "%H:%M:%S.%f")
utc_time = dt.datetime.strptime(sline[-1], "%H:%M:%S.%f")
utc_time = utc_time.replace(year=int(sline[-4]),
month=int(sline[-3]),
day=int(sline[-2]))
Expand Down
18 changes: 9 additions & 9 deletions pyorbital/tests/test_astronomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""Unit testing the Astronomy methods and functions."""


from datetime import datetime
import datetime as dt

import dask.array as da
import numpy as np
Expand Down Expand Up @@ -60,16 +60,16 @@ class TestAstronomy:
"""Testing the Astronomy class."""

@pytest.mark.parametrize(
("dt", "exp_jdays", "exp_j2000"),
("dat", "exp_jdays", "exp_j2000"),
[
(datetime(2000, 1, 1, 12, 0), 2451545.0, 0),
(datetime(2009, 10, 8, 14, 30), 2455113.1041666665, 3568.1041666666665),
(dt.datetime(2000, 1, 1, 12, 0), 2451545.0, 0),
(dt.datetime(2009, 10, 8, 14, 30), 2455113.1041666665, 3568.1041666666665),
]
)
def test_jdays(self, dt, exp_jdays, exp_j2000):
def test_jdays(self, dat, exp_jdays, exp_j2000):
"""Test julian day functions."""
assert astr.jdays(dt) == exp_jdays
assert astr.jdays2000(dt) == exp_j2000
assert astr.jdays(dat) == exp_jdays
assert astr.jdays2000(dat) == exp_j2000

@pytest.mark.parametrize(
("lon", "lat", "exp_theta"),
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_sunangles(self, lon, lat, exp_theta, dtype, array_construct):
if array_construct is None and dtype is not None:
pytest.skip(reason="Xarray dependency unavailable")

time_slot = datetime(2011, 9, 23, 12, 0)
time_slot = dt.datetime(2011, 9, 23, 12, 0)
abs_tolerance = 1e-8
if dtype is not None:
lon = array_construct([lon], dtype=dtype)
Expand All @@ -117,7 +117,7 @@ def test_sunangles(self, lon, lat, exp_theta, dtype, array_construct):

def test_sun_earth_distance_correction(self):
"""Test the sun-earth distance correction."""
utc_time = datetime(2022, 6, 15, 12, 0, 0)
utc_time = dt.datetime(2022, 6, 15, 12, 0, 0)
corr = astr.sun_earth_distance_correction(utc_time)
corr_exp = 1.0156952156742332
assert corr == pytest.approx(corr_exp, abs=1e-8)
4 changes: 2 additions & 2 deletions pyorbital/tests/test_geoloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"""Test the geoloc module."""


from datetime import datetime
import datetime as dt

import numpy as np

Expand Down Expand Up @@ -108,7 +108,7 @@ def test_scan_geometry(self):

# Test times

start_of_scan = np.datetime64(datetime(2014, 1, 8, 11, 30))
start_of_scan = np.datetime64(dt.datetime(2014, 1, 8, 11, 30))
times = instrument.times(start_of_scan)

assert times[0, 1] == start_of_scan
Expand Down
Loading
Loading