3 hour forecasts with openweathermap and python
This post is for people who wish to explore the 3 hour forecast features of openweathermap.org
I’ve kind of found the learning process fun so here is it is:
#Documentation is at https://pyowm.readthedocs.org/en/latest/pyowm.html #github documentation is at https://github.com/csparpa/pyowm/wiki/Usage-examples #retrieve weather data forecaster = owm.three_hours_forecast(location_var) forecast = forecaster.get_forecast() weather_list = forecast.get_weathers() #id for the location_name --- the registry doesn't seem to be working 😦 #location_id = owm.city_id_registry().id_for(location_name) #print(location_id) #But we can get the location id by the following fc_JSON=json.loads(forecast.to_JSON()) #print(fc_JSON['Location']['name'],str(fc_JSON['Location']['ID'])) location_name=fc_JSON['Location']['name'] location_id=str(fc_JSON['Location']['ID']) #Don't forget to add time difference in when displaying dates and times! strDate =(datetime.datetime.strptime(forecast.get_reception_time('iso'),"%Y-%m-%d %H:%M:%S+00") + datetime.timedelta(hours=10)).strftime("%Y-%m-%d %H:00")
Once you have called a list of weathers, then you can work you way through that list using a FOR LOOP. I personally find loading each weather to JSON makes it just a bit easier to manipulate, but there are other functions available through the pyown api that could be of use too:
#Set up the comparative vars total_rain =0 temp_min = 999 temp_min_time='sometime' temp_max_time='sometime' temp_max = -999 hum_min = 999 hum_max = -999 hum_min_time='sometime' hum_max_time='sometime' cloud_min = 999 cloud_max = -999 cloud_min_time='sometime' cloud_max_time='sometime' for x in weather_list: #Dont forget to put the date and time calculations in your timezone! ref_date = datetime.datetime.strptime(x.get_reference_time('iso'),"%Y-%m-%d %H:%M:%S+00") + datetime.timedelta(hours=10) if (ref_date.strftime("%Y-%m-%d")==time.strftime("%Y-%m-%d")) : x_data = json.loads(x.to_JSON()) #print(str(x_data['humidity'])) #print(str(datetime.datetime.strptime(x.get_reference_time('iso').split( )[1],"%H:%M:%S+00"))) #print(str(ref_date.strftime("%Y-%m-%d")) + ' - ' + x_data['detailed_status'] + ' at ' + str(datetime.datetime.strptime(x.get_reference_time('iso').split( )[1],"%H:%M:%S+00").strftime("%I%p").lstrip('0')) + ' -- ' + str(x.get_temperature('celsius')['temp']) + 'C') #Record if it is a max or min temperature, humidity, cloud and the time it occurs if (x.get_temperature('celsius')['temp_min']<temp_min) : temp_min = x.get_temperature('celsius')['temp_max'] temp_min_time = int(ref_date.strftime("%H")) #str(ref_date.strftime("%I%p")).lstrip('0').lower() if (x.get_temperature('celsius')['temp_max']>temp_max) : temp_max = x.get_temperature('celsius')['temp_max'] temp_max_time = int(ref_date.strftime("%H")) # str(ref_date.strftime("%I%p")).lstrip('0').lower() if (x_data['humidity']<hum_min) : hum_min = x_data['humidity'] hum_min_time = int(ref_date.strftime("%H")) # str(ref_date.strftime("%I%p")).lstrip('0').lower() if (x_data['humidity']>hum_max) : hum_max = x_data['humidity'] hum_max_time = int(ref_date.strftime("%H")) # str(ref_date.strftime("%I%p")).lstrip('0').lower() if (x_data['clouds']<cloud_min) : cloud_min = x_data['clouds'] cloud_min_time = int(ref_date.strftime("%H")) # str(ref_date.strftime("%I%p")).lstrip('0').lower() if (x_data['clouds']>cloud_max) : cloud_max = x_data['clouds'] cloud_max_time = int(ref_date.strftime("%H")) # str(ref_date.strftime("%I%p")).lstrip('0').lower() if ('3h'in x_data['rain']) : #print(str(x_data['rain']['3h']) + 'mm') total_rain+=float(x_data['rain']['3h'])
I was particularly interested in the rainfall data. I’ll just point out that the rainfall data is only evident if there is rainfall predicted for that weather. The code to check that is as follows:
if ('3h'in x_data['rain']) : #print(str(x_data['rain']['3h']) + 'mm') total_rain+=float(x_data['rain']['3h'])
The output that a finished version of the above code produced is as follows:
Port Hacking today= temp 22.8C at 10am & 23.5C at 7pm, humidity 95% at 10am & 88% at 7pm, clouds 0% at 10am & 24% at 7pm; Atmospheric summary is {clear sky from 10am (22.76C) ; few clouds from 7pm (23.54C)}/ tomorrow= temp 29.5C at 1pm & 19.8C at 10pm, humidity 64% at 1pm & 100% at 7pm, clouds 0% at 7am & 100% at 10pm, total rain 18.24mm; Atmospheric summary is {few clouds from 1am (23.6C) ; clear sky from 4am (23.85C) ; few clouds from 10am (27.57C) ; light rain from 4pm (23.34C) 1.33mm; moderate rain from 7pm (21.59C) 16.91mm} … http://openweathermap.org/city/2152674
itsericcheung View All →
Loves Jesus and loves telling people about Him.