def test_grib_keys_iterator_skip(): gid = eccodes.codes_grib_new_from_samples("reduced_gg_pl_1280_grib1") iterid = eccodes.codes_keys_iterator_new(gid, "ls") count = 0 eccodes.codes_skip_computed(iterid) # codes_skip_coded(iterid) eccodes.codes_skip_edition_specific(iterid) eccodes.codes_skip_duplicates(iterid) eccodes.codes_skip_read_only(iterid) eccodes.codes_skip_function(iterid) while eccodes.codes_keys_iterator_next(iterid): keyname = eccodes.codes_keys_iterator_get_name(iterid) keyval = eccodes.codes_get_string(gid, keyname) assert len(keyval) > 0 count += 1 # centre, level and dataType assert count == 3 eccodes.codes_keys_iterator_delete(iterid) iterid = eccodes.codes_keys_iterator_new(gid) count = 0 eccodes.codes_skip_coded(iterid) while eccodes.codes_keys_iterator_next(iterid): count += 1 assert count == 141 eccodes.codes_keys_iterator_delete(iterid) eccodes.codes_release(gid)
def message_grib_keys(self, namespace=None): # type: (str) -> T.Generator[str, None, None] iterator = eccodes.codes_keys_iterator_new(self.codes_id, namespace=namespace) while eccodes.codes_keys_iterator_next(iterator): yield eccodes.codes_keys_iterator_get_name(iterator) eccodes.codes_keys_iterator_delete(iterator)
def message_grib_keys(self, namespace: T.Optional[str] = None ) -> T.Iterator[str]: iterator = eccodes.codes_keys_iterator_new(self.codes_id, namespace=namespace) while eccodes.codes_keys_iterator_next(iterator): yield eccodes.codes_keys_iterator_get_name(iterator) eccodes.codes_keys_iterator_delete(iterator)
def keys(self, namespace=None): """Get available keys in message.""" iterator = eccodes.codes_keys_iterator_new(self.codes_id, namespace=namespace) keys = [] while eccodes.codes_keys_iterator_next(iterator): key = eccodes.codes_keys_iterator_get_name(iterator) keys.append(key) eccodes.codes_keys_iterator_delete(iterator) return keys
def message_iterkeys(self, namespace=None): # type: (str) -> T.Generator[str, None, None] if namespace is not None: bnamespace = namespace # type: T.Optional[str] else: bnamespace = None iterator = eccodes.codes_keys_iterator_new(self.codes_id, namespace=bnamespace) while eccodes.codes_keys_iterator_next(iterator): yield eccodes.codes_keys_iterator_get_name(iterator) eccodes.codes_keys_iterator_delete(iterator)
def test_grib_keys_iterator(): gid = eccodes.codes_grib_new_from_samples("reduced_gg_pl_1280_grib1") iterid = eccodes.eccodes.codes_keys_iterator_new(gid, "ls") count = 0 while eccodes.codes_keys_iterator_next(iterid): keyname = eccodes.codes_keys_iterator_get_name(iterid) keyval = eccodes.codes_get_string(gid, keyname) assert len(keyval) > 0 count += 1 assert count == 10 eccodes.codes_keys_iterator_rewind(iterid) eccodes.eccodes.codes_keys_iterator_delete(iterid) eccodes.codes_release(gid)
def get_ecc_msg_keys(gid, namespace=None, skipkeys=None): """Retrieve keys from one particular ecc message Parameters ---------- gid : ecc message id namespace : string namespace to be retrieved, defaults to None (means all) 'ls', 'parameter', 'time', 'geography', 'vertical', 'statistics', 'mars' skipkeys : list of strings keys to be skipped, defaults to None possible keys: 'computed', 'coded', 'edition', 'duplicates', 'read_only', 'function' Returns ------- data : list of ecc message keys """ # get key iterator iterid = ecc.codes_keys_iterator_new(gid, namespace) # Different types of keys can be skipped if skipkeys: if 'computed' in skipkeys: ecc.codes_skip_computed(iterid) if 'coded' in skipkeys: ecc.codes_skip_coded(iterid) if 'edition' in skipkeys: ecc.codes_skip_edition_specific(iterid) if 'duplicates' in skipkeys: ecc.codes_skip_duplicates(iterid) if 'read_only' in skipkeys: ecc.codes_skip_read_only(iterid) if 'function' in skipkeys: ecc.codes_skip_function(iterid) data = [] # iterate over message keys while ecc.codes_keys_iterator_next(iterid): keyname = ecc.codes_keys_iterator_get_name(iterid) # add keyname-keyvalue-pair to output dictionary data.append(keyname) # release iterator ecc.codes_keys_iterator_delete(iterid) return data
def _index_grib_file(path, path_name=None): import eccodes with open(path, "rb") as f: h = eccodes.codes_grib_new_from_file(f) while h: try: field = dict() if isinstance(path_name, str): field["_path"] = path_name elif path_name is False: pass elif path_name is None: field["_path"] = path else: raise ValueError( f"Value of path_name cannot be '{path_name}.'") i = eccodes.codes_keys_iterator_new(h, "mars") try: while eccodes.codes_keys_iterator_next(i): name = eccodes.codes_keys_iterator_get_name(i) value = eccodes.codes_get_string(h, name) field[name] = value finally: eccodes.codes_keys_iterator_delete(i) field["_offset"] = eccodes.codes_get_long(h, "offset") field["_length"] = eccodes.codes_get_long(h, "totalLength") field["_param_id"] = eccodes.codes_get_string(h, "paramId") field["param"] = eccodes.codes_get_string(h, "shortName") yield field finally: eccodes.codes_release(h) h = eccodes.codes_grib_new_from_file(f)
def get_ecc_msg(gid, namespace=None, skipkeys=None): """Read data from one particular ecc message Parameters ---------- gid : ecc message id namespace : string namespace to be retrieved, defaults to None (means all) 'ls', 'parameter', 'time', 'geography', 'vertical', 'statistics', 'mars' skipkeys : list of strings keys to be skipped, defaults to None possible keys: 'computed', 'coded', 'edition', 'duplicates', 'read_only', 'function' Returns ------- data : dictionary of ecc message contents """ # get key iterator iterid = ecc.codes_keys_iterator_new(gid, namespace) # Different types of keys can be skipped if skipkeys: if 'computed' in skipkeys: ecc.codes_skip_computed(iterid) if 'coded' in skipkeys: ecc.codes_skip_coded(iterid) if 'edition' in skipkeys: ecc.codes_skip_edition_specific(iterid) if 'duplicates' in skipkeys: ecc.codes_skip_duplicates(iterid) if 'read_only' in skipkeys: ecc.codes_skip_read_only(iterid) if 'function' in skipkeys: ecc.codes_skip_function(iterid) data = OrderedDict() # iterate over message keys while ecc.codes_keys_iterator_next(iterid): keyname = ecc.codes_keys_iterator_get_name(iterid) #print(keyname) #print("Size:", ecc.codes_get_size(gid, keyname)) #print("Values:", ecc.codes_get_values(gid, keyname)) #print("Array:", ecc.codes_get_values(gid, keyname)) # try to get key values, # use get_array for sizes > 1 and get for sizes == 1 if ecc.codes_get_size(gid,keyname) > 1: #print("has array", type is str) #print(type is not <type 'str'>) if ecc.codes_get_native_type(iterid, keyname) is not str: keyval = ecc.codes_get_array(gid, keyname, None) else: keyval = ecc.codes_get(gid, keyname, None) #print("Arr:", keyval) else: # Todo: fix reading mybits if keyname not in ['mybits']: keyval = ecc.codes_get(gid, keyname, None) #print("Val:", keyval) else: keyval = 'err' # add keyname-keyvalue-pair to output dictionary data[keyname] = keyval #print('Message processed') # release iterator ecc.codes_keys_iterator_delete(iterid) return data