Weather Forecast V2 (Python)

Doh! I confused Kelvin units with Fahrenheit units which was totally throwing out the Celsius units. I’ve also started to modify the the first forecast to display more information, thinking of going down the “email me if the temp drops below…” route.

#!/bin/python3

#geektechstuff

#print weather location for Manchester, UK

#imports required modules

import json, requests

current_w2=”

#calls on the API of openweathermap.org

#ID is for Manchester, see http://openweathermap.org/api

url=’http://api.openweathermap.org/data/2.5/forecast?id=3333169&APPID=XXXXXX’

response=requests.get(url)

response.raise_for_status()

#Loads the JSON

weatherData=json.loads(response.text)

w=weatherData[‘list’]

print(“Weather for Manchester, UK”)

print(”)

#Uses the next forecast!

print(w[0][‘dt_txt’])

print(w[0][‘weather’][0][‘main’],’-‘,w[0][‘weather’][0][‘description’])

#Converts Kevlin into Celsius, as I’m more of a celsius person

temp_c=w[0][‘main’][‘temp’]

temp_c=float(temp_c)

temp_c=temp_c-273.15

print(‘Average temp in celsius:’,temp_c)

print(”)

#Checks the next temp

if temp_c <= 0:

print(‘WARNING: FREEZING POINT. Make sure home heating is on.’)

elif temp_c >18:

        print(‘It\’s warm out today’)

#Bit more detail depending on the weather forecase

current_w=w[0][‘weather’][0][‘main’]

current_w2=current_w.lower()

if current_w2 == ‘snow’:

        print(‘Snow; do you really need to go out?’)

print(”)

#Cycles through the next 10 3hr blocks which is roughly the next 30hrs

weather=range(1,10)

for x in weather:

    print(w[x][‘dt_txt’])

    print(w[x][‘weather’][0][‘main’],’-‘,w[x][‘weather’][0][‘description’])

    #Converts Kevlin into Celsius, as I’m more of a celsius person

    temp_c=w[x][‘main’][‘temp’]

    temp_c=float(temp_c)

    temp_c=temp_c-273.15

    print(‘Average temp in celsius:’,temp_c)

    x=x+1

    print(”)    

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.