Struct eccodes::codes_handle::KeyedMessage[][src]

pub struct KeyedMessage { /* fields omitted */ }
Expand description

Structure used to access keys inside the GRIB file message. All data (including data values) contained by the file can only be accessed through the message and keys.

The structure implements Clone trait which comes with a memory overhead. You should take care that your system has enough memory before cloning KeyedMessage.

Keys inside the message can be accessed directly with read_key() function or using FallibleIterator. The function find_nearest() allows to get the values of four nearest gridpoints to requested coordinates. FallibleIterator parameters can be set with set_iterator_parameters() to specify the subset of keys to iterate over.

Implementations

Function that allows to set the flags and namespace for FallibleIterator. Must be called before calling the iterator. Changing the parameters after first call of next() will have no effect on the iterator.

The flags are set by providing any combination of KeysIteratorFlags inside a vector. Check the documentation for the details of each flag meaning.

Namespace is set simply as string, eg. "ls", "time", "parameter", "geography", "statistics". Invalid namespace will result in empty iterator.

Default parameters are AllKeys flag and "" namespace, which implies iteration over all keys available in the message.

Example

let file_path = Path::new("./data/iceland.grib");
let product_kind = ProductKind::GRIB;

let mut handle = CodesHandle::new_from_file(file_path, product_kind).unwrap();
let mut current_message = handle.next().unwrap().unwrap();


let flags = vec![
    KeysIteratorFlags::AllKeys,
    KeysIteratorFlags::SkipOptional,
    KeysIteratorFlags::SkipReadOnly,
    KeysIteratorFlags::SkipDuplicates,
];

let namespace = "geography".to_owned();

current_message.set_iterator_parameters(flags, namespace);


while let Some(key) = current_message.next().unwrap() {
    println!("{:?}", key);
}

Method to get a Key with provided name from the KeyedMessage.

This function takes a key name and returns the key value as Key if requested key exists. Check the Key documentation for details of possible key types.

Example

let file_path = Path::new("./data/iceland.grib");
let product_kind = ProductKind::GRIB;

let mut handle = CodesHandle::new_from_file(file_path, product_kind).unwrap();
let message = handle.next().unwrap().unwrap();
let message_short_name = message.read_key("shortName").unwrap();

assert_eq!(message_short_name.value, Str("msl".to_string()));

This function will try to retrieve the key of native string type as string even when the nul byte is not positioned at the end of key value.

If retrieving the key value in native type fails this function will try to read the requested key as bytes.

Errors

Returns CodesInternal::CodesNotFound wrapped in CodesError::Internal when a key of given name has not been found in the message.

Returns CodesError::MissingKey when a given key has a missing type.

Returns CodesError::Internal when one of internal ecCodes functions to read the key fails.

Returns CodesError::CstrUTF8 and CodesError::NulChar when the string returned by ecCodes library cannot be parsed as valid UTF8 Rust string.

Panics

Panics when the size of given key is lower than 1. This indicates corrupted data file, bug in the crate or bug in the ecCodes library. If you encounter this panic please check if your file is correct and report it on Github.

Function to write given KeyedMessage to a file at provided path. If file does not exists it will be created. If append is set to true file will be opened in append mode and no data will be overwritten (useful when writing mutiple messages to one file).

Example

let in_path = Path::new("./data/iceland-levels.grib");
let out_path  = Path::new("./data/iceland-temperature-levels.grib");

let handle = CodesHandle::new_from_file(in_path, GRIB)?;

let mut t_levels =
    handle.filter(|msg| Ok(msg.read_key("shortName")?.value == Str("t".to_string())));

while let Some(msg) = t_levels.next()? {
    msg.write_to_file(out_path, true)?;
}

Errors

Returns CodesError::FileHandlingInterrupted when the file cannot be opened, created or correctly written.

Returns CodesInternal when internal ecCodes function returns non-zero code.

Function to set specified Key inside the KeyedMessage. This function automatically matches the KeyType and uses adequate internal ecCodes function to set the key. The message must be mutable to use this function.

User must provide the Key with correct type, otherwise error will occur. Note that not all keys can be set, for example "name" and shortName are read-only. Trying to set such keys will result in error. Some keys can also be set using a non-native type (eg. centre), but read_key() function will only read then in native type.

Refer to ecCodes library documentation for more details.

Example

let file_path = Path::new("./data/iceland.grib");

let mut handle = CodesHandle::new_from_file(file_path, GRIB).unwrap();
let mut current_message = handle.next().unwrap().unwrap();

let new_key = Key {
    name: "centre".to_string(),
    value: KeyType::Str("cnmc".to_string()),
};

current_message.write_key(new_key).unwrap();

Errors

This method will return CodesInternal when internal ecCodes function returns non-zero code.

Function to get four NearestGridpoints of a point represented by requested coordinates.

The inputs are latitude and longitude of requested point in respectively degrees north and degreed east.

In the output gridpoints, the value field refers to parameter held by the KeyedMessage for which the function is called in adequate units, coordinates are in degrees north/east, and distance field represents the distance between requested point and output point in kilometers.

Example

let file_path = Path::new("./data/iceland.grib");
let product_kind = ProductKind::GRIB;

let mut handle = CodesHandle::new_from_file(file_path, product_kind).unwrap();
let mut msg = handle.next().unwrap().unwrap();


let out = msg.find_nearest(64.13, -21.89).unwrap();

Errors

This function returns CodesInternal when one of ecCodes function returns the non-zero code.

Trait Implementations

Custom function to clone the KeyedMessage. This function comes with memory overhead. During clone iterator flags and namespace are not copied, and the iterator is reset.

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. This method calls codes_handle_delete(), codes_keys_iterator_delete() codes_grib_nearest_delete() from ecCodes for graceful cleanup. However in some edge cases ecCodes can return non-zero code. In such case all pointers and file descriptors are safely deleted. However memory leaks can still occur.

If any function called in the destructor returns an error warning will appear in log. If bugs occurs during CodesHandle drop please enable log output and post issue on Github.

Technical note: delete functions in ecCodes can only fail with CodesInternalError when other functions corrupt the inner memory of pointer, in that case memory leak is possible. In case of corrupt pointer segmentation fault will occur. The pointers are cleared at the end of drop as they ar not not functional despite the result of delete functions.

FallibleIterator implementation for KeyedMessage to access keyes inside message. Mainly useful to discover what keys are present inside the message.

This function internally calls read_key() function so it is probably more efficient to call that function directly only for keys you are interested in.

FallibleIterator is used instead of classic Iterator because internal ecCodes functions can return internal error in some edge-cases. The usage of FallibleIterator is sligthly different than usage of Iterator, check its documentation for more details.

Example

let file_path = Path::new("./data/iceland.grib");
let product_kind = ProductKind::GRIB;

let mut handle = CodesHandle::new_from_file(file_path, product_kind).unwrap();
let mut current_message = handle.next().unwrap().unwrap();

while let Some(key) = current_message.next().unwrap() {
    println!("{:?}", key);
}

Errors

The next() method will return CodesInternal when internal ecCodes function returns non-zero code.

The type being iterated over.

The error type.

Advances the iterator and returns the next value. Read more

Returns bounds on the remaining length of the iterator. Read more

Consumes the iterator, returning the number of remaining items.

Returns the last element of the iterator.

Returns the nth element of the iterator.

Returns an iterator starting at the same point, but stepping by the given amount at each iteration. Read more

Returns an iterator which yields the elements of this iterator followed by another. Read more

Returns an iterator that yields pairs of this iterator’s and another iterator’s values. Read more

Returns an iterator which applies a fallible transform to the elements of the underlying iterator. Read more

Calls a fallible closure on each element of an iterator.

Returns an iterator which uses a predicate to determine which values should be yielded. The predicate may fail; such failures are passed to the caller. Read more

Returns an iterator which both filters and maps. The closure may fail; such failures are passed along to the consumer. Read more

Returns an iterator which yields the current iteration count as well as the value. Read more

Returns an iterator that can peek at the next element without consuming it. Read more

Returns an iterator that skips elements based on a predicate.

Returns an iterator that yields elements based on a predicate.

Returns an iterator which skips the first n values of this iterator.

Returns an iterator that yields only the first n values of this iterator. Read more

Returns an iterator which applies a stateful map to values of this iterator. Read more

Returns an iterator which maps this iterator’s elements to iterators, yielding those iterators’ values.

Returns an iterator which flattens an iterator of iterators, yielding those iterators’ values.

Returns an iterator which yields this iterator’s elements and ends after the first Ok(None). Read more

Returns an iterator which passes each element to a closure before returning it.

Borrow an iterator rather than consuming it. Read more

Transforms the iterator into a collection. Read more

Transforms the iterator into two collections, partitioning elements by a closure.

Applies a function over the elements of the iterator, producing a single final value. Read more

Applies a function over the elements of the iterator, producing a single final value. Read more

Determines if all elements of this iterator match a predicate.

Determines if any element of this iterator matches a predicate.

Returns the first element of the iterator that matches a predicate.

Applies a function to the elements of the iterator, returning the first non-None result.

Returns the position of the first element of this iterator that matches a predicate. The predicate may fail; such failures are returned to the caller. Read more

Returns the maximal element of the iterator.

Returns the element of the iterator which gives the maximum value from the function. Read more

Returns the element that gives the maximum value with respect to the function.

Returns the minimal element of the iterator.

Returns the element of the iterator which gives the minimum value from the function. Read more

Returns the element that gives the minimum value with respect to the function.

Returns an iterator that yields this iterator’s items in the opposite order. Read more

Converts an iterator of pairs into a pair of containers.

Returns an iterator which clones all of its elements.

Returns an iterator which repeas this iterator endlessly.

Lexicographically compares the elements of this iterator to that of another. Read more

Lexicographically compares the elements of this iterator to that of another. Read more

Determines if the elements of this iterator are equal to those of another. Read more

Determines if the elements of this iterator are not equal to those of another. Read more

Determines if the elements of this iterator are lexicographically less than those of another. Read more

Determines if the elements of this iterator are lexicographically less than or equal to those of another. Read more

Determines if the elements of this iterator are lexicographically greater than those of another. Read more

Determines if the elements of this iterator are lexicographically greater than or equal to those of another. Read more

Returns a normal (non-fallible) iterator over Result<Item, Error>.

Returns an iterator which applies a transform to the errors of the underlying iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The elements of the iterator.

The error value of the iterator.

The iterator.

Creates a fallible iterator from a value.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.