Skip to content

Commit 7bec2d4

Browse files
bouweandelajlenh
authored andcommitted
Set correct calendar when reading file dates (#2826)
(cherry picked from commit e2fad72)
1 parent 9f10d32 commit 7bec2d4

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

esmvalcore/local.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,16 @@ def _get_start_end_date(
161161
with Dataset(file) as dataset:
162162
for variable in dataset.variables.values():
163163
var_name = _get_var_name(variable)
164-
if var_name == "time" and "units" in variable.ncattrs():
165-
time_units = Unit(variable.getncattr("units"))
164+
attrs = variable.ncattrs()
165+
if (
166+
var_name == "time"
167+
and "units" in attrs
168+
and "calendar" in attrs
169+
):
170+
time_units = Unit(
171+
variable.getncattr("units"),
172+
calendar=variable.getncattr("calendar"),
173+
)
166174
start_date = isodate.date_isoformat(
167175
time_units.num2date(variable[0]),
168176
format=isodate.isostrf.DATE_BAS_COMPLETE,

tests/unit/local/test_time.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import iris
66
import pyesgf
77
import pytest
8+
from cf_units import Unit
89

910
from esmvalcore.esgf import ESGFFile
1011
from esmvalcore.local import (
@@ -182,11 +183,11 @@ def test_read_datetime_from_cube(tmp_path):
182183
temp_file = tmp_path / "test.nc"
183184
cube = iris.cube.Cube([0, 0, 0, 0], var_name="var")
184185
time = iris.coords.DimCoord(
185-
[0, 100, 200, 366],
186+
[0, 100, 200, 361],
186187
standard_name=None,
187188
long_name="time",
188189
var_name="t",
189-
units="days since 1990-01-01",
190+
units=Unit("days since 1990-01-01", calendar="360_day"),
190191
)
191192
cube.add_dim_coord(time, 0)
192193
iris.save(cube, temp_file)

0 commit comments

Comments
 (0)