# ---
# 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 API key
from config 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).
#

# +
# Loop through the list of cities and perform a request for data on each
lat = []
lng = []
max_temp = []
humid = []
cloud = []
wind_speed = []
country = []
date = []
city_name = []
count = 1
sets = 1
print('Beginning Data Retrieval')    
print('-----------------------------')
for city in cities:
    # Save config information
    url = "http://api.openweathermap.org/data/2.5/weather?"

    # Build query URL
    query_url = url + "units=imperial&appid=" + weather_api_key + "&q=" + city
    response = requests.get(query_url).json()
    # Try to grab city and record #
    try:
        
        print(f'Processing Record {count} of Set {sets} | {city}')
        lat.append(response['coord']['lat'])
        lng.append(response['coord']['lon'])
        max_temp.append(response['main']['temp_max'])
        humid.append(response['main']['humidity'])
        cloud.append(response['clouds']['all'])
        wind_speed.append(response['wind']['speed'])
        country.append(response['sys']['country'])
        date.append(response['dt'])
        city_name.append(response['name'])
        
        
    # Handle exceptions for a city that is not available in the API
    except:
        # Append null values
        print(f'City not found...skipping')
        pass
    
    #keep track of records and sets(max 50 per set)
    count = count +1
    if count == 50:
        count = 0
        sets = sets + 1

print('-----------------------------')
print('Data Retrieval Complete')
print('-----------------------------')
# -



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

df = pd.DataFrame({'City': city_name, 'Lat': lat, 'Lon': lng, 'Max Temp (F)': max_temp, 'Humidity': humid, 'Cloudiness': cloud,'Wind Speed': wind_speed, 'Country': country, 'Date': date})
df.to_csv('../output_data/cities.csv', encoding='utf-8', index=False, date_format='%d/%m/%Y')
df.head()

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



len(df)

# +
quartiles = df['Humidity'].sort_values().quantile([.25,.5,.75])
lowerq = quartiles[0.25]
upperq = quartiles[0.75]
iqr = upperq-lowerq

print(f"The lower quartile of humidity is: {lowerq}")
print(f"The upper quartile of humidity is: {upperq}")
print(f"The interquartile range of humidity is: {iqr}")
print(f"The the median of humidity is: {quartiles[0.5]} ")

lower_bound = lowerq - (1.5*iqr)
upper_bound = upperq + (1.5*iqr)
print(f"Values below {lower_bound} could be outliers.")
print(f"Values above {upper_bound} could be outliers.")

# +
# Example outlier plot of humidity

fig1, ax1 = plt.subplots()
ax1.set_title('Humidity %')
ax1.set_ylabel('% Humidity')
ax1.boxplot(df['Humidity'])
plt.show()
# -

#  Get the indices of cities that have humidity over 100%.


clean_city_data = df.drop(df.query('Humidity < 24.5 | Humidity > 124.5').index, inplace=False)
# print(len(clean_city_data) - len(df))
clean_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

x_values = clean_city_data['Lat']
y_values = clean_city_data['Max Temp (F)']
plt.scatter(x_values,y_values, edgecolors='b')
plt.title('City Latitude vs Max Tempertature')
plt.xlabel('Latitude')
plt.ylabel('Max Tempertature (F)')
plt.grid()
plt.savefig('../output_data/lat_max_temp.png')
plt.show()



# ## Latitude vs. Humidity Plot

x_values = clean_city_data['Lat']
y_values = clean_city_data['Humidity']
plt.scatter(x_values,y_values, edgecolors='b')
plt.title('City Latitude vs Humidity')
plt.xlabel('City Latitude')
plt.ylabel('Humidity %')
plt.grid()
plt.savefig('../output_data/lat_humidity.png')
plt.show()



# ## Latitude vs. Cloudiness Plot

x_values = clean_city_data['Lat']
y_values = clean_city_data['Cloudiness']
plt.scatter(x_values,y_values, edgecolors='b')
plt.title('City Latitude vs Cloudiness')
plt.xlabel('City Latitude')
plt.ylabel('Cloudiness %')
plt.grid()
plt.savefig('../output_data/lat_cloudiness.png')
plt.show()



# ## Latitude vs. Wind Speed Plot

x_values = clean_city_data['Lat']
y_values = clean_city_data['Wind Speed']
plt.scatter(x_values,y_values, edgecolors='b')
plt.title('City Latitude vs Wind Speed')
plt.xlabel('City Latitude')
plt.ylabel('Wind Speed (mph)')
plt.grid()
plt.savefig('../output_data/lat_wind.png')
plt.show()



# ## Linear Regression

# create df's separating northern and southern hemisphere
north_data = clean_city_data.drop(clean_city_data.query('Lat < 0').index, inplace=False)
south_data = clean_city_data.drop(clean_city_data.query('Lat > 0').index, inplace=False)
south_data.head()

# +
# Add the linear regression equation and line to plot

x_values = north_data['Lat']
y_values = north_data['Wind Speed']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values, edgecolors='b')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-3,30),fontsize=15,color="red")
plt.title('City Latitude vs Wind Speed')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.grid()
print(f'The r-value is: {rvalue}')
plt.show()

# -

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

# +

x_values = north_data['Lat']
y_values = north_data['Max Temp (F)']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values, edgecolors='b')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-3,20),fontsize=15,color="red")
plt.title('Northern Hemisphere - Max Temp vs. Latitude Linear Regression')
plt.xlabel('Latitude')
plt.ylabel('Max Temp (F)')
plt.grid()
plt.savefig('../output_data/north_lat_temp_lr.png')
print(f'The r-value is: {rvalue}')
plt.show()
# -



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

x_values = south_data['Lat']
y_values = south_data['Max Temp (F)']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values, edgecolors='b')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-30,50),fontsize=15,color="red")
plt.title('Southern Hemisphere - Max Temp vs. Latitude Linear Regression')
plt.xlabel('Latitude')
plt.ylabel('Max Temp (F)')
plt.grid()
plt.savefig('../output_data/south_lat_temp_lr.png')
print(f'The r-value is: {rvalue}')
plt.show()



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

x_values = north_data['Lat']
y_values = north_data['Humidity']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values, edgecolors='b')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(40,35),fontsize=15,color="red")
plt.title('Northern Hemisphere - Humidity (%) vs. Latitude')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.grid()
print(f'The r-value is: {rvalue}')
plt.savefig('../output_data/north_lat_humidity_lr.png')
plt.show()



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

x_values = south_data['Lat']
y_values = south_data['Humidity']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values, edgecolors='b')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-40,35),fontsize=15,color="red")
plt.title('Southern Hemisphere - Humidity (%) vs. Latitude')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.grid()
print(f'The r-value is: {rvalue}')
plt.savefig('../output_data/south_lat_humidity_lr.png')
plt.show()



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

x_values = north_data['Lat']
y_values = north_data['Cloudiness']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values, edgecolors='b')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(40,40),fontsize=15,color="red")
plt.title('Northern Hemisphere - Cloudiness (%) vs. Latitude')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.grid()
print(f'The r-value is: {rvalue}')
plt.savefig('../output_data/south_lat_cloud_lr.png')
plt.show()



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

x_values = south_data['Lat']
y_values = south_data['Cloudiness']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values, edgecolors='b')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-40,20),fontsize=15,color="red")
plt.title('Southern Hemisphere - Cloudiness (%) vs. Latitude')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.grid()
print(f'The r-value is: {rvalue}')
plt.savefig('../output_data/south_lat_cloud_lr.png')
plt.show()



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

x_values = north_data['Lat']
y_values = north_data['Wind Speed']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values, edgecolors='b')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(40,30),fontsize=15,color="red")
plt.title('Northern Hemisphere - Wind Speed (mph) vs. Latitude')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.grid()
print(f'The r-value is: {rvalue}')
plt.savefig('../output_data/south_lat_wind_lr.png')
plt.show()



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

x_values = south_data['Lat']
y_values = south_data['Wind Speed']
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values, edgecolors='b')
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-40,20),fontsize=15,color="red")
plt.title('Southern Hemisphere - Wind Speed (mph) vs. Latitude')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.grid()
print(f'The r-value is: {rvalue}')
plt.savefig('../output_data/south_lat_wind_lr.png')
plt.show()





StarcoderdataJupyterScriptsDedupFiltered