Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add conversions from v1 node and storage threshold parameters #319

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion pywr-schema/src/nodes/piecewise_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ impl TryFromV1<PiecewiseLinkNodeV1> 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::<Result<Vec<_>, _>>()?,
};

Expand Down
10 changes: 10 additions & 0 deletions pywr-schema/src/parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ impl TryFromV1<ParameterV1> 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(),
Expand Down
57 changes: 55 additions & 2 deletions pywr-schema/src/parameters/thresholds.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;

Expand Down Expand Up @@ -111,3 +112,55 @@ impl TryFromV1<ParameterThresholdParameterV1> for ThresholdParameter {
Ok(p)
}
}

impl TryFromV1<NodeThresholdParameterV1> for ThresholdParameter {
type Error = ComponentConversionError;

fn try_from_v1(
v1: NodeThresholdParameterV1,
parent_node: Option<&str>,
conversion_data: &mut ConversionData,
) -> Result<Self, Self::Error> {
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<StorageThresholdParameterV1> for ThresholdParameter {
type Error = ComponentConversionError;

fn try_from_v1(
v1: StorageThresholdParameterV1,
parent_node: Option<&str>,
conversion_data: &mut ConversionData,
) -> Result<Self, Self::Error> {
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)
}
}
Loading