-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from LabSid-USP/feature/155-implement-start-d…
…ate-alignment-for-input-raster-series Implement start date alignment for input raster series
- Loading branch information
Showing
6 changed files
with
122 additions
and
54 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,35 +1,65 @@ | ||
from datetime import date | ||
from datetime import date, datetime | ||
import logging | ||
from typing import Optional, Union | ||
|
||
DATE_FORMAT = "%d/%m/%Y" | ||
|
||
|
||
class SimulationPeriod: | ||
""" | ||
Represents a period of time for simulation. | ||
:param start: The start date of the simulation period. | ||
:type start: date | ||
:type start: Union[date, datetime] | ||
:param end: The end date of the simulation period. | ||
:type end: date | ||
:type end: Union[date, datetime] | ||
:param alignment: The date to align the simulation period to. If not provided, the start date is used. | ||
:type alignment: Optional[Union[date, datetime]] | ||
:raises ValueError: If the start date is not before the end date. | ||
""" | ||
|
||
def __init__(self, start: date, end: date): | ||
def __init__( | ||
self, | ||
start: Union[date, datetime], | ||
end: Union[date, datetime], | ||
alignment: Optional[Union[date, datetime]] = None, | ||
): | ||
self.logger = logging.getLogger(__name__) | ||
|
||
if start >= end: | ||
self.logger.error("Start date must be before end date.") | ||
raise ValueError("Start date must be before end date.") | ||
self.logger.error( | ||
"Start date (%s) must be before end date (%s).", | ||
start.strftime(DATE_FORMAT), | ||
end.strftime(DATE_FORMAT), | ||
) | ||
raise ValueError( | ||
f"Start date ({start.strftime(DATE_FORMAT)}) must be before end date ({end.strftime(DATE_FORMAT)})." | ||
) | ||
|
||
self.start_date = start | ||
self.end_date = end | ||
|
||
self.total_steps = ( | ||
(self.end_date.year - self.start_date.year) * 12 | ||
+ (self.end_date.month - self.start_date.month) | ||
+ 1 | ||
) | ||
self.first_step = 1 # PCRaster Dynamic Framework uses 1-based indexing | ||
self.last_step = self.total_steps | ||
if not alignment: | ||
self.logger.info("No alignment date provided. Using start date as alignment.") | ||
alignment = self.start_date | ||
|
||
if alignment > self.start_date: | ||
self.logger.error( | ||
"Alignment date (%s) is after start date (%s).", | ||
alignment.strftime(DATE_FORMAT), | ||
self.start_date.strftime(DATE_FORMAT), | ||
) | ||
raise ValueError( | ||
f"Alignment date ({alignment.strftime(DATE_FORMAT)}) must be before start date ({self.start_date.strftime(DATE_FORMAT)})." | ||
) | ||
|
||
self.first_step = (start.year - alignment.year) * 12 + (start.month - alignment.month) + 1 | ||
self.last_step = (end.year - alignment.year) * 12 + (end.month - alignment.month) + 1 | ||
|
||
self.total_steps = self.last_step - self.first_step + 1 | ||
|
||
def __str__(self) -> str: | ||
return f"{self.start_date} to {self.end_date}" |
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