Skip to content

Commit

Permalink
Merge pull request #84 from roshnaeem/open-meteo
Browse files Browse the repository at this point in the history
Accessing open-meteo dataset from AWS s3 using docker
  • Loading branch information
jacobbieker authored Mar 30, 2024
2 parents f930472 + 68982fb commit d1d0f87
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
OPEN_METEO_MODELS=ncep_gfs013
OPEN_METEO_VARIABLES=temperature_2m,precipitation,cloud_cover
OPEN_METEO_MAX_AGE_DAYS=3
OPEN_METEO_REPEAT_INTERVAL=5
OPEN_METEO_CONCURRENT=4
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.env
scripts/datapipes/test.nc
scripts/datapipes/UK_PV_metadata.csv
scripts/datapipes/nwp_data
Expand Down
29 changes: 29 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: '3.8'

x-shared_environment: &shared_environment
LOG_LEVEL: ${LOG_LEVEL:-info}

services:
open-meteo-api:
image: ghcr.io/open-meteo/open-meteo
container_name: open-meteo-api
ports:
- "8080:8080"
command: serve
volumes:
- data:/app/data
restart: always
environment:
<<: *shared_environment
open-meteo-sync:
image: ghcr.io/open-meteo/open-meteo
container_name: open-meteo-sync
command: sync ${OPEN_METEO_MODELS} ${OPEN_METEO_VARIABLES} --past-days ${OPEN_METEO_MAX_AGE_DAYS} --repeat-interval ${OPEN_METEO_REPEAT_INTERVAL} --concurrent ${OPEN_METEO_CONCURRENT}
volumes:
- data:/app/data
restart: always
environment:
<<: *shared_environment

volumes:
data:
56 changes: 56 additions & 0 deletions quartz_solar_forecast/open-meteo/open_meteo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import requests
import pandas as pd
from datetime import datetime, timedelta
import xarray as xr

from quartz_solar_forecast.pydantic_models import PVSite

def fetch_open_meteo_temperature(site: PVSite, ts: datetime):
"""
Fetch hourly temperature data from the local Open-Meteo API for a specific site.
:param site: The PV site object containing latitude and longitude.
:param ts: Timestamp for when the forecast is desired.
:return: A Dataset with the requested weather data.
"""

variables = ['temperature_2m', 'precipitation', 'cloud_cover']

# Format the timestamp to a suitable string format for the API
start_date = ts.strftime('%Y-%m-%d')
end_date = (ts + timedelta(days=2)).strftime('%Y-%m-%d')

# Construct the API URL
api_url = (
f"http://127.0.0.1:8080/v1/forecast?latitude={site.latitude}&longitude={site.longitude}"
f"&hourly={','.join(variables)}"
f"&start_date={start_date}&end_date={end_date}"
)

try:
response = requests.get(api_url)
response.raise_for_status()
data = response.json()

df = pd.DataFrame({
'time': pd.to_datetime(data['hourly']['time']),
'temperature_2m': data['hourly'].get('temperature_2m', [None] * len(data['hourly']['time'])),
'precipitation': data['hourly'].get('precipitation', [None] * len(data['hourly']['time'])),
'cloud_cover': data['hourly'].get('cloud_cover', [None] * len(data['hourly']['time']))
}).set_index('time')

# Convert the DataFrame to an xarray Dataset
data_xr = xr.Dataset.from_dataframe(df)

return df

except requests.RequestException as e:
print(f"Request failed: {e}")
return None


# Example usage
site = PVSite(latitude=47.1, longitude=8.4, capacity_kwp=1)
ts = datetime.now()
data = fetch_open_meteo_temperature(site, ts)
print(data)

0 comments on commit d1d0f87

Please sign in to comment.