Submit this and your other two notebooks (Spotify + NYT) to GitHub by Monday morning. If you're struggling with the previous assignment I recommend trying this one out instead, it's a lot simpler! I'll send out some readings later in the week, too.
HOMEWORK 06
You'll be using the Dark Sky Forecast API from Forecast.io, available at https://developer.forecast.io. It's a pretty simple API, but be sure to read the documentation!
1) Make a request from the Forecast.io API for where you were born (or lived, or want to visit!).
Tip: Once you've imported the JSON into a variable, check the timezone's name to make sure it seems like it got the right part of the world! Tip 2: How is north vs. south and east vs. west latitude/longitude represented? Is it the normal North/South/East/West?
2) What's the current wind speed? How much warmer does it feel than it actually is?
3) The first daily forecast is the forecast for today. For the place you decided on up above, how much of the moon is currently visible?
4) What's the difference between the high and low temperatures for today?
5) Loop through the daily forecast, printing out the next week's worth of predictions. I'd like to know the high temperature for each day, and whether it's hot, warm, or cold, based on what temperatures you think are hot, warm or cold.
6) What's the weather looking like for the rest of today in Miami, Florida? I'd like to know the temperature for every hour, and if it's going to have cloud cover of more than 0.5 say "{temperature} and cloudy" instead of just the temperature.
7) What was the temperature in Central Park on Christmas Day, 1980? How about 1990? 2000?
Tip: You'll need to use UNIX time, which is the number of seconds since January 1, 1970. Google can help you convert a normal date! Tip: You'll want to use Forecast.io's "time machine" API at https://developer.forecast.io/docs/v2
In [1]:
import requests
api='53343f8442d30d598f50f5910124610a'
url='https://api.forecast.io/forecast/'
# this is the lat/long for cleveland, oh
lat='41.4993'
long='-81.6944'
In [2]:
response=requests.get(url+api+'/'+lat+','+long)
cleveland_weather=response.json()
In [3]:
print("The timezone for the request was", cleveland_weather['timezone'])
In [4]:
print("The windspeed in Cleveland is currently", cleveland_weather['currently']['windSpeed'], "mph")
print("While it currently feels", cleveland_weather['currently']['apparentTemperature'], "it is actually", cleveland_weather['currently']['temperature'], "degrees F")
In [5]:
print("The moon in Cleveland is currently", cleveland_weather['daily']['data'][0]['moonPhase']*100, "percent full")
In [6]:
max_temp=cleveland_weather['daily']['data'][0]['temperatureMax']
min_temp=cleveland_weather['daily']['data'][0]['temperatureMin']
print("The max temp in Cleveland today was", max_temp, "and the min was", min_temp, "which is a difference of", '%.2f' % (max_temp-min_temp), "degrees")
In [12]:
def temp_feeling(temp):
if temp <= 32:
return "freezing cold!"
elif temp <= 50:
return "pretty cold."
elif temp <= 65:
return "a little chill."
elif temp <= 75:
return "downright nice."
elif temp <= 85:
return "pretty warm."
else:
return "freaking hot."
In [15]:
print("Here is your high temps for the week, including today:")
for day, forecast in enumerate(cleveland_weather['daily']['data']):
temp=forecast['temperatureMax']
print("Day", str(day)+":", temp, "I would say that's", temp_feeling(temp))
In [73]:
# coords for miami florida
lat='25.7617'
long='-80.1918'
response=requests.get(url+api+'/'+lat+','+long)
miami_weather=response.json()
In [74]:
miami_weather['hourly']['data'][0].keys()
Out[74]:
In [75]:
def is_midnight(hour):
# hour=datetime.datetime.fromtimestamp(int(time['time'])).strftime('%H')
if hour=='00':
return True
else:
return False
In [76]:
def time_to_hour(time):
hour=datetime.datetime.fromtimestamp(int(time['time'])).strftime('%H')
return hour
In [89]:
import datetime
print('Conditions in Miami for the rest of the day:')
for hour in miami_weather['hourly']['data']:
clock=time_to_hour(hour)
print(clock+':00:', str(hour['temperature'])+'°F and', hour['summary'].lower()+'.')
if is_midnight(clock):
break
In [108]:
def date_to_unixtime(date):
import time
unix_date=int(time.mktime(datetime.datetime.strptime(str(date), "%Y%m%d").timetuple()))
return str(unix_date)
In [123]:
# this way of looping through dates is pretty terrible
api='53343f8442d30d598f50f5910124610a'
url='https://api.forecast.io/forecast/'
date=19801225
lat='40.7829'
long='73.9654'
while date <= 20101225:
response=requests.get(url+api+'/'+lat+','+long+','+date_to_unixtime(date))
christmas_weather=response.json()
summary=christmas_weather['daily']['data'][0]['summary']
max_temp=christmas_weather['daily']['data'][0]['temperatureMax']
print("On Christmas of "+str(date)[0:4], "it was", summary.lower(), "The high temperature for that day was", str(max_temp)+'°F.')
date+=100000
In [115]:
Out[115]:
In [ ]: