1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! A cookbook of examples for GRIB2 data handling.
//!
//! # Table of contents
//!
//! 1. [Comparison of various GRIB2 data library/tool operations][cmp]
//!     * [Listing all submessages inside][cmp-listing]
//!     * [Finding submessages inside that match some condition][cmp-finding]
//!     * [Extracting values with location info from a submessage][cmp-decoding]
//!
//! [cmp]: #comparison-of-various-grib2-data-librarytool-operations
//! [cmp-listing]: #listing-all-submessages-inside
//! [cmp-finding]: #finding-submessages-inside-that-match-some-condition
//! [cmp-decoding]: #extracting-values-with-location-info-from-a-submessage
//!
//! # Comparison of various GRIB2 data library/tool operations
//!
//! This section provides example code of data operations using some GRIB
//! processing libraries and tools.
//!
//! ## Listing all submessages inside
//!
//! GRIB tools from ecCodes:
//!
//! ```shell
//! $ grib_ls datafile.grib
//! ```
//!
//! wgrib2:
//!
//! ```shell
//! $ wgrib2 datafile.grib
//! ```
//!
//! pygrib:
//!
//! ```python
//! import pygrib
//!
//! path = "testdata/icon_global_icosahedral_single-level_2021112018_000_TOT_PREC.grib2"
//! grib = pygrib.open(path)
//! for submessage in grib:
//!     print(submessage)
//! ```
//!
//! grib-rs:
//!
//! ```rust
//! use std::{fs::File, io::BufReader, path::Path};
//!
//! use grib::codetables::{CodeTable4_2, Lookup};
//!
//! fn list_submessages<P>(path: P)
//! where
//!     P: AsRef<Path>,
//! {
//!     let f = File::open(path).unwrap();
//!     let f = BufReader::new(f);
//!
//!     let grib2 = grib::from_reader(f).unwrap();
//!
//!     for (_index, submessage) in grib2.iter() {
//!         let discipline = submessage.indicator().discipline;
//!         let category = submessage.prod_def().parameter_category().unwrap();
//!         let parameter = submessage.prod_def().parameter_number().unwrap();
//!         let parameter = CodeTable4_2::new(discipline, category).lookup(usize::from(parameter));
//!
//!         let forecast_time = submessage.prod_def().forecast_time().unwrap();
//!
//!         let (first, _second) = submessage.prod_def().fixed_surfaces().unwrap();
//!         let elevation_level = first.value();
//!
//!         println!(
//!             "{:<31} {:>14} {:>17}",
//!             parameter.to_string(),
//!             forecast_time.to_string(),
//!             elevation_level
//!         );
//!     }
//! }
//!
//! fn main() {
//!     let path = "testdata/icon_global_icosahedral_single-level_2021112018_000_TOT_PREC.grib2";
//!     list_submessages(&path);
//! }
//! ```
//!
//! gribber:
//!
//! ```shell
//! $ gribber list datafile.grib
//! ```
//!
//! ## Finding submessages inside that match some condition
//!
//! GRIB tools from ecCodes:
//!
//! ```shell
//! $ grib_ls -w forecastTime=0,level=850,shortName=u datafile.grib
//! ```
//!
//! wgrib2:
//!
//! ```shell
//! $ wgrib2 datafile.grib -match ':3 hour fcst:'
//! ```
//!
//! pygrib:
//!
//! ```python
//! import pygrib
//!
//! path = "testdata/icon_global_icosahedral_single-level_2021112018_000_TOT_PREC.grib2"
//! grib = pygrib.open(path)
//! for submessage in grib.select(forecastTime=3):
//!     print(submessage)
//! ```
//!
//! grib-rs:
//!
//! ```rust
//! use std::{fs::File, io::BufReader, path::Path};
//!
//! use grib::{codetables::grib2::*, ForecastTime, Name};
//!
//! fn find_submessages<P>(path: P, forecast_time_hours: u32)
//! where
//!     P: AsRef<Path>,
//! {
//!     let f = File::open(path).unwrap();
//!     let f = BufReader::new(f);
//!
//!     let grib2 = grib::from_reader(f).unwrap();
//!
//!     for (index, submessage) in grib2.iter() {
//!         let ft = submessage.prod_def().forecast_time();
//!         match ft {
//!             Some(ForecastTime {
//!                 unit: Name(Table4_4::Hour),
//!                 value: hours,
//!             }) => {
//!                 if hours == forecast_time_hours {
//!                     println!("{}.{}: {}", index.0, index.1, hours);
//!                 }
//!             }
//!             _ => {}
//!         }
//!     }
//! }
//!
//! fn main() {
//!     let path = "testdata/icon_global_icosahedral_single-level_2021112018_000_TOT_PREC.grib2";
//!     find_submessages(&path, 3);
//! }
//! ```
//!
//! gribber:
//!
//! ```shell
//! $ gribber list datafile.grib | grep '3 Hour'
//! ```
//!
//! (gribber's API for finding submessages is still in the conceptual stage and
//! is not yet available.)
//!
//! ## Extracting values with location info from a submessage
//!
//! GRIB tools from ecCodes:
//!
//! ```shell
//! $ grib_get_data -w forecastTime=0,count=1 datafile.grib
//! ```
//!
//! wgrib2 (creating a flat binary file of values):
//!
//! ```shell
//! $ wgrib2 -d 1.1 -order we:ns -no_header -bin output.bin datafile.grib
//! ```
//!
//! pygrib:
//!
//! ```python
//! import pygrib
//!
//! path = "datafile.grib"
//! grib = pygrib.open(path)
//! submessage = grib.message(1)
//! lats, lons = submessage.latlons()
//! values = submessage.values
//! print((lats, lons, values))
//! ```
//!
//! grib-rs:
//!
//! ```rust
//! use std::{
//!     fs::File,
//!     io::{BufReader, Read, Write},
//!     path::Path,
//! };
//!
//! use grib::codetables::{grib2::*, *};
//!
//! fn decode_layer<P>(path: P, message_index: (usize, usize))
//! where
//!     P: AsRef<Path>,
//! {
//!     let f = File::open(path).unwrap();
//!     let f = BufReader::new(f);
//!
//!     let grib2 = grib::from_reader(f).unwrap();
//!     let (_index, submessage) = grib2
//!         .iter()
//!         .find(|(index, _)| *index == message_index)
//!         .ok_or("no such index")
//!         .unwrap();
//!
//!     let latlons = submessage.latlons().unwrap();
//!     let decoder = grib::Grib2SubmessageDecoder::from(submessage).unwrap();
//!     let values = decoder.dispatch().unwrap();
//!
//!     for ((lat, lon), value) in latlons.zip(values) {
//!         println!("{lat} {lon} {value}");
//!     }
//! }
//!
//! fn main() {
//!     let path = "testdata/gdas.t12z.pgrb2.0p25.f000.0-10.xz";
//!
//!     let mut buf = Vec::new();
//!     let mut out = tempfile::NamedTempFile::new().unwrap();
//!
//!     let f = File::open(path).unwrap();
//!     let f = BufReader::new(f);
//!     let mut f = xz2::bufread::XzDecoder::new(f);
//!     f.read_to_end(&mut buf).unwrap();
//!     out.write_all(&buf).unwrap();
//!
//!     decode_layer(&out.path(), (0, 0));
//! }
//! ```
//!
//! gribber (showing values along with lat/lon info):
//!
//! ```shell
//! $ gribber decode datafile.grib 0.0
//! ```
//!
//! gribber (creating a flat binary file of values):
//!
//! ```shell
//! $ gribber decode -b output.bin datafile.grib 0.0
//! ```