Skip to main content

grib/grid/
latlon.rs

1use super::{
2    GridPointIndexIterator, Unrotate,
3    helpers::{RegularGridIterator, evenly_spaced_degrees, evenly_spaced_longitudes},
4};
5use crate::{
6    GridPointIndex, LatLons,
7    def::grib2::{
8        table,
9        template::{Template3_1, param_set},
10    },
11    error::GribError,
12    grid::AngleUnit,
13};
14
15impl crate::GridShortName for param_set::LatLonGrid {
16    fn short_name(&self) -> &'static str {
17        "regular_ll"
18    }
19}
20
21impl GridPointIndex for param_set::LatLonGrid {
22    fn grid_shape(&self) -> (usize, usize) {
23        (self.grid.ni as usize, self.grid.nj as usize)
24    }
25
26    fn scanning_mode(&self) -> &table::ScanningMode {
27        &self.scanning_mode
28    }
29}
30
31impl LatLons for param_set::LatLonGrid {
32    type Iter<'a> = RegularGridIterator;
33
34    fn latlons_unchecked<'a>(&'a self) -> Result<Self::Iter<'a>, GribError> {
35        if !self.is_consistent_for_j() {
36            return Err(GribError::InvalidValueError(
37                "Latitudes for first/last grid points are not consistent with scanning mode"
38                    .to_owned(),
39            ));
40        }
41
42        let ij = self.ij()?;
43        let angle_units = self.angle_unit();
44        let lat = evenly_spaced_degrees(
45            self.grid.first_point_lat as f64,
46            self.grid.last_point_lat as f64,
47            (self.grid.nj - 1) as usize,
48            angle_units,
49        );
50        let lon = evenly_spaced_longitudes(
51            self.grid.first_point_lon,
52            self.grid.last_point_lon,
53            (self.grid.ni - 1) as usize,
54            angle_units,
55            self.scanning_mode,
56        );
57
58        let iter = RegularGridIterator::new(lat, lon, ij);
59        Ok(iter)
60    }
61}
62
63impl AngleUnit for param_set::LatLonGrid {
64    fn angle_unit(&self) -> f64 {
65        self.grid.angle_unit()
66    }
67}
68
69impl param_set::LatLonGrid {
70    pub(crate) fn is_consistent_for_j(&self) -> bool {
71        let lat_diff = self.grid.last_point_lat - self.grid.first_point_lat;
72        !((lat_diff > 0) ^ self.scanning_mode.scans_positively_for_j())
73    }
74}
75
76impl crate::GridShortName for Template3_1 {
77    fn short_name(&self) -> &'static str {
78        "rotated_ll"
79    }
80}
81
82impl GridPointIndex for Template3_1 {
83    fn grid_shape(&self) -> (usize, usize) {
84        self.lat_lon.grid_shape()
85    }
86
87    fn scanning_mode(&self) -> &crate::def::grib2::table::ScanningMode {
88        self.lat_lon.scanning_mode()
89    }
90
91    fn ij(&self) -> Result<GridPointIndexIterator, GribError> {
92        self.lat_lon.ij()
93    }
94}
95
96impl LatLons for Template3_1 {
97    type Iter<'a>
98        = Unrotate<RegularGridIterator>
99    where
100        Self: 'a;
101
102    fn latlons_unchecked<'a>(&'a self) -> Result<Self::Iter<'a>, GribError> {
103        let iter = Unrotate::new(
104            self.lat_lon.latlons_unchecked()?,
105            &self.rotation,
106            self.angle_unit(),
107        );
108        Ok(iter)
109    }
110}
111
112impl AngleUnit for Template3_1 {
113    fn angle_unit(&self) -> f64 {
114        self.lat_lon.grid.angle_unit()
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121    use crate::grid::helpers::test_helpers::assert_coord_almost_eq;
122
123    macro_rules! test_lat_lon_calculation_for_inconsistent_longitude_definitions {
124        ($((
125            $name:ident,
126            $grid:expr,
127            $scanning_mode:expr,
128            $expected_head:expr,
129            $expected_tail:expr $(,)?
130        )),* $(,)?) => ($(
131            #[test]
132            fn $name() {
133                let grid = param_set::LatLonGrid {
134                    grid: $grid,
135                    i_direction_inc: 0xffffffff,
136                    j_direction_inc: 0xffffffff,
137                    scanning_mode: $scanning_mode,
138                };
139                let latlons = grid.latlons();
140                assert!(latlons.is_ok());
141
142                let delta = 1e-10;
143
144                let latlons = latlons.unwrap();
145                let actual = latlons.clone().take(3).collect::<Vec<_>>();
146                let expected = $expected_head;
147                for (a, e) in actual.iter().zip(expected) {
148                    assert_coord_almost_eq(*a, e, delta);
149                }
150
151                let (len, _) = latlons.size_hint();
152                let actual = latlons.skip(len - 3).collect::<Vec<_>>();
153                let expected = $expected_tail;
154                for (a, e) in actual.iter().zip(expected) {
155                    assert_coord_almost_eq(*a, e, delta);
156                }
157            }
158        )*);
159    }
160
161    test_lat_lon_calculation_for_inconsistent_longitude_definitions! {
162        (
163            lat_lon_calculation_preserves_microdegree_precision,
164            param_set::Grid {
165                ni: 3,
166                nj: 2,
167                initial_production_domain_basic_angle: 0,
168                basic_angle_subdivisions: 0xffffffff,
169                first_point_lat: 35000001,
170                first_point_lon: 139000001,
171                resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00110000),
172                last_point_lat: 35000002,
173                last_point_lon: 139000003,
174            },
175            table::ScanningMode(0b01000000),
176            vec![(35.000001, 139.000001), (35.000001, 139.000002), (35.000001, 139.000003)],
177            vec![(35.000002, 139.000001), (35.000002, 139.000002), (35.000002, 139.000003)],
178        ),
179        (
180            lat_lon_calculation_for_increasing_longitudes_and_positive_direction_scan,
181            param_set::Grid {
182                ni: 1500,
183                nj: 751,
184                initial_production_domain_basic_angle: 0,
185                basic_angle_subdivisions: 0xffffffff,
186                first_point_lat: -90000000,
187                first_point_lon: 0,
188                resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00110000),
189                last_point_lat: 90000000,
190                last_point_lon: 359760000,
191            },
192            table::ScanningMode(0b01000000),
193            vec![(-90.0, 0.0), (-90.0, 0.24), (-90.0, 0.48)],
194            vec![(90.0, -0.72), (90.0, -0.48), (90.0, -0.24)],
195        ),
196        (
197            // grid point definition extracted from
198            // testdata/CMC_glb_TMP_ISBL_1_latlon.24x.24_2021051800_P000.grib2
199            lat_lon_calculation_for_decreasing_longitudes_and_positive_direction_scan,
200            param_set::Grid {
201                ni: 1500,
202                nj: 751,
203                initial_production_domain_basic_angle: 0,
204                basic_angle_subdivisions: 0xffffffff,
205                first_point_lat: -90000000,
206                first_point_lon: 180000000,
207                resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00110000),
208                last_point_lat: 90000000,
209                last_point_lon: 179760000,
210            },
211            table::ScanningMode(0b01000000),
212            vec![(-90.0, -180.0), (-90.0, -179.76), (-90.0, -179.52)],
213            vec![(90.0, 179.28), (90.0, 179.52), (90.0, 179.76)],
214        ),
215        (
216            lat_lon_calculation_for_decreasing_longitudes_and_negative_direction_scan,
217            param_set::Grid {
218                ni: 1500,
219                nj: 751,
220                initial_production_domain_basic_angle: 0,
221                basic_angle_subdivisions: 0xffffffff,
222                first_point_lat: -90000000,
223                first_point_lon: 359760000,
224                resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00110000),
225                last_point_lat: 90000000,
226                last_point_lon: 0,
227            },
228            table::ScanningMode(0b11000000),
229            vec![(-90.0, -0.24), (-90.0, -0.48), (-90.0, -0.72)],
230            vec![(90.0, 0.48), (90.0, 0.24), (90.0, 0.0)],
231        ),
232        (
233            lat_lon_calculation_for_increasing_longitudes_and_negative_direction_scan,
234            param_set::Grid {
235                ni: 1500,
236                nj: 751,
237                initial_production_domain_basic_angle: 0,
238                basic_angle_subdivisions: 0xffffffff,
239                first_point_lat: -90000000,
240                first_point_lon: 179760000,
241                resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00110000),
242                last_point_lat: 90000000,
243                last_point_lon: 180000000,
244            },
245            table::ScanningMode(0b11000000),
246            vec![(-90.0, 179.76), (-90.0, 179.52), (-90.0, 179.28)],
247            vec![(90.0, -179.52), (90.0, -179.76), (90.0, -180.0)],
248        ),
249    }
250
251    macro_rules! test_consistencies_between_lat_lon_and_scanning_mode {
252        ($((
253            $name:ident,
254            $first_point_lat:expr,
255            $first_point_lon:expr,
256            $last_point_lat:expr,
257            $last_point_lon:expr,
258            $scanning_mode:expr,
259            $expected_for_j:expr $(,)?
260        )),* $(,)?) => ($(
261            #[test]
262            fn $name() {
263                let grid = param_set::LatLonGrid {
264                    grid: param_set::Grid {
265                        ni: 1,
266                        nj: 1,
267                        initial_production_domain_basic_angle: 0,
268                        basic_angle_subdivisions: 0xffffffff,
269                        first_point_lat: $first_point_lat,
270                        first_point_lon: $first_point_lon,
271                        resolution_and_component_flags:
272                            table::ResolutionAndComponentFlags(0b00110000),
273                        last_point_lat: $last_point_lat,
274                        last_point_lon: $last_point_lon,
275                    },
276                    i_direction_inc: 0xffffffff,
277                    j_direction_inc: 0xffffffff,
278                    scanning_mode: table::ScanningMode($scanning_mode),
279                };
280                assert_eq!(grid.is_consistent_for_j(), $expected_for_j);
281            }
282        )*);
283    }
284
285    test_consistencies_between_lat_lon_and_scanning_mode! {
286        (
287            consistency_between_lat_decrease_and_scanning_mode_0b00000000,
288            37_000_000,
289            140_000_000,
290            36_000_000,
291            141_000_000,
292            0b00000000,
293            true,
294        ),
295        (
296            consistency_between_lat_decrease_and_scanning_mode_0b01000000,
297            37_000_000,
298            140_000_000,
299            36_000_000,
300            141_000_000,
301            0b01000000,
302            false,
303        ),
304        (
305            consistency_between_lat_increase_and_scanning_mode_0b00000000,
306            36_000_000,
307            140_000_000,
308            37_000_000,
309            141_000_000,
310            0b00000000,
311            false,
312        ),
313        (
314            consistency_between_lat_increase_and_scanning_mode_0b01000000,
315            36_000_000,
316            140_000_000,
317            37_000_000,
318            141_000_000,
319            0b01000000,
320            true,
321        ),
322    }
323}