Skip to content

Commit

Permalink
Add renewable reading + TU. Add timeserie services on AreaLocalService
Browse files Browse the repository at this point in the history
  • Loading branch information
killian-scalian committed Dec 2, 2024
1 parent 7344e80 commit b9f3099
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/antares/model/area.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ def get_reserves_matrix(self) -> pd.DataFrame:

def get_misc_gen_matrix(self) -> pd.DataFrame:
return self._area_service.get_misc_gen_matrix(self)

def get_renewable_matrix(self, renewable: RenewableCluster) -> pd.DataFrame:
return self._renewable_service.get_renewable_matrix(renewable)

def delete_thermal_clusters(self, thermal_clusters: List[ThermalCluster]) -> None:
self._area_service.delete_thermal_clusters(self, thermal_clusters)
Expand Down
14 changes: 6 additions & 8 deletions src/antares/service/local_services/area_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,13 @@ def delete_renewable_clusters(self, area: Area, renewable_clusters: List[Renewab

def delete_st_storages(self, area: Area, storages: List[STStorage]) -> None:
raise NotImplementedError

def _read_timeseries(self, ts_file_type: TimeSeriesFileType, study_path: Path, area_id: Optional[str] = None, constraint_id: Optional[str] = None) -> pd.DataFrame:
file_path = study_path / (
ts_file_type.value
if not (area_id or constraint_id)
else ts_file_type.value.format(area_id=area_id, constraint_id=constraint_id)
)

if os.path.getsize(file_path) != 0:
_time_series = df_read(file_path)
else:
Expand All @@ -335,17 +334,17 @@ def get_load_matrix(self, area: Area) -> pd.DataFrame:
return self._read_timeseries(TimeSeriesFileType.LOAD, self.config.study_path, area_id=area.id)

def get_solar_matrix(self, area: Area) -> pd.DataFrame:
raise NotImplementedError
return self._read_timeseries(TimeSeriesFileType.SOLAR, self.config.study_path, area_id=area.id)

def get_wind_matrix(self, area: Area) -> pd.DataFrame:
raise NotImplementedError
return self._read_timeseries(TimeSeriesFileType.WIND, self.config.study_path, area_id=area.id)

def get_reserves_matrix(self, area: Area) -> pd.DataFrame:
raise NotImplementedError
return self._read_timeseries(TimeSeriesFileType.RESERVES, self.config.study_path, area_id=area.id)

def get_misc_gen_matrix(self, area: Area) -> pd.DataFrame:
raise NotImplementedError

return self._read_timeseries(TimeSeriesFileType.MISC_GEN, self.config.study_path, area_id=area.id)
def read_areas(self) -> List[Area]:
local_path = self.config.local_path
areas_path = local_path / self.study_name / "input" / "areas"
Expand All @@ -362,4 +361,3 @@ def read_areas(self) -> List[Area]:
)
)
return areas

42 changes: 38 additions & 4 deletions src/antares/service/local_services/renewable_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
#
# This file is part of the Antares project.

from typing import Any, List
import os

from pathlib import Path
from typing import Any, List, Optional

import pandas as pd

from antares.config.local_configuration import LocalConfiguration
from antares.model.renewable import RenewableCluster, RenewableClusterProperties
from antares.model.renewable import RenewableCluster, RenewableClusterProperties, RenewableClusterPropertiesLocal
from antares.service.base_services import BaseRenewableService
from antares.tools.ini_tool import IniFile, IniFileTypes
from antares.tools.matrix_tool import df_read
from antares.tools.time_series_tool import TimeSeriesFileType


class RenewableLocalService(BaseRenewableService):
Expand All @@ -29,12 +35,40 @@ def update_renewable_properties(
self, renewable_cluster: RenewableCluster, properties: RenewableClusterProperties
) -> RenewableClusterProperties:
raise NotImplementedError

def _read_timeseries(self, ts_file_type: TimeSeriesFileType, study_path: Path, area_id: Optional[str] = None, renewable_id: Optional[str] = None) -> pd.DataFrame:
file_path = study_path / (
ts_file_type.value
if not (area_id or renewable_id)
else ts_file_type.value.format(area_id=area_id, renewable_id=renewable_id)
)
if os.path.getsize(file_path) != 0:
_time_series = df_read(file_path)
else:
_time_series = pd.DataFrame()

return _time_series

def get_renewable_matrix(
self,
renewable: RenewableCluster,
) -> pd.DataFrame:
raise NotImplementedError
return self._read_timeseries(TimeSeriesFileType.RENEWABLE_DATA_SERIES, self.config.study_path, area_id=renewable.area_id, renewable_id=renewable.id)


def read_renewables(self, area_id: str) -> List[RenewableCluster]:
raise NotImplementedError
renewable_dict = IniFile(self.config.study_path, IniFileTypes.RENEWABLES_LIST_INI, area_name=area_id).ini_dict
renewables_clusters = []
if renewable_dict:
for renewable_cluster in renewable_dict:
renewable_properties = RenewableClusterPropertiesLocal(
group=renewable_dict[renewable_cluster]["group"],
renewable_name=renewable_dict[renewable_cluster]["name"],
enabled=renewable_dict[renewable_cluster]["enabled"],
unit_count=renewable_dict[renewable_cluster]["unitcount"],
nominal_capacity=renewable_dict[renewable_cluster]["nominalcapacity"],
ts_interpretation=renewable_dict[renewable_cluster]["ts-interpretation"],
)
renewables_clusters.append(RenewableCluster(renewable_service=self, area_id=area_id, name=renewable_dict[renewable_cluster]["name"], properties=renewable_properties.yield_renewable_cluster_properties()))
return renewables_clusters

1 change: 1 addition & 0 deletions src/antares/tools/time_series_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class TimeSeriesFileType(Enum):
WIND_DATA = "input/wind/prepro/{area_id}/data.txt"
WIND_K = "input/wind/prepro/{area_id}/k.txt"
WIND_TRANSLATION = "input/wind/prepro/{area_id}/translation.txt"
RENEWABLE_DATA_SERIES = "input/renewables/series/{area_id}/{renewable_id}/series.txt"


class TimeSeries:
Expand Down
56 changes: 49 additions & 7 deletions tests/antares/services/local_services/test_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import pytest

import os
import typing as t

from configparser import ConfigParser
Expand Down Expand Up @@ -1116,15 +1117,16 @@ def test_read_areas_local(self, local_study_w_areas):
assert area.id in expected_areas


def _write_file(_file_path, _time_series) -> None:
_file_path.parent.mkdir(parents=True, exist_ok=True)
_time_series.to_csv(_file_path, sep="\t", header=False, index=False, encoding="utf-8")

class TestReadLoad:
def test_read_load_local(self, local_study_w_areas):
study_path = local_study_w_areas.service.config.study_path
local_study_object = read_study_local(study_path)
areas = local_study_object.read_areas()

def _write_file(_file_path, _time_series) -> None:
_file_path.parent.mkdir(parents=True, exist_ok=True)
_time_series.to_csv(file_path, sep="\t", header=False, index=False, encoding="utf-8")

for area in areas:
expected_time_serie = pd.DataFrame(
Expand All @@ -1137,12 +1139,52 @@ def _write_file(_file_path, _time_series) -> None:
file_path = study_path / "input" / "load" / "series" / f"load_{area.id}.txt"
_write_file(file_path, expected_time_serie)

load_object = area.get_load_matrix()
pd.testing.assert_frame_equal(load_object.astype(str), expected_time_serie.astype(str), check_dtype=False)
matrix = area.get_load_matrix()
pd.testing.assert_frame_equal(matrix.astype(str), expected_time_serie.astype(str), check_dtype=False)

expected_time_serie = pd.DataFrame([])
for area in areas:
file_path = study_path / "input" / "load" / "series" / f"load_{area.id}.txt"
_write_file(file_path, expected_time_serie)
load_object = area.get_load_matrix()
pd.testing.assert_frame_equal(load_object, expected_time_serie)
matrix = area.get_load_matrix()
pd.testing.assert_frame_equal(matrix, expected_time_serie)


class TestReadRenewable:
def test_read_renewable_local(self, local_study_with_renewable):
study_path = local_study_with_renewable.service.config.study_path
local_study_object = read_study_local(study_path)
areas = local_study_object.read_areas()

expected_time_serie = pd.DataFrame(
[
[-9999999980506447872, 0, 9999999980506447872],
[0, area.id, 0],
]
, dtype="object")

for area in areas:
renewable_list = area.read_renewables(area.id)

if renewable_list:
assert area.id == "fr"
assert isinstance(renewable_list, list)
assert isinstance(renewable_list[0], RenewableCluster)

for renewable in renewable_list:
assert renewable.name == "renewable cluster"
assert renewable.properties.unit_count == 1
assert renewable.properties.ts_interpretation.value == "power-generation"
assert renewable.properties.nominal_capacity == 0.000000
assert renewable.properties.enabled
assert renewable.properties.group.value == "Other RES 1"

# Create folder and file for timeserie.
cluster_path = study_path / "input" / "renewables" / "series"/ Path(area.id) / Path(renewable.id)
os.makedirs(cluster_path, exist_ok=True)
series_path = cluster_path / 'series.txt'
_write_file(series_path, expected_time_serie)

# Check matrix
matrix = area.get_renewable_matrix(renewable)
pd.testing.assert_frame_equal(matrix.astype(str), expected_time_serie.astype(str), check_dtype=False)

0 comments on commit b9f3099

Please sign in to comment.