Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions lib/iris/tests/integration/ugrid/test_ucube_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Copyright Iris contributors
#
# This file is part of Iris and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
"""
Integration tests for the
:mod:`iris.fileformats.ugrid_cf_reader.UGridCFReader` class.

"""
# Import iris.tests first so that some things can be initialised before
# importing anything else.
import iris.tests as tests

import numpy as np

from iris.cube import CubeList
from iris.fileformats.netcdf import load_cubes

from iris.util.ucube_operations import (
ugrid_plot,
identify_cubesphere,
ucube_subset,
ugrid_subset,
pseudo_cube,
PseudoshapedCubeIndexer,
latlon_extract_faces,
)


def load_unstructured_testcube():
# Load a standard unstructured cube to work with.
testfile_path = tests.get_data_path(
("NetCDF", "unstructured_grid", "data_C4.nc")
)

cubes = CubeList(list(load_cubes(testfile_path)))
(cube,) = cubes.extract("sample_data")

return cube


class TestUgridSubset(tests.IrisTest):
# For now, only testing the 'face' extract functionality.
def test_faces_subset(self):
grid = load_unstructured_testcube().ugrid.grid
selected_face_indices = [12, 3, 7]
subset_grid = ugrid_subset(grid, selected_face_indices, "face")
self.assertEqual(subset_grid.mesh_name, "mesh")
self.assertTrue(np.all(subset_grid.nodes == grid.nodes))
self.assertEqual(subset_grid.faces.shape, (3, 4))
self.assertTrue(
np.all(subset_grid.faces == grid.faces[selected_face_indices])
)

def test_faces_subset_boolarray(self):
grid = load_unstructured_testcube().ugrid.grid
faces_yesno = np.zeros(96, dtype=bool)
faces_yesno[[1, 5, 3, 2, 8, 6]] = True
subset_grid = ugrid_subset(grid, faces_yesno, "face")
self.assertEqual(subset_grid.mesh_name, "mesh")
self.assertTrue(np.all(subset_grid.nodes == grid.nodes))
self.assertTrue(subset_grid.faces.shape == (6, 4))
self.assertTrue(np.all(subset_grid.faces == grid.faces[faces_yesno]))


class TestUcubeSubset(tests.IrisTest):
# NOTE: the testdata we're using here has data mapped to faces.
# For now, test just + only that functionality.
def test_faces_subset_indices(self):
cube = load_unstructured_testcube()
selected_face_indices = [3, 5, 2, 17]
subset_cube = ucube_subset(cube, selected_face_indices)
self.assertIsNotNone(subset_cube.ugrid)
self.assertEqual(subset_cube.ugrid.grid.mesh_name, "mesh")
self.assertTrue(subset_cube.shape == (4,))
self.assertTrue(
np.all(subset_cube.data == cube.data[selected_face_indices])
)

def test_faces_subset_boolarray(self):
cube = load_unstructured_testcube()
faces_yesno = np.zeros(96, dtype=bool)
faces_yesno[[1, 5, 3, 2, 8, 6]] = True
subset_cube = ucube_subset(cube, faces_yesno)
self.assertIsNotNone(subset_cube.ugrid)
self.assertEqual(subset_cube.ugrid.grid.mesh_name, "mesh")
self.assertTrue(subset_cube.shape == (6,))
self.assertTrue(np.all(subset_cube.data == cube.data[faces_yesno]))


class TestIdentifyCubesphere(tests.IrisTest):
def test_identify(self):
cube = load_unstructured_testcube()
cube_shape = identify_cubesphere(cube.ugrid.grid)
self.assertEqual(cube_shape, (6, 4, 4))


class TestPlotCubesphere(tests.GraphicsTest):
def test_plot(self):
cube = load_unstructured_testcube()
ugrid_plot(cube)
self.check_graphic()


class TestPseudoCube(tests.IrisTest):
def test_pseudocube(self):
cube = load_unstructured_testcube()
shape = (6, 4, 4)
names = ["n_face", "face_y", "face_x"]
pseudo_cubesphere = pseudo_cube(cube, shape=shape, new_dim_names=names)
self.assertEqual(pseudo_cubesphere.shape, (6, 4, 4))
coord_names = [
co.name() for co in pseudo_cubesphere.coords(dim_coords=True)
]
self.assertEqual(coord_names, names)


class TestPseudoshapedCubeIndexer(tests.IrisTest):
def test_indexer(self):
cube = load_unstructured_testcube()
cube_shape = (6, 4, 4)
cs_partial_cube = PseudoshapedCubeIndexer(cube, cube_shape)[1, 1:]
self.assertIsNotNone(cs_partial_cube.ugrid)
self.assertEqual(cs_partial_cube.ugrid.grid.mesh_name, "mesh")
self.assertTrue(cs_partial_cube.shape == (12,))
self.assertTrue(np.all(cs_partial_cube.data == cube.data[20:32]))


class TestlatlonExtract(tests.IrisTest):
def test_indexer(self):
cube = load_unstructured_testcube()
region = [-20, 60, 20, 65]
region_cube = latlon_extract_faces(cube, region)
self.assertIsNotNone(region_cube.ugrid)
self.assertEqual(region_cube.ugrid.grid.mesh_name, "mesh")
self.assertEqual(region_cube.shape, (7,))
selected_face_indices = [1, 2, 3, 16, 68, 72, 76]
self.assertTrue(
np.all(region_cube.data == cube.data[selected_face_indices])
)


if __name__ == "__main__":
tests.main()
5 changes: 4 additions & 1 deletion lib/iris/tests/results/imagerepo.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@
"iris.tests.integration.plot.test_vector_plots.TestQuiver.test_non_latlon_2d_coords.0": [
"https://scitools.github.io/test-iris-imagehash/images/v4/afac26367251d3493617632df45c26a6e126c6f392593b4937266f26ccf232d0.png"
],
"iris.tests.integration.ugrid.test_ucube_operations.TestPlotCubesphere.test_plot.0": [
"https://scitools.github.io/test-iris-imagehash/images/v4/e1a531999612ce349a699a63cde4718c3196365934dacf33633933d92671ce67.png"
],
"iris.tests.test_analysis.TestProject.test_cartopy_projection.0": [
"https://scitools.github.io/test-iris-imagehash/images/v4/9e1952c9c165b4fc668a9d47c1461d7a60fb2e853eb426bd62fd229c9f04c16d.png"
],
Expand Down Expand Up @@ -1033,4 +1036,4 @@
"https://scitools.github.io/test-iris-imagehash/images/v4/82fe81987fdf77ffe0002addd4002805dd28df67d9a9d4625bfddc209841de20.png",
"https://scitools.github.io/test-iris-imagehash/images/v4/82fa80997f547799a0037a00d52f0956ddaf9f7e98a1816e09f5d8260bfffe00.png"
]
}
}
File renamed without changes.
Loading