open-meteo / python-requests

Open-Meteo Python Library using `requests`
MIT License
15 stars 4 forks source link

get variable units from response #80

Closed SchabiDesigns closed 2 weeks ago

SchabiDesigns commented 3 weeks ago

Hello guys Is there a way to get the variable units from the response?

responses = openmeteo.weather_api(url, params=params)
for response, model in zip(responses, models):
  hourly = response.Hourly()
  hourly_variables = {var: hourly.Variables(i).ValuesAsNumpy() for var, i in zip(variables, range(len(variables)))}

_variables and models are just lists of variables I pass to the function... for example: variables = ["temperature_2m", "precipitation"] models = ["bestmatch"]

I would like to do something similar for the units in my variables like that: hourly_units = {var: hourly.Units(i).ValuesAsNumpy() for var, i in zip(variables, range(len(variables)))}` If I check the API in the response there is hourly_units which is what I need... grafik

Thanks for your help! To the developers: Thanks for that epic possiblity to get forcast and historical data for so many weather-models!!!! I absolutly love it 💕 Regards Schabi

patrick-zippenfenig commented 2 weeks ago

Hi, the unit is encoded in .Unit() which returns an integer from an enumation which den be converted with

import openmeteo_requests
import openmeteo_sdk

[...]

def unit_to_name(code):
  for name, value in openmeteo_sdk.Unit.Unit.__dict__.items():
    if value == code:
       return name
  return None

# Process hourly data. The order of variables needs to be the same as requested.
hourly = response.Hourly()
#hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy()
hourly_temperature_2m = hourly.Variables(0)
unit = hourly_temperature_2m.Unit()
print(unit)
print(type(unit))
print(unit_to_name(unit))

hourly_temperature_2m_Data = hourly_temperature_2m.ValuesAsNumpy()

Prints

1
<class 'int'>
celsius