From f3b6b04cf46d20c318c830d24b28cac33b4b96e6 Mon Sep 17 00:00:00 2001 From: bnb32 Date: Sat, 9 Nov 2024 06:22:28 -0700 Subject: [PATCH] mfdataset preprocessing: cftime to datetimeindex before casting to int --- sup3r/utilities/utilities.py | 6 +++++- tests/utilities/test_era_downloader.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/sup3r/utilities/utilities.py b/sup3r/utilities/utilities.py index c79467fb3..ebfb07a50 100644 --- a/sup3r/utilities/utilities.py +++ b/sup3r/utilities/utilities.py @@ -22,7 +22,11 @@ def preprocess_datasets(dset): """Standardization preprocessing applied before datasets are concatenated by ``xr.open_mfdataset``""" if 'time' in dset and dset.time.size > 1: - ti = dset.time.astype(int) + if 'time' in dset.indexes and hasattr( + dset.indexes['time'], 'to_datetimeindex' + ): + dset['time'] = dset.indexes['time'].to_datetimeindex() + ti = dset['time'].astype(int) dset['time'] = ti if 'latitude' in dset.dims: dset = dset.swap_dims({'latitude': 'south_north'}) diff --git a/tests/utilities/test_era_downloader.py b/tests/utilities/test_era_downloader.py index 5b79109c1..2afbc137b 100644 --- a/tests/utilities/test_era_downloader.py +++ b/tests/utilities/test_era_downloader.py @@ -98,7 +98,11 @@ def test_era_dl_year(tmpdir_factory): yearly_file_pattern=yearly_file_pattern, max_workers=1, combine_all_files=True, - res_kwargs={'compat': 'override', 'engine': 'netcdf4'}, + res_kwargs={ + 'compat': 'override', + 'engine': 'netcdf4', + 'concat_dim': 'time', + }, ) combined_file = yearly_file_pattern.replace('_{var}_', '').format(