Skip to content

Commit ad39a99

Browse files
Ignore overflow errors when converting branch times (#2668)
Co-authored-by: Valeriu Predoi <[email protected]>
1 parent c6dd6f5 commit ad39a99

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

esmvalcore/cmor/_fixes/fix.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -831,15 +831,23 @@ def _fix_time_units(self, cube: Cube, cube_coord: Coord) -> None:
831831

832832
branch_parent = "branch_time_in_parent"
833833
if branch_parent in attrs:
834-
attrs[branch_parent] = parent_units.convert(
835-
attrs[branch_parent], cube_coord.units
836-
)
834+
try:
835+
attrs[branch_parent] = parent_units.convert(
836+
attrs[branch_parent], cube_coord.units
837+
)
838+
except OverflowError:
839+
# This happens when the time is very large.
840+
pass
837841

838842
branch_child = "branch_time_in_child"
839843
if branch_child in attrs:
840-
attrs[branch_child] = old_units.convert(
841-
attrs[branch_child], cube_coord.units
842-
)
844+
try:
845+
attrs[branch_child] = old_units.convert(
846+
attrs[branch_child], cube_coord.units
847+
)
848+
except OverflowError:
849+
# This happens when the time is very large.
850+
pass
843851

844852
def _fix_time_bounds(self, cube: Cube, cube_coord: Coord) -> None:
845853
"""Fix time bounds."""

tests/integration/cmor/test_fix.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -734,18 +734,42 @@ def test_fix_metadata_amon_tas_invalid_time_units(self):
734734
assert self.mock_debug.call_count == 3
735735
assert self.mock_warning.call_count == 7
736736

737-
def test_fix_metadata_amon_tas_invalid_time_attrs(self):
737+
@pytest.mark.parametrize("problem", ["none", "bad_units", "bad_values"])
738+
def test_fix_metadata_amon_tas_invalid_time_attrs(self, problem):
738739
"""Test ``fix_metadata`` with invalid time attributes."""
739740
short_name = "tas"
740741
project = "CMIP6"
741742
dataset = "__MODEL_WITH_NO_EXPLICIT_FIX__"
742743
mip = "Amon"
743744

744-
self.cubes_2d_latlon[0].attributes = {
745-
"parent_time_units": "this is certainly not a unit",
746-
"branch_time_in_parent": "BRANCH TIME IN PARENT",
747-
"branch_time_in_child": "BRANCH TIME IN CHILD",
748-
}
745+
attributes = {}
746+
expected_attributes = {}
747+
if problem == "bad_units":
748+
attributes["parent_time_units"] = "this is certainly not a unit"
749+
else:
750+
attributes["parent_time_units"] = "days since 1000-01-01 00:00:0.0"
751+
752+
if problem == "bad_values":
753+
attributes["branch_time_in_child"] = 1e9
754+
attributes["branch_time_in_parent"] = 1e9
755+
else:
756+
attributes["branch_time_in_child"] = 0.0
757+
attributes["branch_time_in_parent"] = 91676.0
758+
759+
if problem == "none":
760+
expected_attributes = {
761+
"parent_time_units": "days since 1850-1-1 00:00:00",
762+
"branch_time_in_child": 365,
763+
"branch_time_in_parent": -218574,
764+
}
765+
else:
766+
expected_attributes = attributes.copy()
767+
if problem == "bad_values":
768+
expected_attributes["parent_time_units"] = (
769+
"days since 1850-1-1 00:00:00"
770+
)
771+
772+
self.cubes_2d_latlon[0].attributes = attributes
749773

750774
fixed_cubes = fix_metadata(
751775
self.cubes_2d_latlon,
@@ -762,11 +786,7 @@ def test_fix_metadata_amon_tas_invalid_time_attrs(self):
762786
self.assert_lat_metadata(fixed_cube)
763787
self.assert_lon_metadata(fixed_cube)
764788

765-
assert fixed_cube.attributes == {
766-
"parent_time_units": "this is certainly not a unit",
767-
"branch_time_in_parent": "BRANCH TIME IN PARENT",
768-
"branch_time_in_child": "BRANCH TIME IN CHILD",
769-
}
789+
assert fixed_cube.attributes == expected_attributes
770790

771791
cmor_check_metadata(fixed_cube, project, mip, short_name)
772792

0 commit comments

Comments
 (0)