The world bank provides climate data via it's web
http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/var/year/iso3.ext
var is either tas or pr ext is usually CSV iso3 is the ISO standard 3 letter code for the country of interset (in capitals)
In [18]:
# Python requets Library lets us get data straight from a URL
import requests
In [19]:
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GBR.csv"
response = requests.get(url)
if response.status_code != 200:
print ('Failed to get data:', response.status_code)
else:
print ('First 100 characters of the data are:')
print ( response.text[:100])
Gettting Status codes for Guatemala (Country Code is GTM)
Fetch rainfall for Afghanistan between 1980 and 1999
In [20]:
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GTM.csv"
response = requests.get(url)
if response.status_code != 200:
print ('Failed to get data:', response.status_code)
else:
print ('First 100 characters of the data are:')
print ( response.text[:100])
In [21]:
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/1980/1999/AFG.csv"
response = requests.get(url)
if response.status_code != 200:
print ('Failed to get data:', response.status_code)
else:
print ('First 100 characters of the data are:')
print ( response.text[:100])
In [22]:
# Create a csv file: test01.csv
1901,12.3
1902,45.6
1903,78.9
Out[22]:
In [23]:
with open ('test01.csv', 'r') as reader:
for line in reader:
print (len (line))
In [24]:
with open ('test01.csv', 'r') as reader:
for line in reader:
fields = line.split(',')
print (fields)
In [25]:
# We need to get rid of the hidden newline \n
with open ('test01.csv', 'r') as reader:
for line in reader:
fields = line.strip().split(',')
print (fields)
In [34]:
import csv
In [41]:
with open ('test01.csv', 'r') as rawdata:
csvdata = csv.reader(rawdata)
for record in csvdata:
print (record)
In [38]:
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/pr/year/GBR.csv"
response = requests.get(url)
if response.status_code != 200:
print ('Failed to get data:', response.status_code)
else:
wrapper = csv.reader(response.text.strip().split('\n'))
for record in wrapper:
if record[0] != 'year':
year = int(record [0])
value = float(record [1])
print (year, value)
In [ ]: