Skip to content

Commit

Permalink
Merge pull request #871 from gridsingularity/bug/D3ASIM-2825
Browse files Browse the repository at this point in the history
Bug/d3 asim 2825
  • Loading branch information
spyrostz authored Sep 15, 2020
2 parents ee07247 + 20653a4 commit 91094ed
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/d3a/models/read_user_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pendulum import duration, from_format, from_timestamp, today, DateTime
from typing import Dict
from d3a.constants import TIME_FORMAT, DATE_TIME_FORMAT, TIME_ZONE
from d3a_interface.constants_limits import GlobalConfig
from d3a_interface.constants_limits import GlobalConfig, DATE_TIME_FORMAT_SECONDS
from d3a.d3a_core.util import generate_market_slot_list

"""
Expand All @@ -42,7 +42,7 @@ def _str_to_datetime(time_str, time_format) -> DateTime:
:return: DateTime
"""
time = from_format(time_str, time_format, tz=TIME_ZONE)
if time_format == DATE_TIME_FORMAT:
if time_format == DATE_TIME_FORMAT or time_format == DATE_TIME_FORMAT_SECONDS:
return time
elif time_format == TIME_FORMAT:
return GlobalConfig.start_date.add(
Expand Down Expand Up @@ -99,7 +99,7 @@ def _eval_time_format(time_dict: Dict) -> str:
"""
Evaluates which time format the provided dictionary has, also checks if the time-format is
consistent for each time_slot
:return: TIME_FORMAT or DATE_TIME_FORMAT
:return: TIME_FORMAT or DATE_TIME_FORMAT or DATE_TIME_FORMAT_SECONDS
"""
try:
[from_format(str(ti), TIME_FORMAT) for ti in time_dict.keys()]
Expand All @@ -109,8 +109,12 @@ def _eval_time_format(time_dict: Dict) -> str:
[from_format(str(ti), DATE_TIME_FORMAT) for ti in time_dict.keys()]
return DATE_TIME_FORMAT
except ValueError:
raise Exception(f"Format of time-stamp is not one of ('{TIME_FORMAT}', "
f"'{DATE_TIME_FORMAT}')")
try:
[from_format(str(ti), DATE_TIME_FORMAT_SECONDS) for ti in time_dict.keys()]
return DATE_TIME_FORMAT_SECONDS
except ValueError:
raise Exception(f"Format of time-stamp is not one of ('{TIME_FORMAT}', "
f"'{DATE_TIME_FORMAT}', '{DATE_TIME_FORMAT_SECONDS}')")


def _readCSV(path: str) -> Dict:
Expand Down
7 changes: 7 additions & 0 deletions src/d3a/resources/datetime_seconds_profile.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
INTERVAL;REALISTICPOWER(W)
2019-03-02T00:00:00;6000
2019-03-02T00:15:00;5000
2019-03-02T00:30:00;4000
2019-03-02T00:45:00;3000
2019-03-02T01:00:00;2000
2019-03-02T01:15:00;1000
18 changes: 16 additions & 2 deletions tests/test_strategy_pvpredefined.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import uuid
import pathlib
import os
from pendulum import DateTime, duration, today
from pendulum import DateTime, duration, today, instance
from datetime import datetime, date
from typing import Dict # NOQA

from d3a.d3a_core.util import d3a_path, change_global_config
from d3a.constants import TIME_ZONE, TIME_FORMAT
Expand All @@ -36,7 +38,7 @@ def setup_function():
change_global_config(**DEFAULT_CONFIG.__dict__)


ENERGY_FORECAST = {} # type: Dict[Time, float]
ENERGY_FORECAST = {} # type: Dict[datetime, float]
TIME = pendulum.today(tz=TIME_ZONE).at(hour=10, minute=45, second=0)


Expand Down Expand Up @@ -384,3 +386,15 @@ def test_pv_user_profile_constructor_rejects_incorrect_parameters():
with pytest.raises(D3ADeviceException):
PVUserProfileStrategy(power_profile=user_profile_path,
fit_to_limit=False, energy_rate_decrease_per_update=-1)


def test_profile_with_date_and_seconds_can_be_parsed():
GlobalConfig.slot_length = duration(minutes=15)
profile_date = date(year=2019, month=3, day=2)
GlobalConfig.start_date = instance((datetime.combine(profile_date, datetime.min.time())))
profile_path = pathlib.Path(d3a_path + '/resources/datetime_seconds_profile.csv')
profile = read_arbitrary_profile(InputProfileTypes.POWER, str(profile_path))
# After the 6th element the rest of the entries are populated with the last value
assert list(profile.values())[:6] == [1.5, 1.25, 1.0, 0.75, 0.5, 0.25]
assert all(x == 0.25 for x in list(profile.values())[6:])
GlobalConfig.start_date = instance((datetime.combine(date.today(), datetime.min.time())))

0 comments on commit 91094ed

Please sign in to comment.