Graded = 7/7
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 [3]:
import config
import requests
weather_key = config.weather_key
# api key - latitude, longitude, time (epoch)
#without time parameter
response = requests.get('https://api.forecast.io/forecast/' + weather_key + '/39.0068,76.7791')
#on my birthdate - time parameter
#response = requests.get('https://api.forecast.io/forecast/' + weather_key + '/39.0068,76.7791,765407717')
data = response.json()
# print(data)
2) What's the current wind speed? How much warmer does it feel than it actually is?
In [4]:
response = requests.get('https://api.forecast.io/forecast/' + weather_key + '/39.0068,76.7791')
data = response.json()
# the current date's weath
print(data['currently'])
print("The current wind speed is", data['currently']['windSpeed'], "and it feels", data['currently']['apparentTemperature']-data['currently']['temperature'], "degrees warmer than it 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?
In [5]:
# the moon is not currently printing
print("Currently", data['daily']['data'][0]['moonPhase'], "of the moon is showing.")
4) What's the difference between the high and low temperatures for today?
In [6]:
print("There is a ", data['daily']['data'][0]['temperatureMax'] - data['daily']['data'][0]['temperatureMin'], "degree 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.
In [7]:
dailies = data['daily']['data']
import time
for day in dailies:
date = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(day['temperatureMaxTime']))
if(day['temperatureMax']) > 95:
print(date, "will be a hot day of",day['temperatureMax'], "degrees" )
else:
print(date, "will be a warm day of", day['temperatureMax'], "degrees")
#print(dailies)
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 [8]:
response = requests.get('https://api.forecast.io/forecast/' + weather_key + '/25.7617,80.1918')
data = response.json()
#print(data.keys())
#print(data['hourly'])
current_hour = 1
#print(data['hourly']['data'])
for hour in data['hourly']['data']:
# need to make it so that this only looks at the rest of today
if current_hour < 12:
print("In the next", current_hour, "hour it will be", hour['apparentTemperature'], "in Miami, Florida")
current_hour = current_hour + 1
In [ ]:
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 [36]:
central_park_lat = str(40.7829)
central_park_long = str(73.9654)
# 12/25/1980 @ 12:00am (UTC)
#unix_xmas = str(346550400)
#12/25/1990
#662083200
# 12/25/2000
#977702400
#start year = 1980
year = 1980
# unix conversion of chrimas for 1980, 1990 and 2000.
#http://www.unixtimestamp.com/index.php website used to convert dates
for xmas in [346550400, 662083200, 977702400]:
unix_xmas = str(xmas)
response = requests.get('https://api.forecast.io/forecast/' + weather_key + '/'+ central_park_lat + ',' + central_park_long + ',' + unix_xmas)
response = response.json()
daily_data = response['daily']['data']
# print(daily_data)
for day in daily_data:
print("It was an average of", day['temperatureMax'] - day['temperatureMin'], "degrees on Christmas in", year)
year = year + 10
#print(response['apparentTemperatureMax'])
In [ ]: