Example #1
0
 def test_year_filter_none(self):
     msgs = GribMessage.messages_from_filename(self.file_path)
     chosen_messages = []
     for gmsg in msgs:
         if gmsg.sections[1]['year'] == 1958:
             chosen_messages.append(gmsg)
     cubes_msgs = list(load_pairs_from_fields(chosen_messages))
     self.assertEqual(len(cubes_msgs), 0)
Example #2
0
 def test_data(self):
     # Check that GribMessage.data pickles without errors.
     path = tests.get_data_path(('GRIB', 'fp_units', 'hours.grib2'))
     messages = GribMessage.messages_from_filename(path)
     message = next(messages)
     with self.temp_filename('.pkl') as filename:
         with open(filename, 'wb') as f:
             pickle.dump(message.data, f)
Example #3
0
 def test_year_filter_none(self):
     msgs = GribMessage.messages_from_filename(self.file_path)
     chosen_messages = []
     for gmsg in msgs:
         if gmsg.sections[1]['year'] == 1958:
             chosen_messages.append(gmsg)
     cubes_msgs = list(load_pairs_from_fields(chosen_messages))
     self.assertEqual(len(cubes_msgs), 0)
Example #4
0
 def test_data(self):
     # Check that GribMessage.data pickles without errors.
     path = tests.get_data_path(('GRIB', 'fp_units', 'hours.grib2'))
     messages = GribMessage.messages_from_filename(path)
     message = next(messages)
     with self.temp_filename('.pkl') as filename:
         with open(filename, 'wb') as f:
             pickle.dump(message.data, f)
Example #5
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 #6
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 #7
0
 def test_as_pairs(self):
     messages = GribMessage.messages_from_filename(self.file_path)
     cubes = []
     cube_msg_pairs = load_pairs_from_fields(messages)
     for cube, gmsg in cube_msg_pairs:
         if gmsg.sections[1]['year'] == 1998:
             cube.attributes['the year is'] = gmsg.sections[1]['year']
             cubes.append(cube)
     self.assertEqual(len(cubes), 1)
     self.assertEqual(cubes[0].attributes['the year is'], 1998)
Example #8
0
 def test_as_pairs(self):
     messages = GribMessage.messages_from_filename(self.file_path)
     cubes = []
     cube_msg_pairs = load_pairs_from_fields(messages)
     for cube, gmsg in cube_msg_pairs:
         if gmsg.sections[1]['year'] == 1998:
             cube.attributes['the year is'] = gmsg.sections[1]['year']
             cubes.append(cube)
     self.assertEqual(len(cubes), 1)
     self.assertEqual(cubes[0].attributes['the year is'], 1998)
Example #9
0
def _load_generate(filename):
    messages = GribMessage.messages_from_filename(filename)
    for message in messages:
        editionNumber = message.sections[0]['editionNumber']
        if editionNumber == 1:
            message_id = message._raw_message._message_id
            grib_fh = message._file_ref.open_file
            message = GribWrapper(message_id, grib_fh=grib_fh)
        elif editionNumber != 2:
            emsg = 'GRIB edition {} is not supported by {!r}.'
            raise TranslationError(emsg.format(editionNumber,
                                               type(message).__name__))
        yield message
Example #10
0
def _load_generate(filename):
    messages = GribMessage.messages_from_filename(filename)
    for message in messages:
        editionNumber = message.sections[0]['editionNumber']
        if editionNumber == 1:
            message_id = message._raw_message._message_id
            grib_fh = message._file_ref.open_file
            message = GribWrapper(message_id, grib_fh=grib_fh)
        elif editionNumber != 2:
            emsg = 'GRIB edition {} is not supported by {!r}.'
            raise TranslationError(
                emsg.format(editionNumber,
                            type(message).__name__))
        yield message
Example #11
0
    def assertGribMessageContents(self, filename, contents):
        """
        Evaluate whether all messages in a GRIB2 file contain the provided
        contents.

        * filename (string)
            The path on disk of an existing GRIB file

        * contents
            An iterable of GRIB message keys and expected values.

        """
        messages = GribMessage.messages_from_filename(filename)
        for message in messages:
            for element in contents:
                section, key, val = element
                self.assertEqual(message.sections[section][key], val)
Example #12
0
    def assertGribMessageContents(self, filename, contents):
        """
        Evaluate whether all messages in a GRIB2 file contain the provided
        contents.

        * filename (string)
            The path on disk of an existing GRIB file

        * contents
            An iterable of GRIB message keys and expected values.

        """
        messages = GribMessage.messages_from_filename(filename)
        for message in messages:
            for element in contents:
                section, key, val = element
                self.assertEqual(message.sections[section][key], val)
Example #13
0
def _make_test_message(sections):
    raw_message = mock.Mock(sections=sections)
    recreate_raw = mock.Mock(return_value=raw_message)
    return GribMessage(raw_message, recreate_raw)
 def _make_test_message(self, sections):
     raw_message = mock.Mock(sections=sections, _message_id=self.message_id)
     file_ref = mock.Mock(open_file=self.grib_fh)
     return GribMessage(raw_message, None, file_ref=file_ref)
Example #15
0
    def assertGribMessageDifference(self,
                                    filename1,
                                    filename2,
                                    diffs,
                                    skip_keys=(),
                                    skip_sections=()):
        """
        Evaluate that the two messages only differ in the ways specified.

        * filename[0|1] (string)
            The path on disk of existing GRIB files

        * diffs
            An dictionary of GRIB message keys and expected diff values:
            {key: (m1val, m2val),...} .

        * skip_keys
            An iterable of key names to ignore during comparison.

        * skip_sections
            An iterable of section numbers to ignore during comparison.

        """
        messages1 = list(GribMessage.messages_from_filename(filename1))
        messages2 = list(GribMessage.messages_from_filename(filename2))
        self.assertEqual(len(messages1), len(messages2))
        for m1, m2 in zip(messages1, messages2):
            m1_sect = set(m1.sections.keys())
            m2_sect = set(m2.sections.keys())

            for missing_section in (m1_sect ^ m2_sect):
                what = ('introduced'
                        if missing_section in m1_sect else 'removed')
                # Assert that an introduced section is in the diffs.
                self.assertIn(missing_section,
                              skip_sections,
                              msg='Section {} {}'.format(
                                  missing_section, what))

            for section in (m1_sect & m2_sect):
                # For each section, check that the differences are
                # known diffs.
                m1_keys = set(m1.sections[section]._keys)
                m2_keys = set(m2.sections[section]._keys)

                difference = m1_keys ^ m2_keys
                unexpected_differences = difference - set(skip_keys)
                if unexpected_differences:
                    self.fail("There were keys in section {} which \n"
                              "weren't in both messages and which weren't "
                              "skipped.\n{}"
                              "".format(section,
                                        ', '.join(unexpected_differences)))

                keys_to_compare = m1_keys & m2_keys - set(skip_keys)

                for key in keys_to_compare:
                    m1_value = m1.sections[section][key]
                    m2_value = m2.sections[section][key]
                    msg = '{} {} != {}'
                    if key not in diffs:
                        # We have a key which we expect to be the same for
                        # both messages.
                        if isinstance(m1_value, np.ndarray):
                            # A large tolerance appears to be required for
                            # gribapi 1.12, but not for 1.14.
                            self.assertArrayAlmostEqual(m1_value,
                                                        m2_value,
                                                        decimal=2)
                        else:
                            self.assertEqual(m1_value,
                                             m2_value,
                                             msg=msg.format(
                                                 key, m1_value, m2_value))
                    else:
                        # We have a key which we expect to be different
                        # for each message.
                        self.assertEqual(m1_value,
                                         diffs[key][0],
                                         msg=msg.format(
                                             key, m1_value, diffs[key][0]))

                        self.assertEqual(m2_value,
                                         diffs[key][1],
                                         msg=msg.format(
                                             key, m2_value, diffs[key][1]))
Example #16
0
    def assertGribMessageDifference(self, filename1, filename2, diffs,
                                    skip_keys=(), skip_sections=()):
        """
        Evaluate that the two messages only differ in the ways specified.

        * filename[0|1] (string)
            The path on disk of existing GRIB files

        * diffs
            An dictionary of GRIB message keys and expected diff values:
            {key: (m1val, m2val),...} .

        * skip_keys
            An iterable of key names to ignore during comparison.

        * skip_sections
            An iterable of section numbers to ignore during comparison.

        """
        messages1 = list(GribMessage.messages_from_filename(filename1))
        messages2 = list(GribMessage.messages_from_filename(filename2))
        self.assertEqual(len(messages1), len(messages2))
        for m1, m2 in zip(messages1, messages2):
            m1_sect = set(m1.sections.keys())
            m2_sect = set(m2.sections.keys())

            for missing_section in (m1_sect ^ m2_sect):
                what = ('introduced'
                        if missing_section in m1_sect else 'removed')
                # Assert that an introduced section is in the diffs.
                self.assertIn(missing_section, skip_sections,
                              msg='Section {} {}'.format(missing_section,
                                                         what))

            for section in (m1_sect & m2_sect):
                # For each section, check that the differences are
                # known diffs.
                m1_keys = set(m1.sections[section]._keys)
                m2_keys = set(m2.sections[section]._keys)

                difference = m1_keys ^ m2_keys
                unexpected_differences = difference - set(skip_keys)
                if unexpected_differences:
                    self.fail("There were keys in section {} which \n"
                              "weren't in both messages and which weren't "
                              "skipped.\n{}"
                              "".format(section,
                                        ', '.join(unexpected_differences)))

                keys_to_compare = m1_keys & m2_keys - set(skip_keys)

                for key in keys_to_compare:
                    m1_value = m1.sections[section][key]
                    m2_value = m2.sections[section][key]
                    msg = '{} {} != {}'
                    if key not in diffs:
                        # We have a key which we expect to be the same for
                        # both messages.
                        if isinstance(m1_value, np.ndarray):
                            # A large tolerance appears to be required for
                            # gribapi 1.12, but not for 1.14.
                            self.assertArrayAlmostEqual(m1_value, m2_value,
                                                        decimal=2)
                        else:
                            self.assertEqual(m1_value, m2_value,
                                             msg=msg.format(key, m1_value,
                                                            m2_value))
                    else:
                        # We have a key which we expect to be different
                        # for each message.
                        self.assertEqual(m1_value, diffs[key][0],
                                         msg=msg.format(key, m1_value,
                                                         diffs[key][0]))

                        self.assertEqual(m2_value, diffs[key][1],
                                         msg=msg.format(key, m2_value,
                                                        diffs[key][1]))
Example #17
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 #18
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)