forked from frequenz-floss/frequenz-api-weather
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend the client to stream historical weather forecast data.
Add examples to demonstrate: - How to stream live data with the client. - How to stream the historical data with the client.
- Loading branch information
1 parent
7fc2504
commit e4e3cf1
Showing
3 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" | ||
Streams historical Germany wind forecast data. | ||
Example run: | ||
PYTHONPATH=py python examples/stream_hist_forecast.py "localhost:50051" | ||
License: MIT | ||
Copyright © 2024 Frequenz Energy-as-a-Service GmbH | ||
""" | ||
|
||
import asyncio | ||
import datetime | ||
import sys | ||
|
||
import grpc.aio as grpcaio | ||
from frequenz.client.weather._client import Client | ||
from frequenz.client.weather._types import ForecastFeature, Location | ||
|
||
service_address = sys.argv[1] | ||
|
||
|
||
async def main(service_address): | ||
|
||
client = Client( | ||
grpcaio.insecure_channel(service_address), # or secure channel with credentials | ||
service_address, | ||
) | ||
|
||
features = [ | ||
ForecastFeature.V_WIND_COMPONENT_100_METRE, | ||
ForecastFeature.U_WIND_COMPONENT_100_METRE, | ||
] | ||
|
||
locations = [ | ||
Location( | ||
latitude=52.5, | ||
longitude=13.4, | ||
country_code="DE", | ||
), | ||
] | ||
|
||
now = datetime.datetime.utcnow() | ||
start = now - datetime.timedelta(days=30) | ||
end = now + datetime.timedelta(days=7) | ||
|
||
stream = await client.stream_hist_forecast( | ||
features=features, locations=locations, start=start, end=end | ||
) | ||
|
||
async for fc in stream: | ||
print(fc) | ||
print(fc.to_ndarray_vlf()) | ||
print( | ||
fc.to_ndarray_vlf( | ||
features=[ForecastFeature.U_WIND_COMPONENT_100_METRE], | ||
) | ||
) | ||
|
||
|
||
asyncio.run(main(service_address)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
""" | ||
Streams live Germany wind forecast data. | ||
Example run: | ||
PYTHONPATH=py python examples/stream_live_forecast.py "localhost:50051" | ||
License: MIT | ||
Copyright © 2024 Frequenz Energy-as-a-Service GmbH | ||
""" | ||
|
||
import asyncio | ||
import sys | ||
|
||
import grpc.aio as grpcaio | ||
from frequenz.client.weather._client import Client | ||
from frequenz.client.weather._types import ForecastFeature, Location | ||
|
||
service_address = sys.argv[1] | ||
|
||
|
||
async def main(service_address): | ||
|
||
client = Client( | ||
grpcaio.insecure_channel(service_address), # or secure channel with credentials | ||
service_address, | ||
) | ||
|
||
features = [ | ||
ForecastFeature.V_WIND_COMPONENT_100_METRE, | ||
ForecastFeature.U_WIND_COMPONENT_100_METRE, | ||
] | ||
|
||
locations = [ | ||
Location( | ||
latitude=52.5, | ||
longitude=13.4, | ||
country_code="DE", | ||
), | ||
] | ||
|
||
stream = await client.stream_live_forecast( | ||
features=features, | ||
locations=locations, | ||
) | ||
|
||
async for fc in stream: | ||
print(fc) | ||
print(fc.to_ndarray_vlf()) | ||
print( | ||
fc.to_ndarray_vlf( | ||
features=[ForecastFeature.U_WIND_COMPONENT_100_METRE], | ||
) | ||
) | ||
|
||
|
||
asyncio.run(main(service_address)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters