Skip to main content

nexrad_data/aws/archive/
identifier.rs

1use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
2
3/// Identifying metadata for a NEXRAD archive volume file.
4#[derive(Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
5pub struct Identifier(String);
6
7impl Identifier {
8    /// Constructs a new identifier from the provided name.
9    pub fn new(name: String) -> Self {
10        Identifier(name)
11    }
12
13    /// The file name.
14    pub fn name(&self) -> &str {
15        &self.0
16    }
17
18    /// The radar site this file was produced at, e.g. KDMX.
19    pub fn site(&self) -> Option<&str> {
20        self.0.get(0..4)
21    }
22
23    /// This file's data collection time.
24    pub fn date_time(&self) -> Option<DateTime<Utc>> {
25        let date_string = self.0.get(4..12)?;
26        if let Ok(date) = NaiveDate::parse_from_str(date_string, "%Y%m%d") {
27            let time_string = self.0.get(13..19)?;
28            if let Ok(time) = NaiveTime::parse_from_str(time_string, "%H%M%S") {
29                let naive_datetime = NaiveDateTime::new(date, time);
30                return Some(DateTime::from_naive_utc_and_offset(naive_datetime, Utc));
31            }
32        }
33
34        None
35    }
36}