Skip to content

Commit

Permalink
Fix datetime imports
Browse files Browse the repository at this point in the history
  • Loading branch information
lahtinep committed Apr 22, 2024
1 parent 211cda2 commit a5a9815
Show file tree
Hide file tree
Showing 147 changed files with 1,006 additions and 916 deletions.
4 changes: 2 additions & 2 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

from __future__ import annotations

import datetime as dt
import os
import sys
from datetime import datetime

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down Expand Up @@ -117,7 +117,7 @@ def __getattr__(cls, name):

# General information about the project.
project = u"Satpy"
copyright = u"2009-{}, The PyTroll Team".format(datetime.utcnow().strftime("%Y")) # noqa: A001
copyright = u"2009-{}, The PyTroll Team".format(dt.datetime.utcnow().strftime("%Y")) # noqa: A001

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
6 changes: 4 additions & 2 deletions satpy/cf/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
#
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.

"""CF decoding."""

import copy
import datetime as dt
import json
from datetime import datetime


def decode_attrs(attrs):
Expand Down Expand Up @@ -69,6 +71,6 @@ def _datetime_parser_json(json_dict):
def _str2datetime(string):
"""Convert string to datetime object."""
try:
return datetime.fromisoformat(string)
return dt.datetime.fromisoformat(string)
except (TypeError, ValueError):
return None
6 changes: 4 additions & 2 deletions satpy/composites/viirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
#
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.

"""Composite classes for the VIIRS instrument."""

from __future__ import annotations

import datetime as dt
import logging
import math
from datetime import datetime

import dask
import dask.array as da
Expand Down Expand Up @@ -842,7 +844,7 @@ def _linear_normalization_from_0to1(
data[mask] = data[mask] / theoretical_max


def _check_moon_phase(moon_datasets: list[xr.DataArray], start_time: datetime) -> float:
def _check_moon_phase(moon_datasets: list[xr.DataArray], start_time: dt.datetime) -> float:
"""Check if we have Moon phase as an input dataset and, if not, calculate it."""
if moon_datasets:
# convert to decimal instead of %
Expand Down
9 changes: 5 additions & 4 deletions satpy/dataset/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
#
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.

"""Utilities for merging metadata from various sources."""

import datetime as dt
import warnings
from collections.abc import Collection
from datetime import datetime
from functools import partial, reduce
from operator import eq, is_

Expand Down Expand Up @@ -135,7 +136,7 @@ def _combine_time_parameters(values):

def _filter_time_values(values):
"""Remove values that are not datetime objects."""
return [v for v in values if isinstance(v, datetime)]
return [v for v in values if isinstance(v, dt.datetime)]


def average_datetimes(datetime_list):
Expand All @@ -152,8 +153,8 @@ def average_datetimes(datetime_list):
Returns: Average datetime as a datetime object
"""
total = [datetime.timestamp(dt) for dt in datetime_list]
return datetime.fromtimestamp(sum(total) / len(total))
total = [dt.datetime.timestamp(d) for d in datetime_list]
return dt.datetime.fromtimestamp(sum(total) / len(total))


def _are_values_combinable(values):
Expand Down
10 changes: 5 additions & 5 deletions satpy/modifiers/angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
"""Utilties for getting various angles for a dataset.."""
from __future__ import annotations

import datetime as dt
import hashlib
import os
import shutil
import warnings
from datetime import datetime
from functools import update_wrapper
from glob import glob
from typing import Any, Callable, Optional, Union
Expand All @@ -45,7 +45,7 @@
# pyorbital's get_observer_look function.
# The difference is on the order of 1e-10 at most as time changes so we force
# it to a single time for easier caching. It is *only* used if caching.
STATIC_EARTH_INERTIAL_DATETIME = datetime(2000, 1, 1, 12, 0, 0)
STATIC_EARTH_INERTIAL_DATETIME = dt.datetime(2000, 1, 1, 12, 0, 0)
DEFAULT_UNCACHE_TYPES = (SwathDefinition, xr.DataArray, da.Array)
HASHABLE_GEOMETRIES = (AreaDefinition, StackedAreaDefinition)

Expand Down Expand Up @@ -263,7 +263,7 @@ def _hash_args(*args, unhashable_types=DEFAULT_UNCACHE_TYPES):
raise TypeError(f"Unhashable type ({type(arg)}).")
if isinstance(arg, HASHABLE_GEOMETRIES):
arg = hash(arg)
elif isinstance(arg, datetime):
elif isinstance(arg, dt.datetime):
arg = arg.isoformat(" ")
hashable_args.append(arg)
arg_hash = hashlib.sha1() # nosec
Expand All @@ -274,7 +274,7 @@ def _hash_args(*args, unhashable_types=DEFAULT_UNCACHE_TYPES):
def _sanitize_observer_look_args(*args):
new_args = []
for arg in args:
if isinstance(arg, datetime):
if isinstance(arg, dt.datetime):
new_args.append(STATIC_EARTH_INERTIAL_DATETIME)
elif isinstance(arg, (float, np.float64, np.float32)):
# Round floating point numbers to nearest tenth. Numpy types don't
Expand Down Expand Up @@ -448,7 +448,7 @@ def _cos_zen_ndarray(lons, lats, utc_time):
return pyob_cos_zen(utc_time, lons, lats)


def _get_sun_azimuth_ndarray(lons: np.ndarray, lats: np.ndarray, start_time: datetime) -> np.ndarray:
def _get_sun_azimuth_ndarray(lons: np.ndarray, lats: np.ndarray, start_time: dt.datetime) -> np.ndarray:
with ignore_invalid_float_warnings():
suna = get_alt_az(start_time, lons, lats)[1]
suna = np.rad2deg(suna)
Expand Down
8 changes: 5 additions & 3 deletions satpy/readers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
#
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.

"""Shared objects of the various reader classes."""

from __future__ import annotations

import datetime as dt
import logging
import os
import pathlib
import pickle # nosec B403
import warnings
from datetime import datetime, timedelta
from functools import total_ordering

import yaml
Expand Down Expand Up @@ -213,7 +215,7 @@ def _get_sorted_file_groups(all_file_keys, time_threshold): # noqa: D417
# interest of sorting
flat_keys = ((v[0], rn, v[1]) for (rn, vL) in all_file_keys.items() for v in vL)
prev_key = None
threshold = timedelta(seconds=time_threshold)
threshold = dt.timedelta(seconds=time_threshold)
# file_groups is sorted, because dictionaries are sorted by insertion
# order in Python 3.7+
file_groups = {}
Expand All @@ -222,7 +224,7 @@ def _get_sorted_file_groups(all_file_keys, time_threshold): # noqa: D417
if prev_key is None:
is_new_group = True
prev_key = gk
elif isinstance(gk[0], datetime):
elif isinstance(gk[0], dt.datetime):
# datetimes within threshold difference are "the same time"
is_new_group = (gk[0] - prev_key[0]) > threshold
else:
Expand Down
12 changes: 7 additions & 5 deletions satpy/readers/aapp_l1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.

"""Reader for aapp level 1b data.
Options for loading:
Expand All @@ -24,9 +25,10 @@
https://nwp-saf.eumetsat.int/site/download/documentation/aapp/NWPSAF-MF-UD-003_Formats_v8.0.pdf
"""

import datetime as dt
import functools
import logging
from datetime import datetime, timedelta

import dask.array as da
import numpy as np
Expand Down Expand Up @@ -102,14 +104,14 @@ def _set_filedata_layout(self):
@property
def start_time(self):
"""Get the time of the first observation."""
return datetime(self._data["scnlinyr"][0], 1, 1) + timedelta(
return dt.datetime(self._data["scnlinyr"][0], 1, 1) + dt.timedelta(
days=int(self._data["scnlindy"][0]) - 1,
milliseconds=int(self._data["scnlintime"][0]))

@property
def end_time(self):
"""Get the time of the final observation."""
return datetime(self._data["scnlinyr"][-1], 1, 1) + timedelta(
return dt.datetime(self._data["scnlinyr"][-1], 1, 1) + dt.timedelta(
days=int(self._data["scnlindy"][-1]) - 1,
milliseconds=int(self._data["scnlintime"][-1]))

Expand All @@ -129,10 +131,10 @@ def _get_platform_name(self, platform_names_lookup):

def read(self):
"""Read the data."""
tic = datetime.now()
tic = dt.datetime.now()
header = np.memmap(self.filename, dtype=self._header_type, mode="r", shape=(1, ))
data = np.memmap(self.filename, dtype=self._scan_type, offset=self._header_offset, mode="r")
logger.debug("Reading time %s", str(datetime.now() - tic))
logger.debug("Reading time %s", str(dt.datetime.now() - tic))

self._header = header
self._data = data
Expand Down
7 changes: 4 additions & 3 deletions satpy/readers/abi_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
#
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.

"""Advance Baseline Imager reader base class for the Level 1b and l2+ reader."""

import datetime as dt
import logging
import math
from contextlib import suppress
from datetime import datetime

import dask
import numpy as np
Expand Down Expand Up @@ -291,12 +292,12 @@ def _get_areadef_fixedgrid(self, key):
@property
def start_time(self):
"""Start time of the current file's observations."""
return datetime.strptime(self.nc.attrs["time_coverage_start"], "%Y-%m-%dT%H:%M:%S.%fZ")
return dt.datetime.strptime(self.nc.attrs["time_coverage_start"], "%Y-%m-%dT%H:%M:%S.%fZ")

@property
def end_time(self):
"""End time of the current file's observations."""
return datetime.strptime(self.nc.attrs["time_coverage_end"], "%Y-%m-%dT%H:%M:%S.%fZ")
return dt.datetime.strptime(self.nc.attrs["time_coverage_end"], "%Y-%m-%dT%H:%M:%S.%fZ")

def spatial_resolution_to_number(self):
"""Convert the 'spatial_resolution' global attribute to meters."""
Expand Down
6 changes: 4 additions & 2 deletions satpy/readers/acspo.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
#
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.

"""ACSPO SST Reader.
See the following page for more information:
https://podaac.jpl.nasa.gov/dataset/VIIRS_NPP-OSPO-L2P-v2.3
"""

import datetime as dt
import logging
from datetime import datetime

import numpy as np

Expand Down Expand Up @@ -83,7 +85,7 @@ def get_shape(self, ds_id, ds_info):

@staticmethod
def _parse_datetime(datestr):
return datetime.strptime(datestr, "%Y%m%dT%H%M%SZ")
return dt.datetime.strptime(datestr, "%Y%m%dT%H%M%SZ")

@property
def start_time(self):
Expand Down
21 changes: 11 additions & 10 deletions satpy/readers/ahi_hsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#
# You should have received a copy of the GNU General Public License along with
# satpy. If not, see <http://www.gnu.org/licenses/>.

"""Advanced Himawari Imager (AHI) standard format data reader.
References:
Expand Down Expand Up @@ -58,10 +59,10 @@
"""

import datetime as dt
import logging
import os
import warnings
from datetime import datetime, timedelta

import dask.array as da
import numpy as np
Expand Down Expand Up @@ -419,12 +420,12 @@ def end_time(self):
@property
def observation_start_time(self):
"""Get the observation start time."""
return datetime(1858, 11, 17) + timedelta(days=float(self.basic_info["observation_start_time"].item()))
return dt.datetime(1858, 11, 17) + dt.timedelta(days=float(self.basic_info["observation_start_time"].item()))

@property
def observation_end_time(self):
"""Get the observation end time."""
return datetime(1858, 11, 17) + timedelta(days=float(self.basic_info["observation_end_time"].item()))
return dt.datetime(1858, 11, 17) + dt.timedelta(days=float(self.basic_info["observation_end_time"].item()))

@property
def _timeline(self):
Expand Down Expand Up @@ -760,7 +761,7 @@ def __init__(self, timeline, area):

def _parse_timeline(self, timeline):
try:
return datetime.strptime(timeline, "%H%M").time()
return dt.datetime.strptime(timeline, "%H%M").time()
except ValueError:
return None

Expand All @@ -771,8 +772,8 @@ def get_nominal_start_time(self, observation_start_time):
def get_nominal_end_time(self, nominal_start_time):
"""Get nominal end time of the scan."""
freq = self._observation_frequency
return nominal_start_time + timedelta(minutes=freq // 60,
seconds=freq % 60)
return nominal_start_time + dt.timedelta(minutes=freq // 60,
seconds=freq % 60)

def _modify_observation_time_for_nominal(self, observation_time):
"""Round observation time to a nominal time based on known observation frequency.
Expand All @@ -793,8 +794,8 @@ def _modify_observation_time_for_nominal(self, observation_time):
)
return observation_time
timeline = self._get_closest_timeline(observation_time)
dt = self._get_offset_relative_to_timeline()
return timeline + timedelta(minutes=dt//60, seconds=dt % 60)
date = self._get_offset_relative_to_timeline()
return timeline + dt.timedelta(minutes=date//60, seconds=date % 60)

def _get_closest_timeline(self, observation_time):
"""Find the closest timeline for the given observation time.
Expand All @@ -808,11 +809,11 @@ def _get_closest_timeline(self, observation_time):
"""
delta_days = [-1, 0, 1]
surrounding_dates = [
(observation_time + timedelta(days=delta)).date()
(observation_time + dt.timedelta(days=delta)).date()
for delta in delta_days
]
timelines = [
datetime.combine(date, self.timeline)
dt.datetime.combine(date, self.timeline)
for date in surrounding_dates
]
diffs = [
Expand Down
Loading

0 comments on commit a5a9815

Please sign in to comment.