Improving Python Trip Planner with Weather Forecasts: Open Meteo API

Improving Python Trip Planner with Weather Forecasts: Open Meteo API

June 30, 2023

Python Weather API - Open Meteo

After releasing the Python Trip Planner Project, I have received some comments about an open source project with a wonderful API to include weather forecasts.

I am talking about the Open-meteo project and its wonderful API to query weather data.

Using Open-Meteo: Weather Forecast in Python

If you enjoy developing in Python like I do, you will be wondering how to use this API. Let me show you how simple it is.

We are going to be using the Python Interface for Open-Meteo

pip install openmeteo-py

Once it is installed, you can query a forecast for weather data from python by just providing the latitude and longitude:

from openmeteo_py import Hourly,Daily,Options,OWmanager

# Latitude, Longitude input
latitude = 30
longitude = 20

hourly = Hourly()
daily = Daily()
options = Options(latitude,longitude)

mgr = OWmanager(options,
    hourly.all(),
    daily.all())

# Download data
meteo = mgr.get_data() #This is a dict with your data

Open-Meteo: Discovering Weather Fields

The good news - Now we have our weather data ready in Python. The not so good news - It is in dictionary format, but dont worry - I will show you how to extract it

print(meteo) #we almost have our plots
print(type(meteo))

The extracted dictionary contains a couple of dictionaries inside. One interesting one is the dictionary daily: we can extract the lists time and list apparent_temperature_max from that dictionary like so:

import pandas as pd

# assuming that your dictionary is called `meteo`
daily_data = meteo["daily"]

# check if 'time' and 'apparent_temperature_max' are in daily_data
if 'time' in daily_data and 'apparent_temperature_max' in daily_data:
    df = pd.DataFrame({
        'time': daily_data['time'],
        'apparent_temperature_max': daily_data['apparent_temperature_max']
    })
else:
    print("'time' or 'apparent_temperature_max' not found in daily data")

Another interesting dictionary is called hourly and we can extract some interesting weather attributes like so:

import pandas as pd

# assuming that your dictionary is called `meteo`
hourly_data = meteo["hourly"]

# check if 'time', 'apparent_temperature_max' and 'apparent_temperature_min' are in daily_data
if 'time' in daily_data and 'apparent_temperature' in hourly_data:
    df_meteo_h = pd.DataFrame({
        'time': hourly_data['time'],
        'apparent_temperature': hourly_data['apparent_temperature'],
        'windspeed_10m': hourly_data['windspeed_10m'],
        'winddirection_10m': hourly_data['winddirection_10m'],
        'precipitation': hourly_data['precipitation']
    })
else:
    print("'time', 'apparent_temperature' not found in daily data")

Now that we have a great pandas dataframe, lets create some plots:

#import plotly.express as px
import plotly.graph_objects as go

#pip install --upgrade nbformat


fig = go.Figure()
fig.add_trace(go.Scatter(x=df_meteo_h['time'], y=df_meteo_h['apparent_temperature'], name='Temperature', line=dict(color='green')))

fig.update_layout(
    title='Temperature Forecast',
    xaxis=dict(title='Time'),
    yaxis=dict(title='Temperature (Celsius)')
)

fig.show()

FAQ

How can I try the Python DASH App?

Trying the app locally might be an easy process for people that are familiar with software development. But I want this project to help as much people as possible.

How can I Contribute?

  • I have made all the code Open Source and this is the public Github repository where I have built the code, please feel free to have a look, experiment with the code and suggest any improvements: