Skip to main content

grib_reader/
parameter.rs

1//! WMO parameter tables (Code Table 4.2) for GRIB2.
2//!
3//! Maps (discipline, parameter_category, parameter_number) to human-readable names.
4
5/// Look up a GRIB1 parameter short name.
6pub fn grib1_parameter_name(table_version: u8, number: u8) -> &'static str {
7    match (table_version, number) {
8        (_, 1) => "PRES",
9        (_, 2) => "PRMSL",
10        (_, 7) => "HGT",
11        (_, 11) => "TMP",
12        (_, 17) => "DPT",
13        (_, 33) => "UGRD",
14        (_, 34) => "VGRD",
15        (_, 39) => "VVEL",
16        (_, 52) => "RH",
17        (_, 54) => "PWAT",
18        (_, 61) => "APCP",
19        (_, 71) => "TCDC",
20        _ => "unknown",
21    }
22}
23
24/// Look up a GRIB1 parameter description.
25pub fn grib1_parameter_description(table_version: u8, number: u8) -> &'static str {
26    match (table_version, number) {
27        (_, 1) => "Pressure",
28        (_, 2) => "Pressure reduced to mean sea level",
29        (_, 7) => "Geopotential height",
30        (_, 11) => "Temperature",
31        (_, 17) => "Dew point temperature",
32        (_, 33) => "U-component of wind",
33        (_, 34) => "V-component of wind",
34        (_, 39) => "Vertical velocity",
35        (_, 52) => "Relative humidity",
36        (_, 54) => "Precipitable water",
37        (_, 61) => "Total precipitation",
38        (_, 71) => "Total cloud cover",
39        _ => "Unknown parameter",
40    }
41}
42
43/// Look up a parameter short name from WMO code tables.
44pub fn parameter_name(discipline: u8, category: u8, number: u8) -> &'static str {
45    match (discipline, category, number) {
46        // Discipline 0: Meteorological products
47        // Category 0: Temperature
48        (0, 0, 0) => "TMP",  // Temperature
49        (0, 0, 1) => "VTMP", // Virtual temperature
50        (0, 0, 2) => "POT",  // Potential temperature
51        (0, 0, 4) => "TMAX", // Maximum temperature
52        (0, 0, 5) => "TMIN", // Minimum temperature
53        (0, 0, 6) => "DPT",  // Dew point temperature
54        // Category 1: Moisture
55        (0, 1, 0) => "SPFH", // Specific humidity
56        (0, 1, 1) => "RH",   // Relative humidity
57        (0, 1, 3) => "PWAT", // Precipitable water
58        (0, 1, 8) => "APCP", // Total precipitation
59        // Category 2: Momentum
60        (0, 2, 0) => "WDIR",  // Wind direction
61        (0, 2, 1) => "WIND",  // Wind speed
62        (0, 2, 2) => "UGRD",  // U-component of wind
63        (0, 2, 3) => "VGRD",  // V-component of wind
64        (0, 2, 22) => "GUST", // Wind gust
65        // Category 3: Mass
66        (0, 3, 0) => "PRES",  // Pressure
67        (0, 3, 1) => "PRMSL", // Pressure reduced to MSL
68        (0, 3, 5) => "HGT",   // Geopotential height
69        // Category 4: Short-wave radiation
70        (0, 4, 7) => "DSWRF", // Downward short-wave radiation flux
71        // Category 5: Long-wave radiation
72        (0, 5, 3) => "DLWRF", // Downward long-wave radiation flux
73        // Category 6: Cloud
74        (0, 6, 1) => "TCDC", // Total cloud cover
75        // Category 7: Thermodynamic stability
76        (0, 7, 6) => "CAPE", // Convective available potential energy
77        (0, 7, 7) => "CIN",  // Convective inhibition
78
79        // Discipline 10: Oceanographic products
80        // Category 0: Waves
81        (10, 0, 3) => "HTSGW", // Significant wave height
82        (10, 0, 4) => "WVDIR", // Wave direction
83        (10, 0, 5) => "WVPER", // Wave period
84        // Category 3: Surface properties
85        (10, 3, 0) => "WTMP", // Water temperature
86
87        _ => "unknown",
88    }
89}
90
91/// Look up a human-readable parameter description.
92pub fn parameter_description(discipline: u8, category: u8, number: u8) -> &'static str {
93    match (discipline, category, number) {
94        (0, 0, 0) => "Temperature",
95        (0, 0, 1) => "Virtual temperature",
96        (0, 0, 2) => "Potential temperature",
97        (0, 0, 4) => "Maximum temperature",
98        (0, 0, 5) => "Minimum temperature",
99        (0, 0, 6) => "Dew point temperature",
100        (0, 1, 0) => "Specific humidity",
101        (0, 1, 1) => "Relative humidity",
102        (0, 1, 3) => "Precipitable water",
103        (0, 1, 8) => "Total precipitation",
104        (0, 2, 0) => "Wind direction",
105        (0, 2, 1) => "Wind speed",
106        (0, 2, 2) => "U-component of wind",
107        (0, 2, 3) => "V-component of wind",
108        (0, 2, 22) => "Wind gust",
109        (0, 3, 0) => "Pressure",
110        (0, 3, 1) => "Pressure reduced to MSL",
111        (0, 3, 5) => "Geopotential height",
112        (0, 4, 7) => "Downward short-wave radiation flux",
113        (0, 5, 3) => "Downward long-wave radiation flux",
114        (0, 6, 1) => "Total cloud cover",
115        (0, 7, 6) => "Convective available potential energy",
116        (0, 7, 7) => "Convective inhibition",
117        (10, 0, 3) => "Significant height of combined wind waves and swell",
118        (10, 0, 4) => "Direction of wind waves",
119        (10, 0, 5) => "Mean period of wind waves",
120        (10, 3, 0) => "Water temperature",
121        _ => "Unknown parameter",
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    #[test]
130    fn known_parameters() {
131        assert_eq!(grib1_parameter_name(2, 11), "TMP");
132        assert_eq!(grib1_parameter_name(2, 33), "UGRD");
133        assert_eq!(parameter_name(0, 0, 0), "TMP");
134        assert_eq!(parameter_name(0, 2, 2), "UGRD");
135        assert_eq!(parameter_name(0, 3, 5), "HGT");
136        assert_eq!(parameter_name(10, 0, 3), "HTSGW");
137        assert_eq!(parameter_description(0, 0, 2), "Potential temperature");
138        assert_eq!(parameter_description(10, 3, 0), "Water temperature");
139    }
140
141    #[test]
142    fn unknown_parameter() {
143        assert_eq!(parameter_name(255, 255, 255), "unknown");
144    }
145}