In this gist you can see how to obtain historical weather data from Open Meteo api.
Last active
January 23, 2025 21:16
-
-
Save KobaKhit/e0230cd7f9e51846777f4934a3c68d5a to your computer and use it in GitHub Desktop.
A free historical weather API using Python and Open-Meteo.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openmeteo_requests | |
import requests_cache | |
import pandas as pd | |
from retry_requests import retry | |
def get_weather_data(latitude, longitude, start_date, end_date, id = None): | |
""" | |
Fetches historical weather data for a given location and date range using the Open-Meteo API. | |
Args: | |
latitude (float): Latitude of the location. | |
longitude (float): Longitude of the location. | |
start_date (str): Start date in 'YYYY-MM-DD' format. | |
end_date (str): End date in 'YYYY-MM-DD' format. | |
Returns: | |
pd.DataFrame: DataFrame containing hourly weather data including temperature, humidity, precipitation, rain, snowfall, snow depth, and weather code. | |
""" | |
# Setup the Open-Meteo API client with cache and retry on error | |
cache_session = requests_cache.CachedSession('.cache', expire_after = -1) | |
retry_session = retry(cache_session, retries = 5, backoff_factor = 0.2) | |
openmeteo = openmeteo_requests.Client(session = retry_session) | |
# Make sure all required weather variables are listed here | |
# The order of variables in hourly or daily is important to assign them correctly below | |
url = "https://archive-api.open-meteo.com/v1/archive" | |
params = { | |
"latitude": latitude, | |
"longitude": longitude, | |
"start_date": start_date, | |
"end_date": end_date, | |
"hourly": ["temperature_2m", "relative_humidity_2m", "precipitation", "rain", "snowfall", "snow_depth", "weather_code"] | |
} | |
responses = openmeteo.weather_api(url, params=params) | |
# Process first location. Add a for-loop for multiple locations or weather models | |
response = responses[0] | |
# print(f"Coordinates {response.Latitude()}°N {response.Longitude()}°E") | |
# print(f"Elevation {response.Elevation()} m asl") | |
# print(f"Timezone {response.Timezone()} {response.TimezoneAbbreviation()}") | |
# print(f"Timezone difference to GMT+0 {response.UtcOffsetSeconds()} s") | |
# Process hourly data. The order of variables needs to be the same as requested. | |
hourly = response.Hourly() | |
hourly_temperature_2m = hourly.Variables(0).ValuesAsNumpy() | |
hourly_relative_humidity_2m = hourly.Variables(1).ValuesAsNumpy() | |
hourly_precipitation = hourly.Variables(2).ValuesAsNumpy() | |
hourly_rain = hourly.Variables(3).ValuesAsNumpy() | |
hourly_snowfall = hourly.Variables(4).ValuesAsNumpy() | |
hourly_snow_depth = hourly.Variables(5).ValuesAsNumpy() | |
hourly_weather_code = hourly.Variables(6).ValuesAsNumpy() | |
hourly_data = {"date": pd.date_range( | |
start = pd.to_datetime(hourly.Time(), unit = "s", utc = True), | |
end = pd.to_datetime(hourly.TimeEnd(), unit = "s", utc = True), | |
freq = pd.Timedelta(seconds = hourly.Interval()), | |
inclusive = "left" | |
)} | |
hourly_data["temperature_2m"] = hourly_temperature_2m | |
hourly_data["relative_humidity_2m"] = hourly_relative_humidity_2m | |
hourly_data["precipitation"] = hourly_precipitation | |
hourly_data["rain"] = hourly_rain | |
hourly_data["snowfall"] = hourly_snowfall | |
hourly_data["snow_depth"] = hourly_snow_depth | |
hourly_data["weather_code"] = hourly_weather_code | |
hourly_dataframe = pd.DataFrame(data = hourly_data) | |
hourly_dataframe['latitude'] = latitude | |
hourly_dataframe['longitude'] = longitude | |
if id is not None: | |
hourly_dataframe['id'] = id | |
return hourly_dataframe | |
temp = get_weather_data(34.021586439393936, -118.299255,'2025-01-21','2025-01-21') | |
temp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
code | day_description | day_image | night_description | night_image | |
---|---|---|---|---|---|
0 | Sunny | http://openweathermap.org/img/wn/[email protected] | Clear | http://openweathermap.org/img/wn/[email protected] | |
1 | Mainly Sunny | http://openweathermap.org/img/wn/[email protected] | Mainly Clear | http://openweathermap.org/img/wn/[email protected] | |
2 | Partly Cloudy | http://openweathermap.org/img/wn/[email protected] | Partly Cloudy | http://openweathermap.org/img/wn/[email protected] | |
3 | Cloudy | http://openweathermap.org/img/wn/[email protected] | Cloudy | http://openweathermap.org/img/wn/[email protected] | |
45 | Foggy | http://openweathermap.org/img/wn/[email protected] | Foggy | http://openweathermap.org/img/wn/[email protected] | |
48 | Rime Fog | http://openweathermap.org/img/wn/[email protected] | Rime Fog | http://openweathermap.org/img/wn/[email protected] | |
51 | Light Drizzle | http://openweathermap.org/img/wn/[email protected] | Light Drizzle | http://openweathermap.org/img/wn/[email protected] | |
53 | Drizzle | http://openweathermap.org/img/wn/[email protected] | Drizzle | http://openweathermap.org/img/wn/[email protected] | |
55 | Heavy Drizzle | http://openweathermap.org/img/wn/[email protected] | Heavy Drizzle | http://openweathermap.org/img/wn/[email protected] | |
56 | Light Freezing Drizzle | http://openweathermap.org/img/wn/[email protected] | Light Freezing Drizzle | http://openweathermap.org/img/wn/[email protected] | |
57 | Freezing Drizzle | http://openweathermap.org/img/wn/[email protected] | Freezing Drizzle | http://openweathermap.org/img/wn/[email protected] | |
61 | Light Rain | http://openweathermap.org/img/wn/[email protected] | Light Rain | http://openweathermap.org/img/wn/[email protected] | |
63 | Rain | http://openweathermap.org/img/wn/[email protected] | Rain | http://openweathermap.org/img/wn/[email protected] | |
65 | Heavy Rain | http://openweathermap.org/img/wn/[email protected] | Heavy Rain | http://openweathermap.org/img/wn/[email protected] | |
66 | Light Freezing Rain | http://openweathermap.org/img/wn/[email protected] | Light Freezing Rain | http://openweathermap.org/img/wn/[email protected] | |
67 | Freezing Rain | http://openweathermap.org/img/wn/[email protected] | Freezing Rain | http://openweathermap.org/img/wn/[email protected] | |
71 | Light Snow | http://openweathermap.org/img/wn/[email protected] | Light Snow | http://openweathermap.org/img/wn/[email protected] | |
73 | Snow | http://openweathermap.org/img/wn/[email protected] | Snow | http://openweathermap.org/img/wn/[email protected] | |
75 | Heavy Snow | http://openweathermap.org/img/wn/[email protected] | Heavy Snow | http://openweathermap.org/img/wn/[email protected] | |
77 | Snow Grains | http://openweathermap.org/img/wn/[email protected] | Snow Grains | http://openweathermap.org/img/wn/[email protected] | |
80 | Light Showers | http://openweathermap.org/img/wn/[email protected] | Light Showers | http://openweathermap.org/img/wn/[email protected] | |
81 | Showers | http://openweathermap.org/img/wn/[email protected] | Showers | http://openweathermap.org/img/wn/[email protected] | |
82 | Heavy Showers | http://openweathermap.org/img/wn/[email protected] | Heavy Showers | http://openweathermap.org/img/wn/[email protected] | |
85 | Light Snow Showers | http://openweathermap.org/img/wn/[email protected] | Light Snow Showers | http://openweathermap.org/img/wn/[email protected] | |
86 | Snow Showers | http://openweathermap.org/img/wn/[email protected] | Snow Showers | http://openweathermap.org/img/wn/[email protected] | |
95 | Thunderstorm | http://openweathermap.org/img/wn/[email protected] | Thunderstorm | http://openweathermap.org/img/wn/[email protected] | |
96 | Light Thunderstorms With Hail | http://openweathermap.org/img/wn/[email protected] | Light Thunderstorms With Hail | http://openweathermap.org/img/wn/[email protected] | |
99 | Thunderstorm With Hail | http://openweathermap.org/img/wn/[email protected] | Thunderstorm With Hail | http://openweathermap.org/img/wn/[email protected] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment