Skip to content

Commit

Permalink
feat(api-hydro): add inflow structure form service and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
hdinia committed Jan 31, 2024
1 parent 6f51372 commit 23a28ce
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
44 changes: 44 additions & 0 deletions antarest/study/business/areas/hydro_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
from antarest.study.storage.storage_service import StudyStorageService
from antarest.study.storage.variantstudy.model.command.update_config import UpdateConfig

INFLOW_PATH = "input/hydro/prepro/{area_id}/prepro/prepro"


class InflowStructure(FormFieldsBaseModel):
"""Represents the inflow structure form values in the hydrolic configuration."""

# NOTE: Currently, there is only one field for the inflow structure model due to the scope of hydro config requirements, it may change.
inter_monthly_correlation: float = Field(default=0.5, ge=0, le=1, description="Inter-monthly correlation value")


class ManagementOptionsFormFields(FormFieldsBaseModel):
inter_daily_breakdown: Optional[float] = Field(ge=0)
Expand Down Expand Up @@ -121,3 +130,38 @@ def set_field_values(
if len(commands) > 0:
file_study = self.storage_service.get_storage(study).get_raw(study)
execute_or_add_commands(study, file_study, commands, self.storage_service)

def get_inflowstructure(self, study: Study, area_id: str) -> InflowStructure:
"""
Retrieves inflow structure values for a specific area within a study.
Returns:
InflowStructure: The inflow structure values.
"""
# NOTE: Focusing on the single field "intermonthly-correlation" due to current model scope.
path = INFLOW_PATH.format(area_id=area_id)
file_study = self.storage_service.get_storage(study).get_raw(study)
inter_monthly_correlation = file_study.tree.get(path.split("/")).get("intermonthly-correlation", 0.5)
return InflowStructure(inter_monthly_correlation=inter_monthly_correlation)

def update_inflowstructure(self, study: Study, area_id: str, values: InflowStructure) -> None:
"""
Updates inflow structure values for a specific area within a study.
Parameters:
study: The study instance to update the inflow data for.
area_id: The area identifier to update data for.
values (InflowStructure): The new inflow structure values to be updated.
Raises:
ValueError: If the provided `values` parameter is empty or invalid.
"""
# NOTE: Updates only "intermonthly-correlation" due to current model scope.
path = INFLOW_PATH.format(area_id=area_id)
command = UpdateConfig(
target=path,
data={"intermonthly-correlation": values.inter_monthly_correlation},
command_context=self.storage_service.variant_study_service.command_factory.command_context,
)
file_study = self.storage_service.get_storage(study).get_raw(study)
execute_or_add_commands(study, file_study, [command], self.storage_service)
41 changes: 40 additions & 1 deletion antarest/study/web/study_data_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from antarest.study.business.advanced_parameters_management import AdvancedParamsFormFields
from antarest.study.business.allocation_management import AllocationFormFields, AllocationMatrix
from antarest.study.business.area_management import AreaCreationDTO, AreaInfoDTO, AreaType, AreaUI, LayerInfoDTO
from antarest.study.business.areas.hydro_management import ManagementOptionsFormFields
from antarest.study.business.areas.hydro_management import ManagementOptionsFormFields, InflowStructure
from antarest.study.business.areas.properties_management import PropertiesFormFields
from antarest.study.business.areas.renewable_management import (
RenewableClusterCreation,
Expand Down Expand Up @@ -423,6 +423,45 @@ def set_hydro_form_values(

study_service.hydro_manager.set_field_values(study, data, area_id)

@bp.get(
"/studies/{study_id}/areas/{area_id}/hydro/inflowstructure",
tags=[APITag.study_data],
summary="Get inflow structure form values",
response_model=InflowStructure,
)
def get_inflowstructure(
study_id: str,
area_id: str,
current_user: JWTUser = Depends(auth.get_current_user),
) -> InflowStructure:
logger.info(
msg=f"Getting inflow structure values for area {area_id} of study {study_id}",
extra={"user": current_user.id},
)
params = RequestParameters(user=current_user)
study = study_service.check_study_access(study_id, StudyPermissionType.READ, params)
return study_service.hydro_manager.get_inflowstructure(study, area_id)

@bp.put(
"/studies/{study_id}/areas/{area_id}/hydro/inflowstructure",
tags=[APITag.study_data],
summary="Update inflow structure form values",
response_model=InflowStructure,
)
def update_inflowstructure(
study_id: str,
area_id: str,
values: InflowStructure,
current_user: JWTUser = Depends(auth.get_current_user),
) -> InflowStructure:
logger.info(
msg=f"Updating inflow structure values for area {area_id} of study {study_id}",
extra={"user": current_user.id},
)
params = RequestParameters(user=current_user)
study = study_service.check_study_access(study_id, StudyPermissionType.READ, params)
return study_service.hydro_manager.update_inflowstructure(study, area_id, values)

@bp.put(
"/studies/{uuid}/matrix",
tags=[APITag.study_data],
Expand Down

0 comments on commit 23a28ce

Please sign in to comment.