-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfind_layers.rs
44 lines (37 loc) · 1.32 KB
/
find_layers.rs
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
use std::{env, error::Error, fs::File, io::BufReader, path::Path};
use grib::{codetables::grib2::*, ForecastTime, Name};
fn main() -> Result<(), Box<dyn Error>> {
// This example shows how to find layers in a GRIB2 message.
// Take the first argument as an input file path and the second argument as
// forecast time in hours.
let mut args = env::args().skip(1);
if let (Some(file_path), Some(forecast_time)) = (args.next(), args.next()) {
let forecast_time = forecast_time.parse::<u32>()?;
find_layers(file_path, forecast_time)
} else {
panic!("Usage: find_layers <path> <forecast_time>");
}
}
fn find_layers<P>(path: P, forecast_time_hours: u32) -> Result<(), Box<dyn Error>>
where
P: AsRef<Path>,
{
// Open the input file in a normal way.
let f = File::open(path)?;
let f = BufReader::new(f);
// Read with the reader.
let grib2 = grib::from_reader(f)?;
for (index, submessage) in grib2.iter() {
let ft = submessage.prod_def().forecast_time();
if matches!(
ft,
Some(ForecastTime {
unit: Name(Table4_4::Hour),
value: hours,
}) if hours == forecast_time_hours
) {
println!("{}.{}\n{}", index.0, index.1, submessage.describe());
}
}
Ok(())
}