open-meteo / sdk

Open-Meteo schema files
MIT License
19 stars 14 forks source link

Return selected weather model on 'best_match' #90

Open danicarovo opened 3 months ago

danicarovo commented 3 months ago

Our company uses Open-Meteo's Forecast API for the simulation of scenarios. We allow users to select a weather model for wind simulations. We default to best_match if no weather model is provided or if the requested weather model is unavailable for the location. In this case, we would like to inform the user which weather model was selected.

Currently, when best_match is requested as a weather model, open-meteo's response returns best_match, and no information on which weather model was actually selected. We kindly ask for this information to be returned.

Here is some code to reproduce:

import openmeteo_requests

import requests_cache
import pandas as pd
from retry_requests import retry
from openmeteo_sdk import Model

# Returns model as string
def _model_to_string(model):
        """
        Convert the weather model to a string.

        Args:
            model: Weather model.

        Returns:
            str: Weather model as a string.
        """
        for name, value in Model.Model.__dict__.items():
            if value == model:
                return name
        return None

# Setup the Open-Meteo API client with cache and retry on error
cache_session = requests_cache.CachedSession('.cache', expire_after = 3600)
retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2)
openmeteo = openmeteo_requests.Client(session = retry_session)

# Make sure all required weather variables are listed here
# The order of variables in hourly or daily is important to assign them correctly below
url = "https://api.open-meteo.com/v1/forecast"
params = {
    "latitude": 52.52,
    "longitude": 13.41,
    "hourly": "temperature_2m",
         # Non-valid wm for location: ["arpae_cosmo_2i"]. Valid wm for location: arpae_cosmo_5m
        "models": ["arpae_cosmo_2i","arpae_cosmo_5m","best_match"] 
}
responses = openmeteo.weather_api(url, params=params)

accepted_wms_names = []
accepted_wms_enums = []
for response in responses:
    accepted_wms_names.append(_model_to_string(response.Model()))
    accepted_wms_enums.append(response.Model())
print(accepted_wms_names)
print(accepted_wms_enums)

Returns:

['arpae_cosmo_5m', 'best_match']
[59, 1]

We wish that it returned (assuming that ecmwf_ifs04 was selected by open-meteo as the best weather model for the location):

['arpae_cosmo_5m', 'ecmwf_ifs04']
[59, 2]
patrick-zippenfenig commented 3 months ago

Hi, thanks for the suggestion. best_match is a bit more complicated than just selecting a single weather model. Depending on the weather variable, different models might be used. Getting all meta data right will challenging. See #mete