Skip to main content

grib/def/grib2/
template3.rs

1use grib_template_derive::{Dump, TryFromSlice, WriteToBuffer};
2
3use super::table3;
4
5/// Grid definition template 3.0 - latitude/longitude (or equidistant
6/// cylindrical, or Plate Carrée).
7#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
8pub struct Template3_0 {
9    pub earth: param_set::EarthShape,
10    pub lat_lon: param_set::LatLonGrid,
11}
12
13/// Grid definition template 3.1 - rotated latitude/longitude (or equidistant
14/// cylindrical, or Plate Carrée).
15///
16/// # Examples
17///
18/// ```
19/// use std::io::Read;
20///
21/// use grib::{
22///     TryFromSlice,
23///     def::grib2::{
24///         table,
25///         template::{Template3_1, param_set},
26///     },
27/// };
28///
29/// fn main() -> Result<(), Box<dyn std::error::Error>> {
30///     let mut buf = Vec::new();
31///
32///     let f = std::fs::File::open(
33///         "testdata/20260219T00Z_MSC_HRDPS_CAPE_Sfc_RLatLon0.0225_PT000H.grib2",
34///     )?;
35///     let mut f = std::io::BufReader::new(f);
36///     f.read_to_end(&mut buf)?;
37///
38///     let mut pos = 0x33;
39///     let actual = Template3_1::try_from_slice(&buf, &mut pos)?;
40///     let expected = Template3_1 {
41///         earth: param_set::EarthShape {
42///             shape: table::EarthShapeType(6),
43///             spherical_earth_radius: param_set::ScaledValue {
44///                 scale_factor: 0xff,
45///                 scaled_value: 0xffffffff,
46///             },
47///             major_axis: param_set::ScaledValue {
48///                 scale_factor: 0xff,
49///                 scaled_value: 0xffffffff,
50///             },
51///             minor_axis: param_set::ScaledValue {
52///                 scale_factor: 0xff,
53///                 scaled_value: 0xffffffff,
54///             },
55///         },
56///         lat_lon: param_set::LatLonGrid {
57///             grid: param_set::Grid {
58///                 ni: 2540,
59///                 nj: 1290,
60///                 initial_production_domain_basic_angle: 0,
61///                 basic_angle_subdivisions: 0xffffffff,
62///                 first_point_lat: -12302501,
63///                 first_point_lon: 345178780,
64///                 resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00111000),
65///                 last_point_lat: 16700001,
66///                 last_point_lon: 42306283,
67///             },
68///             scanning_mode: table::ScanningMode(0b01000000),
69///             i_direction_inc: 22500,
70///             j_direction_inc: 22500,
71///         },
72///         rotation: param_set::Rotation {
73///             south_pole_lat: -36088520,
74///             south_pole_lon: 245305142,
75///             rot_angle: 0.,
76///         },
77///     };
78///     assert_eq!(actual, expected);
79///
80///     Ok(())
81/// }
82/// ```
83#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
84pub struct Template3_1 {
85    pub earth: param_set::EarthShape,
86    pub lat_lon: param_set::LatLonGrid,
87    pub rotation: param_set::Rotation,
88}
89
90/// Grid definition template 3.2 - stretched latitude/longitude (or equidistant
91/// cylindrical, or Plate Carrée).
92#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
93pub struct Template3_2 {
94    pub earth: param_set::EarthShape,
95    pub lat_lon: param_set::LatLonGrid,
96    pub stretching: param_set::Stretching,
97}
98
99/// Grid definition template 3.3 - stretched and rotated latitude/longitude (or
100/// equidistant cylindrical, or Plate Carrée).
101#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
102pub struct Template3_3 {
103    pub earth: param_set::EarthShape,
104    pub lat_lon: param_set::LatLonGrid,
105    pub rotation: param_set::Rotation,
106    pub stretching: param_set::Stretching,
107}
108
109/// Grid definition template 3.10 - Mercator.
110///
111/// # Examples
112///
113/// ```
114/// use std::io::{BufReader, Read};
115///
116/// use grib::{
117///     TryFromSlice,
118///     def::grib2::{
119///         table,
120///         template::{Template3_10, param_set},
121///     },
122/// };
123///
124/// fn main() -> Result<(), Box<dyn std::error::Error>> {
125///     let mut buf = Vec::new();
126///
127///     let f = std::fs::File::open("testdata/ds.wwa.bin.xz")?;
128///     let f = BufReader::new(f);
129///     let mut f = xz2::bufread::XzDecoder::new(f);
130///     f.read_to_end(&mut buf)?;
131///
132///     let mut pos = 0xa6;
133///     let actual = Template3_10::try_from_slice(&buf, &mut pos)?;
134///     let expected = Template3_10 {
135///         earth_shape: param_set::EarthShape {
136///             shape: table::EarthShapeType(1),
137///             spherical_earth_radius: param_set::ScaledValue {
138///                 scale_factor: 0,
139///                 scaled_value: 6371200,
140///             },
141///             major_axis: param_set::ScaledValue {
142///                 scale_factor: 0,
143///                 scaled_value: 0,
144///             },
145///             minor_axis: param_set::ScaledValue {
146///                 scale_factor: 0,
147///                 scaled_value: 0,
148///             },
149///         },
150///         ni: 2517,
151///         nj: 1793,
152///         first_point_lat: -30419200,
153///         first_point_lon: 129906005,
154///         resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00000000),
155///         lad: 20000000,
156///         last_point_lat: 80010000,
157///         last_point_lon: 10710000,
158///         scanning_mode: table::ScanningMode(0b01010000),
159///         orientation: 0,
160///         di: 10000000,
161///         dj: 10000000,
162///     };
163///     assert_eq!(actual, expected);
164///
165///     Ok(())
166/// }
167/// ```
168#[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
169pub struct Template3_10 {
170    pub earth_shape: param_set::EarthShape,
171    /// Ni - number of points along a parallel.
172    pub ni: u32,
173    /// Nj - number of points along a meridian.
174    pub nj: u32,
175    /// La1 - latitude of first grid point.
176    pub first_point_lat: i32,
177    /// Lo1 - longitude of first grid point.
178    pub first_point_lon: u32,
179    pub resolution_and_component_flags: table3::ResolutionAndComponentFlags,
180    /// LaD - Latitude(s) at which the Mercator projection intersects the Earth
181    /// (Latitude(s) where Di and Dj are specified).
182    pub lad: i32,
183    /// La2 - latitude of last grid point.
184    pub last_point_lat: i32,
185    /// Lo2 - longitude of last grid point.
186    pub last_point_lon: u32,
187    pub scanning_mode: table3::ScanningMode,
188    /// Orientation of the grid, angle between i direction on the map and the
189    /// equator (see Note 1).
190    pub orientation: u32,
191    /// Di - longitudinal direction grid length (see Note 2).
192    pub di: u32,
193    /// Dj - latitudinal direction grid length (see Note 2).
194    pub dj: u32,
195}
196
197/// Grid definition template 3.20 - polar stereographic projection.
198///
199/// # Examples
200///
201/// ```
202/// use std::io::{BufReader, Read};
203///
204/// use grib::{
205///     TryFromSlice,
206///     def::grib2::{
207///         table,
208///         template::{Template3_20, param_set},
209///     },
210/// };
211///
212/// fn main() -> Result<(), Box<dyn std::error::Error>> {
213///     let mut buf = Vec::new();
214///
215///     let f = std::fs::File::open(
216///         "testdata/CMC_RDPA_APCP-024-0100cutoff_SFC_0_ps10km_2023121806_000.grib2.xz",
217///     )?;
218///     let f = BufReader::new(f);
219///     let mut f = xz2::bufread::XzDecoder::new(f);
220///     f.read_to_end(&mut buf)?;
221///
222///     let mut pos = 0x33;
223///     let actual = Template3_20::try_from_slice(&buf, &mut pos)?;
224///     let expected = Template3_20 {
225///         earth_shape: param_set::EarthShape {
226///             shape: table::EarthShapeType(6),
227///             spherical_earth_radius: param_set::ScaledValue {
228///                 scale_factor: 0xff,
229///                 scaled_value: 0xffffffff,
230///             },
231///             major_axis: param_set::ScaledValue {
232///                 scale_factor: 0xff,
233///                 scaled_value: 0xffffffff,
234///             },
235///             minor_axis: param_set::ScaledValue {
236///                 scale_factor: 0xff,
237///                 scaled_value: 0xffffffff,
238///             },
239///         },
240///         ni: 935,
241///         nj: 824,
242///         first_point_lat: 18145030,
243///         first_point_lon: 217107456,
244///         resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00001000),
245///         lad: 60000000,
246///         lov: 249000000,
247///         dx: 10000000,
248///         dy: 10000000,
249///         projection_centre: table::ProjectionCentreFlag(0b00000000),
250///         scanning_mode: table::ScanningMode(0b01000000),
251///     };
252///     assert_eq!(actual, expected);
253///
254///     Ok(())
255/// }
256/// ```
257#[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
258pub struct Template3_20 {
259    pub earth_shape: param_set::EarthShape,
260    /// Nx - number of points along the x-axis.
261    pub ni: u32,
262    /// Ny - number of points along the y-axis.
263    pub nj: u32,
264    /// La1 - latitude of first grid point.
265    pub first_point_lat: i32,
266    /// Lo1 - longitude of first grid point.
267    pub first_point_lon: u32,
268    pub resolution_and_component_flags: table3::ResolutionAndComponentFlags,
269    /// LaD - latitude where Dx and Dy are specified.
270    pub lad: i32,
271    /// LoV - orientation of the grid (see Note 2).
272    pub lov: i32,
273    /// Dx - x-direction grid length (see Note 3).
274    pub dx: u32,
275    /// Dy - y-direction grid length (see Note 3).
276    pub dy: u32,
277    pub projection_centre: table3::ProjectionCentreFlag,
278    pub scanning_mode: table3::ScanningMode,
279}
280
281/// Grid definition template 3.30 - Lambert conformal.
282///
283/// # Examples
284///
285/// ```
286/// use std::io::{BufReader, Read};
287///
288/// use grib::{
289///     TryFromSlice,
290///     def::grib2::{
291///         table,
292///         template::{Template3_30, param_set},
293///     },
294/// };
295///
296/// fn main() -> Result<(), Box<dyn std::error::Error>> {
297///     let mut buf = Vec::new();
298///
299///     let f = std::fs::File::open("testdata/ds.critfireo.bin.xz")?;
300///     let f = BufReader::new(f);
301///     let mut f = xz2::bufread::XzDecoder::new(f);
302///     f.read_to_end(&mut buf)?;
303///
304///     let mut pos = 0x83;
305///     let actual = Template3_30::try_from_slice(&buf, &mut pos)?;
306///     let expected = Template3_30 {
307///         earth_shape: param_set::EarthShape {
308///             shape: table::EarthShapeType(1),
309///             spherical_earth_radius: param_set::ScaledValue {
310///                 scale_factor: 0,
311///                 scaled_value: 6371200,
312///             },
313///             major_axis: param_set::ScaledValue {
314///                 scale_factor: 0,
315///                 scaled_value: 0,
316///             },
317///             minor_axis: param_set::ScaledValue {
318///                 scale_factor: 0,
319///                 scaled_value: 0,
320///             },
321///         },
322///         ni: 2145,
323///         nj: 1377,
324///         first_point_lat: 20190000,
325///         first_point_lon: 238449996,
326///         resolution_and_component_flags: table::ResolutionAndComponentFlags(0b00000000),
327///         lad: 25000000,
328///         lov: 265000000,
329///         dx: 2539703,
330///         dy: 2539703,
331///         projection_centre: table::ProjectionCentreFlag(0b00000000),
332///         scanning_mode: table::ScanningMode(0b01010000),
333///         latin1: 25000000,
334///         latin2: 25000000,
335///         south_pole_lat: -90000000,
336///         south_pole_lon: 0,
337///     };
338///     assert_eq!(actual, expected);
339///
340///     Ok(())
341/// }
342/// ```
343#[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
344pub struct Template3_30 {
345    pub earth_shape: param_set::EarthShape,
346    /// Nx - number of points along the x-axis.
347    pub ni: u32,
348    /// Ny - number of points along the y-axis.
349    pub nj: u32,
350    /// La1 - latitude of first grid point.
351    pub first_point_lat: i32,
352    /// Lo1 - longitude of first grid point.
353    pub first_point_lon: u32,
354    pub resolution_and_component_flags: table3::ResolutionAndComponentFlags,
355    /// LaD - latitude where Dx and Dy are specified.
356    pub lad: i32,
357    /// LoV - longitude of meridian parallel to y-axis along which latitude
358    /// increases as the y-coordinate increases.
359    pub lov: u32,
360    /// Dx - x-direction grid length (see Note 1).
361    pub dx: u32,
362    /// Dy - y-direction grid length (see Note 1).
363    pub dy: u32,
364    pub projection_centre: table3::ProjectionCentreFlag,
365    pub scanning_mode: table3::ScanningMode,
366    /// Latin 1 - first latitude from the pole at which the secant cone cuts the
367    /// sphere.
368    pub latin1: i32,
369    /// Latin 2 - second latitude from the pole at which the secant cone cuts
370    /// the sphere.
371    pub latin2: i32,
372    /// Latitude of the southern pole of projection.
373    pub south_pole_lat: i32,
374    /// Longitude of the southern pole of projection.
375    pub south_pole_lon: u32,
376}
377
378/// Grid definition template 3.40 - Gaussian latitude/longitude.
379#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
380pub struct Template3_40 {
381    pub earth: param_set::EarthShape,
382    pub gaussian: param_set::GaussianGrid,
383}
384
385/// Grid definition template 3.41 - rotated Gaussian latitude/longitude.
386#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
387pub struct Template3_41 {
388    pub earth: param_set::EarthShape,
389    pub gaussian: param_set::GaussianGrid,
390    pub rotation: param_set::Rotation,
391}
392
393/// Grid definition template 3.42 - stretched Gaussian latitude/longitude.
394#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
395pub struct Template3_42 {
396    pub earth: param_set::EarthShape,
397    pub gaussian: param_set::GaussianGrid,
398    pub stretching: param_set::Stretching,
399}
400
401/// Grid definition template 3.43 - stretched and rotated Gaussian
402/// latitude/longitude.
403#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
404pub struct Template3_43 {
405    pub earth: param_set::EarthShape,
406    pub gaussian: param_set::GaussianGrid,
407    pub rotation: param_set::Rotation,
408    pub stretching: param_set::Stretching,
409}
410
411/// Grid definition template 3.101 - general unstructured grid.
412#[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
413pub struct Template3_101 {
414    pub earth_shape: table3::EarthShapeType,
415    /// Number of grid used (defined by originating centre).
416    #[grib_template(num_octets = 3)]
417    pub grid_num: u32,
418    /// Number of grid in reference (to allow annotating for Arakawa C-grid on
419    /// arbitrary grid) (see Note).
420    pub ref_grid_num: u8,
421    /// Universally Unique Identifier of horizontal grid.
422    pub uuid: [u8; 16],
423}
424
425pub(crate) mod param_set {
426    use grib_template_derive::{Dump, TryFromSlice, WriteToBuffer};
427
428    use super::super::{
429        table3::{EarthShapeType, ResolutionAndComponentFlags, ScanningMode},
430        template::param_set::ScaledValue,
431    };
432
433    #[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
434    pub struct EarthShape {
435        pub shape: EarthShapeType,
436        /// Radius of spherical Earth.
437        #[dump(doc(
438            scale_factor = "Scale factor of radius of spherical Earth.",
439            scaled_value = "Scaled value of radius of spherical Earth.",
440        ))]
441        pub spherical_earth_radius: ScaledValue<u8, u32>,
442        /// Major axis of oblate spheroid Earth.
443        #[dump(doc(
444            scale_factor = "Scale factor of major axis of oblate spheroid Earth.",
445            scaled_value = "Scaled value of major axis of oblate spheroid Earth.",
446        ))]
447        pub major_axis: ScaledValue<u8, u32>,
448        /// Minor axis of oblate spheroid Earth.
449        #[dump(doc(
450            scale_factor = "Scale factor of minor axis of oblate spheroid Earth.",
451            scaled_value = "Scaled value of minor axis of oblate spheroid Earth.",
452        ))]
453        pub minor_axis: ScaledValue<u8, u32>,
454    }
455
456    #[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
457    pub struct LatLonGrid {
458        pub grid: Grid,
459        /// Di - i direction increment (see Notes 1 and 5).
460        pub i_direction_inc: u32,
461        /// Dj - j direction increment (see Notes 1 and 5).
462        pub j_direction_inc: u32,
463        pub scanning_mode: ScanningMode,
464    }
465
466    #[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
467    pub struct GaussianGrid {
468        pub grid: Grid,
469        /// Di - i direction increment (see Notes 1 and 5).
470        pub i_direction_inc: u32,
471        /// N - number of parallels between a pole and the Equator (see Note 2).
472        pub n: u32,
473        pub scanning_mode: ScanningMode,
474    }
475
476    #[derive(Debug, PartialEq, Eq, Clone, TryFromSlice, WriteToBuffer, Dump)]
477    pub struct Grid {
478        /// Ni - number of points along a parallel.
479        pub ni: u32,
480        /// Nj - number of points along a meridian.
481        pub nj: u32,
482        /// Basic angle of the initial production domain (see Note 1).
483        pub initial_production_domain_basic_angle: u32,
484        /// Subdivisions of basic angle used to define extreme longitudes and
485        /// latitudes, and direction increments (see Note 1).
486        pub basic_angle_subdivisions: u32,
487        /// La1 - latitude of first grid point (see Note 1).
488        pub first_point_lat: i32,
489        /// Lo1 - longitude of first grid point (see Note 1).
490        pub first_point_lon: u32,
491        pub resolution_and_component_flags: ResolutionAndComponentFlags,
492        /// La2 - latitude of last grid point (see Note 1).
493        pub last_point_lat: i32,
494        /// Lo2 - longitude of last grid point (see Note 1).
495        pub last_point_lon: u32,
496    }
497
498    #[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
499    pub struct Rotation {
500        /// Latitude of the southern pole of projection.
501        pub south_pole_lat: i32,
502        /// Longitude of the southern pole of projection.
503        pub south_pole_lon: u32,
504        /// Angle of rotation of projection.
505        pub rot_angle: f32,
506    }
507
508    #[derive(Debug, PartialEq, Clone, TryFromSlice, WriteToBuffer, Dump)]
509    pub struct Stretching {
510        /// Latitude of the pole of stretching.
511        pub pole_lat: i32,
512        /// Longitude of the pole of stretching.
513        pub pole_lon: u32,
514        /// Stretching factor.
515        pub factor: u32,
516    }
517}