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 3 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
25 changes: 25 additions & 0 deletions 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(
pp-mo marked this conversation as resolved.
Show resolved Hide resolved
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)
):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Experiments so far have produced many cases where this block is true, which makes me worried I need an extra factor in here like scaling or units or something. That's why I changed the PR back to draft.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still unsure about this one, but if @pp-mo has no objections I guess I'll take it out of draft.

Copy link
Member

@pp-mo pp-mo Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess your experiments might indicate that more tolerance is required.
Since it's only a double-check, we can afford to be tolerant, don't want to be crying wolf.
Maybe something like atol=0.5 rtol=0.001 ?
So 10. +/- 0.5, 10,000. +/- 10.

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 iris_grib/_save_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,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