<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
from pprint import pprint
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 = []
city_id=[]
cloudinesses =[]
countries = []
dates= []
humidities = []
max_temps = []
wind_speeds = []
r_lat=[]
r_lng=[]

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

url = "http://api.openweathermap.org/data/2.5/weather?"

# 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
    #print(city)
    query_url = url + "appid=" + weather_api_key + "&q="
# If the city is unique, then add it to a our cities list
    try:
        if city not in cities:
            response = requests.get(query_url + city).json()
            cities.append(city)
            city_id.append(response['id'])
            countries.append(response['sys']['country'])
            cloudinesses.append(response['clouds']['all'])
            dates.append(response['dt'])
            humidities.append(response['main']['humidity'])
            max_temps.append(response['main']['temp_max'])
            wind_speeds.append(response['wind']['speed'])
            r_lat.append(response['coord']['lat'])
            r_lng.append(response['coord']['lon'])

    except:
        print("City not found. Skipping...")
        pass
# Print the city count to confirm sufficient count
len(cities)
ktoF1= [(x-273.15)*9/5+32 for x in max_temps]  
ktoF= [round(a, 2) for a in ktoF1]
ktoF
cities
weather_json = response
pprint(weather_json)<jupyter_output>{'base': 'stations',
 'clouds': {'all': 23},
 'cod': 200,
 'coord': {'lat': -2.9248, 'lon': -39.9167},
 'dt': 1619446414,
 'id': 3393692,
 'main': {'feels_like': 305.95,
          'grnd_level': 1009,
          'humidity': 62,
          'pressure': 1011,
          'sea_level': 1011,
          'temp': 302.93,
          'temp_max': 302.93,
          'temp_min': 302.93},
 'name': 'Itarema',
 'sys': {'country': 'BR', 'sunrise': 1619426202, 'sunset': 1619469472},
 'timezone': -10800,
 'visibility': 10000,
 'weather': [{'description': 'few clouds',
              'icon': '02d',
              'id': 801,
              'main': 'Clouds'}],
 'wind': {'deg': 110, 'gust': 6.77, 'speed': 5.08}}<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>total= list(zip(city_id, cities))
total<jupyter_output><empty_output><jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>Weather_list = list(zip(cities, r_lat, r_lng, ktoF, humidities, cloudinesses, wind_speeds, countries, dates))
#"City","Lat", "Lng", "Max Temp", "Humidity", "Cloudiness", "Wind Speed", "Country", "Date"
Weather_list

# weather_dict = {
#     "City":cities,
#     "Cloudiness":cloudinesses,
#     "Country":countries,
#     "Date":dates,
#     "Humidity":humidities,
#     "Lat":lats,
#     "Lng":lngs,
#     "Max Temp":max_temps,
#     "Wind Speed":wind_speeds
# }
import csv
import os

cities, cloudinesses,countries,dates, humidities, lats, lngs, ktoF, wind_speeds

# Zip all three lists together into tuples
Weather_list

# save the output file path
output_file = os.path.join("citydf.csv")

# open the output file, create a header row, and then write the zipped object to the csv
with open(output_file, "w", newline='') as datafile:
    writer = csv.writer(datafile)

    writer.writerow(["City","Lat", "Lng", "Max Temp", "Humidity", "Cloudiness", "Wind Speed", "Country", "Date"])

    writer.writerows(Weather_list)

citydata = pd.read_csv ('citydf.csv')

citydf= pd.DataFrame(citydata)
citydf
citydf.describe()<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>humid_edit = citydf[citydf['Humidity']<=100]
humid_edit
#  Get the indices of cities that have humidity over 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".<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>import matplotlib.pyplot as plt



len(lats)
len(max_temps)
latitude= citydf['Lat']
Temp = citydf['Max Temp']
plt.title("City Latitude v Max Temperature")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature(F)")
plt.scatter(latitude, Temp, marker="o", facecolors="blue", edgecolors="black")<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>latitude
Humid = citydf['Humidity']
plt.title("City Latitude v Humidity")
plt.xlabel("Latitude")
plt.ylabel("Humidity(%)")
plt.scatter(latitude, Humid, marker="o", facecolors="blue", edgecolors="black")<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>latitude
Cloudy = citydf['Cloudiness']
plt.title("City Latitude v Cloudiness")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness(%)")
plt.scatter(latitude, Cloudy, marker="o", facecolors="blue", edgecolors="black")<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>latitude
Windy = citydf['Wind Speed']
plt.title("City Latitude v Wind Speed")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed(mph)")
plt.scatter(latitude, Windy, marker="o", facecolors="blue", edgecolors="black")<jupyter_output><empty_output><jupyter_text>Linear Regression   Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>northern_hemisphere = citydf[citydf['Lat']>0]
northern_hemisphere
x_valuesLT = northern_hemisphere['Lat']
y_valuesLT = northern_hemisphere['Max Temp']
(slopeLT, interceptLT, rvalueLT, pvalueLT, stderrLT) = linregress(x_valuesLT, y_valuesLT)
regress_valuesLT = x_valuesLT * slopeLT + interceptLT
line_eqLT = "y = " + str(round(slopeLT,2)) + "x + " + str(round(interceptLT,2))


print("The r-value is:"+ str(rvalueLT))
plt.scatter(x_valuesLT,y_valuesLT)
plt.plot(x_valuesLT,regress_valuesLT,"r-")
plt.annotate(line_eqLT,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Max Temp)')
plt.show()<jupyter_output>The r-value is:-0.8624834946089305<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>southern_hemisphere = citydf[citydf['Lat']<0]
x_valuesST = southern_hemisphere['Lat']
y_valuesST = southern_hemisphere['Max Temp']
(slopeST, interceptST, rvalueST, pvalueST, stderrST) = linregress(x_valuesST, y_valuesST)
regress_valuesST = x_valuesST * slopeST + interceptST
line_eqST = "y = " + str(round(slopeST,2)) + "x + " + str(round(interceptST,2))


print("The r-value is:"+ str(rvalueST))
plt.scatter(x_valuesST,y_valuesST)
plt.plot(x_valuesST,regress_valuesST,"r-")
plt.annotate(line_eqST,(-50,80),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Max Temp)')
plt.show()<jupyter_output>The r-value is:0.7607926652348054<jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_valuesHT = northern_hemisphere['Lat']
y_valuesHT = northern_hemisphere['Humidity']
(slopeHT, interceptHT, rvalueHT, pvalueHT, stderrHT) = linregress(x_valuesHT, y_valuesHT)
regress_valuesHT = x_valuesHT * slopeHT + interceptHT
line_eqHT = "y = " + str(round(slopeHT,2)) + "x + " + str(round(interceptHT,2))


print("The r-value is:"+ str(rvalueHT))
plt.scatter(x_valuesHT,y_valuesHT)
plt.plot(x_valuesHT,regress_valuesHT,"r-")
plt.annotate(line_eqHT,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Humidity')
plt.show()<jupyter_output>The r-value is:0.3074519875034634<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_valuesSH = southern_hemisphere['Lat']
y_valuesSH = southern_hemisphere['Humidity']
(slopeSH, interceptSH, rvalueSH, pvalueSH, stderrSH) = linregress(x_valuesSH, y_valuesSH)
regress_valuesSH = x_valuesSH * slopeSH + interceptSH
line_eqSH = "y = " + str(round(slopeSH,2)) + "x + " + str(round(interceptSH,2))


print("The r-value is:"+ str(rvalueSH))
plt.scatter(x_valuesSH,y_valuesSH)
plt.plot(x_valuesSH,regress_valuesSH,"r-")
plt.annotate(line_eqSH,(-50, 20),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Humidity')
plt.show()<jupyter_output>The r-value is:0.062423934650281385<jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_valuesNC = northern_hemisphere['Lat']
y_valuesNC = northern_hemisphere['Cloudiness']
(slopeNC, interceptNC, rvalueNC, pvalueNC, stderrNC) = linregress(x_valuesNC, y_valuesNC)
regress_valuesNC = x_valuesNC * slopeNC + interceptNC
line_eqNC = "y = " + str(round(slopeNC,2)) + "x + " + str(round(interceptNC,2))


print("The r-value is:"+ str(rvalueNC))
plt.scatter(x_valuesNC,y_valuesNC)
plt.plot(x_valuesNC,regress_valuesNC,"r-")
plt.annotate(line_eqNC,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Cloudiness')
plt.show()<jupyter_output>The r-value is:0.18315709837182706<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_valuesSC = southern_hemisphere['Lat']
y_valuesSC = southern_hemisphere['Cloudiness']
(slopeSC, interceptSC, rvalueSC, pvalueSC, stderrSC) = linregress(x_valuesSC, y_valuesSC)
regress_valuesSC = x_valuesSC * slopeSC + interceptSC
line_eqSC = "y = " + str(round(slopeSC,2)) + "x + " + str(round(interceptSC,2))


print("The r-value is:"+ str(rvalueSC))
plt.scatter(x_valuesSC,y_valuesSC)
plt.plot(x_valuesSC,regress_valuesSC,"r-")
plt.annotate(line_eqSC,(-30, 35),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Cloudiness')
plt.show()<jupyter_output>The r-value is:0.21401670168840595<jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_valuesNW = northern_hemisphere['Lat']
y_valuesNW = northern_hemisphere['Wind Speed']
(slopeNW, interceptNW, rvalueNW, pvalueNW, stderrNW) = linregress(x_valuesNW, y_valuesNW)
regress_valuesNW = x_valuesNW * slopeNW + interceptNW
line_eqNW = "y = " + str(round(slopeNW,2)) + "x + " + str(round(interceptNW,2))


print("The r-value is:"+ str(rvalueNW))
plt.scatter(x_valuesNW,y_valuesNW)
plt.plot(x_valuesNW,regress_valuesNW,"r-")
plt.annotate(line_eqNW,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Wind Speed')
plt.show()<jupyter_output>The r-value is:0.09137195135218146<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_valuesSW = southern_hemisphere['Lat']
y_valuesSW = southern_hemisphere['Wind Speed']
(slopeSW, interceptSW, rvalueSW, pvalueSW, stderrSW) = linregress(x_valuesSW, y_valuesSW)
regress_valuesSW = x_valuesSW * slopeSW + interceptSW
line_eqSW = "y = " + str(round(slopeSW,2)) + "x + " + str(round(interceptSW,2))


print("The r-value is:"+ str(rvalueSW))
plt.scatter(x_valuesSW,y_valuesSW)
plt.plot(x_valuesSW,regress_valuesSW,"r-")
plt.annotate(line_eqSW,(-50,6),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Wind Speed')
plt.show()<jupyter_output>The r-value is:-0.09203367484314111
{'fraction_non_alphanumeric': 0.1439816333224008, 'fraction_numerical': 0.029845851098720892, 'mean_word_length': 6.124415887850467, 'pattern_counts': {'":': 9, '<': 62, '<?xml version=': 0, '>': 63, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '449232', 'license': 'ADSL', 'n_tokens_mistral': 4865, 'n_tokens_neox': 4446, 'n_words': 921, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'davew306/python-api-challenge', 'size': '12196'}