Skip to content

Commit

Permalink
Add conveinence parsing to noaa model source enum
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiannucci committed Apr 17, 2024
1 parent 9c880b2 commit caddfbb
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/model/noaa_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use crate::{
}, units::{UnitSystem, Unit}
};

#[derive(Debug, Clone)]
pub struct ModelDataSourceError(pub String);

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum ModelDataSource {
Expand All @@ -18,6 +21,24 @@ pub enum ModelDataSource {
NOMADS,
}

impl TryFrom<&str> for ModelDataSource {
type Error = ModelDataSourceError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
let lowered = value.to_lowercase();

if lowered.contains("aws") || lowered.contains("amazon") {
Ok(ModelDataSource::NODDAWS)
} else if lowered.contains("gcp") || lowered.contains("gcs") || lowered.contains("google") {
Ok(ModelDataSource::NODDGCP)
} else if lowered.contains("nomads") || lowered.contains("noaa") {
Ok(ModelDataSource::NOMADS)
} else {
Err(ModelDataSourceError(format!("Unknown data source: {value}")))
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ModelTimeOutputResolution {
Expand Down Expand Up @@ -295,8 +316,20 @@ pub trait NOAAModel {

#[cfg(test)]
mod test {
use crate::model::ModelDataSource;

use super::ModelTimeOutputResolution;

#[test]
fn test_model_source_parse() {
assert_eq!(ModelDataSource::try_from("NOMADS").unwrap(), ModelDataSource::NOMADS);
assert_eq!(ModelDataSource::try_from("noaa").unwrap(), ModelDataSource::NOMADS);
assert_eq!(ModelDataSource::try_from("NODDAWS").unwrap(), ModelDataSource::NODDAWS);
assert_eq!(ModelDataSource::try_from("noddgcp").unwrap(), ModelDataSource::NODDGCP);
assert_eq!(ModelDataSource::try_from("noddgcs").unwrap(), ModelDataSource::NODDGCP);
assert!(ModelDataSource::try_from("unknown").is_err());
}

#[test]
fn test_model_output_time_index_to_hour() {
assert_eq!(ModelTimeOutputResolution::Hourly.hour_for_index(140), 140);
Expand Down

0 comments on commit caddfbb

Please sign in to comment.