date_start = dt.datetime(2011, 9, 1)
date_end = dt.datetime(2012, 2, 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 = 'pcde-' + c['meter_name'] + '-' + c['ip_address'][-3:] + '.pdf'
        print 'querying for ' + filename

        daily_energy = og.get_daily_energy_for_circuit_id(c['circuit_id'], date_start, date_end)

        if daily_energy == None:
            continue

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

        # fit linear slope to watthour data
        import numpy as np
        dates = daily_energy.index
        watthours = daily_energy.values

        date_max = max(dates)
        date_min = min(dates)
    #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,
                                              date_end)

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

        # plot energy on axis 0
    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))

        # get hourly credit
        credit = og.get_credit_for_circuit_id(c["circuit_id"], date_start, date_end)

        # plot each circuit daily energy values for all time
        f, ax = plt.subplots(4, 1, sharex=True, figsize=(8, 12))
# 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:

        # generate appropriate dictionary key
        if method == 'meter':
            label = c['meter_name'] + '-' + c['ip_address'][-2:]
        if method == 'pin':
            label = c['pin']

        # query database and append to dictionary
        print 'querying for', i, 'th circuit =', label
        if non_resetting_data:
            d[label], error = og.get_daily_energy_for_circuit_id_nr(c['circuit_id'], date_start, date_end)
        else:
            d[label], error = og.get_daily_energy_for_circuit_id(c['circuit_id'], date_start, date_end)

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

# transpose dataframe and output to CSV
filename = 'energy_daily_' + str(date_start.year) + '-' + str(date_start.month)
filename += '_' + method + '.csv'

if columns == 'dates':
    df.T.to_csv(filename)
if columns == 'circuits':
    df.to_csv(filename)
    if not filter_by_meter_list or c['meter_name'] in meter_list:

        # generate appropriate dictionary key
        if method == 'meter':
            label = c['meter_name'] + '-' + c['ip_address'][-2:]
        if method == 'pin':
            label = c['pin']

        # query database and append to dictionary
        print 'querying for', i, 'th circuit =', label
        if non_resetting_data:
            d[label], error = og.get_daily_energy_for_circuit_id_nr(
                c['circuit_id'], date_start, date_end)
        else:
            d[label], error = og.get_daily_energy_for_circuit_id(
                c['circuit_id'], date_start, date_end)

# assemble dictionary into dataframe
import pandas as p

df = p.DataFrame(d)

# transpose dataframe and output to CSV
filename = 'energy_daily_' + str(date_start.year) + '-' + str(date_start.month)
filename += '_' + method + '.csv'

if columns == 'dates':
    df.T.to_csv(filename)
if columns == 'circuits':
    df.to_csv(filename)