-
Notifications
You must be signed in to change notification settings - Fork 4
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
WIP: feat: switch from time-rs to chrono and implement non-daily timesteps #115
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3e2fe43
feat: switch from time-rs to chrono and implement non-daily timesteps
Batch21 d751aeb
fix python test + remove some unwraps
Batch21 d035095
Merge branch 'main' into chrono
Batch21 5174881
impl TryFrom<Timestepper> for ModelDomain and TimeDomain + remove unwrap
Batch21 4dd53eb
use untagged enum for model start/end attributes that allows naivedat…
Batch21 3138ca3
Merge branch 'main' into chrono
Batch21 427f432
feat: Add a PywrDuration struct
Batch21 ef3fd54
more review response
Batch21 7ac9e6f
Merge branch 'main' into chrono
Batch21 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,8 @@ use crate::scenario::ScenarioIndex; | |
use crate::state::{ParameterState, State}; | ||
use crate::timestep::Timestep; | ||
use crate::PywrError; | ||
use chrono::{Datelike, NaiveDateTime}; | ||
use std::any::Any; | ||
use time::util::days_in_year_month; | ||
use time::Date; | ||
|
||
#[derive(Copy, Clone)] | ||
pub enum MonthlyInterpDay { | ||
|
@@ -30,10 +29,20 @@ impl MonthlyProfileParameter { | |
} | ||
} | ||
|
||
fn days_in_year_month(datetime: &NaiveDateTime) -> u32 { | ||
match datetime.month() { | ||
jetuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31, | ||
4 | 6 | 9 | 11 => 30, | ||
2 if datetime.date().leap_year() => 29, | ||
2 => 28, | ||
_ => panic!("Invalid month"), | ||
} | ||
} | ||
|
||
/// Interpolate between first_value and last value based on the day of the month. The last | ||
/// value is assumed to correspond to the first day of the next month. | ||
fn interpolate_first(date: &Date, first_value: f64, last_value: f64) -> f64 { | ||
let days_in_month = days_in_year_month(date.year(), date.month()); | ||
fn interpolate_first(date: &NaiveDateTime, first_value: f64, last_value: f64) -> f64 { | ||
let days_in_month = days_in_year_month(date); | ||
|
||
if date.day() <= 1 { | ||
first_value | ||
|
@@ -46,8 +55,8 @@ fn interpolate_first(date: &Date, first_value: f64, last_value: f64) -> f64 { | |
|
||
/// Interpolate between first_value and last value based on the day of the month. The first | ||
/// value is assumed to correspond to the last day of the previous month. | ||
fn interpolate_last(date: &Date, first_value: f64, last_value: f64) -> f64 { | ||
let days_in_month = days_in_year_month(date.year(), date.month()); | ||
fn interpolate_last(date: &NaiveDateTime, first_value: f64, last_value: f64) -> f64 { | ||
let days_in_month = days_in_year_month(date); | ||
|
||
if date.day() < 1 { | ||
first_value | ||
|
@@ -76,13 +85,16 @@ impl Parameter for MonthlyProfileParameter { | |
let v = match &self.interp_day { | ||
Some(interp_day) => match interp_day { | ||
MonthlyInterpDay::First => { | ||
let first_value = self.values[timestep.date.month() as usize - 1]; | ||
let last_value = self.values[timestep.date.month().next() as usize - 1]; | ||
let next_month0 = (timestep.date.month0() + 1) % 12; | ||
let first_value = self.values[timestep.date.month0() as usize]; | ||
let last_value = self.values[next_month0 as usize]; | ||
|
||
interpolate_first(×tep.date, first_value, last_value) | ||
} | ||
MonthlyInterpDay::Last => { | ||
let first_value = self.values[timestep.date.month().previous() as usize - 1]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The time API is much nicer here. |
||
let current_month = timestep.date.month(); | ||
let last_month = if current_month == 1 { 12 } else { current_month - 1 }; | ||
let first_value = self.values[last_month as usize - 1]; | ||
let last_value = self.values[timestep.date.month() as usize - 1]; | ||
|
||
interpolate_last(×tep.date, first_value, last_value) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename this to
try_from