# ---
# 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 [conda env:PythonData] *
#     language: python
#     name: conda-env-PythonData-py
# ---

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

# Observation and analysis based on the data plotted
#
# 1. Location nearby equator latitude have higher Max Tempurature and temperature decreases as one moves away from the equator towards Norther or Sothern Hemisphere, there is strong positive linear relationship for Northen Hemisphere vs temp and strong negative linear relationship for Sothern Hemisphere vs temp.
#
# 2. In Northern and Southern Hemisphere Humidity has no correlated with Latitude.
#
# 3. In Northern has no correlated with Latitude and Southern Hemisphere Cloudness seems to have very weak correlated with Latitude.
#
# 4. Wind Speed have no correlated to latitude in the Northern and Southern Hemisphere.

# +
# 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 csv
import os
from pprint import pprint
from datetime import date

# 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 = os.path.join("../output_data/cities.csv")

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

# check point
# print(citipy)
# print(output_data_file)
# -

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

# +
#save cofig information
url = "http://api.openweathermap.org/data/2.5/weather?"
units = "imperial"

# Build partial query url
base_url = f"{url}appid={weather_api_key}&units={units}&q="

# check point to build api query request
# query_url = base_url + cities[0]
# weather_response = requests.get(query_url).json()
# pprint(weather_response)

# Set up a list
lat = []           
lng = []           
max_temp = []      
humidity = []      
cloudiness = []    
wind_speed = []    
country = []       
date = []
cities_list = []

# loop through the cities list for requested data

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

city_count = 0
set_count = 1

for city in cities:
    # build query url
    query_url= f"{base_url}+{city}"
    weather_response = requests.get(query_url).json()
    
    try:
        print(f"Processing Record {city_count} of Set {set_count} | {city}")
        lat.append(weather_response["coord"]["lat"])
        lng.append(weather_response["coord"]["lon"])
        max_temp.append(weather_response["main"]["temp_max"])
        humidity.append(weather_response["main"]["humidity"])
        cloudiness.append(weather_response["clouds"]["all"])
        wind_speed.append(weather_response["wind"]["speed"])
        country.append(weather_response["sys"]["country"])
        date.append(weather_response["dt"])
        cities_list.append(city)
        
    except:
        print("City not found. Skipping {city}.")
    
    
    city_count += 1
    if city_count == 50:
        city_count = 0
        set_count += 1
print("-----------------------------\nData Retrieval Complete\n-----------------------------")
# -

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

# +
# format data to dataframe
city_data = pd.DataFrame({"City": cities_list,
                           "Lat": lat,
                           "Lng": lng,
                           "Max Temp": max_temp,
                           "Humidity": humidity,
                           "Cloudiness": cloudiness,
                           "Wind Speed": wind_speed,
                           "Country": country,
                           "Date": date})

# count length of columns
city_data.count()
# -

# show the top dataframe
city_data.head()

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

city_data.describe()

#  Get the indices of cities that have humidity over 100%.
humid_weather = city_data[(city_data["Humidity"] > 100)].index
humid_weather

# 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".
clean_city_data = city_data.drop(humid_weather, inplace=False)
clean_city_data.head()

# +
# Extract relevant fields from the data frame
lat = clean_city_data["Lat"]
max_temp = clean_city_data["Max Temp"]
humidity = clean_city_data["Humidity"]
cloudiness = clean_city_data["Cloudiness"]
wind_speed = clean_city_data["Wind Speed"]

# Export the City_Data into a csv
clean_city_data.to_csv("Output/city_data.csv", index=False)
# -

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

# +
#Create a Scatter Plot for Lattitude vs Temperature of City
dt = time.strftime("%m/%d/%Y")
plt.scatter(lat, max_temp, marker='o',s=max_temp, edgecolors= "black")
plt.title(f"City Latitude vs Max Temperature plot ({dt})")
plt.ylim(min(clean_city_data["Max Temp"])-10,max(clean_city_data["Max Temp"])+10)
plt.xlim(min(clean_city_data["Lat"])-10, max(clean_city_data["Lat"])+10)
plt.ylabel("Max Temperature (F)")
plt.xlabel("Latitude")
plt.grid()
plt.show()

plt.savefig("Latitude Vs Max Temperature.png", bbox_inches="tight")
# -

# ## Latitude vs. Humidity Plot

# +
#Create a Scatter Plot for Lattitude vs Humidity of City
dt = time.strftime("%m/%d/%Y")
plt.scatter(lat, humidity, marker='o',s=humidity, edgecolors= "black")
plt.title(f"City Latitude vs Humidity plot ({dt})")
plt.ylim(min(clean_city_data["Humidity"])-10,max(clean_city_data["Humidity"])+10)
plt.xlim(min(clean_city_data["Lat"])-10, max(clean_city_data["Lat"])+10)
plt.ylabel("Humidity (%)")
plt.xlabel("Latitude")
plt.grid()
plt.show()

plt.savefig("Latitude Vs Humidity.png", bbox_inches="tight")
# -

# ## Latitude vs. Cloudiness Plot

# +
#Create a Scatter Plot for Lattitude vs Cloudiness of City
dt = time.strftime("%m/%d/%Y")
plt.scatter(lat, cloudiness, marker='o',s=40, edgecolors= "black")
plt.title(f"City Latitude vs Cloudiness plot ({dt})")
plt.ylim(min(clean_city_data["Cloudiness"])-10,max(clean_city_data["Cloudiness"])+10)
plt.xlim(min(clean_city_data["Lat"])-10, max(clean_city_data["Lat"])+10)
plt.ylabel("Cloudiness (%)")
plt.xlabel("Latitude")
plt.grid()
plt.show()

plt.savefig("Latitude Vs Cloudiness.png", bbox_inches="tight")
# -

# ## Latitude vs. Wind Speed Plot

# +
#Create a Scatter Plot for Lattitude vs Wind Speed of City
dt = time.strftime("%m/%d/%Y")
plt.scatter(lat, wind_speed, marker='o',s=40, edgecolors= "black")
plt.title(f"City Latitude vs Wind Speed plot ({dt})")
plt.ylim(min(clean_city_data["Wind Speed"])-10,max(clean_city_data["Wind Speed"])+10)
plt.xlim(min(clean_city_data["Lat"])-10, max(clean_city_data["Lat"])+10)
plt.ylabel("Wind Speed (mph)")
plt.xlabel("Latitude")
plt.grid()
plt.show()

plt.savefig("Latitude Vs Wind Speed.png", bbox_inches="tight")


# -

# ## Linear Regression

# OPTIONAL: Create a function to create Linear Regression plots
def regressionplot(x_values, y_values, x_label, y_label, hemisphere, line_placement, ylim=None):
    (slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
    
    # regression values
    regress_values = x_values * slope + intercept
    print(f"The r-squared is: {round(rvalue**2,2)}")
    
    # Create line equation string
    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,line_placement,fontsize=15,color="red")
    plt.xlabel(x_label)
    plt.ylabel(y_label)
    dt = time.strftime("%m/%d/%Y")
    plt.title(f"{hemisphere} Hemisphere - {y_label} vs. {x_label} Linear Regression Plot ({dt})")
    plt.savefig(f"{hemisphere} Hemisphere {y_label} vs. {x_label} Linear Regression Plot.png", bbox_inches="tight")
    plt.show()
    return


# +
# Create Northern and Southern Hemisphere DataFrames
northern_hemisphere = clean_city_data.loc[clean_city_data["Lat"]>0]
southern_hemisphere = clean_city_data.loc[clean_city_data["Lat"]<0]

# Check point
# equator = clean_city_data.loc[clean_city_data["Lat"] == 0]
# northern_hemisphere.count()
# northern_hemisphere.head()
# southern_hemisphere.count()
# southern_hemisphere.head()
# equator.count()
# -

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

x_values = northern_hemisphere['Lat']
y_values = northern_hemisphere['Max Temp']
regressionplot(x_values, y_values, "Latitude", "Max Temp", "Northern",(0, 60))

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

x_values = southern_hemisphere['Lat']
y_values = southern_hemisphere['Max Temp']
regressionplot(x_values, y_values, "Latitude", "Max Temp", "Southern",(-30, 40))

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

x_values = northern_hemisphere['Lat']
y_values = northern_hemisphere['Humidity']
regressionplot(x_values, y_values, "Latitude", "Humidity", "Northern",(0, 55))

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

x_values = southern_hemisphere['Lat']
y_values = southern_hemisphere['Humidity']
regressionplot(x_values, y_values, "Latitude", "Humidity", "Southern",(-50, 60))

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

x_values = northern_hemisphere['Lat']
y_values = northern_hemisphere['Cloudiness']
regressionplot(x_values, y_values, "Latitude", "Cloudiness", "Northern",(20, 60))

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

x_values = southern_hemisphere['Lat']
y_values = southern_hemisphere['Cloudiness']
regressionplot(x_values, y_values, "Latitude", "Cloudiness", "Southern",(-50, 60))

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

x_values = northern_hemisphere['Lat']
y_values = northern_hemisphere['Wind Speed']
regressionplot(x_values, y_values, "Latitude", "Wind Speed", "Northern",(0, 14))

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

x_values = southern_hemisphere['Lat']
y_values = southern_hemisphere['Wind Speed']
regressionplot(x_values, y_values, "Latitude", "Wind Speed", "Southern",(-52, 12))

StarcoderdataJupyterScriptsDedupFiltered