1#[cfg(feature = "gridpoints-proj")]
2use crate::projection::OsgeoProj;
3#[cfg(not(feature = "gridpoints-proj"))]
4use crate::projection::Project;
5use crate::{
6 GridPointIndex, LatLons,
7 def::grib2::{table, template::Template3_10},
8 error::GribError,
9 grid::AngleUnit,
10 projection,
11};
12
13impl crate::GridShortName for Template3_10 {
14 fn short_name(&self) -> &'static str {
15 "mercator"
16 }
17}
18
19impl GridPointIndex for Template3_10 {
20 fn grid_shape(&self) -> (usize, usize) {
21 (self.ni as usize, self.nj as usize)
22 }
23
24 fn scanning_mode(&self) -> &table::ScanningMode {
25 &self.scanning_mode
26 }
27}
28
29impl LatLons for Template3_10 {
30 type Iter<'a> = std::vec::IntoIter<(f64, f64)>;
31
32 fn latlons_unchecked<'a>(&'a self) -> Result<Self::Iter<'a>, GribError> {
33 if self.orientation != 0 {
34 return Err(GribError::NotSupported(format!(
35 "Mercator grid orientation {}",
36 self.orientation
37 )));
38 }
39
40 if !self.is_consistent_for_j() {
41 return Err(GribError::InvalidValueError(
42 "Latitudes for first/last grid points are not consistent with scanning mode"
43 .to_owned(),
44 ));
45 }
46
47 let angle_units = self.angle_unit();
48 let first_point_lon = self.first_point_lon as f64 * angle_units;
49 let last_point_lon = self.last_point_lon as f64 * angle_units;
50 let lon_diff = last_point_lon - first_point_lon;
51 let (first_point_lon, last_point_lon) =
52 if self.scanning_mode.scans_positively_for_i() && lon_diff < 0. {
53 (first_point_lon, last_point_lon + 360.)
54 } else if !self.scanning_mode.scans_positively_for_i() && lon_diff > 0. {
55 (first_point_lon + 360., last_point_lon)
56 } else {
57 (first_point_lon, last_point_lon)
58 };
59 let first_point = (self.first_point_lat as f64 * angle_units, first_point_lon);
60 let last_point = (self.last_point_lat as f64 * angle_units, last_point_lon);
61 let lon_0 = (first_point_lon + last_point_lon) / 2.;
64
65 let (a, b) = self.earth_shape.radii().ok_or_else(|| {
66 GribError::NotSupported(format!(
67 "unknown value of Code Table 3.2 (shape of the Earth): {}",
68 self.earth_shape.shape.0
69 ))
70 })?;
71 let params = projection::MercParams {
72 ellipsoid: projection::Ellipsoid::from_a_and_b(a, b),
73 lat_ts: self.lad as f64 * angle_units,
74 lon_0,
75 };
76
77 #[cfg(feature = "gridpoints-proj")]
78 {
79 super::helpers::latlons_from_projection_with_first_point_and_last_point(
80 ¶ms.proj_args(),
81 first_point,
82 last_point,
83 self.grid_shape(),
84 self.ij()?,
85 )
86 }
87
88 #[cfg(not(feature = "gridpoints-proj"))]
89 {
90 let projection = projection::Merc::new(¶ms)?;
91 let (first_point_lat, first_point_lon) = first_point;
92 let (first_corner_x, first_corner_y) = projection.project(
93 &(first_point_lon.to_radians(), first_point_lat.to_radians()),
94 false,
95 )?;
96 let (last_point_lat, last_point_lon) = last_point;
97 let (last_corner_x, last_corner_y) = projection.project(
98 &(last_point_lon.to_radians(), last_point_lat.to_radians()),
99 false,
100 )?;
101
102 let dx = (last_corner_x - first_corner_x) / (self.ni - 1) as f64;
103 let dy = (last_corner_y - first_corner_y) / (self.nj - 1) as f64;
104 let latlons = self
105 .ij()?
106 .map(|(i, j)| {
107 projection
108 .project(
109 &(
110 first_corner_x + dx * i as f64,
111 first_corner_y + dy * j as f64,
112 ),
113 true,
114 )
115 .map(|(lon, lat)| (lat.to_degrees(), lon.to_degrees()))
116 })
117 .collect::<Result<Vec<_>, _>>()?;
118 Ok(latlons.into_iter())
119 }
120 }
121}
122
123impl AngleUnit for Template3_10 {
124 fn angle_unit(&self) -> f64 {
125 1e-6
126 }
127}
128
129impl Template3_10 {
130 pub(crate) fn is_consistent_for_j(&self) -> bool {
131 let lat_diff = self.last_point_lat - self.first_point_lat;
132 !((lat_diff > 0) ^ self.scanning_mode.scans_positively_for_j())
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139 use crate::{
140 def::grib2::template::param_set, grid::helpers::test_helpers::assert_coord_almost_eq,
141 };
142
143 fn grid_definition() -> Template3_10 {
144 Template3_10 {
146 earth_shape: param_set::EarthShape {
147 shape: table::EarthShapeType(1),
148 spherical_earth_radius: param_set::ScaledValue {
149 scale_factor: 0,
150 scaled_value: 6371200,
151 },
152 major_axis: param_set::ScaledValue {
153 scale_factor: 0,
154 scaled_value: 0,
155 },
156 minor_axis: param_set::ScaledValue {
157 scale_factor: 0,
158 scaled_value: 0,
159 },
160 },
161 ni: 2517,
162 nj: 1793,
163 first_point_lat: -30419200,
164 first_point_lon: 129906005,
165 resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00000000),
166 lad: 20000000,
167 last_point_lat: 80010000,
168 last_point_lon: 10710000,
169 scanning_mode: table::ScanningMode(0b01010000),
170 orientation: 0,
171 di: 10000000,
172 dj: 10000000,
173 }
174 }
175
176 #[test]
177 fn mercator_grid_latlon_computation() -> Result<(), Box<dyn std::error::Error>> {
178 let grid_def = grid_definition();
179 let latlons = grid_def.latlons()?.collect::<Vec<_>>();
180
181 let num_points = latlons.len();
184 let ni = grid_def.ni as usize;
185 let delta = 3e-5;
186 assert_coord_almost_eq(latlons[0], (-30.4192, 129.906005), delta);
188 assert_coord_almost_eq(latlons[1], (-30.4192, 130.00171406), delta);
190 assert_coord_almost_eq(latlons[ni], (-30.33658686, 10.71), delta);
192 assert_coord_almost_eq(
194 latlons[num_points - ni - 1],
195 (79.9933742, 129.906005),
196 delta,
197 );
198 assert_coord_almost_eq(latlons[num_points - 2], (80.01, 10.61429094), delta);
200 assert_coord_almost_eq(latlons[num_points - 1], (80.01, 10.71), delta);
202
203 Ok(())
204 }
205}