# ---
# 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: Python 3
#     language: python
#     name: python3
# ---

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

# !pip install citipy

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

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

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

# +
url = "http://api.openweathermap.org/data/2.5/weather?"
units = "imperial"
query_url = f"{url}appid={weather_api_key}&units={units}&q="

record = 0
set = 1

city_name = []
lat = []
long = []
max_temp = []
humidity = []
clouds = []
wind_speed = []
country = []
date = []


print("Beginning Data Retrieval")
print("-----------------------------")
for city in cities:
    
    #Count of cities/sets of 50
    if record == 50:
        record = 0
        set += 1
    record += 1
    
    #Try validating each city
    try:
        #Pull weather info with successful attempts
        weather_response = requests.get(query_url+city).json()
        city_name.append(weather_response["name"])
        lat.append(weather_response["coord"]["lat"])
        long.append(weather_response["coord"]["lon"])
        max_temp.append(weather_response["main"]["temp_max"])
        humidity.append(weather_response["main"]["humidity"])
        clouds.append(weather_response["clouds"]["all"])
        wind_speed.append(weather_response["wind"]["speed"])
        country.append(weather_response["sys"]["country"])
        date.append(weather_response["dt"])
        
        print(f"Processing Record {record} of Set {set} | {city}")
        
        
    except:
        #Print out for each city that can not be validated
        print("City not found. Skipping...")
        continue
    
    
# -

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

# +
#Put data into dictionary
wx_dict = {'City':city_name,
          'Lat':lat,
          'Lng':long,
          'Max Temp':max_temp,
          'Humidity':humidity,
          'Cloudiness':clouds,
          'Wind Speed':wind_speed,
          'Country':country,
          'Date':date}

#Put dictionary into DataFrame
wx_summary = pd.DataFrame(wx_dict)

#Print DataFrame into a csv
wx_summary.to_csv(output_data_file)

#Print DataFrame here
wx_summary

# +
#Perform quick analysis of data in DataFrame
wx_analysis = pd.DataFrame(wx_summary.describe())

#Print quick analysis
wx_analysis
# -

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


# 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

# +
#Outline Scatter Plot data
plt.scatter(wx_summary["Lat"],wx_summary["Max Temp"])

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.title("City Latitude vs. Max Temperature")
plt.grid(True)

#Save Scatter Plot
plt.savefig("output_data/Latitude vs. Temperature Plot.png")

#Print Scatter Plot
plt.show()

#The plot shows that temperatures are warmer the closer they are to the equator and colder at further latitudes
# -

# ## Latitude vs. Humidity Plot

# +
#Outline Scatter Plot data
plt.scatter(wx_summary["Lat"],wx_summary["Humidity"])

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.title("City Latitude vs. Humidity")
plt.grid(True)

#Save Scatter Plot
plt.savefig("output_data/Latitude vs. Humidity Plot.png")

#Print Scatter Plot
plt.show()

#The plot appears to show that cities at latitudes further from the equator have a higher percentage of humidity 
#and almost no low percentages of humidity in the same areas
# -

# ## Latitude vs. Cloudiness Plot

# +
#Outline Scatter Plot data
plt.scatter(wx_summary["Lat"],wx_summary["Cloudiness"])

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.title("City Latitude vs. Cloudiness")
plt.grid(True)

#Save Scatter Plot
plt.savefig("output_data/Latitude vs. Cloudiness Plot.png")

#Print Scatter Plot
plt.show()

#The plot appears to show that there is some higher percentages of cloudiness further from the equator
# -

# ## Latitude vs. Wind Speed Plot

# +
#Outline Scatter Plot data
plt.scatter(wx_summary["Lat"],wx_summary["Wind Speed"])

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")
plt.title("City Latitude vs. Wind Speed")
plt.grid(True)

#Save Scatter Plot
plt.savefig("output_data/Latitude vs. Wind Speed Plot.png")

#Print Scatter Plot
plt.show()

#The plot appears to show there is very little correlation between windspeed and latitude
# -

# ## Linear Regression

#Outline data in northern and southern hemisphere
northern_hem = wx_summary.loc[wx_summary["Lat"] > 0]
southern_hem = wx_summary.loc[wx_summary["Lat"] < 0]

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

# +
#Create equation
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(northern_hem["Lat"],northern_hem["Max Temp"])
regress_values = northern_hem["Lat"] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

#Outline graph
plt.scatter(northern_hem["Lat"],northern_hem["Max Temp"])
plt.plot(northern_hem["Lat"],regress_values,"r-")
plt.annotate(line_eq,(5,-15),fontsize=15,color="red")

#Label and detail graph
plt.xlabel("Latitude")
plt.ylabel("Max Temp (F)")
plt.title("Northern Hemisphere - City Latitude vs. Max Temp")

#Save Scatter Plot
plt.savefig("output_data/Northern Hemisphere - Max Temp vs. Latitude.png")

#Print Scatter Plot
plt.show()

#Print R-Value
print(f"The r-value is: {rvalue**2}")

#The plot shows there is a direct correlation between high max temps and lower latitudes as well as low max temps
# and higher latitudes
# -

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

# +
#Create equation
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(southern_hem["Lat"],southern_hem["Max Temp"])
regress_values = southern_hem["Lat"] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

#Outline graph
plt.scatter(southern_hem["Lat"],southern_hem["Max Temp"])
plt.plot(southern_hem["Lat"],regress_values,"r-")
plt.annotate(line_eq,(-25,40),fontsize=15,color="red")

#Label and detail graph
plt.xlabel("Latitude")
plt.ylabel("Max Temp (F)")
plt.title("Southern Hemisphere - City Latitude vs. Max Temp")

#Save Scatter Plot
plt.savefig("output_data/Southern Hemisphere - Max Temp vs. Latitude.png")

#Print Scatter Plot
plt.show()

#Print R-Value
print(f"The r-value is: {rvalue**2}")

#The plot shows there is a correlation between medium to high max temps and latitudes closer to the equator 
#as well as low max temps and further away from the equator
# -

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

# +
#Create equation
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(northern_hem["Lat"],northern_hem["Humidity"])
regress_values = northern_hem["Lat"] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

#Outline and detail graph
plt.scatter(northern_hem["Lat"],northern_hem["Humidity"])
plt.plot(northern_hem["Lat"],regress_values,"r-")
plt.annotate(line_eq,(40,20),fontsize=15,color="red")

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.title("Northern Hemisphere - City Latitude vs. Humidity")

#Save Scatter Plot
plt.savefig("output_data/Northern Hemisphere - Humidity (%) vs. Latitude.png")

#Print Scatter Plot
plt.show()

#Print R-Value
print(f"The r-value is: {rvalue**2}")

#This plot appears to show that humidity is becomes reliably higher at latitudes further from the equator and lower
#humidities are essentially non-existent
# -

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

# +
#Create equation
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(southern_hem["Lat"],southern_hem["Humidity"])
regress_values = southern_hem["Lat"] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

#Outline and detail graph
plt.scatter(southern_hem["Lat"],southern_hem["Humidity"])
plt.plot(southern_hem["Lat"],regress_values,"r-")
plt.annotate(line_eq,(-50,15),fontsize=15,color="red")

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.title("Southern Hemisphere - City Latitude vs. Humidity")

#Save Scatter Plot
plt.savefig("output_data/Southern Hemisphere - Humidity (%) vs. Latitude.png")

#Print Scatter Plot
plt.show()

#Print R-Value
print(f"The r-value is: {rvalue**2}")

#This plot appears to show there is a very very slight correlation between the rise in humidity as the latitudes 
#approach the equator
# -

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

# +
#Create equation
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(northern_hem["Lat"],northern_hem["Cloudiness"])
regress_values = northern_hem["Lat"] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

#Outline and detail graph
plt.scatter(northern_hem["Lat"],northern_hem["Cloudiness"])
plt.plot(northern_hem["Lat"],regress_values,"r-")
plt.annotate(line_eq,(45,25),fontsize=15,color="red")

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.title("Northern Hemisphere - City Latitude vs. Cloudiness")

#Save Scatter Plot
plt.savefig("output_data/Northern Hemisphere - Cloudiness (%) vs. Latitude.png")

#Print Scatter Plot
plt.show()

#Print R-Value
print(f"The r-value is: {rvalue**2}")

#This plot appears to show there is a correlation between low percentages of cloudiness at lower latitudes and higher
#percentages of cloudiness at higher latitudes
# -

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

# +
#Create equation
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(southern_hem["Lat"],southern_hem["Cloudiness"])
regress_values = southern_hem["Lat"] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

#Outline and detail graph
plt.scatter(southern_hem["Lat"],southern_hem["Cloudiness"])
plt.plot(southern_hem["Lat"],regress_values,"r-")
plt.annotate(line_eq,(-50,60),fontsize=15,color="red")

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.title("Southern Hemisphere - City Latitude vs. Cloudiness")

#Save Scatter Plot
plt.savefig("output_data/Southern Hemisphere - Cloudiness (%) vs. Latitude.png")

#Print Scatter Plot
plt.show()

#Print R-Value
print(f"The r-value is: {rvalue**2}")

#This plot appears to show there is a very slight correlation between the rise in cloudiness as the latitudes 
#approach the equator
# -

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

# +
#Create equation
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(northern_hem["Lat"],northern_hem["Wind Speed"])
regress_values = northern_hem["Lat"] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

#Outline and detail graph
plt.scatter(northern_hem["Lat"],northern_hem["Wind Speed"])
plt.plot(northern_hem["Lat"],regress_values,"r-")
plt.annotate(line_eq,(10,30),fontsize=15,color="red")

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")
plt.title("Northern Hemisphere - City Latitude vs. Wind Speed")

#Save Scatter Plot
plt.savefig("output_data/Northern Hemisphere - Wind Speed (mph) vs. Latitude.png")

#Print Scatter Plot
plt.show()

#Print R-Value
print(f"The r-value is: {rvalue**2}")

#This plot appears to show that wind speeds increases slightly from the equator to higher latitudes but is overall rather low
# -

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

# +
#Create equation
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(southern_hem["Lat"],southern_hem["Wind Speed"])
regress_values = southern_hem["Lat"] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

#Outline and detail graph
plt.scatter(southern_hem["Lat"],southern_hem["Wind Speed"])
plt.plot(southern_hem["Lat"],regress_values,"r-")
plt.annotate(line_eq,(-25,25),fontsize=15,color="red")

#Label graph
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")
plt.title("Southern Hemisphere - City Latitude vs. Wind Speed")

#Save Scatter Plot
plt.savefig("output_data/Southern Hemisphere - Wind Speed (mph) vs. Latitude.png")

#Print Scatter Plot
plt.show()

#Print R-Value
print(f"The r-value is: {rvalue**2}")

#This plot appears to show that wind speed reduces as it approaches the equator from lower latitudes
# -



StarcoderdataJupyterScriptsDedupFiltered