OpenWeatherMap Tutorial in Python using PyOWM
In this tutorial, we will learn OpenWeatherMap tutorial in Python using PyOWM. This tutorial includes the following:
- How to get API key from OpenWeatherMap
- Get weather data from OpenWeatherMap
- Forecast weather
- You can also check historical weather data
We will use the Python OpenWeatherMap API (PyOWM) to get the weather details.
Initial installations and getting API key from OpenWeatherMap
First, we start off by installing pyowm library. The easiest way to do this is to type this into your command line:
$- pip install pyowm
Then, you need to have an API key to access the weather data from https://openweathermap.org/
For this, all you need to do is sign up on the OpenWeatherMap Website. Refer to the picture below:
After signing up, click on “API keys” and copy your API key to your clipboard. Refer to the picture below:
By default, the API key that you get will be free. But, you can choose to go for the paid key which has additional privileges. But for this tutorial, the free one will work just fine.
Now that we have installed pyowm and gotten our API key, we can start with our script!
Getting Weather data in Python from OpenWeatherMap
Firstly, import the pyowm library.
import pyowm
Then, we authenticate using an API key. The authenticated connection to the API is stored in the owm_obj object. You will have to provide the API key that you got after signing up at the Open Weather Map website in place of <Enter_API_KEY>
api_key = "<Enter_API_KEY>" #Enter your own API Key owm_obj=pyowm.OWM(api_key)
To get the current weather for a particular city, you need to create an observation object using the omw object by providing the city name, id or coordinates. To get the observation object using the name of the city, use weather_at_place() method.
obs_obj = owm_obj.weather_at_place('Mumbai')
The observation object stores two important objects: A weather object and a location object.
Weather object:
To get all the current weather information, we will create the weather object.
weather=obs_obj.get_weather()
We now get the values by making use of the different methods of pyowm. The library provides many methods to access weather data. It is only a matter of our requirement as to which one suits our needs the most. To have a look at all the methods provided by the pyowm library, visit: pyowm documentation
Let’s take a few examples:
- Temperature:
weather.get_temperature()
Output:
{'temp': 298.8, 'temp_kf': None, 'temp_max': 300.5, 'temp_min': 294.3}
To get the temperature in Celsius write, “weather.get_temperature(unit=‘celsius’) ” , instead.
- Humidity:
weather.get_humidity()
Output:
84
- Description:
Used to give a short or detailed description of the current weather in the city.
w.get_status() #for brief description w.get_detailed_status() #for detailed description
Output:
Rain Light Drizzle
Location Object:
Now, create the location object in the same way as before.
l = obs.get_location()
And try the methods given below:
l.get_name() l.get_ID()
Output:
'Mumbai' 2435743
Weather Forecasting in Python
In addition to all the above methods, this powerful library is also capable of forecasting the weather based on the weather data of the previous days.
This API provides weather forecasts. We need to create a new forecaster object for this purpose.
For 3 hour forecast:
forecast = owm_obj.three_hours_forecast('mumbai')
For daily forecast:
forecast = owm_obj.three_hours_forecast('mumbai')
Some of the forecaster methods are:
fc.will_have_rain() fc.will_have_sun() fc.will_have_clouds()
Output:
True False True
time = "2019-11-30 12:00+00" fc.will_be_rainy_at(time) fc.will_be_sunny_at(time) fc.will_be_cloudy_at(time)
Output:
False False True
For learning about more methods and information about pyowm library, go to https://pyowm.readthedocs.io/en/latest/
To apply what you have learned in this tutorial, visit: Weather script using OpenWeatherMap in Python with GUI
Leave a Reply