<jupyter_start><jupyter_text>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.<jupyter_code># 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

from datetime import datetime


# 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)<jupyter_output><empty_output><jupyter_text>Generate Cities List<jupyter_code># 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)<jupyter_output><empty_output><jupyter_text>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).<jupyter_code># after ? is the info that im retrieving 
url= "http://api.openweathermap.org/data/2.5/weather?"
units = "metric"

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

#query_url

# Temperature (F) vs. Latitude
# Humidity (%) vs. Latitude
# Cloudiness (%) vs. Latitude
# Wind Speed (mph) vs. Latitude

setcount=1
citycount=0 
date = []
country = []
name = []
temp = []
lat = []
humid = []
cloud = []
wind = []
lon = []



# # # Loop through the list of cities and perform a request for data on each
for city in cities:
    
    if citycount == 50:
        citycount = 0 
        setcount +=1
    
    try:
        #print(query_url+city)
        response = requests.get(query_url + city).json()
        
        date.append(response['dt'])
        country.append(response['sys']['country'])
        lat.append(response['coord']['lat'])
        temp.append(response['main']['temp'])
        lon.append(response['coord']['lon'])
        humid.append(response['main']['humidity'])
        cloud.append(response['clouds']['all'])
        wind.append(response['wind']['speed'])
        name.append(city)
        citycount +=1
        print(f"Processing record {citycount} of {setcount}:{city}")
    except:
        print(f"No data for {city}")



# query_url<jupyter_output>Processing record 1 of 1:mataura
Processing record 2 of 1:vostok
Processing record 3 of 1:ovsyanka
Processing record 4 of 1:new norfolk
No data for phan rang
Processing record 5 of 1:aguililla
Processing record 6 of 1:port-de-bouc
Processing record 7 of 1:necochea
Processing record 8 of 1:torbay
Processing record 9 of 1:muros
Processing record 10 of 1:lompoc
No data for tumannyy
Processing record 11 of 1:knysna
Processing record 12 of 1:isangel
Processing record 13 of 1:hohhot
Processing record 14 of 1:batangafo
Processing record 15 of 1:albany
Processing record 16 of 1:ribeira grande
Processing record 17 of 1:hilo
No data for umzimvubu
Processing record 18 of 1:erzin
Processing record 19 of 1:busselton
Processing record 20 of 1:victoria
Processing record 21 of 1:rikitea
Processing record 22 of 1:upernavik
Processing record 23 of 1:saint george
Processing record 24 of 1:sheridan
Processing record 25 of 1:nikolskoye
Processing record 26 of 1:hermanus
No data for talawdi
Processing recor[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>weather_df= pd.DataFrame({"City":name,
                        "Country": country,
                        "Max Temp": temp,
                        "Humidity": humid,
                        "Cloudiness": cloud,
                        "Wind Speed": wind,
                        "Lat": lat,
                        "Lng":lon,
                        "Date": date
                        })

weather_df
# Save the dataframe into a csv file

weather_df.to_csv("weather.csv",encoding="utf-8", index=False)<jupyter_output><empty_output><jupyter_text>Inspect the data and remove the cities where the humidity > 100%.----Skip this step if there are no cities that have humidity > 100%.<jupyter_code># 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= weather_df[weather_df['Humidity'] <=100]
clean_city_data


# another way to loop as Humidity >100
# if (weather_df['Humidity'] > 100).any():
#     print("found a city with greater than 100% humidity")

# clean_city_data = weather_df.drop(highhumid, inplace=False)
# clean_city_data.head()

# Extract relevant fields from the data frame
City_Data=clean_city_data[["City","Country","Max Temp","Humidity","Cloudiness","Wind Speed","Lat","Lng","Date"]]

# Export the City_Data into a csv
City_Data.to_csv('C:/Users/Keke/git/python-api-challenge/output_data/City_Data.csv', index=False)<jupyter_output><empty_output><jupyter_text>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<jupyter_code># set a date to use
Date= City_Data['Date'].max()
dateon= datetime.utcfromtimestamp(Date).strftime('%m-%d-%Y')

x=City_Data['Lat']
y=City_Data['Max Temp']

plt.scatter(x,y,marker="o")


plt.grid(True)

plt.title(f"Latitude vs Temperature {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Temperature (Farenheit)")

# export as png
plt.savefig(f'../output_data/Latitude vs Temperature on {dateon}.png')

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>x=City_Data['Lat']
y=City_Data['Humidity']

plt.scatter(x,y,marker="o")


plt.grid(True)

plt.title(f"Latitude vs Humidity {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Humidity")

# export as png
plt.savefig(f'../output_data/Latitude vs Humidity on {dateon}.png')

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>x=City_Data['Lat']
y=City_Data['Cloudiness']

plt.scatter(x,y,marker="o")


plt.grid(True)

plt.title(f"Latitude vs Cloudiness {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness")

# export as png
plt.savefig(f'../output_data/Latitude vs Cloudiness on {dateon}.png')

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>x=City_Data['Lat']
y=City_Data['Wind Speed']

plt.scatter(x,y,marker="o")


plt.grid(True)

plt.title(f"Latitude vs Wind Speed {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed")

# export as png
plt.savefig(f'../output_data/Latitude vs Wind Speed on {dateon}.png')

plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code># OPTIONAL: Create a function to create Linear Regression plots
# set axis
xlabel= City_Data['Lat'] 
ylabel= City_Data['Cloudiness']


(slope, intercept, rvalue, pvalue, stderr) = linregress(xlabel, ylabel)
regress_values = xlabel * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


plt.scatter(xlabel, ylabel)

plt.plot(xlabel,regress_values,"r-")

plt.annotate(line_eq,(77,38),fontsize=15,color="red")

plt.title(f"Latitude vs Cloudiness Linear Regression {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness")

print(f"R squared: {rvalue**2}")
plt.savefig(f'../output_data/Latitude vs Cloudiness Linear Regression on {dateon}.png')
plt.show()

# Create Northern and Southern Hemisphere DataFrames

north = weather_df.loc[weather_df['Lat'] >0]
north

south= weather_df.loc[weather_df['Lat'] <0]
south<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>xlabel= north["Lat"] 
ylabel= north["Max Temp"]


(slope, intercept, rvalue, pvalue, stderr) = linregress(xlabel, ylabel)
regress_values = xlabel * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


plt.scatter(xlabel, ylabel)

plt.plot(xlabel,regress_values,"r-")

plt.annotate(line_eq,(47,38),fontsize=15,color="red")


plt.title(f"Northern Hemisphere Max Temp vs. Latitude Linear Regression {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Temperature(F)")

plt.savefig(f'../output_data/Northern Hemisphere Max Temp vs. Latitude Linear Regression on {dateon}.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>xlabel= south["Lat"] 
ylabel= south["Max Temp"]


(slope, intercept, rvalue, pvalue, stderr) = linregress(xlabel, ylabel)
regress_values = xlabel * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


plt.scatter(xlabel, ylabel)

plt.plot(xlabel,regress_values,"r-")

# to have the equation shown with - number
plt.annotate(line_eq,(-30,10),fontsize=15,color="red")


plt.title(f"Southern Hemisphere Temperature(F) vs. Latitude Linear Regression {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Temperature(F)")

plt.savefig(f'../output_data/Southern Hemisphere Temperature(F) vs. Latitude Linear Regression on {dateon}.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>xlabel= north["Lat"] 
ylabel= north["Humidity"]


(slope, intercept, rvalue, pvalue, stderr) = linregress(xlabel, ylabel)
regress_values = xlabel * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


plt.scatter(xlabel, ylabel)

plt.plot(xlabel,regress_values,"r-")

plt.annotate(line_eq,(47,38),fontsize=15,color="red")


plt.title(f"Northern Hemisphere Humidity(%) vs. Latitude Linear Regression {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Humidity(%)")

plt.savefig(f'../output_data/Northern Hemisphere Humidity(%) vs. Latitude Linear Regression on {dateon}.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>xlabel= south["Lat"] 
ylabel= south["Humidity"]


(slope, intercept, rvalue, pvalue, stderr) = linregress(xlabel, ylabel)
regress_values = xlabel * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


plt.scatter(xlabel, ylabel)

plt.plot(xlabel,regress_values,"r-")

plt.annotate(line_eq,(-38,28),fontsize=15,color="red")


plt.title(f"Southern Hemisphere Humidity(%) vs. Latitude Linear Regression {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Humidity(%)")

plt.savefig(f'../output_data/Southern Hemisphere Humidity(%) vs. Latitude Linear Regression on {dateon}.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>xlabel= north["Lat"] 
ylabel= north["Cloudiness"]


(slope, intercept, rvalue, pvalue, stderr) = linregress(xlabel, ylabel)
regress_values = xlabel * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


plt.scatter(xlabel, ylabel)

plt.plot(xlabel,regress_values,"r-")

plt.annotate(line_eq,(47,38),fontsize=15,color="red")


plt.title(f"Northern Hemisphere Cloudiness (%) vs. Latitude Linear Regression {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")

plt.savefig(f'../output_data/Northern Hemisphere Cloudiness (%) vs. Latitude Linear Regression on {dateon}.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>xlabel= south["Lat"] 
ylabel= south["Cloudiness"]


(slope, intercept, rvalue, pvalue, stderr) = linregress(xlabel, ylabel)
regress_values = xlabel * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


plt.scatter(xlabel, ylabel)

plt.plot(xlabel,regress_values,"r-")

plt.annotate(line_eq,(-37,22),fontsize=15,color="red")

plt.title(f"Southern Hemisphere Cloudiness (%) vs. Latitude Linear Regression {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")

plt.savefig(f'../output_data/Southern Hemisphere Cloudiness (%) vs. Latitude Linear Regression on {dateon}.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>xlabel= north["Lat"] 
ylabel= north["Wind Speed"]

(slope, intercept, rvalue, pvalue, stderr) = linregress(xlabel, ylabel)
regress_values = xlabel * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


plt.scatter(xlabel, ylabel)

plt.plot(xlabel,regress_values,"r-")

plt.annotate(line_eq,(15,10),fontsize=15,color="red")

plt.title(f"Northern Hemisphere Wind Speed vs. Latitude Linear Regression {dateon}")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed")

plt.savefig(f'../output_data/Northern Hemisphere Wind Speed vs. Latitude Linear Regression on {dateon}.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>xlabel= south["Lat"] 
ylabel= south["Wind Speed"]


(slope, intercept, rvalue, pvalue, stderr) = linregress(xlabel, ylabel)
regress_values = xlabel * slope + intercept

line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


plt.scatter(xlabel, ylabel)

plt.plot(xlabel,regress_values,"r-")

plt.annotate(line_eq,(-40,8),fontsize=15,color="red")

plt.title(f"Southern Hemisphere Windspeed vs. Latitude Linear Regression")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed")

plt.savefig(f'../output_data/Southern Hemisphere Wind Speed vs. Latitude Linear Regression on {dateon}.png')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.135453144698137, 'fraction_numerical': 0.013657374982490545, 'mean_word_length': 5.323737821080602, 'pattern_counts': {'":': 9, '<': 74, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '158861', 'license': 'ADSL', 'n_tokens_mistral': 5378, 'n_tokens_neox': 4925, 'n_words': 1194, 'path': 'WeatherPy/.ipynb_checkpoints/WeatherPy-Copy1-checkpoint.ipynb', 'repo_name': 'kekelee0714/python-api-challenge', 'size': '14278'}