From 9ea32516e5759743866bf4a0b0cf5248c2ea7a6e Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Wed, 13 Jul 2022 11:14:07 +0200 Subject: [PATCH 01/15] Made ICON output UGRID-compliant (on-the-fly) --- esmvalcore/cmor/_fixes/icon/icon.py | 239 +++++++++++++++++++++++----- 1 file changed, 195 insertions(+), 44 deletions(-) diff --git a/esmvalcore/cmor/_fixes/icon/icon.py b/esmvalcore/cmor/_fixes/icon/icon.py index 05dfc84017..6cfe9869e4 100644 --- a/esmvalcore/cmor/_fixes/icon/icon.py +++ b/esmvalcore/cmor/_fixes/icon/icon.py @@ -11,6 +11,7 @@ from iris import NameConstraint from iris.coords import AuxCoord, DimCoord from iris.cube import CubeList +from iris.experimental.ugrid import Connectivity, Mesh from esmvalcore.iris_helpers import add_leading_dim_to_cube, date2num @@ -64,9 +65,9 @@ def fix_metadata(self, cubes): else: lon_idx = None - # Fix cell index for unstructured grid if necessary - if self._cell_index_needs_fixing(lat_idx, lon_idx): - self._fix_unstructured_cell_index(cube, lat_idx) + # Fix unstructured mesh of unstructured grid is present + if self._is_unstructured_grid(lat_idx, lon_idx): + self._fix_mesh(cube, lat_idx) # Fix scalar coordinates self._fix_scalar_coords(cube) @@ -76,8 +77,7 @@ def fix_metadata(self, cubes): return CubeList([cube]) - def _add_coord_from_grid_file(self, cube, coord_name, - target_coord_long_name): + def _add_coord_from_grid_file(self, cube, coord_name): """Add coordinate from grid file to cube. Note @@ -90,10 +90,8 @@ def _add_coord_from_grid_file(self, cube, coord_name, cube: iris.cube.Cube ICON data to which the coordinate from the grid file is added. coord_name: str - Name of the coordinate in the grid file. Must be one of - ``'grid_latitude'``, ``'grid_longitude'``. - target_coord_long_name: str - Long name that is assigned to the newly added coordinate. + Name of the coordinate to add from the grid file. Must be one of + ``'latitude'``, ``'longitude'``. Raises ------ @@ -103,35 +101,43 @@ def _add_coord_from_grid_file(self, cube, coord_name, coordinate. """ - allowed_coord_names = ('grid_latitude', 'grid_longitude') - if coord_name not in allowed_coord_names: + # The following dict maps from desired coordinate name in output file + # (dict keys) to coordinate name in grid file (dict values) + coord_names_mapping = { + 'latitude': 'grid_latitude', + 'longitude': 'grid_longitude', + } + if coord_name not in coord_names_mapping: raise ValueError( - f"coord_name must be one of {allowed_coord_names}, got " + f"coord_name must be one of {list(coord_names_mapping)}, got " f"'{coord_name}'") - horizontal_grid = self.get_horizontal_grid(cube) + coord_name_in_grid = coord_names_mapping[coord_name] - # Use 'cell_area' as dummy cube to extract coordinates + # Use 'cell_area' as dummy cube to extract desired coordinates # Note: it might be necessary to expand this when more coord_names are # supported + horizontal_grid = self.get_horizontal_grid(cube) grid_cube = horizontal_grid.extract_cube( NameConstraint(var_name='cell_area')) - coord = grid_cube.coord(coord_name) + coord = grid_cube.coord(coord_name_in_grid) - # Find index of horizontal coordinate (= single unnamed dimension) + # Find index of mesh dimension (= single unnamed dimension) n_unnamed_dimensions = cube.ndim - len(cube.dim_coords) if n_unnamed_dimensions != 1: raise ValueError( f"Cannot determine coordinate dimension for coordinate " - f"'{target_coord_long_name}', cube does not contain a single " - f"unnamed dimension:\n{cube}") + f"'{coord_name}', cube does not contain a single unnamed " + f"dimension:\n{cube}") coord_dims = () for idx in range(cube.ndim): if not cube.coords(dimensions=idx, dim_coords=True): coord_dims = (idx,) break + # Adapt coordinate names so that the coordinate can be referenced with + # 'cube.coord(coord_name)'; the exact name will be set at a later stage coord.standard_name = None - coord.long_name = target_coord_long_name + coord.long_name = coord_name cube.add_aux_coord(coord, coord_dims) def _add_time(self, cube, cubes): @@ -155,7 +161,7 @@ def _fix_lat(self, cube): # Add latitude coordinate if not already present if not cube.coords(lat_name): try: - self._add_coord_from_grid_file(cube, 'grid_latitude', lat_name) + self._add_coord_from_grid_file(cube, 'latitude') except Exception as exc: msg = "Failed to add missing latitude coordinate to cube" raise ValueError(msg) from exc @@ -176,8 +182,7 @@ def _fix_lon(self, cube): # Add longitude coordinate if not already present if not cube.coords(lon_name): try: - self._add_coord_from_grid_file( - cube, 'grid_longitude', lon_name) + self._add_coord_from_grid_file(cube, 'longitude') except Exception as exc: msg = "Failed to add missing longitude coordinate to cube" raise ValueError(msg) from exc @@ -247,6 +252,123 @@ def _fix_time(self, cube, cubes): return cube + def _get_mesh(self, cube): + """Create mesh from horizontal grid file. + + Note + ---- + This functions creates a new :class:`iris.experimental.ugrid.Mesh` from + the ``clat`` (already present in the cube), ``clon`` (already present + in the cube), ``vertex_index``, ``vertex_of_cell``, ``vlat``, and + ``vlon`` variables of the horizontal grid file. + + We do not use :func:`iris.experimental.ugrid.Mesh.from_coords` with the + existing latitude and longitude coordinates here because this would + produce lots of duplicated entries for the node coordinates. The reason + for this is that the node coordinates are constructed from the bounds; + since each node is contained 6 times in the bounds array (each node is + shared by 6 neighboring cells) the number of nodes is 6 times higher + with :func:`iris.experimental.ugrid.Mesh.from_coords` compared to using + the information already present in the horizontal grid file. + + """ + horizontal_grid = self.get_horizontal_grid(cube) + + # Extract connectivity (i.e., the mapping cell faces -> cell nodes) + # from the the horizontal grid file (in ICON jargon called + # 'vertex_of_cell'; since UGRID expects a different dimension ordering + # we transpose the cube here) + vertex_of_cell = horizontal_grid.extract_cube( + NameConstraint(var_name='vertex_of_cell')) + vertex_of_cell.transpose() + + # Extract start index used to name nodes from the the horizontal grid + # file + start_index = self._get_start_index(horizontal_grid) + + # Extract face coordinates from cube (in ICON jargon called 'cell + # latitude' and 'cell longitude') + face_lat = cube.coord('latitude') + face_lon = cube.coord('longitude') + + # Extract node coordinates from horizontal grid + (node_lat, node_lon) = self._get_node_coords(horizontal_grid) + + # The bounds given by the face coordinates slightly differ from the + # bounds determined by the connectivity. + # We arbitrarily assume here that the information given by the + # connectivity is correct + conn_node_inds = vertex_of_cell.data - start_index + + # Latitude: there might be slight numerical differences (-> check that + # the differences are very small before fixing it) + if not np.allclose(face_lat.bounds, node_lat.points[conn_node_inds]): + raise ValueError( + "Cannot create mesh from horizontal grid file: latitude " + "bounds of the face coordinate ('clat_vertices' in the grid " + "file) differ from the corresponding values calculated from " + "the connectivity ('vertex_of_cell') and the node coordinate " + "('vlat')") + face_lat.bounds = node_lat.points[conn_node_inds] + + # Longitude: there might be differences at the poles, where the + # longitude information does not matter (-> check that the only large + # differences are located at the poles). In addition, values might + # differ by 360°, which is also okay. + face_lon_bounds_to_check = face_lon.bounds % 360 + node_lon_conn_to_check = node_lon.points[conn_node_inds] % 360 + idx_notclose = ~np.isclose(face_lon_bounds_to_check, + node_lon_conn_to_check) + if not np.allclose(np.abs(face_lat.bounds[idx_notclose]), 90.0): + raise ValueError( + "Cannot create mesh from horizontal grid file: longitude " + "bounds of the face coordinate ('clon_vertices' in the grid " + "file) differ from the corresponding values calculated from " + "the connectivity ('vertex_of_cell') and the node coordinate " + "('vlon'). Note that these values are allowed to differ by " + "360° or at the poles of the grid.") + face_lon.bounds = node_lon.points[conn_node_inds] + + # Create mesh + connectivity = Connectivity( + indices=vertex_of_cell.data, + cf_role='face_node_connectivity', + start_index=start_index, + location_axis=0, + ) + mesh = Mesh( + topology_dimension=2, + node_coords_and_axes=[(node_lat, 'y'), (node_lon, 'x')], + connectivities=[connectivity], + face_coords_and_axes=[(face_lat, 'y'), (face_lon, 'x')], + ) + return mesh + + def _fix_mesh(self, cube, mesh_idx): + """Fix mesh.""" + # Remove any already-present dimensional coordinate describing the mesh + # dimension + if cube.coords(dimensions=mesh_idx, dim_coords=True): + cube.remove_coord(cube.coord(dimensions=mesh_idx, dim_coords=True)) + + # Add dimensional coordinate that describes the mesh dimension + index_coord = DimCoord( + np.arange(cube.shape[mesh_idx[0]]), + var_name='i', + long_name=('first spatial index for variables stored on an ' + 'unstructured grid'), + units='1', + ) + cube.add_dim_coord(index_coord, mesh_idx) + + # Create mesh and replace the original latitude and longitude + # coordinates with their new mesh versions + mesh = self._get_mesh(cube) + cube.remove_coord('latitude') + cube.remove_coord('longitude') + for mesh_coord in mesh.to_MeshCoords('face'): + cube.add_aux_coord(mesh_coord, mesh_idx) + def _fix_var_metadata(self, cube): """Fix metadata of variable.""" if self.vardef.standard_name == '': @@ -261,22 +383,66 @@ def _fix_var_metadata(self, cube): cube.attributes['positive'] = self.vardef.positive @staticmethod - def _cell_index_needs_fixing(lat_idx, lon_idx): - """Check if cell index coordinate of unstructured grid needs fixing.""" + def _get_start_index(horizontal_grid): + """Get start index used to name nodes from horizontal grid. + + Extract start index used to name nodes from the the horizontal grid + file (in ICON jargon called 'vertex_index'). + + Note + ---- + UGRID expects this to be a int32. + + """ + vertex_index = horizontal_grid.extract_cube( + NameConstraint(var_name='vertex_index')) + return np.int32(np.min(vertex_index.data)) + + @staticmethod + def _get_node_coords(horizontal_grid): + """Get node coordinates from horizontal grid. + + Extract node coordinates from dummy variable 'dual_area' in horizontal + grid file (in ICON jargon called 'vertex latitude' and 'vertex + longitude'), remove their bounds (not accepted by UGRID), and adapt + metadata. + + """ + dual_area_cube = horizontal_grid.extract_cube( + NameConstraint(var_name='dual_area')) + node_lat = dual_area_cube.coord(var_name='vlat') + node_lon = dual_area_cube.coord(var_name='vlon') + + node_lat.bounds = None + node_lon.bounds = None + node_lat.var_name = 'nlat' + node_lon.var_name = 'nlon' + node_lat.standard_name = 'latitude' + node_lon.standard_name = 'longitude' + node_lat.long_name = 'node latitude' + node_lon.long_name = 'node longitude' + node_lat.convert_units('degrees_north') + node_lon.convert_units('degrees_east') + + return (node_lat, node_lon) + + @staticmethod + def _is_unstructured_grid(lat_idx, lon_idx): + """Check if data is defined on an unstructured grid.""" # If either latitude or longitude are not present (i.e., the - # corresponding index is None), no fix is necessary + # corresponding index is None), no unstructured grid is present if lat_idx is None: return False if lon_idx is None: return False - # If latitude and longitude do not share their dimensions, no fix is - # necessary + # If latitude and longitude do not share their dimensions, no + # unstructured grid is present if lat_idx != lon_idx: return False - # If latitude and longitude are multi-dimensional (i.e., curvilinear - # instead of unstructured grid is given), no fix is necessary + # If latitude and longitude are multi-dimensional (e.g., curvilinear + # grid), no unstructured grid is present if len(lat_idx) != 1: return False @@ -349,21 +515,6 @@ def _fix_height(cube, cubes): return cube - @staticmethod - def _fix_unstructured_cell_index(cube, horizontal_idx): - """Fix unstructured cell index coordinate.""" - if cube.coords(dimensions=horizontal_idx, dim_coords=True): - cube.remove_coord(cube.coord(dimensions=horizontal_idx, - dim_coords=True)) - index_coord = DimCoord( - np.arange(cube.shape[horizontal_idx[0]]), - var_name='i', - long_name=('first spatial index for variables stored on an ' - 'unstructured grid'), - units='1', - ) - cube.add_dim_coord(index_coord, horizontal_idx) - class Siconc(IconFix): """Fixes for ``siconc``.""" From a0eee14296f70a7819cdde6a13db973ec16440ba Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Wed, 13 Jul 2022 16:26:33 +0200 Subject: [PATCH 02/15] Fixed existing tests --- esmvalcore/cmor/_fixes/icon/icon.py | 7 +- .../integration/cmor/_fixes/icon/test_icon.py | 155 +++++++----------- .../cmor/_fixes/test_data/icon_2d.nc | Bin 50901 -> 50901 bytes .../cmor/_fixes/test_data/icon_3d.nc | Bin 47656 -> 47656 bytes .../cmor/_fixes/test_data/icon_grid.nc | Bin 16176 -> 15466 bytes 5 files changed, 64 insertions(+), 98 deletions(-) diff --git a/esmvalcore/cmor/_fixes/icon/icon.py b/esmvalcore/cmor/_fixes/icon/icon.py index 6cfe9869e4..ac5e1819ed 100644 --- a/esmvalcore/cmor/_fixes/icon/icon.py +++ b/esmvalcore/cmor/_fixes/icon/icon.py @@ -65,7 +65,7 @@ def fix_metadata(self, cubes): else: lon_idx = None - # Fix unstructured mesh of unstructured grid is present + # Fix unstructured mesh of unstructured grid if present if self._is_unstructured_grid(lat_idx, lon_idx): self._fix_mesh(cube, lat_idx) @@ -295,9 +295,8 @@ def _get_mesh(self, cube): (node_lat, node_lon) = self._get_node_coords(horizontal_grid) # The bounds given by the face coordinates slightly differ from the - # bounds determined by the connectivity. - # We arbitrarily assume here that the information given by the - # connectivity is correct + # bounds determined by the connectivity. We arbitrarily assume here + # that the information given by the connectivity is correct. conn_node_inds = vertex_of_cell.data - start_index # Latitude: there might be slight numerical differences (-> check that diff --git a/tests/integration/cmor/_fixes/icon/test_icon.py b/tests/integration/cmor/_fixes/icon/test_icon.py index b34bbb9d47..61d15e1340 100644 --- a/tests/integration/cmor/_fixes/icon/test_icon.py +++ b/tests/integration/cmor/_fixes/icon/test_icon.py @@ -14,6 +14,14 @@ from esmvalcore.cmor.fix import Fix from esmvalcore.cmor.table import get_var_info + +# TODO: ADAPT ONCE THE FILE IS AVAILABLE ON GITHUB +TEST_GRID_FILE_URI = ( + 'https://seafile.zfn.uni-bremen.de/f/f66b000792434878bcbf/?dl=1' +) +TEST_GRID_FILE_NAME = 'f66b000792434878bcbf' + + # Note: test_data_path is defined in tests/integration/cmor/_fixes/conftest.py @@ -181,13 +189,14 @@ def check_heightxm(cube, height_value): assert height.bounds is None +# TODO: uncomment checks def check_lat(cube): """Check latitude coordinate of cube.""" assert cube.coords('latitude', dim_coords=False) lat = cube.coord('latitude', dim_coords=False) - assert lat.var_name == 'lat' + # assert lat.var_name == 'lat' assert lat.standard_name == 'latitude' - assert lat.long_name == 'latitude' + # assert lat.long_name == 'latitude' assert lat.units == 'degrees_north' np.testing.assert_allclose( lat.points, @@ -211,13 +220,14 @@ def check_lat(cube): return lat +# TODO: uncomment checks def check_lon(cube): """Check longitude coordinate of cube.""" assert cube.coords('longitude', dim_coords=False) lon = cube.coord('longitude', dim_coords=False) - assert lon.var_name == 'lon' + # assert lon.var_name == 'lon' assert lon.standard_name == 'longitude' - assert lon.long_name == 'longitude' + # assert lon.long_name == 'longitude' assert lon.units == 'degrees_east' np.testing.assert_allclose( lon.points, @@ -227,14 +237,14 @@ def check_lon(cube): np.testing.assert_allclose( lon.bounds, [ - [-135.0, -90.0, -180.0], - [-45.0, 0.0, -90.0], - [45.0, 90.0, 0.0], - [135.0, 180.0, 90.0], - [-180.0, -90.0, -135.0], - [-90.0, 0.0, -45.0], - [0.0, 90.0, 45.0], - [90.0, 180.0, 135.0], + [0.0, -90.0, -180.0], + [0.0, 0.0, -90.0], + [0.0, 90.0, 0.0], + [0.0, -180.0, 90.0], + [-180.0, -90.0, 0.0], + [-90.0, 0.0, 0.0], + [0.0, 90.0, 0.0], + [90.0, -180.0, 0.0], ], rtol=1e-5 ) @@ -721,21 +731,16 @@ def test_add_time_fail(): fix._add_time(cube, cubes) -def test_add_latitude(cubes_2d, tmp_path): +def test_add_latitude(cubes_2d, tmp_path, monkeypatch): """Test fix.""" # Remove latitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) tas_cube.remove_coord('latitude') - tas_cube.attributes['grid_file_uri'] = ( - 'https://github.com/ESMValGroup/ESMValCore/raw/main/tests/' - 'integration/cmor/_fixes/test_data/icon_grid.nc' - ) cubes = CubeList([tas_cube]) fix = get_allvars_fix('Amon', 'tas') # Temporary overwrite default cache location for downloads - original_cache_dir = fix.CACHE_DIR - fix.CACHE_DIR = tmp_path + monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) assert len(fix._horizontal_grids) == 0 fixed_cubes = fix.fix_metadata(cubes) @@ -744,27 +749,19 @@ def test_add_latitude(cubes_2d, tmp_path): assert cube.shape == (1, 8) check_lat_lon(cube) assert len(fix._horizontal_grids) == 1 - assert 'icon_grid.nc' in fix._horizontal_grids + assert TEST_GRID_FILE_NAME in fix._horizontal_grids - # Restore cache location - fix.CACHE_DIR = original_cache_dir - -def test_add_longitude(cubes_2d, tmp_path): +def test_add_longitude(cubes_2d, tmp_path, monkeypatch): """Test fix.""" # Remove longitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) tas_cube.remove_coord('longitude') - tas_cube.attributes['grid_file_uri'] = ( - 'https://github.com/ESMValGroup/ESMValCore/raw/main/tests/' - 'integration/cmor/_fixes/test_data/icon_grid.nc' - ) cubes = CubeList([tas_cube]) fix = get_allvars_fix('Amon', 'tas') # Temporary overwrite default cache location for downloads - original_cache_dir = fix.CACHE_DIR - fix.CACHE_DIR = tmp_path + monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) assert len(fix._horizontal_grids) == 0 fixed_cubes = fix.fix_metadata(cubes) @@ -773,28 +770,20 @@ def test_add_longitude(cubes_2d, tmp_path): assert cube.shape == (1, 8) check_lat_lon(cube) assert len(fix._horizontal_grids) == 1 - assert 'icon_grid.nc' in fix._horizontal_grids - - # Restore cache location - fix.CACHE_DIR = original_cache_dir + assert TEST_GRID_FILE_NAME in fix._horizontal_grids -def test_add_latitude_longitude(cubes_2d, tmp_path): +def test_add_latitude_longitude(cubes_2d, tmp_path, monkeypatch): """Test fix.""" # Remove latitude and longitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) tas_cube.remove_coord('latitude') tas_cube.remove_coord('longitude') - tas_cube.attributes['grid_file_uri'] = ( - 'https://github.com/ESMValGroup/ESMValCore/raw/main/tests/' - 'integration/cmor/_fixes/test_data/icon_grid.nc' - ) cubes = CubeList([tas_cube]) fix = get_allvars_fix('Amon', 'tas') # Temporary overwrite default cache location for downloads - original_cache_dir = fix.CACHE_DIR - fix.CACHE_DIR = tmp_path + monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) assert len(fix._horizontal_grids) == 0 fixed_cubes = fix.fix_metadata(cubes) @@ -803,17 +792,16 @@ def test_add_latitude_longitude(cubes_2d, tmp_path): assert cube.shape == (1, 8) check_lat_lon(cube) assert len(fix._horizontal_grids) == 1 - assert 'icon_grid.nc' in fix._horizontal_grids - - # Restore cache location - fix.CACHE_DIR = original_cache_dir + assert TEST_GRID_FILE_NAME in fix._horizontal_grids def test_add_latitude_fail(cubes_2d): """Test fix.""" - # Remove latitude from tas cube to test automatic addition + # Remove latitude and grid file attribute from tas cube to test automatic + # addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) tas_cube.remove_coord('latitude') + tas_cube.attributes = {} cubes = CubeList([tas_cube]) fix = get_allvars_fix('Amon', 'tas') @@ -824,9 +812,11 @@ def test_add_latitude_fail(cubes_2d): def test_add_longitude_fail(cubes_2d): """Test fix.""" - # Remove longitude from tas cube to test automatic addition + # Remove longitude and grid file attribute from tas cube to test automatic + # addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) tas_cube.remove_coord('longitude') + tas_cube.attributes = {} cubes = CubeList([tas_cube]) fix = get_allvars_fix('Amon', 'tas') @@ -841,8 +831,7 @@ def test_add_coord_from_grid_file_fail_invalid_coord(): msg = r"coord_name must be one of .* got 'invalid_coord_name'" with pytest.raises(ValueError, match=msg): - fix._add_coord_from_grid_file(mock.sentinel.cube, 'invalid_coord_name', - 'invalid_target_name') + fix._add_coord_from_grid_file(mock.sentinel.cube, 'invalid_coord_name') def test_add_coord_from_grid_file_fail_no_url(): @@ -852,58 +841,44 @@ def test_add_coord_from_grid_file_fail_no_url(): msg = ("Cube does not contain the attribute 'grid_file_uri' necessary to " "download the ICON horizontal grid file") with pytest.raises(ValueError, match=msg): - fix._add_coord_from_grid_file(Cube(0), 'grid_latitude', 'latitude') + fix._add_coord_from_grid_file(Cube(0), 'latitude') -def test_add_coord_from_grid_fail_no_unnamed_dim(cubes_2d, tmp_path): +def test_add_coord_from_grid_fail_no_unnamed_dim(cubes_2d, tmp_path, + monkeypatch): """Test fix.""" # Remove latitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) tas_cube.remove_coord('latitude') - tas_cube.attributes['grid_file_uri'] = ( - 'https://github.com/ESMValGroup/ESMValCore/raw/main/tests/' - 'integration/cmor/_fixes/test_data/icon_grid.nc' - ) index_coord = DimCoord(np.arange(8), var_name='ncells') tas_cube.add_dim_coord(index_coord, 1) fix = get_allvars_fix('Amon', 'tas') # Temporary overwrite default cache location for downloads - original_cache_dir = fix.CACHE_DIR - fix.CACHE_DIR = tmp_path + monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) msg = ("Cannot determine coordinate dimension for coordinate 'latitude', " "cube does not contain a single unnamed dimension") with pytest.raises(ValueError, match=msg): - fix._add_coord_from_grid_file(tas_cube, 'grid_latitude', 'latitude') + fix._add_coord_from_grid_file(tas_cube, 'latitude') - # Restore cache location - fix.CACHE_DIR = original_cache_dir - -def test_add_coord_from_grid_fail_two_unnamed_dims(cubes_2d, tmp_path): +def test_add_coord_from_grid_fail_two_unnamed_dims(cubes_2d, tmp_path, + monkeypatch): """Test fix.""" # Remove latitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) tas_cube.remove_coord('latitude') - tas_cube.attributes['grid_file_uri'] = ( - 'https://github.com/ESMValGroup/ESMValCore/raw/main/tests/' - 'integration/cmor/_fixes/test_data/icon_grid.nc' - ) tas_cube = iris.util.new_axis(tas_cube) fix = get_allvars_fix('Amon', 'tas') # Temporary overwrite default cache location for downloads - original_cache_dir = fix.CACHE_DIR - fix.CACHE_DIR = tmp_path + monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) msg = ("Cannot determine coordinate dimension for coordinate 'latitude', " "cube does not contain a single unnamed dimension") with pytest.raises(ValueError, match=msg): - fix._add_coord_from_grid_file(tas_cube, 'grid_latitude', 'latitude') - - # Restore cache location - fix.CACHE_DIR = original_cache_dir + fix._add_coord_from_grid_file(tas_cube, 'latitude') @mock.patch('esmvalcore.cmor._fixes.icon._base_fixes.requests', autospec=True) @@ -919,7 +894,8 @@ def test_get_horizontal_grid_cached_in_dict(mock_requests): @mock.patch('esmvalcore.cmor._fixes.icon._base_fixes.requests', autospec=True) -def test_get_horizontal_grid_cached_in_file(mock_requests, tmp_path): +def test_get_horizontal_grid_cached_in_file(mock_requests, tmp_path, + monkeypatch): """Test fix.""" cube = Cube(0, attributes={ 'grid_file_uri': 'https://temporary.url/this/is/the/grid_file.nc'}) @@ -931,8 +907,7 @@ def test_get_horizontal_grid_cached_in_file(mock_requests, tmp_path): iris.save(grid_cube, str(tmp_path / 'grid_file.nc')) # Temporary overwrite default cache location for downloads - original_cache_dir = fix.CACHE_DIR - fix.CACHE_DIR = tmp_path + monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) grid = fix.get_horizontal_grid(cube) assert isinstance(grid, CubeList) @@ -942,16 +917,10 @@ def test_get_horizontal_grid_cached_in_file(mock_requests, tmp_path): assert 'grid_file.nc' in fix._horizontal_grids assert mock_requests.mock_calls == [] - # Restore cache location - fix.CACHE_DIR = original_cache_dir - -def test_get_horizontal_grid_cache_file_too_old(tmp_path): +def test_get_horizontal_grid_cache_file_too_old(tmp_path, monkeypatch): """Test fix.""" - cube = Cube(0, attributes={ - 'grid_file_uri': 'https://github.com/ESMValGroup/ESMValCore/raw/main/' - 'tests/integration/cmor/_fixes/test_data/' - 'icon_grid.nc'}) + cube = Cube(0, attributes={'grid_file_uri': TEST_GRID_FILE_URI}) fix = get_allvars_fix('Amon', 'tas') assert len(fix._horizontal_grids) == 0 @@ -961,21 +930,19 @@ def test_get_horizontal_grid_cache_file_too_old(tmp_path): # Temporary overwrite default cache location for downloads and cache # validity duration - original_cache_dir = fix.CACHE_DIR - original_cache_validity = fix.CACHE_VALIDITY - fix.CACHE_DIR = tmp_path - fix.CACHE_VALIDITY = -1 + monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) + monkeypatch.setattr(fix, 'CACHE_VALIDITY', -1) grid = fix.get_horizontal_grid(cube) assert isinstance(grid, CubeList) - assert len(grid) == 1 - assert grid[0].var_name == 'cell_area' + assert len(grid) == 4 + var_names = [cube.var_name for cube in grid] + assert 'cell_area' in var_names + assert 'dual_area' in var_names + assert 'vertex_index' in var_names + assert 'vertex_of_cell' in var_names assert len(fix._horizontal_grids) == 1 - assert 'icon_grid.nc' in fix._horizontal_grids - - # Restore cache location - fix.CACHE_DIR = original_cache_dir - fix.CACHE_VALIDITY = original_cache_validity + assert TEST_GRID_FILE_NAME in fix._horizontal_grids # Test with single-dimension cubes diff --git a/tests/integration/cmor/_fixes/test_data/icon_2d.nc b/tests/integration/cmor/_fixes/test_data/icon_2d.nc index b6b7d66fe7d23de4455132f3c4a9f05b191f35ab..f650492ebd80c1a3b2292ebeafc87f277e773ba1 100644 GIT binary patch delta 178 zcmccG%Y3z$c|txT)5L-lX#oKSAYiQokqiv^5Q>4}Z^!GDjqjx@>Uk0w7#JCt8Tc4D z7+4q>(u*=v;?pv7QsYaDG8u${%I$!d35YXFN(zdt^!1BV6G8HNRcU#8rFogUNkyr- zsd;)SsrqU9X=Y|g1_lP^mPRJVCKl!vNy$lR`t~U~wuXt-?~9>!vqR}UK}uUd#Q@a`x7Nu U&M+`=PQH`q%gDaDq%zSL0FVC_3jhEB diff --git a/tests/integration/cmor/_fixes/test_data/icon_3d.nc b/tests/integration/cmor/_fixes/test_data/icon_3d.nc index 4c4e5fd9117c2c2d505b192c1b085cfed1b8fae2..a3f03badffc14057ab3cfa538bf7e0dc09835967 100644 GIT binary patch delta 259 zcmZ4Sg=xhXrV06sOcM)Iqy+>RfFLLzL^3cyDFz1SE0gwbd|%}@S>HySlNBP#z&d&5 z6wS?>+!v@%wpU@zN@QSQWMF3CW8h$5VPHrv%1nt*%gjlQFD=Ss5C&?o17ap1&L}A< zD7MnqFHTJa$?H|6<>{5?W$GpsrRJvQ>7}IVr|GAenI#z*7?@icnHZZ`m|G+zC#C7z zr{vfgCRV>M-h43s8{6bOcg4w@%G4(JyDM&%FISP_+EfN{*p0Hu{hSJub0{^Cz1|vleCQEJb6 diff --git a/tests/integration/cmor/_fixes/test_data/icon_grid.nc b/tests/integration/cmor/_fixes/test_data/icon_grid.nc index 882f63cedcae9c2d97d8ae0ad4c422d2bbcdf4bd..0e4a0634eb4f2596f7d27030e3ddb3f9ca82a50a 100644 GIT binary patch literal 15466 zcmeGj3yf4(^}LzcFOOxRd<3@kEnvYdo&B;bwA=m8u$wNsz+i(=JG|L>%g$zbv&_s= zsMS_f0#>Cl)h5MMO|-!nwe}N1+k%^t8Vj++ruEYbuC*3zC23;~LcnwGz31&P`-cHb zGABEG-u<0>&bjBFd*40x?QLoHPtBi~@5;>uhKmY%ZkIV_s$lNfWivWjn%BAXXIu+J zo_ne)`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;OlZ4>PLCXU&0xzPXiS)V?=85XPc>0vBOA%N1H zap9rmjT)qdO;06slBiBgU0~%5+aZ;v|D(ac9kZwj^_H0fbbe#`E=&;Nc;ysiYv3uR zfiJ0i+Kj2s>W2X{ZN|-SWR7OD*}qQKcgW_%>eG*@hW|gZy=Ay*@^_w>{(OFF=>6)q z&nVig(pzTB&#A}tb7?d4&85tLA3XN&_p0AL^Wf>99(cdH)b0Gxe4YgX^ Date: Wed, 13 Jul 2022 17:56:14 +0200 Subject: [PATCH 03/15] Added tests for mesh --- .../integration/cmor/_fixes/icon/test_icon.py | 170 +++++++++++++++++- 1 file changed, 167 insertions(+), 3 deletions(-) diff --git a/tests/integration/cmor/_fixes/icon/test_icon.py b/tests/integration/cmor/_fixes/icon/test_icon.py index 61d15e1340..7a04997d5d 100644 --- a/tests/integration/cmor/_fixes/icon/test_icon.py +++ b/tests/integration/cmor/_fixes/icon/test_icon.py @@ -14,7 +14,6 @@ from esmvalcore.cmor.fix import Fix from esmvalcore.cmor.table import get_var_info - # TODO: ADAPT ONCE THE FILE IS AVAILABLE ON GITHUB TEST_GRID_FILE_URI = ( 'https://seafile.zfn.uni-bremen.de/f/f66b000792434878bcbf/?dl=1' @@ -198,6 +197,7 @@ def check_lat(cube): assert lat.standard_name == 'latitude' # assert lat.long_name == 'latitude' assert lat.units == 'degrees_north' + assert lat.attributes == {} np.testing.assert_allclose( lat.points, [-45.0, -45.0, -45.0, -45.0, 45.0, 45.0, 45.0, 45.0], @@ -229,6 +229,7 @@ def check_lon(cube): assert lon.standard_name == 'longitude' # assert lon.long_name == 'longitude' assert lon.units == 'degrees_east' + assert lon.attributes == {} np.testing.assert_allclose( lon.points, [-135.0, -45.0, 45.0, 135.0, -135.0, -45.0, 45.0, 135.0], @@ -252,11 +253,15 @@ def check_lon(cube): def check_lat_lon(cube): - """Check latitude, longitude and spatial index coordinates of cube.""" + """Check latitude, longitude and mesh of cube.""" lat = check_lat(cube) lon = check_lon(cube) - # Check spatial index coordinate + # Check that latitude and longitude are mesh coordinates + assert cube.coords('latitude', mesh_coords=True) + assert cube.coords('longitude', mesh_coords=True) + + # Check dimensional coordinate describing the mesh assert cube.coords('first spatial index for variables stored on an ' 'unstructured grid', dim_coords=True) i_coord = cube.coord('first spatial index for variables stored on an ' @@ -273,6 +278,130 @@ def check_lat_lon(cube): assert cube.coord_dims(lat) == cube.coord_dims(lon) assert cube.coord_dims(lat) == cube.coord_dims(i_coord) + # Check the mesh itself + mesh = cube.mesh + check_mesh(mesh) + + +def check_mesh(mesh): + """Check the mesh.""" + assert mesh is not None + assert mesh.var_name is None + assert mesh.standard_name is None + assert mesh.long_name is None + assert mesh.units == 'unknown' + assert mesh.attributes == {} + assert mesh.cf_role == 'mesh_topology' + assert mesh.topology_dimension == 2 + + # Check face coordinates + assert len(mesh.coords(include_faces=True)) == 2 + + mesh_face_lat = mesh.coord(include_faces=True, axis='y') + assert mesh_face_lat.var_name == 'lat' + assert mesh_face_lat.standard_name == 'latitude' + assert mesh_face_lat.long_name == 'latitude' + assert mesh_face_lat.units == 'degrees_north' + assert mesh_face_lat.attributes == {} + np.testing.assert_allclose( + mesh_face_lat.points, + [-45.0, -45.0, -45.0, -45.0, 45.0, 45.0, 45.0, 45.0], + rtol=1e-5 + ) + np.testing.assert_allclose( + mesh_face_lat.bounds, + [ + [-90.0, 0.0, 0.0], + [-90.0, 0.0, 0.0], + [-90.0, 0.0, 0.0], + [-90.0, 0.0, 0.0], + [0.0, 0.0, 90.0], + [0.0, 0.0, 90.0], + [0.0, 0.0, 90.0], + [0.0, 0.0, 90.0], + ], + rtol=1e-5 + ) + + mesh_face_lon = mesh.coord(include_faces=True, axis='x') + assert mesh_face_lon.var_name == 'lon' + assert mesh_face_lon.standard_name == 'longitude' + assert mesh_face_lon.long_name == 'longitude' + assert mesh_face_lon.units == 'degrees_east' + assert mesh_face_lon.attributes == {} + np.testing.assert_allclose( + mesh_face_lon.points, + [-135.0, -45.0, 45.0, 135.0, -135.0, -45.0, 45.0, 135.0], + rtol=1e-5 + ) + np.testing.assert_allclose( + mesh_face_lon.bounds, + [ + [0.0, -90.0, -180.0], + [0.0, 0.0, -90.0], + [0.0, 90.0, 0.0], + [0.0, -180.0, 90.0], + [-180.0, -90.0, 0.0], + [-90.0, 0.0, 0.0], + [0.0, 90.0, 0.0], + [90.0, -180.0, 0.0], + ], + rtol=1e-5 + ) + + # Check node coordinates + assert len(mesh.coords(include_nodes=True)) == 2 + + mesh_node_lat = mesh.coord(include_nodes=True, axis='y') + assert mesh_node_lat.var_name == 'nlat' + assert mesh_node_lat.standard_name == 'latitude' + assert mesh_node_lat.long_name == 'node latitude' + assert mesh_node_lat.units == 'degrees_north' + assert mesh_node_lat.attributes == {} + np.testing.assert_allclose( + mesh_node_lat.points, + [-90.0, 0.0, 0.0, 0.0, 0.0, 90.0], + rtol=1e-5 + ) + assert mesh_node_lat.bounds is None + + mesh_node_lon = mesh.coord(include_nodes=True, axis='x') + assert mesh_node_lon.var_name == 'nlon' + assert mesh_node_lon.standard_name == 'longitude' + assert mesh_node_lon.long_name == 'node longitude' + assert mesh_node_lon.units == 'degrees_east' + assert mesh_node_lon.attributes == {} + np.testing.assert_allclose( + mesh_node_lon.points, + [0.0, -180.0, -90.0, 0.0, 90, 0.0], + rtol=1e-5 + ) + assert mesh_node_lon.bounds is None + + # Check connectivity + assert len(mesh.connectivities()) == 1 + conn = mesh.connectivity() + assert conn.var_name is None + assert conn.standard_name is None + assert conn.long_name is None + assert conn.units == 'unknown' + assert conn.attributes == {} + assert conn.cf_role == 'face_node_connectivity' + assert conn.start_index == 1 + assert conn.location_axis == 0 + assert conn.shape == (8, 3) + np.testing.assert_array_equal( + conn.indices, + [[1, 3, 2], + [1, 4, 3], + [1, 5, 4], + [1, 2, 5], + [2, 3, 6], + [3, 4, 6], + [4, 5, 6], + [5, 2, 6]], + ) + def check_typesi(cube): """Check scalar typesi coordinate of cube.""" @@ -1156,3 +1285,38 @@ def test_invalid_time_units(cubes_2d): msg = "Expected time units" with pytest.raises(ValueError, match=msg): fix.fix_metadata(cubes_2d) + + +# Test mesh creation fails because bounds do not match vertices + + +def test_get_mesh_fail_invalid_clat_bounds(cubes_2d): + """Test fix.""" + # Slightly modify latitude bounds from tas cube to make mesh creation fail + tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) + lat_bnds = tas_cube.coord('latitude').bounds.copy() + lat_bnds[0, 0] = 40.0 + tas_cube.coord('latitude').bounds = lat_bnds + cubes = CubeList([tas_cube]) + fix = get_allvars_fix('Amon', 'tas') + + msg = ("Cannot create mesh from horizontal grid file: latitude bounds of " + "the face coordinate") + with pytest.raises(ValueError, match=msg): + fix.fix_metadata(cubes) + + +def test_get_mesh_fail_invalid_clon_bounds(cubes_2d): + """Test fix.""" + # Slightly modify longitude bounds from tas cube to make mesh creation fail + tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) + lon_bnds = tas_cube.coord('longitude').bounds.copy() + lon_bnds[0, 1] = 40.0 + tas_cube.coord('longitude').bounds = lon_bnds + cubes = CubeList([tas_cube]) + fix = get_allvars_fix('Amon', 'tas') + + msg = ("Cannot create mesh from horizontal grid file: longitude bounds " + "of the face coordinate") + with pytest.raises(ValueError, match=msg): + fix.fix_metadata(cubes) From f931a5b64e6d212205f05e246ad436f0debe6bc0 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Wed, 13 Jul 2022 18:08:48 +0200 Subject: [PATCH 04/15] Added test for the cube's location --- tests/integration/cmor/_fixes/icon/test_icon.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration/cmor/_fixes/icon/test_icon.py b/tests/integration/cmor/_fixes/icon/test_icon.py index 7a04997d5d..e47c9535be 100644 --- a/tests/integration/cmor/_fixes/icon/test_icon.py +++ b/tests/integration/cmor/_fixes/icon/test_icon.py @@ -279,6 +279,7 @@ def check_lat_lon(cube): assert cube.coord_dims(lat) == cube.coord_dims(i_coord) # Check the mesh itself + assert cube.location == 'face' mesh = cube.mesh check_mesh(mesh) From 4ef5bf0e1fbd28dc4eb7995365c17766e7d5e94f Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Thu, 20 Oct 2022 18:17:14 +0200 Subject: [PATCH 05/15] Make sure ICON lon is in [0, 360] --- esmvalcore/cmor/_fixes/icon/icon.py | 69 +++++++++++++++++------------ 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/esmvalcore/cmor/_fixes/icon/icon.py b/esmvalcore/cmor/_fixes/icon/icon.py index c26778dbf7..f2f55ab519 100644 --- a/esmvalcore/cmor/_fixes/icon/icon.py +++ b/esmvalcore/cmor/_fixes/icon/icon.py @@ -238,8 +238,9 @@ def _fix_lon(self, cube): msg = "Failed to add missing longitude coordinate to cube" raise ValueError(msg) from exc - # Fix metadata + # Fix metadata and convert to [0, 360] lon = self.fix_lon_metadata(cube, lon_name) + self._set_range_in_0_360(lon) return cube.coord_dims(lon) @@ -370,6 +371,37 @@ def _get_mesh(self, cube): ) return mesh + def _get_node_coords(self, horizontal_grid): + """Get node coordinates from horizontal grid. + + Extract node coordinates from dummy variable 'dual_area' in horizontal + grid file (in ICON jargon called 'vertex latitude' and 'vertex + longitude'), remove their bounds (not accepted by UGRID), and adapt + metadata. + + """ + dual_area_cube = horizontal_grid.extract_cube( + NameConstraint(var_name='dual_area')) + node_lat = dual_area_cube.coord(var_name='vlat') + node_lon = dual_area_cube.coord(var_name='vlon') + + # Fix metadata + node_lat.bounds = None + node_lon.bounds = None + node_lat.var_name = 'nlat' + node_lon.var_name = 'nlon' + node_lat.standard_name = 'latitude' + node_lon.standard_name = 'longitude' + node_lat.long_name = 'node latitude' + node_lon.long_name = 'node longitude' + node_lat.convert_units('degrees_north') + node_lon.convert_units('degrees_east') + + # Convert longitude to [0, 360] + self._set_range_in_0_360(node_lon) + + return (node_lat, node_lon) + def _fix_mesh(self, cube, mesh_idx): """Fix mesh.""" # Remove any already-present dimensional coordinate describing the mesh @@ -424,34 +456,6 @@ def _get_start_index(horizontal_grid): NameConstraint(var_name='vertex_index')) return np.int32(np.min(vertex_index.data)) - @staticmethod - def _get_node_coords(horizontal_grid): - """Get node coordinates from horizontal grid. - - Extract node coordinates from dummy variable 'dual_area' in horizontal - grid file (in ICON jargon called 'vertex latitude' and 'vertex - longitude'), remove their bounds (not accepted by UGRID), and adapt - metadata. - - """ - dual_area_cube = horizontal_grid.extract_cube( - NameConstraint(var_name='dual_area')) - node_lat = dual_area_cube.coord(var_name='vlat') - node_lon = dual_area_cube.coord(var_name='vlon') - - node_lat.bounds = None - node_lon.bounds = None - node_lat.var_name = 'nlat' - node_lon.var_name = 'nlon' - node_lat.standard_name = 'latitude' - node_lon.standard_name = 'longitude' - node_lat.long_name = 'node latitude' - node_lon.long_name = 'node longitude' - node_lat.convert_units('degrees_north') - node_lon.convert_units('degrees_east') - - return (node_lat, node_lon) - @staticmethod def _is_unstructured_grid(lat_idx, lon_idx): """Check if data is defined on an unstructured grid.""" @@ -474,6 +478,13 @@ def _is_unstructured_grid(lat_idx, lon_idx): return True + @staticmethod + def _set_range_in_0_360(lon_coord): + """Convert longitude coordinate to [0, 360].""" + lon_coord.points = (lon_coord.points + 360.0) % 360.0 + if lon_coord.bounds is not None: + lon_coord.bounds = (lon_coord.bounds + 360.0) % 360.0 + Hur = SetUnitsTo1 From a731521505c56e8256f836ca009d21a4e1abe421 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Wed, 30 Nov 2022 16:53:33 +0100 Subject: [PATCH 06/15] Fixed tests --- esmvalcore/cmor/_fixes/icon/icon.py | 13 -------- .../integration/cmor/_fixes/icon/test_icon.py | 30 +++++++++---------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/esmvalcore/cmor/_fixes/icon/icon.py b/esmvalcore/cmor/_fixes/icon/icon.py index f2f55ab519..e4c76e6fd0 100644 --- a/esmvalcore/cmor/_fixes/icon/icon.py +++ b/esmvalcore/cmor/_fixes/icon/icon.py @@ -427,19 +427,6 @@ def _fix_mesh(self, cube, mesh_idx): for mesh_coord in mesh.to_MeshCoords('face'): cube.add_aux_coord(mesh_coord, mesh_idx) - def _fix_var_metadata(self, cube): - """Fix metadata of variable.""" - if self.vardef.standard_name == '': - cube.standard_name = None - else: - cube.standard_name = self.vardef.standard_name - cube.var_name = self.vardef.short_name - cube.long_name = self.vardef.long_name - if cube.units != self.vardef.units: - cube.convert_units(self.vardef.units) - if self.vardef.positive != '': - cube.attributes['positive'] = self.vardef.positive - @staticmethod def _get_start_index(horizontal_grid): """Get start index used to name nodes from horizontal grid. diff --git a/tests/integration/cmor/_fixes/icon/test_icon.py b/tests/integration/cmor/_fixes/icon/test_icon.py index 594da8887e..384bb16068 100644 --- a/tests/integration/cmor/_fixes/icon/test_icon.py +++ b/tests/integration/cmor/_fixes/icon/test_icon.py @@ -232,20 +232,20 @@ def check_lon(cube): assert lon.attributes == {} np.testing.assert_allclose( lon.points, - [-135.0, -45.0, 45.0, 135.0, -135.0, -45.0, 45.0, 135.0], + [225.0, 315.0, 45.0, 135.0, 225.0, 315.0, 45.0, 135.0], rtol=1e-5 ) np.testing.assert_allclose( lon.bounds, [ - [0.0, -90.0, -180.0], - [0.0, 0.0, -90.0], + [0.0, 270.0, 180.0], + [0.0, 0.0, 270.0], [0.0, 90.0, 0.0], - [0.0, -180.0, 90.0], - [-180.0, -90.0, 0.0], - [-90.0, 0.0, 0.0], + [0.0, 180.0, 90.0], + [180.0, 270.0, 0.0], + [270.0, 0.0, 0.0], [0.0, 90.0, 0.0], - [90.0, -180.0, 0.0], + [90.0, 180.0, 0.0], ], rtol=1e-5 ) @@ -332,20 +332,20 @@ def check_mesh(mesh): assert mesh_face_lon.attributes == {} np.testing.assert_allclose( mesh_face_lon.points, - [-135.0, -45.0, 45.0, 135.0, -135.0, -45.0, 45.0, 135.0], + [225.0, 315.0, 45.0, 135.0, 225.0, 315.0, 45.0, 135.0], rtol=1e-5 ) np.testing.assert_allclose( mesh_face_lon.bounds, [ - [0.0, -90.0, -180.0], - [0.0, 0.0, -90.0], + [0.0, 270.0, 180.0], + [0.0, 0.0, 270.0], [0.0, 90.0, 0.0], - [0.0, -180.0, 90.0], - [-180.0, -90.0, 0.0], - [-90.0, 0.0, 0.0], + [0.0, 180.0, 90.0], + [180.0, 270.0, 0.0], + [270.0, 0.0, 0.0], [0.0, 90.0, 0.0], - [90.0, -180.0, 0.0], + [90.0, 180.0, 0.0], ], rtol=1e-5 ) @@ -374,7 +374,7 @@ def check_mesh(mesh): assert mesh_node_lon.attributes == {} np.testing.assert_allclose( mesh_node_lon.points, - [0.0, -180.0, -90.0, 0.0, 90, 0.0], + [0.0, 180.0, 270.0, 0.0, 90, 0.0], rtol=1e-5 ) assert mesh_node_lon.bounds is None From 8731faa5ef6a992a290ab60579d525eaeb6da370 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Thu, 1 Dec 2022 12:18:08 +0100 Subject: [PATCH 07/15] Fixed tests --- .../integration/cmor/_fixes/icon/test_icon.py | 77 +++++++------------ 1 file changed, 29 insertions(+), 48 deletions(-) diff --git a/tests/integration/cmor/_fixes/icon/test_icon.py b/tests/integration/cmor/_fixes/icon/test_icon.py index 384bb16068..d7c898f867 100644 --- a/tests/integration/cmor/_fixes/icon/test_icon.py +++ b/tests/integration/cmor/_fixes/icon/test_icon.py @@ -9,6 +9,7 @@ from iris.coords import AuxCoord, DimCoord from iris.cube import Cube, CubeList +from esmvalcore.cmor._fixes.icon._base_fixes import IconFix from esmvalcore.cmor._fixes.icon.icon import AllVars, Siconc, Siconca from esmvalcore.cmor.fix import Fix from esmvalcore.cmor.table import get_var_info @@ -21,6 +22,12 @@ TEST_GRID_FILE_NAME = 'f66b000792434878bcbf' +@pytest.fixture(autouse=True) +def tmp_cache_dir(monkeypatch, tmp_path): + """Always use temporary path as cache directory.""" + monkeypatch.setattr(IconFix, 'CACHE_DIR', tmp_path) + + # Note: test_data_path is defined in tests/integration/cmor/_fixes/conftest.py @@ -861,7 +868,7 @@ def test_add_time_fail(): fix._add_time(cube, cubes) -def test_add_latitude(cubes_2d, tmp_path, monkeypatch): +def test_add_latitude(cubes_2d): """Test fix.""" # Remove latitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) @@ -869,9 +876,6 @@ def test_add_latitude(cubes_2d, tmp_path, monkeypatch): cubes = CubeList([tas_cube]) fix = get_allvars_fix('Amon', 'tas') - # Temporary overwrite default cache location for downloads - monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) - assert len(fix._horizontal_grids) == 0 fixed_cubes = fix.fix_metadata(cubes) @@ -882,7 +886,7 @@ def test_add_latitude(cubes_2d, tmp_path, monkeypatch): assert TEST_GRID_FILE_NAME in fix._horizontal_grids -def test_add_longitude(cubes_2d, tmp_path, monkeypatch): +def test_add_longitude(cubes_2d): """Test fix.""" # Remove longitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) @@ -890,9 +894,6 @@ def test_add_longitude(cubes_2d, tmp_path, monkeypatch): cubes = CubeList([tas_cube]) fix = get_allvars_fix('Amon', 'tas') - # Temporary overwrite default cache location for downloads - monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) - assert len(fix._horizontal_grids) == 0 fixed_cubes = fix.fix_metadata(cubes) @@ -903,7 +904,7 @@ def test_add_longitude(cubes_2d, tmp_path, monkeypatch): assert TEST_GRID_FILE_NAME in fix._horizontal_grids -def test_add_latitude_longitude(cubes_2d, tmp_path, monkeypatch): +def test_add_latitude_longitude(cubes_2d): """Test fix.""" # Remove latitude and longitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) @@ -912,9 +913,6 @@ def test_add_latitude_longitude(cubes_2d, tmp_path, monkeypatch): cubes = CubeList([tas_cube]) fix = get_allvars_fix('Amon', 'tas') - # Temporary overwrite default cache location for downloads - monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) - assert len(fix._horizontal_grids) == 0 fixed_cubes = fix.fix_metadata(cubes) @@ -974,8 +972,7 @@ def test_add_coord_from_grid_file_fail_no_url(): fix._add_coord_from_grid_file(Cube(0), 'latitude') -def test_add_coord_from_grid_fail_no_unnamed_dim(cubes_2d, tmp_path, - monkeypatch): +def test_add_coord_from_grid_fail_no_unnamed_dim(cubes_2d): """Test fix.""" # Remove latitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) @@ -984,17 +981,13 @@ def test_add_coord_from_grid_fail_no_unnamed_dim(cubes_2d, tmp_path, tas_cube.add_dim_coord(index_coord, 1) fix = get_allvars_fix('Amon', 'tas') - # Temporary overwrite default cache location for downloads - monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) - msg = ("Cannot determine coordinate dimension for coordinate 'latitude', " "cube does not contain a single unnamed dimension") with pytest.raises(ValueError, match=msg): fix._add_coord_from_grid_file(tas_cube, 'latitude') -def test_add_coord_from_grid_fail_two_unnamed_dims(cubes_2d, tmp_path, - monkeypatch): +def test_add_coord_from_grid_fail_two_unnamed_dims(cubes_2d): """Test fix.""" # Remove latitude from tas cube to test automatic addition tas_cube = cubes_2d.extract_cube(NameConstraint(var_name='tas')) @@ -1002,9 +995,6 @@ def test_add_coord_from_grid_fail_two_unnamed_dims(cubes_2d, tmp_path, tas_cube = iris.util.new_axis(tas_cube) fix = get_allvars_fix('Amon', 'tas') - # Temporary overwrite default cache location for downloads - monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) - msg = ("Cannot determine coordinate dimension for coordinate 'latitude', " "cube does not contain a single unnamed dimension") with pytest.raises(ValueError, match=msg): @@ -1024,8 +1014,7 @@ def test_get_horizontal_grid_cached_in_dict(mock_requests): @mock.patch('esmvalcore.cmor._fixes.icon._base_fixes.requests', autospec=True) -def test_get_horizontal_grid_cached_in_file(mock_requests, tmp_path, - monkeypatch): +def test_get_horizontal_grid_cached_in_file(mock_requests, tmp_path): """Test fix.""" cube = Cube(0, attributes={ 'grid_file_uri': 'https://temporary.url/this/is/the/grid_file.nc'}) @@ -1036,9 +1025,6 @@ def test_get_horizontal_grid_cached_in_file(mock_requests, tmp_path, grid_cube = Cube(0, var_name='grid') iris.save(grid_cube, str(tmp_path / 'grid_file.nc')) - # Temporary overwrite default cache location for downloads - monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) - grid = fix.get_horizontal_grid(cube) assert isinstance(grid, CubeList) assert len(grid) == 1 @@ -1060,7 +1046,6 @@ def test_get_horizontal_grid_cache_file_too_old(tmp_path, monkeypatch): # Temporary overwrite default cache location for downloads and cache # validity duration - monkeypatch.setattr(fix, 'CACHE_DIR', tmp_path) monkeypatch.setattr(fix, 'CACHE_VALIDITY', -1) grid = fix.get_horizontal_grid(cube) @@ -1078,15 +1063,14 @@ def test_get_horizontal_grid_cache_file_too_old(tmp_path, monkeypatch): # Test with single-dimension cubes -def test_only_time(): +def test_only_time(monkeypatch): """Test fix.""" # We know that ta has dimensions time, plev19, latitude, longitude, but the # ICON CMORizer is designed to check for the presence of each dimension # individually. To test this, remove all but one dimension of ta to create # an artificial, but realistic test case. vardef = get_var_info('ICON', 'Amon', 'ta') - original_dimensions = vardef.dimensions - vardef.dimensions = ['time'] + monkeypatch.setattr(vardef, 'dimensions', ['time']) extra_facets = get_extra_facets('ICON', 'ICON', 'Amon', 'ta', ()) fix = AllVars(vardef, extra_facets=extra_facets) @@ -1119,19 +1103,18 @@ def test_only_time(): np.testing.assert_allclose(new_time_coord.bounds, [[-0.5, 0.5], [0.5, 1.5]]) - # Restore original dimensions of ta - vardef.dimensions = original_dimensions + # Check that no mesh has been created + assert cube.mesh is None -def test_only_height(): +def test_only_height(monkeypatch): """Test fix.""" # We know that ta has dimensions time, plev19, latitude, longitude, but the # ICON CMORizer is designed to check for the presence of each dimension # individually. To test this, remove all but one dimension of ta to create # an artificial, but realistic test case. vardef = get_var_info('ICON', 'Amon', 'ta') - original_dimensions = vardef.dimensions - vardef.dimensions = ['plev19'] + monkeypatch.setattr(vardef, 'dimensions', ['plev19']) extra_facets = get_extra_facets('ICON', 'ICON', 'Amon', 'ta', ()) fix = AllVars(vardef, extra_facets=extra_facets) @@ -1164,19 +1147,18 @@ def test_only_height(): np.testing.assert_allclose(new_height_coord.points, [1.0, 10.0]) assert new_height_coord.bounds is None - # Restore original dimensions of ta - vardef.dimensions = original_dimensions + # Check that no mesh has been created + assert cube.mesh is None -def test_only_latitude(): +def test_only_latitude(monkeypatch): """Test fix.""" # We know that ta has dimensions time, plev19, latitude, longitude, but the # ICON CMORizer is designed to check for the presence of each dimension # individually. To test this, remove all but one dimension of ta to create # an artificial, but realistic test case. vardef = get_var_info('ICON', 'Amon', 'ta') - original_dimensions = vardef.dimensions - vardef.dimensions = ['latitude'] + monkeypatch.setattr(vardef, 'dimensions', ['latitude']) extra_facets = get_extra_facets('ICON', 'ICON', 'Amon', 'ta', ()) fix = AllVars(vardef, extra_facets=extra_facets) @@ -1208,19 +1190,18 @@ def test_only_latitude(): np.testing.assert_allclose(new_lat_coord.points, [0.0, 10.0]) assert new_lat_coord.bounds is None - # Restore original dimensions of ta - vardef.dimensions = original_dimensions + # Check that no mesh has been created + assert cube.mesh is None -def test_only_longitude(): +def test_only_longitude(monkeypatch): """Test fix.""" # We know that ta has dimensions time, plev19, latitude, longitude, but the # ICON CMORizer is designed to check for the presence of each dimension # individually. To test this, remove all but one dimension of ta to create # an artificial, but realistic test case. vardef = get_var_info('ICON', 'Amon', 'ta') - original_dimensions = vardef.dimensions - vardef.dimensions = ['longitude'] + monkeypatch.setattr(vardef, 'dimensions', ['longitude']) extra_facets = get_extra_facets('ICON', 'ICON', 'Amon', 'ta', ()) fix = AllVars(vardef, extra_facets=extra_facets) @@ -1252,8 +1233,8 @@ def test_only_longitude(): np.testing.assert_allclose(new_lon_coord.points, [0.0, 180.0]) assert new_lon_coord.bounds is None - # Restore original dimensions of ta - vardef.dimensions = original_dimensions + # Check that no mesh has been created + assert cube.mesh is None # Test variable not available in file From 55b45b6a4991a616118dd527578e45f7da7d5b57 Mon Sep 17 00:00:00 2001 From: Bouwe Andela Date: Wed, 4 Jan 2023 14:24:15 +0100 Subject: [PATCH 08/15] Remove an extra space character from a filename (#1883) --- esmvalcore/cmor/_fixes/cordex/{__init__ .py => __init__.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename esmvalcore/cmor/_fixes/cordex/{__init__ .py => __init__.py} (100%) diff --git a/esmvalcore/cmor/_fixes/cordex/__init__ .py b/esmvalcore/cmor/_fixes/cordex/__init__.py similarity index 100% rename from esmvalcore/cmor/_fixes/cordex/__init__ .py rename to esmvalcore/cmor/_fixes/cordex/__init__.py From 2b138175c0d402f08e0eddc244d61efcfd393fa1 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Wed, 4 Jan 2023 17:08:05 +0100 Subject: [PATCH 09/15] Added doc --- doc/quickstart/find_data.rst | 26 +++++++++++++++++++ doc/recipe/preprocessor.rst | 16 ++++++------ .../integration/cmor/_fixes/icon/test_icon.py | 12 ++++----- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/doc/quickstart/find_data.rst b/doc/quickstart/find_data.rst index 3eba26400c..eadddb4ad9 100644 --- a/doc/quickstart/find_data.rst +++ b/doc/quickstart/find_data.rst @@ -363,6 +363,32 @@ Key Description Default value if not specified file default DRS is used) ============= ============================= ================================= +ESMValTool automatically makes native ICON data `UGRID +`__-compliant when +loading the data. +The UGRID conventions provide a standardized format to store data on +unstructured grids, which is required by many software packages or tools to +work correctly. +An example is the horizontal regridding of ICON data to a regular grid. While +the built-in :ref:`unstructured nearest scheme ` +can handle unstructured grids not in UGRID format, using more complex +regridding algorithms (for example provided by the :mod:`iris-esmf-regrid` +module through :ref:`generic regridding schemes`) requires the input data in +UGRID format. +The following code snippet provides a preprocessor that regrids native ICON +data to a 1°x1° grid using `ESMF's first-order conservative regridding +algorithm `__: + +.. code-block:: yaml + + preprocessors: + regrid_icon: + regrid: + target_grid: 1x1 + scheme: + reference: esmf_regrid.experimental.unstructured_scheme:regrid_unstructured_to_rectilinear + method: conservative + .. hint:: In order to read cell area files (``areacella`` and ``areacello``), one diff --git a/doc/recipe/preprocessor.rst b/doc/recipe/preprocessor.rst index 57e1dfc943..d31fda9745 100644 --- a/doc/recipe/preprocessor.rst +++ b/doc/recipe/preprocessor.rst @@ -932,17 +932,17 @@ arguments is also supported. The call function for such schemes must be defined The `regrid` module will automatically pass the source and grid cubes as inputs of the scheme. An example of this usage is the :func:`~esmf_regrid.schemes.regrid_rectilinear_to_rectilinear` -scheme available in :doc:`iris-esmf-regrid:index`:git +scheme available in :doc:`iris-esmf-regrid:index`: .. code-block:: yaml - preprocessors: - regrid_preprocessor: - regrid: - target_grid: 2.5x2.5 - scheme: - reference: esmf_regrid.schemes:regrid_rectilinear_to_rectilinear - mdtol: 0.7 + preprocessors: + regrid_preprocessor: + regrid: + target_grid: 2.5x2.5 + scheme: + reference: esmf_regrid.schemes:regrid_rectilinear_to_rectilinear + mdtol: 0.7 .. _ensemble statistics: diff --git a/tests/integration/cmor/_fixes/icon/test_icon.py b/tests/integration/cmor/_fixes/icon/test_icon.py index 8a7fe49c56..706fad7e06 100644 --- a/tests/integration/cmor/_fixes/icon/test_icon.py +++ b/tests/integration/cmor/_fixes/icon/test_icon.py @@ -24,7 +24,7 @@ @pytest.fixture(autouse=True) def tmp_cache_dir(monkeypatch, tmp_path): - """Always use temporary path as cache directory.""" + """Use temporary path as cache directory for all tests in this module.""" monkeypatch.setattr(IconFix, 'CACHE_DIR', tmp_path) @@ -195,14 +195,13 @@ def check_heightxm(cube, height_value): assert height.bounds is None -# TODO: uncomment checks def check_lat(cube): """Check latitude coordinate of cube.""" assert cube.coords('latitude', dim_coords=False) lat = cube.coord('latitude', dim_coords=False) - # assert lat.var_name == 'lat' + assert lat.var_name == 'lat' assert lat.standard_name == 'latitude' - # assert lat.long_name == 'latitude' + assert lat.long_name == 'latitude' assert lat.units == 'degrees_north' assert lat.attributes == {} np.testing.assert_allclose( @@ -227,14 +226,13 @@ def check_lat(cube): return lat -# TODO: uncomment checks def check_lon(cube): """Check longitude coordinate of cube.""" assert cube.coords('longitude', dim_coords=False) lon = cube.coord('longitude', dim_coords=False) - # assert lon.var_name == 'lon' + assert lon.var_name == 'lon' assert lon.standard_name == 'longitude' - # assert lon.long_name == 'longitude' + assert lon.long_name == 'longitude' assert lon.units == 'degrees_east' assert lon.attributes == {} np.testing.assert_allclose( From 84bf92883683873c90a7fdd7f9ce5d9542c5bf84 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Wed, 4 Jan 2023 17:19:28 +0100 Subject: [PATCH 10/15] Optimized doc --- doc/quickstart/find_data.rst | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/doc/quickstart/find_data.rst b/doc/quickstart/find_data.rst index eadddb4ad9..8a29919379 100644 --- a/doc/quickstart/find_data.rst +++ b/doc/quickstart/find_data.rst @@ -369,12 +369,12 @@ loading the data. The UGRID conventions provide a standardized format to store data on unstructured grids, which is required by many software packages or tools to work correctly. -An example is the horizontal regridding of ICON data to a regular grid. While -the built-in :ref:`unstructured nearest scheme ` -can handle unstructured grids not in UGRID format, using more complex -regridding algorithms (for example provided by the :mod:`iris-esmf-regrid` -module through :ref:`generic regridding schemes`) requires the input data in -UGRID format. +An example is the horizontal regridding of native ICON data to a regular grid. +While the built-in :ref:`unstructured_nearest scheme ` can handle unstructured grids not in UGRID format, using more complex +regridding algorithms (for example provided by the +:doc:`iris-esmf-regrid:index` module through :ref:`generic regridding schemes`) +requires the input data in UGRID format. The following code snippet provides a preprocessor that regrids native ICON data to a 1°x1° grid using `ESMF's first-order conservative regridding algorithm `__: @@ -385,9 +385,9 @@ algorithm `__: regrid_icon: regrid: target_grid: 1x1 - scheme: - reference: esmf_regrid.experimental.unstructured_scheme:regrid_unstructured_to_rectilinear - method: conservative + scheme: + reference: esmf_regrid.experimental.unstructured_scheme:regrid_unstructured_to_rectilinear + method: conservative .. hint:: From a82cfef576f3f72ee2a6c191b3b289a7bde7e6e0 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Wed, 4 Jan 2023 17:29:52 +0100 Subject: [PATCH 11/15] Optimized doc --- doc/quickstart/find_data.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/quickstart/find_data.rst b/doc/quickstart/find_data.rst index 8a29919379..ab2496d2f9 100644 --- a/doc/quickstart/find_data.rst +++ b/doc/quickstart/find_data.rst @@ -373,8 +373,8 @@ An example is the horizontal regridding of native ICON data to a regular grid. While the built-in :ref:`unstructured_nearest scheme ` can handle unstructured grids not in UGRID format, using more complex regridding algorithms (for example provided by the -:doc:`iris-esmf-regrid:index` module through :ref:`generic regridding schemes`) -requires the input data in UGRID format. +:doc:`iris-esmf-regrid:index` package through :ref:`generic regridding +schemes`) requires the input data in UGRID format. The following code snippet provides a preprocessor that regrids native ICON data to a 1°x1° grid using `ESMF's first-order conservative regridding algorithm `__: From 1b56c24efe94d7007645fa9edec9d0b9eee36ba2 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Tue, 10 Jan 2023 15:54:16 +0100 Subject: [PATCH 12/15] Do not overwrite regridding scheme in product.settings --- esmvalcore/preprocessor/_regrid.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/esmvalcore/preprocessor/_regrid.py b/esmvalcore/preprocessor/_regrid.py index e4edbce9fe..c85f2d6434 100644 --- a/esmvalcore/preprocessor/_regrid.py +++ b/esmvalcore/preprocessor/_regrid.py @@ -478,14 +478,11 @@ def regrid(cube, target_grid, scheme, lat_offset=True, lon_offset=True): target_grid : Cube or str or dict The (location of a) cube that specifies the target or reference grid for the regridding operation. - Alternatively, a string cell specification may be provided, of the form ``MxN``, which specifies the extent of the cell, longitude by latitude (degrees) for a global, regular target grid. - Alternatively, a dictionary with a regional target grid may be specified (see above). - scheme : str or dict The regridding scheme to perform. If both source and target grid are structured (regular or irregular), can be one of the built-in schemes @@ -574,6 +571,7 @@ def regrid(cube, target_grid, scheme, lat_offset=True, lon_offset=True): raise ValueError(f'Expecting a cube, got {target_grid}.') if isinstance(scheme, dict): + scheme = dict(scheme) # do not overwrite original scheme try: object_ref = scheme.pop("reference") except KeyError as key_err: From daa618494ce0d6b3a9141c83f176f3f44a2f8259 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Tue, 10 Jan 2023 16:05:32 +0100 Subject: [PATCH 13/15] Updated necessary iris version --- environment.yml | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/environment.yml b/environment.yml index c17f9de011..ee0fa3f3be 100644 --- a/environment.yml +++ b/environment.yml @@ -17,7 +17,7 @@ dependencies: - geopy - humanfriendly - importlib_resources - - iris>=3.2.1 + - iris>=3.4.0 - iris-esmf-regrid - isodate - jinja2 diff --git a/setup.py b/setup.py index 698011b115..38abfb1bc7 100755 --- a/setup.py +++ b/setup.py @@ -55,7 +55,7 @@ 'pyyaml', 'requests', 'scipy>=1.6', - 'scitools-iris>=3.2.1', + 'scitools-iris>=3.4.0', 'shapely[vectorized]', 'stratify', 'yamale', From 67adc8d3cbb2f9600538cc8ce340326a2d5a9c55 Mon Sep 17 00:00:00 2001 From: Manuel Schlund <32543114+schlunma@users.noreply.github.com> Date: Thu, 26 Jan 2023 14:20:54 +0100 Subject: [PATCH 14/15] Update doc/quickstart/find_data.rst Co-authored-by: Bouwe Andela --- doc/quickstart/find_data.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/quickstart/find_data.rst b/doc/quickstart/find_data.rst index ab2496d2f9..e828fbdb47 100644 --- a/doc/quickstart/find_data.rst +++ b/doc/quickstart/find_data.rst @@ -363,7 +363,7 @@ Key Description Default value if not specified file default DRS is used) ============= ============================= ================================= -ESMValTool automatically makes native ICON data `UGRID +ESMValCore automatically makes native ICON data `UGRID `__-compliant when loading the data. The UGRID conventions provide a standardized format to store data on From 4c4d63d085086ff27f5a074af5ef5d38c70003d1 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Wed, 1 Feb 2023 10:32:52 +0100 Subject: [PATCH 15/15] Make sure esmvaltool cache dir exists prior to downloading ICON grid file --- esmvalcore/cmor/_fixes/fix.py | 2 +- esmvalcore/cmor/_fixes/icon/_base_fixes.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/esmvalcore/cmor/_fixes/fix.py b/esmvalcore/cmor/_fixes/fix.py index f7239a7a33..44eb24e8c9 100644 --- a/esmvalcore/cmor/_fixes/fix.py +++ b/esmvalcore/cmor/_fixes/fix.py @@ -1,7 +1,7 @@ """Contains the base class for dataset fixes.""" import importlib -import os import inspect +import os from ..table import CMOR_TABLES diff --git a/esmvalcore/cmor/_fixes/icon/_base_fixes.py b/esmvalcore/cmor/_fixes/icon/_base_fixes.py index ecb722109e..7fe0ab1c1e 100644 --- a/esmvalcore/cmor/_fixes/icon/_base_fixes.py +++ b/esmvalcore/cmor/_fixes/icon/_base_fixes.py @@ -88,6 +88,7 @@ def get_horizontal_grid(self, cube): # Download file if necessary logger.debug("Attempting to download ICON grid file from '%s' to '%s'", grid_url, grid_path) + self.CACHE_DIR.mkdir(parents=True, exist_ok=True) with requests.get(grid_url, stream=True, timeout=self.TIMEOUT) as response: response.raise_for_status()