How to use eccodes - 10 common examples

To help you get started, we’ve selected a few eccodes examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jdkloe / pybufr-ecmwf / pybufr_ecmwf / bufr.py View on Github external
print('  %s: %s' %
                  (keyname, eccodes.codes_get(self._bufr, keyname)))

        # get unexpanded descriptors
        key = 'unexpandedDescriptors'
        num = eccodes.codes_get_size(self._bufr, key)
        print('  size of %s is: %s' % (key, num))
        values = eccodes.codes_get_array(self._bufr, key)
        for i in xrange(len(values)):
            print("   %d %06d" % (i + 1, values[i]))
        
        # get the expanded descriptors
        key = 'bufrdcExpandedDescriptors'
        num = eccodes.codes_get_size(self._bufr, key)
        print('  size of %s is: %s' % (key, num))
        values = eccodes.codes_get_array(self._bufr, key)
        for i in xrange(len(values)):
            print("   %d %06d" % (i + 1, values[i]))
github jdkloe / pybufr-ecmwf / pybufr_ecmwf / bufr.py View on Github external
for field in field_names:
            if field[0] in string.digits:
                print('cannot get data for field: ',field)
                continue
            # print('trying field name: ', field)
            # key = '/subsetNumber={0:d}/{1:s}'.format(subset_nr, field)
            key = field
            # print('trying key: ', key)
            s = eccodes.codes_get_size(self._bufr, key)
            # print('s=', s)
            if s==1:
                value = eccodes.codes_get(self._bufr, key)
                data.append(value)
            else:
                values = eccodes.codes_get_array(self._bufr, key)
                # print('DEBUG: values: ', values)
                data.append(values)

        # finally convert to a numpy array of type object
        # for user convenience
        values = numpy.array(data, dtype='object')

        return values
        #  #]
github jdkloe / pybufr-ecmwf / pybufr_ecmwf / bufr.py View on Github external
def _get_abbr_names(self, subset=1):
        #  #[ request abbr. name of each descriptor for the given subset
        '''
        internal method to get the key name needed to extract the data
        '''
        if (self.msg_index == -1):
            raise NoMsgLoadedError

        # remove entries that are not expanded and numeric.
        # these typically are replication codes and not part
        # of the field list
        abbr_names = [n for n in
                      eccodes.codes_get_array(self._bufr,
                                              'expandedAbbreviations')
                      if not n[0] in string.digits]
        return abbr_names
        #  #]
github jdkloe / pybufr-ecmwf / pybufr_ecmwf / bufr.py View on Github external
'scale',
            'reference',
            'width'
            ]
        
        iterid = eccodes.codes_keys_iterator_new(self._bufr, 'ls')
        while eccodes.codes_keys_iterator_next(iterid):
            keyname = eccodes.codes_keys_iterator_get_name(iterid)
            print('  %s: %s' %
                  (keyname, eccodes.codes_get(self._bufr, keyname)))

        # get unexpanded descriptors
        key = 'unexpandedDescriptors'
        num = eccodes.codes_get_size(self._bufr, key)
        print('  size of %s is: %s' % (key, num))
        values = eccodes.codes_get_array(self._bufr, key)
        for i in xrange(len(values)):
            print("   %d %06d" % (i + 1, values[i]))
        
        # get the expanded descriptors
        key = 'bufrdcExpandedDescriptors'
        num = eccodes.codes_get_size(self._bufr, key)
        print('  size of %s is: %s' % (key, num))
        values = eccodes.codes_get_array(self._bufr, key)
        for i in xrange(len(values)):
            print("   %d %06d" % (i + 1, values[i]))
github jdkloe / pybufr-ecmwf / pybufr_ecmwf / bufr.py View on Github external
def get_units(self, subset=1):
        #  #[ request unit of each descriptor for the given subset
        if (self.msg_index == -1):
            raise NoMsgLoadedError

        return eccodes.codes_get_array(self._bufr, 'expandedUnits')
        #  #]        
github jdkloe / pybufr-ecmwf / pybufr_ecmwf / bufr.py View on Github external
def get_num_elements(self):
        #  #[
        """
        request the number of elements (descriptors) in the current subset
        """
        if (self.msg_index == -1):
            raise NoMsgLoadedError

        fieldnames = eccodes.codes_get_array(self._bufr, 'expandedNames')

        # other possibilities are:
        # 'expandedAbbreviations'
        # 'expandedNames'
        # 'expandedUnits'
        # 'expandedOriginalScales'
        # 'expandedOriginalReferences'
        # 'expandedOriginalWidths'
        
        return len(fieldnames)
        #  #]
github pytroll / satpy / satpy / readers / seviri_l2_bufr.py View on Github external
def get_array(self, key):
        """Get all data from file for the given BUFR key."""
        with open(self.filename, "rb") as fh:
            msgCount = 0
            while True:
                bufr = ec.codes_bufr_new_from_file(fh)
                if bufr is None:
                    break

                ec.codes_set(bufr, 'unpack', 1)

                # if is the first message initialise our final array
                if (msgCount == 0):
                    arr = da.from_array(ec.codes_get_array(
                        bufr, key, float), chunks=CHUNK_SIZE)
                else:
                    tmpArr = da.from_array(ec.codes_get_array(
                        bufr, key, float), chunks=CHUNK_SIZE)
                    arr = da.concatenate((arr, tmpArr))

                msgCount = msgCount+1
                ec.codes_release(bufr)

        if arr.size == 1:
            arr = arr[0]

        return arr
github jdkloe / pybufr-ecmwf / pybufr_ecmwf / bufr.py View on Github external
"""
        if (self.msg_index == -1):
            raise NoMsgLoadedError

        list_of_names = self._get_abbr_names()
        keyname = list_of_names[descr_nr]
        print('keyname: ', keyname)
        s = eccodes.codes_get_size(self._bufr, keyname)
        t = eccodes.codes_get_native_type(self._bufr, keyname)
        print('key:', keyname, 'size = ', s, 'type = ', t)

        if s==1: # or t==str:
            # values = eccodes.codes_get_string(_bufr, keyname)
            values = [eccodes.codes_get(self._bufr, keyname),]
        else:
            values = eccodes.codes_get_array(self._bufr, keyname)

        return values
        #  #]
github ecmwf / eccodes / examples / python / high_level_api.py View on Github external
def test_memory_management(self):
        """Messages in BufrFile can be opened and closed properly."""
        with BufrFile(TESTBUFR) as bufr_file:
            self.assertEqual(len(bufr_file), 3)
            for i in range(len(bufr_file)):
                msg = BufrMessage(bufr_file)
                self.assertEqual(msg["bufrHeaderCentre"], 98)
                self.assertEqual(msg['count'], i + 1)
            self.assertEqual(len(bufr_file.open_messages), 3)
        self.assertEqual(len(bufr_file.open_messages), 0)
github GribApiDotNet / GribApi.XP / grib_api / examples / python / high_level_api.py View on Github external
def test_metadata(self):
        """Metadata is read correctly from BufrMessage."""
        with BufrFile(TESTBUFR) as bufr_file:
            msg = BufrMessage(bufr_file)
            msg.unpack()
            msg_keys = msg.keys()
            self.assertEqual(len(msg_keys), 140)
            for key in KNOWN_BUFR_KEYS:
                assert key in msg_keys
            # Size of message in bytes
            self.assertEqual(msg.size(), 220)
            self.assertEqual(len(msg.keys()), len(msg))