In [1]:
import requests
import json
import pandas as pd
import numpy as np
token = 'RiQdQYKoEqYhQXPXuoNoonkzYPajlwIr'
headers = {'token': token}
url = 'http://www.ncdc.noaa.gov/cdo-web/api/v2/stations?'
url += 'datasetid=NORMAL_DLY'
url += '&extent=' + '35.10,-111.75,35.30,-111.5'
url += '&limit=100'
r = requests.get(url, headers=headers)
data_ = r.json()
stations = pd.io.json.json_normalize(data_['results'])
stations_list = list(stations['id'])
In [463]:
stations_list
Out[463]:
In [499]:
stationID = ''
for st in stations_list:
stationID = stationID + '&stationid=' + st
startDate = '2010-01-01'
endDate = '2010-12-31'
url = 'http://www.ncdc.noaa.gov/cdo-web/api/v2/data?'
url += 'datasetid=NORMAL_DLY'
url += '&datatypeid=DLY-TAVG-NORMAL'
url += stationID
url += '&startdate=' + startDate
url += '&enddate=' + endDate
url += '&limit=1000&offset=1001'
r = requests.get(url, headers=headers)
data_ = r.json()
data = pd.io.json.json_normalize(data_['results'])
data['datetime']=pd.to_datetime(data['date'])
In [500]:
data
Out[500]:
In [501]:
%matplotlib inline
data.plot('datetime', 'value', style='ko-')
Out[501]:
In [454]:
stations_list
Out[454]:
In [469]:
temp_data = np.zeros((4,365))
In [470]:
temp_data
Out[470]:
In [514]:
t4 = data[data['station'] == stations_list[3]]
In [515]:
temp_data[3,250:] = np.array(t4['value'])
In [516]:
temp_data
Out[516]:
In [524]:
np.savetxt('data/temperature.csv',temp_data,fmt='%.2f',delimiter=',')
In [ ]: