-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherPy.py
More file actions
205 lines (131 loc) · 4.3 KB
/
WeatherPy.py
File metadata and controls
205 lines (131 loc) · 4.3 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
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python
# coding: utf-8
# # 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.
# In[38]:
# 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 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)
# In[39]:
weather_api_key
# ## Generate Cities List
# In[40]:
# 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)
# ### 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).
#
# In[41]:
url = 'http://api.openweathermap.org/data/2.5/weather?units=Imperial&APPID=' + weather_api_key
number = 1
city_name = []
lat = []
lng = []
temp = []
humid = []
clouds = []
wind = []
print('------------------------------')
print('Beginning Data Retrieval')
print('------------------------------')
for city in cities:
try:
city_data = (requests.get(url + '&q=' + city)).json()
city_name.append(city_data['name'])
lat.append(city_data['coord']['lat'])
lng.append(city_data['coord']['lon'])
temp.append(city_data['main']['temp'])
humid.append(city_data['main']['humidity'])
clouds.append(city_data['clouds']['all'])
wind.append(city_data['wind']['speed'])
print(f'Processing record {number} of set {len(cities)} | {city}')
number = number + 1
except KeyError:
print(f'Missing data in city number {number} of {len(cities)}. | Skipping {city}')
number = number + 1
print('------------------------------')
print('Data Retrieval Complete')
print('------------------------------')
# ### Convert Raw Data to DataFrame
# * Export the city data into a .csv.
# * Display the DataFrame
# In[42]:
cdf = pd.DataFrame({'City': city_name,
'Latitude': lat,
'Longitude': lng,
'Temperature': temp,
'Humidity': humid,
'Cloudiness': clouds,
'Wind Speed': wind})
pd.DataFrame.to_csv(cdf, 'city_data.csv')
cdf
# ### 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
# In[43]:
plt.scatter(cdf['Latitude'], cdf['Temperature'])
plt.title(f'Latitude vs. Temperature')
plt.xlabel('Latitude')
plt.ylabel('Temperature (F)')
plt.grid(True)
plt.savefig('latvstemp.png')
# #### Latitude vs. Humidity Plot
# In[44]:
#Plot latitude vs humidity and save as .png
plt.scatter(cdf['Latitude'], cdf['Humidity'])
plt.title(f'Latitude vs. Humidity')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.grid(True)
plt.savefig('latvshumid.png')
# #### Latitude vs. Cloudiness Plot
# In[45]:
#Plot latitude vs cloudiness and save as .png
plt.scatter(cdf['Latitude'], cdf['Cloudiness'])
plt.title('Latitude vs. Cloudiness')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.grid(True)
plt.savefig('latvscloud.png')
# #### Latitude vs. Wind Speed Plot
# In[47]:
plt.scatter(cdf['Latitude'], cdf['Wind Speed'])
plt.title('Latitude vs. Wind Speed')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.grid(True)
plt.savefig('latvswind.png')
# In[ ]:
# In[ ]:
# In[ ]: