Created
April 8, 2025 12:39
-
-
Save antichaos/7e406d1d7597759ba28a1b687103ea3a to your computer and use it in GitHub Desktop.
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 requests | |
| import pandas as pd | |
| from datetime import datetime, timezone | |
| api_key = 'xxxxxxxxxx' # Replace with your OpenWeatherMap API key | |
| location_name = "San Diego" | |
| lon = -117.16017202682153 | |
| lat = 32.7054914849287 | |
| def wind_deg_to_windrose(deg): | |
| # convert degrees to windrose direction | |
| directions = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'] | |
| idx = int((deg + 11.25) / 22.5) % 16 | |
| return directions[idx] | |
| def convert_data(data): | |
| # convert weather data from OWM to a table expected by Tableau | |
| # Create a list to hold the daily data | |
| daily_data = [] | |
| for day in data['daily']: | |
| # Extract the date and temperature information | |
| day_data = { | |
| 'date': datetime.fromtimestamp(day['dt'], tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S'), | |
| 'temp_min': day['temp']['min'], | |
| 'temp_max': day['temp']['max'], | |
| 'temp_day': day['temp']['day'], | |
| 'temp_night': day['temp']['night'], | |
| 'temp_eve': day['temp']['eve'], | |
| 'temp_morn': day['temp']['morn'], | |
| 'feels_like_day': day['feels_like']['day'], | |
| 'feels_like_night': day['feels_like']['night'], | |
| 'feels_like_eve': day['feels_like']['eve'], | |
| 'feels_like_morn': day['feels_like']['morn'], | |
| 'pressure': day['pressure'], | |
| 'humidity': day['humidity'] / 100, | |
| 'dew_point': day['dew_point'], | |
| 'wind_speed': day['wind_speed'], | |
| 'wind_deg': day['wind_deg'], | |
| 'wind_gust': day['wind_gust'], | |
| 'weather_0_id': day['weather'][0]['id'], | |
| 'weather_0_main': day['weather'][0]['main'], | |
| 'weather_0_description': day['weather'][0]['description'], | |
| 'weather_0_icon': day['weather'][0]['icon'], | |
| 'weather_0_icon_url': 'https://openweathermap.org/img/wn/' + day['weather'][0]['icon'] + '@2x.png', | |
| 'weather_clouds': day['clouds'], | |
| 'weather_pop': day['pop'], | |
| 'weather_uvi': day['uvi'], | |
| 'sunrise': datetime.fromtimestamp(day['sunrise'], tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S'), | |
| 'sunset': datetime.fromtimestamp(day['sunset'], tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S'), | |
| 'moonrise': datetime.fromtimestamp(day['moonrise'], tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S'), | |
| 'moonset': datetime.fromtimestamp(day['moonset'], tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S'), | |
| 'moon_phase': day['moon_phase'], | |
| 'summary': day['summary'] | |
| } | |
| daily_data.append(day_data) | |
| # Convert the list of dictionaries to a DataFrame | |
| df = pd.DataFrame(daily_data) | |
| # Convert 'date' column to datetime type | |
| df['date'] = pd.to_datetime(df['date']) | |
| # Convert datetime fields to string to make them JSON serializable | |
| df['date'] = df['date'].dt.strftime('%Y-%m-%d %H:%M:%S') | |
| # Add all temperature conversions | |
| temp_fields = ['temp_min', 'temp_max', 'temp_day', 'temp_night', 'temp_eve', 'temp_morn', | |
| 'feels_like_day', 'feels_like_night', 'feels_like_eve', 'feels_like_morn', 'dew_point'] | |
| for field in temp_fields: | |
| df[f'{field}_celsius'] = df[field] - 273.15 | |
| df[f'{field}_fahrenheit'] = (df[field] - 273.15) * 9 / 5 + 32 | |
| # Convert wind_deg to windrose location | |
| df['windrose'] = df['wind_deg'].apply(wind_deg_to_windrose) | |
| return df.to_dict(orient='list') | |
| def get_weather(location_name, lon, lat, api_key): | |
| # get the weather data from OWM | |
| url = f"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude=hourly&appid={api_key}" | |
| response = requests.get(url) | |
| data = response.json() | |
| if response.status_code == 200: | |
| return data | |
| else: | |
| return data.get('message', 'Error fetching data') | |
| # Fetch weather data and process it | |
| weather_data = get_weather(location_name, lon, lat, api_key) | |
| tabdata = convert_data(weather_data) | |
| # Call the function and print the result | |
| return(tabdata) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment