# use subsample while debugging
#circuit_dict_list = circuit_dict_list[:20]

import datetime as dt
date_start = dt.datetime(2012, 1, 1)
date_end   = dt.datetime(2012, 2, 1)

# place time series for credit of each pin in a dictionary
d = {}
for i, c in enumerate(circuit_dict_list):

    if not filter_by_meter_list or c['meter_name'] in meter_list:

        #choose method of labeling data
        #label = c['meter_name'] + '-' + c['ip_address'][-2:]
        label = c['pin']

        # query database
        print 'querying for', i, 'th circuit =', label
        watthours = og.get_watthours_for_circuit_id(c['circuit_id'], date_start, date_end)

        # append to dictionary
        d[label] = watthours

# assemble dictionary into dataframe
import pandas as p
df = p.DataFrame(d)

# transpose dataframe and output to CSV
df.T.to_csv('energy_hourly.csv')
date_end = dt.datetime(2012, 3, 1)

if __name__ == '__main__':

    import offline_gateway as og
    cdl = og.get_circuit_dict_list(mains=True)
    #cdl = cdl[:1]

    for i, c in enumerate(cdl):

        filename = 'pche-' + c['meter_name'] + '-' + c['ip_address'][
            -3:] + '.pdf'
        print 'querying for ' + filename

        # grab hourly energy, if empty drop through loop
        hourly_energy = og.get_watthours_for_circuit_id(
            c['circuit_id'], date_start, date_end)
        if hourly_energy == None:
            continue

        # grab daily energy, if empty drop through loop
        daily_energy = og.get_daily_energy_for_circuit_id(
            c['circuit_id'], date_start, date_end)
        if daily_energy == None:
            continue

        # shift daily_energy index by one to line up better
        import pandas as p
        daily_energy = daily_energy.shift(1, offset=p.DateOffset(days=1))

        # get hourly credit
        credit = og.get_credit_for_circuit_id(c['circuit_id'], date_start,

if __name__ == "__main__":

    import offline_gateway as og

    cdl = og.get_circuit_dict_list(mains=True)
    # cdl = cdl[:10]

    for i, c in enumerate(cdl):

        filename = "pcha-" + c["meter_name"] + "-" + c["ip_address"][-3:] + ".pdf"
        print "querying for " + filename

        # grab hourly energy, if empty drop through loop
        hourly_energy, error = og.get_watthours_for_circuit_id(c["circuit_id"], date_start, date_end)
        # if hourly_energy == -1:
        if error != 0:
            continue

        # grab daily energy, if empty drop through loop
        daily_energy, error = og.get_daily_energy_for_circuit_id(c["circuit_id"], date_start, date_end)
        # if daily_energy == None:
        if error != 0:
            continue

        # shift daily_energy index by one to line up better
        import pandas as p

        daily_energy = daily_energy.shift(1, offset=p.DateOffset(days=1))
cl = gw.get_circuit_list()
circ_list = []#list of circuit names as strings
circ_dict = {} #dictionary circuit name string as key, with number equavilent 
wh_DF = pd.DataFrame(index = datelist)
cred_DF = pd.DataFrame(index = datelist)

# Populate the list of all circuits and mains
# AND populate dictionary to translate string name to number name
for ix, row in enumerate(cl):
	circ_list.append(row[1].encode('ascii')+'_'+str(int(row[2][-2:])))
	circ_dict[circ_list[ix]] = row[0]

# Create a DataFrame of Wh for each main or site
for ix, row in enumerate(circ_list):
	wh_col = gw.get_watthours_for_circuit_id(circ_dict[row], date_start, date_end)[0].apply(np.float32)
	wh_col.resample('H', how = np.max) # resample options sum, mean, std, max, min, median, first, last, ohlc.
	wh_col = pd.Series(wh_col, name = row)
	wh_DF = wh_DF.join(wh_col)

	cred_col = gw.get_credit_for_circuit_id(circ_dict[row], date_start, date_end).apply(np.float32)
	cred_col.resample('H', how = np.min)
	cred_col = pd.Series(cred_col, name = row)
	cred_DF = cred_DF.join(cred_col)	
	print row

wh_DF.to_csv('gw_wh.csv', index_label = 'dates' )
cred_DF.to_csv('gw_cred.csv',index_label = 'dates' )


if __name__ == '__main__':

    import offline_gateway as og
    circuit_list = og.get_circuit_list()

    debug = False
    if debug:
        circuit_list = circuit_list[:2]

    # iterate over list of circuits
    for c in circuit_list:
        filename = 'pchp-' + c[1] + '-' + c[2][-3:] + '.pdf'
        print 'querying for', filename

        import offline_gateway as og
        df = og.get_watthours_for_circuit_id(c[0], date_start, date_end)

        if df == None:
            continue

        # offset by 1 hour
        import pandas as p
        offset = df - df.shift(1, offset=p.DateOffset(hours=1))

        positive_only = True
        #positive_only = False
        if positive_only:
            offset = offset[offset.values >= 0]

        # plot each circuit daily energy values for all time
        f, ax = plt.subplots(1, 1, sharex=True)
    import offline_gateway as og

    circuit_list = og.get_circuit_list()

    debug = False
    if debug:
        circuit_list = circuit_list[:2]

    # iterate over list of circuits
    for c in circuit_list:
        filename = "pchp-" + c[1] + "-" + c[2][-3:] + ".pdf"
        print "querying for", filename

        import offline_gateway as og

        df = og.get_watthours_for_circuit_id(c[0], date_start, date_end)

        if df == None:
            continue

        # offset by 1 hour
        import pandas as p

        offset = df - df.shift(1, offset=p.DateOffset(hours=1))

        positive_only = True
        # positive_only = False
        if positive_only:
            offset = offset[offset.values >= 0]

        # plot each circuit daily energy values for all time