{'name':'mint', 'interval':'mly', 'duration':dur, 'reduce':'mean', 'normal':'departure'},
    {'name':'mint', 'interval':'mly', 'duration':dur, 'reduce':{'reduce':'min', 'add':'date'}},
    {'name':'mint', 'interval':'mly', 'duration':dur, 'reduce':'cnt_le_32'},

    {'name':'pcpn', 'interval':'mly', 'duration':dur, 'reduce':{'reduce':'sum', 'add':'mcnt'}, 'maxmissing':5},
    {'name':'pcpn', 'interval':'mly', 'duration':dur, 'reduce':'sum', 'normal':'departure'},
    {'name':'pcpn', 'interval':'mly', 'duration':dur, 'reduce':{'reduce':'max', 'add':'date'}},
    {'name':'pcpn', 'interval':'mly', 'duration':dur, 'reduce':'cnt_gt_0'},

    {'name':'snow', 'interval':'mly', 'duration':dur, 'reduce':{'reduce':'sum', 'add':'mcnt'}, 'maxmissing':5},
    {'name':'snow', 'interval':'mly', 'duration':dur, 'reduce':'sum', 'normal':'departure'},
    {'name':'snow', 'interval':'mly', 'duration':dur, 'reduce':{'reduce':'max', 'add':'date'}},
    {'name':'snow', 'interval':'mly', 'duration':dur, 'reduce':'cnt_gt_0'}
    ]
input_dict = {'state':'NJ', 'date':edate, 'elems':elems, 'meta':['name','sids','state','county']}
acis_data = acis_request('MultiStnData',input_dict)

#COUNTIES
acis_county = acis_request('General/county', {'state':'NJ'})
counties = {}
for c in acis_county['meta']:
    counties[c['id']] = c['name']

final_data = []
for line in acis_data['data']:
    line['meta']['id'] = ""
    for id in line['meta']['sids']:
        id = id.split()
        if (id[1]=="2"):
            line['meta']['id'] = id[0]
    try:
    img_buf = StringIO(data[21:].decode('base64'))
    img_buf.seek(0)
    out_img = cairo.ImageSurface.create_from_png(img_buf)
    ofile = open(ofilename,'w')
    out_img.write_to_png(ofile)
    ofile.close()

try:
    year = sys.argv[1]
    month = sys.argv[2]
except:
    year = "2012"
    month = "07"

input_dict = {"state":"nj","grid":"3","elems":[{"name":"avgt","interval":"mly","duration":"mly","reduce":"mean"}],"image":{"proj":"lcc","overlays":["county:0.25:black","state:2:black"],"interp":"cspline","cmap":"jet","width":200,"levels":range(0,100,2)},"date":year+'-'+month}
acis = acis_request('GridData', input_dict)
createImage(acis['data'], 'monthly_almanac/mly_'+year+'_'+month+'_avgt.png')

input_dict = {"state":"nj","grid":"3","elems":[{"name":"maxt","interval":"mly","duration":"mly","reduce":"mean"}],"image":{"proj":"lcc","overlays":["county:0.25:black","state:2:black"],"interp":"cspline","cmap":"jet","width":200,"levels":range(0,100,2)},"date":year+'-'+month}
acis = acis_request('GridData', input_dict)
createImage(acis['data'], 'monthly_almanac/mly_'+year+'_'+month+'_maxt.png')

input_dict = {"state":"nj","grid":"3","elems":[{"name":"mint","interval":"mly","duration":"mly","reduce":"mean"}],"image":{"proj":"lcc","overlays":["county:0.25:black","state:2:black"],"interp":"cspline","cmap":"jet","width":200,"levels":range(0,100,2)},"date":year+'-'+month}
acis = acis_request('GridData', input_dict)
createImage(acis['data'], 'monthly_almanac/mly_'+year+'_'+month+'_mint.png')

input_dict = {"state":"nj","grid":"3","elems":[{"name":"pcpn","interval":"mly","duration":"mly","reduce":"sum"}],"image":{"proj":"lcc","overlays":["county:0.25:black","state:2:black"],"interp":"cspline","cmap":"jet","width":200,"levels":[0.01,0.10,0.25,0.50,0.75,1.00,1.50,2.00,2.50,3.00,4.00,5.00,6.00,8.00,12.00]},"date":year+'-'+month}
acis = acis_request('GridData', input_dict)
createImage(acis['data'], 'monthly_almanac/mly_'+year+'_'+month+'_pcpn.png')

#input_dict = {"state":"nj","grid":"3","elems":[{"name":"snow","interval":"mly","duration":"mly","reduce":"sum"}],"image":{"proj":"lcc","overlays":["county:0.25:black","state:2:black"],"interp":"cspline","cmap":"jet","width":200,"levels":[0.01,0.10,0.25,0.50,0.75,1.00,1.50,2.00,2.50,3.00,4.00,5.00,6.00,8.00,12.00]},"date":year+'-'+month}