Skip to content

Commit

Permalink
add rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdudfield committed Sep 27, 2023
1 parent 3e67a1f commit b912c3a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
13 changes: 12 additions & 1 deletion src/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from nowcasting_datamodel.models import Forecast, ForecastSQL, ForecastValue, Location, LocationSQL
from nowcasting_datamodel.models.utils import EnhancedBaseModel
from pydantic import Field
from pydantic import Field, validator

logger = logging.getLogger(__name__)

Expand All @@ -16,6 +16,11 @@ class GSPYield(EnhancedBaseModel):
datetime_utc: datetime = Field(..., description="The timestamp of the gsp yield")
solar_generation_kw: float = Field(..., description="The amount of solar generation")

@validator("solar_generation_kw")
def result_check(cls, v):
...
return round(v, 2)


class LocationWithGSPYields(Location):
"""Location object with GSPYields"""
Expand Down Expand Up @@ -171,6 +176,12 @@ class NationalForecastValue(ForecastValue):
None, description="Dictionary to hold properties of the forecast, like p_levels. "
)

@validator("expected_power_generation_megawatts")
def result_check(cls, v):
...
return round(v, 2)



class NationalForecast(Forecast):
"""One Forecast of generation at one timestamp"""
Expand Down
17 changes: 10 additions & 7 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,27 @@ def format_plevels(national_forecast_value: NationalForecastValue):
if (not isinstance(national_forecast_value.plevels, dict)) or (
national_forecast_value.plevels == {}
):
power = national_forecast_value.expected_power_generation_megawatts
national_forecast_value.plevels = {
"plevel_10": national_forecast_value.expected_power_generation_megawatts * 0.8,
"plevel_90": national_forecast_value.expected_power_generation_megawatts * 1.2,
"plevel_10": round(power * 0.8, 2),
"plevel_90": round(power * 1.2, 2),
}

# rename '10' and '90' to plevel_10 and plevel_90
for c in ["10", "90"]:
if c in national_forecast_value.plevels.keys():
national_forecast_value.plevels[f"plevel_{c}"] = national_forecast_value.plevels.pop(c)
national_forecast_value.plevels[f"plevel_{c}"] = round(
national_forecast_value.plevels.pop(c), 2
)

if national_forecast_value.plevels["plevel_10"] is None:
national_forecast_value.plevels["plevel_10"] = (
national_forecast_value.expected_power_generation_megawatts * 0.8
national_forecast_value.plevels["plevel_10"] = round(
national_forecast_value.expected_power_generation_megawatts * 0.8, 2
)

if national_forecast_value.plevels["plevel_90"] is None:
national_forecast_value.plevels["plevel_90"] = (
national_forecast_value.expected_power_generation_megawatts * 1.2
national_forecast_value.plevels["plevel_90"] = round(
national_forecast_value.expected_power_generation_megawatts * 1.2, 2
)


Expand Down

0 comments on commit b912c3a

Please sign in to comment.