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'])


The timezone for the request was America/New_York

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


The windspeed in Cleveland is currently 3.45 mph
While it currently feels 84.11 it is actually 80.73 degrees F

In [5]:
print("The moon in Cleveland is currently", cleveland_weather['daily']['data'][0]['moonPhase']*100, "percent full")


The moon in Cleveland is currently 5.0 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")


The max temp in Cleveland today was 80.83 and the min was 69.61 which is a difference of 11.22 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))


Here is your high temps for the week, including today:
Day 0: 80.83 I would say that's pretty warm.
Day 1: 86.4 I would say that's freaking hot.
Day 2: 85.63 I would say that's freaking hot.
Day 3: 86.8 I would say that's freaking hot.
Day 4: 77.82 I would say that's pretty warm.
Day 5: 77.5 I would say that's pretty warm.
Day 6: 82.15 I would say that's pretty warm.
Day 7: 88.47 I would say that's freaking hot.

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]:
dict_keys(['precipType', 'dewPoint', 'precipIntensity', 'humidity', 'apparentTemperature', 'windSpeed', 'ozone', 'time', 'cloudCover', 'icon', 'windBearing', 'temperature', 'summary', 'precipProbability', 'visibility', 'pressure'])

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


Conditions in Miami for the rest of the day:
20:00: 86.31°F and clear.
21:00: 85.05°F and clear.
22:00: 84.26°F and clear.
23:00: 83.44°F and partly cloudy.
00:00: 82.94°F and partly cloudy.

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


On Christmas of 1980 it was partly cloudy starting in the afternoon. The high temperature for that day was 41.58°F.
On Christmas of 1990 it was foggy in the evening. The high temperature for that day was 32.27°F.
On Christmas of 2000 it was mostly cloudy until evening. The high temperature for that day was 42.25°F.
On Christmas of 2010 it was foggy starting in the evening. The high temperature for that day was 41°F.

In [115]:



Out[115]:
'Foggy starting in the evening.'

In [ ]: