Skip to content

Commit

Permalink
Fix netcdf with time coordinate containing cftime (#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandorkertesz authored Jun 6, 2024
1 parent 2034750 commit 0603bc5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/earthkit/data/readers/netcdf/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@


def as_datetime(self, time):
return datetime.datetime.strptime(str(time)[:19], "%Y-%m-%dT%H:%M:%S")
if isinstance(time, datetime.datetime):
return time
elif hasattr(time, "isoformat"):
return datetime.datetime.fromisoformat(time.isoformat())
elif hasattr(time, "to_datetime"):
return time.to_datetime()
else:
return datetime.datetime.strptime(str(time)[:19], "%Y-%m-%dT%H:%M:%S")


def as_level(self, level):
Expand Down
10 changes: 8 additions & 2 deletions src/earthkit/data/readers/netcdf/fieldlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ def _skip_attr(v, attr_name):
use = True

# Of course, not every one sets the standard_name
if standard_name in ["time", "forecast_reference_time"] or long_name in ["time"] or axis == "T":
if (
standard_name in ["time", "forecast_reference_time"]
or long_name in ["time"]
or coord_name.lower() in ["time"]
or axis == "T"
):
# we might not be able to convert time to datetime
try:
coordinates.append(TimeCoordinate(c, coord in info))
Expand Down Expand Up @@ -200,7 +205,8 @@ def to_xarray_multi_from_paths(cls, paths, **kwargs):
)

def to_netcdf(self, *args, **kwargs):
"""Save the data to a netCDF file.
"""
Save the data to a netCDF file.
Parameters
----------
Expand Down
12 changes: 12 additions & 0 deletions tests/netcdf/test_netcdf_fieldlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import numpy as np

import earthkit.data
from earthkit.data.testing import earthkit_remote_test_data_file


def test_netcdf_fieldlist_string_coord():
Expand Down Expand Up @@ -60,6 +61,17 @@ def test_netcdf_fieldlist_bounds():
assert len(ds) == 2


def test_netcdf_fieldlist_ctime():
ds = earthkit.data.from_source(
"url",
earthkit_remote_test_data_file("test-data", "zgrid_rhgmet_metop_200701_R_2305_0010.nc"),
)

assert len(ds) == 1506
assert ds[0].metadata("valid_datetime") == "2007-01-16T00:00:00"
assert ds[5].metadata("valid_datetime") == "2007-01-16T00:00:00"


if __name__ == "__main__":
from earthkit.data.testing import main

Expand Down

0 comments on commit 0603bc5

Please sign in to comment.