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
use std::path::Path;
mod upper_air_section;
mod surface_section;
mod upper_air;
mod surface;
use sounding_base::{Sounding, MissingData};
use self::surface::SurfaceData;
use self::surface_section::{SurfaceSection, SurfaceIterator};
use self::upper_air::UpperAir;
use self::upper_air_section::{UpperAirSection, UpperAirIterator};
use error::*;
pub struct BufkitFile {
    file_text: String,
}
impl BufkitFile {
    
    pub fn load(path: &Path) -> Result<BufkitFile> {
        use std::fs::File;
        use std::io::BufReader;
        use std::io::prelude::Read;
        
        let mut file = BufReader::new(File::open(path).chain_err(|| "Unable to opend file.")?);
        let mut contents = String::new();
        file.read_to_string(&mut contents).chain_err(
            || "Unable to read file.",
        )?;
        Ok(BufkitFile { file_text: contents })
    }
    
    pub fn validate(&self) -> Result<()> {
        let data = self.data().chain_err(
            || "Unable to split upper air and surface sections.",
        )?;
        data.validate().chain_err(
            || "Failed validation of file format.",
        )?;
        for sndg in &data {
            sndg.validate().chain_err(
                || "Failed data validation (not file format).",
            )?;
        }
        Ok(())
    }
    
    pub fn data(&self) -> Result<BufkitData> {
        BufkitData::new(&self.file_text)
    }
}
pub struct BufkitData<'a> {
    upper_air: UpperAirSection<'a>,
    surface: SurfaceSection<'a>,
}
impl<'a> BufkitData<'a> {
    
    pub fn validate(&self) -> Result<()> {
        self.upper_air.validate_section().chain_err(
            || "Failed upper air section.",
        )?;
        self.surface.validate_section().chain_err(
            || "Failed surface section.",
        )?;
        Ok(())
    }
    
    pub fn new(text: &str) -> Result<BufkitData> {
        let break_point = BufkitData::find_break_point(text)?;
        BufkitData::new_with_break_point(text, break_point)
    }
    fn new_with_break_point(text: &str, break_point: usize) -> Result<BufkitData> {
        Ok(BufkitData {
            upper_air: UpperAirSection::new(&text[0..break_point]),
            surface: SurfaceSection::new(&text[break_point..]).chain_err(
                || "Unable to get surface section.",
            )?,
        })
    }
    fn find_break_point(text: &str) -> Result<usize> {
        match text.find("STN YYMMDD/HHMM") {
            None => Err(Error::from(
                "Unable to find break between surface and upper air data.",
            )),
            Some(val) => Ok(val),
        }
    }
}
impl<'a> IntoIterator for &'a BufkitData<'a> {
    type Item = Sounding;
    type IntoIter = SoundingIterator<'a>;
    fn into_iter(self) -> Self::IntoIter {
        SoundingIterator {
            upper_air_it: self.upper_air.into_iter(),
            surface_it: self.surface.into_iter(),
        }
    }
}
fn combine_data(ua: &UpperAir, sd: &SurfaceData) -> Sounding {
    Sounding {
        
        num: ua.num.into(),
        valid_time: ua.valid_time.into(),
        lead_time: ua.lead_time.into(),
        lat: ua.lat.into(),
        lon: ua.lon.into(),
        elevation: ua.elevation.into(),
        
        show: ua.show.into(),
        li: ua.li.into(),
        swet: ua.swet.into(),
        kinx: ua.kinx.into(),
        lclp: ua.lclp.into(),
        pwat: ua.pwat.into(),
        totl: ua.totl.into(),
        cape: ua.cape.into(),
        lclt: ua.lclt.into(),
        cins: ua.cins.into(),
        eqlv: ua.eqlv.into(),
        lfc: ua.lfc.into(),
        brch: ua.brch.into(),
        hain: i32::MISSING.into(),
        
        pressure: ua.pressure.iter().map(|val| (*val).into()).collect(),
        temperature: ua.temperature.iter().map(|val| (*val).into()).collect(),
        wet_bulb: ua.wet_bulb.iter().map(|val| (*val).into()).collect(),
        dew_point: ua.dew_point.iter().map(|val| (*val).into()).collect(),
        theta_e: ua.theta_e.iter().map(|val| (*val).into()).collect(),
        direction: ua.direction.iter().map(|val| (*val).into()).collect(),
        speed: ua.speed.iter().map(|val| (*val).into()).collect(),
        omega: ua.omega.iter().map(|val| (*val).into()).collect(),
        height: ua.height.iter().map(|val| (*val).into()).collect(),
        cloud_fraction: ua.cloud_fraction.iter().map(|val| (*val).into()).collect(),
        
        mslp: sd.mslp.into(),
        station_pres: sd.station_pres.into(),
        low_cloud: sd.low_cloud.into(),
        mid_cloud: sd.mid_cloud.into(),
        hi_cloud: sd.hi_cloud.into(),
        uwind: sd.uwind.into(),
        vwind: sd.vwind.into(),
    }
}
pub struct SoundingIterator<'a> {
    upper_air_it: UpperAirIterator<'a>,
    surface_it: SurfaceIterator<'a>,
}
impl<'a> Iterator for SoundingIterator<'a> {
    type Item = Sounding;
    fn next(&mut self) -> Option<Self::Item> {
        let mut next_ua_opt = self.upper_air_it.next();
        if next_ua_opt.is_none() {
            return None;
        }
        let mut next_ua = next_ua_opt.unwrap();
        let mut next_sd_opt = self.surface_it.next();
        if next_sd_opt.is_none() {
            return None;
        }
        let mut next_sd = next_sd_opt.unwrap();
        loop {
            while next_sd.valid_time < next_ua.valid_time {
                next_sd_opt = self.surface_it.next();
                if next_sd_opt.is_none() {
                    return None;
                }
                next_sd = next_sd_opt.unwrap();
            }
            while next_ua.valid_time < next_sd.valid_time {
                next_ua_opt = self.upper_air_it.next();
                if next_ua_opt.is_none() {
                    return None;
                }
                next_ua = next_ua_opt.unwrap();
            }
            if next_ua.valid_time == next_sd.valid_time {
                return Some(combine_data(&next_ua, &next_sd));
            }
        }
    }
}