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
use sounding_base::{Sounding, Profile};
use sounding_base::Profile::*;

/// Find the dendtritic growth zones throughout the profile. It is unusual, but possible there is
/// more than 1!
pub fn dendritic_growth_zone(snd: &Sounding, v_coord: Profile) -> Vec<(f64, f64)> {
    let mut result = Vec::with_capacity(2);

    // Dendritic snow growth zone temperature range in C
    const WARM_SIDE: f64 = -12.0;
    const COLD_SIDE: f64 = -18.0;

    let mut profile = snd.get_profile(Temperature).iter().zip(
        snd.get_profile(v_coord)
            .iter(),
    );

    let mut bottom = ::std::f64::MAX; // Only initialize because compiler can't tell value not used
    let mut top: f64;

    // Initialize the bottom of the sounding
    let mut last_t: f64;
    let mut last_coord: f64;
    loop {
        if let Some((t, coord)) = profile.by_ref().next() {
            if let (Some(t), Some(coord)) = (t.as_option(), coord.as_option()) {
                last_t = t;
                last_coord = coord;
                break;
            }
        }
    }

    // Check to see if we are already in the dendtritic zone
    if last_t < WARM_SIDE && last_t > COLD_SIDE {
        bottom = last_coord;
    }

    for (t, coord) in profile {
        if let (Some(t), Some(coord)) = (t.as_option(), coord.as_option()) {
            if last_t > WARM_SIDE && t < WARM_SIDE {
                // Just crossed into a dendritic zone
                bottom = ::interpolation::linear_interp(WARM_SIDE, last_t, t, last_coord, coord);
            }
            if last_t > COLD_SIDE && t < COLD_SIDE {
                // Just crossed out of a dendritic zone
                top = ::interpolation::linear_interp(COLD_SIDE, last_t, t, last_coord, coord);
                result.push((bottom, top));
            }
            last_t = t;
            last_coord = coord;
        }
    }

    // Check to see if we ended in a dendtritic zone
    if last_t < WARM_SIDE && last_t > COLD_SIDE {
        top = last_coord;
        result.push((bottom, top));
    }

    result
}