From 723d96aabb57cb78463b7a8a32f1f96030b8aca2 Mon Sep 17 00:00:00 2001 From: Rosheen Naeem Date: Fri, 15 Mar 2024 02:40:33 +0100 Subject: [PATCH 1/2] Adding open-meteo dataset --- .../open-meteo/open_meteo.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 quartz_solar_forecast/open-meteo/open_meteo.py diff --git a/quartz_solar_forecast/open-meteo/open_meteo.py b/quartz_solar_forecast/open-meteo/open_meteo.py new file mode 100644 index 00000000..90ba2faf --- /dev/null +++ b/quartz_solar_forecast/open-meteo/open_meteo.py @@ -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) From 68982fbf9918aaf5894ac3ea9f2f8de5226272b3 Mon Sep 17 00:00:00 2001 From: Rosheen Naeem Date: Fri, 15 Mar 2024 02:42:11 +0100 Subject: [PATCH 2/2] added docker support for open-meteo --- .env | 5 +++++ .gitignore | 1 - docker-compose.yaml | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 .env create mode 100644 docker-compose.yaml diff --git a/.env b/.env new file mode 100644 index 00000000..407f7a17 --- /dev/null +++ b/.env @@ -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 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 00b8ec74..9cd8e31e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ -.env scripts/datapipes/test.nc scripts/datapipes/UK_PV_metadata.csv scripts/datapipes/nwp_data diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..1e2e7756 --- /dev/null +++ b/docker-compose.yaml @@ -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: \ No newline at end of file