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 [2]:
#API key: e63bc0790517df7cd7bbb5f455e7f0ce
#Place in decimal degrees: Berlin = {'Latitude': 52.5243700, 'Longitude': 13.4105300}
import requests
weather_response = requests.get ('https://api.forecast.io/forecast/e63bc0790517df7cd7bbb5f455e7f0ce/52.5243700,13.4105300')
weather_data = weather_response.json()

#STEP 1: Exploring the data stucture and checking whether correct place selected:
print(type(weather_data))
print("The following dictionaries are available for further endeavours:", weather_data.keys())
print("The place selected is in the timezone:", weather_data['timezone'])


<class 'dict'>
dict_keys(['flags', 'daily', 'longitude', 'hourly', 'offset', 'currently', 'latitude', 'timezone'])
Europe/Berlin

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


In [18]:
weather_data_current = weather_data['currently']
#print(weather_data_current.keys())

print("The current wind speed  in Berlin is", weather_data_current['windSpeed'], "miles per hour.")
temperature_difference = weather_data_current['apparentTemperature'] - weather_data_current['temperature']
if temperature_difference > 0:
    print("The felt temperature is", weather_data_current['apparentTemperature'], "degrees Farenheit, whereas the actual temperature is", weather_data_current['temperature'], "degrees Fahrenheit. So it feels", temperature_difference, "degrees Fahrenheit warmer than it actually is." )
if temperature_difference == 0:
    print("The felt temperature of", weather_data_current['apparentTemperature'], "degrees Fahrenheit is the same as the actual temperature of", weather_data_current['temperature'], "degrees Fahrenheit.")
else:
    print("The felt temperature is", weather_data_current['apparentTemperature'], "degrees Farenheit, whereas the actual temperature is", weather_data_current['temperature'], "degrees Fahrenheit. So it feels", temperature_difference, "degrees Fahrenheit colder than it actually is." )


The current wind speed  in Berlin is 9.3 miles per hour.
The felt temperature of 70.1 degrees Fahrenheit is the same as the actual temperature of 70.1 degrees Fahrenheit.

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 [90]:
#from the documentation:  a value of 0 corresponds to a new moon, 0.25 to a first quarter moon, 0.5 to a full moon, 
#and 0.75 to a last quarter moon. 
#The ranges in between these represent waxing crescent, waxing gibbous, waning gibbous, and waning crescent moons.

weather_data_daily = weather_data['daily']
#print(weather_data_daily.keys())
weather_data_today = weather_data_daily['data'][0]
#print(weather_data_today['moonPhase'])
if weather_data_today['moonPhase'] == 0:
    print("It's new moon, so no moon is currently visible in Berlin.")
if weather_data_today['moonPhase'] > 0 and weather_data_today['moonPhase'] < 0.25:
    print("Something between nothing and a quarter of the moon is visible in Berlin, this phase is called waxing crescent.")
if weather_data_today['moonPhase'] == 0.25:
    print("The first quarter of the moon is currently visible in Berlin.")
if weather_data_today['moonPhase'] > 0.25 and weather_data_today['moonPhase'] < 0.5:
    print("Something between a quarter of the moon and a full moon is visible in Berlin, this phase is called waxing gibbous.")
if weather_data_today['moonPhase'] == 0.5:
    print("It's a full moon that is visible in Berlin.")
if weather_data_today['moonPhase'] > 0.5 and weather_data_today['moonPhase'] < 0.75:
    print("Something less than a full moon is visible in Berlin, but yet more than a quarter. This phase is called waning gibbous.")
if weather_data_today['moonPhase'] == 0.75:
    print("The last quarter of the moon is currently visible in Berlin.")
if weather_data_today['moonPhase'] > 0.75:
    print("Less than the moon's last quarter is visible in Berlin, soon there'll be new moon, this phase is called waning crescent.")


The first quarter of the moon is currently visible in Berlin.

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


In [28]:
#print(weather_data_today.keys())
temp_min_max_diff = weather_data_today['temperatureMax'] - weather_data_today['temperatureMin']
print("The difference between the highest and lowest temperature today in Berlin is", temp_min_max_diff, "degrees Fahrenheit.")


The difference between the highest and lowest temperature today in Berlin is 18.870000000000005 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 [91]:
weather_forecast = weather_data['daily']['data']
hot_threshold = 80
cold_threshold = 68

day_count = 0
for day in weather_forecast:
    if day_count == 0:
        day_temp_max = day['temperatureMax']
        print("The maximum temperature in Berlin today is", day_temp_max, "degrees Fahrenheit.")
        day_count = day_count + 1
    elif day_count == 1:
        day_temp_max = day['temperatureMax']
        print("The maximum temperature in Berlin", day_count, "day from now is", day_temp_max, "degrees Fahrenheit.")
        day_count = day_count + 1
    elif day_count > 1:
        day_temp_max = day['temperatureMax']
        print("The maximum temperature in Berlin", day_count, "days from now is", day_temp_max, "degrees Fahrenheit.")
        day_count = day_count + 1
    if day_temp_max > hot_threshold:
        print("That's quite hot!")
    if day_temp_max > cold_threshold and day_temp_max < hot_threshold:
        print("That's nicely warm!")
    if day_temp_max < cold_threshold:
        print("That's pretty chilly!")


The maximum temperature in Berlin today is 71.23 degrees Fahrenheit.
That's nicely warm!
The maximum temperature in Berlin 1 day from now is 70.73 degrees Fahrenheit.
That's nicely warm!
The maximum temperature in Berlin 2 days from now is 70.59 degrees Fahrenheit.
That's nicely warm!
The maximum temperature in Berlin 3 days from now is 69.48 degrees Fahrenheit.
That's nicely warm!
The maximum temperature in Berlin 4 days from now is 73.53 degrees Fahrenheit.
That's nicely warm!
The maximum temperature in Berlin 5 days from now is 70.01 degrees Fahrenheit.
That's nicely warm!
The maximum temperature in Berlin 6 days from now is 67.36 degrees Fahrenheit.
That's pretty chilly!
The maximum temperature in Berlin 7 days from now is 65.91 degrees Fahrenheit.
That's pretty chilly!

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 [107]:
#Place in decimal degrees: Miami = {'Latitude': 25.7742700, 'Longitude': -80.1936600}
import requests
weather_Miami_response = requests.get ('https://api.forecast.io/forecast/e63bc0790517df7cd7bbb5f455e7f0ce/25.7742700,-80.1936600')
weather_Miami_data = weather_Miami_response.json()
#print(weather_Miami_data['timezone'])
#print(weather_Miami_data.keys())

weather_data_Miami_day = weather_Miami_data['hourly']['data']
print(weather_data_Miami_day[0])
#print(weather_data_Miami_current.keys())


{'humidity': 0.65, 'windBearing': 50, 'apparentTemperature': 95.64, 'precipProbability': 0.45, 'precipIntensity': 0.0092, 'icon': 'rain', 'cloudCover': 0.56, 'visibility': 9.98, 'dewPoint': 73.83, 'precipType': 'rain', 'pressure': 1020.02, 'windSpeed': 5.91, 'ozone': 284.36, 'time': 1465750800, 'summary': 'Light Rain', 'temperature': 87.28}

In [108]:
hour_count = 0
for hour in weather_data_Miami_day:
    #limiting results to the current day, otherwise it would run until midnight of the following day
    if hour_count <= 24:
        if hour['cloudCover'] > 0.5:
            print(hour_count, "hours past midnight it's", hour['temperature'], "degrees Fahrenheit and cloudy.")
            hour_count = hour_count + 1
        else:
            print(hour_count, "hours past midnight it's", hour['temperature'], "degrees Fahrenheit.")
            hour_count = hour_count + 1


0 hours past midnight it's 87.28 degrees Fahrenheit and cloudy.
1 hours past midnight it's 86.17 degrees Fahrenheit and cloudy.
2 hours past midnight it's 85.72 degrees Fahrenheit and cloudy.
3 hours past midnight it's 85.09 degrees Fahrenheit and cloudy.
4 hours past midnight it's 84.34 degrees Fahrenheit and cloudy.
5 hours past midnight it's 83.37 degrees Fahrenheit and cloudy.
6 hours past midnight it's 82.14 degrees Fahrenheit and cloudy.
7 hours past midnight it's 81.33 degrees Fahrenheit and cloudy.
8 hours past midnight it's 80.44 degrees Fahrenheit and cloudy.
9 hours past midnight it's 79.84 degrees Fahrenheit and cloudy.
10 hours past midnight it's 79.29 degrees Fahrenheit and cloudy.
11 hours past midnight it's 78.58 degrees Fahrenheit and cloudy.
12 hours past midnight it's 77.92 degrees Fahrenheit and cloudy.
13 hours past midnight it's 76.8 degrees Fahrenheit.
14 hours past midnight it's 76.1 degrees Fahrenheit.
15 hours past midnight it's 75.44 degrees Fahrenheit and cloudy.
16 hours past midnight it's 75.07 degrees Fahrenheit and cloudy.
17 hours past midnight it's 76 degrees Fahrenheit and cloudy.
18 hours past midnight it's 77.21 degrees Fahrenheit.
19 hours past midnight it's 79.19 degrees Fahrenheit.
20 hours past midnight it's 81.4 degrees Fahrenheit.
21 hours past midnight it's 83.14 degrees Fahrenheit and cloudy.
22 hours past midnight it's 84.97 degrees Fahrenheit and cloudy.
23 hours past midnight it's 86.52 degrees Fahrenheit and cloudy.
24 hours past midnight it's 87.15 degrees Fahrenheit and cloudy.

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!


In [67]:
# Central Park: Latitude:40.769362 Longitude: -73.9779°
dates = [{'date': '1980', 'unix': 346588200},
         {'date': '1990', 'unix': 662121000},
         {'date': '2000', 'unix': 977740200}
        ]

for timepoint in dates:
    weather_central_response = requests.get('https://api.forecast.io/forecast/e63bc0790517df7cd7bbb5f455e7f0ce/40.769362,-73.9779,'+ str(timepoint['unix']) +'')
    weather_central_data = weather_central_response.json()
    #print(weather_central_data.keys())
    #print(weather_central_data['timezone'])

    #From the documentation: "The currently data point will refer to the time provided, rather than the current time."
    weather_central_christmas = weather_central_data['currently']
    print("The temperature in Central Park on Christmas Day in", timepoint['date'], "was", weather_central_christmas['temperature'], "degrees Fahrenheit.")


The temperature in Central Park on Christmas Day in 1980 was 14.97 degrees Fahrenheit.
The temperature in Central Park on Christmas Day in 1990 was 25.48 degrees Fahrenheit.
The temperature in Central Park on Christmas Day in 2000 was 21.76 degrees Fahrenheit.

In [ ]: