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/01d@2x.png | Clear | http://openweathermap.org/img/wn/01n@2x.png | |
| 1 | Mainly Sunny | http://openweathermap.org/img/wn/01d@2x.png | Mainly Clear | http://openweathermap.org/img/wn/01n@2x.png | |
| 2 | Partly Cloudy | http://openweathermap.org/img/wn/02d@2x.png | Partly Cloudy | http://openweathermap.org/img/wn/02n@2x.png | |
| 3 | Cloudy | http://openweathermap.org/img/wn/03d@2x.png | Cloudy | http://openweathermap.org/img/wn/03n@2x.png | |
| 45 | Foggy | http://openweathermap.org/img/wn/50d@2x.png | Foggy | http://openweathermap.org/img/wn/50n@2x.png | |
| 48 | Rime Fog | http://openweathermap.org/img/wn/50d@2x.png | Rime Fog | http://openweathermap.org/img/wn/50n@2x.png | |
| 51 | Light Drizzle | http://openweathermap.org/img/wn/09d@2x.png | Light Drizzle | http://openweathermap.org/img/wn/09n@2x.png | |
| 53 | Drizzle | http://openweathermap.org/img/wn/09d@2x.png | Drizzle | http://openweathermap.org/img/wn/09n@2x.png | |
| 55 | Heavy Drizzle | http://openweathermap.org/img/wn/09d@2x.png | Heavy Drizzle | http://openweathermap.org/img/wn/09n@2x.png | |
| 56 | Light Freezing Drizzle | http://openweathermap.org/img/wn/09d@2x.png | Light Freezing Drizzle | http://openweathermap.org/img/wn/09n@2x.png | |
| 57 | Freezing Drizzle | http://openweathermap.org/img/wn/09d@2x.png | Freezing Drizzle | http://openweathermap.org/img/wn/09n@2x.png | |
| 61 | Light Rain | http://openweathermap.org/img/wn/10d@2x.png | Light Rain | http://openweathermap.org/img/wn/10n@2x.png | |
| 63 | Rain | http://openweathermap.org/img/wn/10d@2x.png | Rain | http://openweathermap.org/img/wn/10n@2x.png | |
| 65 | Heavy Rain | http://openweathermap.org/img/wn/10d@2x.png | Heavy Rain | http://openweathermap.org/img/wn/10n@2x.png | |
| 66 | Light Freezing Rain | http://openweathermap.org/img/wn/10d@2x.png | Light Freezing Rain | http://openweathermap.org/img/wn/10n@2x.png | |
| 67 | Freezing Rain | http://openweathermap.org/img/wn/10d@2x.png | Freezing Rain | http://openweathermap.org/img/wn/10n@2x.png | |
| 71 | Light Snow | http://openweathermap.org/img/wn/13d@2x.png | Light Snow | http://openweathermap.org/img/wn/13n@2x.png | |
| 73 | Snow | http://openweathermap.org/img/wn/13d@2x.png | Snow | http://openweathermap.org/img/wn/13n@2x.png | |
| 75 | Heavy Snow | http://openweathermap.org/img/wn/13d@2x.png | Heavy Snow | http://openweathermap.org/img/wn/13n@2x.png | |
| 77 | Snow Grains | http://openweathermap.org/img/wn/13d@2x.png | Snow Grains | http://openweathermap.org/img/wn/13n@2x.png | |
| 80 | Light Showers | http://openweathermap.org/img/wn/09d@2x.png | Light Showers | http://openweathermap.org/img/wn/09n@2x.png | |
| 81 | Showers | http://openweathermap.org/img/wn/09d@2x.png | Showers | http://openweathermap.org/img/wn/09n@2x.png | |
| 82 | Heavy Showers | http://openweathermap.org/img/wn/09d@2x.png | Heavy Showers | http://openweathermap.org/img/wn/09n@2x.png | |
| 85 | Light Snow Showers | http://openweathermap.org/img/wn/13d@2x.png | Light Snow Showers | http://openweathermap.org/img/wn/13n@2x.png | |
| 86 | Snow Showers | http://openweathermap.org/img/wn/13d@2x.png | Snow Showers | http://openweathermap.org/img/wn/13n@2x.png | |
| 95 | Thunderstorm | http://openweathermap.org/img/wn/11d@2x.png | Thunderstorm | http://openweathermap.org/img/wn/11n@2x.png | |
| 96 | Light Thunderstorms With Hail | http://openweathermap.org/img/wn/11d@2x.png | Light Thunderstorms With Hail | http://openweathermap.org/img/wn/11n@2x.png | |
| 99 | Thunderstorm With Hail | http://openweathermap.org/img/wn/11d@2x.png | Thunderstorm With Hail | http://openweathermap.org/img/wn/11n@2x.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment