Example #1
0
    def test_name2_field(self):
        filepath = tests.get_data_path(("NAME", "NAMEII_field.txt"))
        name_cubes = iris.load(filepath)

        # There is a known load/save problem with numerous
        # gribapi/eccodes versions and
        # zero only data, where min == max.
        # This may be a problem with data scaling.
        for i, name_cube in enumerate(name_cubes):
            data = name_cube.data
            if np.min(data) == np.max(data):
                msg = (
                    'NAMEII cube #{}, "{}" has empty data : '
                    "SKIPPING test for this cube, as save/load will "
                    "not currently work."
                )
                warnings.warn(msg.format(i, name_cube.name()))
                continue

            with self.temp_filename(".grib2") as temp_filename:
                iris.save(name_cube, temp_filename)
                grib_cube = iris.load_cube(temp_filename, callback=name_cb)
                self.check_common(name_cube, grib_cube)
                self.assertCML(
                    grib_cube,
                    self.get_result_path(
                        (
                            "integration",
                            "name_grib",
                            "NAMEII",
                            "{}_{}.cml".format(i, name_cube.name()),
                        )
                    ),
                )
Example #2
0
 def setUp(self):
     filename = tests.get_data_path(('GRIB', 'umukv', 'ukv_chan9.grib2'))
     with open(filename, 'rb') as grib_fh:
         self.grib_id = gribapi.grib_new_from_file(grib_fh)
     self.keys = ['satelliteSeries', 'satelliteNumber', 'instrumentType',
                  'scaleFactorOfCentralWaveNumber',
                  'scaledValueOfCentralWaveNumber']
Example #3
0
 def test_time_mean(self):
     # This test for time-mean fields also tests negative forecast time.
     source_grib = tests.get_data_path(
         ("GRIB", "time_processed", "time_bound.grib2"))
     cubes = iris.load(source_grib)
     expect_diffs = {
         "totalLength": (21232, 21227),
         "productionStatusOfProcessedData": (0, 255),
         "scaleFactorOfRadiusOfSphericalEarth": (MDI, 0),
         "shapeOfTheEarth": (0, 1),
         "scaledValueOfRadiusOfSphericalEarth": (MDI, 6367470),
         "scaledValueOfEarthMajorAxis": (MDI, 0),
         "scaleFactorOfEarthMajorAxis": (MDI, 0),
         "scaledValueOfEarthMinorAxis": (MDI, 0),
         "scaleFactorOfEarthMinorAxis": (MDI, 0),
         "longitudeOfLastGridPoint": (356249908, 356249809),
         "latitudeOfLastGridPoint": (-89999938, -89999944),
         "typeOfGeneratingProcess": (0, 255),
         "generatingProcessIdentifier": (128, 255),
         "typeOfTimeIncrement": (2, 255),
     }
     self.skip_keys.append("stepType")
     self.skip_keys.append("stepTypeInternal")
     with self.temp_filename(suffix=".grib2") as temp_file_path:
         iris.save(cubes, temp_file_path)
         self.assertGribMessageDifference(
             source_grib,
             temp_file_path,
             expect_diffs,
             self.skip_keys,
             skip_sections=[2],
         )
Example #4
0
 def test_gdt40_computed(self):
     fname = tests.get_data_path(('GRIB', 'gaussian', 'regular_gg.grib2'))
     with open(fname, 'rb') as grib_fh:
         self.grib_id = gribapi.grib_new_from_file(grib_fh)
         section = Section(self.grib_id, None, [])
     latitudes = section.get_computed_key('latitudes')
     self.assertTrue(88.55 < latitudes[0] < 88.59)
Example #5
0
 def test_latlon_forecast_plev(self):
     source_grib = tests.get_data_path(("GRIB", "uk_t", "uk_t.grib2"))
     cubes = iris.load(source_grib)
     with self.temp_filename(suffix=".grib2") as temp_file_path:
         iris.save(cubes, temp_file_path)
         expect_diffs = {
             "totalLength": (4837, 4832),
             "productionStatusOfProcessedData": (0, 255),
             "scaleFactorOfRadiusOfSphericalEarth": (MDI, 0),
             "shapeOfTheEarth": (0, 1),
             "scaledValueOfRadiusOfSphericalEarth": (MDI, 6367470),
             "scaledValueOfEarthMajorAxis": (MDI, 0),
             "scaleFactorOfEarthMajorAxis": (MDI, 0),
             "scaledValueOfEarthMinorAxis": (MDI, 0),
             "scaleFactorOfEarthMinorAxis": (MDI, 0),
             "typeOfGeneratingProcess": (0, 255),
             "generatingProcessIdentifier": (128, 255),
         }
         self.assertGribMessageDifference(
             source_grib,
             temp_file_path,
             expect_diffs,
             self.skip_keys,
             skip_sections=[2],
         )
Example #6
0
 def test_rotated_latlon(self):
     source_grib = tests.get_data_path(
         ("GRIB", "rotated_nae_t", "sensible_pole.grib2"))
     cubes = iris.load(source_grib)
     with self.temp_filename(suffix=".grib2") as temp_file_path:
         iris.save(cubes, temp_file_path)
         expect_diffs = {
             "totalLength": (648196, 648191),
             "productionStatusOfProcessedData": (0, 255),
             "scaleFactorOfRadiusOfSphericalEarth": (MDI, 0),
             "shapeOfTheEarth": (0, 1),
             "scaledValueOfRadiusOfSphericalEarth": (MDI, 6367470),
             "scaledValueOfEarthMajorAxis": (MDI, 0),
             "scaleFactorOfEarthMajorAxis": (MDI, 0),
             "scaledValueOfEarthMinorAxis": (MDI, 0),
             "scaleFactorOfEarthMinorAxis": (MDI, 0),
             "longitudeOfLastGridPoint": (392109982, 32106370),
             "latitudeOfLastGridPoint": (19419996, 19419285),
             "typeOfGeneratingProcess": (0, 255),
             "generatingProcessIdentifier": (128, 255),
         }
         self.assertGribMessageDifference(
             source_grib,
             temp_file_path,
             expect_diffs,
             self.skip_keys,
             skip_sections=[2],
         )
Example #7
0
 def test_time_and_forecast_period_round_trip(self):
     pp_path = tests.get_data_path(
         ("PP", "meanMaxMin", "200806081200__qwpb.T24.pp"))
     # Choose the first time-bounded Cube in the PP dataset.
     original = [
         cube for cube in iris.load(pp_path)
         if cube.coord("time").has_bounds()
     ][0]
     # Save it to GRIB2 and re-load.
     with self.temp_filename(".grib2") as grib_path:
         iris.save(original, grib_path)
         from_grib = iris.load_cube(grib_path)
         # Avoid the downcasting warning when saving to PP.
         from_grib.data = from_grib.data.astype("f4")
     # Re-save to PP and re-load.
     with self.temp_filename(".pp") as pp_path:
         iris.save(from_grib, pp_path)
         from_pp = iris.load_cube(pp_path)
     self.assertEqual(original.coord("time"), from_grib.coord("time"))
     self.assertEqual(
         original.coord("forecast_period"),
         from_grib.coord("forecast_period"),
     )
     self.assertEqual(original.coord("time"), from_pp.coord("time"))
     self.assertEqual(original.coord("forecast_period"),
                      from_pp.coord("forecast_period"))
Example #8
0
 def test_reduced_raw(self):
     # Loading a GRIB message defined on a reduced grid without
     # interpolating to a regular grid.
     gribfile = tests.get_data_path(
         ("GRIB", "reduced", "reduced_gg.grib2"))
     grib_generator = load_cubes(gribfile)
     self.assertCML(next(grib_generator))
    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)
Example #10
0
 def test_release_file(self):
     filename = tests.get_data_path(('GRIB', '3_layer_viz',
                                     '3_layer.grib2'))
     my_file = open(filename)
     self.patch('__builtin__.open', mock.Mock(return_value=my_file))
     messages = list(GribMessage.messages_from_filename(filename))
     self.assertFalse(my_file.closed)
     del messages
     self.assertTrue(my_file.closed)
Example #11
0
 def test_release_file(self):
     filename = tests.get_data_path(
         ('GRIB', '3_layer_viz', '3_layer.grib2'))
     my_file = open(filename)
     self.patch('__builtin__.open', mock.Mock(return_value=my_file))
     messages = list(GribMessage.messages_from_filename(filename))
     self.assertFalse(my_file.closed)
     del messages
     self.assertTrue(my_file.closed)
Example #12
0
 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)
Example #13
0
 def test_bounded_level(self):
     cube = iris.load_cube(
         tests.get_data_path(("GRIB", "uk_t", "uk_t.grib2")))
     with self.temp_filename(".grib2") as testfile:
         iris.save(cube, testfile)
         with open(testfile, "rb") as saved_file:
             g = gribapi.grib_new_from_file(saved_file)
             self.assertEqual(
                 gribapi.grib_get_double(g,
                                         "scaledValueOfFirstFixedSurface"),
                 0.0,
             )
             self.assertEqual(
                 gribapi.grib_get_double(g,
                                         "scaledValueOfSecondFixedSurface"),
                 2147483647.0,
             )
Example #14
0
    def test_name3_field(self):
        filepath = tests.get_data_path(("NAME", "NAMEIII_field.txt"))
        name_cubes = iris.load(filepath)
        for i, name_cube in enumerate(name_cubes):
            with self.temp_filename(".grib2") as temp_filename:
                iris.save(name_cube, temp_filename)
                grib_cube = iris.load_cube(temp_filename, callback=name_cb)

                self.check_common(name_cube, grib_cube)
                self.assertCML(
                    grib_cube,
                    self.get_result_path(
                        (
                            "integration",
                            "name_grib",
                            "NAMEIII",
                            "{}_{}.cml".format(i, name_cube.name()),
                        )
                    ),
                )
Example #15
0
 def test_polar_stereo_grib2_grid_definition(self):
     cube = iris.load_cube(
         tests.get_data_path((
             "GRIB",
             "polar_stereo",
             "CMC_glb_TMP_ISBL_1015_ps30km_2013052000_P006.grib2",
         )))
     self.assertEqual(cube.shape, (200, 247))
     pxc = cube.coord("projection_x_coordinate")
     self.assertAlmostEqual(pxc.points.max(), 4769905.5125, places=4)
     self.assertAlmostEqual(pxc.points.min(), -2610094.4875, places=4)
     pyc = cube.coord("projection_y_coordinate")
     self.assertAlmostEqual(pyc.points.max(), -216.1459, places=4)
     self.assertAlmostEqual(pyc.points.min(), -5970216.1459, places=4)
     self.assertEqual(pyc.coord_system, pxc.coord_system)
     self.assertEqual(pyc.coord_system.grid_mapping_name, "stereographic")
     self.assertEqual(pyc.coord_system.central_lat, 90.0)
     self.assertEqual(pyc.coord_system.central_lon, 249.0)
     self.assertEqual(pyc.coord_system.false_easting, 0.0)
     self.assertEqual(pyc.coord_system.false_northing, 0.0)
     self.assertEqual(pyc.coord_system.true_scale_lat, 60.0)
Example #16
0
 def test_load_rotated(self):
     cubes = iris.load(
         tests.get_data_path(("GRIB", "rotated_uk", "uk_wrongparam.grib1")))
     self.assertCML(cubes, _RESULTDIR_PREFIX + ("rotated.cml", ))
Example #17
0
 def test_load_time_processed(self):
     cubes = iris.load(
         tests.get_data_path(
             ("GRIB", "time_processed", "time_bound.grib2")))
     self.assertCML(cubes, _RESULTDIR_PREFIX + ("time_bound_grib2.cml", ))
Example #18
0
 def _old_compat_load(name):
     filepath = tests.get_data_path(("GRIB", "ij_directions", name))
     cube = iris.load_cube(filepath)
     return cube
Example #19
0
 def _old_compat_load(name):
     filepath = tests.get_data_path(("GRIB", "shape_of_earth", name))
     cube = iris.load_cube(filepath)
     return cube
Example #20
0
 def test_reduced(self):
     path = tests.get_data_path(("GRIB", "reduced", "reduced_gg.grib2"))
     cube = load_cube(path)
     self.assertCMLApproxData(cube)
Example #21
0
 def test_reduced_gg(self):
     cube = iris.load_cube(
         tests.get_data_path(("GRIB", "reduced", "reduced_gg.grib2")))
     self.assertCML(cube, _RESULTDIR_PREFIX + ("reduced_gg_grib2.cml", ))
Example #22
0
 def setUp(self):
     filename = tests.get_data_path(('GRIB', 'uk_t', 'uk_t.grib2'))
     with open(filename, 'rb') as grib_fh:
         self.grib_id = gribapi.grib_new_from_file(grib_fh)
Example #23
0
 def test_polar_stereo_grib1(self):
     cube = iris.load_cube(
         tests.get_data_path(
             ("GRIB", "polar_stereo", "ST4.2013052210.01h")))
     self.assertCML(cube, _RESULTDIR_PREFIX + ("polar_stereo_grib1.cml", ))
Example #24
0
 def test_regular_data(self):
     filename = tests.get_data_path(
         ('GRIB', 'gaussian', 'regular_gg.grib1'))
     messages = list(_load_generate(filename))
     self.assertTrue(is_lazy_data(messages[0]._data))
Example #25
0
 def test_reduced_data(self):
     filename = tests.get_data_path(('GRIB', 'reduced', 'reduced_ll.grib1'))
     messages = list(_load_generate(filename))
     self.assertTrue(is_lazy_data(messages[0]._data))
Example #26
0
 def test_no_forecast_period(self):
     # The stock cube has a non-compliant forecast_period.
     fname = tests.get_data_path(('GRIB', 'global_t', 'global.grib2'))
     [cube] = load_cubes(fname)
     cube.remove_coord("forecast_period")
     self._test(cube)
Example #27
0
 def test_no_forecast_period(self):
     # The stock cube has a non-compliant forecast_period.
     fname = tests.get_data_path(('GRIB', 'global_t', 'global.grib2'))
     [cube] = load_cubes(fname)
     cube.remove_coord("forecast_period")
     self._test(cube)
Example #28
0
 def test_lambert_grib2(self):
     cube = iris.load_cube(
         tests.get_data_path(("GRIB", "lambert", "lambert.grib2")))
     self.assertCML(cube, _RESULTDIR_PREFIX + ("lambert_grib2.cml", ))
Example #29
0
 def test_load_3_layer(self):
     cubes = iris.load(
         tests.get_data_path(("GRIB", "3_layer_viz", "3_layer.grib2")))
     cubes = iris.cube.CubeList([cubes[1], cubes[0], cubes[2]])
     self.assertCML(cubes, _RESULTDIR_PREFIX + ("3_layer.cml", ))
Example #30
0
 def test_reduced_data(self):
     filename = tests.get_data_path(('GRIB', 'reduced',
                                     'reduced_ll.grib1'))
     messages = list(_load_generate(filename))
     self.assertTrue(is_lazy_data(messages[0]._data))
Example #31
0
 def test_load_masked(self):
     gribfile = tests.get_data_path(
         ("GRIB", "missing_values", "missing_values.grib2"))
     cubes = iris.load(gribfile)
     self.assertCML(cubes,
                    _RESULTDIR_PREFIX + ("missing_values_grib2.cml", ))
Example #32
0
 def test_regular_data(self):
     filename = tests.get_data_path(('GRIB', 'gaussian',
                                     'regular_gg.grib1'))
     messages = list(_load_generate(filename))
     self.assertTrue(is_lazy_data(messages[0]._data))
Example #33
0
 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)
Example #34
0
 def test(self):
     filename = tests.get_data_path(('GRIB', '3_layer_viz',
                                     '3_layer.grib2'))
     messages = list(GribMessage.messages_from_filename(filename))
     self.assertEqual(len(messages), 3)
Example #35
0
 def test_regular_gg_grib2(self):
     cube = iris.load_cube(
         tests.get_data_path(("GRIB", "gaussian", "regular_gg.grib2")))
     self.assertCML(cube, _RESULTDIR_PREFIX + ("regular_gg_grib2.cml", ))
Example #36
0
 def setUp(self):
     self.path = tests.get_data_path(("GRIB", "fp_units", "hours.grib2"))