def copy_needed_field(gid, fout): """Copy the needed field""" nx = ecc.codes_get(gid, 'Ni') ny = ecc.codes_get(gid, 'Nj') first_lat = ecc.codes_get(gid, 'latitudeOfFirstGridPointInDegrees') north_south_step = ecc.codes_get(gid, 'jDirectionIncrementInDegrees') filter_north = 0 new_ny = int((first_lat - filter_north) / north_south_step) + 1 values = ecc.codes_get_values(gid) values_r = np.reshape(values, (ny, nx)) new_values = values_r[:new_ny, :] clone_id = ecc.codes_clone(gid) ecc.codes_set(clone_id, 'latitudeOfLastGridPointInDegrees', (filter_north)) ecc.codes_set(clone_id, 'Nj', new_ny) ecc.codes_set_values(clone_id, new_values.flatten()) ecc.codes_write(clone_id, fout) ecc.codes_release(clone_id)
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 _find_start_end_steps(gribs): # return input_steps, # change step if a second time resolution is found start_steps = [ codes_get(gribs[i], 'startStep') for i in range(len(gribs)) ] end_steps = [codes_get(gribs[i], 'endStep') for i in range(len(gribs))] start_grib = min(start_steps) end_grib = max(end_steps) ord_end_steps = sorted(end_steps) ord_start_steps = sorted(start_steps) if len(ord_end_steps) > 1: step = ord_end_steps[1] - ord_end_steps[0] else: step = ord_end_steps[0] - ord_start_steps[0] step2 = -1 change_step_at = '' if len(ord_end_steps) > 1: step = ord_end_steps[1] - ord_end_steps[0] for i in range(2, len(ord_end_steps)): if step2 == -1 and ord_end_steps[i] - ord_end_steps[i - 1] != step: # change of time resolution step2 = ord_end_steps[i] - ord_end_steps[i - 1] change_step_at = f'{ord_start_steps[i]}-{ord_end_steps[i]}' return start_grib, end_grib, step, step2, change_step_at
def _check_level_type( message_id, level_type: str or typing.List or typing.Dict or None, ) -> bool: if level_type is None: return True message_type = eccodes.codes_get(message_id, "typeOfLevel", ktype=str) if isinstance(level_type, str): return message_type == level_type elif isinstance(level_type, typing.List): for cur_level_type in level_type: if _check_level_type(message_id, cur_level_type): return True return False elif isinstance(level_type, typing.Dict): for key in level_type: requested_value = level_type[key] value = eccodes.codes_get(message_id, key, ktype=type(requested_value)) if requested_value != value: return False return True else: raise ValueError(f"level_type is not supported: {level_type}")
def test_grib_clone(): gid = eccodes.codes_grib_new_from_samples("GRIB2") clone = eccodes.codes_clone(gid) assert gid assert clone assert eccodes.codes_get(clone, "identifier") == "GRIB" assert eccodes.codes_get(clone, "totalLength") == 179 eccodes.codes_release(gid) eccodes.codes_release(clone)
def get_grid_from_gid(gid, rotated=False): Ni = codes.codes_get(gid, 'Ni') Nj = codes.codes_get(gid, 'Nj') lat_start = codes.codes_get(gid, 'latitudeOfFirstGridPointInDegrees') lon_start = codes.codes_get(gid, 'longitudeOfFirstGridPointInDegrees') lat_stop = codes.codes_get(gid, 'latitudeOfLastGridPointInDegrees') lon_stop = codes.codes_get(gid, 'longitudeOfLastGridPointInDegrees') print("LL: ({0},{1})".format(lon_start, lat_start)) print("UR: ({0},{1})".format(lon_stop, lat_stop)) lat_sp = codes.codes_get(gid, 'latitudeOfSouthernPole') lon_sp = codes.codes_get(gid, 'longitudeOfSouthernPole') ang_rot = codes.codes_get(gid, 'angleOfRotation') print("SP: ({0},{1}) - Ang:{2}".format(lon_sp, lat_sp, ang_rot)) # create grid arrays from grid details # iarr, jarr one-dimensional data iarr = np.linspace(lon_start, lon_stop, num=Ni, endpoint=False) jarr = np.linspace(lat_start, lat_stop, num=Nj, endpoint=False) # converted by meshgrid to 2d-arrays i2d, j2d = np.meshgrid(iarr, jarr) grid_rot = np.dstack((i2d, j2d)) if not rotated: return rotated_grid_transform(grid_rot, 1, [lon_sp, lat_sp]) else: return grid_rot
def scan_for_gid(filename, short_name, time_since_init, level): filee = open(filename, "rb") for j in np.arange(0, ec.codes_count_in_file(filee)): gid = ec.codes_grib_new_from_file(filee, headers_only = True) if ec.codes_get(gid, "shortName") == short_name and ec.codes_get(gid, "forecastTime") == time_since_init and ec.codes_get(gid, "level") == level: filee.close() return gid else: ec.codes_release(gid) filee.close() exit(1)
def test_bufr_read_write(tmpdir): bid = eccodes.codes_new_from_samples("BUFR4", eccodes.CODES_PRODUCT_BUFR) eccodes.codes_set(bid, "unpack", 1) assert eccodes.codes_get(bid, "typicalYear") == 2012 assert eccodes.codes_get(bid, "centre", str) == "ecmf" eccodes.codes_set(bid, "totalSunshine", 13) eccodes.codes_set(bid, "pack", 1) output = tmpdir.join("test_bufr_write.bufr") with open(str(output), "wb") as fout: eccodes.codes_write(bid, fout) assert eccodes.codes_get(bid, "totalSunshine") == 13 eccodes.codes_release(bid)
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 matches(self, gid): if eccodes is None: raise Exception("eccodes not found. Needed for reading grib files") par = int(eccodes.codes_get(gid, "indicatorOfParameter")) lev = int(eccodes.codes_get(gid, "level")) typ = str(eccodes.codes_get(gid, "levelType")).strip("") tri = int(eccodes.codes_get(gid, "timeRangeIndicator")) if self.par == par and self.level == lev and self.typ == typ and self.tri == tri: return True else: return False
def _find(gid, **kwargs): for k, v in kwargs.items(): if not codes_is_defined(gid, k): return False iscontainer = utils.is_container(v) iscallable = utils.is_callable(v) if (not iscontainer and not iscallable and codes_get(gid, k) == v) or\ (iscontainer and codes_get(gid, k) in v) or \ (iscallable and v(codes_get(gid, k))): continue else: return False return True
def _check_parameter(message_id, parameter: str or typing.Dict or None) -> bool: if parameter is None: return True if isinstance(parameter, str): short_name = eccodes.codes_get(message_id, "shortName") return short_name == parameter elif isinstance(parameter, typing.Dict): for key, value in parameter.items(): if eccodes.codes_get(message_id, key) != value: return False return True else: raise ValueError(f"parameter is not supported: {parameter}")
def get_ecc_value(gids, keyname, keyvalue): first = True for i, gid in enumerate(gids): if ecc.codes_get(gid, keyname) == keyvalue: if first: Ni = ecc.codes_get(gid, 'Ni') Nj = ecc.codes_get(gid, 'Nj') data = np.array(ecc.codes_get_values(gid)) first = False else: data = np.dstack((data, np.array(ecc.codes_get_values(gid)))) return np.squeeze(data.reshape(Ni, Nj, -1))
def get(self, name): try: if name == "values": return eccodes.codes_get_values(self.handle) return eccodes.codes_get(self.handle, name) except eccodes.KeyValueNotFoundError: return None
def cli(file_path): with open(file_path, 'rb') as f: handle = eccodes.codes_grib_new_from_file(f, headers_only=False) while handle is not None: date = eccodes.codes_get(handle, "dataDate") type_of_level = eccodes.codes_get(handle, "typeOfLevel") level = eccodes.codes_get(handle, "level") values = eccodes.codes_get_array(handle, "values") value = values[-1] values_array = eccodes.codes_get_values(handle, "values") value_array = values[-1] print(date, type_of_level, level, value) eccodes.codes_release(handle) handle = eccodes.codes_grib_new_from_file(f, headers_only=False)
def test_new_from_message(): gid = eccodes.codes_grib_new_from_samples("sh_sfc_grib1") message = eccodes.codes_get_message(gid) eccodes.codes_release(gid) assert len(message) == 9358 newgid = eccodes.codes_new_from_message(message) assert eccodes.codes_get(newgid, "packingType") == "spectral_complex" assert eccodes.codes_get(newgid, "gridType") == "sh" eccodes.codes_release(newgid) # This time read from a string rather than a file metar_str = "METAR LQMO 022350Z 09003KT 6000 FEW010 SCT035 BKN060 08/08 Q1003=" newgid = eccodes.codes_new_from_message(metar_str) cccc = eccodes.codes_get(newgid, "CCCC") assert cccc == "LQMO" eccodes.codes_release(newgid)
def get_grib_info(self, select_args): _gribs_for_utils = self._get_gids(**select_args) if len(_gribs_for_utils) > 0: # instant, avg, cumul. get last stepType available because first one is sometimes misleading type_of_step = codes_get(_gribs_for_utils[-1], 'stepType') self._mv = codes_get_double(_gribs_for_utils[0], 'missingValue') start_grib, end_grib, self._step_grib, self._step_grib2, self._change_step_at = self._find_start_end_steps( _gribs_for_utils) self._log("Grib input step %d [type of step: %s]" % (self._step_grib, type_of_step)) self._log('Gribs from %d to %d' % (start_grib, end_grib)) for g in _gribs_for_utils: codes_release(g) _gribs_for_utils = None del _gribs_for_utils info = GRIBInfo(input_step=self._step_grib, input_step2=self._step_grib2, change_step_at=self._change_step_at, type_of_param=type_of_step, start=start_grib, end=end_grib, mv=self._mv) return info # no messages found else: raise ApplicationException.get_exc(NO_MESSAGES, details="using " + str(select_args))
def get_subset_values(self, subset_nr , autoget_cval=False): # #[ """ request an array of values containing the values for a given subset for this bufr message """ if (self.msg_loaded == -1): raise NoMsgLoadedError data = [] field_names = self._get_abbr_names(subset_nr) print('field_names = ', field_names) print('DEBUG: names = ',self.get_names(subset_nr)) for field in field_names: if field[0] in string.digits: print('cannot get data for field: ',field) continue print('trying field name: ', field) s = eccodes.codes_get_size(self.bufr_id,field) if s==1: value = eccodes.codes_get(self.bufr_id,field) data.append(value) else: values = eccodes.codes_get_array(self.bufr_id,field) data.append(values[subset_nr]) return data
def cli(file_path): with open(file_path, 'rb') as f: handle = eccodes.codes_grib_new_from_file(f, headers_only=False) while handle is not None: date = eccodes.codes_get(handle, "dataDate") type_of_level = eccodes.codes_get(handle, "typeOfLevel") level = eccodes.codes_get(handle, "level") points = eccodes.codes_grib_find_nearest(handle, 39.92, 116.46, False, 1) point = points[0] print(date, type_of_level, level, " :", point.lat, point.lon, point.value, point.distance) eccodes.codes_release(handle) handle = eccodes.codes_grib_new_from_file(f, headers_only=False)
def test_any_read(): fpath = get_sample_fullpath("GRIB1.tmpl") if fpath is None: return with open(fpath, "rb") as f: msgid = eccodes.codes_any_new_from_file(f) assert eccodes.codes_get(msgid, "edition") == 1 assert eccodes.codes_get(msgid, "identifier") == "GRIB" assert eccodes.codes_get(msgid, "identifier", str) == "GRIB" eccodes.codes_release(msgid) fpath = get_sample_fullpath("BUFR3.tmpl") with open(fpath, "rb") as f: msgid = eccodes.codes_any_new_from_file(f) assert eccodes.codes_get(msgid, "edition") == 3 assert eccodes.codes_get(msgid, "identifier") == "BUFR" eccodes.codes_release(msgid)
def get_unexp_descr_list(self): # #[ get unexpanded descfriptor list ''' wrapper around self.bufr_obj.py_unexp_descr_list ''' if (self.msg_loaded == -1): raise NoMsgLoadedError n = eccodes.codes_get(self.bufr_id, 'numberOfUnexpandedDescriptors') if n==1: list_of_unexp_descr = [eccodes.codes_get(self.bufr_id, 'unexpandedDescriptors'),] else: list_of_unexp_descr = eccodes.codes_get_array(self.bufr_id, 'unexpandedDescriptors') return list_of_unexp_descr
def get(self, name): try: if name == "values": return eccodes.codes_get_values(self.handle) if name in ("distinctLatitudes", "distinctLongitudes"): return eccodes.codes_get_double_array(self.handle, name) return eccodes.codes_get(self.handle, name) except eccodes.KeyValueNotFoundError: return None
def get_num_subsets(self): # #[ """ request the number of subsets in the current BUFR message """ if (self.msg_loaded == -1): raise NoMsgLoadedError return eccodes.codes_get(self.bufr_id,"numberOfSubsets")
def get(self, key, ktype=None): """Get value of a given key as its native or specified type.""" # if self.missing(key): # raise KeyError("Value of key %s is MISSING." % key) if eccodes.codes_get_size(self.codes_id, key) > 1: ret = eccodes.codes_get_array(self.codes_id, key, ktype) else: ret = eccodes.codes_get(self.codes_id, key, ktype) return ret
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 cli(file_path): with open(file_path, 'rb') as f: handle = eccodes.codes_grib_new_from_file(f, headers_only=False) date = eccodes.codes_get(handle, "dataDate") type_of_level = eccodes.codes_get(handle, "typeOfLevel") level = eccodes.codes_get(handle, "level") iter_id = eccodes.codes_grib_iterator_new(handle, 0) while 1: result = eccodes.codes_grib_iterator_next(iter_id) if not result: break [lat, lon, value] = result print(lat, lon, value) eccodes.codes_grib_iterator_delete(iter_id) eccodes.codes_release(handle)
def test_scalar(file_path, modelvar_file_path): test_cases = [ TestCase( query=QueryOption(parameter="t", level_type="pl", level=1.5), expected_keys=dict(typeOfLevel="isobaricInhPa", level=1) ), TestCase( query=QueryOption(parameter="t", level_type="isobaricInhPa", level=850), expected_keys=dict(typeOfLevel="isobaricInhPa", level=850) ), TestCase( query=QueryOption(parameter="tmax", level_type="heightAboveGround", level=2), expected_keys=dict(typeOfLevel="heightAboveGround", level=2) ) ] for test_case in test_cases: message = load_message_from_file( file_path, **asdict(test_case.query) ) assert message is not None for key, expected_value in test_case.expected_keys.items(): assert eccodes.codes_get(message, key) == expected_value eccodes.codes_release(message) test_cases = [ TestCase( query=QueryOption(parameter="u", level_type={"typeOfFirstFixedSurface": 131}, level=10), expected_keys=dict(typeOfFirstFixedSurface=131, level=10) ) ] for test_case in test_cases: message = load_message_from_file( modelvar_file_path, **asdict(test_case.query) ) assert message is not None for key, expected_value in test_case.expected_keys.items(): assert eccodes.codes_get(message, key, ktype=int) == expected_value eccodes.codes_release(message)
def scan_for_gid(filename, short_name): filee = open(filename, "rb") for j in range(ec.codes_count_in_file(filee)): gid = ec.codes_grib_new_from_file(filee, headers_only=True) if ec.codes_get(gid, "shortName") == short_name: filee.close() return gid else: ec.codes_release(gid) filee.close() exit(1)
def confirm_packing_type(gribfile, packing_type): """Confirm that gribfile contains only GRIBs with specified packingType.""" comparisons = [] with open(gribfile) as infile: while True: gid = codes_grib_new_from_file(infile) if gid is None: break encoded_type = codes_get(gid, "packingType") codes_release(gid) comparisons.append(encoded_type == packing_type) return comparisons
def test_bufr_multi_element_constant_arrays(): eccodes.codes_bufr_multi_element_constant_arrays_off() bid = eccodes.codes_bufr_new_from_samples("BUFR3_local_satellite") eccodes.codes_set(bid, "unpack", 1) assert eccodes.codes_get_size(bid, "satelliteIdentifier") == 1 eccodes.codes_release(bid) eccodes.codes_bufr_multi_element_constant_arrays_on() bid = eccodes.codes_bufr_new_from_samples("BUFR3_local_satellite") eccodes.codes_set(bid, "unpack", 1) numSubsets = eccodes.codes_get(bid, "numberOfSubsets") assert eccodes.codes_get_size(bid, "satelliteIdentifier") == numSubsets eccodes.codes_release(bid)
def test_embedded_short_name(file_path): test_cases = [ TestCase(query=QueryOption("DEPR", "pl", 850)), TestCase(query=QueryOption( { "discipline": 0, "parameterCategory": 0, "parameterNumber": 7, }, "pl", 850)), ] messages = [] for test_case in test_cases: message = load_message_from_file(file_path, **asdict(test_case.query)) assert message is not None messages.append(message) assert eccodes.codes_get(messages[0], "count") == eccodes.codes_get( messages[1], "count") for message in messages: eccodes.codes_release(message)
def load_bytes_from_file( file_path: str or Path, parameter: str or typing.Dict, level_type: str = None, level: int = None, ) -> bytes or None: """ Load one message from grib file and return message's original bytes. Parameters ---------- file_path parameter level_type level Returns ------- bytes or None Examples -------- Load bytes of 850hPa temperature from GRAPES GFS GMF and create GRIB message using `eccodes.codes_new_from_message`. >>> file_path = "/sstorage1/COMMONDATA/OPER/NWPC/GRAPES_GFS_GMF/Prod-grib/2020031721/ORIG/gmf.gra.2020031800105.grb2" >>> message_bytes = load_bytes_from_file( ... file_path=file_path, ... parameter="t", ... level_type="pl", ... level=850, ... ) >>> message = eccodes.codes_new_from_message(message_bytes) >>> values = eccodes.codes_get_double_array(message, "values") >>> print(len(values)) 1036800 """ offset = 0 fixed_level_type = fix_level_type(level_type) with open(file_path, "rb") as f: while True: message_id = eccodes.codes_grib_new_from_file(f) length = eccodes.codes_get(message_id, "totalLength") if message_id is None: return None if not _check_message(message_id, parameter, fixed_level_type, level): eccodes.codes_release(message_id) offset += length continue return eccodes.codes_get_message(message_id)
def test_grib_index_new_from_file(tmpdir): fpath = get_sample_fullpath("GRIB1.tmpl") if fpath is None: return index_keys = ["shortName", "level", "number", "step", "referenceValue"] iid = eccodes.codes_index_new_from_file(fpath, index_keys) index_file = str(tmpdir.join("temp.grib.index")) eccodes.codes_index_write(iid, index_file) key = "level" assert eccodes.eccodes.codes_index_get_size(iid, key) == 1 # Cannot get the native type of a key from an index # so right now the default is str. assert eccodes.codes_index_get(iid, key) == ("500", ) assert eccodes.codes_index_get(iid, key, int) == (500, ) assert eccodes.codes_index_get_long(iid, key) == (500, ) key = "referenceValue" refVal = 47485.4 assert eccodes.codes_index_get_double(iid, key) == (refVal, ) assert eccodes.codes_index_get(iid, key, float) == (refVal, ) eccodes.codes_index_select(iid, "level", 500) eccodes.codes_index_select(iid, "shortName", "z") eccodes.codes_index_select(iid, "number", 0) eccodes.codes_index_select(iid, "step", 0) eccodes.codes_index_select(iid, "referenceValue", refVal) gid = eccodes.codes_new_from_index(iid) assert eccodes.codes_get(gid, "edition") == 1 assert eccodes.codes_get(gid, "totalLength") == 107 eccodes.codes_release(gid) eccodes.codes_index_release(iid) iid2 = eccodes.codes_index_read(index_file) assert eccodes.codes_index_get(iid2, "shortName") == ("z", ) eccodes.codes_index_release(iid2)
def cli(file_path): index_id = eccodes.codes_index_new_from_file(str(file_path), ["paramId"]) eccodes.codes_index_add_file(index_id, str(file_path)) param_size = eccodes.codes_index_get_size(index_id, "paramId") print("param id count:", param_size) param_id_list = eccodes.codes_index_get(index_id, "paramId") print("param id list:", param_id_list) eccodes.codes_index_select(index_id, "paramId", '131') handle = eccodes.codes_new_from_index(index_id) while handle is not None: short_name = eccodes.codes_get(handle, "shortName") date = eccodes.codes_get(handle, "dataDate") type_of_level = eccodes.codes_get(handle, "typeOfLevel") level = eccodes.codes_get(handle, "level") print(short_name, date, type_of_level, level) eccodes.codes_release(handle) handle = eccodes.codes_new_from_index(index_id)
def get(self, name): # LOG.warn(str(self) + str(name)) if name in CHEAT: return CHEAT[name] try: if name == "values": return eccodes.codes_get_values(self.handle) size = eccodes.codes_get_size(self.handle, name) LOG.debug(f"{name}:{size}") if size and size > 1: return eccodes.codes_get_array(self.handle, name) return eccodes.codes_get(self.handle, name) except eccodes.KeyValueNotFoundError: return None
def gribs_match(left, right): """Check if GRIBs in both input files store the same data.""" comparisons = [] with open(left) as a, open(right) as b: while True: a_gid = codes_grib_new_from_file(a) if a_gid is None: break b_gid = codes_grib_new_from_file(b) if b_gid is None: comparisons.append(False) info("GRIBs contain unequal number of messages.") continue packing_errors = [0] try: packing_errors.append(codes_get(a_gid, "packingError")) packing_errors.append(codes_get(b_gid, "packingError")) except CodesInternalError: pass tolerance = max(packing_errors) a_values = codes_get_values(a_gid) b_values = codes_get_values(b_gid) comparisons.append(np.allclose(a_values, b_values, atol=tolerance)) return comparisons
def extract_msg_date_extremes(self, bufr, date_min=None, date_max=None): """Extract the minimum and maximum dates from a single bufr message.""" ec.codes_set(bufr, 'unpack', 1) size = ec.codes_get(bufr, 'numberOfSubsets') years = np.resize(ec.codes_get_array(bufr, 'year'), size) months = np.resize(ec.codes_get_array(bufr, 'month'), size) days = np.resize(ec.codes_get_array(bufr, 'day'), size) hours = np.resize(ec.codes_get_array(bufr, 'hour'), size) minutes = np.resize(ec.codes_get_array(bufr, 'minute'), size) seconds = np.resize(ec.codes_get_array(bufr, 'second'), size) for year, month, day, hour, minute, second in zip(years, months, days, hours, minutes, seconds): time_stamp = datetime(year, month, day, hour, minute, second) date_min = time_stamp if not date_min else min(date_min, time_stamp) date_max = time_stamp if not date_max else max(date_max, time_stamp) return date_min, date_max
def get_values(self, descr_nr, autoget_cval=False): # #[ """ request an array of values containing the values for a given descriptor number for all subsets NOTE: this may not work for templates using delayed replication. """ if (self.msg_loaded == -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_id,keyname) t = eccodes.codes_get_native_type(self.bufr_id, keyname) print('key:', keyname, 'size = ', s, 'type = ', t) if s==1: # or t==str: # values = eccodes.codes_get_string(bufr_id, keyname) values = [eccodes.codes_get(self.bufr_id, keyname),] else: values = eccodes.codes_get_array(self.bufr_id, keyname) return values
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