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

# +
# 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 json

# 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
#print(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?q=houston&appid=018a5d02bbc7412a29ca7b6e59de87ea'
response= requests.get(url).json()
display = "imperial"

# +
#Empty list to hold needed values
result=[]
lat=[]
lng=[]
temp =[]
humidity=[]
clouds=[]
wind=[]
country= []
date=[]


#Initialize a counter to hold variables.
record_no = 0

print('Beginning Data Retrieval')
print('-' * 25) 

for city in cities:
    url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=018a5d02bbc7412a29ca7b6e59de87ea&units={display}'
    response = requests.get(url).json()
    #print(response)
    if 'name' in response:
        result.append(response['name'])
        lat.append(response['coord']['lat'])
        lng.append(response['coord']['lon'])
        clouds.append(response['clouds']['all'])
        wind.append(response['wind']['speed'])
        humidity.append(response['main']['humidity'])
        temp.append(response['main']['temp'])
        country.append(response['sys']['country'])
        city_name =(response['name'])
        date.append(response['dt'])
        print(f'Processing Record {record_no} | {city_name}')
        record_no = record_no + 1
    else:
        print(f'City not found: {city}')


# -



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

# +
#Set columns for new df
weather_info = {'City':result, 'Lat':lat, 'Lng':lng, 'Max Temp':temp,'Humidity':humidity, 'Cloudiness':clouds, 'Wind Speed':wind, 'Country':country, 'Date': date}

weather_df = pd.DataFrame(weather_info)

weather_df.head()
# -



weather_df.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%.
weather_df.index[weather_df['Humidity'] > 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".

weather_df.drop(weather_df[weather_df['Humidity'] > 100].index, inplace = True)
weather_df.to_csv('clean_weather.csv')
weather_df.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.scatter(weather_df['Lat'],weather_df['Max Temp'])

#Add the title
plt.title('City Latitude vs. Max Temperture')
#Add label for x axis
plt.xlabel('Latitude')
#Add label for y axis
plt.ylabel('Max Temperture (F)')
#Add grid lines
plt.grid()

plt.savefig("City Latitude vs. Max Temperture.png")
print('Observation: This data shows that temperture increases as you move further from the equator.')
# -



# ## Latitude vs. Humidity Plot

# +
plt.scatter(weather_df['Lat'],weather_df['Humidity'])

plt.title('City Latitude vs. Humidity')
plt.xlabel('Latitude')
plt.ylabel('Humidity(%)')
plt.grid()

plt.savefig('City Latitude vs. Humidity.png')
print('Observation: Humidity appears to be slightly higher and concentrated in the southern hemisphere.')
# -



# ## Latitude vs. Cloudiness Plot

# +
plt.scatter(weather_df['Lat'],weather_df['Cloudiness'])

plt.title('City Latitude vs. Cloudiness')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness(%)')
plt.grid()

plt.savefig('City Latitude vs. Cloudiness.png')
# -



# ## Latitude vs. Wind Speed Plot

# +
plt.scatter(weather_df['Lat'],weather_df['Wind Speed'])

plt.title('City Latitude vs. Wind Speed')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed(%)')
plt.grid()

plt.savefig('City Latitude vs. Wind Speed.png')
print('Observation: The wind speed is primarily below 20% across both hemispheres.')
# -



# ## Linear Regression

nh_weather_df = (weather_df[weather_df['Lat'] > 0])
nh_weather_df.head()

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

# +
plt.scatter(nh_weather_df['Lat'],nh_weather_df['Max Temp'])

plt.title('Latitude vs. Max Temperture')
plt.xlabel('Latitude')
plt.ylabel('Max Temperture (F)')
plt.grid()

(slope, intercept, rvalue, pvalue, stderr) = linregress(nh_weather_df['Lat'],nh_weather_df['Max Temp'])
regress_values = nh_weather_df['Lat'] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.plot(nh_weather_df['Lat'], regress_values, color = 'red')
plt.annotate(line_eq,(0,-40),fontsize = 15,color ='red')
print(f"The r-squared is: {rvalue**2}")

plt.savefig('NH Latitude vs. Max Temperture.png')
plt.show()
# -



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

# +
sh_weather_df = (weather_df[weather_df['Lat'] < 0])

plt.scatter(sh_weather_df['Lat'],sh_weather_df['Max Temp'])
plt.xlabel('Latitude')
plt.ylabel('Max Temperture (F)')
plt.grid()

(slope, intercept, rvalue, pvalue, stderr) = linregress(sh_weather_df['Lat'],sh_weather_df['Max Temp'])
regress_values = sh_weather_df['Lat'] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.plot(sh_weather_df['Lat'], regress_values, color = 'red')
plt.annotate(line_eq,(-50,90),fontsize = 15,color ='red')
print(f"The r-squared is: {rvalue**2}")

plt.savefig('SH Latitude vs. Max Temperture.png')
# -



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

# +
plt.scatter(nh_weather_df['Lat'],nh_weather_df['Humidity'])

plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.grid()

(slope, intercept, rvalue, pvalue, stderr) = linregress(nh_weather_df['Lat'],nh_weather_df['Humidity'])
regress_values = nh_weather_df['Lat'] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.plot(nh_weather_df['Lat'], regress_values, color = 'red')
plt.annotate(line_eq,(40,20),fontsize = 15,color ='red')
print(f"The r-squared is: {rvalue**2}")

plt.savefig('NH Humidity vs. Lat Linear Regression.png')
# -



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

# +
plt.scatter(sh_weather_df['Lat'],sh_weather_df['Humidity'])

plt.xlabel('Latitude')
plt.ylabel('Humidity(%)')
plt.grid()

(slope, intercept, rvalue, pvalue, stderr) = linregress(sh_weather_df['Lat'],sh_weather_df['Humidity'])
regress_values = sh_weather_df['Lat'] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.plot(sh_weather_df['Lat'], regress_values, color = 'red')
plt.annotate(line_eq,(-50,20),fontsize = 15,color ='red')
print(f"The r-squared is: {rvalue**2}")

plt.savefig('SH Humidity vs. Lat Linear Regression.png')
# -



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

# +
plt.scatter(nh_weather_df['Lat'],nh_weather_df['Cloudiness'])

plt.xlabel('Latitude')
plt.ylabel('Cloudiness(%)')
plt.grid()

(slope, intercept, rvalue, pvalue, stderr) = linregress(nh_weather_df['Lat'],nh_weather_df['Cloudiness'])
regress_values = nh_weather_df['Lat'] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.plot(nh_weather_df['Lat'], regress_values, color = 'red')
plt.annotate(line_eq,(40,27),fontsize = 15,color ='red')
print(f"The r-squared is: {rvalue**2}")

plt.savefig('NH Cloudiness vs. Lat Linear Regression.png')
# -

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

# +
plt.scatter(sh_weather_df['Lat'],sh_weather_df['Cloudiness'])

plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.grid()

(slope, intercept, rvalue, pvalue, stderr) = linregress(sh_weather_df['Lat'],sh_weather_df['Cloudiness'])
regress_values = sh_weather_df['Lat'] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.plot(sh_weather_df['Lat'], regress_values, color = 'red')
plt.annotate(line_eq,(-50,10),fontsize = 15,color ='red')
print(f"The r-squared is: {rvalue**2}")

plt.savefig('SH Cloudiness vs. Lat Linear Regression.png')
# -



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

# +
plt.scatter(nh_weather_df['Lat'],nh_weather_df['Wind Speed'])

plt.title('Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed(%)')
plt.grid()

(slope, intercept, rvalue, pvalue, stderr) = linregress(nh_weather_df['Lat'],nh_weather_df['Wind Speed'])
regress_values = nh_weather_df['Lat'] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.plot(nh_weather_df['Lat'], regress_values, color = 'red')
plt.annotate(line_eq,(0,40),fontsize = 15,color ='red')
print(f"The r-squared is: {rvalue**2}")

plt.savefig('NH Wind Speed vs. Lat Linear Regression.png')
# -



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

# +
plt.scatter(sh_weather_df['Lat'],sh_weather_df['Wind Speed'])

plt.title('Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed(%)')
plt.grid()

(slope, intercept, rvalue, pvalue, stderr) = linregress(sh_weather_df['Lat'],sh_weather_df['Wind Speed'])
regress_values = sh_weather_df['Lat'] * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.plot(sh_weather_df['Lat'], regress_values, color = 'red')
plt.annotate(line_eq,(-50,25),fontsize = 15,color ='red')
print(f"The r-squared is: {rvalue**2}")

plt.savefig('SH Wind Speed vs. Lat Linear Regression.png')
# -



StarcoderdataJupyterScriptsDedupFiltered