|
| 1 | +# Copyright Iris contributors |
| 2 | +# |
| 3 | +# This file is part of Iris and is released under the LGPL license. |
| 4 | +# See COPYING and COPYING.LESSER in the root of the repository for full |
| 5 | +# licensing details. |
| 6 | +"""Test function :func:`iris.analysis.cartography._get_lon_lat_coords""" |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from iris.analysis.cartography import _get_lon_lat_coords as g_lon_lat |
| 11 | +from iris.coords import AuxCoord |
| 12 | +from iris.tests.stock import lat_lon_cube |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def dim_only_cube(): |
| 17 | + return lat_lon_cube() |
| 18 | + |
| 19 | + |
| 20 | +def test_dim_only(dim_only_cube): |
| 21 | + t_lat, t_lon = dim_only_cube.dim_coords |
| 22 | + |
| 23 | + lon, lat = g_lon_lat(dim_only_cube) |
| 24 | + |
| 25 | + assert lon == t_lon |
| 26 | + assert lat == t_lat |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def dim_aux_cube(dim_only_cube): |
| 31 | + lat_dim, lon_dim = dim_only_cube.dim_coords |
| 32 | + |
| 33 | + lat_aux = AuxCoord.from_coord(lat_dim) |
| 34 | + lat_aux.standard_name = "grid_latitude" |
| 35 | + lon_aux = AuxCoord.from_coord(lon_dim) |
| 36 | + lon_aux.standard_name = "grid_longitude" |
| 37 | + |
| 38 | + dim_aux_cube = dim_only_cube |
| 39 | + dim_aux_cube.add_aux_coord(lat_aux, 0) |
| 40 | + dim_aux_cube.add_aux_coord(lon_aux, 1) |
| 41 | + |
| 42 | + return dim_aux_cube |
| 43 | + |
| 44 | + |
| 45 | +def test_dim_aux(dim_aux_cube): |
| 46 | + t_lat_dim, t_lon_dim = dim_aux_cube.dim_coords |
| 47 | + |
| 48 | + lon, lat = g_lon_lat(dim_aux_cube) |
| 49 | + |
| 50 | + assert lon == t_lon_dim |
| 51 | + assert lat == t_lat_dim |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture |
| 55 | +def aux_only_cube(dim_aux_cube): |
| 56 | + lon_dim, lat_dim = dim_aux_cube.dim_coords |
| 57 | + |
| 58 | + aux_only_cube = dim_aux_cube |
| 59 | + aux_only_cube.remove_coord(lon_dim) |
| 60 | + aux_only_cube.remove_coord(lat_dim) |
| 61 | + |
| 62 | + return dim_aux_cube |
| 63 | + |
| 64 | + |
| 65 | +def test_aux_only(aux_only_cube): |
| 66 | + aux_lat, aux_lon = aux_only_cube.aux_coords |
| 67 | + |
| 68 | + lon, lat = g_lon_lat(aux_only_cube) |
| 69 | + |
| 70 | + assert lon == aux_lon |
| 71 | + assert lat == aux_lat |
| 72 | + |
| 73 | + |
| 74 | +@pytest.fixture |
| 75 | +def double_dim_cube(dim_only_cube): |
| 76 | + double_dim_cube = dim_only_cube |
| 77 | + double_dim_cube.coord("latitude").standard_name = "grid_longitude" |
| 78 | + |
| 79 | + return double_dim_cube |
| 80 | + |
| 81 | + |
| 82 | +def test_double_dim(double_dim_cube): |
| 83 | + t_error_message = "with multiple.*is currently disallowed" |
| 84 | + |
| 85 | + with pytest.raises(ValueError, match=t_error_message): |
| 86 | + g_lon_lat(double_dim_cube) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.fixture |
| 90 | +def double_aux_cube(aux_only_cube): |
| 91 | + double_aux_cube = aux_only_cube |
| 92 | + double_aux_cube.coord("grid_latitude").standard_name = "longitude" |
| 93 | + |
| 94 | + return double_aux_cube |
| 95 | + |
| 96 | + |
| 97 | +def test_double_aux(double_aux_cube): |
| 98 | + t_error_message = "with multiple.*is currently disallowed" |
| 99 | + |
| 100 | + with pytest.raises(ValueError, match=t_error_message): |
| 101 | + g_lon_lat(double_aux_cube) |
| 102 | + |
| 103 | + |
| 104 | +@pytest.fixture |
| 105 | +def missing_lat_cube(dim_only_cube): |
| 106 | + missing_lat_cube = dim_only_cube |
| 107 | + missing_lat_cube.remove_coord("latitude") |
| 108 | + |
| 109 | + return missing_lat_cube |
| 110 | + |
| 111 | + |
| 112 | +def test_missing_coord(missing_lat_cube): |
| 113 | + with pytest.raises(IndexError): |
| 114 | + g_lon_lat(missing_lat_cube) |
0 commit comments