This assignment required me to leverage csv, randomly generated numbers, and an online API to conduct exploratory analyses on weather patterns across hundreds of cities.
After importing my dependencies and entering my API key into a file I created called "api_keys", I initialized some list variables, and used numpy to generate random latitudes and longitudes to fill one of the lists. Then, I used citypy to find the nearest city to each latitude and logitude. Next, I used the openweathermap API to obtain data for several weather dimensions across a single timepoint. Finally, I created scatterplots of those weather dimensions against the cities generated earlier. For a fully deployed webpage that showcases the scatterplots created in this notebook, click here.
# Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import time
from pprint import pprint
# Import API key
from api_keys import api_key
# Incorporated citipy to determine city based on latitude and longitude
from citipy import citipy
# Output File (CSV)
output_data_file = "output_data/cities.csv"
# Range of latitudes and longitudes
lat_range = (-90, 90)
lng_range = (-180, 180)
print(api_key)
# List for holding lat_lngs and cities
lat_lngs = []
cities = []
# Create a set of random lat and lng combinations
lats = np.random.uniform(low=-90.000, high=90.000, size=1500)
lngs = np.random.uniform(low=-180.000, high=180.000, size=1500)
lat_lngs = zip(lats, lngs)
# Identify nearest city for each lat, lng combination
for lat_lng in lat_lngs:
city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name
# If the city is unique, then add it to a our cities list
if city not in cities:
cities.append(city)
# Print the city count to confirm sufficient count
len(cities)
print(cities)
url = "http://api.openweathermap.org/data/2.5/weather?"
units = "metric"
# Build partial query URL
query_url = f"{url}appid={api_key}&units={units}&q="
print(query_url)
city_list = []
lat_list = []
lng_list = []
max_temp_list = []
humidity_list = []
cloudiness_list = []
windspeed_list = []
counter = 0
record_counter = 0
setcount = 1
for city in cities:
counter += 1
if (counter % 50 == 0):
setcount += 1
record_counter = 0
print(setcount)
print(record_counter)
print(counter)
record_counter += 1
print("processing Record " + str(record_counter) + " of Set " + str(setcount) + " | " + city)
try:
weather_response = requests.get(query_url + city).json()
#pprint(weather_response)
city_list.append(weather_response['name'])
lat_list.append(weather_response['coord']['lat'])
lng_list.append(weather_response['coord']['lon'])
max_temp_list.append(weather_response['main']['temp_max'])
humidity_list.append(weather_response['main']['humidity'])
cloudiness_list.append(weather_response['clouds']['all'])
windspeed_list.append(weather_response['wind']['speed'])
except:
print("City not found: " + city)
print(len(weather_response))
print(len(city_list))
print(len(lat_list))
print(len(lng_list))
print(len(max_temp_list))
print(len(humidity_list))
print(len(cloudiness_list))
print(len(windspeed_list))
weather_dict = {"city":city_list,
"lat":lat_list,
"lng":lng_list,
"max_temp":max_temp_list,
"humidity":humidity_list,
"cloudiness":cloudiness_list,
"windspeed":windspeed_list
}
weather_df = pd.DataFrame(weather_dict)
weather_df.head()
weather_df.to_html("table.html")
plt.scatter(weather_df['lat'], weather_df['max_temp'], edgecolor="black", linewidths=1, marker="o",
alpha=0.8, label="Cities")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.title("City Latitude vs. Max Temperature (04/03/2019)")
plt.savefig("Max_temp.png")
plt.scatter(weather_df['lat'], weather_df['humidity'], edgecolor="black", linewidths=1, marker="o",
alpha=0.8, label="Cities")
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.title("City Latitude vs. Humidity (04/03/2019)")
plt.savefig("Humidity.png")
plt.scatter(weather_df['lat'], weather_df['cloudiness'], edgecolor="black", linewidths=1, marker="o",
alpha=0.8, label="Cities")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.title("City Latitude vs. Cloudiness (04/03/2019)")
plt.savefig("Cloudiness.png")
plt.scatter(weather_df['lat'], weather_df['windspeed'], edgecolor="black", linewidths=1, marker="o",
alpha=0.8, label="Cities")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")
plt.title("City Latitude vs. Wind Speed (04/03/2019)")
plt.savefig("Wind_Speed.png")