# Python request library lets us get data straight from a URL 
import requests 

url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GBR.csv"
response = requests.get(url ) # gets the get function from the request library to find the url and put it in a loop etc 

if response.status_code != 200:
    print ('Failed to get data: ', response.status_code)
else: 
    print ('First 100 charecters of data are: ')
    print (response.text[:100])

"""
Explanation: Getting Data from the 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 interest (capitals) 
look up country codes online 
e.g. http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GBR.csv
Use (https://datahelpdesk.worldbank.org/knowledgebase/articles/902061-climate-data-api and http://www.nationsonline.org/oneworld/country_code_list.htm)
End of explanation
"""


url = 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/GTM.csv'
response = requests.get(url ) # gets the get function from the request library to find the url and put it in a loop etc 

if response.status_code != 200:
    print ('Failed to get data: ', response.status_code)
else: 
    print ('First 100 charecters of data are: ')
    print (response.text[:100])

url = 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/1980/1999/AFG.csv'
response = requests.get(url ) # gets the get function from the request library to find the url and put it in a loop etc 

if response.status_code != 200:
    print ('Failed to get data: ', response.status_code)
else: 
    print ('First 100 charecters of data are: ')
    print (response.text[:100])

# Create a csv file: test01.csv

1901, 12.3
1902, 45.6
1903, 78.9

with open('test01.csv', 'r') as reader: 
    for line in reader: 
        print (len(line))

with open ('test01.csv', 'r') as reader: 
    for line in reader: 
        fields = line.split (',')
        print (fields)

# 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)

"""
Explanation: To do:
Get temperature for Guatemala
Fetch rainfall for Afghanistan between 1980 and 1999
End of explanation
"""


import csv

with open ('test01.csv', 'r') as rawdata: 
    csvdata = csv.reader(rawdata)
    for record in csvdata: 
            print (record)

url = 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GTM.csv'
response = requests.get(url ) # gets the get function from the request library to find the url and put it in a loop etc 

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)

"""
Explanation: Using the csv library instead
End of explanation
"""


