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]:
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.")
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)
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.")
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
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
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")
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")
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")
In [ ]: