Grib2SubmessageDecoder

Struct Grib2SubmessageDecoder 

Source
pub struct Grib2SubmessageDecoder { /* private fields */ }
Expand description

Decoder for grid point values of GRIB2 submessages.

§Examples

use grib::Grib2SubmessageDecoder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f =
        std::fs::File::open("testdata/CMC_glb_TMP_ISBL_1_latlon.24x.24_2021051800_P000.grib2")?;
    let f = std::io::BufReader::new(f);
    let grib2 = grib::from_reader(f)?;
    let (_index, first_submessage) = grib2.iter().next().unwrap();

    let decoder = Grib2SubmessageDecoder::from(first_submessage)?;
    let mut decoded = decoder.dispatch()?;
    assert_eq!(decoded.size_hint(), (1126500, Some(1126500)));

    let first_value = decoded.next();
    assert_eq!(first_value.map(|f| f.round()), Some(236.0_f32));

    let last_value = decoded.nth(1126498);
    assert_eq!(last_value.map(|f| f.round()), Some(286.0_f32));

    let next_to_last_value = decoded.next();
    assert_eq!(next_to_last_value, None);
    Ok(())
}

If the byte sequences for Sections 5, 6, and 7 of the GRIB2 data are known, and the number of grid points (described in Section 3) is also known, it is also possible to create a decoder instance by passing them to Grib2SubmessageDecoder::new. The example above is equivalent to the following:

use std::io::Read;

use grib::Grib2SubmessageDecoder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f =
        std::fs::File::open("testdata/CMC_glb_TMP_ISBL_1_latlon.24x.24_2021051800_P000.grib2")?;
    let mut f = std::io::BufReader::new(f);
    let mut buf = Vec::new();
    f.read_to_end(&mut buf)?;

    let decoder = Grib2SubmessageDecoder::new(
        1126500,
        buf[0x0000008f..0x000000a6].to_vec(),
        buf[0x000000a6..0x000000ac].to_vec(),
        buf[0x000000ac..0x0003d6c7].to_vec(),
    )?;
    let mut decoded = decoder.dispatch()?;
    assert_eq!(decoded.size_hint(), (1126500, Some(1126500)));

    let first_value = decoded.next();
    assert_eq!(first_value.map(|f| f.round()), Some(236.0_f32));

    let last_value = decoded.nth(1126498);
    assert_eq!(last_value.map(|f| f.round()), Some(286.0_f32));

    let next_to_last_value = decoded.next();
    assert_eq!(next_to_last_value, None);
    Ok(())
}

Implementations§

Source§

impl Grib2SubmessageDecoder

Source

pub fn new( num_points_total: usize, sect5_bytes: Vec<u8>, sect6_bytes: Vec<u8>, sect7_bytes: Vec<u8>, ) -> Result<Self, GribError>

Creates an instance from the number of grid points (described in Section 3) and byte sequences for Sections 5, 6, and 7 of the GRIB2 data.

For code examples, refer to the description of this struct.

Source

pub fn from<R: Grib2Read>( submessage: SubMessage<'_, R>, ) -> Result<Self, GribError>

Sets up a decoder for grid point values of submessage.

Source

pub fn dispatch( &self, ) -> Result<Grib2DecodedValues<'_, impl Iterator<Item = f32> + '_>, GribError>

Dispatches a decoding process and gets an iterator of decoded values.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.