Skip to content
Merged
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
60 changes: 0 additions & 60 deletions lib/iris/tests/unit/fileformats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,3 @@
# This file is part of Iris and is released under the BSD license.
# See LICENSE in the root of the repository for full licensing details.
"""Unit tests for the :mod:`iris.fileformats` package."""

import iris.tests as tests # isort:skip


class TestField(tests.IrisTest):
def _test_for_coord(
self, field, convert, coord_predicate, expected_points, expected_bounds
):
(
factories,
references,
standard_name,
long_name,
units,
attributes,
cell_methods,
dim_coords_and_dims,
aux_coords_and_dims,
) = convert(field)

# Check for one and only one matching coordinate.
coords_and_dims = dim_coords_and_dims + aux_coords_and_dims
matching_coords = [
coord for coord, _ in coords_and_dims if coord_predicate(coord)
]
self.assertEqual(len(matching_coords), 1, str(matching_coords))
coord = matching_coords[0]

# Check points and bounds.
if expected_points is not None:
self.assertArrayEqual(coord.points, expected_points)

if expected_bounds is None:
self.assertIsNone(coord.bounds)
else:
self.assertArrayEqual(coord.bounds, expected_bounds)

def assertCoordsAndDimsListsMatch(
self, coords_and_dims_got, coords_and_dims_expected
):
"""Check that coords_and_dims lists are equivalent.

The arguments are lists of pairs of (coordinate, dimensions).
The elements are compared one-to-one, by coordinate name (so the order
of the lists is _not_ significant).
It also checks that the coordinate types (DimCoord/AuxCoord) match.

"""

def sorted_by_coordname(list):
return sorted(list, key=lambda item: item[0].name())

coords_and_dims_got = sorted_by_coordname(coords_and_dims_got)
coords_and_dims_expected = sorted_by_coordname(coords_and_dims_expected)
self.assertEqual(coords_and_dims_got, coords_and_dims_expected)
# Also check coordinate type equivalences (as Coord.__eq__ does not).
self.assertEqual(
[type(coord) for coord, dims in coords_and_dims_got],
[type(coord) for coord, dims in coords_and_dims_expected],
)
56 changes: 56 additions & 0 deletions lib/iris/tests/unit/fileformats/pp_load_rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,59 @@
# This file is part of Iris and is released under the BSD license.
# See LICENSE in the root of the repository for full licensing details.
"""Unit tests for the :mod:`iris.fileformats.pp_load_rules` module."""

# general utility functions for PP field tests
from iris.tests._shared_utils import assert_array_equal


def assert_test_for_coord(
field, convert, coord_predicate, expected_points, expected_bounds
):
(
factories,
references,
standard_name,
long_name,
units,
attributes,
cell_methods,
dim_coords_and_dims,
aux_coords_and_dims,
) = convert(field)

# Check for one and only one matching coordinate.
coords_and_dims = dim_coords_and_dims + aux_coords_and_dims
matching_coords = [coord for coord, _ in coords_and_dims if coord_predicate(coord)]
assert len(matching_coords) == 1, str(matching_coords)
coord = matching_coords[0]

# Check points and bounds.
if expected_points is not None:
assert_array_equal(coord.points, expected_points)

if expected_bounds is None:
assert coord.bounds is None
else:
assert_array_equal(coord.bounds, expected_bounds)


def assert_coords_and_dims_lists_match(coords_and_dims_got, coords_and_dims_expected):
"""Check that coords_and_dims lists are equivalent.

The arguments are lists of pairs of (coordinate, dimensions).
The elements are compared one-to-one, by coordinate name (so the order
of the lists is _not_ significant).
It also checks that the coordinate types (DimCoord/AuxCoord) match.

"""

def sorted_by_coordname(list):
return sorted(list, key=lambda item: item[0].name())

coords_and_dims_got = sorted_by_coordname(coords_and_dims_got)
coords_and_dims_expected = sorted_by_coordname(coords_and_dims_expected)
assert coords_and_dims_got == coords_and_dims_expected
# Also check coordinate type equivalences (as Coord.__eq__ does not).
assert [type(coord) for coord, dims in coords_and_dims_got] == [
type(coord) for coord, dims in coords_and_dims_expected
]
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from iris.coords import AuxCoord, CellMethod, DimCoord
from iris.fileformats.pp import SplittableInt
from iris.fileformats.pp_load_rules import _all_other_rules
from iris.tests.unit.fileformats import TestField
from iris.tests import IrisTest
from iris.tests.unit.fileformats.pp_load_rules import assert_coords_and_dims_lists_match

# iris.fileformats.pp_load_rules._all_other_rules() returns a tuple of
# of various metadata. This constant is the index into this
Expand Down Expand Up @@ -142,7 +143,7 @@ def test_multiple_unordered_rotated_lbprocs(self):
self.assertEqual(res, expected)


class TestCrossSectionalTime(TestField):
class TestCrossSectionalTime(IrisTest):
def test_lbcode3x23(self):
time_bounds = np.array(
[[0.875, 1.125], [1.125, 1.375], [1.375, 1.625], [1.625, 1.875]]
Expand Down Expand Up @@ -201,10 +202,10 @@ def test_lbcode3x23(self):
0,
)
]
self.assertCoordsAndDimsListsMatch(res, expected)
assert_coords_and_dims_lists_match(res, expected)


class TestLBTIMx2x_ZeroYears(TestField):
class TestLBTIMx2x_ZeroYears(IrisTest):
_spec = [
"lbtim",
"lbcode",
Expand Down Expand Up @@ -272,28 +273,28 @@ def test_month_coord(self):
None,
),
]
self.assertCoordsAndDimsListsMatch(res, expected)
assert_coords_and_dims_lists_match(res, expected)

def test_diff_month(self):
field = self._make_field(lbmon=3, lbmond=4)
field.mock_add_spec(self._spec)
res = _all_other_rules(field)[AUX_COORDS_INDEX]

self.assertCoordsAndDimsListsMatch(res, [])
assert_coords_and_dims_lists_match(res, [])

def test_nonzero_year(self):
field = self._make_field(lbyr=1)
field.mock_add_spec(self._spec)
res = _all_other_rules(field)[AUX_COORDS_INDEX]

self.assertCoordsAndDimsListsMatch(res, [])
assert_coords_and_dims_lists_match(res, [])

def test_nonzero_yeard(self):
field = self._make_field(lbyrd=1)
field.mock_add_spec(self._spec)
res = _all_other_rules(field)[AUX_COORDS_INDEX]

self.assertCoordsAndDimsListsMatch(res, [])
assert_coords_and_dims_lists_match(res, [])


if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

from iris.coords import DimCoord
from iris.fileformats.pp_load_rules import _convert_scalar_pseudo_level_coords
from iris.tests.unit.fileformats import TestField
from iris.tests import IrisTest


class Test(TestField):
class Test(IrisTest):
def test_valid(self):
coords_and_dims = _convert_scalar_pseudo_level_coords(lbuser5=21)
self.assertEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

from iris.coords import DimCoord
from iris.fileformats.pp_load_rules import _convert_scalar_realization_coords
from iris.tests.unit.fileformats import TestField
from iris.tests import IrisTest


class Test(TestField):
class Test(IrisTest):
def test_valid(self):
coords_and_dims = _convert_scalar_realization_coords(lbrsvd4=21)
self.assertEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
from iris.coords import AuxCoord, DimCoord
from iris.fileformats.pp import SplittableInt
from iris.fileformats.pp_load_rules import _convert_time_coords
from iris.tests.unit.fileformats import TestField
from iris.tests import IrisTest as TestField
from iris.tests.unit.fileformats.pp_load_rules import assert_coords_and_dims_lists_match


def _lbtim(ia=0, ib=0, ic=0):
Expand Down Expand Up @@ -69,7 +70,7 @@ def _check_timepoint(self, lbcode, expect_match=True):
]
else:
expect_result = []
self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result)
assert_coords_and_dims_lists_match(coords_and_dims, expect_result)

def test_normal_xy_dims(self):
self._check_timepoint(_lbcode(1))
Expand Down Expand Up @@ -126,7 +127,7 @@ def _check_forecast(self, lbcode, expect_match=True):
]
else:
expect_result = []
self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result)
assert_coords_and_dims_lists_match(coords_and_dims, expect_result)

def test_normal_xy(self):
self._check_forecast(_lbcode(1))
Expand Down Expand Up @@ -218,7 +219,7 @@ def _check_period(self, lbcode, expect_match=True):
]
else:
expect_result = []
self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result)
assert_coords_and_dims_lists_match(coords_and_dims, expect_result)

def test_normal_xy(self):
self._check_period(_lbcode(1))
Expand Down Expand Up @@ -280,7 +281,7 @@ def _check_yearly(self, lbcode, expect_match=True):
]
else:
expect_result = []
self.assertCoordsAndDimsListsMatch(coords_and_dims, expect_result)
assert_coords_and_dims_lists_match(coords_and_dims, expect_result)

def test_normal_xy(self):
self._check_yearly(_lbcode(1))
Expand Down Expand Up @@ -355,7 +356,7 @@ def test(self):
None,
)
]
self.assertCoordsAndDimsListsMatch(coords_and_dims, expected_result)
assert_coords_and_dims_lists_match(coords_and_dims, expected_result)


class TestArrayInputWithLBTIM_0_0_1(TestField):
Expand Down Expand Up @@ -387,7 +388,7 @@ def test_t1_list(self):
(24 * 8) + 3 + hours, standard_name="time", units=_EPOCH_HOURS_UNIT
)
expected = [(time_coord, (0,))]
self.assertCoordsAndDimsListsMatch(coords_and_dims, expected)
assert_coords_and_dims_lists_match(coords_and_dims, expected)


class TestArrayInputWithLBTIM_0_1_1(TestField):
Expand Down Expand Up @@ -436,7 +437,7 @@ def test_t1_list_t2_scalar(self):
(time_coord, (0,)),
(fref_time_coord, None),
]
self.assertCoordsAndDimsListsMatch(coords_and_dims, expected)
assert_coords_and_dims_lists_match(coords_and_dims, expected)

def test_t1_and_t2_list(self):
# lbtim ia = 0, ib = 1, ic = 1
Expand Down Expand Up @@ -485,7 +486,7 @@ def test_t1_and_t2_list(self):
(time_coord, (0,)),
(fref_time_coord, None),
]
self.assertCoordsAndDimsListsMatch(coords_and_dims, expected)
assert_coords_and_dims_lists_match(coords_and_dims, expected)

def test_t1_and_t2_orthogonal_lists(self):
# lbtim ia = 0, ib = 1, ic = 1
Expand Down Expand Up @@ -532,7 +533,7 @@ def test_t1_and_t2_orthogonal_lists(self):
(time_coord, (0,)),
(fref_time_coord, (1,)),
]
self.assertCoordsAndDimsListsMatch(coords_and_dims, expected)
assert_coords_and_dims_lists_match(coords_and_dims, expected)

def test_t1_multi_dim_list_t2_scalar(self):
# Another case of lbtim ia = 0, ib = 1, ic = 1 but
Expand Down Expand Up @@ -586,7 +587,7 @@ def test_t1_multi_dim_list_t2_scalar(self):
(time_coord, (0, 1)),
(fref_time_coord, None),
]
self.assertCoordsAndDimsListsMatch(coords_and_dims, expected)
assert_coords_and_dims_lists_match(coords_and_dims, expected)

def test_t1_and_t2_nparrays(self):
# lbtim ia = 0, ib = 1, ic = 1
Expand Down Expand Up @@ -639,7 +640,7 @@ def test_t1_and_t2_nparrays(self):
(time_coord, (0,)),
(fref_time_coord, None),
]
self.assertCoordsAndDimsListsMatch(coords_and_dims, expected)
assert_coords_and_dims_lists_match(coords_and_dims, expected)


class TestArrayInputWithLBTIM_0_2_1(TestField):
Expand Down Expand Up @@ -694,7 +695,7 @@ def test_t1_list_t2_scalar(self):
(time_coord, (0,)),
(fref_time_coord, None),
]
self.assertCoordsAndDimsListsMatch(coords_and_dims, expected)
assert_coords_and_dims_lists_match(coords_and_dims, expected)


class TestArrayInputWithLBTIM_0_3_1(TestField):
Expand Down Expand Up @@ -754,7 +755,7 @@ def test_t1_scalar_t2_list(self):
(time_coord, (0,)),
(fref_time_coord, (0,)),
]
self.assertCoordsAndDimsListsMatch(coords_and_dims, expected)
assert_coords_and_dims_lists_match(coords_and_dims, expected)


if __name__ == "__main__":
Expand Down
Loading