From dd263394f3403b4eaf375d1c7c5918f7445f526b Mon Sep 17 00:00:00 2001 From: rbeucher Date: Wed, 14 Aug 2024 16:43:05 +1000 Subject: [PATCH 1/6] Fix ERA5 native6 fix to handle single monthly-averaged NetCDF files. --- esmvalcore/cmor/_fixes/native6/era5.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/esmvalcore/cmor/_fixes/native6/era5.py b/esmvalcore/cmor/_fixes/native6/era5.py index 6c67494aaa..e17b506a83 100644 --- a/esmvalcore/cmor/_fixes/native6/era5.py +++ b/esmvalcore/cmor/_fixes/native6/era5.py @@ -20,17 +20,21 @@ def get_frequency(cube): time = cube.coord(axis='T') except iris.exceptions.CoordinateNotFoundError: return 'fx' - + time.convert_units('days since 1850-1-1 00:00:00.0') - if len(time.points) == 1: - if cube.long_name != 'Geopotential': - raise ValueError('Unable to infer frequency of cube ' - f'with length 1 time dimension: {cube}') + + if (len(time.points) == 1 and + cube.long_name == 'Geopotential'): return 'fx' - - interval = time.points[1] - time.points[0] - if interval - 1 / 24 < 1e-4: - return 'hourly' + + if len(time.points) > 1: + interval = time.points[1] - time.points[0] + if interval - 1 / 24 < 1e-4: + return 'hourly' + else: + logger.warning(f"Unable to infer frequency of cube + with length 1 time dimension: {cube}, + assuming 'monthly' frequency") return 'monthly' From e87106ebb6d120e974424dd2c13246c209a78872 Mon Sep 17 00:00:00 2001 From: rbeucher Date: Wed, 14 Aug 2024 16:49:59 +1000 Subject: [PATCH 2/6] Fix string --- esmvalcore/cmor/_fixes/native6/era5.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esmvalcore/cmor/_fixes/native6/era5.py b/esmvalcore/cmor/_fixes/native6/era5.py index e17b506a83..99fe6fdc3e 100644 --- a/esmvalcore/cmor/_fixes/native6/era5.py +++ b/esmvalcore/cmor/_fixes/native6/era5.py @@ -32,9 +32,9 @@ def get_frequency(cube): if interval - 1 / 24 < 1e-4: return 'hourly' else: - logger.warning(f"Unable to infer frequency of cube - with length 1 time dimension: {cube}, - assuming 'monthly' frequency") + logger.warning("Unable to infer frequency of cube " + f"with length 1 time dimension: {cube}," + "assuming 'monthly' frequency") return 'monthly' From ebd55123857ce279f808ce7e09a54d88489eaf7d Mon Sep 17 00:00:00 2001 From: rbeucher Date: Wed, 14 Aug 2024 16:55:41 +1000 Subject: [PATCH 3/6] Fix cosmetics --- esmvalcore/cmor/_fixes/native6/era5.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/esmvalcore/cmor/_fixes/native6/era5.py b/esmvalcore/cmor/_fixes/native6/era5.py index 99fe6fdc3e..f9cdbc1f46 100644 --- a/esmvalcore/cmor/_fixes/native6/era5.py +++ b/esmvalcore/cmor/_fixes/native6/era5.py @@ -20,13 +20,13 @@ def get_frequency(cube): time = cube.coord(axis='T') except iris.exceptions.CoordinateNotFoundError: return 'fx' - + time.convert_units('days since 1850-1-1 00:00:00.0') - + if (len(time.points) == 1 and - cube.long_name == 'Geopotential'): + cube.long_name == 'Geopotential'): return 'fx' - + if len(time.points) > 1: interval = time.points[1] - time.points[0] if interval - 1 / 24 < 1e-4: From a4b13a1917a6fc7d0ec95a2c6c6cf7c94aa195bd Mon Sep 17 00:00:00 2001 From: rbeucher Date: Wed, 14 Aug 2024 17:04:36 +1000 Subject: [PATCH 4/6] happy? --- esmvalcore/cmor/_fixes/native6/era5.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esmvalcore/cmor/_fixes/native6/era5.py b/esmvalcore/cmor/_fixes/native6/era5.py index f9cdbc1f46..4c938e29b6 100644 --- a/esmvalcore/cmor/_fixes/native6/era5.py +++ b/esmvalcore/cmor/_fixes/native6/era5.py @@ -24,8 +24,8 @@ def get_frequency(cube): time.convert_units('days since 1850-1-1 00:00:00.0') if (len(time.points) == 1 and - cube.long_name == 'Geopotential'): - return 'fx' + cube.long_name == 'Geopotential'): + return 'fx' if len(time.points) > 1: interval = time.points[1] - time.points[0] From ce1c78c221a75318fa4ac9f40e75a906c8b5ef95 Mon Sep 17 00:00:00 2001 From: rbeucher Date: Wed, 14 Aug 2024 17:13:36 +1000 Subject: [PATCH 5/6] Formatting --- esmvalcore/cmor/_fixes/native6/era5.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/esmvalcore/cmor/_fixes/native6/era5.py b/esmvalcore/cmor/_fixes/native6/era5.py index 4c938e29b6..8375d5ac1c 100644 --- a/esmvalcore/cmor/_fixes/native6/era5.py +++ b/esmvalcore/cmor/_fixes/native6/era5.py @@ -23,9 +23,8 @@ def get_frequency(cube): time.convert_units('days since 1850-1-1 00:00:00.0') - if (len(time.points) == 1 and - cube.long_name == 'Geopotential'): - return 'fx' + if (len(time.points) == 1 and cube.long_name == 'Geopotential'): + return 'fx' if len(time.points) > 1: interval = time.points[1] - time.points[0] From 5bbae26949e81f37c4429b597ea9290db3a2df71 Mon Sep 17 00:00:00 2001 From: rbeucher Date: Wed, 14 Aug 2024 18:08:34 +1000 Subject: [PATCH 6/6] Fix test --- tests/integration/cmor/_fixes/native6/test_era5.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/integration/cmor/_fixes/native6/test_era5.py b/tests/integration/cmor/_fixes/native6/test_era5.py index 70b432541d..553485cbf5 100644 --- a/tests/integration/cmor/_fixes/native6/test_era5.py +++ b/tests/integration/cmor/_fixes/native6/test_era5.py @@ -86,8 +86,7 @@ def test_get_frequency_fx(): ) assert get_frequency(cube) == 'fx' cube.long_name = 'Not geopotential' - with pytest.raises(ValueError): - get_frequency(cube) + assert get_frequency(cube) == 'monthly' def _era5_latitude():