<jupyter_start><jupyter_text>WeatherPy---- Analysis <NAME>* Strangely, around 60°N the temperature of sampled cities seems to follow two trendlines; there is a noticeable cluster that recorded higher temperature than others on the same latitude. Since max temperature is a measure of the highest recorded temperature of the day of analysis, cities that are located in different western/eastern hemispheres might have different data due to time zone differences.  This results in cities with as much of the day already over (with its daily max temperature already recorded) being pitted against cities whose days have just begun (and still haven't warmed up due to lack of sunlight).* Humidity seems to be the highest closest to the equator.  There is a bell curve shape on the scatterplot, with lows of around 80% humidity at the equator (0° latitude) whereas there are several cities with 0% humidity at 60°N and around 30% humidity around 30°S.* Temperatures from the southern hemisphere (which is currently experiecing summer) are compareble to to those of the equator while the northern hemisphere (currently winter) is noticebly colder.  Even so, there is still a slight trend of increasing temperature from the south towards the equator.  Temperatures appear to start dropping after 10°N (rather than at exactly 0°).<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import time

# Import API key
# NOTE - file not uploaded with rest of hw - please use your own API key
from api_keys import weather_api_key

# Incorporated citipy to determine city based on latitude and longitude
from citipy import citipy<jupyter_output><empty_output><jupyter_text>Generate Cities List<jupyter_code># List for holding latitude/longitude and city names
lat_lngs = []
cities = []

# Dataset is fleshed out by sampling different latitude/longitude, to account for all hemispheres
coordinate_range = pd.DataFrame({
    "lat_low": [-90, -90, 0, 0],
    "lat_high": [0, 0, 90, 90],
    "lng_low": [-180, 0, -180, 0],
    "lng_high": [0, 180, 0, 180]})

# Identify nearest city for each latitude/longitude combination
for x in range(0,4):
    
    lats = np.random.uniform(low=coordinate_range.loc[x, 'lat_low'], high=coordinate_range.loc[x, 'lat_high'], size=350)
    lngs = np.random.uniform(low=coordinate_range.loc[x, 'lng_low'], high=coordinate_range.loc[x, 'lng_high'], size=350)
    lat_lngs = zip(lats, lngs)

    for lat_lng in lat_lngs:
        city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name
    
        if city not in cities:
            cities.append(city)
            
# Check sample size        
print(f"sample size: {len(cities)} cities")<jupyter_output>sample size: 566 cities<jupyter_text>Perform API Calls* Perform a weather check on each city using a series of successive API calls.* Include a print log of each city as it'sbeing processed (with the city number and city name).<jupyter_code># Lists to hold results

cloudiness = []
country = []
date = []
humidity = []
latitude = []
longitude = []
max_temp = []
wind_speed = []

url = "http://api.openweathermap.org/data/2.5/weather?"

print("Beginning Data Retrieval")     
print("-----------------------------")

for city in cities:
    query_url = url + "appid=" + weather_api_key + "&q=" + city + "&units=imperial"
    weather_response = requests.get(query_url)
    weather_json = weather_response.json()
    
    print(f"Processing dataset {cities.index(city)+1} of {len(cities)} - {city}")
    
    # Retrieve data for various categories
    try:
        cloudiness.append(weather_json["clouds"]["all"])
    except (KeyError, IndexError):
        print(f"- missing data for {city} - cloudiness")
        cloudiness.append(None)
        
    try:
        country.append(weather_json["sys"]["country"])
    except (KeyError, IndexError):
        print(f"- missing data for {city} - country")
        country.append(None)

    try:
        date.append(weather_json["dt"])
    except (KeyError, IndexError):
        print(f"- missing data for {city} - date")
        date.append(None) 

    try:
        humidity.append(weather_json["main"]["humidity"])
    except (KeyError, IndexError):
        print(f"- missing data for {city} - humidity")
        humidity.append(None)
        
    try:
        latitude.append(weather_json["coord"]["lat"])
    except (KeyError, IndexError):
        print(f"- missing data for {city} - latitude")
        latitude.append(None)
        
    try:
        longitude.append(weather_json["coord"]["lon"])
    except (KeyError, IndexError):
        print(f"- missing data for {city} - longitude")
        longitude.append(None)
        
    try:
        max_temp.append(weather_json["main"]["temp_max"])
    except (KeyError, IndexError):
        print(f"- missing data for {city} - maximum temperature")
        max_temp.append(None)
        
    try:
        wind_speed.append(weather_json["wind"]["speed"])
    except (KeyError, IndexError):
        print(f"- missing data for {city} - wind speed")
        wind_speed.append(None)
        
            
print("-----------------------------")
print("Data Retrieval Finished")<jupyter_output>Beginning Data Retrieval
-----------------------------
Processing dataset 1 of 566 - chuy
Processing dataset 2 of 566 - ushuaia
Processing dataset 3 of 566 - rikitea
Processing dataset 4 of 566 - punta arenas
Processing dataset 5 of 566 - mataura
Processing dataset 6 of 566 - mar del plata
Processing dataset 7 of 566 - ipira
Processing dataset 8 of 566 - coihaique
Processing dataset 9 of 566 - puerto ayora
Processing dataset 10 of 566 - avarua
Processing dataset 11 of 566 - castro
Processing dataset 12 of 566 - georgetown
Processing dataset 13 of 566 - maragogi
Processing dataset 14 of 566 - hermanus
Processing dataset 15 of 566 - lebu
Processing dataset 16 of 566 - takoradi
Processing dataset 17 of 566 - calama
Processing dataset 18 of 566 - vila velha
Processing dataset 19 of 566 - arraial do cabo
Processing dataset 20 of 566 - samusu
- missing data for samusu - cloudiness
- missing data for samusu - country
- missing data for samusu - date
- missing data for samusu - humidity
- miss[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv* Display the DataFrame<jupyter_code># Dataframe from various lists of data

weather_data = pd.DataFrame({
    "City": cities,
    "Cloudiness" : cloudiness,
    "Country": country,
    "Date": date,
    "Humidity": humidity,
    "Latitude": latitude,
    "Longitude": longitude,
    "Max Temp": max_temp,
    "Wind Speed": wind_speed
})

weather_data.head()
# Export results to CSV file
weather_data.to_csv("output_data/cities.csv", index=False, header=True)<jupyter_output><empty_output><jupyter_text>Plotting the Data* Use proper labeling of the plots using plot titles (including date of analysis) and axes labels.* Save the plotted figures as .pngs<jupyter_code># Pull accurate unix timestamp based on compiled dates (which is presumably slightly variable due to time zones)

import datetime

date_of_analysis = weather_data["Date"].mode()

date_from_unix = datetime.datetime.fromtimestamp(date_of_analysis)

date_reformatted = date_from_unix.strftime('%m/%d/%y')

print(f"date of analysis: {date_reformatted}")<jupyter_output>date of analysis: 02/03/19<jupyter_text>Latitude vs. Temperature Plot<jupyter_code>plt.scatter(weather_data["Latitude"], weather_data["Max Temp"],
            marker="o", facecolors="royalblue", edgecolors="black", alpha=0.6)

plt.title(f"City Latitude vs Max Temperature ({date_reformatted})", fontsize=14)
plt.xlabel("Latitude", fontsize=12)
plt.ylabel("Max Temperature (°F)", fontsize=12)
plt.grid()

plt.savefig("output_data/city_lat_vs_max_temp.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>plt.scatter(weather_data["Latitude"], weather_data["Humidity"],
            marker="o", facecolors="royalblue", edgecolors="black", alpha=0.6)

plt.title(f"City Latitude vs Humidity ({date_reformatted})", fontsize=16)
plt.xlabel("Latitude", fontsize=12)
plt.ylabel("Humidity (%)", fontsize=12)
plt.grid()

plt.savefig("output_data/city_lat_vs_humidity.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>plt.scatter(weather_data["Latitude"], weather_data["Cloudiness"], marker="o",
            facecolors="royalblue", edgecolors="black", alpha=0.6)

plt.title(f"City Latitude vs Cloudiness ({date_reformatted})", fontsize=16)
plt.xlabel("Latitude", fontsize=12)
plt.ylabel("Cloudiness (%)", fontsize=12)
plt.grid()

plt.savefig("output_data/city_lat_vs_cloudiness.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>plt.scatter(weather_data["Latitude"], weather_data["Wind Speed"], marker="o",
            facecolors="royalblue", edgecolors="black", alpha=0.6)

plt.title(f"City Latitude vs Wind Speed ({date_reformatted})", fontsize=16)
plt.xlabel("Latitude", fontsize=12)
plt.ylabel("Wind Speed (MPH))", fontsize=12)
plt.grid()

plt.savefig("output_data/city_lat_vs_wind_speed.png")

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11646586345381527, 'fraction_numerical': 0.02040594811679149, 'mean_word_length': 4.116046640755136, 'pattern_counts': {'":': 12, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '541311', 'license': 'MIT', 'n_tokens_mistral': 3059, 'n_tokens_neox': 2821, 'n_words': 882, 'path': 'WeatherPy_final_wendychau.ipynb', 'repo_name': 'wendychau/HW-6-python-APIs', 'size': '9217'}