Ejemplo n.º 1
0
def env_demand():

    home = '/home/nealbob'
    folder = '/Dropbox/Model/results/chapter7/chapter7/'
    out = '/Dropbox/Thesis/IMG/chapter7/'
    img_ext = '.pdf'
    table_out = '/Dropbox/Thesis/STATS/chapter7/'
    
    rows = ['CS', 'CS-HL', 'SWA', 'OA']
    
    data = {}

    for row in rows:
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            data[row] = pickle.load(f)[2]['F3'][:, 1]

    duration_curve(data, OUTFILE=home + out + 'down_win_dec' + img_ext)
    
    for row in rows:
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            data[row] = pickle.load(f)[2]['F3'][:, 0]
            f.close()

    duration_curve(data, OUTFILE=home + out + 'down_sum_dec' + img_ext)
    
    from econlearn import TilecodeRegressor as TR
    
    tr = TR(1, [11], 20)
    fig = pylab.figure()

    for row in rows:
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            Q = np.sum(pickle.load(f)[2]['Q_env'], axis = 1) /1000
            f.close()
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            S = np.sum(pickle.load(f)[2]['S'], axis = 1) / 2000
            f.close()
        #with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
        #    I = np.sum(pickle.load(f)[2]['I'], axis = 1)
        #    f.close()

        tr.fit(S, Q)
        tr.tile.plot(['x'], showdata=False, label=row)
    
    pylab.xlabel('Storage volume, $S_t$ (GL)')
    pylab.ylabel('Mean environmental flow, $q_{0t}$ (GL)')
    setFigLinesBW_list(fig)
    pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4, mode="expand", borderaxespad=0.) 
    pylab.savefig(home + out + 'env_demand_S.pdf', bbox_inches='tight')
    pylab.show()
    
    
    tr = TR(1, [11], 20)
    fig = pylab.figure()

    for row in rows:
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            Q = np.sum(pickle.load(f)[2]['Q_env'], axis = 1) /1000
            f.close()
        #with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
        #    S = np.sum(pickle.load(f)[2]['S'], axis = 1) / 2.0
        #    f.close()
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            I = np.sum(pickle.load(f)[2]['I'], axis = 1) / 1000
            f.close()

        tr.fit(I, Q)
        tr.tile.plot(['x'], showdata=False, label=row)
    
    pylab.xlabel('Inflow, $I_t$ (GL)')
    pylab.ylabel('Mean environmental flow, $q_{0t}$ (GL)')
    pylab.xlim(0, 2000)
    setFigLinesBW_list(fig)
    pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4, mode="expand", borderaxespad=0.) 
    pylab.savefig(home + out + 'env_demand_I.pdf', bbox_inches='tight')
    pylab.show()

    tr = TR(1, [11], 20)
    fig = pylab.figure()

    for row in rows:
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            B = np.sum(pickle.load(f)[2]['Budget'], axis = 1) / 1000000
            f.close()
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            S = np.sum(pickle.load(f)[2]['S'], axis = 1) / 2000
            f.close()
        #with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
        #    I = np.sum(pickle.load(f)[2]['I'], axis = 1)
        #    f.close()

        tr.fit(S, B)
        tr.tile.plot(['x'], showdata=False, label=row)
    
    pylab.xlabel('Storage volume, $S_t$ (GL)')
    pylab.ylabel('Mean environmental trade, $P_t(a_{0t} - q_{0t})$ (\$m)')
    setFigLinesBW_list(fig)
    pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4, mode="expand", borderaxespad=0.) 
    pylab.savefig(home + out + 'env_trade_S.pdf', bbox_inches='tight')
    pylab.show()
    
    tr = TR(1, [11], 20)
    fig = pylab.figure()

    for row in rows:
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            B = np.sum(pickle.load(f)[2]['Budget'], axis = 1) / 1000000
            f.close()
        with open(home + folder + '0' + row + '_' + '0' + '_result.pkl', 'rb') as f:
            I = np.sum(pickle.load(f)[2]['I'], axis = 1) /1000
            f.close()

        tr.fit(I, B)
        tr.tile.plot(['x'], showdata=False, label=row)
    
    pylab.xlabel('Inflow, $I_t$ (GL)')
    pylab.ylabel('Mean environmental trade, $P_t(a_{0t} - q_{0t})$ (\$m)')
    pylab.xlim(0, 2000)
    setFigLinesBW_list(fig)
    pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4, mode="expand", borderaxespad=0.) 
    pylab.savefig(home + out + 'env_trade_I.pdf', bbox_inches='tight')
    pylab.show()
Ejemplo n.º 2
0
def planner(results):

    """
        Generate charts and tables for central case scenarios
    """
    home = '/home/nealbob'
    folder = '/Dropbox/Model/results/chapter7/'
    out = '/Dropbox/Thesis/IMG/chapter7/'
    img_ext = '.pdf'
    table_out = '/Dropbox/Thesis/STATS/chapter7/'
     
    #with open(home + folder + 'central_result.pkl', 'rb') as f:
    #    results = pickle.load(f)
    #    f.close()

    stats_envoff, timeseries_envoff, stats, timeseries = results


    ###### Summary results #####
    
    cols = ['Mean', 'SD', '2.5th', '25th', '75th', '97.5th']
    rows = ['Consumptive', 'Optimal']
    series = ['SW', 'Profit', 'B', 'S', 'W', 'E', 'P']
    scale = {'SW' : 1000000, 'Profit' : 1000000, 'S' : 1000, 'W' : 1000, 'E' : 1000, 'B' : 1000000, 'P' : 1}

    m = 2

    for x in series:
        data0 = []

        record = {}
        for col in cols:
            record[col] = stats_envoff[x]['Annual'][col][m] / scale[x]
        data0.append(record)

        record = {}
        for col in cols:
            record[col] = stats[x]['Annual'][col][m] / scale[x]
        data0.append(record)
        
        data = pandas.DataFrame(data0)
        data.index = rows

        with open(home + table_out + ' ' + x + '.txt', 'w') as f:
            f.write(data.to_latex(float_format='{:,.2f}'.format, columns=cols))
            f.close()
    
    ###### Environmental flows #####
    
    cols = ['Mean', 'SD', '2.5th', '25th', '75th', '97.5th']
    rows = ['Summer', 'Winter', 'Annual']
    series = ['Q_env', 'Q']
    scale = {'Q_env' : 1000, 'Q' : 1000}

    m = 2

    for x in series:
        data0 = []
        for row in rows:
            record = {}
            for col in cols:
                record[col] = stats[x][row][col][m] / scale[x]
            data0.append(record)
        
        data = pandas.DataFrame(data0)
        data.index = rows

        with open(home + table_out + ' ' + x + '.txt', 'w') as f:
            f.write(data.to_latex(float_format='{:,.2f}'.format, columns=cols))
            f.close()


    ###### River flows #########

    cols = ['Mean', 'SD', '2.5th', '25th', '75th', '97.5th']
    rows = ['Summer', 'Winter', 'Annual']
    series = ['F1', 'F1_tilde', 'F3', 'F3_tilde']
    scale = 1000

    m = 2

    for x in series:
        data0 = []

        for row in rows:
            record = {}
            for col in cols:
                record[col] = stats[x][row][col][m] / scale
            data0.append(record)

        data = pandas.DataFrame(data0)
        data.index = rows

        with open(home + table_out + ' ' + x + '.txt', 'w') as f:
            f.write(data.to_latex(float_format='{:,.2f}'.format, columns=cols))
            f.close()
    
    for x in series:
        data0 = []

        for row in rows:
            record = {}
            for col in cols:
                record[col] = stats_envoff[x][row][col][m] / scale
            data0.append(record)

        data = pandas.DataFrame(data0)
        data.index = rows

        with open(home + table_out + ' ' + x + '_envoff.txt', 'w') as f:
            f.write(data.to_latex(float_format='{:,.2f}'.format, columns=cols))
            f.close()

    # Flow duration curves
    data = {'Natural' : timeseries['F1_tilde'][:, 0]/1000,
            'Consumptive' : timeseries_envoff['F1'][:, 0]/1000,
            'Optimal' :  timeseries['F1'][:, 0]/1000 } 
    duration_curve(data, OUTFILE=home + out + 'up_sum' + img_ext)

    data = {'Natural' : timeseries['F1_tilde'][:, 1]/1000,
            'Consumptive' : timeseries_envoff['F1'][:, 1]/1000,
            'Optimal' :  timeseries['F1'][:, 1]/1000 } 
    duration_curve(data, OUTFILE=home + out + 'up_win' + img_ext)
    
    data = {'Natural' : timeseries['F3_tilde'][:, 0]/1000,
            'Consumptive' : timeseries_envoff['F3'][:, 0]/1000,
            'Optimal' :  timeseries['F3'][:, 0]/1000 } 
    duration_curve(data, OUTFILE=home + out + 'down_sum' + img_ext)
    
    data = {'Natural' : timeseries['F3_tilde'][:, 1] /1000,
            'Consumptive' : timeseries_envoff['F3'][:, 1] /1000,
            'Optimal' :  timeseries['F3'][:, 1]/1000 } 
    duration_curve(data, OUTFILE=home + out + 'down_win' + img_ext)

    from econlearn import TilecodeRegressor as TR
    
    tr = TR(1, [11], 20)
    
    I = timeseries['I'][1:100000,1] / 1000
    idx = I < 1400
    I = I[idx]
    Q = timeseries['W'][1:100000,1] / 1000
    Q = Q[idx]
    S = timeseries['S'][1:100000,1] / 1000
    S = S[idx]
    S2 = timeseries['S'][1:100000,0] / 1000
    S2 = S2[idx]
    
    Popt = timeseries['P'][:,0]
    Pcons = timeseries_envoff['P'][:,0]

    tr.fit(I[1:30000], Q[1:30000])
    tr.tile.plot(['x'], showdata=True)
    pylab.xlabel('Inflow, $I_t$ (GL)')
    pylab.ylabel('Release, $W_t$ (GL)')
    pylab.xlim(0, 1400)
    #setFigLinesBW_list(fig)
    #pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4, mode="expand", borderaxespad=0.) 
    pylab.savefig(home + out + 'env_dem_planner.pdf', bbox_inches='tight')
    pylab.show()
    
    tr.fit(S[1:30000], Q[1:30000])
    tr.tile.plot(['x'], showdata=True)
    pylab.xlabel('Storage, $S_t$ (GL)')
    pylab.ylabel('Release, $W_t$ (GL)')
    #setFigLinesBW_list(fig)
    #pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=4, mode="expand", borderaxespad=0.) 
    pylab.savefig(home + out + 'env_dem_plannerS.pdf', bbox_inches='tight')
    pylab.show()

    pylab.hexbin(S2, I, C=Q)
    pylab.xlabel('Summer storage, $S_t$ (GL)')
    pylab.ylabel('Winter inflow, $I_t$ (GL)')
    cb = pylab.colorbar()
    cb.set_label('Winter release, $W_t$ (GL)') 
    #pylab.ylim(0, 1000)
    pylab.savefig(home + out + 'env_dem_plannerZ.pdf', bbox_inches='tight')
    pylab.show()

    xopt = pylab.hist(Popt, bins=120)
    xcons = pylab.hist(Pcons, bins=120)
    pylab.show()
    
    fig = pylab.figure() 
    pylab.plot(xopt[1][1::], xopt[0]/500000, label = 'Optimal')
    pylab.plot(xcons[1][1::], xcons[0]/500000, label = 'Consumptive')
    setFigLinesBW_list(fig)
    pylab.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=2, mode="expand", borderaxespad=0.) 
    pylab.xlabel('Summer shadow price, $ per ML')
    pylab.ylabel('Frequency')
    pylab.xlim(0, 500)
    pylab.savefig(home + out + 'plan_price.pdf', bbox_inches='tight')
    pylab.show()