Skip to content

Commit

Permalink
Sync with deps
Browse files Browse the repository at this point in the history
  • Loading branch information
mpiannucci committed Jul 3, 2024
1 parent caddfbb commit 7e3dee6
Show file tree
Hide file tree
Showing 8 changed files with 581 additions and 599 deletions.
882 changes: 435 additions & 447 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions src/data/grib_index_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ impl ParseableDataRecord for GribIndexRecord {
let index: usize = row[0].parse().map_err(DataRecordParsingError::from)?;
let offset: usize = row[1].parse().map_err(DataRecordParsingError::from)?;
// HACK: Postfix with 00s to add minute stamp to allow NaiveDateTime to parse it
let raw_date_string = format!("{raw}00", raw=row[2]);
let raw_date_string = format!("{raw}00", raw = row[2]);
let var = row[3].to_string();
let level = row[4].to_string();
let valid = row[5].to_string();

const DATE_FORMAT: &'static str = "d=%Y%m%d%H%M";
let reference_date = NaiveDateTime::parse_from_str(&raw_date_string, DATE_FORMAT)
.map_err(DataRecordParsingError::from)?;
let reference_date = DateTime::<Utc>::from_utc(reference_date, Utc);
let reference_date = DateTime::<Utc>::from_naive_utc_and_offset(reference_date, Utc);

Ok(GribIndexRecord {
index,
Expand Down Expand Up @@ -94,12 +94,15 @@ mod tests {
let data_row: Vec<&str> = raw_data.split(":").collect();
let grib_index = GribIndexRecord::from_data_row(None, &data_row).unwrap();

let ref_date = Utc.with_ymd_and_hms(2023, 05, 15, 12, 0, 0).single().unwrap();
let ref_date = Utc
.with_ymd_and_hms(2023, 05, 15, 12, 0, 0)
.single()
.unwrap();
assert_eq!(grib_index.index, 8);
assert_eq!(grib_index.offset, 2540945);
assert_eq!(grib_index.reference_date, ref_date);
assert_eq!(grib_index.var, "REFD");
assert_eq!(grib_index.level, "2 hybrid level");
assert_eq!(grib_index.valid, "34 hour fcst");
}
}
}
215 changes: 103 additions & 112 deletions src/data/meteorological_data_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,24 +163,15 @@ impl From<MeteorologicalDataRecord> for HashMap<String, DimensionalData<f64>> {
value: Some(m.date.timestamp() as f64),
variable_name: "epoch".to_string(),
unit: Unit::Seconds,
}
),
},
),
(
m.wind_direction.variable_name.clone(),
m.wind_direction.into(),
),
(
m.wind_speed.variable_name.clone(),
m.wind_speed,
),
(
m.wind_gust_speed.variable_name.clone(),
m.wind_gust_speed,
),
(
m.wave_height.variable_name.clone(),
m.wave_height,
),
(m.wind_speed.variable_name.clone(), m.wind_speed),
(m.wind_gust_speed.variable_name.clone(), m.wind_gust_speed),
(m.wave_height.variable_name.clone(), m.wave_height),
(
m.dominant_wave_period.variable_name.clone(),
m.dominant_wave_period,
Expand All @@ -193,14 +184,8 @@ impl From<MeteorologicalDataRecord> for HashMap<String, DimensionalData<f64>> {
m.mean_wave_direction.variable_name.clone(),
m.mean_wave_direction.into(),
),
(
m.air_pressure.variable_name.clone(),
m.air_pressure,
),
(
m.air_temperature.variable_name.clone(),
m.air_temperature,
),
(m.air_pressure.variable_name.clone(), m.air_pressure),
(m.air_temperature.variable_name.clone(), m.air_temperature),
(
m.water_temperature.variable_name.clone(),
m.water_temperature,
Expand All @@ -209,10 +194,7 @@ impl From<MeteorologicalDataRecord> for HashMap<String, DimensionalData<f64>> {
m.dewpoint_temperature.variable_name.clone(),
m.dewpoint_temperature,
),
(
m.visibility.variable_name.clone(),
m.visibility,
),
(m.visibility.variable_name.clone(), m.visibility),
(m.tide.variable_name.clone(), m.tide),
])
}
Expand Down Expand Up @@ -315,22 +297,20 @@ impl<'a> MeteorologicalDataRecordCollection<'a> {

pub struct StdmetDataRecordCollection<'a> {
dataset: DodsDataset<'a>,
dates: Vec<i64>
dates: Vec<i64>,
}

impl<'a> StdmetDataRecordCollection<'a> {
pub fn from_data(data: &'a [u8]) -> Self {
let dataset = DodsDataset::from_bytes(&data).unwrap();
let coords = dataset.variable_coords("gust").unwrap();
let dates: Vec<i64> =coords[0].1.clone().try_into().unwrap();
let dates: Vec<i64> = coords[0].1.clone().try_into().unwrap();

Self { dataset, dates }
}

pub fn records(&'a self) -> impl Iterator<Item = MeteorologicalDataRecord> + 'a {
let dates = self.dates
.iter()
.map(|t| -> DateTime<Utc> {DateTime::from_utc(NaiveDateTime::from_timestamp_opt(*t, 0).unwrap(), Utc)});
let dates = self.dates.iter().map(|t| DateTime::from_timestamp(*t, 0).unwrap());
let wind_dir = self.dataset.variable_data_iter("wind_dir").unwrap();
let wind_spd = self.dataset.variable_data_iter("wind_spd").unwrap();
let gust = self.dataset.variable_data_iter("gust").unwrap();
Expand All @@ -340,8 +320,14 @@ impl<'a> StdmetDataRecordCollection<'a> {
let mean_wave_dir = self.dataset.variable_data_iter("mean_wave_dir").unwrap();
let air_pressure = self.dataset.variable_data_iter("air_pressure").unwrap();
let air_temperature = self.dataset.variable_data_iter("air_temperature").unwrap();
let sea_surface_temperature = self.dataset.variable_data_iter("sea_surface_temperature").unwrap();
let dewpt_temperature = self.dataset.variable_data_iter("dewpt_temperature").unwrap();
let sea_surface_temperature = self
.dataset
.variable_data_iter("sea_surface_temperature")
.unwrap();
let dewpt_temperature = self
.dataset
.variable_data_iter("dewpt_temperature")
.unwrap();
let visibility = self.dataset.variable_data_iter("visibility").unwrap();
let water_level = self.dataset.variable_data_iter("water_level").unwrap();

Expand All @@ -361,88 +347,93 @@ impl<'a> StdmetDataRecordCollection<'a> {
air_pressure,
air_temperature,
sea_surface_temperature,
dewpt_temperature,
dewpt_temperature,
visibility,
water_level,
).map(|(d, wdir, wspd, gust, wheight, dom, avg, mwd, press, temp, sst, dewpt, vis, wl)| MeteorologicalDataRecord {
date: d,
wind_direction: DimensionalData {
value: match is_some_missing(wdir.try_into().unwrap(), MISSING) {
Some(v) => Some(Direction::from_degrees(v as i32)),
None => None,
},
variable_name: "wind direction".into(),
unit: Unit::Degrees,
},
wind_speed: DimensionalData {
value: is_some_missing(wspd.try_into().unwrap(), MISSING_SMALL),
variable_name: "wind speed".into(),
unit: Unit::MetersPerSecond,
},
wind_gust_speed: DimensionalData {
value: is_some_missing(gust.try_into().unwrap(), MISSING_SMALL),
variable_name: "wind gust".into(),
unit: Unit::MetersPerSecond,
},
wave_height: DimensionalData {
value: is_some_missing(wheight.try_into().unwrap(), MISSING_SMALL),
variable_name: "wave height".into(),
unit: Unit::Meters,
},
dominant_wave_period: DimensionalData {
value: is_some_missing(dom.try_into().unwrap(), MISSING_SMALL),
variable_name: "dominant wave period".into(),
unit: Unit::Seconds,
},
average_wave_period: DimensionalData {
value: is_some_missing(avg.try_into().unwrap(), MISSING_SMALL),
variable_name: "mean wave period".into(),
unit: Unit::Seconds,
},
mean_wave_direction: DimensionalData {
value: match is_some_missing(mwd.try_into().unwrap(), MISSING) {
Some(v) => Some(Direction::from_degrees(v as i32)),
None => None,
},
variable_name: "mean wave direction".into(),
unit: Unit::Degrees,
},
air_pressure: DimensionalData {
value: is_some_missing(press.try_into().unwrap(), MISSING_LARGE),
variable_name: "air pressure".into(),
unit: Unit::HectaPascal,
},
air_pressure_tendency: DimensionalData {
value: None,
variable_name: "air pressure tendency".into(),
unit: Unit::HectaPascal,
},
air_temperature: DimensionalData {
value: is_some_missing(temp.try_into().unwrap(), MISSING),
variable_name: "air temperature".into(),
unit: Unit::Celsius,
},
water_temperature: DimensionalData {
value: is_some_missing(sst.try_into().unwrap(), MISSING),
variable_name: "water temperature".into(),
unit: Unit::Celsius,
},
dewpoint_temperature: DimensionalData {
value: is_some_missing(dewpt.try_into().unwrap(), MISSING),
variable_name: "dewpoint temperature".into(),
unit: Unit::Celsius,
},
visibility: DimensionalData {
value: is_some_missing(vis.try_into().unwrap(), MISSING_SMALL),
variable_name: "visibility".into(),
unit: Unit::NauticalMiles,
},
tide: DimensionalData {
value: is_some_missing(wl.try_into().unwrap(), MISSING_SMALL),
variable_name: "water level".into(),
unit: Unit::Feet,
)
.map(
|(d, wdir, wspd, gust, wheight, dom, avg, mwd, press, temp, sst, dewpt, vis, wl)| {
MeteorologicalDataRecord {
date: d,
wind_direction: DimensionalData {
value: match is_some_missing(wdir.try_into().unwrap(), MISSING) {
Some(v) => Some(Direction::from_degrees(v as i32)),
None => None,
},
variable_name: "wind direction".into(),
unit: Unit::Degrees,
},
wind_speed: DimensionalData {
value: is_some_missing(wspd.try_into().unwrap(), MISSING_SMALL),
variable_name: "wind speed".into(),
unit: Unit::MetersPerSecond,
},
wind_gust_speed: DimensionalData {
value: is_some_missing(gust.try_into().unwrap(), MISSING_SMALL),
variable_name: "wind gust".into(),
unit: Unit::MetersPerSecond,
},
wave_height: DimensionalData {
value: is_some_missing(wheight.try_into().unwrap(), MISSING_SMALL),
variable_name: "wave height".into(),
unit: Unit::Meters,
},
dominant_wave_period: DimensionalData {
value: is_some_missing(dom.try_into().unwrap(), MISSING_SMALL),
variable_name: "dominant wave period".into(),
unit: Unit::Seconds,
},
average_wave_period: DimensionalData {
value: is_some_missing(avg.try_into().unwrap(), MISSING_SMALL),
variable_name: "mean wave period".into(),
unit: Unit::Seconds,
},
mean_wave_direction: DimensionalData {
value: match is_some_missing(mwd.try_into().unwrap(), MISSING) {
Some(v) => Some(Direction::from_degrees(v as i32)),
None => None,
},
variable_name: "mean wave direction".into(),
unit: Unit::Degrees,
},
air_pressure: DimensionalData {
value: is_some_missing(press.try_into().unwrap(), MISSING_LARGE),
variable_name: "air pressure".into(),
unit: Unit::HectaPascal,
},
air_pressure_tendency: DimensionalData {
value: None,
variable_name: "air pressure tendency".into(),
unit: Unit::HectaPascal,
},
air_temperature: DimensionalData {
value: is_some_missing(temp.try_into().unwrap(), MISSING),
variable_name: "air temperature".into(),
unit: Unit::Celsius,
},
water_temperature: DimensionalData {
value: is_some_missing(sst.try_into().unwrap(), MISSING),
variable_name: "water temperature".into(),
unit: Unit::Celsius,
},
dewpoint_temperature: DimensionalData {
value: is_some_missing(dewpt.try_into().unwrap(), MISSING),
variable_name: "dewpoint temperature".into(),
unit: Unit::Celsius,
},
visibility: DimensionalData {
value: is_some_missing(vis.try_into().unwrap(), MISSING_SMALL),
variable_name: "visibility".into(),
unit: Unit::NauticalMiles,
},
tide: DimensionalData {
value: is_some_missing(wl.try_into().unwrap(), MISSING_SMALL),
variable_name: "water level".into(),
unit: Unit::Feet,
},
}
},
})
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/data/swden_wave_data_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'a> SwdenWaveDataRecordIterator<'a> {
let dates: Vec<i64> = coords[0].1.clone().try_into().unwrap();
let dates = dates
.iter()
.map(|t| DateTime::from_utc(NaiveDateTime::from_timestamp_opt(*t, 0).unwrap(), Utc))
.map(|t| DateTime::from_timestamp(*t, 0).unwrap())
.collect::<Vec<DateTime<Utc>>>();
let energy_spectra = dataset.variable_data_iter("spectral_wave_density").unwrap();
let mean_wave_direction = dataset.variable_data_iter("mean_wave_dir").unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/data/tidal_data_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use serde_json::Value;
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum TidalEvent {
#[serde(rename = "H")]
High,
High,
#[serde(rename = "L")]
Low,
Low,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -29,7 +29,7 @@ where
{
let s: String = Deserialize::deserialize(deserializer)?;
let naive = NaiveDateTime::parse_from_str(&s, "%Y-%m-%d %H:%M").map_err(de::Error::custom)?;
Ok(DateTime::<Utc>::from_utc(naive, Utc))
Ok(DateTime::<Utc>::from_naive_utc_and_offset(naive, Utc))
}

fn tidal_value_f64<'de, D: Deserializer<'de>>(deserializer: D) -> Result<f64, D::Error> {
Expand Down Expand Up @@ -84,4 +84,4 @@ mod tests {
assert!(hilo_data.event.is_some());
assert_eq!(hilo_data.event, Some(TidalEvent::High));
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub mod swell;
pub mod tide_station;
pub mod tools;
pub mod units;
pub mod weather;
pub mod weather;
Loading

0 comments on commit 7e3dee6

Please sign in to comment.