Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get forecasts for an asset-id instead of passing location #16

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6f5a55f
add asset-id to get forecasts, used GenericAsset(type) instead of Ass…
Ahmad-Wahid Jul 22, 2023
acf1e03
refactoring
Ahmad-Wahid Jul 24, 2023
9080a0a
refactoring
Ahmad-Wahid Jul 25, 2023
f9448ce
remove extra brackets
Ahmad-Wahid Jul 25, 2023
abea5df
add both assets name in the exception message
Ahmad-Wahid Jul 25, 2023
ddfc405
add asset-id to get forecasts, used GenericAsset(type) instead of Ass…
Ahmad-Wahid Jul 22, 2023
adb193b
refactoring
Ahmad-Wahid Jul 24, 2023
caae0c1
refactoring
Ahmad-Wahid Jul 25, 2023
06d26c1
remove extra brackets
Ahmad-Wahid Jul 25, 2023
767f4c1
add both assets name in the exception message
Ahmad-Wahid Jul 25, 2023
59ef4e0
clear exception message
Ahmad-Wahid Jul 26, 2023
1fc3691
Merge remote-tracking branch 'origin/2-allow-to-pass-asset-id-andor-n…
Ahmad-Wahid Jul 26, 2023
dbc475b
Merge remote-tracking branch 'origin/main' into 2-allow-to-pass-asset…
Ahmad-Wahid Jul 26, 2023
8465c17
removed unused package
Ahmad-Wahid Jul 26, 2023
3022ffd
add asset-id cli param
Ahmad-Wahid Jul 26, 2023
96a7c53
add asset-id to register a weather sensor
Ahmad-Wahid Jul 27, 2023
6a93236
fix sensor schema validation and raise Warnings
Ahmad-Wahid Jul 27, 2023
7c4a077
refactoring
Ahmad-Wahid Jul 27, 2023
1333fc1
refactor schema and update generic asset and type
Ahmad-Wahid Jul 28, 2023
22e52b7
stop code when no close station is found
Ahmad-Wahid Jul 28, 2023
eddc198
update warning message and refactoring
Ahmad-Wahid Aug 1, 2023
b33dd7a
Merge main + update test warning message
Mar 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions flexmeasures_openweathermap/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from ..utils.modeling import (
get_or_create_weather_station,
)
from ..utils.locating import get_locations, get_asset_id_location
from ..utils.locating import get_locations, get_location_by_asset_id
from ..utils.filing import make_file_path
from ..utils.owm import (
save_forecasts_in_db,
Expand Down Expand Up @@ -105,7 +105,7 @@ def add_weather_sensor(**args):
"--asset-id",
type=int,
required=False,
help='Get location "latitude,longitude" for a given asset',
help="ID of a weather station asset - forecasts will be gotten for its location. If present, --location will be ignored."
)
@click.option(
"--store-in-db/--store-as-json-files",
Expand Down Expand Up @@ -146,9 +146,13 @@ def collect_weather_data(location, asset_id, store_in_db, num_cells, method, reg
"[FLEXMEASURES-OWM] Setting OPENWEATHERMAP_API_KEY not available."
)
if asset_id is not None:
locations = get_asset_id_location(asset_id)
else:
locations = [(get_location_by_asset_id(asset_id))]
Ahmad-Wahid marked this conversation as resolved.
Show resolved Hide resolved
elif location is not None:
locations = get_locations(location, num_cells, method)
else:
raise Warning(
"[FLEXMEASURES-OWM] Pass either location or asset-id to get weather forecasts."
)

# Save the results
if store_in_db:
Expand Down
16 changes: 12 additions & 4 deletions flexmeasures_openweathermap/utils/locating.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from flexmeasures.utils.grid_cells import LatLngGrid, get_cell_nums
from flexmeasures import Sensor
from flexmeasures.data.models.generic_assets import GenericAsset
from flexmeasures.data.models.generic_assets import GenericAsset, GenericAssetType
from flexmeasures.utils import flexmeasures_inflection

from .. import WEATHER_STATION_TYPE_NAME
Expand Down Expand Up @@ -108,13 +108,21 @@ def find_weather_sensor_by_location_or_fail(
return weather_sensor


def get_asset_id_location(asset_id: int) -> List[Tuple[float, float]]:
def get_location_by_asset_id(asset_id: int) -> Tuple[float, float]:
"""Get location for forecasting by passing an asset id"""
asset = GenericAsset.query.filter(
GenericAsset.generic_asset_type_id == asset_id
).one_or_none()
if asset is None:
if asset is not None:
asset_type = GenericAssetType.query.filter(
Ahmad-Wahid marked this conversation as resolved.
Show resolved Hide resolved
GenericAssetType.id == asset.generic_asset_type_id
).one_or_none()
if asset_type.name != WEATHER_STATION_TYPE_NAME:
raise Exception(
"[FLEXMEASURES-OWM] Generic asset type name and weather station type name are not the same."
)
else:
raise Exception(
"[FLEXMEASURES-OWM] No asset found for the given asset id %s." % asset_id
)
Ahmad-Wahid marked this conversation as resolved.
Show resolved Hide resolved
return [(asset.latitude, asset.longitude)]
return (asset.latitude, asset.longitude)