Skip to content

Commit

Permalink
Forecasts can be limited to only the registered sensors (#13)
Browse files Browse the repository at this point in the history
* check on sensors

* Update OWM API version to 3 and auto version checker (#12)

* change owm version to 3 and add version checker

* rais a warning

* try black only

* make API_VERSION a module constant and add tested versions to the warning

Signed-off-by: Nicolas Höning <nicolas@seita.nl>

---------

Signed-off-by: Nicolas Höning <nicolas@seita.nl>
Co-authored-by: Nicolas Höning <nicolas@seita.nl>

* check on sensors

* refactoring

* refactoring

---------

Signed-off-by: Nicolas Höning <nicolas@seita.nl>
Co-authored-by: Nicolas Höning <nicolas@seita.nl>
  • Loading branch information
Ahmad-Wahid and nhoening committed Jul 26, 2023
1 parent 6db4cc0 commit 9645e6d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
9 changes: 5 additions & 4 deletions flexmeasures_openweathermap/utils/locating.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Tuple, List, Optional

import click
from flask import current_app

from flexmeasures.utils.grid_cells import LatLngGrid, get_cell_nums
from flexmeasures import Asset, Sensor
Expand Down Expand Up @@ -72,11 +73,11 @@ def get_locations(
return locations


def find_weather_sensor_by_location_or_fail(
def find_weather_sensor_by_location(
location: Tuple[float, float],
max_degree_difference_for_nearest_weather_sensor: int,
sensor_name: str,
) -> Sensor:
) -> Sensor | None:
"""
Try to find a weather sensor of fitting type close by.
Complain if the nearest weather sensor is further away than some minimum degrees.
Expand All @@ -96,11 +97,11 @@ def find_weather_sensor_by_location_or_fail(
location[1] - weather_station.location[1]
> max_degree_difference_for_nearest_weather_sensor
):
raise Exception(
current_app.logger.warning(
f"[FLEXMEASURES-OWM] No sufficiently close weather sensor found (within {max_degree_difference_for_nearest_weather_sensor} {flexmeasures_inflection.pluralize('degree', max_degree_difference_for_nearest_weather_sensor)} distance) for measuring {sensor_name}! We're looking for: {location}, closest available: ({weather_station.location})"
)
else:
raise Exception(
current_app.logger.warning(
"[FLEXMEASURES-OWM] No weather sensor set up yet for measuring %s. Try the register-weather-sensor CLI task."
% sensor_name
)
Expand Down
67 changes: 38 additions & 29 deletions flexmeasures_openweathermap/utils/owm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
from flexmeasures.data.models.time_series import Sensor, TimedBelief
from flexmeasures.data.utils import save_to_db

from .locating import find_weather_sensor_by_location_or_fail
from .locating import find_weather_sensor_by_location
from ..sensor_specs import mapping
from .modeling import (
get_or_create_owm_data_source,
get_or_create_owm_data_source_for_derived_data,
)
from .radiating import compute_irradiance


API_VERSION = "3.0"


Expand Down Expand Up @@ -103,8 +103,8 @@ def save_forecasts_in_db(
datetime.fromtimestamp(fc["dt"], get_timezone())
)
click.echo(f"[FLEXMEASURES-OWM] Processing forecast for {fc_datetime} ...")
data_source = get_or_create_owm_data_source()
for sensor_specs in mapping:
data_source = get_or_create_owm_data_source()
sensor_name = str(sensor_specs["fm_sensor_name"])
owm_response_label = sensor_specs["owm_sensor_name"]
if owm_response_label in fc:
Expand All @@ -114,31 +114,37 @@ def save_forecasts_in_db(
weather_sensors,
max_degree_difference_for_nearest_weather_sensor,
)
if weather_sensor not in db_forecasts.keys():
db_forecasts[weather_sensor] = []

fc_value = fc[owm_response_label]

# the irradiance is not available in OWM -> we compute it ourselves
if sensor_name == "irradiance":
fc_value = compute_irradiance(
location[0],
location[1],
fc_datetime,
# OWM sends cloud cover in percent, we need a ratio
fc_value / 100.0,
if weather_sensor is not None:
click.echo(
f"Found pre-configured weather sensor {weather_sensor.name} ..."
)
data_source = get_or_create_owm_data_source_for_derived_data()

db_forecasts[weather_sensor].append(
TimedBelief(
event_start=fc_datetime,
belief_time=now,
event_value=fc_value,
sensor=weather_sensor,
source=data_source,
if weather_sensor not in db_forecasts.keys():
db_forecasts[weather_sensor] = []

fc_value = fc[owm_response_label]

# the irradiance is not available in OWM -> we compute it ourselves
if sensor_name == "irradiance":
fc_value = compute_irradiance(
location[0],
location[1],
fc_datetime,
# OWM sends cloud cover in percent, we need a ratio
fc_value / 100.0,
)
data_source = (
get_or_create_owm_data_source_for_derived_data()
)

db_forecasts[weather_sensor].append(
TimedBelief(
event_start=fc_datetime,
belief_time=now,
event_value=fc_value,
sensor=weather_sensor,
source=data_source,
)
)
)
else:
# we will not fail here, but issue a warning
msg = "No label '%s' in response data for time %s" % (
Expand Down Expand Up @@ -170,19 +176,22 @@ def get_weather_sensor(
location: Tuple[float, float],
weather_sensors: Dict[str, Sensor],
max_degree_difference_for_nearest_weather_sensor: int,
) -> Sensor:
) -> Sensor | None:
"""Get the weather sensor for this own response label and location, if we haven't retrieved it already."""
sensor_name = str(sensor_specs["fm_sensor_name"])
if sensor_name in weather_sensors:
weather_sensor = weather_sensors[sensor_name]
else:
weather_sensor = find_weather_sensor_by_location_or_fail(
weather_sensor = find_weather_sensor_by_location(
location,
max_degree_difference_for_nearest_weather_sensor,
sensor_name=sensor_name,
)
weather_sensors[sensor_name] = weather_sensor
if weather_sensor.event_resolution != sensor_specs["event_resolution"]:
if (
weather_sensor is not None
and weather_sensor.event_resolution != sensor_specs["event_resolution"]
):
raise Exception(
f"[FLEXMEASURES-OWM] The weather sensor found for {sensor_name} has an unfitting event resolution (should be {sensor_specs['event_resolution']}, but is {weather_sensor.event_resolution}."
)
Expand Down

0 comments on commit 9645e6d

Please sign in to comment.