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
//! Indexes that are specific to a sounding, but not a particular parcel analysis of that sounding.

use sounding_base::{Profile, Sounding};

use error::*;
use interpolation::linear_interpolate_sounding;

/// The Total Totals index
pub fn total_totals(snd: &Sounding) -> Result<f64> {
    let h5 = linear_interpolate_sounding(snd, 500.0)?;
    let h85 = linear_interpolate_sounding(snd, 850.0)?;
    let t_500 = h5.temperature.ok_or(AnalysisError::MissingValue)?;
    let t_850 = h85.temperature.ok_or(AnalysisError::MissingValue)?;
    let td_850 = h85.dew_point.ok_or(AnalysisError::MissingValue)?;

    let cross_totals = td_850 - t_500;
    let vertical_totals = t_850 - t_500;

    Ok(cross_totals + vertical_totals)
}

/// The SWeT (Severe Weather Threat) index
#[inline]
pub fn swet(snd: &Sounding) -> Result<f64> {
    let h5 = linear_interpolate_sounding(snd, 500.0)?;
    let h85 = linear_interpolate_sounding(snd, 850.0)?;

    let mut td_850 = h85.dew_point.ok_or(AnalysisError::MissingValue)?;
    if td_850 < 0.0 {
        td_850 = 0.0;
    }

    let v_850 = h85.speed.ok_or(AnalysisError::MissingValue)?;
    let d_850 = h85.direction.ok_or(AnalysisError::MissingValue)?;

    let v_500 = h5.speed.ok_or(AnalysisError::MissingValue)?;
    let d_500 = h5.direction.ok_or(AnalysisError::MissingValue)?;

    let mut total_totals = total_totals(snd)?;
    if total_totals < 49.0 {
        total_totals = 49.0;
    }

    let mut dir_component = (d_500 - d_850).to_radians().sin();
    if dir_component < 0.0
        || (d_850 >= 130.0 && d_850 <= 250.0)
        || (d_500 >= 210.0 && d_500 <= 310.0)
        || (d_500 - d_850) >= 0.0
        || (v_850 >= 15.0 && v_500 >= 15.0)
    {
        dir_component = 0.0;
    } else {
        dir_component = 125.0 * (dir_component + 0.2);
    }

    Ok(12.0 * td_850 + 20.0 * (total_totals - 49.0) + 2.0 * v_850 + v_500 + dir_component)
}

/// The K-index
#[inline]
pub fn kindex(snd: &Sounding) -> Result<f64> {
    let h5 = linear_interpolate_sounding(snd, 500.0)?;
    let h7 = linear_interpolate_sounding(snd, 700.0)?;
    let h85 = linear_interpolate_sounding(snd, 850.0)?;

    let t5 = h5.temperature.ok_or(AnalysisError::MissingValue)?;
    let t7 = h7.temperature.ok_or(AnalysisError::MissingValue)?;
    let t85 = h85.temperature.ok_or(AnalysisError::MissingValue)?;

    let td7 = h7.dew_point.ok_or(AnalysisError::MissingValue)?;
    let td85 = h85.dew_point.ok_or(AnalysisError::MissingValue)?;

    Ok(t85 - t5 + td85 - t7 + td7)
}

/// Precipitable water (mm)
#[inline]
pub fn precipitable_water(snd: &Sounding) -> Result<f64> {
    let p_profile = snd.get_profile(Profile::Pressure);
    let dp_profile = snd.get_profile(Profile::DewPoint);

    let (integrated_mw, _, _) = izip!(p_profile, dp_profile)
        .filter_map(|pair| {
            let (p, dp) = pair;
            if p.is_some() && dp.is_some() {
                Some((p.unpack(), dp.unpack()))
            } else {
                None
            }
        })
        .filter_map(|(p, dp)| {
            ::metfor::mixing_ratio(dp, p)
                .ok()
                .and_then(|mw| Some((p, mw)))
        })
        .fold((0.0, 0.0, 0.0), |(mut acc_mw, prev_p, prev_mw), (p, mw)| {
            let dp = prev_p - p;
            if dp > 0.0 {
                acc_mw += (mw + prev_mw) * dp;
            }

            (acc_mw, p, mw)
        });

    Ok(integrated_mw / 9.81 / 997.0 * 100_000.0 / 2.0)
}

/// The Haines index for fire weather
#[inline]
pub fn haines(snd: &Sounding) -> Result<f64> {
    snd.get_station_info()
        .elevation()
        .into_option()
        .and_then(|elev_meters| {
            let (stability_term, stability_cutoffs, moisture_term, moisture_cutoffs) =
                if elev_meters <= 304.8 {
                    // Low elevation Haines
                    let level1 = linear_interpolate_sounding(snd, 950.0).ok()?;
                    let level2 = linear_interpolate_sounding(snd, 850.0).ok()?;

                    let t_low = level1.temperature.ok_or(AnalysisError::MissingValue).ok()?;
                    let t_hi = level2.temperature.ok_or(AnalysisError::MissingValue).ok()?;
                    let dp_hi = level2.dew_point.ok_or(AnalysisError::MissingValue).ok()?;

                    let stability_term = t_low - t_hi;
                    const STABILITY_CUTOFFS: (f64, f64) = (4.0, 7.0);

                    let moisture_term = t_hi - dp_hi;
                    const MOISTURE_CUTOFFS: (f64, f64) = (6.0, 9.0);

                    (
                        stability_term,
                        STABILITY_CUTOFFS,
                        moisture_term,
                        MOISTURE_CUTOFFS,
                    )
                } else if elev_meters <= 914.4 {
                    // Mid elevation Haines
                    let level1 = linear_interpolate_sounding(snd, 850.0).ok()?;
                    let level2 = linear_interpolate_sounding(snd, 700.0).ok()?;

                    let t_low = level1.temperature.ok_or(AnalysisError::MissingValue).ok()?;
                    let t_hi = level2.temperature.ok_or(AnalysisError::MissingValue).ok()?;
                    let dp_low = level2.dew_point.ok_or(AnalysisError::MissingValue).ok()?;

                    let stability_term = t_low - t_hi;
                    const STABILITY_CUTOFFS: (f64, f64) = (6.0, 10.0);

                    let moisture_term = t_low - dp_low;
                    const MOISTURE_CUTOFFS: (f64, f64) = (6.0, 12.0);

                    (
                        stability_term,
                        STABILITY_CUTOFFS,
                        moisture_term,
                        MOISTURE_CUTOFFS,
                    )
                } else {
                    // High elevation Haines
                    let level1 = linear_interpolate_sounding(snd, 700.0).ok()?;
                    let level2 = linear_interpolate_sounding(snd, 500.0).ok()?;

                    let t_low = level1.temperature.ok_or(AnalysisError::MissingValue).ok()?;
                    let t_hi = level2.temperature.ok_or(AnalysisError::MissingValue).ok()?;
                    let dp_low = level2.dew_point.ok_or(AnalysisError::MissingValue).ok()?;

                    let stability_term = t_low - t_hi;
                    const STABILITY_CUTOFFS: (f64, f64) = (18.0, 21.0);

                    let moisture_term = t_low - dp_low;
                    const MOISTURE_CUTOFFS: (f64, f64) = (15.0, 20.0);

                    (
                        stability_term,
                        STABILITY_CUTOFFS,
                        moisture_term,
                        MOISTURE_CUTOFFS,
                    )
                };

            let stability_part = if stability_term < stability_cutoffs.0 {
                1.0
            } else if stability_term < stability_cutoffs.1 {
                2.0
            } else {
                3.0
            };

            let moisture_part = if moisture_term < moisture_cutoffs.0 {
                1.0
            } else if moisture_term < moisture_cutoffs.1 {
                2.0
            } else {
                3.0
            };

            let haines_index = stability_part + moisture_part;

            debug_assert!(haines_index <= 6.0);
            Some(haines_index)
        })
        .ok_or(AnalysisError::MissingValue)
}