grib 0.18.3

GRIB format parser & writer for Rust
Documentation
#[cfg(feature = "gridpoints-proj")]
use crate::projection::OsgeoProj;
#[cfg(not(feature = "gridpoints-proj"))]
use crate::projection::Project;
use crate::{
    GridPointIndex, LatLons,
    def::grib2::template::{Template3_30, param_set},
    error::GribError,
    grid::AngleUnit,
    projection,
};

impl crate::GridShortName for Template3_30 {
    fn short_name(&self) -> &'static str {
        "lambert"
    }
}

impl GridPointIndex for Template3_30 {
    fn grid_shape(&self) -> (usize, usize) {
        (self.ni as usize, self.nj as usize)
    }

    fn scanning_mode(&self) -> &param_set::ScanningMode {
        &self.scanning_mode
    }
}

impl LatLons for Template3_30 {
    type Iter<'a> = std::vec::IntoIter<(f32, f32)>;

    fn latlons_unchecked<'a>(&'a self) -> Result<Self::Iter<'a>, GribError> {
        let angle_units = self.angle_unit();
        let lad = self.lad as f64 * angle_units;
        let lov = self.lov as f64 * angle_units;
        let latin1 = self.latin1 as f64 * angle_units;
        let latin2 = self.latin2 as f64 * angle_units;
        let (a, b) = self.earth_shape.radii().ok_or_else(|| {
            GribError::NotSupported(format!(
                "unknown value of Code Table 3.2 (shape of the Earth): {}",
                self.earth_shape.shape
            ))
        })?;
        let params = projection::LccParams {
            ellipsoid: projection::Ellipsoid::from_a_and_b(a, b),
            lat_0: lad,
            lon_0: lov,
            lat_1: latin1,
            lat_2: latin2,
        };

        let dx = self.dx as f64 * 1e-3;
        let dy = self.dy as f64 * 1e-3;
        let dx = if !self.scanning_mode.scans_positively_for_i() && dx > 0. {
            -dx
        } else {
            dx
        };
        let dy = if !self.scanning_mode.scans_positively_for_j() && dy > 0. {
            -dy
        } else {
            dy
        };

        let first_point = (
            self.first_point_lat as f64 * angle_units,
            self.first_point_lon as f64 * angle_units,
        );

        #[cfg(feature = "gridpoints-proj")]
        {
            super::helpers::latlons_from_projection_definition_and_first_point(
                &params.proj_args(),
                first_point,
                (dx, dy),
                self.ij()?,
            )
        }

        #[cfg(not(feature = "gridpoints-proj"))]
        {
            let projection = projection::Lcc::new(&params)?;
            let (first_point_lat, first_point_lon) = first_point;
            let (first_corner_x, first_corner_y) = projection.project(
                &(first_point_lon.to_radians(), first_point_lat.to_radians()),
                false,
            )?;

            let latlons = self
                .ij()?
                .map(|(i, j)| {
                    projection.project(
                        &(
                            first_corner_x + dx * i as f64,
                            first_corner_y + dy * j as f64,
                        ),
                        true,
                    )
                })
                .map(|result| {
                    result.map(|(lon, lat)| (lat.to_degrees() as f32, lon.to_degrees() as f32))
                })
                .collect::<Result<Vec<_>, _>>()?;
            Ok(latlons.into_iter())
        }
    }
}

impl AngleUnit for Template3_30 {
    fn angle_unit(&self) -> f64 {
        1e-6
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lambert_grid_latlon_computation() -> Result<(), Box<dyn std::error::Error>> {
        use crate::grid::helpers::test_helpers::assert_coord_almost_eq;
        // grid point definition extracted from testdata/ds.critfireo.bin.xz
        let grid_def = Template3_30 {
            earth_shape: param_set::EarthShape {
                shape: 1,
                spherical_earth_radius: param_set::ScaledValue {
                    scale_factor: 0,
                    scaled_value: 6371200,
                },
                major_axis: param_set::ScaledValue {
                    scale_factor: 0,
                    scaled_value: 0,
                },
                minor_axis: param_set::ScaledValue {
                    scale_factor: 0,
                    scaled_value: 0,
                },
            },
            ni: 2145,
            nj: 1377,
            first_point_lat: 20190000,
            first_point_lon: 238449996,
            resolution_and_component_flags: param_set::ResolutionAndComponentFlags(0b00000000),
            lad: 25000000,
            lov: 265000000,
            dx: 2539703,
            dy: 2539703,
            projection_centre: param_set::ProjectionCentreFlag(0b00000000),
            scanning_mode: param_set::ScanningMode(0b01010000),
            latin1: 25000000,
            latin2: 25000000,
            south_pole_lat: -90000000,
            south_pole_lon: 0,
        };
        let latlons = grid_def.latlons()?.collect::<Vec<_>>();

        // Following lat/lon values are taken from the calculation results using
        // pygrib.
        let delta = 1e-4;
        assert_coord_almost_eq(latlons[0], (20.19, -121.550004), delta);
        assert_coord_almost_eq(latlons[1], (20.19442682, -121.52621665), delta);
        assert_coord_almost_eq(
            latlons[latlons.len() - 2],
            (50.10756403, -60.91298217),
            delta,
        );
        assert_coord_almost_eq(
            latlons[latlons.len() - 1],
            (50.1024611, -60.88202274),
            delta,
        );

        Ok(())
    }
}