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]:
apikey = '34b41fe7b9db6c1bd5f8ea3492bca332'
# TA-COMMENT: Nice!
coordinates = {'San Antonio': '29.4241,-98.4936', 'Miami': '25.7617,-80.1918', 'Central Park': '40.7829,-73.9654'}
In [3]:
import requests
url = 'https://api.forecast.io/forecast/' + apikey + '/' + coordinates['San Antonio']
response = requests.get(url)
data = response.json()
In [10]:
# #Is it in my time zone?
# #temp. Answer: dict
# print(type(data))
# #temp. Answer: ['offset', 'latitude', 'hourly', 'flags', 'minutely', 'longitude', 'timezone', 'daily', 'currently']
# print(data.keys())
# #temp. Answer: dict
# print(type(data['currently']))
# #temp. Answer: ['windSpeed', 'time', 'dewPoint', 'icon', 'temperature', 'apparentTemperature', 'precipProbability',
#'visibility', 'cloudCover', 'nearestStormDistance', 'pressure', 'windBearing', 'ozone', 'humidity', 'precipIntensity',
#'summary', 'nearestStormBearing']
# print(data['currently'].keys())
# #temp. It's in my time zone!
# print(data['currently']['time'])
#Oh, this would have been easier:
#temp. Answer: America/Chicago
print(data['timezone'])
2) What's the current wind speed? How much warmer does it feel than it actually is?
In [22]:
print('The current wind speed is', data['currently']['windSpeed'], 'miles per hour.')
In [49]:
print('It feels', round(data['currently']['apparentTemperature'] - data['currently']['temperature'], 2), 'degrees Fahrenheit warmer 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?
In [31]:
# #temp. Answer: dict
# print(type(data['daily']))
# #temp. Answer: ['summary', 'data', 'icon']
# print(data['daily'].keys())
# #temp. Answer: list
# print(type(data['daily']['data']))
# #temp. It's a list of dictionaries
# #this time means Wed, 08 Jun 2016 05:00:00 GMT, which is currently today
# print(data['daily']['data'][0])
# #this time means Thu, 09 Jun 2016 05:00:00 GMT
# print(data['daily']['data'][1])
# #temp. Answer: 8
# print(len(data['daily']['data']))
# #temp. Answer: ['windSpeed', 'time', 'sunsetTime', 'precipIntensityMaxTime', 'apparentTemperatureMax', 'windBearing',
# #'temperatureMinTime', 'precipIntensityMax', 'precipProbability', 'sunriseTime', 'temperatureMin',
# #'apparentTemperatureMaxTime', 'precipIntensity', 'apparentTemperatureMinTime', 'temperatureMax', 'dewPoint',
# #'temperatureMaxTime', 'icon', 'moonPhase', 'precipType', 'visibility', 'cloudCover', 'pressure',
# #'apparentTemperatureMin', 'ozone', 'humidity', 'summary']
# print(data['daily']['data'][0].keys())
In [48]:
today_moon = data['daily']['data'][0]['moonPhase']
print(100 * (1 - abs(1 - (today_moon * 2))), 'percent of the moon is visible today.')
4) What's the difference between the high and low temperatures for today?
In [55]:
print('The difference between today\'s high and low temperatures is', round(data['daily']['data'][0]['temperatureMax'] - data['daily']['data'][0]['temperatureMin'], 2), 'degrees Fahrenheit.')
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 [66]:
daily_forecast = data['daily']['data']
print('Starting with today\'s, the forecasts for the next week are for highs of:')
for day in daily_forecast:
if 85 <= day['temperatureMax']:
warmth = 'hot'
elif 70 <= day['temperatureMax'] < 85:
warmth = 'warm'
else:
warmth = 'cold'
print(day['temperatureMax'], 'degrees Fahrenheit, a pretty', warmth, '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 [67]:
fl_url = 'https://api.forecast.io/forecast/' + apikey + '/' + coordinates['Miami']
fl_response = requests.get(url)
fl_data = fl_response.json()
In [74]:
# #temp. Answer: dict
# print(type(fl_data['hourly']))
# #temp. Answer: ['summary', 'data', 'icon']
# print(fl_data['hourly'].keys())
# #temp. Answer: list
# print(type(fl_data['hourly']['data']))
# #temp. Answer: 49
# print(len(fl_data['hourly']['data']))
# #temp. It's a list of dictionaries
# #the top of this hour
# print(fl_data['hourly']['data'][0])
# #the top of next hour
# print(fl_data['hourly']['data'][1])
# #temp. Answer: ['precipType', 'time', 'apparentTemperature', 'windSpeed', 'icon', 'summary', 'precipProbability',
# #'visibility', 'cloudCover', 'pressure', 'windBearing', 'ozone', 'humidity', 'precipIntensity', 'temperature',
# #'dewPoint']
# print(fl_data['hourly']['data'][0].keys())
In [92]:
# # how many hours are left in the day in EDT: (24 - ((time % 86400)/3600 - 4))
# times = [1465423200, 1465426800]
# for time in times:
# print (24 - ((time % 86400)/3600 - 4))
hourly_data = fl_data['hourly']['data']
hours_left = range(int(24 - ((hourly_data[0]['time'] % 86400)/3600 - 4)))
print('Starting with this hour, the hourly forecasts for the rest of the day are for:')
for hour in hours_left:
if hourly_data[hour]['cloudCover'] > .5:
print(hourly_data[hour]['temperature'], 'degrees Fahrenheit and cloudy')
else:
print(hourly_data[hour]['temperature'], 'degrees Fahrenheit')
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 [4]:
decades = range(3)
for decade in decades:
cp_url = 'https://api.forecast.io/forecast/' + apikey + '/' + coordinates['Central Park'] + ',' + str(10 * decade + 1980) + '-12-25T12:00:00'
cp_response = requests.get(cp_url)
cp_data = cp_response.json()
print('On Christmas Day in', str(1980 + decade * 10) + ', the high in Central Park was', cp_data['daily']['data'][0]['temperatureMax'], 'degrees Fahrenheit.')
In [ ]:
In [ ]: