# Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import time
from datetime import datetime
from scipy.stats import linregress
import scipy.stats as st
# 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)
# List for holding lat_lngs and cities
lat_lngs = []
cities = []
# Create a set of random lat and lng combinations
lats = np.random.uniform(low=-90.000, high=90.000, size=1500)
lngs = np.random.uniform(low=-180.000, high=180.000, 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)
639
# Url and units
url = "http://api.openweathermap.org/data/2.5/weather?"
units = "imperial"
# Build query URL
query_url = f"{url}appid={weather_api_key}&units={units}&q="
# List for holding reponse information
lat = []
temp = []
temp_max = []
humidity = []
wind_speed = []
lon = []
date = []
country = []
cloudiness = []
# Loop through the list of cities and request for data on each
print("Beginning Data Retrieval")
print("--------------------------------------------")
count = 0
set = 1
for idx, city in enumerate(cities):
count = count + 1
# To avoid api call rate limits, get city weather data for 50 cities,
# then sleep for 5 seconds, and then continue with next 50 cities and so on...
if count == 51:
count = 1
set = set + 1
time.sleep(5)
print(f"Processing Record {count} of Set {set} | {city}")
try:
response = requests.get(query_url + city).json()
lat.append(response['coord']['lat'])
lon.append(response['coord']['lon'])
temp.append(response['main']['temp'])
temp_max.append(response['main']['temp_max'])
humidity.append(response['main']['humidity'])
wind_speed.append(response['wind']['speed'])
date.append(response['dt'])
country.append(response['sys']['country'])
cloudiness.append(response['clouds']['all'])
except KeyError:
print("City not found. Skipping...")
lat.append(np.nan)
lon.append(np.nan)
temp.append(np.nan)
temp_max.append(np.nan)
humidity.append(np.nan)
wind_speed.append(np.nan)
date.append(np.nan)
country.append(np.nan)
cloudiness.append(np.nan)
print("------------------------------------------------")
print("Data Retrieval Complete")
print("------------------------------------------------")
Beginning Data Retrieval -------------------------------------------- Processing Record 1 of Set 1 | severo-kurilsk Processing Record 2 of Set 1 | ust-kamchatsk City not found. Skipping... Processing Record 3 of Set 1 | vaini Processing Record 4 of Set 1 | cidreira Processing Record 5 of Set 1 | taolanaro City not found. Skipping... Processing Record 6 of Set 1 | kalmar Processing Record 7 of Set 1 | merauke Processing Record 8 of Set 1 | barrow Processing Record 9 of Set 1 | mahebourg Processing Record 10 of Set 1 | east london Processing Record 11 of Set 1 | albany Processing Record 12 of Set 1 | codrington Processing Record 13 of Set 1 | pangai Processing Record 14 of Set 1 | yarim Processing Record 15 of Set 1 | trelew Processing Record 16 of Set 1 | dingle Processing Record 17 of Set 1 | jacmel Processing Record 18 of Set 1 | butaritari Processing Record 19 of Set 1 | punta arenas Processing Record 20 of Set 1 | yellowknife Processing Record 21 of Set 1 | port keats Processing Record 22 of Set 1 | thinadhoo Processing Record 23 of Set 1 | masuguru Processing Record 24 of Set 1 | guerrero negro Processing Record 25 of Set 1 | shingu Processing Record 26 of Set 1 | port elizabeth Processing Record 27 of Set 1 | porto velho Processing Record 28 of Set 1 | broome Processing Record 29 of Set 1 | castro Processing Record 30 of Set 1 | katherine Processing Record 31 of Set 1 | umba Processing Record 32 of Set 1 | kahului Processing Record 33 of Set 1 | kodiak Processing Record 34 of Set 1 | goderich Processing Record 35 of Set 1 | atar Processing Record 36 of Set 1 | belushya guba City not found. Skipping... Processing Record 37 of Set 1 | port alfred Processing Record 38 of Set 1 | santa eulalia del rio City not found. Skipping... Processing Record 39 of Set 1 | avarua Processing Record 40 of Set 1 | rikitea Processing Record 41 of Set 1 | hithadhoo Processing Record 42 of Set 1 | bredasdorp Processing Record 43 of Set 1 | klaksvik Processing Record 44 of Set 1 | arraial do cabo Processing Record 45 of Set 1 | ambodifototra City not found. Skipping... Processing Record 46 of Set 1 | tasiilaq Processing Record 47 of Set 1 | soltvadkert Processing Record 48 of Set 1 | mar del plata Processing Record 49 of Set 1 | kapaa Processing Record 50 of Set 1 | karkaralinsk City not found. Skipping... Processing Record 1 of Set 2 | busselton Processing Record 2 of Set 2 | hobart Processing Record 3 of Set 2 | yaguachi Processing Record 4 of Set 2 | aiquile Processing Record 5 of Set 2 | mamallapuram Processing Record 6 of Set 2 | tuktoyaktuk Processing Record 7 of Set 2 | los llanos de aridane Processing Record 8 of Set 2 | ibra Processing Record 9 of Set 2 | ugoofaaru Processing Record 10 of Set 2 | nikolskoye Processing Record 11 of Set 2 | nanortalik Processing Record 12 of Set 2 | portland Processing Record 13 of Set 2 | lata Processing Record 14 of Set 2 | kaitangata Processing Record 15 of Set 2 | kattivakkam Processing Record 16 of Set 2 | miraflores Processing Record 17 of Set 2 | ponta do sol Processing Record 18 of Set 2 | liniere City not found. Skipping... Processing Record 19 of Set 2 | simao dias Processing Record 20 of Set 2 | saskylakh Processing Record 21 of Set 2 | vyara Processing Record 22 of Set 2 | ksenyevka City not found. Skipping... Processing Record 23 of Set 2 | karratha Processing Record 24 of Set 2 | mataura Processing Record 25 of Set 2 | thompson Processing Record 26 of Set 2 | havoysund Processing Record 27 of Set 2 | bueu Processing Record 28 of Set 2 | palana Processing Record 29 of Set 2 | iqaluit Processing Record 30 of Set 2 | saint-pierre Processing Record 31 of Set 2 | henties bay Processing Record 32 of Set 2 | miles city Processing Record 33 of Set 2 | aksay Processing Record 34 of Set 2 | toliary City not found. Skipping... Processing Record 35 of Set 2 | christchurch Processing Record 36 of Set 2 | tabiauea City not found. Skipping... Processing Record 37 of Set 2 | atuona Processing Record 38 of Set 2 | uvalde Processing Record 39 of Set 2 | shahr-e babak Processing Record 40 of Set 2 | grand gaube Processing Record 41 of Set 2 | khatanga Processing Record 42 of Set 2 | bonavista Processing Record 43 of Set 2 | ayios serafim Processing Record 44 of Set 2 | jamestown Processing Record 45 of Set 2 | sumenep Processing Record 46 of Set 2 | melfi Processing Record 47 of Set 2 | sioux lookout Processing Record 48 of Set 2 | mitsamiouli Processing Record 49 of Set 2 | inongo Processing Record 50 of Set 2 | wonthaggi Processing Record 1 of Set 3 | port moresby Processing Record 2 of Set 3 | qaanaaq Processing Record 3 of Set 3 | chuy Processing Record 4 of Set 3 | gorkovskoye City not found. Skipping... Processing Record 5 of Set 3 | provideniya Processing Record 6 of Set 3 | formoso do araguaia City not found. Skipping... Processing Record 7 of Set 3 | hangu Processing Record 8 of Set 3 | gardan diwal City not found. Skipping... Processing Record 9 of Set 3 | chadiza Processing Record 10 of Set 3 | camabatela Processing Record 11 of Set 3 | vila franca do campo Processing Record 12 of Set 3 | bluff Processing Record 13 of Set 3 | payakumbuh Processing Record 14 of Set 3 | tura Processing Record 15 of Set 3 | hovd Processing Record 16 of Set 3 | ushuaia Processing Record 17 of Set 3 | bengkulu Processing Record 18 of Set 3 | pokhara Processing Record 19 of Set 3 | zhuozhou City not found. Skipping... Processing Record 20 of Set 3 | andra Processing Record 21 of Set 3 | camopi Processing Record 22 of Set 3 | port blair Processing Record 23 of Set 3 | skalat Processing Record 24 of Set 3 | vaasa Processing Record 25 of Set 3 | sorong Processing Record 26 of Set 3 | tiksi Processing Record 27 of Set 3 | fortuna Processing Record 28 of Set 3 | vostok Processing Record 29 of Set 3 | inverell Processing Record 30 of Set 3 | tecpatan Processing Record 31 of Set 3 | carnarvon Processing Record 32 of Set 3 | marcona City not found. Skipping... Processing Record 33 of Set 3 | lorengau Processing Record 34 of Set 3 | vardo Processing Record 35 of Set 3 | amahai Processing Record 36 of Set 3 | ostrovnoy Processing Record 37 of Set 3 | sabha Processing Record 38 of Set 3 | cabo san lucas Processing Record 39 of Set 3 | provadija City not found. Skipping... Processing Record 40 of Set 3 | cayenne Processing Record 41 of Set 3 | bethel Processing Record 42 of Set 3 | norman wells Processing Record 43 of Set 3 | samalaeulu City not found. Skipping... Processing Record 44 of Set 3 | mehamn Processing Record 45 of Set 3 | saint-philippe Processing Record 46 of Set 3 | xalpatlahuac Processing Record 47 of Set 3 | nizhneyansk City not found. Skipping... Processing Record 48 of Set 3 | coihaique Processing Record 49 of Set 3 | catamarca Processing Record 50 of Set 3 | sitka Processing Record 1 of Set 4 | new norfolk Processing Record 2 of Set 4 | sanmenxia Processing Record 3 of Set 4 | acapulco Processing Record 4 of Set 4 | saleaula City not found. Skipping... Processing Record 5 of Set 4 | illoqqortoormiut City not found. Skipping... Processing Record 6 of Set 4 | cape town Processing Record 7 of Set 4 | camocim Processing Record 8 of Set 4 | flinders Processing Record 9 of Set 4 | novikovo Processing Record 10 of Set 4 | pisek Processing Record 11 of Set 4 | aswan Processing Record 12 of Set 4 | iskateley Processing Record 13 of Set 4 | amderma City not found. Skipping... Processing Record 14 of Set 4 | ahipara Processing Record 15 of Set 4 | puerto ayora Processing Record 16 of Set 4 | navalcarnero Processing Record 17 of Set 4 | lac du bonnet Processing Record 18 of Set 4 | hermanus Processing Record 19 of Set 4 | vaitupu City not found. Skipping... Processing Record 20 of Set 4 | qurayyat Processing Record 21 of Set 4 | inirida Processing Record 22 of Set 4 | saint-georges Processing Record 23 of Set 4 | georgetown Processing Record 24 of Set 4 | bambous virieux Processing Record 25 of Set 4 | pevek Processing Record 26 of Set 4 | luderitz Processing Record 27 of Set 4 | kiparissia City not found. Skipping... Processing Record 28 of Set 4 | sidi ali Processing Record 29 of Set 4 | leningradskiy Processing Record 30 of Set 4 | kargasok Processing Record 31 of Set 4 | upernavik Processing Record 32 of Set 4 | attawapiskat City not found. Skipping... Processing Record 33 of Set 4 | dikson Processing Record 34 of Set 4 | dunedin Processing Record 35 of Set 4 | airai Processing Record 36 of Set 4 | castlegar Processing Record 37 of Set 4 | pangnirtung Processing Record 38 of Set 4 | port-gentil Processing Record 39 of Set 4 | itaituba Processing Record 40 of Set 4 | kruisfontein Processing Record 41 of Set 4 | yeletskiy City not found. Skipping... Processing Record 42 of Set 4 | richards bay Processing Record 43 of Set 4 | alyangula Processing Record 44 of Set 4 | qandala Processing Record 45 of Set 4 | zhigansk Processing Record 46 of Set 4 | castelo do piaui Processing Record 47 of Set 4 | torbay Processing Record 48 of Set 4 | aklavik Processing Record 49 of Set 4 | durant Processing Record 50 of Set 4 | mount isa Processing Record 1 of Set 5 | esperance Processing Record 2 of Set 5 | rawannawi City not found. Skipping... Processing Record 3 of Set 5 | biak Processing Record 4 of Set 5 | beloha Processing Record 5 of Set 5 | oussouye Processing Record 6 of Set 5 | staryy nadym Processing Record 7 of Set 5 | chiang klang Processing Record 8 of Set 5 | gigmoto Processing Record 9 of Set 5 | byron bay Processing Record 10 of Set 5 | magura ilvei Processing Record 11 of Set 5 | nantucket Processing Record 12 of Set 5 | lebu Processing Record 13 of Set 5 | saint george Processing Record 14 of Set 5 | alofi Processing Record 15 of Set 5 | santiago del estero Processing Record 16 of Set 5 | nueva germania Processing Record 17 of Set 5 | hilo Processing Record 18 of Set 5 | berlevag Processing Record 19 of Set 5 | buta Processing Record 20 of Set 5 | plettenberg bay Processing Record 21 of Set 5 | umzimvubu City not found. Skipping... Processing Record 22 of Set 5 | ashiya Processing Record 23 of Set 5 | rexburg Processing Record 24 of Set 5 | necochea Processing Record 25 of Set 5 | taoudenni Processing Record 26 of Set 5 | havre-saint-pierre Processing Record 27 of Set 5 | samusu City not found. Skipping... Processing Record 28 of Set 5 | baruun-urt Processing Record 29 of Set 5 | yar-sale Processing Record 30 of Set 5 | port augusta Processing Record 31 of Set 5 | chumikan Processing Record 32 of Set 5 | okhotsk Processing Record 33 of Set 5 | saldanha Processing Record 34 of Set 5 | pisco Processing Record 35 of Set 5 | sarkand Processing Record 36 of Set 5 | idritsa Processing Record 37 of Set 5 | kalmunai Processing Record 38 of Set 5 | tutoia Processing Record 39 of Set 5 | hami Processing Record 40 of Set 5 | grand-lahou Processing Record 41 of Set 5 | asyut Processing Record 42 of Set 5 | abu samrah Processing Record 43 of Set 5 | axim Processing Record 44 of Set 5 | shu Processing Record 45 of Set 5 | brasileia Processing Record 46 of Set 5 | mayumba Processing Record 47 of Set 5 | alice springs Processing Record 48 of Set 5 | sao filipe Processing Record 49 of Set 5 | namtsy Processing Record 50 of Set 5 | villarrica Processing Record 1 of Set 6 | anadyr Processing Record 2 of Set 6 | saint anthony Processing Record 3 of Set 6 | longyearbyen Processing Record 4 of Set 6 | guilin Processing Record 5 of Set 6 | ribeira brava Processing Record 6 of Set 6 | tromso Processing Record 7 of Set 6 | neiafu Processing Record 8 of Set 6 | perelyub Processing Record 9 of Set 6 | hit Processing Record 10 of Set 6 | faanui Processing Record 11 of Set 6 | yulara Processing Record 12 of Set 6 | aykhal Processing Record 13 of Set 6 | rafraf Processing Record 14 of Set 6 | nyimba Processing Record 15 of Set 6 | aripuana Processing Record 16 of Set 6 | seymchan Processing Record 17 of Set 6 | mawlaik Processing Record 18 of Set 6 | abu kamal Processing Record 19 of Set 6 | makaha Processing Record 20 of Set 6 | tubinskiy Processing Record 21 of Set 6 | chokurdakh Processing Record 22 of Set 6 | alto araguaia Processing Record 23 of Set 6 | bayji Processing Record 24 of Set 6 | te anau Processing Record 25 of Set 6 | nanded City not found. Skipping... Processing Record 26 of Set 6 | altamont Processing Record 27 of Set 6 | chapais Processing Record 28 of Set 6 | sentyabrskiy City not found. Skipping... Processing Record 29 of Set 6 | hamina Processing Record 30 of Set 6 | oksfjord Processing Record 31 of Set 6 | sukabumi Processing Record 32 of Set 6 | namibe Processing Record 33 of Set 6 | ustek Processing Record 34 of Set 6 | mys shmidta City not found. Skipping... Processing Record 35 of Set 6 | plouzane Processing Record 36 of Set 6 | tsihombe City not found. Skipping... Processing Record 37 of Set 6 | labuhan Processing Record 38 of Set 6 | muros Processing Record 39 of Set 6 | olinda Processing Record 40 of Set 6 | kendari Processing Record 41 of Set 6 | kropotkin Processing Record 42 of Set 6 | souillac Processing Record 43 of Set 6 | karagay Processing Record 44 of Set 6 | chatrapur Processing Record 45 of Set 6 | ceres Processing Record 46 of Set 6 | rodrigues alves Processing Record 47 of Set 6 | imeni poliny osipenko Processing Record 48 of Set 6 | paita Processing Record 49 of Set 6 | chilca Processing Record 50 of Set 6 | santa cruz de la palma Processing Record 1 of Set 7 | sangar Processing Record 2 of Set 7 | stornoway Processing Record 3 of Set 7 | baglung Processing Record 4 of Set 7 | narsaq Processing Record 5 of Set 7 | marzuq Processing Record 6 of Set 7 | sambava Processing Record 7 of Set 7 | kupang Processing Record 8 of Set 7 | camacupa Processing Record 9 of Set 7 | tiradentes Processing Record 10 of Set 7 | tilichiki Processing Record 11 of Set 7 | batagay-alyta Processing Record 12 of Set 7 | viligili City not found. Skipping... Processing Record 13 of Set 7 | tuatapere Processing Record 14 of Set 7 | samarai Processing Record 15 of Set 7 | burica City not found. Skipping... Processing Record 16 of Set 7 | vao Processing Record 17 of Set 7 | trinidad Processing Record 18 of Set 7 | amboasary Processing Record 19 of Set 7 | pitimbu Processing Record 20 of Set 7 | ancud Processing Record 21 of Set 7 | huarmey Processing Record 22 of Set 7 | derzhavinsk Processing Record 23 of Set 7 | warqla City not found. Skipping... Processing Record 24 of Set 7 | tafalla Processing Record 25 of Set 7 | hillsborough Processing Record 26 of Set 7 | san patricio Processing Record 27 of Set 7 | isangel Processing Record 28 of Set 7 | calama Processing Record 29 of Set 7 | bonthe Processing Record 30 of Set 7 | caravelas Processing Record 31 of Set 7 | san juan Processing Record 32 of Set 7 | constitucion Processing Record 33 of Set 7 | balaipungut Processing Record 34 of Set 7 | kalashnikovo Processing Record 35 of Set 7 | marsh harbour Processing Record 36 of Set 7 | bacuit City not found. Skipping... Processing Record 37 of Set 7 | treinta y tres Processing Record 38 of Set 7 | izhma Processing Record 39 of Set 7 | barentsburg City not found. Skipping... Processing Record 40 of Set 7 | buchanan Processing Record 41 of Set 7 | mizque Processing Record 42 of Set 7 | ondangwa Processing Record 43 of Set 7 | san jeronimo Processing Record 44 of Set 7 | akureyri Processing Record 45 of Set 7 | qasigiannguit Processing Record 46 of Set 7 | iracoubo Processing Record 47 of Set 7 | sicuani Processing Record 48 of Set 7 | asau Processing Record 49 of Set 7 | uruzgan Processing Record 50 of Set 7 | hasaki Processing Record 1 of Set 8 | buraydah Processing Record 2 of Set 8 | roald Processing Record 3 of Set 8 | sal rei Processing Record 4 of Set 8 | krasyliv Processing Record 5 of Set 8 | cockburn town Processing Record 6 of Set 8 | altus Processing Record 7 of Set 8 | fairbanks Processing Record 8 of Set 8 | springdale Processing Record 9 of Set 8 | moree Processing Record 10 of Set 8 | clyde river Processing Record 11 of Set 8 | san quintin Processing Record 12 of Set 8 | ribeira grande Processing Record 13 of Set 8 | husavik Processing Record 14 of Set 8 | tautira Processing Record 15 of Set 8 | lubu Processing Record 16 of Set 8 | bakchar Processing Record 17 of Set 8 | cherskiy Processing Record 18 of Set 8 | novobirilyussy Processing Record 19 of Set 8 | grand river south east City not found. Skipping... Processing Record 20 of Set 8 | avera Processing Record 21 of Set 8 | asamankese Processing Record 22 of Set 8 | westport Processing Record 23 of Set 8 | belaya gora Processing Record 24 of Set 8 | meyungs City not found. Skipping... Processing Record 25 of Set 8 | mount gambier Processing Record 26 of Set 8 | rio grande Processing Record 27 of Set 8 | zhuanghe Processing Record 28 of Set 8 | suzhou Processing Record 29 of Set 8 | doha Processing Record 30 of Set 8 | wagar Processing Record 31 of Set 8 | malanje Processing Record 32 of Set 8 | nizwa Processing Record 33 of Set 8 | bereda Processing Record 34 of Set 8 | fort pierce Processing Record 35 of Set 8 | caxias Processing Record 36 of Set 8 | gujar khan Processing Record 37 of Set 8 | mareeba Processing Record 38 of Set 8 | itarema Processing Record 39 of Set 8 | tumannyy City not found. Skipping... Processing Record 40 of Set 8 | grindavik Processing Record 41 of Set 8 | meulaboh Processing Record 42 of Set 8 | northam Processing Record 43 of Set 8 | fukue Processing Record 44 of Set 8 | pangkalanbuun Processing Record 45 of Set 8 | agadez Processing Record 46 of Set 8 | jamiltepec Processing Record 47 of Set 8 | gijon Processing Record 48 of Set 8 | victoria Processing Record 49 of Set 8 | bogorodskoye Processing Record 50 of Set 8 | along Processing Record 1 of Set 9 | jijiga Processing Record 2 of Set 9 | ust-maya Processing Record 3 of Set 9 | jerome Processing Record 4 of Set 9 | santa flavia Processing Record 5 of Set 9 | qaqortoq Processing Record 6 of Set 9 | nouakchott Processing Record 7 of Set 9 | challans Processing Record 8 of Set 9 | puerto el triunfo Processing Record 9 of Set 9 | ucluelet Processing Record 10 of Set 9 | dunmore east Processing Record 11 of Set 9 | ahuimanu Processing Record 12 of Set 9 | jiayuguan Processing Record 13 of Set 9 | valparaiso Processing Record 14 of Set 9 | hanchuan Processing Record 15 of Set 9 | olafsvik Processing Record 16 of Set 9 | verkhoyansk Processing Record 17 of Set 9 | tambilil Processing Record 18 of Set 9 | hofn Processing Record 19 of Set 9 | haines junction Processing Record 20 of Set 9 | ruatoria City not found. Skipping... Processing Record 21 of Set 9 | omaruru Processing Record 22 of Set 9 | san carlos de bariloche Processing Record 23 of Set 9 | okha Processing Record 24 of Set 9 | sechura Processing Record 25 of Set 9 | yegorlykskaya Processing Record 26 of Set 9 | namatanai Processing Record 27 of Set 9 | shar Processing Record 28 of Set 9 | juneau Processing Record 29 of Set 9 | phu ly Processing Record 30 of Set 9 | halifax Processing Record 31 of Set 9 | palmer Processing Record 32 of Set 9 | hailun Processing Record 33 of Set 9 | nome Processing Record 34 of Set 9 | altay Processing Record 35 of Set 9 | katsuura Processing Record 36 of Set 9 | buala Processing Record 37 of Set 9 | osterhofen Processing Record 38 of Set 9 | canandaigua Processing Record 39 of Set 9 | hakkari Processing Record 40 of Set 9 | mandalgovi Processing Record 41 of Set 9 | phan thiet Processing Record 42 of Set 9 | wamba Processing Record 43 of Set 9 | tawkar City not found. Skipping... Processing Record 44 of Set 9 | payson Processing Record 45 of Set 9 | sur Processing Record 46 of Set 9 | birjand Processing Record 47 of Set 9 | nuuk Processing Record 48 of Set 9 | bure Processing Record 49 of Set 9 | george Processing Record 50 of Set 9 | puerto colombia Processing Record 1 of Set 10 | mrirt City not found. Skipping... Processing Record 2 of Set 10 | soma Processing Record 3 of Set 10 | sept-iles Processing Record 4 of Set 10 | lensk Processing Record 5 of Set 10 | anshun Processing Record 6 of Set 10 | sampit Processing Record 7 of Set 10 | sinop Processing Record 8 of Set 10 | aksu Processing Record 9 of Set 10 | ayan Processing Record 10 of Set 10 | korla Processing Record 11 of Set 10 | dukat Processing Record 12 of Set 10 | torit Processing Record 13 of Set 10 | luau Processing Record 14 of Set 10 | conakry Processing Record 15 of Set 10 | krasnoselkup Processing Record 16 of Set 10 | lompoc Processing Record 17 of Set 10 | redlands Processing Record 18 of Set 10 | jacareacanga Processing Record 19 of Set 10 | pandan Processing Record 20 of Set 10 | bojnurd Processing Record 21 of Set 10 | chagda City not found. Skipping... Processing Record 22 of Set 10 | caranavi Processing Record 23 of Set 10 | sao joao da barra Processing Record 24 of Set 10 | honiara Processing Record 25 of Set 10 | todos santos Processing Record 26 of Set 10 | tyler Processing Record 27 of Set 10 | palabuhanratu City not found. Skipping... Processing Record 28 of Set 10 | muroto Processing Record 29 of Set 10 | pospelikha Processing Record 30 of Set 10 | erzin Processing Record 31 of Set 10 | kloulklubed Processing Record 32 of Set 10 | victoria point Processing Record 33 of Set 10 | hambantota Processing Record 34 of Set 10 | paraiso Processing Record 35 of Set 10 | syracuse Processing Record 36 of Set 10 | hirara Processing Record 37 of Set 10 | malwan City not found. Skipping... Processing Record 38 of Set 10 | pochutla Processing Record 39 of Set 10 | port hardy Processing Record 40 of Set 10 | noumea Processing Record 41 of Set 10 | killybegs Processing Record 42 of Set 10 | simao Processing Record 43 of Set 10 | lloydminster Processing Record 44 of Set 10 | mogok Processing Record 45 of Set 10 | totma Processing Record 46 of Set 10 | kibala Processing Record 47 of Set 10 | chifeng Processing Record 48 of Set 10 | talnakh Processing Record 49 of Set 10 | naze Processing Record 50 of Set 10 | demyansk Processing Record 1 of Set 11 | doka Processing Record 2 of Set 11 | janovice nad uhlavou Processing Record 3 of Set 11 | rundu Processing Record 4 of Set 11 | codajas Processing Record 5 of Set 11 | bam Processing Record 6 of Set 11 | karaul City not found. Skipping... Processing Record 7 of Set 11 | bangalore Processing Record 8 of Set 11 | moranbah Processing Record 9 of Set 11 | lanzhou Processing Record 10 of Set 11 | majene Processing Record 11 of Set 11 | santa maria ixhuatan Processing Record 12 of Set 11 | mao Processing Record 13 of Set 11 | jizan Processing Record 14 of Set 11 | povenets Processing Record 15 of Set 11 | grenville Processing Record 16 of Set 11 | karauzyak City not found. Skipping... Processing Record 17 of Set 11 | ewa beach Processing Record 18 of Set 11 | pacific grove Processing Record 19 of Set 11 | coquimbo Processing Record 20 of Set 11 | port macquarie Processing Record 21 of Set 11 | windhoek Processing Record 22 of Set 11 | farso Processing Record 23 of Set 11 | luena Processing Record 24 of Set 11 | debno Processing Record 25 of Set 11 | poros Processing Record 26 of Set 11 | sala Processing Record 27 of Set 11 | puerto madryn Processing Record 28 of Set 11 | manado Processing Record 29 of Set 11 | kavieng Processing Record 30 of Set 11 | pontes e lacerda Processing Record 31 of Set 11 | baie-comeau Processing Record 32 of Set 11 | skjervoy Processing Record 33 of Set 11 | puerto carreno Processing Record 34 of Set 11 | riviere-au-renard Processing Record 35 of Set 11 | calbuco Processing Record 36 of Set 11 | kieta Processing Record 37 of Set 11 | belmonte Processing Record 38 of Set 11 | lar Processing Record 39 of Set 11 | tulsipur Processing Record 40 of Set 11 | bara Processing Record 41 of Set 11 | aflu City not found. Skipping... Processing Record 42 of Set 11 | lokosovo Processing Record 43 of Set 11 | nelson bay Processing Record 44 of Set 11 | valdivia Processing Record 45 of Set 11 | almaznyy Processing Record 46 of Set 11 | the valley Processing Record 47 of Set 11 | canutama Processing Record 48 of Set 11 | ulladulla Processing Record 49 of Set 11 | oktyabrskiy Processing Record 50 of Set 11 | butembo Processing Record 1 of Set 12 | choconta Processing Record 2 of Set 12 | mercedes Processing Record 3 of Set 12 | moose factory Processing Record 4 of Set 12 | hamilton Processing Record 5 of Set 12 | rio cuarto Processing Record 6 of Set 12 | uttarkashi Processing Record 7 of Set 12 | gombe Processing Record 8 of Set 12 | adrar Processing Record 9 of Set 12 | zyryanka Processing Record 10 of Set 12 | dabakala Processing Record 11 of Set 12 | padang Processing Record 12 of Set 12 | lasa Processing Record 13 of Set 12 | egvekinot Processing Record 14 of Set 12 | tekirdag Processing Record 15 of Set 12 | khrebtovaya Processing Record 16 of Set 12 | angoche Processing Record 17 of Set 12 | margate Processing Record 18 of Set 12 | san lawrenz Processing Record 19 of Set 12 | alabaster Processing Record 20 of Set 12 | hay river Processing Record 21 of Set 12 | ixtapa Processing Record 22 of Set 12 | nemuro Processing Record 23 of Set 12 | khangarh Processing Record 24 of Set 12 | geraldton Processing Record 25 of Set 12 | morrope Processing Record 26 of Set 12 | port lincoln Processing Record 27 of Set 12 | gravdal Processing Record 28 of Set 12 | tarakan Processing Record 29 of Set 12 | san juan de la maguana Processing Record 30 of Set 12 | waidhofen Processing Record 31 of Set 12 | alotenango Processing Record 32 of Set 12 | tefe Processing Record 33 of Set 12 | nishihara Processing Record 34 of Set 12 | makakilo city Processing Record 35 of Set 12 | el vigia Processing Record 36 of Set 12 | walvis bay Processing Record 37 of Set 12 | tallahassee Processing Record 38 of Set 12 | rungata City not found. Skipping... Processing Record 39 of Set 12 | beisfjord Processing Record 40 of Set 12 | yakima Processing Record 41 of Set 12 | kidal Processing Record 42 of Set 12 | klimovo Processing Record 43 of Set 12 | dolbeau City not found. Skipping... Processing Record 44 of Set 12 | puerto asis Processing Record 45 of Set 12 | waingapu Processing Record 46 of Set 12 | trairi Processing Record 47 of Set 12 | hihifo City not found. Skipping... Processing Record 48 of Set 12 | canyon Processing Record 49 of Set 12 | mildura Processing Record 50 of Set 12 | ojinaga Processing Record 1 of Set 13 | funadhoo Processing Record 2 of Set 13 | uhlove Processing Record 3 of Set 13 | indramayu Processing Record 4 of Set 13 | maarianhamina Processing Record 5 of Set 13 | black river Processing Record 6 of Set 13 | half moon bay Processing Record 7 of Set 13 | bubaque Processing Record 8 of Set 13 | la paz Processing Record 9 of Set 13 | tunxi City not found. Skipping... Processing Record 10 of Set 13 | faya Processing Record 11 of Set 13 | sao felix do xingu Processing Record 12 of Set 13 | tocopilla Processing Record 13 of Set 13 | sao caetano de odivelas Processing Record 14 of Set 13 | poum Processing Record 15 of Set 13 | saint-paul Processing Record 16 of Set 13 | paamiut Processing Record 17 of Set 13 | west wendover Processing Record 18 of Set 13 | port hedland Processing Record 19 of Set 13 | ostrovskoye Processing Record 20 of Set 13 | lolua City not found. Skipping... Processing Record 21 of Set 13 | luanda Processing Record 22 of Set 13 | puerto narino Processing Record 23 of Set 13 | shemgang Processing Record 24 of Set 13 | bambanglipuro Processing Record 25 of Set 13 | muyezerskiy Processing Record 26 of Set 13 | wanaka Processing Record 27 of Set 13 | salalah Processing Record 28 of Set 13 | bila tserkva Processing Record 29 of Set 13 | pyshma Processing Record 30 of Set 13 | mackay Processing Record 31 of Set 13 | san cristobal Processing Record 32 of Set 13 | roma Processing Record 33 of Set 13 | nurota Processing Record 34 of Set 13 | manjakandriana Processing Record 35 of Set 13 | nueva loja Processing Record 36 of Set 13 | canals Processing Record 37 of Set 13 | comodoro rivadavia Processing Record 38 of Set 13 | vila velha Processing Record 39 of Set 13 | nacala ------------------------------------------------ Data Retrieval Complete ------------------------------------------------
# Length of Latitude and Temprature
len(lat)
len(temp)
639
# Convert raw data to dataframe.
city_weather_df = pd.DataFrame({
"City": cities,
"Lat": lat,
"lng": lon,
"Max Temp": temp_max,
"Humidity": humidity,
"Cloudiness": cloudiness,
"Wind Speed": wind_speed,
"Country": country,
"Date": date,
})
# Drop any cities that were skipped because they could not be found using the OpenWeatherMap API.
city_weather_df = city_weather_df.dropna(how="any")
print(city_weather_df.count())
City 585 Lat 585 lng 585 Max Temp 585 Humidity 585 Cloudiness 585 Wind Speed 585 Country 585 Date 585 dtype: int64
# Export the city data into a .csv file.
city_weather_df.to_csv("./output_data/city_weather_data.csv", index=False)
# Display the DataFrame
weather_df = pd.read_csv("./output_data/city_weather_data.csv")
weather_df.head()
City | Lat | lng | Max Temp | Humidity | Cloudiness | Wind Speed | Country | Date | |
---|---|---|---|---|---|---|---|---|---|
0 | severo-kurilsk | 50.68 | 156.12 | 50.49 | 78.0 | 100.0 | 11.39 | RU | 1.592884e+09 |
1 | vaini | -21.20 | -175.20 | 78.80 | 78.0 | 40.0 | 10.29 | TO | 1.592884e+09 |
2 | cidreira | -30.18 | -50.21 | 64.44 | 92.0 | 5.0 | 16.89 | BR | 1.592884e+09 |
3 | kalmar | 56.66 | 16.36 | 55.99 | 100.0 | 0.0 | 3.36 | SE | 1.592884e+09 |
4 | merauke | -8.47 | 140.33 | 81.84 | 76.0 | 20.0 | 17.05 | ID | 1.592884e+09 |
This section is skiped Because no cities that have humidity > 100%.
date_time = datetime.date(datetime.now())
# Create a scatter plot for latitude vs max temperature.
x_values = weather_df['Lat']
y_values = weather_df['Max Temp']
fig1, ax1 = plt.subplots(figsize=(10,8))
plt.scatter(x_values, y_values,alpha = 0.80, edgecolors = "k", linewidths = 1)
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title(f"City Latitude vs Max Temperature {date_time}")
plt.grid()
plt.savefig("./output_data/latitude_vs_temperature.png", bbox_inches="tight")
plt.show()
# Create a scatter plot for latitude vs humidity.
x_values = weather_df['Lat']
y_values = weather_df['Humidity']
fig1, ax1 = plt.subplots(figsize=(10, 8))
plt.scatter(x_values, y_values,alpha = 0.80, edgecolors = "k", linewidths = 1)
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title(f'City Latitude vs Humidity {date_time}')
plt.grid()
plt.savefig("./output_data/latitude_vs_humidity.png", bbox_inches="tight")
plt.show()
# Create a scatter plot for latitude vs cloudiness.
x_values = weather_df['Lat']
y_values = weather_df['Cloudiness']
fig1, ax1 = plt.subplots(figsize=(10,8))
plt.scatter(x_values, y_values,alpha = 0.80, edgecolors = "k", linewidths = 1)
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.title(f'City Latitude vs Cloudiness {date_time}')
plt.grid()
plt.savefig("./output_data/latitude_vs_cloudiness.png", bbox_inches="tight")
plt.show()
# Create a scatter plot for latitude vs wind speed.
x_values = weather_df['Lat']
y_values = weather_df['Wind Speed']
fig1, ax1 = plt.subplots(figsize=(10,8))
plt.scatter(x_values, y_values,alpha = 0.80, edgecolors = "k", linewidths = 1)
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.title(f'City Latitude vs Wind Speed {date_time}')
plt.grid()
plt.savefig("./output_data/latitude_vs_wind_speed.png", bbox_inches="tight")
plt.show()
# OPTIONAL: Create a function to create Linear Regression plots
def createLinearRegressionPlot(x_values, y_values, x_label, y_label, hemisphere, line_placement, ylim=None):
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
# Get regression values
regress_values = x_values * slope + intercept
# Create line equation string
line_eq = "y = " + str(round(slope,2)) + "x +" + str(round(intercept,2))
fig1, ax1 = plt.subplots(figsize=(10,8))
plt.scatter(x_values, y_values,alpha = 0.80, edgecolors = "k", linewidths = 1)
plt.xlabel(x_label,fontsize = 14)
plt.ylabel(y_label,fontsize = 14)
if ylim is not None:
plt.ylim(0, ylim)
today = datetime.date(datetime.now())
plt.title(f"{hemisphere} Hemisphere - {x_label} vs {y_label} {today}",fontsize = 15)
plt.annotate(line_eq,xy=(10, 50),fontsize=22,xycoords='data',xytext=(0.4, 0.4), textcoords='axes fraction',horizontalalignment='right', verticalalignment='top',color="red")
plt.annotate(line_eq,xy=(-50, 40),fontsize=22,xycoords='data',xytext=(0.4, 0.3), textcoords='axes fraction',horizontalalignment='right', verticalalignment='top',color="red")
plt.annotate(line_eq,xy=(10, 25),fontsize=22,xycoords='data',xytext=(0.4, 0.4), textcoords='axes fraction',horizontalalignment='right', verticalalignment='top',color="red")
# Print r square value
print(f"The r-squared is: {rvalue**2}")
correlation = st.pearsonr(x_values,y_values)
print(f"The correlation between both factors is {round(correlation[0],2)}")
return plt.plot(x_values,regress_values,"r-")
# Create Northern and Southern Hemisphere DataFrames
northern_hemisphere_weather_df = weather_df.loc[weather_df["Lat"] >= 0]
southern_hemisphere_weather_df = weather_df.loc[weather_df["Lat"] < 0]
# Create a scatter plot for latitude vs max temp (northern hemisphere)
x_values = northern_hemisphere_weather_df['Lat']
y_values = northern_hemisphere_weather_df['Max Temp']
createLinearRegressionPlot(x_values, y_values, "Lat", "Max Temp (F)", "Northern",(0, 0))
plt.savefig("./output_data/Northern_Hemisphere_Max_lat_lin.png", bbox_inches="tight")
plt.show()
The r-squared is: 0.430773149999661 The correlation between both factors is -0.66
# Create a scatter plot for latitude vs cloudiness (southern hemisphere)
x_values = southern_hemisphere_weather_df['Lat']
y_values = southern_hemisphere_weather_df['Cloudiness']
createLinearRegressionPlot(x_values, y_values, "Latitude", "Cloudiness(%)", "Southern",(-45, 60))
plt.savefig("./output_data/southern_Hemisphere_Max_lat_lin.png", bbox_inches="tight")
plt.show()
The r-squared is: 0.0022767336118374466 The correlation between both factors is -0.05
# Create a scatter plot for latitude vs humditiy (northern hemisphere)
x_values = northern_hemisphere_weather_df['Lat']
y_values = northern_hemisphere_weather_df['Humidity']
createLinearRegressionPlot(x_values, y_values, "Latitude", "Humidity (%)", "Northern",(40, 20))
plt.savefig("./output_data/Northern_Hemisphere_Humidity_Latitude_Linear.png", bbox_inches="tight")
plt.show()
The r-squared is: 0.0026924601243572105 The correlation between both factors is -0.05
# Create a scatter plot for latitude vs humditiy (southern hemisphere)
x_values1 = southern_hemisphere_weather_df['Lat']
y_values1 = southern_hemisphere_weather_df['Humidity']
createLinearRegressionPlot(x_values, y_values, "Latitude", "Humidity (%)", "Southern",(40, 20), 150)
plt.savefig("./output_data/southern_humudity_Hemisphere_Max_lat_lin.png", bbox_inches="tight")
plt.show()
The r-squared is: 0.0026924601243572105 The correlation between both factors is -0.05
# Create a scatter plot for latitude vs cloudiness (northern hemisphere)
x_values = northern_hemisphere_weather_df['Lat']
y_values = northern_hemisphere_weather_df['Cloudiness']
createLinearRegressionPlot(x_values, y_values, "Latitude", "Cloudiness(%)", "Northern",(30, 50))
plt.savefig("./output_data/Northern Hemisphere_Cloudiness_Latitude_Linear.png", bbox_inches="tight")
plt.show()
The r-squared is: 0.004772807202547001 The correlation between both factors is -0.07
# Create a scatter plot for latitude vs cloudiness (southern hemisphere)
x_values = southern_hemisphere_weather_df['Lat']
y_values = southern_hemisphere_weather_df['Cloudiness']
createLinearRegressionPlot(x_values, y_values, "Latitude", "Cloudiness(%)", "Southern",(-45, 60))
plt.savefig("./output_data/Southern_Hemisphere_Cloudiness_Latitude_Linear.png", bbox_inches="tight")
plt.show()
The r-squared is: 0.0022767336118374466 The correlation between both factors is -0.05
# Create a scatter plot for latitude vs wind speed(northern hemisphere)
x_values = northern_hemisphere_weather_df['Lat']
y_values = northern_hemisphere_weather_df['Wind Speed']
createLinearRegressionPlot(x_values, y_values, "Latitude", "Wind Speed (mph)", "Northern",(20, 25))
plt.savefig("./output_data/Northern_Hemisphere_Wind_Speed_Latitude_Lin.png", bbox_inches="tight")
plt.show()
The r-squared is: 0.0005450822316844967 The correlation between both factors is 0.02
# Create a scatter plot for latitude vs wind speed (southern hemisphere)
x_values = southern_hemisphere_weather_df['Lat']
y_values = southern_hemisphere_weather_df['Wind Speed']
createLinearRegressionPlot(x_values, y_values, "Latitude", "Wind Speed (mph)", "Southern",(-40, 25), ylim=40)
plt.savefig("./output_data/Southern_Hemisphere_Wind_Speed_Latitude_Linear_Regression.png", bbox_inches="tight")
plt.show()
The r-squared is: 0.009772113510229401 The correlation between both factors is -0.1