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
use optional::Optioned;
use metfor;
use sounding_base::{DataRow, Profile::*, Sounding};
use error::*;
use interpolation::{linear_interp, linear_interpolate_sounding};
use profile::equivalent_potential_temperature;
#[derive(Debug, Clone, Copy)]
pub struct Parcel {
pub temperature: f64,
pub pressure: f64,
pub dew_point: f64,
}
impl Parcel {
pub fn theta(&self) -> Result<f64> {
Ok(metfor::theta_kelvin(self.pressure, self.temperature)?)
}
pub fn theta_e(&self) -> Result<f64> {
Ok(metfor::theta_e_kelvin(
self.temperature,
self.dew_point,
self.pressure,
)?)
}
pub fn mixing_ratio(&self) -> Result<f64> {
Ok(metfor::mixing_ratio(self.dew_point, self.pressure)?)
}
pub fn virtual_temperature_c(&self) -> Result<f64> {
Ok(metfor::virtual_temperature_c(
self.temperature,
self.dew_point,
self.pressure,
)?)
}
pub fn from_datarow(mut dr: DataRow) -> Option<Self> {
let temperature = dr.temperature.take()?;
let pressure = dr.pressure.take()?;
let dew_point = dr.dew_point.take()?;
Some(Parcel {
temperature,
pressure,
dew_point,
})
}
}
pub fn mixed_layer_parcel(snd: &Sounding) -> Result<Parcel> {
use sounding_base::Profile::*;
let press = snd.get_profile(Pressure);
let t = snd.get_profile(Temperature);
let dp = snd.get_profile(DewPoint);
if press.is_empty() || t.is_empty() || dp.is_empty() {
return Err(AnalysisError::MissingProfile);
}
let bottom_p = press
.iter()
.filter_map(|&p| p.into_option())
.nth(0)
.ok_or(AnalysisError::NoDataProfile)?;
let (sum_p, sum_t, sum_mw) = izip!(press, t, dp)
.filter_map(|(p, t, dp)| {
if p.is_some() && t.is_some() && dp.is_some() {
Some((p.unpack(), t.unpack(), dp.unpack()))
} else {
None
}
})
.take_while(|&(p, _, _)| p >= bottom_p - 100.0)
.filter_map(|(p, t, dp)| {
metfor::theta_kelvin(p, t).ok().and_then(|theta| {
metfor::mixing_ratio(dp, p)
.ok()
.and_then(|mw| Some((p, theta, mw)))
})
})
.fold((0.0f64, 0.0f64, 0.0f64), |acc, (p, theta, mw)| {
let (sum_p, sum_t, sum_mw) = acc;
(sum_p + p, sum_t + theta * p, sum_mw + mw * p)
});
if sum_p == 0.0 {
return Err(AnalysisError::NotEnoughData);
}
let pressure = bottom_p;
let temperature = metfor::temperature_c_from_theta(sum_t / sum_p, bottom_p)?;
let dew_point = metfor::dew_point_from_p_and_mw(bottom_p, sum_mw / sum_p)
.map_err(|_| AnalysisError::InvalidInput)?;
Ok(Parcel {
temperature,
pressure,
dew_point,
})
}
pub fn surface_parcel(snd: &Sounding) -> Result<Parcel> {
use sounding_base::Surface::*;
let pressure = snd
.get_surface_value(StationPressure)
.ok_or(AnalysisError::MissingValue)?;
let temperature = snd
.get_surface_value(Temperature)
.ok_or(AnalysisError::MissingValue)?;
let dew_point = snd
.get_surface_value(DewPoint)
.ok_or(AnalysisError::MissingValue)?;
Ok(Parcel {
temperature,
pressure,
dew_point,
})
}
pub fn pressure_parcel(snd: &Sounding, pressure: f64) -> Result<Parcel> {
let row = linear_interpolate_sounding(snd, pressure)?;
Parcel::from_datarow(row).ok_or(AnalysisError::MissingValue)
}
pub fn most_unstable_parcel(snd: &Sounding) -> Result<Parcel> {
use sounding_base::Profile::*;
let temp_vec: Vec<Optioned<f64>>;
let theta_e = if !snd.get_profile(ThetaE).is_empty() {
snd.get_profile(ThetaE)
} else {
temp_vec = equivalent_potential_temperature(snd);
&temp_vec
};
let press = snd.get_profile(Pressure);
let top_pressure = press
.iter()
.filter(|p| p.is_some())
.nth(0)
.ok_or(AnalysisError::NotEnoughData)?
.unpack()
- 300.0;
let (idx, _) = izip!(0.., press, theta_e)
.take_while(|&(_, press_opt, _)| {
if press_opt.is_some() {
press_opt.unpack() >= top_pressure
} else {
true }
}).filter_map(|(idx, _, theta_e_opt)| {
if theta_e_opt.is_some() {
Some((idx, theta_e_opt.unpack()))
} else {
None
}
}).fold(
(::std::usize::MAX, ::std::f64::MIN),
|(max_idx, max_val), (i, theta_e)| {
if theta_e > max_val {
(i, theta_e)
} else {
(max_idx, max_val)
}
},
);
let row = snd.get_data_row(idx).ok_or(AnalysisError::NoDataProfile)?;
Parcel::from_datarow(row).ok_or(AnalysisError::MissingValue)
}
pub fn convective_parcel(snd: &Sounding) -> Result<Parcel> {
let Parcel {
dew_point: dp,
pressure: initial_p,
..
} = mixed_layer_parcel(snd)?;
let tgt_mw = metfor::mixing_ratio(dp, initial_p)?;
let pressure = snd.get_profile(Pressure);
let temperature = snd.get_profile(Temperature);
let tgt_theta = izip!(pressure, temperature)
.filter_map(|(p, t)| {
let p = p.into_option()?;
let t = t.into_option()?;
let mw = metfor::mixing_ratio(t, p).ok()?;
let theta = metfor::theta_kelvin(p, t).ok()?;
let theta_v = theta * (1.0 + 0.61 * mw);
Some((p, t, mw, theta_v))
})
.skip_while(|(_, _, mw, _)| *mw <= tgt_mw)
.scan(
(0.0, 0.0, 0.0, 0.0, 0.0),
|(old_p, old_t, old_mw, old_theta_v, max_theta_v), (p, t, mw, theta_v)| {
let result = if mw < tgt_mw && *old_mw >= tgt_mw {
let tgt_theta_v = linear_interp(tgt_mw, *old_mw, mw, *old_theta_v, theta_v);
*max_theta_v = f64::max(*max_theta_v, tgt_theta_v);
let max_theta = *max_theta_v / (1.0 + 0.61 * tgt_mw) + 0.1;
Some(Some(max_theta))
} else {
Some(None)
};
*max_theta_v = max_theta_v.max(theta_v);
*old_p = p;
*old_t = t;
*old_mw = mw;
*old_theta_v = theta_v;
result
},
)
.filter_map(|opt_opt| opt_opt)
.nth(0)
.ok_or(AnalysisError::NotEnoughData)?;
let tgt_t = metfor::temperature_c_from_theta(tgt_theta, initial_p)?;
Ok(Parcel {
pressure: initial_p,
temperature: tgt_t,
dew_point: dp,
})
}