csparpa / pyowm

A Python wrapper around the OpenWeatherMap web API
https://pyowm.readthedocs.io
MIT License
789 stars 169 forks source link

Strange behaviour during iterative requests #395

Open mrneilbutler opened 2 years ago

mrneilbutler commented 2 years ago

I have been having trouble with pyowm not returning the most recent weather data when the request is inside a function definition or a list iteration. (I apologise if my terminology here is incorrect). It seems to return the result for a request from the first time that request was made in that specific way. I have refined the code to show the specific bug:

owm = OWM('removed key') mgr = owm.weather_manager() placesIDs = ['removed list of places']

wobs = mgr.weather_at_id(placesIDs[0]) wweather = wobs.weather wwind = wweather.wind() print(wobs) print(wweather) print(wwind)

listOfObservations = mgr.weather_at_ids(placesIDs) weatherList = [obs.weather for obs in listOfObservations] windList = [weather.wind() for weather in weatherList] print(listOfObservations[0]) print(weatherList[0]) print(windList[0])

The two lumps of code should return the exact same data. However they do not. This is the print out:

<pyowm.weatherapi25.observation.Observation - reception_time=2022-04-01 17:04:14+00:00> <pyowm.weatherapi25.weather.Weather - reference_time=2022-04-01 17:04:13+00:00, status=clouds, detailed_status=broken clouds> {'speed': 2.84, 'deg': 313, 'gust': 5.77} <pyowm.weatherapi25.observation.Observation - reception_time=2022-04-01 17:04:14+00:00> <pyowm.weatherapi25.weather.Weather - reference_time=2022-04-01 16:09:47+00:00, status=clouds, detailed_status=broken clouds> {'speed': 3.11, 'deg': 328}

The weather managers are the same but the .weathers are not and the wind data is different. This is not limited to wind but as a bonus I have noticed that the gust information is stripped from the data created through the iterative method.

This behviour is preserved on Thonny on windows and Raspberry Pi and directly on python3 on Raspberry Pi.

Hopefully I haven't wasted your time!

geofbaum commented 2 years ago

@mrneilbutler My guess without looking through the code again is that weather_at_ids doesn't get the current observation but the last hourly update. You can see that by looking at the reference time that's returned in the Weather Object. @csparpa can likely verify my guess but after doing a quick test on my own with the examples from the documentation it appears to be the case.

Each station called seperately run at 19:10Z

mgr = owm.weather_manager()
my_city_id = 2643743 #London
wobs = mgr.weather_at_id(my_city_id)
wweather = wobs.weather
wwind = wweather.wind()
print(wobs)
print(wweather)
print(wwind)
my_city_id = 4517009 
wobs = mgr.weather_at_id(my_city_id)
wweather = wobs.weather
wwind = wweather.wind()
print(wobs)
print(wweather)
print(wwind)
my_city_id = 5056033 
wobs = mgr.weather_at_id(my_city_id)
wweather = wobs.weather
wwind = wweather.wind()
print(wobs)
print(wweather)
print(wwind)

Output:

<pyowm.weatherapi25.observation.Observation - reception_time=2022-04-01 19:10:15+00>
<pyowm.weatherapi25.weather.Weather - reference_time=2022-04-01 19:08:32+00, status=clear, detailed_status=clear sky>
{'speed': 2.57, 'deg': 0}
<pyowm.weatherapi25.observation.Observation - reception_time=2022-04-01 19:10:15+00>
<pyowm.weatherapi25.weather.Weather - reference_time=2022-04-01 19:09:42+00, status=clouds, detailed_status=overcast clouds>
{'speed': 3.13, 'deg': 211, 'gust': 7.6}
<pyowm.weatherapi25.observation.Observation - reception_time=2022-04-01 19:10:15+00>
<pyowm.weatherapi25.weather.Weather - reference_time=2022-04-01 19:09:43+00, status=clouds, detailed_status=overcast clouds>
{'speed': 4.76, 'deg': 159, 'gust': 5.32}

Multiple stations run at 19:15 UTC

my_list_of_city_ids = [2643743 , 4517009, 5056033]
listOfObservations = mgr.weather_at_ids(my_list_of_city_ids)
weatherList = [obs.weather for obs in listOfObservations]
windList = [weather.wind() for weather in weatherList]
print(listOfObservations[0])
print(listOfObservations)
print(weatherList[0])
print(weatherList)
print(windList[0])
print(windList)

Output:

<pyowm.weatherapi25.observation.Observation - reception_time=2022-04-01 19:16:29+00>
[<pyowm.weatherapi25.observation.Observation - reception_time=2022-04-01 19:16:29+00>, <pyowm.weatherapi25.observation.Observation - reception_time=2022-04-01 19:16:29+00>, <pyowm.weatherapi25.observation.Observation - reception_time=2022-04-01 19:16:29+00>]
<pyowm.weatherapi25.weather.Weather - reference_time=2022-04-01 19:09:13+00, status=clear, detailed_status=clear sky>
[<pyowm.weatherapi25.weather.Weather - reference_time=2022-04-01 19:09:13+00, status=clear, detailed_status=clear sky>, <pyowm.weatherapi25.weather.Weather - reference_time=2022-04-01 19:09:26+00, status=clouds, detailed_status=overcast clouds>, <pyowm.weatherapi25.weather.Weather - reference_time=2022-04-01 19:10:14+00, status=clouds, detailed_status=overcast clouds>]
{'speed': 2.57, 'deg': 0}
[{'speed': 2.57, 'deg': 0}, {'speed': 3.13, 'deg': 211}, {'speed': 4.76, 'deg': 159}]
mrneilbutler commented 2 years ago

Thanks!! Interesting - that's something I can live with. I had an error where a request was in a function and the reference time in the weather object hadn't changed for 12+ hours. This looked like the same problem. Maybe they're two different things. Also, the different calls could explain the absence of gust, then, I guess.

csparpa commented 2 years ago

@mrneilbutler @geofbaum thank you for investigating this. I will be able to do my part in the upcoming days

What I can tell you now is that recently a few issues arose with the legacy OWM API endpoints (which are called by former PyOWM methods such as weahter_at_id and the likes)... My 2 cents on this: OWM is silently telling us to switch to using the OneCall endpoints only...

Which @mrneilbutler is by the way something you can already do from your code via PyOWM, and it would be interesting to see if data misalignments also happen in that case!