<jupyter_start><jupyter_text>WeatherPy---- Note* Instructions have been included for each segment. You do not have to follow them exactly, but they are included to help you think through the steps.<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import time
import json
import pprint
from scipy.stats import linregress
import datetime as dt
import scipy.stats as st
import os

# Import API key
from api_keys import weather_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"
output_log_file = "output_data/cities.log"

# only call openweather if we want to - delete to file to enable
if os.path.exists(output_data_file):
    skip_weather = True
    print("*** Using existing data ***")
else:
    skip_weather = False
    print("*** Getting new data ***")

# Range of latitudes and longitudes
lat_range = (-90, 90)
lng_range = (-180, 180)

# Current date
d = dt.date.today()
analysis_date = (f"({d.month}/{d.day}/{d.year})")
print(analysis_date)

max_limit = 1000
required_length = 500<jupyter_output>*** Getting new data ***
(1/22/2020)<jupyter_text>Generate Cities List<jupyter_code># 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
i = 0
for lat_lng in lat_lngs:
    city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name
    i+=1
    if i > max_limit:
        break

    # 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
length = len(cities)
print(length)

if length < required_length:
    raise Exception(f"not enough cities: {length}: need: {required_length}")<jupyter_output>459<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># TODO: make a def
# city_name = short_cities[0]
# url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={weather_api_key}"
# print(url)

# redefine params
# params = {
#     "q": city_name,
#     "appid": weather_api_key
# }

# Build URL using the Google Maps API
base_url = "https://api.openweathermap.org/data/2.5/weather"
#print(cities)
# Run request
# response = requests.get(base_url, params).json()

'''We only need these
City	Cloudiness	Country	Date	Humidity	Lat	Lng	Max Temp	Wind Speed'''

if skip_weather == True:
    df = pd.read_csv(output_data_file)
else:
    i = 1
    df = pd.DataFrame()
    ind = []

    log = open(output_log_file, "w")
    try:
        for city_name in cities:
            try:
                url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={weather_api_key}"
                response = requests.get(url).json()
                log.write("")
                #print("==============================")
                print(f"Processing City city {i} with name: {city_name}")
                log.write("==============================")
                log.write(f"Processing City city {i} with name: {city_name}")
                # json.dumps(response, indent=4, sort_keys=True)
                pprint.pprint(response, log)
                log.write("")

                # extract the data
                ind.append(i-1) # the index for the DataFrame in case we want to change it later
                cloudiness = response['clouds']['all']
                country = response['sys']['country']
                date = response['dt']
                humidity = response['main']['humidity']
                lat = response['coord']['lat']
                lng = response['coord']['lon']
                kmax_temp = response['main']['temp_max']
                max_temp = ((kmax_temp * (9/5)) - 459.67)
                kwind_speed = response['wind']['speed']
                wind_speed = kwind_speed * 1.6

                data = [city_name, cloudiness, country, date, humidity, lat, lng, max_temp, wind_speed]
                print(data)
                s = pd.Series(data)
                df = df.append(s, ignore_index=True)

                i+=1
            except(KeyError):
                print(f"Error on city {city_name}, skipping")
    finally:
        log.close()
    df.columns = ['City', 'Cloudiness', 'Country', 'Date', 'Humidity', 'Lat', 'Lng', 'Max Temp', 'Wind Speed']
    df['Date'] = df['Date'].apply(np.int64)

#print(df.dtypes)
df.head()<jupyter_output><empty_output><jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>df.to_csv(output_data_file)
print(len(df))
df<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># function to do each plot with different variables
def plot(pdf, x, y, title, xlabel, ylabel, regress = False, hem = ""):

    # Generate a scatter plot of mouse weight versus average tumor volume for the Capomulin regimen
    # Annotate in the regression
    x_axis = pdf[x]
    y_axis = pdf[y]

    df.head(600)
#     plt.scatter(x_axis, y_axis, marker="o", facecolors="red", edgecolors="black",
#                 s=x_axis, alpha=0.75)

    plt.scatter(x_axis, y_axis, marker="o", facecolors="red", edgecolors="black", alpha=0.75)

    if regress:
        # Calculate the correlation coefficient and linear regression model for mouse weight and 
        # average tumor volume for the Capomulin regimen
        correlation = st.pearsonr(x_axis, y_axis)
        print(f"The correlation between both factors is {round(correlation[0],2)}")
        # get and add the regression
        (slope, intercept, rvalue, pvalue, stderr) = linregress(x_axis, y_axis)
        regress_values = x_axis * slope + intercept
        line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
        plt.plot(x_axis,regress_values,"r-")
        x_mean = x_axis.mean()
        y_mean = y_axis.mean()
        plt.annotate(line_eq,(x_mean,y_mean),fontsize=15,color="black")

    plt.title(f"{title} {analysis_date}")
    plt.xlabel(f"{xlabel}")
    plt.ylabel(f"{ylabel}")
    if regress:
        l = "_r"
    else:
        l = ""
    
    xs = x.strip()
    ys = y.strip()
    plt.savefig(f"output_data/fig_{xs}_{ys}{l}{hem}.png")<jupyter_output><empty_output><jupyter_text>Latitude vs. Temperature Plot<jupyter_code>plot(df, 'Lat', 'Max Temp', "City Latitude vs. Max Temperature", 'Latitude', 'Max Temperature (F)')<jupyter_output><empty_output><jupyter_text>This is giving evidence that the southern hemisphere is warmer at this time of year. Of course the temperature peaks at the equator.  Latitude vs. Humidity Plot<jupyter_code>plot(df, 'Lat', 'Humidity', 'City Latitude vs. Humidity', 'Latitude', 'Humidity %')<jupyter_output><empty_output><jupyter_text>This demonstrates that it is more humid in the northern hemisphere at this time of year.  Latitude vs. Cloudiness Plot<jupyter_code>plot(df, 'Lat', 'Cloudiness', 'Cloudiness', 'Latitude', 'Cloudiness %')<jupyter_output><empty_output><jupyter_text>This is looking to show a correlation between cloudiness and latitude, but it doesn't appear to.  Latitude vs. Wind Speed Plot<jupyter_code>plot(df, 'Lat', 'Wind Speed', 'City Latitude vs. Wind Speed', 'Latitude', 'Wind Speed (mph)')<jupyter_output><empty_output><jupyter_text>There could be an indication that there is more wind in the northern hemisphere.  Linear Regression<jupyter_code># Create Northern and Southern Hemisphere DataFrames
ndf = df[df['Lat'] >= 0]
sdf = df[df['Lat'] < 0]<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>plot(ndf, 'Lat', 'Max Temp', "North: City Latitude vs. Max Temperature", 'Latitude', 'Max Temperature (F)', regress = True, hem = "_n")<jupyter_output><empty_output><jupyter_text>The correlation quantifies the negative correlation between temp and latitude. The farther north you go, the colder it is.   Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>plot(sdf, 'Lat', 'Max Temp', "South: City Latitude vs. Max Temperature", 'Latitude', 'Max Temperature (F)', regress = True, hem = "_s")<jupyter_output><empty_output><jupyter_text>In the south, the farther north you go, the warmer it gets.   Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>plot(ndf, 'Lat', 'Humidity', 'North: City Latitude vs. Humidity', 'Latitude', 'Humidity %', regress = True, hem = "_n")<jupyter_output><empty_output><jupyter_text>There a slight positive correlation between humidity and latitude in the north. The farther north you go, it is slightly more humid.   Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>plot(sdf, 'Lat', 'Humidity', 'South: City Latitude vs. Humidity', 'Latitude', 'Humidity %', regress = True, hem = "_s")<jupyter_output><empty_output><jupyter_text>There a slight positive correlation between humidity and latitude in the north. The farther north you go, it is slightly more humid.   Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>plot(ndf, 'Lat', 'Cloudiness', 'North: Cloudiness', 'Latitude', 'Cloudiness %', regress = True, hem = "_n")<jupyter_output><empty_output><jupyter_text>There a slight positive correlation between cloudiness and latitude in the north. I don't see it.   Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>plot(sdf, 'Lat', 'Cloudiness', 'South: Cloudiness', 'Latitude', 'Cloudiness %', regress = True, hem = "_s")<jupyter_output><empty_output><jupyter_text>In the south, there is no measurable correlation between cloudiness and latitude.   Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>plot(ndf, 'Lat', 'Wind Speed', 'North: City Latitude vs. Wind Speed', 'Latitude', 'Wind Speed (mph)', regress = True, hem = "_n")<jupyter_output><empty_output><jupyter_text>There are some interesting outliers in the north as far as wind speed. This gives a slight correlation.   Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>plot(sdf, 'Lat', 'Wind Speed', 'South: City Latitude vs. Wind Speed', 'Latitude', 'Wind Speed (mph)', regress = True, hem = "_s")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12459381673010862, 'fraction_numerical': 0.00928418902608857, 'mean_word_length': 3.936755270394134, 'pattern_counts': {'":': 2, '<': 73, '<?xml version=': 0, '>': 73, 'https://': 3, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '171580', 'license': 'ADSL', 'n_tokens_mistral': 3596, 'n_tokens_neox': 3375, 'n_words': 1036, 'path': 'WeatherPy/.ipynb_checkpoints/WeatherPy-checkpoint.ipynb', 'repo_name': 'ddurnell/python-api-challenge', 'size': '10771'}