# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernel_info:
#     name: python3
#   kernelspec:
#     display_name: PythonData
#     language: python
#     name: pythondata
# ---

# # 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.

# +
# Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import time
from scipy.stats import linregress

# 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"

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

# +



# -

# ## Generate Cities List

# +
# List for holding lat_lngs and cities
lat_lngs = []
cities = []

# Create a set of random lat and lng combinations
lats = np.random.uniform(lat_range[0], lat_range[1], size=1500)
lngs = np.random.uniform(lng_range[0], lng_range[1], 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)
# -

cities

# ### 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).
#

# +
print('Beginning Data Retrieval\n-----------------------------\n')

weather_list = []
rcd = 0
sets = 1

for city in cities:

    rcd += 1
    print(f'Processing Record {rcd} of Set {sets} | {city}')
    
    if rcd == 50:
        rcd = 0
        sets += 1
        
    url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={weather_api_key}&units=imperial'
    
    weather = requests.get(url).json()
    
    try:
        weather_list.append({
            'city': city,
            'lat': weather['coord']['lat'],
            'lng': weather['coord']['lon'],
            'temp': weather['main']['temp_max'],
            'humidity': weather['main']['humidity'],
            'wind':weather['wind']['speed'],
            'cloudiness':weather['clouds']['all'],
            'country':weather['sys']['country'],
            'date':weather['dt']
        })
        
    except:
        print('City not found. Skipping...')
        pass
    
print('-----------------------------\nData Retrieval Complete\n-----------------------------')
        
# -

# ### Convert Raw Data to DataFrame
# * Export the city data into a .csv.
# * Display the DataFrame

city_data = pd.DataFrame(weather_list)
city_data.date = city_data.date.map(time.ctime)
city_data.to_csv('output_data/city_data.csv')
city_data.head()

city_data.describe()

# ## Inspect the data and remove the cities where the humidity > 100%.
# ----
# Skip this step if there are no cities that have humidity > 100%. 

#  Get the indices of cities that have humidity over 100%.
city_data = city_data.loc[city_data["humidity"]<= 100]
city_data.head()

# Make a new DataFrame equal to the city data to drop all humidity outliers by index.
# Passing "inplace=False" will make a copy of the city_data DataFrame, which we call "clean_city_data".


# ## 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.

# ## Latitude vs. Temperature Plot

plt.grid()
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title('City Latitude vs Max Temperature (F) (04/25/2021)')
plt.scatter(city_data['lat'],city_data['temp'],edgecolor='black', linewidths=1,)
plt.savefig("output_data/city_lat_vs_max_temp.png")

# ## Latitude vs. Humidity Plot

plt.grid()
plt.xlabel('Latitude')
plt.ylabel('Humidity')
plt.title('City Latitude vs Humidity (04/25/2021)')
plt.scatter(city_data['lat'],city_data['humidity'],edgecolor='black', linewidths=1,)
plt.savefig("output_data/City_Lat_vs_Humidity.png")

# ## Latitude vs. Cloudiness Plot

plt.grid()
plt.xlabel('Latitude')
plt.ylabel('Cloudiness')
plt.title('City Latitude vs Cloudiness (04/25/2021)')
plt.scatter(city_data['lat'],city_data['cloudiness'],edgecolor='black', linewidths=1,)
plt.savefig("output_data/City_Lat_vs Cloudiness_.png")

# ## Latitude vs. Wind Speed Plot

plt.grid()
plt.xlabel('Latitude')
plt.ylabel('Wind Speed')
plt.title('City Latitude vs Wind Speed (04/25/2021)')
plt.scatter(city_data['lat'],city_data['wind'],edgecolor='black', linewidths=1,)
plt.savefig("output_data/City_Lat_vs_Wind_Speed.png")

# ## Linear Regression

# ####  Northern Hemisphere - Max Temp vs. Latitude Linear Regression

northern_pd = city_data.loc[city_data["lat"]>= 0]
southern_pd = city_data.loc[city_data["lat"]<= 0]

# establish linear regression values = linregress(city_data ("lat","temp".values)
slope, intercept, rValue, pValue, stderror = linregress(northern_pd['lat'], northern_pd['temp'])
print(f'The St. Pearson Correlation Coefficient between both factors is {round(rValue, 2)}')
# linear regression line 
regress = slope*(northern_pd['lat']) + intercept
regresstring = "y = " + str(round(slope, 2)) + 'x + ' + str(round(intercept, 2))
plt.annotate(regresstring, xy=(5,30), fontsize=13, color="blue")     
plt.plot(northern_pd['lat'],regress,"-",color="blue") 
plt.scatter(northern_pd['lat'],northern_pd['temp'], marker = "o", facecolors = 'green', edgecolor='black')
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title('Northern Hemisphere - Max Temp vs. Latitude Linear Regression')
plt.show()
plt.savefig("output_data/Northern_Hemisphere_Max_Temp_vs_Latitude_Linear_Regression.png")

# ####  Southern Hemisphere - Max Temp vs. Latitude Linear Regression

# establish linear regression values = linregress(city_data ("lat","temp".values)
slope, intercept, rValue, pValue, stderror = linregress(southern_pd['lat'], southern_pd['temp'])
print(f'The St. Pearson Correlation Coefficient between both factors is {round(rValue, 2)}')
# linear regression line 
regress = slope*(southern_pd['lat']) + intercept
regresstring = "y = " + str(round(slope, 2)) + 'x + ' + str(round(intercept, 2))
plt.annotate(regresstring, xy=(-20,48), fontsize=13, color="blue")     
plt.plot(southern_pd['lat'],regress,"-",color="blue") 
plt.scatter(southern_pd['lat'],southern_pd['temp'], marker = "o", facecolors = 'green', edgecolor='black')
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title('Southern Hemisphere - Max Temp vs. Latitude Linear Regression')
plt.show()
plt.savefig("output_data/Southern_Hemisphere_Max_Temp_vs_Latitude_Linear_Regression.png")

# ####  Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression

# establish linear regression values = linregress(city_data ("lat","temp".values)
slope, intercept, rValue, pValue, stderror = linregress(northern_pd['lat'], northern_pd['humidity'])
print(f'The St. Pearson Correlation Coefficient between both factors is {round(rValue, 2)}')
# linear regression line 
regress = slope*(northern_pd['lat']) + intercept
regresstring = "y = " + str(round(slope, 2)) + 'x + ' + str(round(intercept, 2))
plt.annotate(regresstring, xy=(45,10), fontsize=13, color="blue")     
plt.plot(northern_pd['lat'],regress,"-",color="blue") 
plt.scatter(northern_pd['lat'],northern_pd['humidity'], marker = "o", facecolors = 'green', edgecolor='black')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title('Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression')
plt.show()
plt.savefig("output_data/Northern_Hemisphere_Humidity_vs_Latitude_Linear_Regression.png")

# ####  Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression

# +
# establish linear regression values 
slope, intercept, rValue, pValue, stderror = linregress(southern_pd['lat'], southern_pd['humidity'])
print(f'The St. Pearson Correlation Coefficient between both factors is {round(rValue, 2)}')

# linear regression line 
regress = slope*(southern_pd['lat']) + intercept
regresstring = "y = " + str(round(slope, 2)) + 'x + ' + str(round(intercept, 2))
plt.annotate(regresstring, xy=(-50,20), fontsize=13, color="blue")     
plt.plot(southern_pd['lat'],regress,"-",color="blue") 
plt.scatter(southern_pd['lat'],southern_pd['humidity'], marker = "o", facecolors = 'green', edgecolor='black')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title('Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression')
plt.annotate(regresstring, xy=(5,30), fontsize=13, color="blue") 
plt.show()
plt.savefig("output_data/Southern_Hemisphere_Humidity_vs_Latitude_Linear_Regression.png")

# -

# ####  Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression

establish_linear_regression_values = linregress(city_data["lat"], city_data["cloudiness"])
slope, intercept, rValue, pValue, stderror = linregress(northern_pd['lat'], northern_pd['cloudiness'])
print(f'The St. Pearson Correlation Coefficient between both factors is {round(rValue, 2)}')
# linear regression line 
regress = slope*(northern_pd['lat']) + intercept
regresstring = "y = " + str(round(slope, 2)) + 'x + ' + str(round(intercept, 2))
plt.annotate(regresstring, xy=(50,52), fontsize=13, color="blue")     
plt.plot(northern_pd['lat'],regress,"-",color="blue") 
plt.scatter(northern_pd['lat'], northern_pd['cloudiness'], marker = "o", facecolors = 'green', edgecolor='black')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.title('Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression')
plt.show()
plt.savefig("output_data/Northern_Hemisphere_Cloudiness_vs_Latitude_Linear_Regression.png")

# ####  Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression

# establish linear regression values 
slope, intercept, rValue, pValue, stderror = linregress(southern_pd['lat'], southern_pd['cloudiness'])
print(f'The St. Pearson Correlation Coefficient between both factors is {round(rValue, 2)}')
# linear regression line 
regress = slope*(southern_pd['lat']) + intercept
regresstring = "y = " + str(round(slope, 2)) + 'x + ' + str(round(intercept, 2))
plt.annotate(regresstring, xy=(-57,50), fontsize=13, color="blue")     
plt.plot(southern_pd['lat'],regress,"-",color="blue") 
plt.scatter(southern_pd['lat'],southern_pd['cloudiness'], marker = "o", facecolors = 'green', edgecolor='black')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness')
plt.title('Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression')
plt.show()
plt.savefig("output_data/Southern_Hemisphere_Cloudiness_vs_Latitude_Linear_Regression.png")

# ####  Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression

# establish linear regression values = linregress(city_data ("lat","wind".values)
slope, intercept, rValue, pValue, stderror = linregress(northern_pd['lat'], northern_pd['wind'])
print(f'The St. Pearson Correlation Coefficient between both factors is {round(rValue, 2)}')
# linear regression line 
regress = slope*(northern_pd['lat']) + intercept
regresstring = "y = " + str(round(slope, 2)) + 'x + ' + str(round(intercept, 2))
plt.annotate(regresstring, xy=(45,30), fontsize=13, color="blue")     
plt.plot(northern_pd['lat'],regress,"-",color="blue") 
plt.scatter(northern_pd['lat'],northern_pd['wind'], marker = "o", facecolors = 'green', edgecolor='black')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.title('Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression')
plt.show()
plt.savefig("output_data/Northern_Hemisphere_Wind_Speed_vs_Latitude_Linear_Regression.png")

# ####  Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression

# establish linear regression values = linregress(city_data ("lat","wind".values)
slope, intercept, rValue, pValue, stderror = linregress(southern_pd['lat'], southern_pd['wind'])
print(f'The St. Pearson Correlation Coefficient between both factors is {round(rValue, 2)}')
# linear regression line 
regress = slope*(southern_pd['lat']) + intercept
regresstring = "y = " + str(round(slope, 2)) + 'x + ' + str(round(intercept, 2))
plt.annotate(regresstring, xy=(-55,12), fontsize=13, color="blue")     
plt.plot(southern_pd['lat'],regress,"-",color="blue") 
plt.scatter(southern_pd['lat'],southern_pd['wind'], marker = "o", facecolors = 'green', edgecolor='black')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.title('Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression')
plt.show()
plt.savefig("output_data/Southern_Hemisphere_Wind_Speed_vs_Latitude_Linear_Regression.png")

Summary
#As latitude increases so does max temperature
#Humidity and Cloudiness appear random in both hemispheres
#Windspeed is fairly constant across the northern latitudes
#Max temp and Latitude are strongly correlated in the the Northern Hemisphere

StarcoderdataJupyterScriptsDedupFiltered