import requests
import json

county =  '22033'
# call data using web servies
input_dict = {
  'county' : county,
  'date' : '2014-6',
  'meta' : 'name, sids',
  'elems' : [{
    'name' : 'pcpn',
    'interval' : [0,1],
    'duration' : 1,
    'reduce' : {'reduce':'sum'},
    'maxmissing' : 3
  },{
    'name' : 'pcpn',
    'interval' : [0,1],
    'duration' : 1,
    'reduce' : {'reduce':'sum'},
    'maxmissing' : 3,
    'normal' : 'departure'
  }]
}
params = {'params': json.dumps(input_dict)}
headers = {'Accept': 'application/json'}
req = requests.post('http://data.rcc-acis.org/MultiStnData', data=params, headers=headers)
response = req.json()
acis_data = response['data']

# get county name
general_dict = {
  'id' : county, 
  'meta' : 'name'
}
params = {'params': json.dumps(general_dict)}
meta = requests.post('https://data.rcc-acis.org/General/county', data=params, headers=headers)
county_meta = meta.json()
county_name = county_meta["meta"][0]["name"]

# print out the data in a formatted text table
print ('Monthly Precipitation Totals (inches) in {} for June, 2014'.format(county_name))
print ('\n%-25s %8s %12s'% ("Station name", "Total", "DFN"))
for v in range (0, len(acis_data)):
  if acis_data[v]['data'][0] != 'M':
    stnname = acis_data[v]['meta']['name']    #station name
    mpcpn = acis_data[v]['data'][0]           #monthly precipitation total
    dpt = acis_data[v]['data'][1]             #monthly departure from normal
    depart = dpt if dpt != 'M' else '-'    # check to see if there is a normals value, if not substitute a dash
    print ('%-25s %8s %12s'% (stnname, mpcpn, depart))
print ('\n* DFN - Departure from the 1991-2020 Climate Normals')