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
use chrono::NaiveDateTime;
use error::*;
use missing_value::OptionVal;
#[derive(Default)]
pub struct Sounding {
pub num: OptionVal<i32>,
pub valid_time: Option<NaiveDateTime>,
pub lead_time: OptionVal<i32>,
pub lat: OptionVal<f64>,
pub lon: OptionVal<f64>,
pub elevation: OptionVal<f64>,
pub show: OptionVal<f64>,
pub li: OptionVal<f64>,
pub swet: OptionVal<f64>,
pub kinx: OptionVal<f64>,
pub lclp: OptionVal<f64>,
pub pwat: OptionVal<f64>,
pub totl: OptionVal<f64>,
pub cape: OptionVal<f64>,
pub lclt: OptionVal<f64>,
pub cins: OptionVal<f64>,
pub eqlv: OptionVal<f64>,
pub lfc: OptionVal<f64>,
pub brch: OptionVal<f64>,
pub hain: OptionVal<i32>,
pub pressure: Vec<OptionVal<f64>>,
pub temperature: Vec<OptionVal<f64>>,
pub wet_bulb: Vec<OptionVal<f64>>,
pub dew_point: Vec<OptionVal<f64>>,
pub theta_e: Vec<OptionVal<f64>>,
pub direction: Vec<OptionVal<f64>>,
pub speed: Vec<OptionVal<f64>>,
pub omega: Vec<OptionVal<f64>>,
pub height: Vec<OptionVal<f64>>,
pub cloud_fraction: Vec<OptionVal<f64>>,
pub mslp: OptionVal<f64>,
pub station_pres: OptionVal<f64>,
pub low_cloud: OptionVal<f64>,
pub mid_cloud: OptionVal<f64>,
pub hi_cloud: OptionVal<f64>,
pub uwind: OptionVal<f64>,
pub vwind: OptionVal<f64>,
}
impl Sounding {
#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
pub fn validate(&self) -> Result<()> {
macro_rules! validate_f64_positive {
($var:ident, $err_msg:ident, $var_name:expr) => {
let opt: Option<f64> = self.$var.into();
if let Some(val) = opt {
if val < 0.0 {
$err_msg.push_str(&format!("\n{} < 0.0: {}", $var_name, val));
}
}
};
}
macro_rules! validate_vector_len {
($var:ident, $len:expr, $err_msg:ident, $var_name:expr) => {
if !self.$var.is_empty() && self.$var.len() != $len {
$err_msg
.push_str(&format!("\n{} array has different length than pressure array.",
$var_name));
}
};
}
let mut error_msg = String::from("");
if self.pressure.is_empty() {
error_msg.push_str("\nPressure variable required, none given.");
}
let len = self.pressure.len();
validate_vector_len!(temperature, len, error_msg, "Temperature");
validate_vector_len!(wet_bulb, len, error_msg, "Wet bulb temperature");
validate_vector_len!(dew_point, len, error_msg, "Dew point");
validate_vector_len!(theta_e, len, error_msg, "Theta-e");
validate_vector_len!(direction, len, error_msg, "Wind direction");
validate_vector_len!(speed, len, error_msg, "wind speed");
validate_vector_len!(omega, len, error_msg, "Omega (pressure vertical velocity)");
validate_vector_len!(height, len, error_msg, "Height");
validate_vector_len!(cloud_fraction, len, error_msg, "Cloud fraction");
let mut pressure_one_level_down = ::std::f64::MAX;
if let Some(val) = self.station_pres.as_option() {
pressure_one_level_down = val;
}
for pres in &self.pressure {
if pres.as_option().is_none() {
continue;
}
let pres_val = pres.unwrap();
if pressure_one_level_down < pres_val {
error_msg.push_str(&format!(
"\nPressure increasing with height: {} < {}",
pressure_one_level_down,
pres_val
));
}
pressure_one_level_down = pres_val;
}
let mut height_one_level_down = ::std::f64::MIN;
for hght in &self.height {
if hght.as_option().is_none() {
continue;
}
let hght_val = hght.unwrap();
if height_one_level_down > hght_val {
error_msg.push_str(&format!(
"\nHeight values decreasing with height: {} < {}",
height_one_level_down,
hght_val
));
}
height_one_level_down = hght_val;
}
for (t, wb) in self.temperature.iter().zip(self.wet_bulb.iter()) {
if t.as_option().is_none() || wb.as_option().is_none() {
continue;
}
if t.unwrap() < wb.unwrap() {
error_msg.push_str(&format!(
"\nTemperature < Wet bulb: {} < {}",
t.unwrap(),
wb.unwrap()
));
}
}
for (t, dp) in self.temperature.iter().zip(self.dew_point.iter()) {
if t.as_option().is_none() || dp.as_option().is_none() {
continue;
}
if t.unwrap() < dp.unwrap() {
error_msg.push_str(&format!(
"\nTemperature < Dew Point: {} < {}",
t.unwrap(),
dp.unwrap()
));
}
}
for (wb, dp) in self.wet_bulb.iter().zip(self.dew_point.iter()) {
if wb.as_option().is_none() || dp.as_option().is_none() {
continue;
}
if wb.unwrap() < dp.unwrap() {
error_msg.push_str(&format!(
"\nWet bulb < Dew Point: {} < {}",
wb.unwrap(),
dp.unwrap()
));
}
}
for spd in &self.speed {
if spd.as_option().is_none() {
continue;
}
if spd.unwrap() < 0.0 {
error_msg.push_str(&format!("\nWind speed < 0: {} < 0.0", spd.unwrap()));
}
}
for cld in &self.cloud_fraction {
if cld.as_option().is_none() {
continue;
}
if cld.unwrap() < 0.0 {
error_msg.push_str(&format!("\nCloud fraction < 0: {} < 0.0", cld.unwrap()));
}
}
validate_f64_positive!(cape, error_msg, "CAPE");
validate_f64_positive!(pwat, error_msg, "PWAT");
let opt: Option<f64> = self.cins.into();
if let Some(val) = opt {
if val > 0.0 {
error_msg.push_str(&format!("\nCINS > 0.0: {}", val));
}
}
let opt: Option<i32> = self.hain.into();
if let Some(val) = opt {
match val {
2...6 => {}
_ => error_msg.push_str(&format!("\nInvalid Haines Index: {}", val)),
}
}
validate_f64_positive!(low_cloud, error_msg, "low cloud");
validate_f64_positive!(mid_cloud, error_msg, "mid cloud");
validate_f64_positive!(hi_cloud, error_msg, "hi cloud");
if error_msg == "" {
Ok(())
} else {
error_msg.push('\n');
Err(Error::from(ErrorKind::ValidationError(error_msg)))
}
}
}