Skip to content

Commit

Permalink
Make timestamp conversion more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinkens committed Jul 5, 2024
1 parent 5419df2 commit 566e11a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
10 changes: 5 additions & 5 deletions satpy/readers/avhrr_l1b_gaclac.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from pygac.lac_pod import LACPODReader

from satpy.readers.file_handlers import BaseFileHandler
from satpy.utils import get_legacy_chunk_size
from satpy.utils import datetime64_to_pydatetime, get_legacy_chunk_size

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -185,8 +185,8 @@ def get_dataset(self, key, info):

# Update start/end time using the actual scanline timestamps
times = self.reader.get_times()
self._start_time = times[0].astype(dt.datetime)
self._end_time = times[-1].astype(dt.datetime)
self._start_time = datetime64_to_pydatetime(times[0])
self._end_time = datetime64_to_pydatetime(times[-1])

# Select user-defined scanlines and/or strip invalid coordinates
if (self.start_line is not None or self.end_line is not None
Expand Down Expand Up @@ -224,8 +224,8 @@ def slice(self, data, times): # noqa: A003
"""
sliced = self._slice(data)
times = self._slice(times)
self._start_time = times[0].astype(dt.datetime)
self._end_time = times[-1].astype(dt.datetime)
self._start_time = datetime64_to_pydatetime(times[0])
self._end_time = datetime64_to_pydatetime(times[-1])
return sliced, times

def _slice(self, data):
Expand Down
10 changes: 7 additions & 3 deletions satpy/readers/gms/gms5_vissr_l1b.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
from satpy.readers.file_handlers import BaseFileHandler
from satpy.readers.hrit_jma import mjd2datetime64
from satpy.readers.utils import generic_open
from satpy.utils import get_legacy_chunk_size
from satpy.utils import datetime64_to_pydatetime, get_legacy_chunk_size

CHUNK_SIZE = get_legacy_chunk_size()

Expand Down Expand Up @@ -307,8 +307,7 @@ def _get_orbital_parameters(self):
}

def _get_time_parameters(self):
start_time = mjd2datetime64(self._mode_block["observation_time_mjd"]).astype("datetime64[us]")
start_time = start_time.astype(dt.datetime).replace(second=0, microsecond=0)
start_time = self._get_start_time()
end_time = start_time + dt.timedelta(
minutes=25
) # Source: GMS User Guide, section 3.3.1
Expand All @@ -317,6 +316,11 @@ def _get_time_parameters(self):
"nominal_end_time": end_time,
}

def _get_start_time(self):
start_time = mjd2datetime64(self._mode_block["observation_time_mjd"])
start_time = datetime64_to_pydatetime(start_time)
return start_time.replace(second=0, microsecond=0)

def get_dataset(self, dataset_id, ds_info):
"""Get dataset from file."""
image_data = self._get_image_data()
Expand Down
6 changes: 2 additions & 4 deletions satpy/readers/viirs_vgac_l1c_nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import xarray as xr

from satpy.readers.file_handlers import BaseFileHandler
from satpy.utils import get_legacy_chunk_size
from satpy.utils import datetime64_to_pydatetime, get_legacy_chunk_size

CHUNK_SIZE = get_legacy_chunk_size()
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -75,9 +75,7 @@ def set_time_attrs(self, data):

def dt64_to_datetime(self, dt64):
"""Conversion of numpy.datetime64 to datetime objects."""
if isinstance(dt64, np.datetime64):
return dt64.astype(dt.datetime)
return dt64
return datetime64_to_pydatetime(dt64)

def extract_time_data(self, data, nc):
"""Decode time data."""
Expand Down

0 comments on commit 566e11a

Please sign in to comment.