Skip to content

Commit c66703a

Browse files
authored
Quieter datum warning (#6050)
* Only warn about datum handling if a datum is present . * Partial conversion to PyTest. * Add test to confirm warnings not raised if no datum is present. * What's New entry. * Rewrite duplicate warning testing. * What's New typo.
1 parent 17dd34a commit c66703a

File tree

4 files changed

+56
-38
lines changed

4 files changed

+56
-38
lines changed

docs/src/whatsnew/latest.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ This document explains the changes made to Iris for this release
5959
#. `@pp-mo`_ corrected the use of mesh dimensions when saving with multiple
6060
meshes. (:issue:`5908`, :pull:`6004`)
6161

62+
#. `@trexfeathers`_ fixed the datum :class:`python:FutureWarning` to only be raised if
63+
the ``datum_support`` :class:`~iris.Future` flag is disabled AND a datum is
64+
present on the loaded NetCDF grid mapping. (:issue:`5749`, :pull:`6050`)
65+
6266

6367
💣 Incompatible Changes
6468
=======================

lib/iris/fileformats/_nc_load_rules/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ def _get_ellipsoid(cf_grid_var):
522522
if datum == "unknown":
523523
datum = None
524524

525-
if not iris.FUTURE.datum_support:
525+
if datum is not None and not iris.FUTURE.datum_support:
526526
wmsg = (
527527
"Ignoring a datum in netCDF load for consistency with existing "
528528
"behaviour. In a future version of Iris, this datum will be "

lib/iris/tests/integration/netcdf/test_coord_systems.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
from os.path import join as path_join
1212
import shutil
1313
import tempfile
14+
import warnings
15+
16+
import pytest
1417

1518
import iris
1619
from iris.coords import DimCoord
@@ -135,15 +138,25 @@ def test_load_datum_wkt(self):
135138
cube = iris.load_cube(nc_path)
136139
test_crs = cube.coord("projection_y_coordinate").coord_system
137140
actual = str(test_crs.as_cartopy_crs().datum)
138-
self.assertMultiLineEqual(expected, actual)
141+
assert actual == expected
139142

140143
def test_no_load_datum_wkt(self):
141144
nc_path = tlc.cdl_to_nc(self.datum_wkt_cdl)
142-
with self.assertWarnsRegex(FutureWarning, "iris.FUTURE.datum_support"):
145+
with pytest.warns(FutureWarning, match="iris.FUTURE.datum_support"):
143146
cube = iris.load_cube(nc_path)
144147
test_crs = cube.coord("projection_y_coordinate").coord_system
145148
actual = str(test_crs.as_cartopy_crs().datum)
146-
self.assertMultiLineEqual(actual, "unknown")
149+
assert actual == "unknown"
150+
151+
def test_no_datum_no_warn(self):
152+
new_cdl = self.datum_wkt_cdl.splitlines()
153+
new_cdl = [line for line in new_cdl if "DATUM" not in line]
154+
new_cdl = "\n".join(new_cdl)
155+
nc_path = tlc.cdl_to_nc(new_cdl)
156+
with warnings.catch_warnings():
157+
# pytest's recommended way to assert for no warnings.
158+
warnings.simplefilter("error", FutureWarning)
159+
_ = iris.load_cube(nc_path)
147160

148161
def test_load_datum_cf_var(self):
149162
expected = "OSGB 1936"
@@ -152,15 +165,15 @@ def test_load_datum_cf_var(self):
152165
cube = iris.load_cube(nc_path)
153166
test_crs = cube.coord("projection_y_coordinate").coord_system
154167
actual = str(test_crs.as_cartopy_crs().datum)
155-
self.assertMultiLineEqual(expected, actual)
168+
assert actual == expected
156169

157170
def test_no_load_datum_cf_var(self):
158171
nc_path = tlc.cdl_to_nc(self.datum_cf_var_cdl)
159-
with self.assertWarnsRegex(FutureWarning, "iris.FUTURE.datum_support"):
172+
with pytest.warns(FutureWarning, match="iris.FUTURE.datum_support"):
160173
cube = iris.load_cube(nc_path)
161174
test_crs = cube.coord("projection_y_coordinate").coord_system
162175
actual = str(test_crs.as_cartopy_crs().datum)
163-
self.assertMultiLineEqual(actual, "unknown")
176+
assert actual == "unknown"
164177

165178
def test_save_datum(self):
166179
expected = "OSGB 1936"
@@ -199,7 +212,7 @@ def test_save_datum(self):
199212

200213
test_crs = cube.coord("projection_y_coordinate").coord_system
201214
actual = str(test_crs.as_cartopy_crs().datum)
202-
self.assertMultiLineEqual(expected, actual)
215+
assert actual == expected
203216

204217

205218
class TestLoadMinimalGeostationary(tests.IrisTest):
@@ -270,9 +283,9 @@ def test_geostationary_no_false_offsets(self):
270283
cube = iris.load_cube(self.path_test_nc)
271284
# Check the coordinate system properties has the correct default properties.
272285
cs = cube.coord_system()
273-
self.assertIsInstance(cs, iris.coord_systems.Geostationary)
274-
self.assertEqual(cs.false_easting, 0.0)
275-
self.assertEqual(cs.false_northing, 0.0)
286+
assert isinstance(cs, iris.coord_systems.Geostationary)
287+
assert cs.false_easting == 0.0
288+
assert cs.false_northing == 0.0
276289

277290

278291
if __name__ == "__main__":

lib/iris/tests/integration/netcdf/test_general.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -488,37 +488,38 @@ def test_path_string_save_same(self):
488488

489489
@tests.skip_data
490490
class TestWarningRepeats(tests.IrisTest):
491-
def test_datum_once(self):
492-
"""Tests for warnings being duplicated.
491+
def test_warning_repeats(self):
492+
"""Confirm Iris load does not break Python duplicate warning handling."""
493+
# units.nc is designed for testing Iris' 'ignoring invalid units'
494+
# warning; it contains two variables with invalid units, producing two
495+
# unique warnings (due to two different messages).
496+
file_path = tests.get_data_path(("NetCDF", "testing", "units.nc"))
493497

494-
Notes
495-
-----
496-
This test relies on `iris.load` throwing a warning. This warning might
497-
be removed in the future, in which case `assert len(record) == 2 should`
498-
be change to `assert len(record) == 1`.
499-
500-
toa_brightness_temperature.nc has an AuxCoord with lazy data, and triggers a
501-
specific part of dask which contains a `catch_warnings()` call which
502-
causes warnings to be repeated, and so has been removed from the
503-
`fnames` list until a solution is found for such a file.
504-
505-
"""
506-
#
507-
fnames = [
508-
"false_east_north_merc.nc",
509-
"non_unit_scale_factor_merc.nc",
510-
# toa_brightness_temperature.nc,
511-
]
512-
fpaths = [
513-
tests.get_data_path(("NetCDF", "mercator", fname)) for fname in fnames
514-
]
498+
def _raise_warning() -> None:
499+
# Contain in function so warning always has identical line number.
500+
warnings.warn("Dummy warning", category=iris.warnings.IrisUserWarning)
515501

516502
with warnings.catch_warnings(record=True) as record:
517503
warnings.simplefilter("default")
518-
for fpath in fpaths:
519-
iris.load(fpath)
520-
warnings.warn("Dummy warning", category=iris.warnings.IrisUserWarning)
521-
assert len(record) == 2
504+
505+
# Warn before Iris has been invoked.
506+
_raise_warning()
507+
assert len(record) == 1
508+
509+
# This Iris call should raise 2 warnings and should NOT affect
510+
# Python's duplicate warning handling.
511+
_ = iris.load(file_path)
512+
assert len(record) == 3
513+
# Raise a duplicate warning.
514+
_raise_warning()
515+
assert len(record) == 3
516+
517+
# Repeated identical calls should only raise duplicate warnings
518+
# and therefore not affect the record.
519+
for i in range(2):
520+
_ = iris.load(file_path)
521+
_raise_warning()
522+
assert len(record) == 3
522523

523524

524525
if __name__ == "__main__":

0 commit comments

Comments
 (0)