import sys, urllib, urllib2
try : 
	import json
except ImportError :
	import simplejson as json

# read in station ids to grab metadata for
sfile = open("long_term_stns.list", "r")
stn = [line.strip() for line in sfile]
sfile.close()

for cstn in stn:
  # call data using web servies
  input_dict = {"sid":cstn,"sdate":"por","edate":"por","meta":"name","elems":[{"name":"maxt","interval":"mly","duration":"mly","reduce":"mean"},{"name":"mint","interval":"mly","duration":"mly","reduce":"mean"},{"name":"pcpn","interval":"mly","duration":"mly","reduce":"sum"},{"name":"snow","interval":"mly","duration":"mly","reduce":"sum"}]}
  params = urllib.urlencode({'params':json.dumps(input_dict)})
  req = urllib2.Request('http://data.rcc-acis.org/StnData', params, {'Accept':'application/json'})
  response = urllib2.urlopen(req)
  a = response.read()
  
  a = a.replace('"','')
  a = a.replace('[','')
  a = a.replace('{','')
  a = a.replace(']','')
  a = a.replace('}','')
  a = a.replace('meta:','')
  a = a.replace('data:','')
  
  # print metadata to output file
  f = open("monthly_histories.txt","a")
  f.write(a)
  f.close()
