csparpa / pyowm

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

Sunrise/Sunset in forecast #180

Closed InunoTaishou closed 7 years ago

InunoTaishou commented 7 years ago

After getting the JSON string from the Weather and Forecast object the sunrise/sunset time are both 0. The member is in the string but it's 0. Unsure if it's the api call where owm doesn't actually have the sunrise/sunset in the forecast or if it's pyowm. I'm trying to get the sunrise and sunset for the week forecast. I was going to create an observation object for each day but all I can find under the usage-examples is getting the observation for the current day.

csparpa commented 7 years ago

Hello @InunoTaishou as regards sunrise/sunset times PyOWM is defaulting to 0 if it is unable to find the sunrise/sunset information in the JSON returned by the OWM API. If you'could mimic the same API call that PyOWM is doing in your specific case, you would be able to understand if sunrise/sunset times actually are in the response JSON - and then we could patch PyOWM to read them.

As regards weather forecasts, the OWM API provides either 3-hours granularity forecasts on a 5-day stripe or 24-hours granularity forecasts on a 16-day stripe. The first could be retrieved using the owm.three_hours_forecast method and the latter using the owm.daily_forecast method - both are explained in the usage-examples doc that you linked

InunoTaishou commented 7 years ago

Hmmm I see. I was looking at the 16 day forecast under the owm api and I didn't see the sunrise/sunset member in the json string on the example but it was in the xml example. I am using daily_forecast to get a week long forecast. I don't know if it's such a big deal to have the sunrise/sunset for each day. I'd assume (based off weatherbug's forecast) that the sunrise/sunset is going to be the same (or relatively close) each day until a certain point. Then the prediction might only be a little bit wrong.

Regardless, if there is a way to get an observation object for a future day (not just today) it would be more accurate for my project. It may be better to show you what I mean and how I'm getting the observation, weather from the observation, and the forecast https://pastebin.com/aypqdtAV

(The code's a bit messy and far from complete but it will demonstrate what I mean)

The important part is

        if self._location.zipcode and self._location.country:
            # forecaster to get the daily forecast up to the limit
            self._forecaster = self._owm.daily_forecast(self._location.zipcode + ',' + self._location.country, limit)
            # forecast object, contains a Location and Weather object
            self._forecast = self._forecaster.get_forecast()
            # gets a list of Weather objects, can retrieve here each day using self._weather[0 to limit]
            self._weather = self._forecast.get_weathers()
            self._observation = self._owm.weather_at_zip_code(self._location.zipcode, self._location.country)
            self._weather_from_observation = self._observation.get_weather()

I'm using the daily_forecast to get the week long forecast and then weather_at_zip_code to get an observation. I can't find a way to get an observation for a future date.

After looking into the owm api there doesn't seem to be a way to get the weather call for future days. Doing the forecast with the xml response (the owm api call) gives the sunrise and sunset in the meta section of the xml string. It's in the forecast.to_XML() but the value is 0. It's strange that the location object is not in the json string

InunoTaishou commented 7 years ago

What openweathermap api call is pyowm using when it does the get_forecast? I thought it would be the 16 day one since it has a day limit but one of the members in the api call (that I've found so far) is not being added to the pyowm class. Calling .get_wind on the weather object just returns the speed. But calling the owm api call for forecast shows the wind speed and direction.

InunoTaishou commented 7 years ago

Sorry I keep posting about stuff not related to the original post. Found another issue when using the daily_forecast. Did a forecaster.when_starts('date') and got 4/8/2017 date (yesterday). I guess pyowm is getting the response in utc because of the interntal tzinfo=UTC()? Is there any way to change the internal tzinfo to use a supplied timezone? So the forecasts are retrieved in the zone I'm trying to request?

I did an api call in my browser and got the datetime from owm and got 1491728400 for the timestamp. Converting the stamp into a date format gives me 2017-04-09 04:00:00 which is the correct day. Also want to add that the api call directly to owm gives me wind speed and deg. I'm not sure where it would be happening by somewhere in the pyowm daily_forecast method it is losing this value (possibly others?) from the actual api call.

csparpa commented 7 years ago

.... chaos... please @InunoTaishou one issue at a time :)

Did you find any bug on PyOWM? Are you proposing any new feature?

PyOWM gets timestamp in UTC from OWM API, then it does not change the timezone, so all timestamps returned are UTC

InunoTaishou commented 7 years ago

Ohh I see

So the sunset/sunrise thing may be an assue with OWM. I made a topic on their site about the json string not have the meta class but the xml does (which contains the sunrise/sunset)

The deg member not being apart of the get_window() method is a bug with PyOWM. The deg is inside the 16 day forecast api call but is dropped somewhere in PyOWM

csparpa commented 7 years ago

Good, thanks! I will close this issue and open a new one about the PyOWM bug

Could you please add code/details to that new issue? I will mention you so you can easily find it

Thank you very much

krisgesling commented 5 years ago

In case anyone else is on the same hunt, it seems that as of March 2018 OWM have sunrise/sunset times "on their roadmap" for the 16 day forecast endpoint: https://openweathermap.desk.com/customer/portal/questions/16786183-sunrise-and-sunset-times-for-forecasted-days