From 7d98edebab59493417d059feb30944f6d454d773 Mon Sep 17 00:00:00 2001 From: James Batchelor Date: Sat, 21 Dec 2024 09:02:43 +0000 Subject: [PATCH] feat: add conversions from v1 node and storage threshold parameters --- Cargo.lock | 10 ++--- Cargo.toml | 2 +- pywr-schema/src/nodes/piecewise_link.rs | 7 ++- pywr-schema/src/parameters/mod.rs | 10 +++++ pywr-schema/src/parameters/thresholds.rs | 57 +++++++++++++++++++++++- 5 files changed, 77 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2a349c0..b056e2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2689,9 +2689,9 @@ dependencies = [ [[package]] name = "pywr-v1-schema" -version = "0.15.3" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "018056c5ca206e3cecad0b88d2718942c8032d52f92cf3d346e00f10d63d1899" +checksum = "57921fc52ff4ab988f6dc32b0a73673499028a3b457f82d3c638002f7dd10c68" dependencies = [ "chrono", "pywr-v1-schema-macros", @@ -2699,14 +2699,14 @@ dependencies = [ "serde_json", "strum", "strum_macros", - "thiserror 1.0.69", + "thiserror 2.0.7", ] [[package]] name = "pywr-v1-schema-macros" -version = "0.15.1" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ba8dff91780fd75b609a70bae1bc08d1d33c22a1ecc4c851dcd621b8a6e3a495" +checksum = "fe86b1c85abc9ca4f163bc9a52ef813520de0aeb1ceb3bf216fdc78cbb93b148" dependencies = [ "quote", "syn 2.0.90", diff --git a/Cargo.toml b/Cargo.toml index 3ac395d..c3cd58e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -54,7 +54,7 @@ pyo3-log = "0.11" tracing = { version = "0.1", features = ["log"] } csv = "1.3" hdf5-metno = { version = "0.9", features = ["static", "zlib"] } -pywr-v1-schema = "0.15" +pywr-v1-schema = "0.17" chrono = { version = "0.4", features = ["serde"] } schemars = { version = "0.8", features = ["chrono"] } rand = "0.8" diff --git a/pywr-schema/src/nodes/piecewise_link.rs b/pywr-schema/src/nodes/piecewise_link.rs index c9735af..50c68df 100644 --- a/pywr-schema/src/nodes/piecewise_link.rs +++ b/pywr-schema/src/nodes/piecewise_link.rs @@ -194,7 +194,12 @@ impl TryFromV1 for PiecewiseLinkNode { None => vec![None; v1.nsteps], Some(v1_max_flows) => v1_max_flows .into_iter() - .map(|v| try_convert_node_attr(&meta.name, "max_flows", v, parent_node, conversion_data).map(Some)) + .map(|v| match v { + None => Ok(None), + Some(v) => { + try_convert_node_attr(&meta.name, "max_flows", v, parent_node, conversion_data).map(Some) + } + }) .collect::, _>>()?, }; diff --git a/pywr-schema/src/parameters/mod.rs b/pywr-schema/src/parameters/mod.rs index 23131ce..debd239 100644 --- a/pywr-schema/src/parameters/mod.rs +++ b/pywr-schema/src/parameters/mod.rs @@ -425,6 +425,16 @@ impl TryFromV1 for ParameterOrTimeseriesRef { CoreParameter::ParameterThreshold(p) => { Parameter::Threshold(p.try_into_v2(parent_node, conversion_data)?).into() } + CoreParameter::NodeThreshold(p) => { + Parameter::Threshold(p.try_into_v2(parent_node, conversion_data)?).into() + } + CoreParameter::StorageThreshold(p) => { + Parameter::Threshold(p.try_into_v2(parent_node, conversion_data)?).into() + } + CoreParameter::MultipleThresholdIndex(_) => todo!(), + CoreParameter::MultipleThresholdParameterIndex(_) => todo!(), + CoreParameter::CurrentYearThreshold(_) => todo!(), + CoreParameter::CurrentOrdinalDayThreshold(_) => todo!(), CoreParameter::TablesArray(p) => Parameter::TablesArray(p.into_v2(parent_node, conversion_data)).into(), CoreParameter::Min(p) => Parameter::Min(p.try_into_v2(parent_node, conversion_data)?).into(), CoreParameter::Division(p) => Parameter::Division(p.try_into_v2(parent_node, conversion_data)?).into(), diff --git a/pywr-schema/src/parameters/thresholds.rs b/pywr-schema/src/parameters/thresholds.rs index 8ad8be0..b138567 100644 --- a/pywr-schema/src/parameters/thresholds.rs +++ b/pywr-schema/src/parameters/thresholds.rs @@ -1,7 +1,7 @@ use crate::error::ComponentConversionError; #[cfg(feature = "core")] use crate::error::SchemaError; -use crate::metric::Metric; +use crate::metric::{Metric, NodeReference}; #[cfg(feature = "core")] use crate::model::LoadArgs; use crate::parameters::{ConversionData, ParameterMeta}; @@ -10,7 +10,8 @@ use crate::v1::{try_convert_parameter_attr, IntoV2, TryFromV1}; use pywr_core::parameters::ParameterIndex; use pywr_schema_macros::PywrVisitAll; use pywr_v1_schema::parameters::{ - ParameterThresholdParameter as ParameterThresholdParameterV1, Predicate as PredicateV1, + NodeThresholdParameter as NodeThresholdParameterV1, ParameterThresholdParameter as ParameterThresholdParameterV1, + Predicate as PredicateV1, StorageThresholdParameter as StorageThresholdParameterV1, }; use schemars::JsonSchema; @@ -111,3 +112,55 @@ impl TryFromV1 for ThresholdParameter { Ok(p) } } + +impl TryFromV1 for ThresholdParameter { + type Error = ComponentConversionError; + + fn try_from_v1( + v1: NodeThresholdParameterV1, + parent_node: Option<&str>, + conversion_data: &mut ConversionData, + ) -> Result { + let meta: ParameterMeta = v1.meta.into_v2(parent_node, conversion_data); + + let value = Metric::Node(NodeReference::new(v1.node, None)); + + let threshold = + try_convert_parameter_attr(&meta.name, "threshold", v1.threshold, parent_node, conversion_data)?; + + let p = Self { + meta, + value, + threshold, + predicate: v1.predicate.into(), + ratchet: false, + }; + Ok(p) + } +} + +impl TryFromV1 for ThresholdParameter { + type Error = ComponentConversionError; + + fn try_from_v1( + v1: StorageThresholdParameterV1, + parent_node: Option<&str>, + conversion_data: &mut ConversionData, + ) -> Result { + let meta: ParameterMeta = v1.meta.into_v2(parent_node, conversion_data); + + let value = Metric::Node(NodeReference::new(v1.storage_node, None)); + + let threshold = + try_convert_parameter_attr(&meta.name, "threshold", v1.threshold, parent_node, conversion_data)?; + + let p = Self { + meta, + value, + threshold, + predicate: v1.predicate.into(), + ratchet: false, + }; + Ok(p) + } +}