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


{'ozone': 273.72, 'pressure': 1004.14, 'humidity': 0.22, 'cloudCover': 0.02, 'precipIntensity': 0, 'windBearing': 317, 'precipProbability': 0, 'dewPoint': 45.04, 'time': 1465823489, 'apparentTemperature': 84.91, 'icon': 'clear-day', 'summary': 'Clear', 'windSpeed': 8.46, 'temperature': 88.21}
The current wind speed is 8.46 and it feels -3.299999999999997 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.")


Currently 0.28 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.")


There is a  15.019999999999996 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)


2016-06-13 07:00:00 will be a warm day of 89.07 degrees
2016-06-14 08:00:00 will be a hot day of 97.97 degrees
2016-06-15 08:00:00 will be a hot day of 99.09 degrees
2016-06-16 07:00:00 will be a hot day of 101.33 degrees
2016-06-17 08:00:00 will be a hot day of 97.98 degrees
2016-06-18 08:00:00 will be a hot day of 95.58 degrees
2016-06-19 08:00:00 will be a warm day of 92.77 degrees
2016-06-20 08:00:00 will be a warm day of 94.71 degrees

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 the next 1 hour it will be 109.05 in Miami, Florida
In the next 2 hour it will be 107.38 in Miami, Florida
In the next 3 hour it will be 105.56 in Miami, Florida
In the next 4 hour it will be 103.91 in Miami, Florida
In the next 5 hour it will be 102.59 in Miami, Florida
In the next 6 hour it will be 101.46 in Miami, Florida
In the next 7 hour it will be 100.21 in Miami, Florida
In the next 8 hour it will be 98.66 in Miami, Florida
In the next 9 hour it will be 97.09 in Miami, Florida
In the next 10 hour it will be 95.76 in Miami, Florida
In the next 11 hour it will be 94.46 in Miami, Florida

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


It was 21.0 degrees on Christmas in 1980
It was 9.860000000000003 degrees on Christmas in 1990
It was 17.2 degrees on Christmas in 2000

In [ ]: