Skip to content

Commit

Permalink
use local open meteo instance
Browse files Browse the repository at this point in the history
  • Loading branch information
nonnontrivial committed May 14, 2024
1 parent 28636f4 commit c323e61
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
4 changes: 4 additions & 0 deletions api/api/prediction/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os

open_meteo_host = os.getenv("OPEN_METEO_HOST", "localhost")
open_meteo_port = int(os.getenv("OPEN_METEO_PORT", "8080"))
3 changes: 1 addition & 2 deletions api/api/prediction/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# FIXME move
features = [
"Latitude",
"Longitude",
Expand All @@ -13,8 +14,6 @@
OUTPUT_SIZE = 1

MODEL_STATE_DICT_FILE_NAME = "model.pth"

OPEN_METEO_BASE_URL = "https://api.open-meteo.com"
MAX_OKTAS = 8

LOGFILE_KEY = "SKY_BRIGHTNESS_LOGFILE"
22 changes: 17 additions & 5 deletions api/api/prediction/open_meteo_client.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
import logging
import typing as t

from .constants import OPEN_METEO_BASE_URL, MAX_OKTAS
from .config import open_meteo_host, open_meteo_port
from .constants import MAX_OKTAS
from .observer_site import ObserverSite
from .utils import get_astro_time_hour


class OpenMeteoClient:
def __init__(self, site: ObserverSite) -> None:
self.site = site
self.url_base = f"http://{open_meteo_host}:{open_meteo_port}"

async def get_values_at_site(self) -> t.Tuple[int, float]:
"""get cloudcover and elevation values for the observer site"""
import httpx

lat, lon = self.site.latitude.value, self.site.longitude.value

async with httpx.AsyncClient() as client:
r = await client.get(
f"{OPEN_METEO_BASE_URL}/v1/forecast?latitude={lat}&longitude={lon}&hourly=temperature_2m,cloud_cover&forecast_days=1"
)
params = {
"latitude": lat,
"longitude": lon,
"models": "ecmwf_ifs04",
"hourly": "temperature_2m,cloud_cover"
}
r = await client.get(f"{self.url_base}/v1/forecast", params=params)
r.raise_for_status()
res_json = r.json()

elevation = float(res_json.get("elevation", 0.))

idx = self.get_hourly_index_of_site_time()
cloud_cover = res_json["hourly"]["cloud_cover"][idx]
cloud_cover = self.get_cloud_cover_as_oktas(cloud_cover)
return cloud_cover, float(res_json["elevation"])

return cloud_cover, elevation

def get_hourly_index_of_site_time(self) -> int:
"""pull out the relevant slice in the meteo data"""
Expand Down
2 changes: 1 addition & 1 deletion api/api/prediction/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
path_to_logfile = (Path.home() / logfile_name) if logfile_name else None

logging.basicConfig(
format="%(asctime)s -> %(levelname)s: %(message)s",
format="%(asctime)s [%(levelname)s] %(message)s",
filename=path_to_logfile if bool(path_to_logfile) else None,
encoding="utf-8",
level=logging.DEBUG,
Expand Down
2 changes: 1 addition & 1 deletion api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ fastapi~=0.110.2
httpx==0.26.0
uvicorn==0.25.0
pytest==7.4.3
Pillow~=10.1.0
Pillow~=10.3.0
numpy~=1.26.2
30 changes: 24 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
version: "3"

services:
api:
build: ./api
ports:
- "8000:8000"
environment:
API_VERSION: "v1"
rabbitmq:
image: "rabbitmq:management"
ports:
Expand All @@ -19,6 +14,25 @@ services:
environment:
RABBITMQ_DEFAULT_USER: "guest"
RABBITMQ_DEFAULT_PASS: "guest"

openmeteo:
image: "ghcr.io/open-meteo/open-meteo"
ports:
- "8080:8080"
volumes:
- open-meteo-data:/app/data

api:
build: ./api
ports:
- "8000:8000"
environment:
API_VERSION: "v1"
OPEN_METEO_HOST: "openmeteo"
restart: on-failure
depends_on:
- openmeteo

cpp:
build: ./cpp
environment:
Expand All @@ -31,4 +45,8 @@ services:
- api
links:
- rabbitmq

volumes:
open-meteo-data:
external: true

0 comments on commit c323e61

Please sign in to comment.