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, 6) => "Dew point temperature",
96        (0, 1, 1) => "Relative humidity",
97        (0, 1, 8) => "Total precipitation",
98        (0, 2, 2) => "U-component of wind",
99        (0, 2, 3) => "V-component of wind",
100        (0, 3, 0) => "Pressure",
101        (0, 3, 1) => "Pressure reduced to MSL",
102        (0, 3, 5) => "Geopotential height",
103        (0, 6, 1) => "Total cloud cover",
104        (0, 7, 6) => "Convective available potential energy",
105        (10, 0, 3) => "Significant height of combined wind waves and swell",
106        _ => "Unknown parameter",
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    #[test]
115    fn known_parameters() {
116        assert_eq!(grib1_parameter_name(2, 11), "TMP");
117        assert_eq!(grib1_parameter_name(2, 33), "UGRD");
118        assert_eq!(parameter_name(0, 0, 0), "TMP");
119        assert_eq!(parameter_name(0, 2, 2), "UGRD");
120        assert_eq!(parameter_name(0, 3, 5), "HGT");
121        assert_eq!(parameter_name(10, 0, 3), "HTSGW");
122    }
123
124    #[test]
125    fn unknown_parameter() {
126        assert_eq!(parameter_name(255, 255, 255), "unknown");
127    }
128}