Skip to content

Commit

Permalink
feat[DEI-118]: pass start_date as parameter from input yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
wschoonveld committed Aug 22, 2023
1 parent 5f6e70c commit c8cff61
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions decoimpact/data/api/i_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class IDatasetData(ABC):
def path(self) -> Path:
"""File path to the dataset"""

@property
@abstractmethod
def start_date(self) -> str:
"""start date to filter the dataset"""

@property
@abstractmethod
def mapping(self) -> dict[str, str]:
Expand Down
10 changes: 7 additions & 3 deletions decoimpact/data/entities/data_access_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"""

import re
from datetime import datetime
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -68,8 +69,11 @@ def read_input_dataset(self, dataset_data: IDatasetData) -> _xr.Dataset:
Returns:
_xr.Dataset: Dataset based on provided dataset_data
"""
start_time = "2014-01-01"
end_time = "2015-12-31"
# filter_start_date = "2014-01-01"
ds_start_date = dataset_data.start_date
date_format = "%d-%m-%Y"
filter_start_date = datetime.strptime(ds_start_date, date_format)
filter_end_date = "2015-12-31"

if not Path.exists(dataset_data.path):
message = f"""The file {dataset_data.path} is not found. \
Expand All @@ -96,7 +100,7 @@ def read_input_dataset(self, dataset_data: IDatasetData) -> _xr.Dataset:
# apply time filter on input dataset
try:
# dataset = dataset.where(dataset["time.year"] > 2014, drop=True)
dataset = dataset.sel(time=slice(start_time, end_time))
dataset = dataset.sel(time=slice(filter_start_date, filter_end_date))
except ValueError as exc:
msg = "ERROR: error applying time filter on dataset"
raise ValueError(msg) from exc
Expand Down
9 changes: 9 additions & 0 deletions decoimpact/data/entities/dataset_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
"""

import datetime
from pathlib import Path
from typing import Any

from tomlkit import date

from decoimpact.data.api.i_dataset import IDatasetData
from decoimpact.data.dictionary_utils import get_dict_element

Expand All @@ -30,12 +33,18 @@ def __init__(self, dataset: dict[str, Any]):
"""
super()
self._path = Path(get_dict_element("filename", dataset)).resolve()
self._start_date = str(get_dict_element("start_date", dataset, False))
self._get_mapping(dataset)

@property
def path(self) -> Path:
"""File path to the input dataset"""
return self._path

@property
def start_date(self) -> str:
"""optional start and end date to filter the dataset"""
return self._start_date

@property
def mapping(self) -> dict[str, str]:
Expand Down
2 changes: 2 additions & 0 deletions docs/tutorials/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ The model needs at least one rule under “rules” to execute.
input-data:
- dataset:
filename: <path_to_file_including_file_name_and_type>
start_date: "<start_date>"
end_date: "<end_date>"
variable_mapping:
<variable1_input_file>: "<variable1_name_in_model>"
<variable2_input_file>: "<variable2_name_in_model>"
Expand Down

0 comments on commit c8cff61

Please sign in to comment.