Skip to content

Commit

Permalink
Merge branch 'main' into 14-make-install-for-dev-is-failing-to-instal…
Browse files Browse the repository at this point in the history
…l-python-modules
  • Loading branch information
nhoening committed Jul 26, 2023
2 parents aab97a5 + 8271f81 commit 79711ec
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 36 deletions.
2 changes: 0 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ repos:
hooks:
- id: flake8
name: flake8 (code linting)
language_version: python3.9
- repo: https://github.com/psf/black
rev: 22.10.0 # New version tags can be found here: https://github.com/psf/black/tags
hooks:
- id: black
name: black (code formatting)
language_version: python3.9
- repo: local
hooks:
- id: mypy
Expand Down
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
10 changes: 9 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@


def load_requirements(use_case):
"""
Loading range requirements.
Packaging should be used for installing the package into existing stacks.
We therefore read the .in file for the use case.
.txt files include the exact pins, and are useful for deployments with
exactly comparable environments.
"""
reqs = []
with open("requirements/%s.txt" % use_case, "r") as f:
with open("requirements/%s.in" % use_case, "r") as f:
reqs = [
req
for req in f.read().splitlines()
if not req.strip() == ""
and not req.strip().startswith("#")
and not req.strip().startswith("-c")
and not req.strip().startswith("--find-links")
]
return reqs

Expand Down

0 comments on commit 79711ec

Please sign in to comment.