def _run_timetests(self, test_set):
        # Check the unit-handling for given units-codes and editions.

        # Operates on lists of cases for various time-units and grib-editions.
        # Format: (edition, code, expected-exception,
        #          equivalent-seconds, description-string)
        with mock.patch('iris_grib.gribapi', _mock_gribapi):
            for test_controls in test_set:
                (grib_edition, timeunit_codenum, expected_error, timeunit_secs,
                 timeunit_str) = test_controls

                # Construct a suitable fake test message.
                message = FakeGribMessage(edition=grib_edition,
                                          time_code=timeunit_codenum)

                if expected_error:
                    # Expect GribWrapper construction to fail.
                    with self.assertRaises(type(expected_error)) as ar_context:
                        msg = iris_grib.GribWrapper(message)
                    self.assertEqual(ar_context.exception.args,
                                     expected_error.args)
                    continue

                # 'ELSE'...
                # Expect the wrapper construction to work.
                # Make a GribWrapper object and test it.
                wrapped_msg = iris_grib.GribWrapper(message)

                # Check the units string.
                forecast_timeunit = wrapped_msg._forecastTimeUnit
                self.assertEqual(
                    forecast_timeunit, timeunit_str,
                    'Bad unit string for edition={ed:01d}, '
                    'unitcode={code:01d} : '
                    'expected="{wanted}" GOT="{got}"'.format(
                        ed=grib_edition,
                        code=timeunit_codenum,
                        wanted=timeunit_str,
                        got=forecast_timeunit))

                # Check the data-starttime calculation.
                interval_start_to_end = (wrapped_msg._phenomenonDateTime -
                                         wrapped_msg._referenceDateTime)
                if grib_edition == 1:
                    interval_from_units = wrapped_msg.P1
                else:
                    interval_from_units = wrapped_msg.forecastTime
                interval_from_units *= datetime.timedelta(0, timeunit_secs)
                self.assertEqual(
                    interval_start_to_end, interval_from_units,
                    'Inconsistent start time offset for edition={ed:01d}, '
                    'unitcode={code:01d} : '
                    'from-unit="{unit_str}" '
                    'from-phenom-minus-ref="{e2e_str}"'.format(
                        ed=grib_edition,
                        code=timeunit_codenum,
                        unit_str=interval_from_units,
                        e2e_str=interval_start_to_end))
Example #2
0
 def cube_from_message(self, grib):
     # Parameter translation now uses the GribWrapper, so we must convert
     # the Mock-based fake message to a FakeGribMessage.
     with mock.patch("iris_grib.gribapi", _mock_gribapi):
         grib_message = FakeGribMessage(**grib.__dict__)
         wrapped_msg = iris_grib.GribWrapper(grib_message)
         cube, _, _ = iris.fileformats.rules._make_cube(
             wrapped_msg, iris_grib._grib1_load_rules.grib1_convert)
     return cube