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
use sounding_base::Sounding;
use error::*;

macro_rules! validate_f64_positive {
    ($var:expr, $var_name:expr, $err_list:ident) => {
        if let Some(val) = $var {
            if val < 0.0 {
                $err_list.push_error(Err(ValidationError::InvalidNegativeValue($var_name, val)));
            }
        }
    };
}

macro_rules! validate_wind_direction {
    ($var:expr, $err_list:ident) => {
        if let Some(val) = $var {
            if val < 0.0 || val > 360.0 {
                $err_list.push_error(Err(ValidationError::InvalidWindDirection(val)));
            }
        }
    };
}

/// Validates the sounding with some simple sanity checks. For instance, checks that pressure
/// decreases with height.
pub fn validate(snd: &Sounding) -> Result<(), ValidationErrors> {
    use sounding_base::Profile;
    use sounding_base::Surface;

    let mut err_return = ValidationErrors::new();

    let pressure = snd.get_profile(Profile::Pressure);

    //
    // Sounding checks
    //

    // Pressure required as vertical coordinate.
    err_return.push_error(check_pressure_exists(pressure));

    let len = pressure.len();
    let temperature = snd.get_profile(Profile::Temperature);
    let wet_bulb = snd.get_profile(Profile::WetBulb);
    let dew_point = snd.get_profile(Profile::DewPoint);
    let theta_e = snd.get_profile(Profile::ThetaE);
    let direction = snd.get_profile(Profile::WindDirection);
    let speed = snd.get_profile(Profile::WindSpeed);
    let omega = snd.get_profile(Profile::PressureVerticalVelocity);
    let height = snd.get_profile(Profile::GeopotentialHeight);
    let cloud_fraction = snd.get_profile(Profile::CloudFraction);

    err_return.push_error(validate_vector_len(temperature, len, "Temperature"));
    err_return.push_error(validate_vector_len(wet_bulb, len, "Wet bulb temperature"));
    err_return.push_error(validate_vector_len(dew_point, len, "Dew point"));
    err_return.push_error(validate_vector_len(theta_e, len, "Theta-e"));
    err_return.push_error(validate_vector_len(direction, len, "Wind direction"));
    err_return.push_error(validate_vector_len(speed, len, "wind speed"));
    err_return.push_error(validate_vector_len(
        omega,
        len,
        "Omega (pressure vertical velocity)",
    ));
    err_return.push_error(validate_vector_len(height, len, "Height"));
    err_return.push_error(validate_vector_len(cloud_fraction, len, "Cloud fraction"));

    // Check that pressure always decreases with height and that the station pressure is more
    // than the lowest pressure level in sounding. AND..
    // Check height always increases with height.
    err_return.push_error(check_vertical_height_pressure(snd));

    // Check that dew point <= wet bulb <= t
    check_temp_wet_bulb_dew_point(snd, &mut err_return);

    // Check that speed >= 0
    for spd in speed {
        validate_f64_positive!(*spd, "Wind speed", err_return);
    }

    // Check that direction >= 0 && <= 360
    for dir in direction {
        validate_wind_direction!(*dir, err_return);
    }

    // Check that cloud fraction >= 0
    for cld in cloud_fraction {
        validate_f64_positive!(*cld, "Cloud fraction", err_return);
    }

    // Surface checks
    // Check that hi, mid, and low cloud are all positive or zero
    validate_f64_positive!(
        snd.get_surface_value(Surface::LowCloud),
        "Low cloud",
        err_return
    );
    validate_f64_positive!(
        snd.get_surface_value(Surface::MidCloud),
        "Mid cloud",
        err_return
    );
    validate_f64_positive!(
        snd.get_surface_value(Surface::HighCloud),
        "Hi cloud",
        err_return
    );

    validate_f64_positive!(
        snd.get_surface_value(Surface::WindSpeed),
        "Surface wind speed",
        err_return
    );

    validate_wind_direction!(snd.get_surface_value(Surface::WindDirection), err_return);

    validate_f64_positive!(snd.get_surface_value(Surface::MSLP), "MSLP", err_return);

    validate_f64_positive!(
        snd.get_surface_value(Surface::StationPressure),
        "Station pressure",
        err_return
    );

    err_return.check_any()
}

fn check_pressure_exists(pressure: &[Option<f64>]) -> Result<(), ValidationError> {
    if pressure.is_empty() {
        Err(ValidationError::NoPressureProfile)
    } else {
        Ok(())
    }
}

fn validate_vector_len(
    vec: &[Option<f64>],
    len: usize,
    var_name: &'static str,
) -> Result<(), ValidationError> {
    if !vec.is_empty() && vec.len() != len {
        Err(ValidationError::InvalidVectorLength(
            var_name,
            vec.len(),
            len,
        ))
    } else {
        Ok(())
    }
}

fn check_vertical_height_pressure(snd: &Sounding) -> Result<(), ValidationError> {
    use sounding_base::Profile::{GeopotentialHeight, Pressure};
    use sounding_base::Surface::StationPressure;

    // Check that pressure always decreases with height and that the station pressure is more
    // than the lowest pressure level in sounding.
    let pressure = snd.get_profile(Pressure);
    let mut pressure_one_level_down = snd.get_surface_value(StationPressure)
        .unwrap_or(::std::f64::MAX);
    for pres in pressure.iter().filter_map(|pres| *pres) {
        if pressure_one_level_down < pres {
            return Err(ValidationError::PressureNotDecreasingWithHeight);
        }
        pressure_one_level_down = pres;
    }

    // Check height always increases with height.
    let height = snd.get_profile(GeopotentialHeight);
    let mut height_one_level_down = snd.get_station_info()
        .elevation()
        .unwrap_or(::std::f64::MIN);
    for hght in height.iter().filter_map(|hght| *hght) {
        if height_one_level_down > hght {
            return Err(ValidationError::PressureNotDecreasingWithHeight);
        }
        height_one_level_down = hght;
    }

    Ok(())
}

fn check_temp_wet_bulb_dew_point(snd: &Sounding, ve: &mut ValidationErrors) {
    use sounding_base::Profile::{DewPoint, Temperature, WetBulb};

    let temperature = snd.get_profile(Temperature);
    let wet_bulb = snd.get_profile(WetBulb);
    let dew_point = snd.get_profile(DewPoint);

    // Check that dew point <= wet bulb <= t
    for (t, wb) in temperature.iter().zip(wet_bulb.iter()) {
        if let (Some(t), Some(wb)) = (*t, *wb) {
            if t < wb {
                ve.push_error(Err(ValidationError::TemperatureLessThanWetBulb(t, wb)));
            }
        }
    }
    for (t, dp) in temperature.iter().zip(dew_point.iter()) {
        if let (Some(t), Some(dp)) = (*t, *dp) {
            if t < dp {
                ve.push_error(Err(ValidationError::TemperatureLessThanDewPoint(t, dp)));
            }
        }
    }
    for (wb, dp) in wet_bulb.iter().zip(dew_point.iter()) {
        if let (Some(wb), Some(dp)) = (*wb, *dp) {
            if wb < dp {
                ve.push_error(Err(ValidationError::WetBulbLessThanDewPoint(wb, dp)));
            }
        }
    }
}