|
| 1 | +# Copyright Iris contributors |
| 2 | +# |
| 3 | +# This file is part of Iris and is released under the BSD license. |
| 4 | +# See LICENSE in the root of the repository for full licensing details. |
| 5 | +"""Integration tests for MeshCoord.coord_system() behaviour.""" |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +import iris |
| 10 | +from iris.coord_systems import GeogCS |
| 11 | +from iris.experimental.ugrid.load import PARSE_UGRID_ON_LOAD |
| 12 | +from iris.tests.stock.netcdf import ncgen_from_cdl |
| 13 | + |
| 14 | +TEST_CDL = """ |
| 15 | +netcdf mesh_test { |
| 16 | + dimensions: |
| 17 | + node = 3 ; |
| 18 | + face = 1 ; |
| 19 | + vertex = 3 ; |
| 20 | + variables: |
| 21 | + int mesh ; |
| 22 | + mesh:cf_role = "mesh_topology" ; |
| 23 | + mesh:topology_dimension = 2 ; |
| 24 | + mesh:node_coordinates = "node_x node_y" ; |
| 25 | + mesh:face_node_connectivity = "face_nodes" ; |
| 26 | + float node_x(node) ; |
| 27 | + node_x:standard_name = "longitude" ; |
| 28 | + float node_y(node) ; |
| 29 | + node_y:standard_name = "latitude" ; |
| 30 | + int face_nodes(face, vertex) ; |
| 31 | + face_nodes:cf_role = "face_node_connectivity" ; |
| 32 | + face_nodes:start_index = 0 ; |
| 33 | + float node_data(node) ; |
| 34 | + node_data:coordinates = "node_x node_y" ; |
| 35 | + node_data:location = "node" ; |
| 36 | + node_data:mesh = "mesh" ; |
| 37 | +} |
| 38 | +""" |
| 39 | + |
| 40 | +BASIC_CRS_STR = """ |
| 41 | + int crs ; |
| 42 | + crs:grid_mapping_name = "latitude_longitude" ; |
| 43 | + crs:semi_major_axis = 6371000.0 ; |
| 44 | +""" |
| 45 | + |
| 46 | + |
| 47 | +def find_i_line(lines, match): |
| 48 | + (i_line,) = [i for i, line in enumerate(lines) if match in line] |
| 49 | + return i_line |
| 50 | + |
| 51 | + |
| 52 | +def make_file(nc_path, node_x_crs=False, node_y_crs=False): |
| 53 | + lines = TEST_CDL.split("\n") |
| 54 | + |
| 55 | + includes = ["node_x"] if node_x_crs else [] |
| 56 | + includes += ["node_y"] if node_y_crs else [] |
| 57 | + includes = " ".join(includes) |
| 58 | + if includes: |
| 59 | + # insert a grid-mapping |
| 60 | + i_line = find_i_line(lines, "variables:") |
| 61 | + lines[i_line + 1 : i_line + 1] = BASIC_CRS_STR.split("\n") |
| 62 | + |
| 63 | + i_line = find_i_line(lines, "float node_data(") |
| 64 | + ref_lines = [ |
| 65 | + # NOTE: space to match the surrounding indent |
| 66 | + f' node_data:grid_mapping = "crs: {includes}" ;', |
| 67 | + # NOTE: this is already present |
| 68 | + # f'node_data:coordinates = "{includes}" ;' |
| 69 | + ] |
| 70 | + lines[i_line + 1 : i_line + 1] = ref_lines |
| 71 | + |
| 72 | + cdl_str = "\n".join(lines) |
| 73 | + ncgen_from_cdl(cdl_str=cdl_str, cdl_path=None, nc_path=nc_path) |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize("cs_axes", ["--", "x-", "-y", "xy"]) |
| 77 | +def test_default_mesh_cs(tmp_path, cs_axes): |
| 78 | + """Test coord-systems of mesh cube and coords, if location coords have a crs.""" |
| 79 | + nc_path = tmp_path / "test_temp.nc" |
| 80 | + do_x = "x" in cs_axes |
| 81 | + do_y = "y" in cs_axes |
| 82 | + make_file(nc_path, node_x_crs=do_x, node_y_crs=do_y) |
| 83 | + with PARSE_UGRID_ON_LOAD.context(): |
| 84 | + cube = iris.load_cube(nc_path, "node_data") |
| 85 | + meshco_x, meshco_y = [cube.coord(mesh_coords=True, axis=ax) for ax in ("x", "y")] |
| 86 | + # NOTE: at present, none of these load with a coordinate system, |
| 87 | + # because we don't support the extended grid-mapping syntax. |
| 88 | + # see: https://github.com/SciTools/iris/issues/3388 |
| 89 | + assert meshco_x.coord_system is None |
| 90 | + assert meshco_y.coord_system is None |
| 91 | + |
| 92 | + |
| 93 | +def test_assigned_mesh_cs(tmp_path): |
| 94 | + # Check that when a coord system is manually assigned to a location coord, |
| 95 | + # the corresponding meshcoord reports the same cs. |
| 96 | + nc_path = tmp_path / "test_temp.nc" |
| 97 | + make_file(nc_path) |
| 98 | + with PARSE_UGRID_ON_LOAD.context(): |
| 99 | + cube = iris.load_cube(nc_path, "node_data") |
| 100 | + nodeco_x = cube.mesh.coord(include_nodes=True, axis="x") |
| 101 | + meshco_x, meshco_y = [cube.coord(axis=ax) for ax in ("x", "y")] |
| 102 | + assert nodeco_x.coord_system is None |
| 103 | + assert meshco_x.coord_system is None |
| 104 | + assert meshco_y.coord_system is None |
| 105 | + assigned_cs = GeogCS(1.0) |
| 106 | + nodeco_x.coord_system = assigned_cs |
| 107 | + assert meshco_x.coord_system is assigned_cs |
| 108 | + assert meshco_y.coord_system is None |
| 109 | + # This also affects cube.coord_system(), even though it is an auxcoord, |
| 110 | + # since there are no dim-coords, or any other coord with a c-s. |
| 111 | + # TODO: this may be a mistake -- see https://github.com/SciTools/iris/issues/6051 |
| 112 | + assert cube.coord_system() is assigned_cs |
| 113 | + |
| 114 | + |
| 115 | +def test_meshcoord_coordsys_copy(tmp_path): |
| 116 | + # Check that copying a meshcoord with a coord system works properly. |
| 117 | + nc_path = tmp_path / "test_temp.nc" |
| 118 | + make_file(nc_path) |
| 119 | + with PARSE_UGRID_ON_LOAD.context(): |
| 120 | + cube = iris.load_cube(nc_path, "node_data") |
| 121 | + node_coord = cube.mesh.coord(include_nodes=True, axis="x") |
| 122 | + assigned_cs = GeogCS(1.0) |
| 123 | + node_coord.coord_system = assigned_cs |
| 124 | + mesh_coord = cube.coord(axis="x") |
| 125 | + assert mesh_coord.coord_system is assigned_cs |
| 126 | + meshco_copy = mesh_coord.copy() |
| 127 | + assert meshco_copy == mesh_coord |
| 128 | + # Note: still the same object, because it is derived from the same node_coord |
| 129 | + assert meshco_copy.coord_system is assigned_cs |
0 commit comments