Skip to content

Commit

Permalink
fix: check if param exists before creating param from timeseries input
Browse files Browse the repository at this point in the history
  • Loading branch information
Batch21 committed Mar 3, 2024
1 parent e5c6822 commit 1b5c5b1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
8 changes: 2 additions & 6 deletions pywr-schema/src/timeseries/align_and_resample.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::{cmp::Ordering, ops::Deref};

use chrono::{format, NaiveDateTime};

use polars::{prelude::*, series::ops::NullBehavior};
use pywr_core::models::ModelDomain;
use pywr_v1_schema::model;
use std::{cmp::Ordering, ops::Deref};

use crate::SchemaError;

Expand Down Expand Up @@ -47,7 +43,7 @@ pub fn align_and_resample(
.time()
.step_duration()
.whole_nanoseconds()
.expect("Nano seconds could not be extracted from model step duration") as i64;
.expect("Nano seconds could not be extracted from model step duration");

let df = match model_duration.cmp(&timeseries_duration) {
Ordering::Greater => {
Expand Down
40 changes: 27 additions & 13 deletions pywr-schema/src/timeseries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
mod align_and_resample;
mod polars_dataset;

use ndarray::{Array1, Array2};
use polars::error::PolarsError;
use ndarray::Array2;
use polars::prelude::DataType::Float64;
use polars::prelude::{DataFrame, Float64Type, IndexOrder, Schema};
use polars::prelude::{DataFrame, Float64Type, IndexOrder};
use pywr_core::models::ModelDomain;
use pywr_core::parameters::{Array1Parameter, Array2Parameter, ParameterIndex};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
use thiserror::Error;
use pywr_core::PywrError;
use std::{collections::HashMap, path::Path};

use crate::{parameters::ParameterMeta, SchemaError};

Expand Down Expand Up @@ -75,8 +71,17 @@ impl LoadedTimeseriesCollection {

let array = series.cast(&Float64)?.f64()?.to_ndarray()?.to_owned();
let name = format!("{}_{}", name, col);
let p = Array1Parameter::new(&name, array, None);
Ok(network.add_parameter(Box::new(p))?)

match network.get_parameter_index_by_name(&name) {
Ok(idx) => Ok(idx),
Err(e) => match e {
PywrError::ParameterNotFound(_) => {
let p = Array1Parameter::new(&name, array, None);
Ok(network.add_parameter(Box::new(p))?)
}
_ => Err(SchemaError::PywrCore(e)),
},
}
}

pub fn load_df(
Expand All @@ -97,9 +102,18 @@ impl LoadedTimeseriesCollection {
.ok_or(SchemaError::TimeseriesNotFound(name.to_string()))?;

let array: Array2<f64> = df.to_ndarray::<Float64Type>(IndexOrder::default()).unwrap();
let name = format!("{}_{}", name, scenario);
let p = Array2Parameter::new(&name, array, scenario_group_index, None);
Ok(network.add_parameter(Box::new(p))?)
let name = format!("timeseries.{}_{}", name, scenario);

match network.get_parameter_index_by_name(&name) {
Ok(idx) => Ok(idx),
Err(e) => match e {
PywrError::ParameterNotFound(_) => {
let p = Array2Parameter::new(&name, array, scenario_group_index, None);
Ok(network.add_parameter(Box::new(p))?)
}
_ => Err(SchemaError::PywrCore(e)),
},
}
}
}

Expand Down

0 comments on commit 1b5c5b1

Please sign in to comment.