Skip to content

Commit 8f5b629

Browse files
schlunmaKlaus Zimmermann
authored and
Klaus Zimmermann
committed
Fixed time coordinate of MIRCO-ESM (#1188)
* Fixed time coordinate of MIRCO-ESM * Added comments on fix for MIROC-ESM * Added missing test case
1 parent 4ade1a5 commit 8f5b629

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

esmvalcore/cmor/_fixes/cmip5/miroc_esm.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""Fixes for MIROC-ESM model."""
22

3+
import numpy as np
4+
from cf_units import Unit
35
from iris.coords import DimCoord
46
from iris.exceptions import CoordinateNotFoundError
57

68
from ..common import ClFixHybridPressureCoord
79
from ..fix import Fix
810

9-
1011
Cl = ClFixHybridPressureCoord
1112

1213

@@ -65,7 +66,8 @@ def fix_metadata(self, cubes):
6566
"""
6667
Fix metadata.
6768
68-
Fixes error in air_pressure coordinate, sometimes called AR5PL35
69+
Fixes error in air_pressure coordinate, sometimes called AR5PL35, and
70+
error in time coordinate.
6971
7072
Parameters
7173
----------
@@ -78,6 +80,7 @@ def fix_metadata(self, cubes):
7880
7981
"""
8082
for cube in cubes:
83+
# Fix air_pressure
8184
try:
8285
old = cube.coord('AR5PL35')
8386
dims = cube.coord_dims(old)
@@ -91,4 +94,22 @@ def fix_metadata(self, cubes):
9194
except CoordinateNotFoundError:
9295
pass
9396

97+
# Fix time for files that contain year < 1 (which is not allowed)
98+
if cube.coords('time'):
99+
expected_time_units = Unit('days since 1950-1-1 00:00:00',
100+
calendar='gregorian')
101+
if cube.coord('time').units != expected_time_units:
102+
continue
103+
if cube.coord('time').bounds is None:
104+
continue
105+
106+
# Only apply fix if there is a year < 1 in the first element
107+
# of the time bounds (-711860.5 days from 1950-01-01 is <
108+
# year 1)
109+
if np.isclose(cube.coord('time').bounds[0][0], -711860.5):
110+
new_points = cube.coord('time').points.copy() + 3.5
111+
new_bounds = cube.coord('time').bounds.copy() + 3.5
112+
cube.coord('time').points = new_points
113+
cube.coord('time').bounds = new_bounds
114+
94115
return cubes

tests/integration/cmor/_fixes/cmip5/test_miroc_esm.py

+41
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Test MIROC-ESM fixes."""
22
import unittest
33

4+
import numpy as np
45
from cf_units import Unit
56
from iris.coords import DimCoord
67
from iris.cube import Cube
@@ -79,6 +80,22 @@ def setUp(self):
7980
calendar='gregorian')), 0)
8081
self.cube.add_dim_coord(DimCoord([0, 1], long_name='AR5PL35'), 1)
8182

83+
time_units = Unit('days since 1950-1-1 00:00:00', calendar='gregorian')
84+
85+
# Setup wrong time coordinate that is present in some files
86+
# (-711860.5 days from 1950-01-01 is < year 1)
87+
time_coord = DimCoord(
88+
[-711845.0, -711814.0],
89+
bounds=[[-711860.5, -711829.5], [-711829.5, -711800.0]],
90+
var_name='time',
91+
standard_name='time',
92+
long_name='time',
93+
units=time_units,
94+
)
95+
self.cube_with_wrong_time = Cube([0.0, 1.0], var_name='co2',
96+
units='ppm',
97+
dim_coords_and_dims=[(time_coord, 0)])
98+
8299
self.fix = AllVars(None)
83100

84101
def test_get(self):
@@ -100,3 +117,27 @@ def test_fix_metadata_no_plev(self):
100117
cube = self.fix.fix_metadata([self.cube])[0]
101118
with self.assertRaises(CoordinateNotFoundError):
102119
cube.coord('air_pressure')
120+
121+
def test_fix_metadata_correct_time(self):
122+
"""Test fix for time."""
123+
fixed_cube = self.fix.fix_metadata([self.cube])[0]
124+
time_coord = fixed_cube.coord('time')
125+
np.testing.assert_allclose(time_coord.points, [0, 1])
126+
assert time_coord.bounds is None
127+
128+
def test_fix_metadata_wrong_time(self):
129+
"""Test fix for time."""
130+
fixed_cube = self.fix.fix_metadata([self.cube_with_wrong_time])[0]
131+
time_coord = fixed_cube.coord('time')
132+
np.testing.assert_allclose(time_coord.points, [-711841.5, -711810.5])
133+
np.testing.assert_allclose(
134+
time_coord.bounds,
135+
[[-711857.0, -711826.0], [-711826.0, -711796.5]])
136+
137+
def test_fix_metadata_wrong_time_no_bounds(self):
138+
"""Test fix for time."""
139+
self.cube_with_wrong_time.coord('time').bounds = None
140+
fixed_cube = self.fix.fix_metadata([self.cube_with_wrong_time])[0]
141+
time_coord = fixed_cube.coord('time')
142+
np.testing.assert_allclose(time_coord.points, [-711845.0, -711814.0])
143+
assert time_coord.bounds is None

0 commit comments

Comments
 (0)