Skip to content

Commit ffb89c2

Browse files
committed
finish readme
1 parent bab0d0f commit ffb89c2

File tree

1 file changed

+54
-42
lines changed

1 file changed

+54
-42
lines changed

README.md

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,58 +60,72 @@ export LD_LIBRARY_PATH=<your_eccodes_path>/lib
6060

6161
### Working with GRIB files
6262

63-
To access a GRIB file you need to create `CodesHandle` with one of provided constructors.
63+
To access a GRIB file you need to create `CodesFile` with one of the provided constructors.
6464

65-
GRIB files consist of messages which represent data fields at specific time and level.
66-
Messages are represented by the `KeyedMessage` structure.
65+
ecCodes represents GRIB files as a set of separate messages, each containing data fields at specific time and level.
66+
Messages are represented here by a generic `CodesMessage` structure, but you shouldn't use it directly.
67+
Instead use `RefMessage`, `ArcMessage` or `BufMessage` to operations - check the docs for more information when to use each.
6768

68-
`CodesHandle` implements `FallibleStreamingIterator`
69-
which allows you to iterate over messages in the file. The iterator returns `&KeyedMessage` which valid until next iteration.
70-
`KeyedMessage` implements several methods to access the data as needed, most of those can be called directly on `&KeyedMessage`.
71-
You can also use `try_clone()` to clone the message and prolong its lifetime.
69+
To obtain `CodesMessage` from `CodesFile` you need to create an instance of `RefMessageIter` or `ArcMessageIter` using
70+
`CodesFile::ref_message_iter()` or `CodesFile::arc_message_iter()`.
71+
Those structures implement `FallibleIterator`, please check its documentation if you are not familiar with it.
7272

73-
Data defining and contained by `KeyedMessage` is represented by `Key`s.
74-
You can read them directly with `read_key()`, use `KeysIterator`
75-
to iterate over them or use `CodesNearest` to get the values of four nearest gridpoints for given coordinates.
73+
`CodesMessage` implements several methods to access the data as needed, most of those can be called directly.
74+
Almost all methods can be called on any `CodesMessage`, except for `KeyWrite` operations, which can be called only on `BufMessage`
75+
to avoid confusion if written keys are save to file or not.
7676

77-
You can also modify the message with `write_key()` and write it to a new file with `write_to_file()`.
77+
Data contained by `CodesMessage` is represented as *keys* (like in dictionary).
78+
Keys can be read with static types using `read_key()` or with dynamic types
79+
using `read_key_dynamic()`.
80+
To discover what keys are present in a message use `KeysIterator`.
7881

79-
#### Example
82+
With `ndarray` feature (enabled by default) you can also read `CodesMessage` into `ndarray` using `to_ndarray()`
83+
and `to_lons_lats_values()`.
8084

81-
```rust
82-
// We are reading the mean sea level pressure for 4 gridpoints
83-
// nearest to Reykjavik (64.13N, -21.89E) for 1st June 2021 00:00 UTC
84-
// from ERA5 Climate Reanalysis
85+
You can use `CodesNearest` to get the data values of four nearest gridpoints for given coordinates.
86+
87+
To modify keys within the message use `write_key_unchecked()`.
88+
To save that modified message use `write_to_file()`.
8589

86-
use eccodes::{ProductKind, CodesHandle, KeyType};
87-
use eccodes::FallibleStreamingIterator;
90+
#### Example 1 - Reading GRIB file
8891

89-
// Open the GRIB file and create the CodesHandle
90-
let file_path = Path::new("./data/iceland.grib");
91-
let product_kind = ProductKind::GRIB;
92-
let mut handle = CodesHandle::new_from_file(file_path, product_kind)?;
92+
In this example we are reading mean sea level pressure for 4 gridpoints nearest to
93+
Reykjavik (64.13N, -21.89E) for 1st June 2021 00:00 UTC from ERA5 Climate Reanalysis.
94+
95+
```rust
96+
use eccodes::{CodesFile, FallibleIterator, KeyRead, ProductKind};
9397

98+
// Open the GRIB file and create the CodesFile
99+
let mut handle = CodesFile::new_from_file("./data/iceland.grib", ProductKind::GRIB)?;
94100
// Use iterator to find a message with shortName "msl" and typeOfLevel "surface"
95101
// We can use while let or for_each() to iterate over the messages
96-
while let Some(msg) = handle.next()? {
97-
if msg.read_key("shortName")?.value == KeyType::Str("msl".to_string())
98-
&& msg.read_key("typeOfLevel")?.value == KeyType::Str("surface".to_string()) {
99-
102+
while let Some(msg) = handle.ref_message_iter().next()? {
103+
// We need to specify the type of key we read
104+
let short_name: String = msg.read_key("shortName")?;
105+
let type_of_level: String = msg.read_key("typeOfLevel")?;
106+
if short_name == "msl" && type_of_level == "surface" {
100107
// Create CodesNearest for given message
101-
let nearest_gridpoints = msg.codes_nearest()?
108+
let nearest_gridpoints = msg
109+
.codes_nearest()?
102110
// Find the nearest gridpoints to Reykjavik
103111
.find_nearest(64.13, -21.89)?;
104112
// Print value and distance of the nearest gridpoint
105-
println!("value: {}, distance: {}",
106-
nearest_gridpoints[3].value,
107-
nearest_gridpoints[3].distance);
113+
println!(
114+
"value: {}, distance: {}",
115+
nearest_gridpoints[3].value, nearest_gridpoints[3].distance
116+
);
108117
}
109118
}
110119
```
111120

121+
### Multithreading
122+
123+
Since v0.14 messages inside a file can be read as `ArcMessage` which
124+
allows to move and share the message across threads.
125+
112126
### Writing GRIB files
113127

114-
The crate provides a basic support for setting `KeyedMessage` keys
128+
The crate provides a basic support for setting `CodesMessage` keys
115129
and writing GRIB files. The easiest (and safest) way to create a
116130
new custom message is to copy existing one from other GRIB file,
117131
modify the keys and write to new file.
@@ -125,11 +139,11 @@ This crate aims to return error whenever possible, even if the error is caused b
125139
As ecCodes is often used in scientific applications with long and extensive jobs,
126140
this allows the user to handle the error in the way that suits them best and not risk crashes.
127141

128-
All error descriptions are provided in the `errors` module.
129-
Destructors, which cannot panic, report errors through the `log` crate.
142+
All error descriptions are provided in the [`errors`] module.
143+
Destructors, which cannot panic, report errors through `tracing` and `log` crate.
130144

131145
None of the functions in this crate explicitly panics.
132-
However, users should not that dependencies might panic in some edge cases.
146+
However, users should be aware that dependencies (eg. `ndarray`) might panic in some edge cases.
133147

134148
## Safety
135149

@@ -139,19 +153,17 @@ Moreover, pointers are always checked for null before being dereferenced.
139153
That said, neither main developer nor contributors have expertise in unsafe Rust and bugs might have
140154
slipped through. We are also not responsible for bugs in the ecCodes library.
141155

142-
If you find a bug or have a suggestion, feel free to discuss it on Github.
156+
**For critical applications always perform extensive testing before using this crate in production.**
143157

144-
## Features
158+
If you find a bug or have a suggestion, feel free to discuss it on Github.
145159

146-
- `ndarray` - enables support for converting `KeyedMessage` to `ndarray::Array`.
147-
This feature is enabled by default. It is currently tested only with simple lat-lon grids.
160+
## Feature Flags
148161

149-
- `experimental_index` - enables support for creating and using index files for GRIB files.
150-
This feature experimental and disabled by default. If you want to use it, please read
151-
the information provided in `codes_index` documentation.
162+
- `ndarray` - enables support for converting [`CodesMessage`](codes_message::CodesMessage) to [`ndarray::Array`].
163+
This feature is enabled by default. It is currently tested only with simple lat-lon grids.
152164

153165
- `docs` - builds the crate without linking ecCodes, particularly useful when building the documentation
154-
on [docs.rs](https://docs.rs/). For more details check documentation of [eccodes-sys](https://crates.io/crates/eccodes-sys).
166+
on [docs.rs](https://docs.rs/). For more details check documentation of [eccodes-sys](https://crates.io/crates/eccodes-sys).
155167

156168
To build your own crate with this crate as dependency on docs.rs without linking ecCodes add following lines to your `Cargo.toml`
157169

0 commit comments

Comments
 (0)