-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscratch.py
More file actions
190 lines (146 loc) · 5.55 KB
/
Copy pathscratch.py
File metadata and controls
190 lines (146 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import time
import os
import csv
#!!they will import a package incorrectly by misspelling it or by using the wrong alias
# Incorporated citipy to determine city based on latitude and longitude
from citipy import citipy
# Output File (CSV)
#output_data_file = 'output_data/cities.csv'
#!!They will export the CSV to the wrong folder name or use the wrong structure
# 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)
# Starting URL for Weather Map API Call
url = 'http://api.openweathermap.org/data/2.5/weather?units=Imperial&APPID=' + '4429ca781d436624e4abcafd3849dd3d' #!!error in the api key due to extra space
# 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))
# Starting URL for Weather Map API Call
url = "http://api.openweathermap.org/data/2.5/weather?units=Imperial&APPID=" + "4429ca781d436624e4abcafd3849dd3d"
# List of city data
city_data = []
# Print to logger
print("Beginning Data Retrieval ")
print("-----------------------------")
# Create counters
record_count = 1
set_count = 1
# Loop through all the cities in our list
for i, city in enumerate(cities):
##!!They will misform the for loop by not using the proper whitespace
# Group cities in sets of 50 for logging purposes
if (i % 50 == 0 and i >= 50):
set_count += 1
record_count = 0
# Create endpoint URL with each city
city_url = url + "&q=" + city
# Log the url, record, and set numbers
print("Processing Record %s of Set %s | %s" % (record_count, set_count, city))
# Add 1 to the record count
record_count += 1
# Run an API request for each of the cities
try:
# Parse the JSON and retrieve data
city_weather = requests.get(city_url).json()
# Parse out the max temp, humidity, and cloudiness
city_lat = city_weather["coord"]["lat"]
city_lng = city_weather["coord"]["lon"]
city_max_temp = city_weather["main"]["temp_max"]
city_humidity = city_weather["main"]["humidity"]
city_clouds = city_weather["clouds"]["all"]
city_wind = city_weather["wind"]["speed"]
city_country = city_weather["sys"]["country"]
city_date = city_weather["dt"]
# Append the City information into city_data list
city_data.append({"City": city,
"Lat": city_lat,
"Lng": city_lng,
"Max Temp": city_max_temp,
"Humidity": city_humidity,
"Cloudiness": city_clouds,
"Wind Speed": city_wind,
"Country": city_country,
"Date": city_date})
# If an error is experienced, skip the city
except:
print("City not found. Skipping...")
pass
# Indicate that Data Loading is complete
print("-----------------------------")
print("Data Retrieval Complete ")
print("-----------------------------")
# Convert array of JSONs into Pandas DataFrame
city_data_pd = pd.DataFrame(city_data)
# Extract relevant fields from the data frame
lats = city_data_pd["Lat"]
max_temps = city_data_pd["Max Temp"]
humidity = city_data_pd["Humidity"]
cloudiness = city_data_pd["Cloudiness"]
wind_speed = city_data_pd["Wind Speed"]
# Export the City_Data into a csv
#city_data_pd.to_csv(output_data_file, index_label="City_ID")
# Show Record Count
city_data_pd.count()
# Build scatter plot for latitude vs. temperature
plt.scatter(lats,
max_temps,
edgecolor="black", linewidths=1, marker="o",
alpha=0.8, label="Cities")
# Incorporate the other graph properties
plt.title("City Latitude vs. Max Temperature (%s)" % time.strftime("%x"))
plt.ylabel("Max Temperature (F)")
plt.xlabel("Latitude")
plt.grid(True)
# Save the figure
direct = os.path.dirname(__file__)
filename = os.path.join(direct, "../Fig1.png")
plt.savefig(filename)
# Show plot
plt.show()
# Build the scatter plots for latitude vs. humidity
plt.scatter(lats,
humidity,
edgecolor="black", linewidths=1, marker="o",
alpha=0.8, label="Cities")
# Incorporate the other graph properties
plt.title("City Latitude vs. Humidity (%s)" % time.strftime("%x"))
plt.ylabel("Humidity (%)")
plt.xlabel("Latitude")
plt.grid(True)
# Save the figure
filename2 = os.path.join(direct, "../Fig2.png")
plt.savefig(filename2)
# Show plot
plt.show()