2024-10-03 11:05:44 +02:00
|
|
|
import hashlib
|
2024-02-16 12:57:09 +01:00
|
|
|
import json
|
2024-10-07 22:38:14 +02:00
|
|
|
import zoneinfo
|
|
|
|
from datetime import datetime, timedelta, timezone
|
2024-11-11 21:38:13 +01:00
|
|
|
from pathlib import Path
|
2024-12-11 07:21:25 +01:00
|
|
|
from typing import Union
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-02-18 15:07:20 +01:00
|
|
|
import numpy as np
|
2024-10-03 11:05:44 +02:00
|
|
|
import requests
|
2024-03-29 08:27:39 +01:00
|
|
|
|
2024-11-11 21:38:13 +01:00
|
|
|
from akkudoktoreos.config import AppConfig, SetupIncomplete
|
|
|
|
|
2024-12-11 07:21:25 +01:00
|
|
|
# Initialize logger with DEBUG level
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-12-11 07:21:25 +01:00
|
|
|
|
|
|
|
def repeat_to_shape(array: np.ndarray, target_shape: tuple[int, ...]) -> np.ndarray:
|
|
|
|
"""Expands an array to a specified shape using repetition."""
|
|
|
|
# logger.debug(f"Expanding array with shape {array.shape} to target shape {target_shape}")
|
2024-05-08 09:58:41 +02:00
|
|
|
if len(target_shape) != array.ndim:
|
2024-12-11 07:21:25 +01:00
|
|
|
error_msg = "Array and target shape must have the same number of dimensions"
|
|
|
|
# logger.debug(f"Validation did not succeed: {error_msg}")
|
|
|
|
raise ValueError(error_msg)
|
2024-05-08 09:58:41 +02:00
|
|
|
|
|
|
|
repeats = tuple(target_shape[i] // array.shape[i] for i in range(array.ndim))
|
|
|
|
expanded_array = np.tile(array, repeats)
|
2024-12-11 07:21:25 +01:00
|
|
|
# logger.debug(f"Expanded array shape: {expanded_array.shape}")
|
2024-05-08 09:58:41 +02:00
|
|
|
return expanded_array
|
|
|
|
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-02-16 12:57:09 +01:00
|
|
|
class HourlyElectricityPriceForecast:
|
2024-10-22 10:29:57 +02:00
|
|
|
def __init__(
|
2024-11-26 22:28:05 +01:00
|
|
|
self,
|
2024-12-11 07:21:25 +01:00
|
|
|
source: Union[str, Path],
|
2024-11-26 22:28:05 +01:00
|
|
|
config: AppConfig,
|
|
|
|
charges: float = 0.000228,
|
|
|
|
use_cache: bool = True,
|
2024-12-11 07:21:25 +01:00
|
|
|
) -> None:
|
|
|
|
# logger.debug("Initializing HourlyElectricityPriceForecast")
|
2024-11-11 21:38:13 +01:00
|
|
|
self.cache_dir = config.working_dir / config.directories.cache
|
|
|
|
self.use_cache = use_cache
|
2024-12-11 07:21:25 +01:00
|
|
|
self.charges = charges
|
|
|
|
self.prediction_hours = config.eos.prediction_hours
|
|
|
|
|
2024-11-11 21:38:13 +01:00
|
|
|
if not self.cache_dir.is_dir():
|
2024-12-11 07:21:25 +01:00
|
|
|
error_msg = f"Output path does not exist: {self.cache_dir}"
|
|
|
|
# logger.debug(f"Validation did not succeed: {error_msg}")
|
|
|
|
raise SetupIncomplete(error_msg)
|
2024-11-11 21:38:13 +01:00
|
|
|
|
|
|
|
self.cache_time_file = self.cache_dir / "cache_timestamp.txt"
|
2024-02-25 15:32:43 +01:00
|
|
|
self.prices = self.load_data(source)
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-12-11 07:21:25 +01:00
|
|
|
def load_data(self, source: Union[str, Path]) -> list[dict[str, Union[str, float]]]:
|
|
|
|
"""Loads data from a cache file or source, returns a list of price entries."""
|
2024-11-11 21:38:13 +01:00
|
|
|
cache_file = self.get_cache_file(source)
|
2024-12-11 07:21:25 +01:00
|
|
|
# logger.debug(f"Loading data from source: {source}, using cache file: {cache_file}")
|
|
|
|
|
|
|
|
if (
|
|
|
|
isinstance(source, str)
|
|
|
|
and self.use_cache
|
|
|
|
and cache_file.is_file()
|
|
|
|
and not self.is_cache_expired()
|
|
|
|
):
|
|
|
|
# logger.debug("Loading data from cache...")
|
|
|
|
with cache_file.open("r") as file:
|
2024-09-20 13:10:02 +02:00
|
|
|
json_data = json.load(file)
|
2024-11-11 21:38:13 +01:00
|
|
|
else:
|
2024-12-11 07:21:25 +01:00
|
|
|
# logger.debug("Fetching data from source and updating cache...")
|
|
|
|
json_data = self.fetch_and_cache_data(source, cache_file)
|
|
|
|
|
|
|
|
return json_data.get("values", [])
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-12-11 07:21:25 +01:00
|
|
|
def get_cache_file(self, source: Union[str, Path]) -> Path:
|
|
|
|
"""Generates a unique cache file path for the source URL."""
|
|
|
|
url = str(source)
|
2024-02-25 15:32:43 +01:00
|
|
|
hash_object = hashlib.sha256(url.encode())
|
|
|
|
hex_dig = hash_object.hexdigest()
|
2024-12-11 07:21:25 +01:00
|
|
|
cache_file = self.cache_dir / f"cache_{hex_dig}.json"
|
|
|
|
# logger.debug(f"Generated cache file path: {cache_file}")
|
|
|
|
return cache_file
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-11-26 22:28:05 +01:00
|
|
|
def is_cache_expired(self) -> bool:
|
2024-12-11 07:21:25 +01:00
|
|
|
"""Checks if the cache has expired based on a one-hour limit."""
|
2024-11-11 21:38:13 +01:00
|
|
|
if not self.cache_time_file.is_file():
|
2024-12-11 07:21:25 +01:00
|
|
|
# logger.debug("Cache timestamp file does not exist; cache considered expired")
|
2024-05-08 09:58:41 +02:00
|
|
|
return True
|
2024-12-11 07:21:25 +01:00
|
|
|
|
2024-11-11 21:38:13 +01:00
|
|
|
with self.cache_time_file.open("r") as file:
|
2024-05-08 09:58:41 +02:00
|
|
|
timestamp_str = file.read()
|
2024-12-11 07:21:25 +01:00
|
|
|
last_cache_time = datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S")
|
|
|
|
cache_expired = datetime.now() - last_cache_time > timedelta(hours=1)
|
|
|
|
# logger.debug(f"Cache expired: {cache_expired}")
|
|
|
|
return cache_expired
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-11-26 22:28:05 +01:00
|
|
|
def update_cache_timestamp(self) -> None:
|
2024-12-11 07:21:25 +01:00
|
|
|
"""Updates the cache timestamp to the current time."""
|
|
|
|
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
2024-11-11 21:38:13 +01:00
|
|
|
with self.cache_time_file.open("w") as file:
|
2024-12-11 07:21:25 +01:00
|
|
|
file.write(current_time)
|
|
|
|
|
|
|
|
# logger.debug(f"Updated cache timestamp to {current_time}")
|
|
|
|
|
|
|
|
def fetch_and_cache_data(self, source: Union[str, Path], cache_file: Path) -> dict:
|
|
|
|
"""Fetches data from a URL or file and caches it."""
|
|
|
|
if isinstance(source, str):
|
|
|
|
# logger.debug(f"Fetching data from URL: {source}")
|
|
|
|
response = requests.get(source)
|
|
|
|
if response.status_code != 200:
|
|
|
|
error_msg = f"Error fetching data: {response.status_code}"
|
|
|
|
# logger.debug(f"Validation did not succeed: {error_msg}")
|
|
|
|
raise Exception(error_msg)
|
|
|
|
|
|
|
|
json_data = response.json()
|
|
|
|
with cache_file.open("w") as file:
|
|
|
|
json.dump(json_data, file)
|
|
|
|
self.update_cache_timestamp()
|
|
|
|
elif source.is_file():
|
|
|
|
# logger.debug(f"Loading data from file: {source}")
|
|
|
|
with source.open("r") as file:
|
|
|
|
json_data = json.load(file)
|
|
|
|
else:
|
|
|
|
error_msg = f"Invalid input path: {source}"
|
|
|
|
# logger.debug(f"Validation did not succeed: {error_msg}")
|
|
|
|
raise ValueError(error_msg)
|
|
|
|
|
|
|
|
return json_data
|
2024-02-25 15:32:43 +01:00
|
|
|
|
2024-11-26 22:28:05 +01:00
|
|
|
def get_price_for_date(self, date_str: str) -> np.ndarray:
|
2024-12-11 07:21:25 +01:00
|
|
|
"""Retrieves all prices for a specified date, adding the previous day's last price if needed."""
|
|
|
|
# logger.debug(f"Getting prices for date: {date_str}")
|
2024-10-03 11:05:44 +02:00
|
|
|
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
|
2024-12-11 07:21:25 +01:00
|
|
|
previous_day_str = (date_obj - timedelta(days=1)).strftime("%Y-%m-%d")
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-12-11 07:21:25 +01:00
|
|
|
previous_day_prices = [
|
2024-09-20 13:10:02 +02:00
|
|
|
entry["marketpriceEurocentPerKWh"] + self.charges
|
2024-10-03 11:05:44 +02:00
|
|
|
for entry in self.prices
|
|
|
|
if previous_day_str in entry["end"]
|
2024-12-11 07:21:25 +01:00
|
|
|
]
|
|
|
|
last_price_of_previous_day = previous_day_prices[-1] if previous_day_prices else 0
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-09-20 13:10:02 +02:00
|
|
|
date_prices = [
|
|
|
|
entry["marketpriceEurocentPerKWh"] + self.charges
|
2024-10-03 11:05:44 +02:00
|
|
|
for entry in self.prices
|
|
|
|
if date_str in entry["end"]
|
2024-09-20 13:10:02 +02:00
|
|
|
]
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-12-11 07:21:25 +01:00
|
|
|
if len(date_prices) < 24:
|
2024-09-20 13:10:02 +02:00
|
|
|
date_prices.insert(0, last_price_of_previous_day)
|
2024-04-01 13:16:24 +02:00
|
|
|
|
2024-12-11 07:21:25 +01:00
|
|
|
# logger.debug(f"Retrieved {len(date_prices)} prices for date {date_str}")
|
|
|
|
return np.round(np.array(date_prices) / 100000.0, 10)
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-11-26 22:28:05 +01:00
|
|
|
def get_price_for_daterange(self, start_date_str: str, end_date_str: str) -> np.ndarray:
|
2024-12-11 07:21:25 +01:00
|
|
|
"""Retrieves all prices within a specified date range."""
|
|
|
|
# logger.debug(f"Getting prices from {start_date_str} to {end_date_str}")
|
|
|
|
start_date = (
|
|
|
|
datetime.strptime(start_date_str, "%Y-%m-%d")
|
|
|
|
.replace(tzinfo=timezone.utc)
|
|
|
|
.astimezone(zoneinfo.ZoneInfo("Europe/Berlin"))
|
|
|
|
)
|
|
|
|
end_date = (
|
|
|
|
datetime.strptime(end_date_str, "%Y-%m-%d")
|
|
|
|
.replace(tzinfo=timezone.utc)
|
|
|
|
.astimezone(zoneinfo.ZoneInfo("Europe/Berlin"))
|
|
|
|
)
|
|
|
|
|
|
|
|
price_list = []
|
2024-02-25 16:47:28 +01:00
|
|
|
|
2024-05-08 09:58:41 +02:00
|
|
|
while start_date < end_date:
|
2024-02-25 16:47:28 +01:00
|
|
|
date_str = start_date.strftime("%Y-%m-%d")
|
|
|
|
daily_prices = self.get_price_for_date(date_str)
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-09-20 13:10:02 +02:00
|
|
|
if daily_prices.size == 24:
|
2024-02-25 16:47:28 +01:00
|
|
|
price_list.extend(daily_prices)
|
|
|
|
start_date += timedelta(days=1)
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-09-20 13:10:02 +02:00
|
|
|
if self.prediction_hours > 0:
|
2024-12-11 07:21:25 +01:00
|
|
|
# logger.debug(f"Reshaping price list to match prediction hours: {self.prediction_hours}")
|
|
|
|
price_list = repeat_to_shape(np.array(price_list), (self.prediction_hours,))
|
2024-10-03 11:05:44 +02:00
|
|
|
|
2024-12-11 07:21:25 +01:00
|
|
|
# logger.debug(f"Total prices retrieved for date range: {len(price_list)}")
|
|
|
|
return np.round(np.array(price_list), 10)
|