grib 0.18.0

GRIB format parser & writer for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use grib_template_derive::{Dump, TryFromSlice, WriteToBuffer};

/// Grid definition template 3.0 - latitude/longitude (or equidistant
/// cylindrical, or Plate Carrée).
#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_0 {
    pub earth: param_set::EarthShape,
    pub lat_lon: param_set::LatLonGrid,
}

/// Grid definition template 3.1 - rotated latitude/longitude (or equidistant
/// cylindrical, or Plate Carrée).
///
/// # Examples
///
/// ```
/// use std::io::Read;
///
/// use grib::{
///     TryFromSlice,
///     def::grib2::template::{Template3_1, param_set},
/// };
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut buf = Vec::new();
///
///     let f = std::fs::File::open(
///         "testdata/20260219T00Z_MSC_HRDPS_CAPE_Sfc_RLatLon0.0225_PT000H.grib2",
///     )?;
///     let mut f = std::io::BufReader::new(f);
///     f.read_to_end(&mut buf)?;
///
///     let mut pos = 0x33;
///     let actual = Template3_1::try_from_slice(&buf, &mut pos)?;
///     let expected = Template3_1 {
///         earth: param_set::EarthShape {
///             shape: 6,
///             spherical_earth_radius: param_set::ScaledValue {
///                 scale_factor: 0xff,
///                 scaled_value: 0xffffffff,
///             },
///             major_axis: param_set::ScaledValue {
///                 scale_factor: 0xff,
///                 scaled_value: 0xffffffff,
///             },
///             minor_axis: param_set::ScaledValue {
///                 scale_factor: 0xff,
///                 scaled_value: 0xffffffff,
///             },
///         },
///         lat_lon: param_set::LatLonGrid {
///             grid: param_set::Grid {
///                 ni: 2540,
///                 nj: 1290,
///                 initial_production_domain_basic_angle: 0,
///                 basic_angle_subdivisions: 0xffffffff,
///                 first_point_lat: -12302501,
///                 first_point_lon: 345178780,
///                 resolution_and_component_flags: param_set::ResolutionAndComponentFlags(
///                     0b00111000,
///                 ),
///                 last_point_lat: 16700001,
///                 last_point_lon: 42306283,
///             },
///             scanning_mode: param_set::ScanningMode(0b01000000),
///             i_direction_inc: 22500,
///             j_direction_inc: 22500,
///         },
///         rotation: param_set::Rotation {
///             south_pole_lat: -36088520,
///             south_pole_lon: 245305142,
///             rot_angle: 0.,
///         },
///     };
///     assert_eq!(actual, expected);
///
///     Ok(())
/// }
/// ```
#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_1 {
    pub earth: param_set::EarthShape,
    pub lat_lon: param_set::LatLonGrid,
    pub rotation: param_set::Rotation,
}

/// Grid definition template 3.2 - stretched latitude/longitude (or equidistant
/// cylindrical, or Plate Carrée).
#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_2 {
    pub earth: param_set::EarthShape,
    pub lat_lon: param_set::LatLonGrid,
    pub stretching: param_set::Stretching,
}

/// Grid definition template 3.3 - stretched and rotated latitude/longitude (or
/// equidistant cylindrical, or Plate Carrée).
#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_3 {
    pub earth: param_set::EarthShape,
    pub lat_lon: param_set::LatLonGrid,
    pub rotation: param_set::Rotation,
    pub stretching: param_set::Stretching,
}

/// Grid definition template 3.10 - Mercator.
#[derive(Debug, PartialEq, Eq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_10 {
    pub earth_shape: param_set::EarthShape,
    /// Ni - number of points along a parallel.
    pub ni: u32,
    /// Nj - number of points along a meridian.
    pub nj: u32,
    /// La1 - latitude of first grid point.
    pub first_point_lat: i32,
    /// Lo1 - longitude of first grid point.
    pub first_point_lon: u32,
    pub resolution_and_component_flags: param_set::ResolutionAndComponentFlags,
    /// LaD - Latitude(s) at which the Mercator projection intersects the Earth
    /// (Latitude(s) where Di and Dj are specified).
    pub lad: i32,
    /// La2 - latitude of last grid point.
    pub last_point_lat: i32,
    /// Lo2 - longitude of last grid point.
    pub last_point_lon: u32,
    pub scanning_mode: param_set::ScanningMode,
    /// Orientation of the grid, angle between i direction on the map and the
    /// equator (see Note 1).
    pub orientation: u32,
    /// Di - longitudinal direction grid length (see Note 2).
    pub di: u32,
    /// Dj - latitudinal direction grid length (see Note 2).
    pub dj: u32,
}

/// Grid definition template 3.20 - polar stereographic projection.
///
/// # Examples
///
/// ```
/// use std::io::{BufReader, Read};
///
/// use grib::{
///     TryFromSlice,
///     def::grib2::template::{Template3_20, param_set},
/// };
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut buf = Vec::new();
///
///     let f = std::fs::File::open(
///         "testdata/CMC_RDPA_APCP-024-0100cutoff_SFC_0_ps10km_2023121806_000.grib2.xz",
///     )?;
///     let f = BufReader::new(f);
///     let mut f = xz2::bufread::XzDecoder::new(f);
///     f.read_to_end(&mut buf)?;
///
///     let mut pos = 0x33;
///     let actual = Template3_20::try_from_slice(&buf, &mut pos)?;
///     let expected = Template3_20 {
///         earth_shape: param_set::EarthShape {
///             shape: 6,
///             spherical_earth_radius: param_set::ScaledValue {
///                 scale_factor: 0xff,
///                 scaled_value: 0xffffffff,
///             },
///             major_axis: param_set::ScaledValue {
///                 scale_factor: 0xff,
///                 scaled_value: 0xffffffff,
///             },
///             minor_axis: param_set::ScaledValue {
///                 scale_factor: 0xff,
///                 scaled_value: 0xffffffff,
///             },
///         },
///         ni: 935,
///         nj: 824,
///         first_point_lat: 18145030,
///         first_point_lon: 217107456,
///         resolution_and_component_flags: param_set::ResolutionAndComponentFlags(0b00001000),
///         lad: 60000000,
///         lov: 249000000,
///         dx: 10000000,
///         dy: 10000000,
///         projection_centre: param_set::ProjectionCentreFlag(0b00000000),
///         scanning_mode: param_set::ScanningMode(0b01000000),
///     };
///     assert_eq!(actual, expected);
///
///     Ok(())
/// }
/// ```
#[derive(Debug, PartialEq, Eq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_20 {
    pub earth_shape: param_set::EarthShape,
    /// Nx - number of points along the x-axis.
    pub ni: u32,
    /// Ny - number of points along the y-axis.
    pub nj: u32,
    /// La1 - latitude of first grid point.
    pub first_point_lat: i32,
    /// Lo1 - longitude of first grid point.
    pub first_point_lon: u32,
    pub resolution_and_component_flags: param_set::ResolutionAndComponentFlags,
    /// LaD - latitude where Dx and Dy are specified.
    pub lad: i32,
    /// LoV - orientation of the grid (see Note 2).
    pub lov: i32,
    /// Dx - x-direction grid length (see Note 3).
    pub dx: u32,
    /// Dy - y-direction grid length (see Note 3).
    pub dy: u32,
    pub projection_centre: param_set::ProjectionCentreFlag,
    pub scanning_mode: param_set::ScanningMode,
}

/// Grid definition template 3.30 - Lambert conformal.
///
/// # Examples
///
/// ```
/// use std::io::{BufReader, Read};
///
/// use grib::{
///     TryFromSlice,
///     def::grib2::template::{Template3_30, param_set},
/// };
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let mut buf = Vec::new();
///
///     let f = std::fs::File::open("testdata/ds.critfireo.bin.xz")?;
///     let f = BufReader::new(f);
///     let mut f = xz2::bufread::XzDecoder::new(f);
///     f.read_to_end(&mut buf)?;
///
///     let mut pos = 0x83;
///     let actual = Template3_30::try_from_slice(&buf, &mut pos)?;
///     let expected = 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,
///     };
///     assert_eq!(actual, expected);
///
///     Ok(())
/// }
/// ```
#[derive(Debug, PartialEq, Eq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_30 {
    pub earth_shape: param_set::EarthShape,
    /// Nx - number of points along the x-axis.
    pub ni: u32,
    /// Ny - number of points along the y-axis.
    pub nj: u32,
    /// La1 - latitude of first grid point.
    pub first_point_lat: i32,
    /// Lo1 - longitude of first grid point.
    pub first_point_lon: u32,
    pub resolution_and_component_flags: param_set::ResolutionAndComponentFlags,
    /// LaD - latitude where Dx and Dy are specified.
    pub lad: i32,
    /// LoV - longitude of meridian parallel to y-axis along which latitude
    /// increases as the y-coordinate increases.
    pub lov: u32,
    /// Dx - x-direction grid length (see Note 1).
    pub dx: u32,
    /// Dy - y-direction grid length (see Note 1).
    pub dy: u32,
    pub projection_centre: param_set::ProjectionCentreFlag,
    pub scanning_mode: param_set::ScanningMode,
    /// Latin 1 - first latitude from the pole at which the secant cone cuts the
    /// sphere.
    pub latin1: i32,
    /// Latin 2 - second latitude from the pole at which the secant cone cuts
    /// the sphere.
    pub latin2: i32,
    /// Latitude of the southern pole of projection.
    pub south_pole_lat: i32,
    /// Longitude of the southern pole of projection.
    pub south_pole_lon: u32,
}

/// Grid definition template 3.40 - Gaussian latitude/longitude.
#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_40 {
    pub earth: param_set::EarthShape,
    pub gaussian: param_set::GaussianGrid,
}

/// Grid definition template 3.41 - rotated Gaussian latitude/longitude.
#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_41 {
    pub earth: param_set::EarthShape,
    pub gaussian: param_set::GaussianGrid,
    pub rotation: param_set::Rotation,
}

/// Grid definition template 3.42 - stretched Gaussian latitude/longitude.
#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_42 {
    pub earth: param_set::EarthShape,
    pub gaussian: param_set::GaussianGrid,
    pub stretching: param_set::Stretching,
}

/// Grid definition template 3.43 - stretched and rotated Gaussian
/// latitude/longitude.
#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_43 {
    pub earth: param_set::EarthShape,
    pub gaussian: param_set::GaussianGrid,
    pub rotation: param_set::Rotation,
    pub stretching: param_set::Stretching,
}

/// Grid definition template 3.101 - general unstructured grid.
#[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
pub struct Template3_101 {
    /// Shape of the Earth (see Code table 3.2).
    pub earth_shape: u8,
    /// Number of grid used (defined by originating centre).
    #[grib_template(num_octets = 3)]
    pub grid_num: u32,
    /// Number of grid in reference (to allow annotating for Arakawa C-grid on
    /// arbitrary grid) (see Note).
    pub ref_grid_num: u8,
    /// Universally Unique Identifier of horizontal grid.
    pub uuid: [u8; 16],
}

pub(crate) mod param_set {
    use grib_template_derive::{Dump, TryFromSlice, WriteToBuffer};

    use super::super::template::param_set::ScaledValue;

    #[derive(Debug, PartialEq, Eq, TryFromSlice, WriteToBuffer, Dump)]
    pub struct EarthShape {
        /// Shape of the Earth (see Code table 3.2).
        pub shape: u8,
        /// Radius of spherical Earth.
        #[dump(doc(
            scale_factor = "Scale factor of radius of spherical Earth.",
            scaled_value = "Scaled value of radius of spherical Earth.",
        ))]
        pub spherical_earth_radius: ScaledValue<u8, u32>,
        /// Major axis of oblate spheroid Earth.
        #[dump(doc(
            scale_factor = "Scale factor of major axis of oblate spheroid Earth.",
            scaled_value = "Scaled value of major axis of oblate spheroid Earth.",
        ))]
        pub major_axis: ScaledValue<u8, u32>,
        /// Minor axis of oblate spheroid Earth.
        #[dump(doc(
            scale_factor = "Scale factor of minor axis of oblate spheroid Earth.",
            scaled_value = "Scaled value of minor axis of oblate spheroid Earth.",
        ))]
        pub minor_axis: ScaledValue<u8, u32>,
    }

    #[derive(Debug, PartialEq, Eq, TryFromSlice, WriteToBuffer, Dump)]
    pub struct LatLonGrid {
        pub grid: Grid,
        /// Di - i direction increment (see Notes 1 and 5).
        pub i_direction_inc: u32,
        /// Dj - j direction increment (see Notes 1 and 5).
        pub j_direction_inc: u32,
        pub scanning_mode: ScanningMode,
    }

    #[derive(Debug, PartialEq, Eq, TryFromSlice, WriteToBuffer, Dump)]
    pub struct GaussianGrid {
        pub grid: Grid,
        /// Di - i direction increment (see Notes 1 and 5).
        pub i_direction_inc: u32,
        /// N - number of parallels between a pole and the Equator (see Note 2).
        pub n: u32,
        pub scanning_mode: ScanningMode,
    }

    #[derive(Debug, PartialEq, Eq, TryFromSlice, WriteToBuffer, Dump)]
    pub struct Grid {
        /// Ni - number of points along a parallel.
        pub ni: u32,
        /// Nj - number of points along a meridian.
        pub nj: u32,
        /// Basic angle of the initial production domain (see Note 1).
        pub initial_production_domain_basic_angle: u32,
        /// Subdivisions of basic angle used to define extreme longitudes and
        /// latitudes, and direction increments (see Note 1).
        pub basic_angle_subdivisions: u32,
        /// La1 - latitude of first grid point (see Note 1).
        pub first_point_lat: i32,
        /// Lo1 - longitude of first grid point (see Note 1).
        pub first_point_lon: u32,
        pub resolution_and_component_flags: ResolutionAndComponentFlags,
        /// La2 - latitude of last grid point (see Note 1).
        pub last_point_lat: i32,
        /// Lo2 - longitude of last grid point (see Note 1).
        pub last_point_lon: u32,
    }

    #[derive(Debug, PartialEq, Eq, Clone, Copy, TryFromSlice, WriteToBuffer, Dump)]
    pub struct ProjectionCentreFlag(
        /// Projection centre flag (see Flag table 3.5).
        pub u8,
    );

    #[derive(Debug, PartialEq, Eq, Clone, Copy, TryFromSlice, WriteToBuffer, Dump)]
    pub struct ResolutionAndComponentFlags(
        /// Resolution and component flags (see Flag table 3.3).
        pub u8,
    );

    #[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
    pub struct Rotation {
        /// Latitude of the southern pole of projection.
        pub south_pole_lat: i32,
        /// Longitude of the southern pole of projection.
        pub south_pole_lon: u32,
        /// Angle of rotation of projection.
        pub rot_angle: f32,
    }

    #[derive(Debug, PartialEq, TryFromSlice, WriteToBuffer, Dump)]
    pub struct Stretching {
        /// Latitude of the pole of stretching.
        pub pole_lat: i32,
        /// Longitude of the pole of stretching.
        pub pole_lon: u32,
        /// Stretching factor.
        pub factor: u32,
    }

    #[derive(Debug, PartialEq, Eq, Clone, Copy, TryFromSlice, WriteToBuffer, Dump)]
    pub struct ScanningMode(
        /// Scanning mode (flags - see Flag table 3.4).
        pub u8,
    );
}