Esempio n. 1
0
class unittest_Grib(unittest.TestCase):
   
  def setUp(self):
    stringBulletin = "TEST_GRIB_TEST"
    self.grib = Grib(stringBulletin)

  def test_Grib(self):      
    self.assertEqual(self.grib.begin, 5)
    self.assertEqual(self.grib.last, -1)
    self.assertEqual(self.grib.validate(), False)
Esempio n. 2
0
 def setUp(self):
   stringBulletin = "TEST_GRIB_TEST"
   self.grib = Grib(stringBulletin)
Esempio n. 3
0
    def splitlinesBulletin(self, stringBulletin):
        """splitlinesBulletin(stringBulletin) -> listeLignes

           stringBulletin       : String
           listeLignes          : Liste

           Return a list of bulletin lines.  Do not use string.splitlines() since
           it will not work with binary data.

           Binary data start with GRIB or BUFR and end with 77777

           Purpose:

                initial split of bulletins to allow change of lineseparator as required.
                or after setting a line separator to split again (call getBulletin, then split again.)

           N.B.: GRIB & BUFR data is normalized by removing all data after 77777
                 and adding a line separator.

        """
        try:
            estBinaire = False

            # On détermine si le bulletin est binaire
            # determine if the bulletin is binary.
            for ligne in stringBulletin.splitlines():
                if ligne.lstrip()[:4] == 'BUFR' or ligne.lstrip(
                )[:4] == 'GRIB' or ligne.lstrip()[:4] == '\211PNG':
                    # Il faut que le BUFR/GRIB soit au début d'une ligne
                    # BUFR/GRIB must be at the beginning of a line.
                    estBinaire = True
                    break

            if estBinaire:
                if stringBulletin.find('GRIB') != -1:
                    # for GRIB data, do a binary split.
                    # TODO check if grib is valid  grib.valid  and if not react

                    grib = Grib(stringBulletin)

                    b = stringBulletin[:grib.begin].split(self.lineSeparator)

                    # If the last token is a '', then there is a blank last line.
                    # it is removed, because we will add it back later.
                    # Si le dernier token est un '', c'est qu'il y avait
                    # un \n à la fin, et on enlève puisque entre 2 éléments de la liste,
                    # on insère un \n
                    if b[-1] == '':
                        b.pop(-1)

                    b = b + [stringBulletin[grib.begin:grib.last]] + ['']

                    return b

                elif stringBulletin.find('BUFR') != -1:
                    # for a BUFR bulletin, do a BUFR split...
                    # TODO check if bufr is valid  bufr.valid  and if not react

                    bufr = Bufr(stringBulletin)

                    b = stringBulletin[:bufr.begin].split(self.lineSeparator)

                    # Si le dernier token est un '', c'est qu'il y avait
                    # un \n à la fin, et on enlève puisque entre 2 éléments de la liste,
                    # on insère un \n
                    if b[-1] == '':
                        b.pop(-1)

                    b = b + [stringBulletin[bufr.begin:bufr.last]] + ['']

                    if bufr.valid:
                        self.emission = bufr.observation
                        self.ep_emission = bufr.ep_observation
                    else:
                        self.logger.warning(
                            'Bufr without a valid internal date in section 1')
                        self.logger.warning('Use date from bulletin header')

                    return b

                elif stringBulletin.find('\211PNG') != -1:
                    # for a PNG pictural bulletin do nothing...

                    png_begin = stringBulletin.find('\211PNG')

                    b = stringBulletin[:png_begin].split(self.lineSeparator)

                    # Si le dernier token est un '', c'est qu'il y avait
                    # un \n à la fin, et on enlève puisque entre 2 éléments de la liste,
                    # on insère un \n
                    if b[-1] == '':
                        b.pop(-1)

                    b = b + [stringBulletin[png_begin:]] + ['']

                    return b
            else:
                # The bulletin is alphanumeric...
                return stringBulletin.split(self.lineSeparator)
        except Exception, e:
            self.logger.exception('Error splitting bulletin:\n' + ''.join(
                traceback.format_exception(Exception, e, sys.exc_traceback)))
            self.setError('Error splitting bulletin into lines')
            return stringBulletin.split(self.lineSeparator)