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?


In [1]:
import requests
url="https://api.forecast.io/forecast/64f4867f7d4c86182f3d1c6ed881dbfc/17.3850,78.4867"
response=requests.get(url)
data=response.json()
data.keys()
data['currently'].keys()


Out[1]:
dict_keys(['precipIntensity', 'ozone', 'summary', 'precipProbability', 'icon', 'visibility', 'windSpeed', 'dewPoint', 'pressure', 'humidity', 'windBearing', 'apparentTemperature', 'time', 'cloudCover', 'temperature', 'precipType'])

2) What's the current wind speed? How much warmer does it feel than it actually is?


In [2]:
print("The current wind speed is",data['currently']['windSpeed'],"miles per hour.")
apparentTemperature=data['currently']['apparentTemperature']
temperature=data['currently']['temperature']
if apparentTemperature-temperature > 0:
    print("It feels", "%.2f" %(apparentTemperature-temperature),"degrees warmer.")
else:
    print("It feels", "%.2f" %(temperature-apparentTemperature),"degrees cooler.")


The current wind speed is 9.67 miles per hour.
It feels 0.00 degrees cooler.

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?


In [3]:
data['daily'].keys()
mooncover=data['daily']['data'][0]['moonPhase']
if mooncover == 0:
    print("Today is a new moon day. The mooncover is",mooncover)
if mooncover > 0 and mooncover <0.25:
    print("The moon is in waxing crescent phase. The mooncover is",mooncover)
if mooncover == 0.25:
    print("Today is a first quarter moon. The mooncover is",mooncover)
if mooncover > 0.25 and mooncover <0.5:
    print("The moon is in waxing gibbous phase. The mooncover is",mooncover)
if mooncover == 0.5:
    print("Today is a full moon day. The mooncover is",mooncover)
if mooncover > 0.5 and mooncover<0.75:
    print("The moon is in waning gibbous phase. The mooncover is",mooncover)
if mooncover == 0.75:
    print("Today is a last quarter moon. The mooncover is",mooncover)    
if mooncover >0.75:
    print("The moon is in waning crescent phase. The mooncover is",mooncover)


The moon is in waxing gibbous phase. The mooncover is 0.43

4) What's the difference between the high and low temperatures for today?


In [5]:
low=data['daily']['data'][0]['temperatureMin']
print("The low temperature is",low)
high=data['daily']['data'][0]['temperatureMax']
print("The high temperature is",high)
print("There is a difference of",high-low,"degrees between the high and low temperatures today.")


The low temperature is 78.36
The high temperature is 86.92
There is a difference of 8.560000000000002 degrees between the high and low temperatures 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.


In [6]:
x=0
hotlimit=86
warmlimit=68
for date in data['daily']['data']:
    if data['daily']['data'][x]['temperatureMax'] > hotlimit:
        print("The high for day",x,"of the mext week is",data['daily']['data'][x]['temperatureMax'],"Fahrenheit")
        print("It's a hot day. ")
    if data['daily']['data'][x]['temperatureMax'] < hotlimit and data['daily']['data'][x]['temperatureMax'] > warmlimit :
        print("The high for day",x,"of the mext week is",data['daily']['data'][x]['temperatureMax'],"Fahrenheit")
        print("It's a warm day. ")
    if data['daily']['data'][x]['temperatureMax'] < warmlimit:
        print("The high for day",x,"of the mext week is",data['daily']['data'][x]['temperatureMax'],"Fahrenheit")
        print("It's a cold day. ")
    x=x+1


The high for day 0 of the mext week is 86.92 Fahrenheit
It's a hot day. 
The high for day 1 of the mext week is 97.88 Fahrenheit
It's a hot day. 
The high for day 2 of the mext week is 99.25 Fahrenheit
It's a hot day. 
The high for day 3 of the mext week is 100.14 Fahrenheit
It's a hot day. 
The high for day 4 of the mext week is 95.39 Fahrenheit
It's a hot day. 
The high for day 5 of the mext week is 81.87 Fahrenheit
It's a warm day. 
The high for day 6 of the mext week is 83.7 Fahrenheit
It's a warm day. 
The high for day 7 of the mext week is 86.06 Fahrenheit
It's a hot day. 

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.


In [7]:
import requests
url="https://api.forecast.io/forecast/64f4867f7d4c86182f3d1c6ed881dbfc/25.7617,-80.1918"
miamiresponse=requests.get(url)
miamidata=miamiresponse.json()
miamidata.keys()

noofhoursinaday=0
miamidata['hourly']['data']
for count in miamidata['hourly']['data']:
    if miamidata['hourly']['data'][noofhoursinaday]['cloudCover'] > 0.5:
        print("The temperature for hour",noofhoursinaday+1,"is",miamidata['hourly']['data'][noofhoursinaday]['temperature'],"degrees F and cloudy.")
    else:
        print("The temperature for hour",noofhoursinaday+1,"is",miamidata['hourly']['data'][noofhoursinaday]['temperature'],"degrees F.")
    noofhoursinaday=noofhoursinaday+1
    
    if noofhoursinaday>23:
        break


The temperature for hour 1 is 82.02 degrees F.
The temperature for hour 2 is 80.56 degrees F.
The temperature for hour 3 is 79.61 degrees F.
The temperature for hour 4 is 78.76 degrees F.
The temperature for hour 5 is 77.6 degrees F.
The temperature for hour 6 is 76.63 degrees F.
The temperature for hour 7 is 75.77 degrees F.
The temperature for hour 8 is 75.21 degrees F.
The temperature for hour 9 is 76.58 degrees F.
The temperature for hour 10 is 78.55 degrees F.
The temperature for hour 11 is 81.02 degrees F.
The temperature for hour 12 is 83.02 degrees F.
The temperature for hour 13 is 84.37 degrees F.
The temperature for hour 14 is 85.79 degrees F.
The temperature for hour 15 is 87.18 degrees F.
The temperature for hour 16 is 88.2 degrees F.
The temperature for hour 17 is 89.11 degrees F.
The temperature for hour 18 is 87.9 degrees F.
The temperature for hour 19 is 86.62 degrees F.
The temperature for hour 20 is 85.16 degrees F.
The temperature for hour 21 is 83.94 degrees F.
The temperature for hour 22 is 82.68 degrees F.
The temperature for hour 23 is 81.3 degrees F.
The temperature for hour 24 is 80.13 degrees F.

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 [16]:
import requests
timestamp='346550400'
url="https://api.forecast.io/forecast/64f4867f7d4c86182f3d1c6ed881dbfc/40.7829,-73.9654,"+timestamp
cpresponse=requests.get(url)
cpdata=cpresponse.json()
print("The temperature at midnight of Christmas in 1980 was",cpdata['currently']['temperature'],"degrees F")


The temperature at midnight of Christmas in 1980 is 33.96 degrees F

In [8]:
import requests
timestamp='662083200'
url="https://api.forecast.io/forecast/64f4867f7d4c86182f3d1c6ed881dbfc/40.7829,-73.9654,"+timestamp
cpresponse=requests.get(url)
cpdata=cpresponse.json()
print("The temperature at midnight of Christmas in 1990 was",cpdata['currently']['temperature'],"degrees F")


The temperature at midnight of Christmas in 1990 was 36.74 degrees F

In [9]:
import requests
timestamp='977702400'
url="https://api.forecast.io/forecast/64f4867f7d4c86182f3d1c6ed881dbfc/40.7829,-73.9654,"+timestamp
cpresponse=requests.get(url)
cpdata=cpresponse.json()
print("The temperature at midnight of Christmas in 2000 was",cpdata['currently']['temperature'],"degrees F")


The temperature at midnight of Christmas in 2000 was 20.52 degrees F

In [ ]: