open-meteo / python-requests

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

Unable to retrieve values for the daily variables 'sunrise' and 'sunset' #44

Open alexiosgkikas opened 7 months ago

alexiosgkikas commented 7 months ago
import openmeteo_requests

url = "https://api.open-meteo.com/v1/forecast"
params = {
    "latitude": 52.52,
    "longitude": 13.41,
    "daily": [
        "sunrise",
        "sunset",
    ],
}
openmeteo = openmeteo_requests.Client()
responses = openmeteo.weather_api(url, params=params)

response = responses[0]
daily = response.Daily()
daily_sunrise = daily.Variables(0).ValuesAsNumpy()
daily_sunset = daily.Variables(1).ValuesAsNumpy()

print("Sunrise type:", type(daily_sunrise))
print("Sunrise values:", daily_sunrise)
print("Sunset type:", type(daily_sunset))
print("Sunset values:", daily_sunset)

By running the previous snippet of code it prints the below results.

Sunrise type: <class 'int'>
Sunrise values: 0
Sunset type: <class 'int'>
Sunset values: 0

Question: Is this the expected behavior or should it return a list of values for each variable(i.e ["2024-02-22T16:30", ...])?

It was tested in Python version 3.11.7, with the below libraries: openmeteo_requests==1.2.0 openmeteo_sdk==1.10.0

patrick-zippenfenig commented 7 months ago

Hi, this is a shortcoming of the automatic code generator. Sunrise and set values are stored as 64-bit integers. Below the corrected code to get the correct values. Support for automatic code generation will follow at some point

daily_sunrise = daily.Variables(0).ValuesInt64AsNumpy()
daily_sunset = daily.Variables(1).ValuesInt64AsNumpy()
Sunwx123 commented 1 month ago

Hi, i use the code that you provided.

The results of data about sunrise and sunset are as follow: [1724126298 1724212798 1724299297 1724385797 1724472297 1724558798, 1724645298].

How do I get a normal sunrise time like ["2024-02-22T16:30", ...]?

patrick-zippenfenig commented 1 month ago

Those are unix timestamps which you can convert using datetime functions