11"""Tests for the fixes of CESM2."""
22
3- import os
4- import unittest .mock
3+ from __future__ import annotations
4+
5+ from collections .abc import Sequence
6+ from typing import TYPE_CHECKING
57
68import iris
79import iris .cube
2729from esmvalcore .cmor ._fixes .common import SiconcFixScalarCoord
2830from esmvalcore .cmor ._fixes .fix import GenericFix
2931from esmvalcore .cmor .fix import Fix
30- from esmvalcore .cmor .table import get_var_info
32+ from esmvalcore .cmor .table import VariableInfo , get_var_info
33+
34+ if TYPE_CHECKING :
35+ from pathlib import Path
3136
3237
3338def test_get_cl_fix ():
@@ -66,11 +71,7 @@ def test_get_cl_fix():
6671)
6772
6873
69- @unittest .mock .patch (
70- "esmvalcore.cmor._fixes.cmip6.cesm2.Fix.get_fixed_filepath" ,
71- autospec = True ,
72- )
73- def test_cl_fix_file (mock_get_filepath , tmp_path , test_data_path ):
74+ def test_cl_fix_file (tmp_path : Path , test_data_path : Path ) -> None :
7475 """Test ``fix_file`` for ``cl``."""
7576 nc_path = test_data_path / "cesm2_cl.nc"
7677 cubes = iris .load (str (nc_path ))
@@ -89,24 +90,17 @@ def test_cl_fix_file(mock_get_filepath, tmp_path, test_data_path):
8990 assert not raw_cube .coords ("air_pressure" )
9091
9192 # Apply fix
92- mock_get_filepath .return_value = os .path .join (
93- tmp_path ,
94- "fixed_cesm2_cl.nc" ,
95- )
96- fix = Cl (None )
97- fixed_file = fix .fix_file (nc_path , tmp_path )
98- mock_get_filepath .assert_called_once_with (
99- tmp_path ,
100- nc_path ,
101- add_unique_suffix = False ,
102- )
103- fixed_cubes = iris .load (fixed_file )
104- assert len (fixed_cubes ) == 2
105- var_names = [cube .var_name for cube in fixed_cubes ]
106- assert "cl" in var_names
107- assert "ps" in var_names
108- fixed_cl_cube = fixed_cubes .extract_cube (
109- "cloud_area_fraction_in_atmosphere_layer" ,
93+ vardef = get_var_info ("CMIP6" , "Amon" , "cl" )
94+ assert isinstance (vardef , VariableInfo )
95+ fix = Cl (vardef )
96+ fixed_cubes = fix .fix_file (nc_path , tmp_path )
97+ assert isinstance (fixed_cubes , Sequence )
98+ assert len (fixed_cubes ) == 1
99+ fixed_cl_cube = fixed_cubes [0 ]
100+ assert fixed_cl_cube .var_name == "cl"
101+ assert (
102+ fixed_cl_cube .standard_name
103+ == "cloud_area_fraction_in_atmosphere_layer"
110104 )
111105 fixed_air_pressure_coord = fixed_cl_cube .coord ("air_pressure" )
112106 assert fixed_air_pressure_coord .points is not None
@@ -121,80 +115,6 @@ def test_cl_fix_file(mock_get_filepath, tmp_path, test_data_path):
121115 )
122116
123117
124- @pytest .fixture
125- def cl_cubes ():
126- """``cl`` cube."""
127- time_coord = iris .coords .DimCoord (
128- [0.0 , 1.0 ],
129- var_name = "time" ,
130- standard_name = "time" ,
131- units = "days since 1850-01-01 00:00:00" ,
132- )
133- a_coord = iris .coords .AuxCoord (
134- [0.1 , 0.2 , 0.1 ],
135- bounds = [[0.0 , 0.15 ], [0.15 , 0.25 ], [0.25 , 0.0 ]],
136- var_name = "a" ,
137- units = "1" ,
138- )
139- b_coord = iris .coords .AuxCoord (
140- [0.9 , 0.3 , 0.1 ],
141- bounds = [[1.0 , 0.8 ], [0.8 , 0.25 ], [0.25 , 0.0 ]],
142- var_name = "b" ,
143- units = "1" ,
144- )
145- lev_coord = iris .coords .DimCoord (
146- [999.0 , 99.0 , 9.0 ],
147- var_name = "lev" ,
148- standard_name = "atmosphere_hybrid_sigma_pressure_coordinate" ,
149- units = "hPa" ,
150- attributes = {"positive" : "up" },
151- )
152- lat_coord = iris .coords .DimCoord (
153- [0.0 , 1.0 ],
154- var_name = "lat" ,
155- standard_name = "latitude" ,
156- units = "degrees" ,
157- )
158- lon_coord = iris .coords .DimCoord (
159- [0.0 , 1.0 ],
160- var_name = "lon" ,
161- standard_name = "longitude" ,
162- units = "degrees" ,
163- )
164- coord_specs = [
165- (time_coord , 0 ),
166- (lev_coord , 1 ),
167- (lat_coord , 2 ),
168- (lon_coord , 3 ),
169- ]
170- cube = iris .cube .Cube (
171- np .arange (2 * 3 * 2 * 2 ).reshape (2 , 3 , 2 , 2 ),
172- var_name = "cl" ,
173- standard_name = "cloud_area_fraction_in_atmosphere_layer" ,
174- units = "%" ,
175- dim_coords_and_dims = coord_specs ,
176- aux_coords_and_dims = [(a_coord , 1 ), (b_coord , 1 )],
177- )
178- return iris .cube .CubeList ([cube ])
179-
180-
181- def test_cl_fix_metadata (cl_cubes ):
182- """Test ``fix_metadata`` for ``cl``."""
183- vardef = get_var_info ("CMIP6" , "Amon" , "cl" )
184- fix = Cl (vardef )
185- out_cubes = fix .fix_metadata (cl_cubes )
186- out_cube = out_cubes .extract_cube (
187- "cloud_area_fraction_in_atmosphere_layer" ,
188- )
189- lev_coord = out_cube .coord (var_name = "lev" )
190- assert lev_coord .units == "1"
191- np .testing .assert_allclose (lev_coord .points , [1.0 , 0.5 , 0.2 ])
192- np .testing .assert_allclose (
193- lev_coord .bounds ,
194- [[1.0 , 0.95 ], [0.95 , 0.5 ], [0.5 , 0.0 ]],
195- )
196-
197-
198118def test_get_cli_fix ():
199119 """Test getting of fix."""
200120 fix = Fix .get_fixes ("CMIP6" , "CESM2" , "Amon" , "cli" )
0 commit comments