-
Notifications
You must be signed in to change notification settings - Fork 298
Add load support for GRIB2 GDT 12. #1475
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,6 +214,40 @@ def _hindcast_fix(forecast_time): | |
| return forecast_time | ||
|
|
||
|
|
||
| def fixup_float32_from_int32(value): | ||
| """ | ||
| Workaround for use when reading an IEEE 32-bit floating-point value | ||
| which the ECMWF GRIB API has erroneously treated as a 4-byte signed | ||
| integer. | ||
|
|
||
| """ | ||
| # Convert from two's complement to sign-and-magnitude. | ||
| # NB. The bit patterns 0x00000000 and 0x80000000 will both be | ||
| # returned by the ECMWF GRIB API as an integer 0. Because they | ||
| # correspond to positive and negative zero respectively it is safe | ||
| # to treat an integer 0 as a positive zero. | ||
| if value < 0: | ||
| value = 0x80000000 - value | ||
| value_as_uint32 = np.array(value, dtype='u4') | ||
| value_as_float32 = value_as_uint32.view(dtype='f4') | ||
| return float(value_as_float32) | ||
|
|
||
|
|
||
| def fixup_int32_from_uint32(value): | ||
| """ | ||
| Workaround for use when reading a signed, 4-byte integer which the | ||
| ECMWF GRIB API has erroneously treated as an unsigned, 4-byte | ||
| integer. | ||
|
|
||
| NB. This workaround is safe to use with values which are already | ||
| treated as signed, 4-byte integers. | ||
|
|
||
| """ | ||
| if value >= 0x80000000: | ||
| value = 0x80000000 - value | ||
| return value | ||
|
|
||
|
|
||
| ############################################################################### | ||
| # | ||
| # Identification Section 1 | ||
|
|
@@ -644,6 +678,83 @@ def grid_definition_template_5(section, metadata): | |
| 'grid_latitude', 'grid_longitude', cs) | ||
|
|
||
|
|
||
| def grid_definition_template_12(section, metadata): | ||
| """ | ||
| Translate template representing transverse Mercator. | ||
|
|
||
| Updates the metadata in-place with the translations. | ||
|
|
||
| Args: | ||
|
|
||
| * section: | ||
| Dictionary of coded key/value pairs from section 3 of the message. | ||
|
|
||
| * metadata: | ||
| :class:`collections.OrderedDict` of metadata. | ||
|
|
||
| """ | ||
| major, minor, radius = ellipsoid_geometry(section) | ||
| geog_cs = ellipsoid(section['shapeOfTheEarth'], major, minor, radius) | ||
|
|
||
| lat = section['latitudeOfReferencePoint'] * _GRID_ACCURACY_IN_DEGREES | ||
| lon = section['longitudeOfReferencePoint'] * _GRID_ACCURACY_IN_DEGREES | ||
| scale = section['scaleFactorAtReferencePoint'] | ||
| # Catch bug in ECMWF GRIB API (present at 1.12.1) where the scale | ||
| # is treated as a signed, 4-byte integer. | ||
| if isinstance(scale, int): | ||
| scale = fixup_float32_from_int32(scale) | ||
| CM_TO_M = 0.01 | ||
| easting = section['XR'] * CM_TO_M | ||
| northing = section['YR'] * CM_TO_M | ||
| cs = icoord_systems.TransverseMercator(lat, lon, easting, northing, | ||
| scale, geog_cs) | ||
|
|
||
| # Deal with bug in ECMWF GRIB API (present at 1.12.1) where these | ||
| # values are treated as unsigned, 4-byte integers. | ||
| x1 = fixup_int32_from_uint32(section['x1']) | ||
| y1 = fixup_int32_from_uint32(section['y1']) | ||
| x2 = fixup_int32_from_uint32(section['x2']) | ||
| y2 = fixup_int32_from_uint32(section['y2']) | ||
|
|
||
| # Rather unhelpfully this grid definition template seems to be | ||
| # overspecified, and thus open to inconsistency. | ||
| last_x = x1 + (section['Ni'] - 1) * section['Di'] | ||
| last_y = y1 + (section['Nj'] - 1) * section['Dj'] | ||
| if (last_x != x2 or last_y != y2): | ||
| raise TranslationError('Inconsistent grid definition') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error or warning?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took it that our philosophy with the new GRIB loader is to explode if we don't know what to do, rather than give ambiguous results. |
||
|
|
||
| x1 = x1 * CM_TO_M | ||
| dx = section['Di'] * CM_TO_M | ||
| x_points = x1 + np.arange(section['Ni']) * dx | ||
| y1 = y1 * CM_TO_M | ||
| dy = section['Dj'] * CM_TO_M | ||
| y_points = y1 + np.arange(section['Nj']) * dy | ||
|
|
||
| # This has only been tested with +x/+y scanning, so raise an error | ||
| # for other permutations. | ||
| scan = scanning_mode(section['scanningMode']) | ||
| if scan.i_negative: | ||
| raise TranslationError('Unsupported -x scanning') | ||
| if not scan.j_positive: | ||
| raise TranslationError('Unsupported -y scanning') | ||
|
|
||
| # Create the X and Y coordinates. | ||
| y_coord = DimCoord(y_points, 'projection_y_coordinate', units='m', | ||
| coord_system=cs) | ||
| x_coord = DimCoord(x_points, 'projection_x_coordinate', units='m', | ||
| coord_system=cs) | ||
|
|
||
| # Determine the lat/lon dimensions. | ||
| y_dim, x_dim = 0, 1 | ||
| scan = scanning_mode(section['scanningMode']) | ||
| if scan.j_consecutive: | ||
| y_dim, x_dim = 1, 0 | ||
|
|
||
| # Add the X and Y coordinates to the metadata dim coords. | ||
| metadata['dim_coords_and_dims'].append((y_coord, y_dim)) | ||
| metadata['dim_coords_and_dims'].append((x_coord, x_dim)) | ||
|
|
||
|
|
||
| def grid_definition_template_90(section, metadata): | ||
| """ | ||
| Translate template representing space view. | ||
|
|
@@ -792,6 +903,9 @@ def grid_definition_section(section, metadata): | |
| elif template == 5: | ||
| # Process variable resolution rotated latitude/longitude. | ||
| grid_definition_template_5(section, metadata) | ||
| elif template == 12: | ||
| # Process transverse Mercator. | ||
| grid_definition_template_12(section, metadata) | ||
| elif template == 90: | ||
| # Process space view. | ||
| grid_definition_template_90(section, metadata) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
lib/iris/tests/unit/fileformats/grib/load_convert/test_fixup_float32_from_int32.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # (C) British Crown Copyright 2014, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Iris is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Iris. If not, see <http://www.gnu.org/licenses/>. | ||
| """ | ||
| Unit tests for `iris.fileformats.grib._load_convert.fixup_float32_from_int32`. | ||
|
|
||
| """ | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
|
|
||
| # Import iris.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris.tests as tests | ||
|
|
||
| from iris.fileformats.grib._load_convert import fixup_float32_from_int32 | ||
|
|
||
|
|
||
| class Test(tests.IrisTest): | ||
| def test_negative(self): | ||
| result = fixup_float32_from_int32(-0x3f000000) | ||
| self.assertEqual(result, -0.5) | ||
|
|
||
| def test_zero(self): | ||
| result = fixup_float32_from_int32(0) | ||
| self.assertEqual(result, 0) | ||
|
|
||
| def test_positive(self): | ||
| result = fixup_float32_from_int32(0x3f000000) | ||
| self.assertEqual(result, 0.5) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| tests.main() |
56 changes: 56 additions & 0 deletions
56
lib/iris/tests/unit/fileformats/grib/load_convert/test_fixup_int32_from_uint32.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # (C) British Crown Copyright 2014, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Iris is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Iris. If not, see <http://www.gnu.org/licenses/>. | ||
| """ | ||
| Unit tests for `iris.fileformats.grib._load_convert.fixup_int32_from_uint32`. | ||
|
|
||
| """ | ||
|
|
||
| from __future__ import (absolute_import, division, print_function) | ||
|
|
||
| # Import iris.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris.tests as tests | ||
|
|
||
| from iris.fileformats.grib._load_convert import fixup_int32_from_uint32 | ||
|
|
||
|
|
||
| class Test(tests.IrisTest): | ||
| def test_negative(self): | ||
| result = fixup_int32_from_uint32(0x80000005) | ||
| self.assertEqual(result, -5) | ||
|
|
||
| def test_negative_zero(self): | ||
| result = fixup_int32_from_uint32(0x80000000) | ||
| self.assertEqual(result, 0) | ||
|
|
||
| def test_zero(self): | ||
| result = fixup_int32_from_uint32(0) | ||
| self.assertEqual(result, 0) | ||
|
|
||
| def test_positive(self): | ||
| result = fixup_int32_from_uint32(200000) | ||
| self.assertEqual(result, 200000) | ||
|
|
||
| def test_already_negative(self): | ||
| # If we *already* have a negative value the fixup routine should | ||
| # leave it alone. | ||
| result = fixup_int32_from_uint32(-7) | ||
| self.assertEqual(result, -7) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| tests.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NB. There's a bug in the ECMWF GRIB API which causes it to interpret this 32-bit float as an integer ... so this will probably give rubbish results for a genuine message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably simplest to check the type of
scaleand if it's an int do the reverse of the float32->int32 workaround in #1480.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the check for an int in a second commit.