SubMessage

Struct SubMessage 

Source
pub struct SubMessage<'a, R>(pub SubMessageSection<'a>, pub SubMessageSection<'a>, pub Option<SubMessageSection<'a>>, pub SubMessageSection<'a>, pub SubMessageSection<'a>, pub SubMessageSection<'a>, pub SubMessageSection<'a>, pub SubMessageSection<'a>, pub SubMessageSection<'a>, _);

Tuple Fields§

§0: SubMessageSection<'a>§1: SubMessageSection<'a>§2: Option<SubMessageSection<'a>>§3: SubMessageSection<'a>§4: SubMessageSection<'a>§5: SubMessageSection<'a>§6: SubMessageSection<'a>§7: SubMessageSection<'a>§8: SubMessageSection<'a>

Implementations§

Source§

impl<R> SubMessage<'_, R>

Source

pub fn parameter(&self) -> Option<Parameter>

Returns the product’s parameter.

In the context of GRIB products, parameters refer to weather elements such as air temperature, air pressure, and humidity, and other physical quantities.

§Examples
use std::{
    fs::File,
    io::{BufReader, Read},
};

use grib::codetables::NCEP;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut buf = Vec::new();

    let f = File::open("testdata/gdas.t12z.pgrb2.0p25.f000.0-10.xz")?;
    let f = BufReader::new(f);
    let mut f = xz2::bufread::XzDecoder::new(f);
    f.read_to_end(&mut buf)?;

    let f = std::io::Cursor::new(buf);
    let grib2 = grib::from_reader(f)?;

    let mut iter = grib2.iter();
    let (_, message) = iter.next().ok_or_else(|| "first message is not found")?;

    let param = message.parameter();
    assert_eq!(
        param,
        Some(grib::Parameter {
            discipline: 0,
            centre: 7,
            master_ver: 2,
            local_ver: 1,
            category: 3,
            num: 1
        })
    );
    let param = param.unwrap();
    assert_eq!(
        param.description(),
        Some("Pressure reduced to MSL".to_owned())
    );
    assert!(param.is_identical_to(NCEP::PRMSL));
    Ok(())
}
Source

pub fn indicator(&self) -> &Indicator

Source

pub fn identification(&self) -> &Identification

Source

pub fn grid_def(&self) -> &GridDefinition

Source

pub fn prod_def(&self) -> &ProdDefinition

Source

pub fn repr_def(&self) -> &ReprDefinition

Source

pub fn describe(&self) -> String

Source

pub fn section1(&self) -> Result<Section1, GribError>

Provides access to the parameters in Section 1.

§Examples
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f = std::fs::File::open(
        "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin",
    )?;
    let f = std::io::BufReader::new(f);
    let grib2 = grib::from_reader(f)?;
    let (_index, first_submessage) = grib2.iter().next().unwrap();

    let actual = first_submessage.section1();
    let expected = Ok(grib::def::grib2::Section1 {
        header: grib::def::grib2::SectionHeader {
            len: 21,
            sect_num: 1,
        },
        payload: grib::def::grib2::Section1Payload {
            centre_id: 34,
            subcentre_id: 0,
            master_table_version: 5,
            local_table_version: 1,
            ref_time_significance: 0,
            ref_time: grib::def::grib2::RefTime {
                year: 2016,
                month: 8,
                day: 22,
                hour: 2,
                minute: 0,
                second: 0,
            },
            prod_status: 0,
            data_type: 2,
            optional: None,
        },
    });
    assert_eq!(actual, expected);

    Ok(())
}
Source

pub fn section3(&self) -> Result<Section3, GribError>

Provides access to the parameters in Section 3.

§Examples
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f = std::fs::File::open(
        "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin",
    )?;
    let f = std::io::BufReader::new(f);
    let grib2 = grib::from_reader(f)?;
    let (_index, first_submessage) = grib2.iter().next().unwrap();

    let actual = first_submessage.section3();
    let expected = Ok(grib::def::grib2::Section3 {
        header: grib::def::grib2::SectionHeader {
            len: 72,
            sect_num: 3,
        },
        payload: grib::def::grib2::Section3Payload {
            grid_def_source: 0,
            num_points: 86016,
            num_point_list_octets: 0,
            point_list_interpretation: 0,
            template_num: 0,
        },
    });
    assert_eq!(actual, expected);

    Ok(())
}
Source

pub fn section4(&self) -> Result<Section4, GribError>

Provides access to the parameters in Section 4.

§Examples
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f = std::fs::File::open(
        "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin",
    )?;
    let f = std::io::BufReader::new(f);
    let grib2 = grib::from_reader(f)?;
    let (_index, first_submessage) = grib2.iter().next().unwrap();

    let actual = first_submessage.section4();
    let expected = Ok(grib::def::grib2::Section4 {
        header: grib::def::grib2::SectionHeader {
            len: 34,
            sect_num: 4,
        },
        payload: grib::def::grib2::Section4Payload {
            num_coord_values: 0,
            template_num: 0,
        },
    });
    assert_eq!(actual, expected);

    Ok(())
}
Source

pub fn section5(&self) -> Result<Section5, GribError>

Provides access to the parameters in Section 5.

§Examples
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f = std::fs::File::open(
        "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin",
    )?;
    let f = std::io::BufReader::new(f);
    let grib2 = grib::from_reader(f)?;
    let (_index, first_submessage) = grib2.iter().next().unwrap();

    let actual = first_submessage.section5();
    let expected = Ok(grib::def::grib2::Section5 {
        header: grib::def::grib2::SectionHeader {
            len: 23,
            sect_num: 5,
        },
        payload: grib::def::grib2::Section5Payload {
            num_encoded_points: 86016,
            template_num: 200,
            template: grib::def::grib2::DataRepresentationTemplate::_5_200(
                grib::def::grib2::template::Template5_200 {
                    num_bits: 8,
                    max_val: 3,
                    max_level: 3,
                    dec: 0,
                    level_vals: vec![1, 2, 3],
                },
            ),
        },
    });
    assert_eq!(actual, expected);

    Ok(())
}
Source

pub fn section6(&self) -> Result<Section6, GribError>

Provides access to the parameters in Section 6.

§Examples
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f = std::fs::File::open(
        "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin",
    )?;
    let f = std::io::BufReader::new(f);
    let grib2 = grib::from_reader(f)?;
    let (_index, first_submessage) = grib2.iter().next().unwrap();

    let actual = first_submessage.section6();
    let expected = Ok(grib::def::grib2::Section6 {
        header: grib::def::grib2::SectionHeader {
            len: 6,
            sect_num: 6,
        },
        payload: grib::def::grib2::Section6Payload {
            bitmap_indicator: 255,
        },
    });
    assert_eq!(actual, expected);

    Ok(())
}
Source

pub fn dump<W: Write>(&self, writer: &mut W) -> Result<(), GribError>

Dumps the GRIB2 submessage.

§Examples
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f = std::fs::File::open(
        "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin",
    )?;
    let f = std::io::BufReader::new(f);
    let grib2 = grib::from_reader(f)?;
    let (_index, first_submessage) = grib2.iter().next().unwrap();

    let mut buf = std::io::Cursor::new(Vec::with_capacity(1024));
    first_submessage.dump(&mut buf)?;
    let expected = "\
#  SUBMESSAGE (total_length = 10321)
##  SECTION 0: INDICATOR SECTION (length = 16)
##  SECTION 1: IDENTIFICATION SECTION (length = 21)
1-4       header.len = 21  // Length of section in octets (nn).
5         header.sect_num = 1  // Number of section.
6-7       payload.centre_id = 34  // Identification of originating/generating centre (see Common Code table C-11).
8-9       payload.subcentre_id = 0  // Identification of originating/generating subcentre (allocated by originating/generating centre).
10        payload.master_table_version = 5  // GRIB master table version number (see Common Code table C-0 and Note 1).
11        payload.local_table_version = 1  // Version number of GRIB Local tables used to augment Master tables (see Code table 1.1 and Note 2).
12        payload.ref_time_significance = 0  // Significance of reference time (see Code table 1.2).
13-14     payload.ref_time.year = 2016  // Year (4 digits).
15        payload.ref_time.month = 8  // Month.
16        payload.ref_time.day = 22  // Day.
17        payload.ref_time.hour = 2  // Hour.
18        payload.ref_time.minute = 0  // Minute.
19        payload.ref_time.second = 0  // Second.
20        payload.prod_status = 0  // Production status of processed data in this GRIB message (see Code table 1.3).
21        payload.data_type = 2  // Type of processed data in this GRIB message (see Code table 1.4).
##  SECTION 3: GRID DEFINITION SECTION (length = 72)
1-4       header.len = 72  // Length of section in octets (nn).
5         header.sect_num = 3  // Number of section.
6         payload.grid_def_source = 0  // Source of grid definition (see Code table 3.0 and Note 1).
7-10      payload.num_points = 86016  // Number of data points.
11        payload.num_point_list_octets = 0  // Number of octets for optional list of numbers (see Note 2).
12        payload.point_list_interpretation = 0  // Interpretation of list of numbers (see Code table 3.11).
13-14     payload.template_num = 0  // Grid definition template number (= N) (see Code table 3.1).
##  SECTION 4: PRODUCT DEFINITION SECTION (length = 34)
1-4       header.len = 34  // Length of section in octets (nn).
5         header.sect_num = 4  // Number of section.
6-7       payload.num_coord_values = 0  // Number of coordinate values after template or number of information according to 3D vertical coordinate GRIB2 message (see Notes 1 and 5).
8-9       payload.template_num = 0  // Product definition template number (see Code table 4.0).
##  SECTION 5: DATA REPRESENTATION SECTION (length = 23)
1-4       header.len = 23  // Length of section in octets (nn).
5         header.sect_num = 5  // Number of section.
6-9       payload.num_encoded_points = 86016  // Number of data points where one or more values are specified in Section 7 when a bit map is present, total number of data points when a bit map is absent.
10-11     payload.template_num = 200  // Data representation template number (see Code table 5.0).
12        payload.template.num_bits = 8  // Number of bits used for each packed value in the run length packing with level value.
13-14     payload.template.max_val = 3  // MV - maximum value within the levels that are used in the packing.
15-16     payload.template.max_level = 3  // MVL - maximum value of level (predefined).
17        payload.template.dec = 0  // Decimal scale factor of representative value of each level.
18-23     payload.template.level_vals = [1, 2, 3]  // List of MVL scaled representative values of each level from lv=1 to MVL.
##  SECTION 6: BIT-MAP SECTION (length = 6)
1-4       header.len = 6  // Length of section in octets (nn).
5         header.sect_num = 6  // Number of section.
6         payload.bitmap_indicator = 255  // Bit-map indicator (see Code table 6.0 and the Note).
##  SECTION 7: DATA SECTION (length = 1391)
##  SECTION 8: END SECTION (length = 4)
";
    assert_eq!(String::from_utf8_lossy(buf.get_ref()), expected);

    Ok(())
}
Source

pub fn temporal_raw_info(&self) -> TemporalRawInfo

Returns time-related raw information associated with the submessage.

§Examples
use std::{
    fs::File,
    io::{BufReader, Read},
};

use grib::{
    Code, ForecastTime, TemporalRawInfo, UtcDateTime,
    codetables::grib2::{Table1_2, Table4_4},
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f = File::open(
        "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin",
    )?;
    let f = BufReader::new(f);
    let grib2 = grib::from_reader(f)?;

    let mut iter = grib2.iter();

    {
        let (_, message) = iter.next().ok_or_else(|| "first message is not found")?;
        let actual = message.temporal_raw_info();
        let expected = TemporalRawInfo {
            ref_time_significance: Code::Name(Table1_2::Analysis),
            ref_time_unchecked: UtcDateTime::new(2016, 8, 22, 2, 0, 0),
            forecast_time_diff: Some(ForecastTime {
                unit: Code::Name(Table4_4::Minute),
                value: 0,
            }),
        };
        assert_eq!(actual, expected);
    }

    {
        let (_, message) = iter.next().ok_or_else(|| "second message is not found")?;
        let actual = message.temporal_raw_info();
        let expected = TemporalRawInfo {
            ref_time_significance: Code::Name(Table1_2::Analysis),
            ref_time_unchecked: UtcDateTime::new(2016, 8, 22, 2, 0, 0),
            forecast_time_diff: Some(ForecastTime {
                unit: Code::Name(Table4_4::Minute),
                value: 10,
            }),
        };
        assert_eq!(actual, expected);
    }

    Ok(())
}
Source

pub fn temporal_info(&self) -> TemporalInfo

Available on crate feature time-calculation only.

Returns time-related calculated information associated with the submessage.

§Examples
use std::{
    fs::File,
    io::{BufReader, Read},
};

use chrono::{TimeZone, Utc};
use grib::{
    Code, ForecastTime, TemporalInfo, UtcDateTime,
    codetables::grib2::{Table1_2, Table4_4},
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let f = File::open(
        "testdata/Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin",
    )?;
    let f = BufReader::new(f);
    let grib2 = grib::from_reader(f)?;

    let mut iter = grib2.iter();

    {
        let (_, message) = iter.next().ok_or_else(|| "first message is not found")?;
        let actual = message.temporal_info();
        let expected = TemporalInfo {
            ref_time: Some(Utc.with_ymd_and_hms(2016, 8, 22, 2, 0, 0).unwrap()),
            forecast_time_target: Some(Utc.with_ymd_and_hms(2016, 8, 22, 2, 0, 0).unwrap()),
        };
        assert_eq!(actual, expected);
    }

    {
        let (_, message) = iter.next().ok_or_else(|| "second message is not found")?;
        let actual = message.temporal_info();
        let expected = TemporalInfo {
            ref_time: Some(Utc.with_ymd_and_hms(2016, 8, 22, 2, 0, 0).unwrap()),
            forecast_time_target: Some(Utc.with_ymd_and_hms(2016, 8, 22, 2, 10, 0).unwrap()),
        };
        assert_eq!(actual, expected);
    }

    Ok(())
}
Source

pub fn grid_shape(&self) -> Result<(usize, usize), GribError>

Returns the shape of the grid, i.e. a tuple of the number of grids in the i and j directions.

§Examples
use std::{
    fs::File,
    io::{BufReader, Read},
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut buf = Vec::new();

    let f = File::open("testdata/gdas.t12z.pgrb2.0p25.f000.0-10.xz")?;
    let f = BufReader::new(f);
    let mut f = xz2::bufread::XzDecoder::new(f);
    f.read_to_end(&mut buf)?;

    let f = std::io::Cursor::new(buf);
    let grib2 = grib::from_reader(f)?;

    let mut iter = grib2.iter();
    let (_, message) = iter.next().ok_or_else(|| "first message is not found")?;

    let shape = message.grid_shape()?;
    assert_eq!(shape, (1440, 721));
    Ok(())
}
Source

pub fn ij(&self) -> Result<GridPointIndexIterator, GribError>

Computes and returns an iterator over (i, j) of grid points.

The order of items is the same as the order of the grid point values, defined by the scanning mode (ScanningMode) in the data.

This iterator allows users to perform their own coordinate calculations for unsupported grid systems and map the results to grid point values.

§Examples
use std::{
    fs::File,
    io::{BufReader, Read},
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut buf = Vec::new();

    let f = File::open("testdata/gdas.t12z.pgrb2.0p25.f000.0-10.xz")?;
    let f = BufReader::new(f);
    let mut f = xz2::bufread::XzDecoder::new(f);
    f.read_to_end(&mut buf)?;

    let f = std::io::Cursor::new(buf);
    let grib2 = grib::from_reader(f)?;

    let mut iter = grib2.iter();
    let (_, message) = iter.next().ok_or_else(|| "first message is not found")?;

    let mut latlons = message.ij()?;
    assert_eq!(latlons.next(), Some((0, 0)));
    assert_eq!(latlons.next(), Some((1, 0)));
    Ok(())
}
Source

pub fn latlons(&self) -> Result<GridPointIterator, GribError>

Computes and returns an iterator over latitudes and longitudes of grid points.

The order of lat/lon data of grid points is the same as the order of the grid point values, defined by the scanning mode (ScanningMode) in the data.

§Examples
use std::{
    fs::File,
    io::{BufReader, Read},
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut buf = Vec::new();

    let f = File::open("testdata/gdas.t12z.pgrb2.0p25.f000.0-10.xz")?;
    let f = BufReader::new(f);
    let mut f = xz2::bufread::XzDecoder::new(f);
    f.read_to_end(&mut buf)?;

    let f = std::io::Cursor::new(buf);
    let grib2 = grib::from_reader(f)?;

    let mut iter = grib2.iter();
    let (_, message) = iter.next().ok_or_else(|| "first message is not found")?;

    let mut latlons = message.latlons()?;
    assert_eq!(latlons.next(), Some((90.0, 0.0)));
    assert_eq!(latlons.next(), Some((90.0, 0.25000003)));
    Ok(())
}

Auto Trait Implementations§

§

impl<'a, R> Freeze for SubMessage<'a, R>

§

impl<'a, R> !RefUnwindSafe for SubMessage<'a, R>

§

impl<'a, R> !Send for SubMessage<'a, R>

§

impl<'a, R> !Sync for SubMessage<'a, R>

§

impl<'a, R> Unpin for SubMessage<'a, R>

§

impl<'a, R> !UnwindSafe for SubMessage<'a, R>

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.