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