Skip to content

Commit 9c5681f

Browse files
authored
Added AllVars fix for ICON-ESM-LR (#1582)
1 parent 8af46d4 commit 9c5681f

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Fixes for ICON-ESM-LR model."""
2+
3+
from ..fix import Fix
4+
5+
6+
class AllVars(Fix):
7+
"""Fixes for all variables."""
8+
9+
def fix_metadata(self, cubes):
10+
"""Rename ``var_name`` of latitude and longitude.
11+
12+
Parameters
13+
----------
14+
cubes: iris.cube.CubeList
15+
Input cubes.
16+
17+
Returns
18+
-------
19+
iris.cube.CubeList
20+
Fixed cubes.
21+
22+
"""
23+
varnames_to_change = {
24+
'latitude': 'lat',
25+
'longitude': 'lon',
26+
}
27+
28+
for cube in cubes:
29+
for (std_name, var_name) in varnames_to_change.items():
30+
if cube.coords(std_name):
31+
cube.coord(std_name).var_name = var_name
32+
33+
return cubes
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""Tests for the fixes of ICON-ESM-LR."""
2+
import pytest
3+
from iris.coords import AuxCoord
4+
from iris.cube import Cube, CubeList
5+
6+
from esmvalcore.cmor._fixes.cmip6.icon_esm_lr import AllVars
7+
from esmvalcore.cmor.fix import Fix
8+
9+
10+
@pytest.fixture
11+
def cubes():
12+
"""Cubes to test fix."""
13+
correct_lat_coord = AuxCoord([0.0], var_name='lat',
14+
standard_name='latitude')
15+
wrong_lat_coord = AuxCoord([0.0], var_name='latitude',
16+
standard_name='latitude')
17+
correct_lon_coord = AuxCoord([0.0], var_name='lon',
18+
standard_name='longitude')
19+
wrong_lon_coord = AuxCoord([0.0], var_name='longitude',
20+
standard_name='longitude')
21+
correct_cube = Cube(
22+
[10.0],
23+
var_name='tas',
24+
aux_coords_and_dims=[(correct_lat_coord, 0), (correct_lon_coord, 0)],
25+
)
26+
wrong_cube = Cube(
27+
[10.0],
28+
var_name='pr',
29+
aux_coords_and_dims=[(wrong_lat_coord, 0), (wrong_lon_coord, 0)],
30+
)
31+
return CubeList([correct_cube, wrong_cube])
32+
33+
34+
def test_get_allvars_fix():
35+
"""Test getting of fix."""
36+
fix = Fix.get_fixes('CMIP6', 'ICON-ESM-LR', 'Amon', 'tas')
37+
assert fix == [AllVars(None)]
38+
39+
40+
def test_allvars_fix_metadata_lat_lon(cubes):
41+
"""Test ``fix_metadata`` for all variables."""
42+
fix = AllVars(None)
43+
out_cubes = fix.fix_metadata(cubes)
44+
assert cubes is out_cubes
45+
for cube in out_cubes:
46+
lat_coord = cube.coord('latitude')
47+
lon_coord = cube.coord('longitude')
48+
assert lat_coord.var_name == 'lat'
49+
assert lon_coord.var_name == 'lon'
50+
51+
52+
def test_allvars_fix_metadata_lat(cubes):
53+
"""Test ``fix_metadata`` for all variables."""
54+
for cube in cubes:
55+
cube.remove_coord('longitude')
56+
fix = AllVars(None)
57+
out_cubes = fix.fix_metadata(cubes)
58+
assert cubes is out_cubes
59+
for cube in out_cubes:
60+
lat_coord = cube.coord('latitude')
61+
assert lat_coord.var_name == 'lat'
62+
63+
64+
def test_allvars_fix_metadata_lon(cubes):
65+
"""Test ``fix_metadata`` for all variables."""
66+
for cube in cubes:
67+
cube.remove_coord('latitude')
68+
fix = AllVars(None)
69+
out_cubes = fix.fix_metadata(cubes)
70+
assert cubes is out_cubes
71+
for cube in out_cubes:
72+
lon_coord = cube.coord('longitude')
73+
assert lon_coord.var_name == 'lon'
74+
75+
76+
def test_allvars_fix_metadata_no_lat_lon(cubes):
77+
"""Test ``fix_metadata`` for all variables."""
78+
for cube in cubes:
79+
cube.remove_coord('latitude')
80+
cube.remove_coord('longitude')
81+
fix = AllVars(None)
82+
out_cubes = fix.fix_metadata(cubes)
83+
assert cubes is out_cubes

0 commit comments

Comments
 (0)