Example #1
0
 def test_unsupported_quasi_regular__number_of_octets(self):
     message = _make_test_message(
         {3: {'sourceOfGridDefinition': 0,
              'numberOfOctectsForNumberOfPoints': 1},
          6: SECTION_6_NO_BITMAP})
     with self.assertRaisesRegexp(TranslationError, 'quasi-regular'):
         message.data
Example #2
0
 def test_unsupported_quasi_regular__interpretation(self):
     message = _make_test_message(
         {3: {'sourceOfGridDefinition': 0,
              'numberOfOctectsForNumberOfPoints': 0,
              'interpretationOfNumberOfPoints': 1}})
     with self.assertRaisesRegexp(TranslationError, 'quasi-regular'):
         message.data
Example #3
0
 def test_unsupported_scanning_mode(self):
     message = _make_test_message({
         3: self.section_3(1),
         6: SECTION_6_NO_BITMAP
     })
     with self.assertRaisesRegexp(TranslationError, 'scanning mode'):
         message.data
Example #4
0
 def test(self):
     sections = [{'discipline': mock.sentinel.discipline},       # section 0
                 {'centre': 'ecmf',                              # section 1
                  'tablesVersion': mock.sentinel.tablesVersion},
                 None,                                           # section 2
                 mock.sentinel.grid_definition_section,          # section 3
                 mock.sentinel.product_definition_section,       # section 4
                 mock.sentinel.data_representation_section,      # section 5
                 mock.sentinel.bitmap_section]                   # section 6
     field = _make_test_message(sections)
     metadata = {'factories': [], 'references': [],
                 'standard_name': None,
                 'long_name': None, 'units': None, 'attributes': {},
                 'cell_methods': [], 'dim_coords_and_dims': [],
                 'aux_coords_and_dims': []}
     expected = copy.deepcopy(metadata)
     centre = 'European Centre for Medium Range Weather Forecasts'
     expected['attributes'] = {'centre': centre}
     # The call being tested.
     grib2_convert(field, metadata)
     self.assertEqual(metadata, expected)
     this = iris.fileformats.grib._load_convert
     this.reference_time_coord.assert_called_with(sections[1])
     this.grid_definition_section.assert_called_with(sections[3],
                                                     expected)
     args = (sections[4], expected, sections[0]['discipline'],
             sections[1]['tablesVersion'], None)
     this.product_definition_section.assert_called_with(*args)
Example #5
0
 def test_unsupported_template(self):
     message = _make_test_message(
         {3: {'sourceOfGridDefinition': 0,
              'numberOfOctectsForNumberOfPoints': 0,
              'interpretationOfNumberOfPoints': 0,
              'gridDefinitionTemplateNumber': 2}})
     with self.assertRaisesRegexp(TranslationError, 'template'):
         message.data
Example #6
0
 def test_unsupported_quasi_regular__number_of_octets(self):
     message = _make_test_message(
         {3: {'sourceOfGridDefinition': 0,
              'numberOfOctectsForNumberOfPoints': 1,
              'gridDefinitionTemplateNumber': 0},
          6: SECTION_6_NO_BITMAP})
     with self.assertRaisesRegexp(TranslationError, 'quasi-regular'):
         message.data
Example #7
0
 def test_bitmap__invalid_indicator(self):
     values = np.arange(12)
     message = _make_test_message({3: self._section_3,
                                   6: {'bitMapIndicator': 100,
                                       'bitmap': None},
                                   7: {'codedValues': values}})
     with self.assertRaisesRegexp(TranslationError, 'unsupported bitmap'):
         message.data.ndarray()
Example #8
0
 def test(self):
     message = _make_test_message(
         {3: _example_section_3(999, 0),
          6: SECTION_6_NO_BITMAP,
          7: {'codedValues': np.arange(12)}})
     with self.assertRaisesRegexp(TranslationError,
                                  'template 999 is not supported'):
         data = message.data
Example #9
0
 def test_bitmap__invalid_indicator(self):
     values = np.arange(12)
     message = _make_test_message({3: self._section_3,
                                   6: {'bitMapIndicator': 100,
                                       'bitmap': None},
                                   7: {'codedValues': values}})
     with self.assertRaisesRegexp(TranslationError, 'unsupported bitmap'):
         as_concrete_data(message.data, nans_replacement=ma.masked)
Example #10
0
 def test(self):
     message = _make_test_message(
         {3: _example_section_3(999, 0),
          6: SECTION_6_NO_BITMAP,
          7: {'codedValues': np.arange(12)}})
     with self.assertRaisesRegexp(TranslationError,
                                  'template 999 is not supported'):
         data = message.data
Example #11
0
 def test_bitmap_unsupported(self):
     # bitMapIndicator in range 1-254.
     # Note that bitMapIndicator = 1-253 and bitMapIndicator = 254 mean two
     # different things, but load_convert treats them identically.
     message = _make_test_message({6: {'bitMapIndicator': 100,
                                       'bitmap': None}})
     with self.assertRaisesRegexp(TranslationError, 'unsupported bitmap'):
         bitmap_section(message.sections[6])
Example #12
0
 def test_bitmap__invalid_indicator(self):
     values = np.arange(12)
     message = _make_test_message({3: self._section_3,
                                   6: {'bitMapIndicator': 100,
                                       'bitmap': None},
                                   7: {'codedValues': values}})
     with self.assertRaisesRegexp(TranslationError, 'unsupported bitmap'):
         message.data.ndarray()
Example #13
0
 def test_unsupported_template(self):
     message = _make_test_message(
         {3: {'sourceOfGridDefinition': 0,
              'numberOfOctectsForNumberOfPoints': 0,
              'interpretationOfNumberOfPoints': 0,
              'gridDefinitionTemplateNumber': 2}})
     with self.assertRaisesRegexp(TranslationError, 'template'):
         message.data
Example #14
0
 def test_no_bitmap(self):
     values = np.arange(12)
     message = _make_test_message({3: self._section_3,
                                   6: SECTION_6_NO_BITMAP,
                                   7: {'codedValues': values}})
     result = message.data.ndarray()
     expected = values.reshape(self.shape)
     self.assertEqual(result.shape, self.shape)
     self.assertArrayEqual(result, expected)
Example #15
0
 def test_unsupported_grid_definition(self):
     message = _make_test_message({
         3: {
             'sourceOfGridDefinition': 1
         },
         6: SECTION_6_NO_BITMAP
     })
     with self.assertRaisesRegexp(TranslationError, 'source'):
         message.data
Example #16
0
 def test_no_bitmap(self):
     values = np.arange(12)
     message = _make_test_message({3: self._section_3,
                                   6: SECTION_6_NO_BITMAP,
                                   7: {'codedValues': values}})
     result = message.data.ndarray()
     expected = values.reshape(self.shape)
     self.assertEqual(result.shape, self.shape)
     self.assertArrayEqual(result, expected)
Example #17
0
 def test_bitmap__shapes_mismatch(self):
     # Test the behaviour where bitmap and codedValues shapes do not match.
     # Too many or too few unmasked values in codedValues will cause this.
     values = np.arange(6)
     message = _make_test_message({3: self._section_3,
                                   6: {'bitMapIndicator': 0,
                                       'bitmap': self.bitmap},
                                   7: {'codedValues': values}})
     with self.assertRaisesRegexp(TranslationError, 'do not match'):
         message.data.masked_array()
Example #18
0
 def test_bitmap__shapes_mismatch(self):
     # Test the behaviour where bitmap and codedValues shapes do not match.
     # Too many or too few unmasked values in codedValues will cause this.
     values = np.arange(6)
     message = _make_test_message({3: self._section_3,
                                   6: {'bitMapIndicator': 0,
                                       'bitmap': self.bitmap},
                                   7: {'codedValues': values}})
     with self.assertRaisesRegexp(TranslationError, 'do not match'):
         message.data.masked_array()
 def test_bitmap_unsupported(self):
     # bitMapIndicator in range 1-254.
     # Note that bitMapIndicator = 1-253 and bitMapIndicator = 254 mean two
     # different things, but load_convert treats them identically.
     message = _make_test_message(
         {6: {
             'bitMapIndicator': 100,
             'bitmap': None
         }})
     with self.assertRaisesRegexp(TranslationError, 'unsupported bitmap'):
         bitmap_section(message.sections[6])
Example #20
0
 def _test(self, scanning_mode):
     message = _make_test_message(
         {3: self.section_3(scanning_mode),
          6: SECTION_6_NO_BITMAP,
          7: {'codedValues': np.arange(12)}})
     data = message.data
     self.assertTrue(is_lazy_data(data))
     self.assertEqual(data.shape, (3, 4))
     self.assertEqual(data.dtype, np.floating)
     self.assertArrayEqual(as_concrete_data(data),
                           np.arange(12).reshape(3, 4))
Example #21
0
 def _test(self, scanning_mode):
     message = _make_test_message(
         {3: self.section_3(scanning_mode),
          6: SECTION_6_NO_BITMAP,
          7: {'codedValues': np.arange(12)}})
     data = message.data
     self.assertIsInstance(data, biggus.Array)
     self.assertEqual(data.shape, (3, 4))
     self.assertEqual(data.dtype, np.floating)
     self.assertIs(data.fill_value, np.nan)
     self.assertArrayEqual(data.ndarray(), np.arange(12).reshape(3, 4))
Example #22
0
 def _test(self, scanning_mode):
     message = _make_test_message(
         {3: self.section_3(scanning_mode),
          6: SECTION_6_NO_BITMAP,
          7: {'codedValues': np.arange(12)}})
     data = message.data
     self.assertIsInstance(data, biggus.Array)
     self.assertEqual(data.shape, (3, 4))
     self.assertEqual(data.dtype, np.floating)
     self.assertIs(data.fill_value, np.nan)
     self.assertArrayEqual(data.ndarray(), np.arange(12).reshape(3, 4))
Example #23
0
 def test_call(self):
     sections = [{'editionNumber': 2}]
     field = _make_test_message(sections)
     this = 'iris.fileformats.grib._load_convert.grib2_convert'
     factory = mock.sentinel.factory
     func = lambda field, metadata: metadata['factories'].append(factory)
     with mock.patch(this, side_effect=func) as grib2_convert:
         # The call being tested.
         result = convert(field)
         self.assertTrue(grib2_convert.called)
         metadata = ([factory], [], None, None, None, {}, [], [], [])
         self.assertEqual(result, metadata)
Example #24
0
 def test_call(self):
     sections = [{'editionNumber': 2}]
     field = _make_test_message(sections)
     this = 'iris.fileformats.grib._load_convert.grib2_convert'
     factory = mock.sentinel.factory
     func = lambda field, metadata: metadata['factories'].append(factory)
     with mock.patch(this, side_effect=func) as grib2_convert:
         # The call being tested.
         result = convert(field)
         self.assertTrue(grib2_convert.called)
         metadata = ([factory], [], None, None, None, {}, [], [], [])
         self.assertEqual(result, metadata)
Example #25
0
 def test_bitmap_present(self):
     # Test the behaviour where bitmap and codedValues shapes
     # are not equal.
     input_values = np.arange(5)
     output_values = np.array([-1, -1, 0, 1, -1, -1, -1, 2, -1, 3, -1, 4])
     message = _make_test_message({3: self._section_3,
                                   6: {'bitMapIndicator': 0,
                                       'bitmap': self.bitmap},
                                   7: {'codedValues': input_values}})
     result = message.data.masked_array()
     expected = np.ma.masked_array(output_values,
                                   np.logical_not(self.bitmap))
     expected = expected.reshape(self.shape)
     self.assertMaskedArrayEqual(result, expected)
Example #26
0
 def test_bitmap__invalid_indicator(self):
     values = np.arange(12)
     message = _make_test_message({
         3: self._section_3,
         6: {
             'bitMapIndicator': 100,
             'bitmap': None
         },
         7: {
             'codedValues': values
         }
     })
     with self.assertRaisesRegexp(TranslationError, 'unsupported bitmap'):
         as_concrete_data(message.data, nans_replacement=ma.masked)
Example #27
0
 def _test(self, scanning_mode):
     message = _make_test_message({
         3: self.section_3(scanning_mode),
         6: SECTION_6_NO_BITMAP,
         7: {
             'codedValues': np.arange(12)
         }
     })
     data = message.data
     self.assertTrue(is_lazy_data(data))
     self.assertEqual(data.shape, (3, 4))
     self.assertEqual(data.dtype, np.floating)
     self.assertArrayEqual(as_concrete_data(data),
                           np.arange(12).reshape(3, 4))
Example #28
0
 def test_bitmap_present(self):
     # Test the behaviour where bitmap and codedValues shapes
     # are not equal.
     input_values = np.arange(5)
     output_values = np.array([-1, -1, 0, 1, -1, -1, -1, 2, -1, 3, -1, 4])
     message = _make_test_message({3: self._section_3,
                                   6: {'bitMapIndicator': 0,
                                       'bitmap': self.bitmap},
                                   7: {'codedValues': input_values}})
     result = message.data.masked_array()
     expected = np.ma.masked_array(output_values,
                                   np.logical_not(self.bitmap))
     expected = expected.reshape(self.shape)
     self.assertMaskedArrayEqual(result, expected)
Example #29
0
 def test_bitmap_present_float_data(self):
     # Test the behaviour where bitmap and codedValues shapes
     # are not equal, and codedValues is float data.
     data_type = np.float32
     input_values = np.arange(5, dtype=np.float32) + 5
     output_values = np.array([-1, -1, 5, 6, -1, -1, -1, 7, -1, 8, -1, 9],
                              dtype=data_type)
     message = _make_test_message({3: self._section_3,
                                   6: {'bitMapIndicator': 0,
                                       'bitmap': self.bitmap},
                                   7: {'codedValues': input_values}})
     result = as_concrete_data(message.data, nans_replacement=ma.masked,
                               result_dtype=data_type)
     expected = ma.masked_array(output_values,
                                np.logical_not(self.bitmap))
     expected = expected.reshape(self.shape)
     self.assertMaskedArrayEqual(result, expected)
Example #30
0
 def test(self):
     sections = [
         {
             'discipline': mock.sentinel.discipline
         },  # section 0
         {
             'centre': 'ecmf',  # section 1
             'tablesVersion': mock.sentinel.tablesVersion
         },
         None,  # section 2
         mock.sentinel.grid_definition_section,  # section 3
         mock.sentinel.product_definition_section,  # section 4
         mock.sentinel.data_representation_section,  # section 5
         mock.sentinel.bitmap_section
     ]  # section 6
     field = _make_test_message(sections)
     metadata = {
         'factories': [],
         'references': [],
         'standard_name': None,
         'long_name': None,
         'units': None,
         'attributes': {},
         'cell_methods': [],
         'dim_coords_and_dims': [],
         'aux_coords_and_dims': []
     }
     expected = copy.deepcopy(metadata)
     centre = 'European Centre for Medium Range Weather Forecasts'
     expected['attributes'] = {'centre': centre}
     # The call being tested.
     grib2_convert(field, metadata)
     self.assertEqual(metadata, expected)
     this = iris.fileformats.grib._load_convert
     this.reference_time_coord.assert_called_with(sections[1])
     this.grid_definition_section.assert_called_with(sections[3], expected)
     args = (sections[4], expected, sections[0]['discipline'],
             sections[1]['tablesVersion'], None)
     this.product_definition_section.assert_called_with(*args)
Example #31
0
 def test_bitmap_present_float_data(self):
     # Test the behaviour where bitmap and codedValues shapes
     # are not equal, and codedValues is float data.
     data_type = np.float32
     input_values = np.arange(5, dtype=np.float32) + 5
     output_values = np.array([-1, -1, 5, 6, -1, -1, -1, 7, -1, 8, -1, 9],
                              dtype=data_type)
     message = _make_test_message({
         3: self._section_3,
         6: {
             'bitMapIndicator': 0,
             'bitmap': self.bitmap
         },
         7: {
             'codedValues': input_values
         }
     })
     result = as_concrete_data(message.data,
                               nans_replacement=ma.masked,
                               result_dtype=data_type)
     expected = ma.masked_array(output_values, np.logical_not(self.bitmap))
     expected = expected.reshape(self.shape)
     self.assertMaskedArrayEqual(result, expected)
Example #32
0
 def test_edition_1_bad(self):
     sections = [{'editionNumber': 1}]
     field = _make_test_message(sections)
     emsg = 'edition 1 is not supported'
     with self.assertRaisesRegexp(TranslationError, emsg):
         convert(field)
Example #33
0
 def test(self):
     # Check that the `sections` attribute defers to the `sections`
     # attribute on the underlying _RawGribMessage.
     message = _make_test_message(mock.sentinel.SECTIONS)
     self.assertIs(message.sections, mock.sentinel.SECTIONS)
Example #34
0
 def test_unsupported_scanning_mode(self):
     message = _make_test_message(
         {3: self.section_3(1),
          6: SECTION_6_NO_BITMAP})
     with self.assertRaisesRegexp(TranslationError, 'scanning mode'):
         message.data
Example #35
0
 def test_unsupported_grid_definition(self):
     message = _make_test_message({3: {'sourceOfGridDefinition': 1},
                                   6: SECTION_6_NO_BITMAP})
     with self.assertRaisesRegexp(TranslationError, 'source'):
         message.data
Example #36
0
 def test(self):
     # Check that the `sections` attribute defers to the `sections`
     # attribute on the underlying _RawGribMessage.
     message = _make_test_message(mock.sentinel.SECTIONS)
     self.assertIs(message.sections, mock.sentinel.SECTIONS)