From 0603bc5445c2555d62fcfe17646dab8db547efe7 Mon Sep 17 00:00:00 2001 From: Sandor Kertesz Date: Thu, 6 Jun 2024 15:49:56 +0100 Subject: [PATCH] Fix netcdf with time coordinate containing cftime (#403) --- src/earthkit/data/readers/netcdf/coords.py | 9 ++++++++- src/earthkit/data/readers/netcdf/fieldlist.py | 10 ++++++++-- tests/netcdf/test_netcdf_fieldlist.py | 12 ++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/earthkit/data/readers/netcdf/coords.py b/src/earthkit/data/readers/netcdf/coords.py index 3c3528f7..2c8f44b3 100644 --- a/src/earthkit/data/readers/netcdf/coords.py +++ b/src/earthkit/data/readers/netcdf/coords.py @@ -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): diff --git a/src/earthkit/data/readers/netcdf/fieldlist.py b/src/earthkit/data/readers/netcdf/fieldlist.py index ca62a91f..280062e2 100644 --- a/src/earthkit/data/readers/netcdf/fieldlist.py +++ b/src/earthkit/data/readers/netcdf/fieldlist.py @@ -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)) @@ -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 ---------- diff --git a/tests/netcdf/test_netcdf_fieldlist.py b/tests/netcdf/test_netcdf_fieldlist.py index 3615a7a4..317aa27f 100644 --- a/tests/netcdf/test_netcdf_fieldlist.py +++ b/tests/netcdf/test_netcdf_fieldlist.py @@ -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(): @@ -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