-
Notifications
You must be signed in to change notification settings - Fork 44
Port tests from iris/tests/integration/test_grib2.py #192
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
2 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
73 changes: 73 additions & 0 deletions
73
iris_grib/tests/integration/load_convert/test_data_section.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,73 @@ | ||
| # Copyright iris-grib contributors | ||
| # | ||
| # This file is part of iris-grib and is released under the LGPL license. | ||
| # See COPYING and COPYING.LESSER in the root of the repository for full | ||
| # licensing details. | ||
| """ | ||
| Integration tests to confirm data is loaded correctly. | ||
|
|
||
| """ | ||
|
|
||
| # import iris_grib.tests first so that some things can be initialised | ||
| # before importing anything else. | ||
| import iris_grib.tests as tests | ||
|
|
||
| import numpy.ma as ma | ||
|
|
||
| from iris import load_cube | ||
|
|
||
|
|
||
| class TestImport(tests.IrisGribTest): | ||
| def test_gdt1(self): | ||
| path = tests.get_data_path( | ||
| ("GRIB", "rotated_nae_t", "sensible_pole.grib2") | ||
| ) | ||
| cube = load_cube(path) | ||
| self.assertCMLApproxData(cube) | ||
|
|
||
| def test_gdt90_with_bitmap(self): | ||
| path = tests.get_data_path(("GRIB", "umukv", "ukv_chan9.grib2")) | ||
| cube = load_cube(path) | ||
| # Pay particular attention to the orientation. | ||
| self.assertIsNot(cube.data[0, 0], ma.masked) | ||
| self.assertIs(cube.data[-1, 0], ma.masked) | ||
| self.assertIs(cube.data[0, -1], ma.masked) | ||
| self.assertIs(cube.data[-1, -1], ma.masked) | ||
| x = cube.coord("projection_x_coordinate").points | ||
| y = cube.coord("projection_y_coordinate").points | ||
| self.assertGreater(x[0], x[-1]) # Decreasing X coordinate | ||
| self.assertLess(y[0], y[-1]) # Increasing Y coordinate | ||
| # Check everything else. | ||
| self.assertCMLApproxData(cube) | ||
|
|
||
|
|
||
| class TestGDT30(tests.IrisGribTest): | ||
| def test_lambert(self): | ||
| path = tests.get_data_path(("GRIB", "lambert", "lambert.grib2")) | ||
| cube = load_cube(path) | ||
| self.assertCMLApproxData(cube) | ||
|
|
||
|
|
||
| class TestGDT40(tests.IrisGribTest): | ||
| def test_regular(self): | ||
| path = tests.get_data_path(("GRIB", "gaussian", "regular_gg.grib2")) | ||
| cube = load_cube(path) | ||
| self.assertCMLApproxData(cube) | ||
|
|
||
| def test_reduced(self): | ||
| path = tests.get_data_path(("GRIB", "reduced", "reduced_gg.grib2")) | ||
| cube = load_cube(path) | ||
| self.assertCMLApproxData(cube) | ||
|
|
||
|
|
||
| class TestDRT3(tests.IrisGribTest): | ||
| def test_grid_complex_spatial_differencing(self): | ||
| path = tests.get_data_path( | ||
| ("GRIB", "missing_values", "missing_values.grib2") | ||
| ) | ||
| cube = load_cube(path) | ||
| self.assertCMLApproxData(cube) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| tests.main() |
74 changes: 74 additions & 0 deletions
74
iris_grib/tests/integration/load_convert/test_product_definition_section.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,74 @@ | ||
| # Copyright iris-grib contributors | ||
| # | ||
| # This file is part of iris-grib and is released under the LGPL license. | ||
| # See COPYING and COPYING.LESSER in the root of the repository for full | ||
| # licensing details. | ||
| """ | ||
| Integration tests for loading various production definitions. | ||
|
|
||
| """ | ||
|
|
||
| # Import iris_grib.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris_grib.tests as tests | ||
|
|
||
|
|
||
| from iris import load_cube | ||
|
|
||
|
|
||
| class TestPDT8(tests.IrisGribTest): | ||
| def setUp(self): | ||
| # Load from the test file. | ||
| file_path = tests.get_data_path( | ||
| ("GRIB", "time_processed", "time_bound.grib2") | ||
| ) | ||
| self.cube = load_cube(file_path) | ||
|
|
||
| def test_coords(self): | ||
| # Check the result has main coordinates as expected. | ||
| for name, shape, is_bounded in [ | ||
| ("forecast_reference_time", (1,), False), | ||
| ("time", (1,), True), | ||
| ("forecast_period", (1,), True), | ||
| ("pressure", (1,), False), | ||
| ("latitude", (73,), False), | ||
| ("longitude", (96,), False), | ||
| ]: | ||
| coords = self.cube.coords(name) | ||
| self.assertEqual( | ||
| len(coords), | ||
| 1, | ||
| "expected one {!r} coord, found {}".format(name, len(coords)), | ||
| ) | ||
| (coord,) = coords | ||
| self.assertEqual( | ||
| coord.shape, | ||
| shape, | ||
| "coord {!r} shape is {} instead of {!r}.".format( | ||
| name, coord.shape, shape | ||
| ), | ||
| ) | ||
| self.assertEqual( | ||
| coord.has_bounds(), | ||
| is_bounded, | ||
| "coord {!r} has_bounds={}, expected {}.".format( | ||
| name, coord.has_bounds(), is_bounded | ||
| ), | ||
| ) | ||
|
|
||
| def test_cell_method(self): | ||
| # Check the result has the expected cell method. | ||
| cell_methods = self.cube.cell_methods | ||
| self.assertEqual( | ||
| len(cell_methods), | ||
| 1, | ||
| "result has {} cell methods, expected one.".format( | ||
| len(cell_methods) | ||
| ), | ||
| ) | ||
| (cell_method,) = cell_methods | ||
| self.assertEqual(cell_method.coord_names, ("time",)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tests.main() |
140 changes: 140 additions & 0 deletions
140
iris_grib/tests/integration/round_trip/test_grid_definition_section.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,140 @@ | ||
| # Copyright iris-grib contributors | ||
| # | ||
| # This file is part of iris-grib and is released under the LGPL license. | ||
| # See COPYING and COPYING.LESSER in the root of the repository for full | ||
| # licensing details. | ||
| """ | ||
| Integration test for round-trip loading and saving of various grids. | ||
|
|
||
| """ | ||
|
|
||
| # import iris_grib.tests first so that some things can be initialised | ||
| # before importing anything else. | ||
| import iris_grib.tests as tests | ||
|
|
||
| from iris import load_cube, save | ||
| from iris.coord_systems import RotatedGeogCS | ||
| from iris.fileformats.pp import EARTH_RADIUS as UM_DEFAULT_EARTH_RADIUS | ||
| from iris.util import is_regular | ||
|
|
||
| from iris.tests import TestGribMessage | ||
|
|
||
| from iris_grib.grib_phenom_translation import GRIBCode | ||
|
|
||
|
|
||
| class TestGDT5(TestGribMessage): | ||
| def test_save_load(self): | ||
| # Load sample UKV data (variable-resolution rotated grid). | ||
| path = tests.get_data_path(("PP", "ukV1", "ukVpmslont.pp")) | ||
| cube = load_cube(path) | ||
|
|
||
| # Extract a single 2D field, for simplicity. | ||
| self.assertEqual(cube.ndim, 3) | ||
| self.assertEqual(cube.coord_dims("time"), (0,)) | ||
| cube = cube[0] | ||
|
|
||
| # Check that it has a rotated-pole variable-spaced grid, as expected. | ||
| x_coord = cube.coord(axis="x") | ||
| self.assertIsInstance(x_coord.coord_system, RotatedGeogCS) | ||
| self.assertFalse(is_regular(x_coord)) | ||
|
|
||
| # Write to temporary file, check that key contents are in the file, | ||
| # then load back in. | ||
| with self.temp_filename("ukv_sample.grib2") as temp_file_path: | ||
| save(cube, temp_file_path) | ||
|
|
||
| # Check that various aspects of the saved file are as expected. | ||
| expect_values = ( | ||
| (0, "editionNumber", 2), | ||
| (3, "gridDefinitionTemplateNumber", 5), | ||
| (3, "Ni", cube.shape[-1]), | ||
| (3, "Nj", cube.shape[-2]), | ||
| (3, "shapeOfTheEarth", 1), | ||
| ( | ||
| 3, | ||
| "scaledValueOfRadiusOfSphericalEarth", | ||
| int(UM_DEFAULT_EARTH_RADIUS), | ||
| ), | ||
| (3, "resolutionAndComponentFlags", 0), | ||
| (3, "latitudeOfSouthernPole", -37500000), | ||
| (3, "longitudeOfSouthernPole", 357500000), | ||
| (3, "angleOfRotation", 0), | ||
| ) | ||
| self.assertGribMessageContents(temp_file_path, expect_values) | ||
|
|
||
| # Load the Grib file back into a new cube. | ||
| cube_loaded_from_saved = load_cube(temp_file_path) | ||
| # Also load data, before the temporary file gets deleted. | ||
| cube_loaded_from_saved.data | ||
|
|
||
| # The re-loaded result will not match the original in every respect: | ||
| # * cube attributes are discarded | ||
| # * horizontal coordinates are rounded to an integer representation | ||
| # * bounds on horizontal coords are lost | ||
| # Thus the following "equivalence tests" are rather piecemeal.. | ||
|
|
||
| # Check those re-loaded properties which should match the original. | ||
| for test_cube in (cube, cube_loaded_from_saved): | ||
| self.assertEqual( | ||
| test_cube.standard_name, "air_pressure_at_sea_level" | ||
| ) | ||
| self.assertEqual(test_cube.units, "Pa") | ||
| self.assertEqual(test_cube.shape, (928, 744)) | ||
| self.assertEqual(test_cube.cell_methods, ()) | ||
|
|
||
| # Check only the GRIB_PARAM attribute exists on the re-loaded cube. | ||
| # Note: this does *not* match the original, but is as expected. | ||
| self.assertEqual( | ||
| cube_loaded_from_saved.attributes, | ||
| {"GRIB_PARAM": GRIBCode("GRIB2:d000c003n001")}, | ||
| ) | ||
|
|
||
| # Now remaining to check: coordinates + data... | ||
|
|
||
| # Check they have all the same coordinates. | ||
| co_names = [coord.name() for coord in cube.coords()] | ||
| co_names_reload = [ | ||
| coord.name() for coord in cube_loaded_from_saved.coords() | ||
| ] | ||
| self.assertEqual(sorted(co_names_reload), sorted(co_names)) | ||
|
|
||
| # Check all the coordinates. | ||
| for coord_name in co_names: | ||
| try: | ||
| co_orig = cube.coord(coord_name) | ||
| co_load = cube_loaded_from_saved.coord(coord_name) | ||
|
|
||
| # Check shape. | ||
| self.assertEqual( | ||
| co_load.shape, | ||
| co_orig.shape, | ||
| 'Shape of re-loaded "{}" coord is {} ' | ||
| "instead of {}".format( | ||
| coord_name, co_load.shape, co_orig.shape | ||
| ), | ||
| ) | ||
|
|
||
| # Check coordinate points equal, within a tolerance. | ||
| self.assertArrayAllClose( | ||
| co_load.points, co_orig.points, rtol=1.0e-6 | ||
| ) | ||
|
|
||
| # Check all coords are unbounded. | ||
| # (NOTE: this is not so for the original X and Y coordinates, | ||
| # but Grib does not store those bounds). | ||
| self.assertIsNone(co_load.bounds) | ||
|
|
||
| except AssertionError as err: | ||
| self.assertTrue( | ||
| False, | ||
| 'Failed on coordinate "{}" : {}'.format( | ||
| coord_name, str(err) | ||
| ), | ||
| ) | ||
|
|
||
| # Check that main data array also matches. | ||
| self.assertArrayAllClose(cube.data, cube_loaded_from_saved.data) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tests.main() | ||
81 changes: 81 additions & 0 deletions
81
iris_grib/tests/integration/round_trip/test_product_definition_section.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,81 @@ | ||
| # Copyright iris-grib contributors | ||
| # | ||
| # This file is part of iris-grib and is released under the LGPL license. | ||
| # See COPYING and COPYING.LESSER in the root of the repository for full | ||
| # licensing details. | ||
| """ | ||
| Integration tests for round-trip loading and saving various product | ||
| definitions. | ||
|
|
||
| """ | ||
|
|
||
| # Import iris_grib.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris_grib.tests as tests | ||
|
|
||
| from cf_units import Unit | ||
| from iris import load_cube, save | ||
| import iris.coords | ||
| import iris.coord_systems | ||
|
|
||
| from iris.tests import TestGribMessage | ||
| import iris.tests.stock as stock | ||
|
|
||
| from iris_grib.grib_phenom_translation import GRIBCode | ||
|
|
||
|
|
||
| class TestPDT11(TestGribMessage): | ||
| def test_perturbation(self): | ||
| path = tests.get_data_path( | ||
| ("NetCDF", "global", "xyt", "SMALL_hires_wind_u_for_ipcc4.nc") | ||
| ) | ||
| cube = load_cube(path) | ||
| # trim to 1 time and regular lats | ||
| cube = cube[0, 12:144, :] | ||
| crs = iris.coord_systems.GeogCS(6371229) | ||
| cube.coord("latitude").coord_system = crs | ||
| cube.coord("longitude").coord_system = crs | ||
| # add a realization coordinate | ||
| cube.add_aux_coord( | ||
| iris.coords.DimCoord( | ||
| points=1, standard_name="realization", units="1" | ||
| ) | ||
| ) | ||
| with self.temp_filename("testPDT11.GRIB2") as temp_file_path: | ||
| iris.save(cube, temp_file_path) | ||
|
|
||
| # Check that various aspects of the saved file are as expected. | ||
| expect_values = ( | ||
| (0, "editionNumber", 2), | ||
| (3, "gridDefinitionTemplateNumber", 0), | ||
| (4, "productDefinitionTemplateNumber", 11), | ||
| (4, "perturbationNumber", 1), | ||
| (4, "typeOfStatisticalProcessing", 0), | ||
| (4, "numberOfForecastsInEnsemble", 255), | ||
| ) | ||
| self.assertGribMessageContents(temp_file_path, expect_values) | ||
|
|
||
|
|
||
| class TestPDT40(tests.IrisTest): | ||
| def test_save_load(self): | ||
| cube = stock.lat_lon_cube() | ||
| cube.rename("atmosphere_mole_content_of_ozone") | ||
| cube.units = Unit("Dobson") | ||
| tcoord = iris.coords.DimCoord( | ||
| 23, "time", units=Unit("days since epoch", calendar="standard") | ||
| ) | ||
| fpcoord = iris.coords.DimCoord(24, "forecast_period", | ||
| units=Unit("hours")) | ||
| cube.add_aux_coord(tcoord) | ||
| cube.add_aux_coord(fpcoord) | ||
| cube.attributes["WMO_constituent_type"] = 0 | ||
| cube.attributes["GRIB_PARAM"] = GRIBCode("GRIB2:d000c014n000") | ||
|
|
||
| with self.temp_filename("test_grib_pdt40.grib2") as temp_file_path: | ||
| save(cube, temp_file_path) | ||
| loaded = load_cube(temp_file_path) | ||
| self.assertEqual(loaded.attributes, cube.attributes) | ||
|
|
||
|
|
||
| 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.
This cube loads in with a shape of (928, 744).
6 years ago, eccodes had a bug such that it could only save square data. Thus, in Iris this tests square off the data before saving it (see here). I have removed that as the bug has been fixed (many years ago!)