Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle missing keys for GDT10 Mercator #446

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions docs/ref/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,53 @@ Release Notes
=============


What's new in iris-grib v0.21.0
-------------------------------

:Release: 0.21.0
:Date: [unreleased]

Features
^^^^^^^^

* `@trexfeathers <https://github.com/trexfeathers>`_ added checks for invalid
values in the following keys when loading grid definition template 3.10
(mercator grids): ``orientationOfTheGrid``, ``longitudeOfLastGridPoint``,
``latitudeOfLastGridPoint``.
`(ISSUE#118) <https://github.com/SciTools/iris-grib/issues/118>`_,
`(PR#446) <https://github.com/SciTools/iris-grib/pull/446>`_


Bugs Fixed
^^^^^^^^^^

* N/A


Documentation
^^^^^^^^^^^^^

* N/A


Dependencies
^^^^^^^^^^^^

* N/A


Internal
^^^^^^^^

* N/A


New Contributors
^^^^^^^^^^^^^^^^

* N/A


What's new in iris-grib v0.20.0
-------------------------------

Expand Down
25 changes: 25 additions & 0 deletions src/iris_grib/_load_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,35 @@ def grid_definition_template_10(section, metadata):
# intersects the Earth
standard_parallel = section["LaD"] * _GRID_ACCURACY_IN_DEGREES

if "orientationOfTheGrid" in section and not np.isclose(
section["orientationOfTheGrid"], 0
):
# Could support in future by using the ObliqueMercator class.
message = (
f"{section['orientationOfTheGrid']=} . iris-grib only supports "
"0.0 orientation for grid definition template 10."
)
raise TranslationError(message)

cs = icoord_systems.Mercator(standard_parallel=standard_parallel, ellipsoid=geog_cs)

# Create the X and Y coordinates.
x_coord, y_coord, scan = _calculate_proj_coords_from_grid_lengths(section, cs)
final_x_point = x_coord.points[-1]
final_y_point = y_coord.points[-1]
if not (
np.isclose(section["longitudeOfLastGridPoint"], final_x_point)
and np.isclose(section["latitudeOfLastGridPoint"], final_y_point)
):
message = (
"File grid definition inconsistent. Grid specification produces "
f"{final_x_point=}, {final_y_point=}. But "
f"{section['longitudeOfLastGridPoint']=} , "
f"{section['latitudeOfLastGridPoint']=} .\n\n"
"(Grid specification for Longitude: Di, Ni, "
"longitudeOfFirstGridPoint, scanningMode. Latitude uses: Dj, Nj)"
)
warnings.warn(message)

# Determine the lat/lon dimensions.
y_dim, x_dim = 0, 1
Expand Down
3 changes: 3 additions & 0 deletions src/iris_grib/_save_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ def grid_definition_template_10(cube, grib):
0x1 << _RESOLUTION_AND_COMPONENTS_GRID_WINDS_BIT,
)

# We don't save "orientationOfTheGrid" since we can't represent it -
# would need a future handling of the Iris ObliqueMercator class.


def grid_definition_template_12(cube, grib):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# before importing anything else.
import iris_grib.tests as tests

import warnings

import numpy as np

import iris.coord_systems
Expand All @@ -37,10 +39,10 @@ def section_3(self):
"Ni": 181,
"Nj": 213,
"latitudeOfFirstGridPoint": 2351555,
"latitudeOfLastGridPoint": 25088204,
"latitudeOfLastGridPoint": 2797793.1090371446,
"LaD": 14000000,
"longitudeOfFirstGridPoint": 114990304,
"longitudeOfLastGridPoint": 135009712,
"longitudeOfLastGridPoint": 14566918.990644248,
"resolutionAndComponentFlags": 56,
"scanningMode": 64,
"Di": 12000000,
Expand Down Expand Up @@ -83,6 +85,34 @@ def test(self):
expected = self.expected(y_dim=0, x_dim=1)
self.assertEqual(metadata, expected)

def test_last_point_warning(self):
section = self.section_3()
metadata = empty_metadata()

# No warnings expected with the standard values.
with warnings.catch_warnings():
warnings.simplefilter("error")
grid_definition_template_10(section, metadata)

# Warning expected if specified last point does not agree with the
# generated one.
section["longitudeOfLastGridPoint"] = 0
expected_message = (
"File grid definition inconsistent. Grid specification produces "
"final_x_point="
)
with self.assertWarnsRegex(UserWarning, expected_message):
grid_definition_template_10(section, metadata)

def test_orientation_error(self):
section = self.section_3()
section["orientationOfTheGrid"] = 1
metadata = empty_metadata()
with self.assertRaisesRegex(
iris.exceptions.TranslationError, "iris-grib only supports 0.0 orientation"
):
grid_definition_template_10(section, metadata)


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