Skip to content

Instantly share code, notes, and snippets.

@timtomch
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timtomch/3b6d2bab99f87c34bf37 to your computer and use it in GitHub Desktop.
Save timtomch/3b6d2bab99f87c34bf37 to your computer and use it in GitHub Desktop.
Sample Python script to use with the Hello World Code4Lib code club
# weather-int.py - get average temperature data from the World Bank ClimateWeb API
#
# Usage: python weather-int.py XXX where XXX is a ISO 3-letter country code
# e.g. CAN, USA, GBR
#
# Collectively written during the Feb 5-6 Software Carpentry workshop
# at the University of Toronto iSchool.
# https://swcarpentry.github.io/2015-02-05-toronto
# Edited by Thomas Guignard <tom@timtom.ca> for the Code4Lib Monday Night code club
# Version: March 30, 2015
# Import libraries
import urllib # Useful to process web queries
import csv # To process CSV data (comma-separated values)
import sys # To interact with the OS, in this case to get command-line arguments
# Define base URL for getting data. {0} is place holder for 3-letter country code
URL = 'http://climatedataapi.worldbank.org/climateweb' + \
'/rest/v1/country/cru/tas/year/{0}.csv'
# Define function get_data
# Usage: get_data(<XXX>) where XXX is a string containing a 3-letter country code
def get_data(where):
# format the URL by replacing the place holder with the 3-letter country code
actual = URL.format(where)
# get the CSV data from the formatted URL
raw = urllib.urlopen(actual)
# skip the first line of the CSV file, as it contains headers
header = raw.readline()
# run the CSV data through the csv.reader function
cooked = csv.reader(raw)
# init values
total = 0.0
num_seen = 0
# loop through all lines in the CSV file
for line in cooked:
# the first column contains the year
year = int(line[0])
# the second column contains the average temperature for that year
temp = float(line[1])
# add this year's temperature to the total so far
total = total + temp
# increment the number of readings
num_seen = num_seen + 1
# once the loop has run, compute simple average
# first, check if we have at least one value to average
if num_seen > 0:
# compute simple average
ave_temp = total / num_seen
# print the 3-letter country code and the computed average on stdout
print '{0},{1}'.format(where, ave_temp)
else:
# if there are no values to average, throw an error of type "Warning"
raise Warning('EmptyCountryFile')
# This is the main code body
# load the provided arguments into the countries array (more than one country can be provided)
# (arguments are not checked for validtiy, valid 3-letter codes are assumed)
countries = sys.argv[1:]
# print a line containing headers for returned values on stdout
print 'country,ave surface temp (C)'
# loop through the arguments array
for country in countries:
# for each country, run the get_data() function
# exceptions are use to avoid breaking the program if one country code is invalid
try:
get_data(country)
# if one of the country codes is invalid, print a line with a value of NULL
except Warning:
print '{0},NULL'.format(country)
# END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment