|
1 | 1 | """Fixes for ICON-ESM-LR model.""" |
2 | 2 |
|
| 3 | +from iris.mesh import MeshXY |
| 4 | + |
3 | 5 | from esmvalcore.cmor._fixes.fix import Fix |
| 6 | +from esmvalcore.iris_helpers import has_unstructured_grid |
4 | 7 |
|
5 | 8 |
|
6 | 9 | class AllVars(Fix): |
7 | 10 | """Fixes for all variables.""" |
8 | 11 |
|
| 12 | + @staticmethod |
| 13 | + def _add_mesh_from_vertices(cube, lat_vertices, lon_vertices): |
| 14 | + """Add Iris mesh from cell vertices if this is an unstructured grid.""" |
| 15 | + # skip if the mesh topology is already provided |
| 16 | + if cube.mesh is not None: |
| 17 | + return False |
| 18 | + # only create meshes for 1D unstructured grids. |
| 19 | + if not has_unstructured_grid(cube): |
| 20 | + return False |
| 21 | + |
| 22 | + lat = cube.coord("latitude") |
| 23 | + lon = cube.coord("longitude") |
| 24 | + lat_dims = cube.coord_dims(lat) |
| 25 | + lon_dims = cube.coord_dims(lon) |
| 26 | + |
| 27 | + # check the dims |
| 28 | + if lat_dims != lon_dims or len(lat_dims) != 1: |
| 29 | + return False |
| 30 | + |
| 31 | + # at least triangular cell bounds |
| 32 | + if ( |
| 33 | + lat_vertices.shape != lon_vertices.shape |
| 34 | + or lat_vertices.ndim != 2 |
| 35 | + or lat_vertices.shape[0] != lat.shape[0] |
| 36 | + or lat_vertices.shape[1] < 3 |
| 37 | + ): |
| 38 | + return False |
| 39 | + |
| 40 | + lat.bounds = lat_vertices |
| 41 | + lon.bounds = lon_vertices |
| 42 | + |
| 43 | + mesh = MeshXY.from_coords(lon, lat) |
| 44 | + # replace aux coords with mesh aware ones |
| 45 | + cube.remove_coord("latitude") |
| 46 | + cube.remove_coord("longitude") |
| 47 | + for mesh_coord in mesh.to_MeshCoords("face"): |
| 48 | + cube.add_aux_coord(mesh_coord, lat_dims) |
| 49 | + |
| 50 | + return True |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def _add_mesh_from_coord_bounds(cls, cube): |
| 54 | + """Add Iris mesh from existing coordinate bounds if possible.""" |
| 55 | + if not cube.coords("latitude") or not cube.coords("longitude"): |
| 56 | + return False |
| 57 | + |
| 58 | + lat = cube.coord("latitude") |
| 59 | + lon = cube.coord("longitude") |
| 60 | + if not lat.has_bounds() or not lon.has_bounds(): |
| 61 | + return False |
| 62 | + |
| 63 | + return cls._add_mesh_from_vertices( |
| 64 | + cube, |
| 65 | + lat.core_bounds(), |
| 66 | + lon.core_bounds(), |
| 67 | + ) |
| 68 | + |
| 69 | + def fix_data(self, cube): |
| 70 | + """Add Iris mesh for unstructured data if possible.""" |
| 71 | + self._add_mesh_from_coord_bounds(cube) |
| 72 | + return cube |
| 73 | + |
9 | 74 | def fix_metadata(self, cubes): |
10 | 75 | """Rename ``var_name`` of latitude and longitude. |
11 | 76 |
|
|
0 commit comments