1use crate::def::grib2::template::param_set::EarthShape;
2
3impl EarthShape {
4 pub fn radii(&self) -> Option<(f64, f64)> {
5 let radii = match self.shape.0 {
6 0 => (6367470.0, 6367470.0),
7 1 => {
8 let radius = f64::from(self.spherical_earth_radius.scaled_value)
9 * f64::powf(10., f64::from(self.spherical_earth_radius.scale_factor));
10 (radius, radius)
11 }
12 2 => (6378160.0, 6356775.0),
13 3 => {
14 let (major, minor) = self.radii_defined();
15 (major * 1000., minor * 1000.)
16 }
17 4 => (6378137.0, 6356752.314),
18 5 => (6378137.0, 6356752.3142), 6 => (6371229.0, 6371229.0),
20 7 => self.radii_defined(),
21 8 => (6371200.0, 6371200.0),
22 9.. => return None,
23 };
24 Some(radii)
25 }
26
27 fn radii_defined(&self) -> (f64, f64) {
28 let major = f64::from(self.major_axis.scaled_value)
29 * f64::powf(10., f64::from(self.major_axis.scale_factor));
30 let minor = f64::from(self.minor_axis.scaled_value)
31 * f64::powf(10., f64::from(self.minor_axis.scale_factor));
32 (major, minor)
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39 use crate::{
40 TryFromSlice,
41 def::grib2::{table::EarthShapeType, template::param_set::ScaledValue},
42 test_utils::decompress_to_vec,
43 };
44
45 #[test]
46 fn radii_for_shape_1() -> Result<(), Box<dyn std::error::Error>> {
47 let buf = decompress_to_vec(crate::test_utils::data::grib2::NOAA_NDFD_CRITFIREO)?;
48 let mut pos = 0x83;
49 let earth_actual = EarthShape::try_from_slice(&buf, &mut pos)?;
50 let earth_expected = EarthShape {
51 shape: EarthShapeType(1),
52 spherical_earth_radius: ScaledValue {
53 scale_factor: 0,
54 scaled_value: 6371200,
55 },
56 major_axis: ScaledValue {
57 scale_factor: 0,
58 scaled_value: 0,
59 },
60 minor_axis: ScaledValue {
61 scale_factor: 0,
62 scaled_value: 0,
63 },
64 };
65 assert_eq!(earth_actual, earth_expected);
66 assert_eq!(earth_actual.radii(), Some((6_371_200., 6_371_200.)));
67
68 Ok(())
69 }
70
71 #[test]
72 fn radii_for_shape_2() -> Result<(), Box<dyn std::error::Error>> {
73 let buf = decompress_to_vec(crate::test_utils::data::grib2::NOAA_MRMS_REFLECTIVITY)?;
74 let mut pos = 0x33;
75 let earth_actual = EarthShape::try_from_slice(&buf, &mut pos)?;
76 let earth_expected = EarthShape {
77 shape: EarthShapeType(2),
78 spherical_earth_radius: ScaledValue {
79 scale_factor: 1,
80 scaled_value: 6367470,
81 },
82 major_axis: ScaledValue {
83 scale_factor: 1,
84 scaled_value: 6378160,
85 },
86 minor_axis: ScaledValue {
87 scale_factor: 1,
88 scaled_value: 6356775,
89 },
90 };
91 assert_eq!(earth_actual, earth_expected);
92 assert_eq!(earth_actual.radii(), Some((6_378_160.0, 6_356_775.0)));
93
94 Ok(())
95 }
96
97 #[test]
98 fn radii_for_shape_4() -> Result<(), Box<dyn std::error::Error>> {
99 let buf = decompress_to_vec(crate::test_utils::data::grib2::JMA_TORNADO_NOWCAST)?;
100 let mut pos = 0x33;
101 let earth_actual = EarthShape::try_from_slice(&buf, &mut pos)?;
102 let earth_expected = EarthShape {
103 shape: EarthShapeType(4),
104 spherical_earth_radius: ScaledValue {
105 scale_factor: 0xff,
106 scaled_value: 0xffffffff,
107 },
108 major_axis: ScaledValue {
109 scale_factor: 1,
110 scaled_value: 63781370,
111 },
112 minor_axis: ScaledValue {
113 scale_factor: 1,
114 scaled_value: 63567523,
115 },
116 };
117 assert_eq!(earth_actual, earth_expected);
118 assert_eq!(earth_actual.radii(), Some((6_378_137.0, 6_356_752.314)));
119
120 Ok(())
121 }
122
123 #[test]
124 fn radii_for_shape_6() -> Result<(), Box<dyn std::error::Error>> {
125 let buf = decompress_to_vec(crate::test_utils::data::grib2::NOAA_GDAS_0_10)?;
126 let mut pos = 0x33;
127 let earth_actual = EarthShape::try_from_slice(&buf, &mut pos)?;
128 let earth_expected = EarthShape {
129 shape: EarthShapeType(6),
130 spherical_earth_radius: ScaledValue {
131 scale_factor: 0,
132 scaled_value: 0,
133 },
134 major_axis: ScaledValue {
135 scale_factor: 0,
136 scaled_value: 0,
137 },
138 minor_axis: ScaledValue {
139 scale_factor: 0,
140 scaled_value: 0,
141 },
142 };
143 assert_eq!(earth_actual, earth_expected);
144 assert_eq!(earth_actual.radii(), Some((6_371_229.0, 6_371_229.0)));
145
146 Ok(())
147 }
148}