Skip to content

Commit

Permalink
Merge pull request #177 from pnuu/fix-datetime-imports
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese authored Dec 13, 2024
2 parents 932c493 + 3384cf8 commit d843af4
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 75 deletions.
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 @@ def hnorm(m):
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)

# 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

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)
# 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 @@ def get_next_passes(self, utc_time, length, lon, lat, alt, tol=0.001, horizon=0)
"""
# 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 @@ def get_next_passes(self, utc_time, length, lon, lat, alt, tol=0.001, horizon=0)
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 @@ def get_next_passes(self, utc_time, length, lon, lat, alt, tol=0.001, horizon=0)
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 @@ def _get_time_at_horizon(self, utc_time, obslon, obslat, **kwargs):
if "precision" in kwargs:
precision = kwargs["precision"]
else:
precision = timedelta(seconds=0.001)
precision = dt.timedelta(seconds=0.001)
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)

# Local derivative:
def fprime(timex):
Expand All @@ -418,7 +418,7 @@ def fprime(timex):
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)
tx1 = utc_time
idx = 0
# eps = 500.
Expand All @@ -431,11 +431,11 @@ def fprime(timex):
# 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]))
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)

if abs(tx1 - tx0) <= precision and idx < nmax_iter:
return tx1
Expand All @@ -445,7 +445,7 @@ def fprime(timex):
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 @@ def _nprime(time_f):
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 _nprime(time_f):
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 @@ def _get_tz_unaware_utctime(utc_time):
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 @@ def kep2xyz(kep):
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)
t = t_start
while t < t_stop:
t += timedelta(seconds=15)
t += dt.timedelta(seconds=15)
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

0 comments on commit d843af4

Please sign in to comment.