Giter VIP home page Giter VIP logo

Comments (17)

FL550 avatar FL550 commented on July 27, 2024

Thanks for reaching out. Can you please provide me the full station name from which you are trying to retrieve the data?

from simple_dwd_weatherforecast.

cflexible avatar cflexible commented on July 27, 2024

from simple_dwd_weatherforecast.

FL550 avatar FL550 commented on July 27, 2024

Ok, so then it might be related to connection problems. This error can only occur if there were retrieved no or only erroneous data. I could improve the error handling to make this more transparent and make troubleshooting more easily.

from simple_dwd_weatherforecast.

cflexible avatar cflexible commented on July 27, 2024

from simple_dwd_weatherforecast.

FL550 avatar FL550 commented on July 27, 2024

Oh, now I understand you. Can you please provide a code snippet which produces the error?

from simple_dwd_weatherforecast.

cflexible avatar cflexible commented on July 27, 2024

from simple_dwd_weatherforecast.

FL550 avatar FL550 commented on July 27, 2024

This hasn't worked unfortunately. The code is not visible for me. Can you please send me the snippet again?

from simple_dwd_weatherforecast.

cflexible avatar cflexible commented on July 27, 2024

from simple_dwd_weatherforecast.

FL550 avatar FL550 commented on July 27, 2024

Hi, first of all, sorry for the late replies. I was not at home. Unfortunately, I can't see your code. Can you please try to attach the code via the webbrowser instead of mail?

from simple_dwd_weatherforecast.

cflexible avatar cflexible commented on July 27, 2024

Hi, here is the code:
from geopy.geocoders import Nominatim

Initialize Nominatim API

geolocator = Nominatim(user_agent="MyChatbot")

For the weather forecast the dwd (Deutscher Wetter Dfrom simple_dwd_weatherforecast import dwdforecast

from simple_dwd_weatherforecast import dwdforecast
from datetime import datetime, timedelta, timezone

Weather functions

Return the location information for a given city name

def getLocation(name):
if name is None:
return None
try:
location = geolocator.geocode(name)
except:
global errorMessage
errorMessage = "Error while looking for location"
return None
return location

Returns the weather (the temperature and the cloud coverage) for a location

This is just an example. A lot of more values are possible.

def getWeather(location, time):
if location is None:
return None

# Attention: There is a bug in the simple_dwd_weatherforecast library. If you call the next functions multiple times for the
# same locationit crashes. I have sent a bug report to the developers on github.com.
try: 
    next_weather_station = dwdforecast.get_nearest_station_id(location.latitude, location.longitude)
    if next_weather_station is not None:
        print("Station ID found: ", next_weather_station)
        dwd_weather = dwdforecast.Weather(next_weather_station)
        # print("Looking for weather at ", next_weather_station, " for time ", time)
        temperature    = dwd_weather.get_forecast_data(dwdforecast.WeatherDataType.TEMPERATURE, time) - 273.16
        cloud_coverage = dwd_weather.get_forecast_data(dwdforecast.WeatherDataType.CLOUD_COVERAGE, time)
        return temperature, cloud_coverage
    else:
        return None
except:
    global errorMessage
    errorMessage = "Error while looking for weather"
    return None, None

myLocation = getLocation('Altenstadt')
time_now = datetime.today()
time_tomorrow = datetime.today() + timedelta(days=+1)
print(getWeather(myLocation, time_now))
print(getWeather(myLocation, time_tomorrow))

from simple_dwd_weatherforecast.

FL550 avatar FL550 commented on July 27, 2024

Ok, I see. You are forcing a new init of the Weather class without actually requesting the new weather data. With your approach there will be a second request to the DWD server which is unnecessary, as the class stores the data for the next days in a single request.

Here is a modified version of your code, which performs your request:

from geopy.geocoders import Nominatim

# Initialize Nominatim API
geolocator = Nominatim(user_agent="MyChatbot")

# For the weather forecast the dwd (Deutscher Wetter Dfrom simple_dwd_weatherforecast import dwdforecast
from simple_dwd_weatherforecast import dwdforecast
from datetime import datetime, timedelta, timezone

# Weather functions
# Return the location information for a given city name
def getLocation(name):
    if name is None:
        return None
    try:
        location = geolocator.geocode(name)
    except:
        global errorMessage
        errorMessage = "Error while looking for location"
        return None
    return location

class CustomWeather:
    weather_station = None
    def __init__(self, location):
        next_station_id = dwdforecast.get_nearest_station_id(location.latitude, location.longitude)
        print("Station ID found: ", next_station_id)
        self.weather_station = dwdforecast.Weather(next_station_id)
    
    def getWeather(self, time):
        # print("Looking for weather at ", next_weather_station, " for time ", time)
        temperature    = round(self.weather_station.get_forecast_data(dwdforecast.WeatherDataType.TEMPERATURE, time) - 273.16, 2)
        cloud_coverage = self.weather_station.get_forecast_data(dwdforecast.WeatherDataType.CLOUD_COVERAGE, time)
        return temperature, cloud_coverage

myLocation = getLocation('Altenstadt')
# Init the CustomWeatherClass
myWeather = CustomWeather(myLocation)

time_now = datetime.today()
time_tomorrow = datetime.today() + timedelta(days=+1)

print(myWeather.getWeather(time_now))
print(myWeather.getWeather(time_tomorrow))

I hope this helps.

from simple_dwd_weatherforecast.

cflexible avatar cflexible commented on July 27, 2024

from simple_dwd_weatherforecast.

FL550 avatar FL550 commented on July 27, 2024

Ok. Glad you could resolve it. May I close this issue then?

from simple_dwd_weatherforecast.

cflexible avatar cflexible commented on July 27, 2024

from simple_dwd_weatherforecast.

FL550 avatar FL550 commented on July 27, 2024

I agree. But can you please give me then a code example which produces this error? I was unable to reproduce this.

from simple_dwd_weatherforecast.

cflexible avatar cflexible commented on July 27, 2024

from simple_dwd_weatherforecast.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.