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
125 changes: 55 additions & 70 deletions lib/iris/tests/unit/fileformats/nimrod_load_rules/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@
# 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 `iris.fileformats.nimrod_load_rules.units` function."""

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

from unittest import mock

import numpy as np
import pytest

from iris.cube import Cube
from iris.fileformats.nimrod import NimrodField
from iris.fileformats.nimrod_load_rules import NIMROD_DEFAULT, units
from iris.tests._shared_utils import (
assert_array_almost_equal,
assert_no_warnings_regexp,
)


class Test(tests.IrisTest):
NIMROD_LOCATION = "iris.fileformats.nimrod_load_rules"

def setUp(self):
self.field = mock.Mock(
class Test:
@pytest.fixture(autouse=True)
def _setup(self, mocker):
self.field = mocker.Mock(
units="",
int_mdi=-32767,
float32_mdi=NIMROD_DEFAULT,
Expand All @@ -36,90 +33,78 @@ def _call_units(self, data=None, units_str=None):
self.field.units = units_str
units(self.cube, self.field)

def test_null(self):
with mock.patch("warnings.warn") as warn:
def test_null(self, mocker):
with assert_no_warnings_regexp():
self._call_units(units_str="m")
self.assertEqual(warn.call_count, 0)
self.assertEqual(self.cube.units, "m")
self.assertArrayAlmostEqual(self.cube.data, np.ones_like(self.cube.data))
assert self.cube.units == "m"
assert_array_almost_equal(self.cube.data, np.ones_like(self.cube.data))

def test_times32(self):
with mock.patch("warnings.warn") as warn:
def test_times32(self, mocker):
with assert_no_warnings_regexp():
self._call_units(
data=np.ones_like(self.cube.data) * 32, units_str="mm/hr*32"
)
self.assertEqual(warn.call_count, 0)
self.assertEqual(self.cube.units, "mm/hr")
self.assertArrayAlmostEqual(self.cube.data, np.ones_like(self.cube.data))
self.assertEqual(self.cube.data.dtype, np.float32)
assert self.cube.units == "mm/hr"
assert_array_almost_equal(self.cube.data, np.ones_like(self.cube.data))
assert self.cube.data.dtype == np.float32

def test_visibility_units(self):
with mock.patch("warnings.warn") as warn:
def test_visibility_units(self, mocker):
with assert_no_warnings_regexp():
self._call_units(
data=((np.ones_like(self.cube.data) / 2) - 25000),
units_str="m/2-25k",
)
self.assertEqual(warn.call_count, 0)
self.assertEqual(self.cube.units, "m")
self.assertArrayAlmostEqual(self.cube.data, np.ones_like(self.cube.data))
self.assertEqual(self.cube.data.dtype, np.float32)
assert self.cube.units == "m"
assert_array_almost_equal(self.cube.data, np.ones_like(self.cube.data))
assert self.cube.data.dtype == np.float32

def test_power_in_units(self):
with mock.patch("warnings.warn") as warn:
def test_power_in_units(self, mocker):
with assert_no_warnings_regexp():
self._call_units(
data=np.ones_like(self.cube.data) * 1000, units_str="mm*10^3"
)
self.assertEqual(warn.call_count, 0)
self.assertEqual(self.cube.units, "mm")
self.assertArrayAlmostEqual(self.cube.data, np.ones_like(self.cube.data))
self.assertEqual(self.cube.data.dtype, np.float32)
assert self.cube.units == "mm"
assert_array_almost_equal(self.cube.data, np.ones_like(self.cube.data))
assert self.cube.data.dtype == np.float32

def test_ug_per_m3_units(self):
with mock.patch("warnings.warn") as warn:
def test_ug_per_m3_units(self, mocker):
with assert_no_warnings_regexp():
self._call_units(
data=(np.ones_like(self.cube.data) * 10),
units_str="ug/m3E1",
)
self.assertEqual(warn.call_count, 0)
self.assertEqual(self.cube.units, "ug/m3")
self.assertArrayAlmostEqual(self.cube.data, np.ones_like(self.cube.data))
self.assertEqual(self.cube.data.dtype, np.float32)
assert self.cube.units == "ug/m3"
assert_array_almost_equal(self.cube.data, np.ones_like(self.cube.data))
assert self.cube.data.dtype == np.float32

def test_g_per_kg(self):
with mock.patch("warnings.warn") as warn:
def test_g_per_kg(self, mocker):
with assert_no_warnings_regexp():
self._call_units(
data=(np.ones_like(self.cube.data) * 1000), units_str="g/Kg"
)
self.assertEqual(warn.call_count, 0)
self.assertEqual(self.cube.units, "kg/kg")
self.assertArrayAlmostEqual(self.cube.data, np.ones_like(self.cube.data))
self.assertEqual(self.cube.data.dtype, np.float32)
assert self.cube.units == "kg/kg"
assert_array_almost_equal(self.cube.data, np.ones_like(self.cube.data))
assert self.cube.data.dtype == np.float32

def test_unit_expection_dictionary(self):
with mock.patch("warnings.warn") as warn:
def test_unit_expection_dictionary(self, mocker):
with assert_no_warnings_regexp():
self._call_units(units_str="mb")
self.assertEqual(warn.call_count, 0)
self.assertEqual(self.cube.units, "hPa")
self.assertArrayAlmostEqual(self.cube.data, np.ones_like(self.cube.data))
self.assertEqual(self.cube.data.dtype, np.float32)
assert self.cube.units == "hPa"
assert_array_almost_equal(self.cube.data, np.ones_like(self.cube.data))
assert self.cube.data.dtype == np.float32

def test_per_second(self):
with mock.patch("warnings.warn") as warn:
def test_per_second(self, mocker):
with assert_no_warnings_regexp():
self._call_units(units_str="/s")
self.assertEqual(warn.call_count, 0)
self.assertEqual(self.cube.units, "s^-1")
self.assertArrayAlmostEqual(self.cube.data, np.ones_like(self.cube.data))
self.assertEqual(self.cube.data.dtype, np.float32)
assert self.cube.units == "s^-1"
assert_array_almost_equal(self.cube.data, np.ones_like(self.cube.data))
assert self.cube.data.dtype == np.float32

def test_unhandled_unit(self):
with mock.patch("warnings.warn") as warn:
def test_unhandled_unit(self, mocker):
warning_message = "Unhandled units 'kittens' recorded in cube attributes"
with pytest.warns(match=warning_message):
self._call_units(units_str="kittens")
self.assertEqual(warn.call_count, 1)
self.assertEqual(self.cube.units, "")
self.assertArrayAlmostEqual(self.cube.data, np.ones_like(self.cube.data))
self.assertEqual(self.cube.data.dtype, np.float32)
self.assertEqual(self.cube.attributes["invalid_units"], "kittens")


if __name__ == "__main__":
tests.main()
assert self.cube.units == ""
assert_array_almost_equal(self.cube.data, np.ones_like(self.cube.data))
assert self.cube.data.dtype == np.float32
assert self.cube.attributes["invalid_units"] == "kittens"
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,21 @@
function.

"""

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

from unittest import mock
import pytest

from iris.fileformats.nimrod import NimrodField
from iris.fileformats.nimrod_load_rules import (
NIMROD_DEFAULT,
TranslationWarning,
vertical_coord,
)
from iris.tests._shared_utils import assert_no_warnings_regexp


class Test(tests.IrisTest):
NIMROD_LOCATION = "iris.fileformats.nimrod_load_rules"

def setUp(self):
self.field = mock.Mock(
class Test:
@pytest.fixture(autouse=True)
def _setup(self, mocker):
self.field = mocker.Mock(
vertical_coord=NIMROD_DEFAULT,
vertical_coord_type=NIMROD_DEFAULT,
reference_vertical_coord=NIMROD_DEFAULT,
Expand All @@ -34,7 +29,7 @@ def setUp(self):
float32_mdi=NIMROD_DEFAULT,
spec=NimrodField,
)
self.cube = mock.Mock()
self.cube = mocker.Mock()

def _call_vertical_coord(
self,
Expand All @@ -54,23 +49,15 @@ def _call_vertical_coord(
vertical_coord(self.cube, self.field)

def test_unhandled(self):
with mock.patch("warnings.warn") as warn:
message_regexp = "Vertical coord -1 not yet handled"
with pytest.warns(TranslationWarning, match=message_regexp):
self._call_vertical_coord(vertical_coord_val=1.0, vertical_coord_type=-1)
warn.assert_called_once_with(
"Vertical coord -1 not yet handled", category=TranslationWarning
)

def test_null(self):
with mock.patch("warnings.warn") as warn:
with assert_no_warnings_regexp():
self._call_vertical_coord(vertical_coord_type=NIMROD_DEFAULT)
self._call_vertical_coord(vertical_coord_type=self.field.int_mdi)
self.assertEqual(warn.call_count, 0)

def test_ground_level(self):
with mock.patch("warnings.warn") as warn:
with assert_no_warnings_regexp():
self._call_vertical_coord(vertical_coord_val=9999.0, vertical_coord_type=0)
self.assertEqual(warn.call_count, 0)


if __name__ == "__main__":
tests.main()