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

from citipy import citipy
import pandas as pd
import time, numpy as np, requests as rq
from api_keys import weather_api_key
import matplotlib.pyplot as plt
from datetime import date
from scipy.stats import linregress

# ## Generate Cities List

# +
# Output File (CSV)
output_data_file = "output_data/cities.csv"

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

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

# +
 #Lists and counters
city_list = []
cloud_list = []
country_list = []
date_list = []
humidity_list = []
lats_list = []
lngs_list = []
temp_max_list = []
wind_speed_list = []
index_counter = 0
set_counter = 1

print("Beginning Data Retrieval ")
print("-------------------------------")

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

#For loop matching city names with city_list
for index, city in enumerate(cities, start = 1):
    try:
        response = rq.get(query_url + city).json()
        city_list.append(response["name"])
        cloud_list.append(response["clouds"]["all"])
        country_list.append(response["sys"]["country"])
        date_list.append(response["dt"])
        humidity_list.append(response["main"]["humidity"])
        lats_list.append(response["coord"]["lat"])
        lngs_list.append(response["coord"]["lon"])
        temp_max_list.append(response['main']['temp_max'])
        wind_speed_list.append(response["wind"]["speed"])
        if index_counter > 49:
            index_counter = 0
            set_counter = set_counter + 1
        
        else:
            index_counter = index_counter + 1
        
        print(f"Processing Record {index_counter} of Set {set_counter} : {city}")
        
    except(KeyError, IndexError):
        print("City not found. Skipping...")
        
print("-------------------------------")
print("Data Retrieval Complete")
print("-------------------------------")


# -

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

len("date_list")

# +
weather_data = pd.DataFrame({
    "City" : city_list,
    "Lat" : lats_list,
    "Lng" : lngs_list,
"Max Temp" : temp_max_list,
    "Humidity" : humidity_list,
    "Clouds" : cloud_list,
    "Wind Speed" : wind_speed_list,
    "Country" : country_list,
    "Date" : date_list  })
weather_data

weather_data_humidity_lesthan_100 = weather_data
weather_data["Humidity"] = weather_data["Humidity"].map("{:d}".format)
weather_data
# -

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

weather_data['Humidity'] = pd.to_numeric(weather_data['Humidity'])
weather_data['Lat'] = pd.to_numeric(weather_data['Lat'])
weather_data['Lng'] = pd.to_numeric(weather_data['Lng'])
weather_data.to_csv("output_data/cities.csv")

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

index_humidity_greater_thn_100 = weather_data[ weather_data['Humidity'] > 100 ].index



# +
# 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".
# So far did not see a city with more than 100 % humidity. This code is in place just in case.
weather_data.drop(index_humidity_greater_thn_100, inplace = True)

weather_data.to_csv("output_data/cities_clean.csv")
weather_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

# +

today = date.today()

# dd/mm/YY
d1 = today.strftime("%m/%d/%Y")
#print("d1 =", d1)

plt.title(f"City Latitude vs Max Temperature ({d1})")
plt.ylabel("Max Temperature (F)")
plt.xlabel("Latitude")
plt.scatter(weather_data["Lat"], weather_data["Max Temp"], color = "steelblue", edgecolor = "black")
plt.grid()
fileLocation = f"Images/City Latitude vs Max Temperature.png"
plt.savefig(fileLocation)
# -

# ## Latitude vs. Humidity Plot

# +

plt.title(f"City Latitude vs Humidity ({d1})")
plt.ylabel("Humidity (%)")
plt.xlabel("Latitude")
plt.scatter(weather_data["Lat"], weather_data["Humidity"], color = "steelblue", edgecolor = "black")
plt.grid()

plt.savefig(f"Images/City Latitude vs Humidity.png")
# -

# ## Latitude vs. Cloudiness Plot

# +
plt.title(f"City Latitude vs Cloudiness ({d1})")
plt.ylabel("Cloudiness (%)")
plt.xlabel("Latitude")
plt.scatter(weather_data["Lat"], weather_data["Clouds"], color = "steelblue", edgecolor = "black")
plt.grid()

plt.savefig(f"Images/City Latitude vs Cloudiness.png")
# -

# ## Latitude vs. Wind Speed Plot

# +
plt.title(f"City Latitude vs Wind Speed ({d1})")
plt.ylabel("Wind Speed (mph)")
plt.xlabel("Latitude")
plt.scatter(weather_data["Lat"], weather_data["Wind Speed"], color = "steelblue", edgecolor = "black")
plt.grid()

plt.savefig(f"Images/City Latitude vs Wind Speed.png")
# -

# ## Linear Regression

northern_hemisphere_df = weather_data.loc[weather_data["Lat"] >= 0]
southern_hemisphere_df = weather_data.loc[weather_data["Lat"]< 0]

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

x_values = pd.to_numeric(northern_hemisphere_df['Lat']).astype(float)
y_values = pd.to_numeric(northern_hemisphere_df['Max Temp']).astype(float)
(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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="firebrick")
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title('Northern Hemisphere - Max Temp vs. Latitude Linear Regression')
plt.grid(True)
plt.savefig("../Images/Northern Hemisphere - Max Temp vs. Latitude Linear Regression.png")
plt.show()

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

x_values = pd.to_numeric(southern_hemisphere_df['Lat']).astype(float)
y_values = pd.to_numeric(southern_hemisphere_df['Max Temp']).astype(float)
(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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="firebrick")
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title('Southern Hemisphere - Max Temp vs. Latitude Linear Regression')
plt.grid(True)
plt.savefig("../Images/Southern Hemisphere - Max Temp vs. Latitude Linear Regression.png")
plt.show()

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

x_values = pd.to_numeric(northern_hemisphere_df['Lat']).astype(float)
y_values = pd.to_numeric(northern_hemisphere_df['Humidity']).astype(float)
(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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="firebrick")
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title('Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression')
plt.grid(True)
plt.savefig("../Images/Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression.png")
plt.show()

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

x_values = pd.to_numeric(southern_hemisphere_df['Lat']).astype(float)
y_values = pd.to_numeric(southern_hemisphere_df['Humidity']).astype(float)
(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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="firebrick")
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title('Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression')
plt.grid(True)
plt.savefig("../Images/Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression.png")
plt.show()

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

x_values = pd.to_numeric(northern_hemisphere_df['Lat']).astype(float)
y_values = pd.to_numeric(northern_hemisphere_df['Clouds']).astype(float)
(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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="firebrick")
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title('Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression')
plt.grid(True)
plt.savefig("../Images/Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression.png")
plt.show()

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

x_values = pd.to_numeric(southern_hemisphere_df['Lat']).astype(float)
y_values = pd.to_numeric(southern_hemisphere_df['Clouds']).astype(float)
(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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="firebrick")
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title('Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression')
plt.grid(True)
plt.savefig("../Images/Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression.png")
plt.show()

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

x_values = pd.to_numeric(northern_hemisphere_df['Lat']).astype(float)
y_values = pd.to_numeric(northern_hemisphere_df['Wind Speed']).astype(float)
(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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="firebrick")
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title('Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression')
plt.grid(True)
plt.savefig("../Images/Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression.png")
plt.show()

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

x_values = pd.to_numeric(southern_hemisphere_df['Lat']).astype(float)
y_values = pd.to_numeric(southern_hemisphere_df['Wind Speed']).astype(float)
(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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="firebrick")
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title('Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression')
plt.grid(True)
plt.savefig("../Images/Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression.png")
plt.show()



StarcoderdataJupyterScriptsDedupFiltered