def matches(self, gid): if eccodes is None: raise Exception("eccodes not found. Needed for reading grib files") discipline = int(eccodes.codes_get(gid, "discipline")) parameter_category = int(eccodes.codes_get(gid, "parameterCategory")) parameter_number = int(eccodes.codes_get(gid, "parameterNumber")) level_type = int(eccodes.codes_get_long(gid, "levelType")) level = int(eccodes.codes_get(gid, "level")) try: type_of_statistical_processing = eccodes.codes_get(gid, "typeOfStatisticalProcessing") except gribapi.errors.KeyValueNotFoundError: type_of_statistical_processing = -1 # print("Matching ") # print(self.discipline, discipline) # print(self.parameterCategory, parameter_category) # print(self.parameterNumber, parameter_number) # print(self.levelType, level_type) # print(self.level, level) # print(self.typeOfStatisticalProcessing, type_of_statistical_processing) if self.discipline == discipline and \ self.parameterCategory == parameter_category and \ self.parameterNumber == parameter_number and \ self.levelType == level_type and \ self.level == level and \ self.typeOfStatisticalProcessing == type_of_statistical_processing: return True else: return False
def fetch_model_output(input_file, time_since_init, short_name, level): file = open(input_file, "rb") gid = ec.codes_grib_new_from_file(file) file.close() values = read_grib_array(input_file, short_name, time_since_init, level) lat = np.deg2rad(ec.codes_get_array(gid, "latitudes")) lon = np.deg2rad(ec.codes_get_array(gid, "longitudes")) no_of_columns = ec.codes_get_long(gid, "Ni") no_of_lines = ec.codes_get_long(gid, "Nj") ec.codes_release(gid) lat_vector = np.zeros([no_of_lines]) lon_vector = np.zeros([no_of_columns]) for i in range(no_of_lines): lat_vector[i] = lat[i*no_of_columns] for i in range(no_of_columns): lon_vector[i] = lon[i] return lat_vector, lon_vector, vector_2_array(values, no_of_lines, no_of_columns)
def index_grib_files(grib_files): logging.info("Indexing grib files") index = {} index_keys = get_index_keys() cnt = 0 for grib_file in grib_files[0]: with open(grib_file) as fp: message_no = 0 offset = 0 while True: gid = ecc.codes_grib_new_from_file(fp) if gid is None: break ref = index for k in index_keys: try: val = ecc.codes_get_long(gid, k) except gribapi.errors.KeyValueNotFoundError as e: val = None if val not in ref: ref[val] = {} ref = ref[val] else: ref = ref[val] length = ecc.codes_get_long(gid, 'totalLength') ref['file_name'] = grib_file ref['message_no'] = message_no ref['length'] = length ref['offset'] = offset # print(ref) message_no += 1 offset += length cnt += 1 logging.info(f"Indexed {cnt} messages from {len(grib_files[0])} file(s)") return index
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 forecast_type_from_grib(gid): ty = ecc.codes_get_long(gid, 'typeOfProcessedData') if ty == 0: return "analysis" elif ty == 1: return "deterministic" elif ty == 3: return "control/%d" % ecc.codes_get(gid, "perturbationNumber") elif ty == 4: return "perturbation/%d" % ecc.codes_get(gid, "perturbationNumber") else: return "unknown"
def test_grib_read(): gid = eccodes.codes_grib_new_from_samples("regular_ll_sfc_grib1") assert eccodes.codes_get(gid, "Ni") == 16 assert eccodes.codes_get(gid, "Nj") == 31 assert eccodes.codes_get(gid, "const") == 1 assert eccodes.codes_get(gid, "centre", str) == "ecmf" assert eccodes.codes_get(gid, "packingType", str) == "grid_simple" assert eccodes.codes_get(gid, "gridType", str) == "regular_ll" eccodes.codes_release(gid) gid = eccodes.codes_grib_new_from_samples("sh_ml_grib2") assert eccodes.codes_get(gid, "const") == 0 assert eccodes.codes_get(gid, "gridType", str) == "sh" assert eccodes.codes_get(gid, "typeOfLevel", str) == "hybrid" assert eccodes.codes_get_long(gid, "avg") == 185 eccodes.codes_release(gid)
def get_long(self, k: str) -> int: return eccodes.codes_get_long(self.gid, k)
def get_long(self, name): try: return eccodes.codes_get_long(self.handle, name) except eccodes.KeyValueNotFoundError: return None
def get_long(self, key): return eccodes.codes_get_long(self.handle, key)