Skip to main content

grib/grid/
lambert.rs

1#[cfg(feature = "gridpoints-proj")]
2use crate::projection::OsgeoProj;
3use crate::{
4    GridPointIndex, LatLons,
5    def::grib2::{table, template::Template3_30},
6    error::GribError,
7    grid::AngleUnit,
8    projection,
9};
10
11impl crate::GridShortName for Template3_30 {
12    fn short_name(&self) -> &'static str {
13        "lambert"
14    }
15}
16
17impl GridPointIndex for Template3_30 {
18    fn grid_shape(&self) -> (usize, usize) {
19        (self.ni as usize, self.nj as usize)
20    }
21
22    fn scanning_mode(&self) -> &table::ScanningMode {
23        &self.scanning_mode
24    }
25}
26
27impl LatLons for Template3_30 {
28    #[cfg(feature = "gridpoints-proj")]
29    type Iter<'a> = super::helpers::ProjectionLatLonIterator;
30    #[cfg(not(feature = "gridpoints-proj"))]
31    type Iter<'a> = std::vec::IntoIter<(f64, f64)>;
32
33    fn latlons_unchecked<'a>(&'a self) -> Result<Self::Iter<'a>, GribError> {
34        let angle_units = self.angle_unit();
35        let lad = self.lad as f64 * angle_units;
36        let lov = self.lov as f64 * angle_units;
37        let latin1 = self.latin1 as f64 * angle_units;
38        let latin2 = self.latin2 as f64 * angle_units;
39        let (a, b) = self.earth_shape.radii().ok_or_else(|| {
40            GribError::NotSupported(format!(
41                "unknown value of Code Table 3.2 (shape of the Earth): {}",
42                self.earth_shape.shape.0
43            ))
44        })?;
45        let params = projection::LccParams {
46            ellipsoid: projection::Ellipsoid::from_a_and_b(a, b),
47            lat_0: lad,
48            lon_0: lov,
49            lat_1: latin1,
50            lat_2: latin2,
51        };
52
53        let dx = self.dx as f64 * 1e-3;
54        let dy = self.dy as f64 * 1e-3;
55        let dx = if !self.scanning_mode.scans_positively_for_i() && dx > 0. {
56            -dx
57        } else {
58            dx
59        };
60        let dy = if !self.scanning_mode.scans_positively_for_j() && dy > 0. {
61            -dy
62        } else {
63            dy
64        };
65
66        let first_point = (
67            self.first_point_lat as f64 * angle_units,
68            self.first_point_lon as f64 * angle_units,
69        );
70
71        #[cfg(feature = "gridpoints-proj")]
72        {
73            super::helpers::latlons_from_projection_with_first_point_and_delta(
74                &params.proj_args(),
75                first_point,
76                (dx, dy),
77                self.ij()?,
78            )
79        }
80
81        #[cfg(not(feature = "gridpoints-proj"))]
82        {
83            let projection = projection::Lcc::new(&params)?;
84            super::helpers::latlons_from_projection_with_first_point_and_delta(
85                &projection,
86                first_point,
87                (dx, dy),
88                self.ij()?,
89            )
90        }
91    }
92}
93
94impl AngleUnit for Template3_30 {
95    fn angle_unit(&self) -> f64 {
96        1e-6
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103    use crate::def::grib2::template::param_set;
104
105    #[test]
106    fn lambert_grid_latlon_computation() -> Result<(), Box<dyn std::error::Error>> {
107        use crate::grid::helpers::test_helpers::assert_coord_almost_eq;
108        // grid point definition extracted from testdata/ds.critfireo.bin.xz
109        let grid_def = Template3_30 {
110            earth_shape: param_set::EarthShape {
111                shape: table::EarthShapeType(1),
112                spherical_earth_radius: param_set::ScaledValue {
113                    scale_factor: 0,
114                    scaled_value: 6371200,
115                },
116                major_axis: param_set::ScaledValue {
117                    scale_factor: 0,
118                    scaled_value: 0,
119                },
120                minor_axis: param_set::ScaledValue {
121                    scale_factor: 0,
122                    scaled_value: 0,
123                },
124            },
125            ni: 2145,
126            nj: 1377,
127            first_point_lat: 20190000,
128            first_point_lon: 238449996,
129            resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00000000),
130            lad: 25000000,
131            lov: 265000000,
132            dx: 2539703,
133            dy: 2539703,
134            projection_centre: table::ProjectionCentreFlag(0b00000000),
135            scanning_mode: table::ScanningMode(0b01010000),
136            latin1: 25000000,
137            latin2: 25000000,
138            south_pole_lat: -90000000,
139            south_pole_lon: 0,
140        };
141        let latlons = grid_def.latlons()?.collect::<Vec<_>>();
142
143        // Following lat/lon values are taken from the calculation results using
144        // pygrib.
145        let delta = 1e-4;
146        assert_coord_almost_eq(latlons[0], (20.19, -121.550004), delta);
147        assert_coord_almost_eq(latlons[1], (20.19442682, -121.52621665), delta);
148        assert_coord_almost_eq(
149            latlons[latlons.len() - 2],
150            (50.10756403, -60.91298217),
151            delta,
152        );
153        assert_coord_almost_eq(
154            latlons[latlons.len() - 1],
155            (50.1024611, -60.88202274),
156            delta,
157        );
158
159        Ok(())
160    }
161}