Пример #1
0
def data_electrical(request):
    supplyPoint = request.GET['supplyPoint']
    m = int(request.GET['month'])
    d = request.GET['days']
    gspName = GSP.objects.get(idx=supplyPoint)

    if d == '':
        d = int(0)
    else:
        d = int(d)

    if m == 13:
        d = 366

    electricalData = electricalGSP.objects.filter(GSP=supplyPoint)
    vlqs = electricalData.values_list()
    r = np.core.records.fromrecords(
        vlqs, names=[f.name for f in electricalGSP._meta.fields])
    l = np.array(r)
    process = np.zeros([len(l), 3])

    for i in range(
            0, len(process)):  # extract the information we need for inputs.
        change = l[i]
        for j in range(1, 4):
            process[i, (j - 1)] = change[j]

    g = np.array(process) / 2

    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': g[0:, 2]})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)

    S = S.reset_index()
    columnsTitles = ["Time", "Data"]
    proData = S.reindex(columns=columnsTitles)
    finalData = proData.iloc[0:(24 * d)]

    response = HttpResponse(content_type='text/csv')
    if m == 13:
        file = gspName.name + 'GSP_0104_3103.csv'
    elif m < 10:
        file = gspName.name + 'GSP_0' + str(int(m))
    else:
        file = gspName.name + 'GSP_' + str(int(m))

    response['Content-Disposition'] = 'attachment; filename="%s"' % file

    writer = csv.writer(response)

    with open('heat_demand.csv', 'w') as csvfile:
        writer.writerow(['', 'Electrical Demand (MWh)'])
        for i in range(0, len(finalData)):
            writer.writerow(finalData.iloc[i, 0:])

    return response
Пример #2
0
def gspDemand(supplyPoint, m, d):
    electricalData = process_data_normal(
        (electricalGSP.objects.filter(GSP=supplyPoint)), electricalGSP, 3)
    electricalData = electricalData[0:, 2] / 2
    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': electricalData})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)
    elec = S['Data']
    elec = elec.iloc[0:(24 * d)]

    return elec
Пример #3
0
def sizing_storage(request):
    supplyPoint=request.GET['supplyPoint']
    m=int(request.GET['month'])
    d=request.GET['days']
    gspName=GSP.objects.get(idx=supplyPoint)
    ap=1 -(int(request.GET['demandReduction'])/100)

    if d =='':
        d=int(0)
    else:
        d = int(d)

    if m == 13:
        d=366
    
    electricalData = electricalGSP.objects.filter(GSP = supplyPoint)
    vlqs = electricalData.values_list()
    r = np.core.records.fromrecords(vlqs, names=[f.name for f in electricalGSP._meta.fields])
    l=np.array(r)
    process = np.zeros([len(l),3])

    for i in range(0,len(process)): # extract the information we need for inputs.
        change = l[i]
        for j in range(1,4):
            process[i,(j-1)] = change[j]

    g=np.array(process)/2

    dS=pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData=pd.DataFrame({'Time':dS[0:8784], 'Data':g[0:,2]})
    data=proData.set_index('Time')
    d1, d2, S = month(m, data)

    elec=S['Data']

    capacityProxy=np.zeros([24, d])
    apProxy=np.zeros([24, d])

    for i in range(1,(d+1)):
        model, sol, capacity, ap = runSolutionCapacity((elec.iloc[(24*i)-24:(24*i)]),ap)
        capacityProxy[0:,(i-1)]=capacity
        apProxy[0:,(i-1)]=ap


    capacityFinal=capacityProxy.reshape((d*24), order='F')
    apFinal=apProxy.reshape((d*24),order='F')

    capacityMax = np.amax(capacityFinal)
    reductionMax = np.amax(apFinal)*100

    return render(request,'energyStorage/sizing_details.html',{'capacityMax':capacityMax, 'reductionMax': reductionMax})
Пример #4
0
def data_charge_demand(request):
    supplyPoint = request.GET['supplyPoint']
    m = int(request.GET['month'])
    d = request.GET['days']

    gspNameProxy = GSP.objects.get(idx=supplyPoint)
    gspName = GSP.objects.filter(idx=supplyPoint)
    na0 = ([p.name for p in gspName])
    gspAuthority = gspLocalAuthority.objects.filter(gsp=na0[0])
    na1 = ([p.localAuthority for p in gspAuthority])

    mediumEV = request.GET['mediumEV']
    smallEV = request.GET['smallEV']
    geographicArea = int(request.GET['geographicArea'])

    if geographicArea == 1:
        gA = 'Urban'
    else:
        gA = 'Rural'

    if d == '':
        d = int(0)
    else:
        d = int(d)

    if m == 13:
        d = 366

    if mediumEV == '':
        mediumEV = int(0)
    else:
        mediumEV = int(mediumEV)

    if smallEV == '':
        smallEV = int(0)
    else:
        smallEV = int(smallEV)

    #electricalData = process_data((electricalGSP.objects.filter(GSP = supplyPoint)), electricalGSP, 3)
    jobElectricalData = process_data.delay(
        (electricalGSP.objects.filter(GSP=supplyPoint)), electricalGSP, 3)
    electricalData = jobElectricalData.perform()

    #chargeDataSmall = process_data((Journey.objects.filter(localAuthority=na1[0], Area=gA, typeEV='Economy')), Journey,7)
    jobChargeDataSmall = process_data.delay((Journey.objects.filter(
        localAuthority=na1[0], Area=gA, typeEV='Economy')), Journey, 7)
    chargeDataSmall = jobChargeDataSmall.perform()

    #chargeDataMedium = process_data((Journey.objects.filter(localAuthority=na1[0], Area=gA, typeEV='Midsize')), Journey, 7)
    jobChargeDataMedium = process_data.delay((Journey.objects.filter(
        localAuthority=na1[0], Area=gA, typeEV='Midsize')), Journey, 7)
    chargeDataMedium = jobChargeDataMedium.perform()

    # good to here
    electricalData = electricalData[0:, 2] / 2
    chargeDataSmall = chargeDataSmall[0:, 0:4]
    chargeDataMedium = chargeDataMedium[0:, 0:4]

    #profileSmall=formatChargeDemand(smallEV, chargeDataSmall, 'Economy',d)
    jobProfileSmall = formatChargeDemand.delay(smallEV, chargeDataSmall,
                                               'Economy', d)
    profileSmall = jobProfileSmall.perform()

    #profileMedium=formatChargeDemand(mediumEV, chargeDataMedium, 'Midsize',d)
    jobProfileMedium = formatChargeDemand.delay(mediumEV, chargeDataMedium,
                                                'Midsize', d)
    profileMedium = jobProfileMedium.perform()

    profileTotal = np.zeros([len(profileSmall), 1])
    profileTotal[0:, 0] = (profileSmall['Charge'].as_matrix() +
                           profileMedium['Charge'].as_matrix()) / 1000

    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': electricalData})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)

    S = S.reset_index()
    columnsTitles = ["Time", "Data"]
    proData = S.reindex(columns=columnsTitles)
    base = np.zeros([(24 * d), 1])
    base[0:, 0] = proData.iloc[0:(24 * d), 1]
    newData = base + profileTotal
    inter = np.append(profileTotal, newData, 1)
    finalData = np.append(proData.iloc[0:(24 * d)], inter, 1)

    ####
    if geographicArea == 1:
        file = gspNameProxy.name + '_' + str(
            mediumEV) + "_MidsizeEV" + '_' + str(
                smallEV) + "_SmallEV_Urban.csv"
    else:
        file = gspNameProxy.name + '_' + str(
            mediumEV) + "_MidsizeEV" + '_' + str(
                smallEV) + "_SmallEV_Rural.csv"

    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="%s"' % file

    writer = csv.writer(response)

    with open('charge_demand.csv', 'w') as csvfile:
        writer.writerow([
            '', 'Base GSP Demand (MWh)', 'EV Charging Demand (MWh)',
            'Future GSP Demand (MWh)'
        ])
        #writer.writerow([''])
        for i in range(0, len(finalData)):
            writer.writerow(finalData[i, 0:])

    return response
Пример #5
0
def plot_ev_demand_compare(request):

    supplyPoint = request.GET['supplyPoint']
    supplyPoint2 = request.GET['supplyPoint2']
    m = int(request.GET['month'])
    d = request.GET['days']

    gspName = GSP.objects.filter(idx=supplyPoint)
    na0 = ([p.name for p in gspName])
    gspAuthority = gspLocalAuthority.objects.filter(gsp=na0[0])
    na1 = ([p.localAuthority for p in gspAuthority])

    gspName2 = GSP.objects.filter(idx=supplyPoint2)
    na0_2 = ([p.name for p in gspName2])
    gspAuthority2 = gspLocalAuthority.objects.filter(gsp=na0_2[0])
    na1_2 = ([p.localAuthority for p in gspAuthority2])

    mediumEV = request.GET['mediumEV']
    smallEV = request.GET['smallEV']
    geographicArea = int(request.GET['geographicArea'])

    if geographicArea == 1:
        gA = 'Urban'
    else:
        gA = 'Rural'

    if d == '':
        d = int(0)
    else:
        d = int(d)

    if m == 13:
        d = 366

    if mediumEV == '':
        mediumEV = int(0)
    else:
        mediumEV = int(mediumEV)

    if smallEV == '':
        smallEV = int(0)
    else:
        smallEV = int(smallEV)

    electricalData = process_data(
        (electricalGSP.objects.filter(GSP=supplyPoint)), electricalGSP, 3)
    chargeDataSmall = process_data((Journey.objects.filter(
        localAuthority=na1[0], Area=gA, typeEV='Economy')), Journey, 7)
    chargeDataMedium = process_data((Journey.objects.filter(
        localAuthority=na1[0], Area=gA, typeEV='Midsize')), Journey, 7)

    electricalData2 = process_data(
        (electricalGSP.objects.filter(GSP=supplyPoint2)), electricalGSP, 3)
    chargeDataSmall2 = process_data((Journey.objects.filter(
        localAuthority=na1_2[0], Area=gA, typeEV='Economy')), Journey, 7)
    chargeDataMedium2 = process_data((Journey.objects.filter(
        localAuthority=na1_2[0], Area=gA, typeEV='Midsize')), Journey, 7)

    ###
    electricalData = electricalData[0:, 2] / 2
    chargeDataSmall = chargeDataSmall[0:, 0:4]
    chargeDataMedium = chargeDataMedium[0:, 0:4]

    profileSmall = formatChargeDemand(smallEV, chargeDataSmall, 'Economy', d)
    profileMedium = formatChargeDemand(mediumEV, chargeDataMedium, 'Midsize',
                                       d)

    profileTotal = np.zeros([len(profileSmall), 1])
    profileTotal[0:, 0] = (profileSmall['Charge'].as_matrix() +
                           profileMedium['Charge'].as_matrix())

    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': electricalData})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)

    elec = S['Data']

    pC = max(profileTotal)[0]
    peakCharge = round(pC, 2)

    ###
    electricalData2 = electricalData2[0:, 2] / 2
    chargeDataSmall2 = chargeDataSmall2[0:, 0:4]
    chargeDataMedium2 = chargeDataMedium2[0:, 0:4]

    profileSmall2 = formatChargeDemand(smallEV, chargeDataSmall2, 'Economy', d)
    profileMedium2 = formatChargeDemand(mediumEV, chargeDataMedium2, 'Midsize',
                                        d)

    profileTotal2 = np.zeros([len(profileSmall), 1])
    profileTotal2[0:, 0] = (profileSmall2['Charge'].as_matrix() +
                            profileMedium2['Charge'].as_matrix())

    pC2 = max(profileTotal2)[0]
    peakCharge2 = round(pC2, 2)
    ###

    if d == 1:
        formatter = DateFormatter('%H-%M')
        rule = rrulewrapper(HOURLY, interval=6)
    elif d < 4:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=1)
    elif d < 10:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=2)
    elif d > 9 and d < 16:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=3)
    elif d > 15 and d < 32:
        formatter = DateFormatter('%d-%m')
        rule = rrulewrapper(DAILY, interval=5)

    if m == 13:
        formatter = DateFormatter('%b-%Y')
        rule = rrulewrapper(MONTHLY, interval=3)

    loc = RRuleLocator(rule)
    delta = datetime.timedelta(hours=1)
    dates = drange(d1, d2, delta)
    finalDates = dates[0:(24 * d)]
    fig, ax = plt.subplots()

    ax.plot_date(finalDates,
                 profileTotal,
                 linestyle='-',
                 marker='None',
                 label=na0[0])
    ax.plot_date(finalDates,
                 profileTotal2,
                 linestyle='-',
                 marker='None',
                 label=na0_2[0])
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(loc)
    plt.ylabel('Charging Demand (kWh)')
    plt.grid(True)
    plt.legend()

    buffer = BytesIO()
    canvas = pylab.get_current_fig_manager().canvas
    canvas.draw()
    pilImage = PIL.Image.frombytes("RGB", canvas.get_width_height(),
                                   canvas.tostring_rgb())
    pilImage.save(buffer, "PNG")
    pylab.close()

    image_png = buffer.getvalue()
    graphic = base64.b64encode(image_png)
    graphic = graphic.decode('utf-8')
    ##
    return render(
        request, 'transport/show_plot_ev_compare.html', {
            'graphic': graphic,
            'name': na0[0],
            'name2': na0_2[0],
            'peakCharge': peakCharge,
            'peakCharge2': peakCharge2
        })
Пример #6
0
def plot_electrical(request):
    supplyPoint = request.GET['supplyPoint']
    m = int(request.GET['month'])
    d = request.GET['days']
    gspName = GSP.objects.get(idx=supplyPoint)

    if d == '':
        d = int(0)
    else:
        d = int(d)

    if m == 13:
        d = 366

    electricalData = electricalGSP.objects.filter(GSP=supplyPoint)
    vlqs = electricalData.values_list()
    r = np.core.records.fromrecords(
        vlqs, names=[f.name for f in electricalGSP._meta.fields])
    l = np.array(r)
    process = np.zeros([len(l), 3])

    for i in range(
            0, len(process)):  # extract the information we need for inputs.
        change = l[i]
        for j in range(1, 4):
            process[i, (j - 1)] = change[j]

    g = np.array(process)

    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': g[0:, 2]})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)

    elec = S['Data']
    finalData = elec.iloc[0:(24 * d)] / 2
    if max(finalData) > abs(min(finalData)):
        maxGSP_proxy = max(finalData)
    else:
        maxGSP_proxy = min(finalData)

    maxGSP = round(maxGSP_proxy, 2)

    statHP = process_data_normal((gspStats.objects.filter(index=supplyPoint)),
                                 gspStats, 16)
    rating = statHP[0, 1]

    if d == 1:
        formatter = DateFormatter('%H-%M')
        rule = rrulewrapper(HOURLY, interval=6)
    elif d < 4:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=1)
    elif d < 10:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=2)
    elif d > 9 and d < 16:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=3)
    elif d > 15 and d < 32:
        formatter = DateFormatter('%d-%m')
        rule = rrulewrapper(DAILY, interval=5)

    if m == 13:
        formatter = DateFormatter('%b-%Y')
        rule = rrulewrapper(MONTHLY, interval=3)

    loc = RRuleLocator(rule)
    delta = datetime.timedelta(hours=1)
    dates = drange(d1, d2, delta)
    finalDates = dates[0:(24 * d)]
    fig, ax = plt.subplots()

    ax.plot_date(finalDates,
                 finalData,
                 linestyle='-',
                 marker='None',
                 label='Demand')

    ##    if (finalData<0).any() == True and (finalData>0).any() == False:
    ##        ax.plot_date(finalDates,ratingNeg, linestyle='-', marker='None', color='r', label='GSP Rating')
    ##    elif (finalData<0).any() == False and (finalData>0).any() == True:
    ##        ax.plot_date(finalDates,rating, linestyle='-', marker='None', color='r', label='GSP Rating')
    ##    elif (finalData<0).any() == True and (finalData>0).any() == True:
    ##        ax.plot_date(finalDates,ratingNeg, linestyle='-', marker='None', color='r', label='GSP Rating')
    ##        ax.plot_date(finalDates,rating, linestyle='-', marker='None', color='r')

    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(loc)
    plt.ylabel('Electrical Demand (MWh)')
    plt.title('Electrical Demand Plot for ' + gspName.name + ' GSP')
    plt.grid(True)

    buffer = BytesIO()
    canvas = pylab.get_current_fig_manager().canvas
    canvas.draw()
    pilImage = PIL.Image.frombytes("RGB", canvas.get_width_height(),
                                   canvas.tostring_rgb())
    pilImage.save(buffer, "PNG")
    pylab.close()

    image_png = buffer.getvalue()
    graphic = base64.b64encode(image_png)
    graphic = graphic.decode('utf-8')
    ##
    return render(request, 'electrical/show_plot.html', {
        'graphic': graphic,
        'maxGSP': maxGSP,
        'rating': rating
    })
def calculateIndustrialHeatPumpDemand(supplyPoint, d, m, temp, manufacturingHP,
                                      commercialHP, entertainmentHP,
                                      educationHP, indicator):
    #heatData = industrialHeat.objects.filter(GSP = supplyPoint)
    heatData = industrialBreakdown.objects.filter(GSP=supplyPoint)

    vlqs = heatData.values_list()
    #r = np.core.records.fromrecords(vlqs, names=[f.name for f in industrialHeat._meta.fields])
    r = np.core.records.fromrecords(
        vlqs, names=[f.name for f in industrialBreakdown._meta.fields])
    l = np.array(r)
    process = np.zeros([len(l), 6])

    for i in range(
            0, len(process)):  # extract the information we need for inputs.
        change = l[i]
        for j in range(1, 7):
            process[i, (j - 1)] = change[j]

    g = np.array(process)

    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')

    proData1 = pd.DataFrame({
        'Time': dS[0:8784],
        'Manufacturing': g[0:, 2],
        'Commercial': g[0:, 3],
        'Entertainment': g[0:, 4],
        'Education': g[0:, 5]
    })
    data1 = proData1.set_index('Time')
    d1, d2, S1 = month(m, data1)

    manufacturing = S1['Manufacturing']
    totalDemandManufacturing = manufacturing.iloc[0:(24 * d)]

    commercial = S1['Commercial']
    totalDemandCommercial = commercial.iloc[0:(24 * d)]

    entertainment = S1['Entertainment']
    totalDemandEntertainment = entertainment.iloc[0:(24 * d)]

    education = S1['Education']
    totalDemandEducation = education.iloc[0:(24 * d)]

    demandIndustrialHP = np.zeros(d * 24)

    if indicator == 1:
        numbs = industrialNumbers.objects.filter(GSP=supplyPoint)
        interNumbs = np.core.records.fromrecords(
            numbs.values_list(),
            names=[f.name for f in industrialNumbers._meta.fields])
        totalNumbs = np.array(interNumbs)
        manufacturingHP = int(totalNumbs[0][2] * (manufacturingHP / 100))
        commercialHP = int(totalNumbs[0][3] * (commercialHP / 100))
        entertainmentHP = int(totalNumbs[0][4] * (entertainmentHP / 100))
        educationHP = int(totalNumbs[0][5] * (educationHP / 100))

    for i in range(1, (d + 1)):
        demMan = (totalDemandManufacturing.iloc[(
            (24 * i) - 24):(24 * i)].as_matrix()) * manufacturingHP
        demCom = (totalDemandCommercial.iloc[(
            (24 * i) - 24):(24 * i)].as_matrix()) * commercialHP
        demEnt = (totalDemandEntertainment.iloc[(
            (24 * i) - 24):(24 * i)].as_matrix()) * entertainmentHP
        demEdu = (totalDemandEducation.iloc[(
            (24 * i) - 24):(24 * i)].as_matrix()) * educationHP
        tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
        demIndustrialB = np.zeros(24)
        for j in range(0, 24):
            demIndustrialB[j] = (demMan[j] /
                                 (tempB[j] * 0.04762 + 3.03283)) + (
                                     demCom[j] /
                                     (tempB[j] * 0.04762 + 3.03283)) + (
                                         demEnt[j] /
                                         (tempB[j] * 0.04762 + 3.03283)) + (
                                             demEdu[j] /
                                             (tempB[j] * 0.04762 + 3.03283))
        demandIndustrialHP[((24 * i) - 24):(24 * i)] = demIndustrialB

    totalHeatPumpIndustrial = demandIndustrialHP

    return totalHeatPumpIndustrial
Пример #8
0
def calculateHeatPumpDemand(supplyPoint, tmy_data, d, m, smallHP, mediumHP,
                            largeHP):
    h40HP = smallHP / 2
    h60HP = smallHP / 2
    h100HP = mediumHP / 2
    h140HP = mediumHP / 2
    h160HP = largeHP

    if isinstance(h40HP, float):
        h40HP = h40HP + 0.5
        h60HP = h60HP - 0.5

    if isinstance(h100HP, float):
        h100HP = h100HP + 0.5
        h140HP = h140HP - 0.5

    numSmallHP = h40HP + h60HP
    numMediumHP = h100HP + h140HP
    numLargeHP = h160HP

    #change data type
    rawData = processData(supplyPoint)
    hourSmallHP, totalDemandSmallHP = format_inputs(rawData, h40HP, h60HP, 0,
                                                    0, 0)
    hourMediumHP, totalDemandMediumHP = format_inputs(rawData, 0, 0, h100HP,
                                                      h140HP, 0)
    hourLargeHP, totalDemandLargeHP = format_inputs(rawData, 0, 0, 0, 0,
                                                    h160HP)

    ##    latitude=statHP[0,2]
    ##    longitude=statHP[0,3]
    ##    tmy_data, altitude = extract_weather(d,m, latitude, longitude)
    temp = tmy_data['DryBulb']

    # add time element to data
    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')

    ##Small Houses with Heat Pumps
    proDataSmallHP = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandSmallHP / 1000)
    })
    dataSmallHP = proDataSmallHP.set_index('Time')
    d1, d2, smallSHP = month(m, dataSmallHP)
    reSmallHP = pd.DataFrame(smallSHP['Data'], index=smallSHP.index)
    reSmallHP.columns = ['Demand']
    finalDataSmallHP = reSmallHP.iloc[0:(24 * d)]
    indSmallHP = finalDataSmallHP / numSmallHP
    demandSmallHP = np.zeros(d * 24)

    if numSmallHP != 0:
        for i in range(1, (d + 1)):
            demB = indSmallHP.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            demSmallB = np.zeros(24)
            for j in range(0, 24):
                demSmallB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
            demandSmallHP[((24 * i) - 24):(24 * i)] = demSmallB
        smallHPTotal = demandSmallHP * numSmallHP
    else:
        smallHPTotal = np.zeros(d * 24)

    ##Medium houses with heat pumps
    proDataMediumHP = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandMediumHP / 1000)
    })
    dataMediumHP = proDataMediumHP.set_index('Time')
    d1, d2, mediumSHP = month(m, dataMediumHP)
    reMediumHP = pd.DataFrame(mediumSHP['Data'], index=mediumSHP.index)
    reMediumHP.columns = ['Demand']
    finalDataMediumHP = reMediumHP.iloc[0:(24 * d)]
    indMediumHP = finalDataMediumHP / numMediumHP
    demandMediumHP = np.zeros(d * 24)

    if numMediumHP != 0:
        for i in range(1, (d + 1)):
            demB = indMediumHP.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            demMediumB = np.zeros(24)
            for j in range(0, 24):
                demMediumB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
            demandMediumHP[((24 * i) - 24):(24 * i)] = demMediumB
        mediumHPTotal = demandMediumHP * numMediumHP
    else:
        mediumHPTotal = np.zeros(d * 24)

    ##Large houses with heat pumps
    proDataLargeHP = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandLargeHP / 1000)
    })
    dataLargeHP = proDataLargeHP.set_index('Time')
    d1, d2, largeSHP = month(m, dataLargeHP)
    reLargeHP = pd.DataFrame(largeSHP['Data'], index=largeSHP.index)
    reLargeHP.columns = ['Demand']
    finalDataLargeHP = reLargeHP.iloc[0:(24 * d)]
    indLargeHP = finalDataLargeHP / numLargeHP
    demandLargeHP = np.zeros(d * 24)

    if numLargeHP != 0:
        for i in range(1, (d + 1)):
            demB = indLargeHP.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            demLargeB = np.zeros(24)
            for j in range(0, 24):
                demLargeB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
            demandLargeHP[((24 * i) - 24):(24 * i)] = demLargeB
        largeHPTotal = demandLargeHP * numLargeHP
    else:
        largeHPTotal = np.zeros(d * 24)

    totHPCharge = (smallHPTotal[0:] / 1000) + (mediumHPTotal[0:] / 1000) + (
        largeHPTotal[0:] / 1000)

    return totHPCharge
Пример #9
0
def plot_gsp_storage(request):
    supplyPoint=request.GET['supplyPoint']
    m=int(request.GET['month'])
    d=request.GET['days']
    gspName=GSP.objects.get(idx=supplyPoint)
    esCap=int(request.GET['esCap'])

    if d =='':
        d=int(0)
    else:
        d = int(d)

    if m == 13:
        d=366
    
    electricalData = electricalGSP.objects.filter(GSP = supplyPoint)
    vlqs = electricalData.values_list()
    r = np.core.records.fromrecords(vlqs, names=[f.name for f in electricalGSP._meta.fields])
    l=np.array(r)
    process = np.zeros([len(l),3])

    for i in range(0,len(process)): # extract the information we need for inputs.
        change = l[i]
        for j in range(1,4):
            process[i,(j-1)] = change[j]

    g=np.array(process)/2

    dS=pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData=pd.DataFrame({'Time':dS[0:8784], 'Data':g[0:,2]})
    data=proData.set_index('Time')
    d1, d2, S = month(m, data)

    elec=S['Data']

    fProxy=np.zeros([24, d])
    cProxy=np.zeros([24, d])
    disProxy=np.zeros([24, d])
    newDemandProxy=np.zeros([24, d])
    
    for i in range(1,(d+1)):
        model, sol, flo, cha, dis, nD = runSolutionBalance(esCap, (elec.iloc[(24*i)-24:(24*i)]))
        fProxy[0:,(i-1)]=flo
        cProxy[0:,(i-1)]=cha
        disProxy[0:,(i-1)]=dis
        newDemandProxy[0:,(i-1)]=nD

    f=fProxy.reshape((d*24), order='F')
    c=cProxy.reshape((d*24),order='F')
    dis=disProxy.reshape((d*24),order='F')
    newDemand=newDemandProxy.reshape((d*24),order='F')

    check=all(i > 0 for i in f)

    if np.absolute(np.amax(f)) > np.absolute(np.amin(f)):
        fMax = np.amax(f)
        newDemandMax = np.amax(newDemand)
        peakReduction = 100 - round(((newDemandMax/fMax)*100),2)
        headroom = round((fMax - newDemandMax), 2)

    else:
        fMax = np.amin(f)
        newDemandMax = np.amin(newDemand)
        peakReduction = 100 - round(((newDemandMax/fMax)*100),2)
        headroom = round((newDemandMax- fMax), 2)

    statHP = process_data_normal((gspStats.objects.filter(index=supplyPoint)), gspStats, 16)
    rating=statHP[0,1]
    
    ####Plotting####
    if d == 1:
        formatter = DateFormatter('%H-%M')
        rule = rrulewrapper(HOURLY, interval=6)
    elif d < 4:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=1)
    elif d < 10:
         formatter = DateFormatter('%d-%m-%y')
         rule = rrulewrapper(DAILY, interval=2)
    elif d > 9 and d < 16:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=3)
    elif d > 15:
        formatter = DateFormatter('%d-%m')
        rule = rrulewrapper(DAILY, interval=5)

    if m==13:
        formatter = DateFormatter('%b-%Y')
        rule = rrulewrapper(MONTHLY, interval=3)

    loc = RRuleLocator(rule)
    delta = datetime.timedelta(hours=1)
    dates = drange(d1, d2, delta)
    finalDates=dates[0:(24*d)]
    fig, ax =plt.subplots()

    ax.plot_date(finalDates, f, linestyle='-', marker='None', label='Base Case (no BES)')
    ax.plot(finalDates, newDemand, linestyle='-', marker='None', label='With BES')
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(loc)
    ax.legend()
    plt.ylabel('Electrical Demand (MWh)')
    plt.title('Electrical Demand Plot for '+ gspName.name + ' GSP' + ' with ' + str(esCap)+ ' MWh BES')
    plt.grid(True)

    buffer = BytesIO()
    canvas = pylab.get_current_fig_manager().canvas
    canvas.draw()
    pilImage = PIL.Image.frombytes("RGB", canvas.get_width_height(), canvas.tostring_rgb())
    pilImage.save(buffer, "PNG")
    pylab.close()

    image_png = buffer.getvalue()
    graphic = base64.b64encode(image_png)
    graphic = graphic.decode('utf-8')

    return render(request, 'energyStorage/show_plot.html', {'graphic':graphic, 'fMax':fMax, 'newDemandMax': newDemandMax, 'peakReduction':peakReduction,'esCap':esCap, 'headroom':headroom, 'rating':rating})
Пример #10
0
def data_gsp_storage(request):
    supplyPoint=request.GET['supplyPoint']
    m=int(request.GET['month'])
    d=request.GET['days']
    gspName=GSP.objects.get(idx=supplyPoint)
    esCap=int(request.GET['esCap'])

    if d =='':
        d=int(0)
    else:
        d = int(d)

    if m == 13:
        d=366
    
    electricalData = electricalGSP.objects.filter(GSP = supplyPoint)
    vlqs = electricalData.values_list()
    r = np.core.records.fromrecords(vlqs, names=[f.name for f in electricalGSP._meta.fields])
    l=np.array(r)
    process = np.zeros([len(l),3])

    for i in range(0,len(process)): # extract the information we need for inputs.
        change = l[i]
        for j in range(1,4):
            process[i,(j-1)] = change[j]

    g=np.array(process)/2

    dS=pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData=pd.DataFrame({'Time':dS[0:8784], 'Data':g[0:,2]})
    data=proData.set_index('Time')
    d1, d2, S = month(m, data)

    elec=S['Data']

    fProxy=np.zeros([24, d])
    cProxy=np.zeros([24, d])
    disProxy=np.zeros([24, d])
    newDemandProxy=np.zeros([24, d])
    
    for i in range(1,(d+1)):
        model, sol, flo, cha, dis, nD = runSolutionBalance(esCap, (elec.iloc[(24*i)-24:(24*i)]))
        fProxy[0:,(i-1)]=flo
        cProxy[0:,(i-1)]=cha
        disProxy[0:,(i-1)]=dis
        newDemandProxy[0:,(i-1)]=nD

    f=fProxy.reshape((d*24), order='F')
    c=cProxy.reshape((d*24),order='F')
    dis=disProxy.reshape((d*24),order='F')
    newDemand=newDemandProxy.reshape((d*24),order='F')

    check=all(i > 0 for i in f)

    if np.absolute(np.amax(f)) > np.absolute(np.amin(f)):
        fMax = np.amax(f)
        newDemandMax = np.amax(newDemand)
        peakReduction = 100 - round(((newDemandMax/fMax)*100),2)
        headroom = round((fMax - newDemandMax), 2)

    else:
        fMax = np.amin(f)
        newDemandMax = np.amin(newDemand)
        peakReduction = 100 - round(((newDemandMax/fMax)*100),2)
        headroom = round((newDemandMax- fMax), 2)

    delta = datetime.timedelta(hours=1)
    dates = drange(d1, d2, delta)
    finalDates=elec[0:(24*d)].index


    powerData=np.zeros([len(f),3])
    powerData[0:,0]=f
    powerData[0:,1]=newDemand
    powerData[0:,2]=c+dis
    finalDataProxy=pd.DataFrame(powerData, index=finalDates)
    finalData=finalDataProxy.reset_index()

    response = HttpResponse(content_type='text/csv')
    file =gspName.name +'GSP_'+str(esCap)+'MWh_energy_storage.csv'
    response['Content-Disposition'] = 'attachment; filename="%s"' % file

    writer = csv.writer(response)

    with open('pv_generation.csv','w') as csvfile:
        writer.writerow(['', 'Base Demand(MWh)', 'New Demand(MWh)', 'Energy Storage Flow (MWh)'])
        for i in range(0,len(finalData)):
            writer.writerow(finalData.iloc[i,0:])
    
    return response
Пример #11
0
def industrial_electric_data(request):

    percent = int(request.GET['percent'])
    supplyPoint = request.GET['supplyPoint']
    m = int(request.GET['month'])
    d = request.GET['days']
    gspName = GSP.objects.get(idx=supplyPoint)

    if d == '':
        d = int(0)
    else:
        d = int(d)

    if m == 3 and d > 30:
        d = 30

    if m == 13:
        d = 365

    statHP = process_data_normal((gspStats.objects.filter(index=supplyPoint)),
                                 gspStats, 16)
    firm = statHP[0, 1]
    latitude = statHP[0, 2]
    longitude = statHP[0, 3]
    tmy_data, altitude = extract_weather(d, m, latitude, longitude)
    temp = tmy_data['DryBulb']

    heatData = industrialHeat.objects.filter(GSP=supplyPoint)

    vlqs = heatData.values_list()
    r = np.core.records.fromrecords(
        vlqs, names=[f.name for f in industrialHeat._meta.fields])
    l = np.array(r)
    process = np.zeros([len(l), 3])

    for i in range(
            0, len(process)):  # extract the information we need for inputs.
        change = l[i]
        for j in range(1, 4):
            process[i, (j - 1)] = change[j]

    g = np.array(process)

    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': g[0:, 2]})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)

    heat = S['Data']
    totalDemand = heat.iloc[0:(24 * d)]
    demandIndustrialHP = np.zeros(d * 24)

    for i in range(1, (d + 1)):
        demB = (totalDemand.iloc[(
            (24 * i) - 24):(24 * i)].as_matrix() * 1e6) * (percent / 100)
        tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
        demIndustrialB = np.zeros(24)
        for j in range(0, 24):
            demIndustrialB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
        demandIndustrialHP[((24 * i) - 24):(24 * i)] = demIndustrialB

    totalHeatPumpIndustrial = demandIndustrialHP / 1e6

    electricalData = process_data_normal(
        (electricalGSP.objects.filter(GSP=supplyPoint)), electricalGSP, 3)
    electricalData = electricalData[0:, 2] / 2
    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': electricalData})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)
    elec = S['Data']
    elec = elec.iloc[0:(24 * d)]
    elecChange = pd.DataFrame((elec + totalHeatPumpIndustrial),
                              index=elec.index)
    dataFinal = np.zeros([(24 * d), 2])
    dataFinal[0:, 0] = totalHeatPumpIndustrial
    dataFinal[0:, 1] = elec.as_matrix() + totalHeatPumpIndustrial

    chargeOut = pd.DataFrame(dataFinal, index=elec.index)
    finalData = chargeOut.reset_index()

    response = HttpResponse(content_type='text/csv')
    file = gspName.name + 'GSP_'
    response['Content-Disposition'] = 'attachment; filename="%s"' % file

    writer = csv.writer(response)

    with open('pv_generation.csv', 'w') as csvfile:
        writer.writerow(['', 'Electric Heat Demand(MWh)'])
        for i in range(0, len(finalData)):
            writer.writerow(finalData.iloc[i, 0:])

    return response
Пример #12
0
def industrial_electric_plot(request):

    percent = int(request.GET['percent'])
    supplyPoint = request.GET['supplyPoint']
    m = int(request.GET['month'])
    d = request.GET['days']
    gspName = GSP.objects.get(idx=supplyPoint)

    if d == '':
        d = int(0)
    else:
        d = int(d)

    if m == 3 and d > 30:
        d = 30

    if m == 13:
        d = 365

    statHP = process_data_normal((gspStats.objects.filter(index=supplyPoint)),
                                 gspStats, 16)
    firm = statHP[0, 1]
    latitude = statHP[0, 2]
    longitude = statHP[0, 3]
    tmy_data, altitude = extract_weather(d, m, latitude, longitude)
    temp = tmy_data['DryBulb']

    heatData = industrialHeat.objects.filter(GSP=supplyPoint)

    vlqs = heatData.values_list()
    r = np.core.records.fromrecords(
        vlqs, names=[f.name for f in industrialHeat._meta.fields])
    l = np.array(r)
    process = np.zeros([len(l), 3])

    for i in range(
            0, len(process)):  # extract the information we need for inputs.
        change = l[i]
        for j in range(1, 4):
            process[i, (j - 1)] = change[j]

    g = np.array(process)

    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': g[0:, 2]})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)

    heat = S['Data']
    totalDemand = heat.iloc[0:(24 * d)]
    demandIndustrialHP = np.zeros(d * 24)

    for i in range(1, (d + 1)):
        demB = (totalDemand.iloc[(
            (24 * i) - 24):(24 * i)].as_matrix() * 1e6) * (percent / 100)
        tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
        demIndustrialB = np.zeros(24)
        for j in range(0, 24):
            demIndustrialB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
        demandIndustrialHP[((24 * i) - 24):(24 * i)] = demIndustrialB

    totalHeatPumpIndustrial = demandIndustrialHP / 1e6

    electricalData = process_data_normal(
        (electricalGSP.objects.filter(GSP=supplyPoint)), electricalGSP, 3)
    electricalData = electricalData[0:, 2] / 2
    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': electricalData})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)
    elec = S['Data']
    elec = elec.iloc[0:(24 * d)]
    elecChange = pd.DataFrame((elec + totalHeatPumpIndustrial),
                              index=elec.index)

    peakChargeProxy = max(totalHeatPumpIndustrial)
    peakCharge = round(peakChargeProxy, 2)

    if max(elec.as_matrix()) > abs(min(elec.as_matrix())):
        maxGSP_proxy = max(elec.as_matrix())
    else:
        maxGSP_proxy = min(elec.as_matrix())

    maxGSP = round(maxGSP_proxy, 2)

    if max((elec + totalHeatPumpIndustrial).as_matrix()) > abs(
            min((elec + totalHeatPumpIndustrial).as_matrix())):
        maxGSPNew_proxy = max((elec + totalHeatPumpIndustrial).as_matrix())
    else:
        maxGSPNew_proxy = min((elec + totalHeatPumpIndustrial).as_matrix())

    maxGSPNew = round(maxGSPNew_proxy, 2)

    if d == 1:
        formatter = DateFormatter('%H-%M')
        rule = rrulewrapper(HOURLY, interval=6)
    elif d < 4:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=1)
    elif d < 10:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=2)
    elif d > 9 and d < 16:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=3)
    elif d > 15 and d < 32:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=5)
    elif d > 31:
        formatter = DateFormatter('%b-%Y')
        rule = rrulewrapper(MONTHLY, interval=3)

    loc = RRuleLocator(rule)
    delta = datetime.timedelta(hours=1)
    dates = drange(d1, d2, delta)
    finalDates = dates[0:(24 * d)]
    fig, ax = plt.subplots()

    ax.plot_date(finalDates,
                 totalHeatPumpIndustrial,
                 linestyle='-',
                 marker='None',
                 label=gspName.name)
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(loc)
    ax.legend()
    plt.grid(True)
    plt.ylabel('Heat Demand (MWh)')
    plt.title('Industrial Heat Demand Plot for ' + gspName.name + ' GSP')

    buffer = BytesIO()
    canvas = pylab.get_current_fig_manager().canvas
    canvas.draw()
    pilImage = PIL.Image.frombytes("RGB", canvas.get_width_height(),
                                   canvas.tostring_rgb())
    pilImage.save(buffer, "PNG")
    pylab.close()

    image_png = buffer.getvalue()
    graphic = base64.b64encode(image_png)
    graphic = graphic.decode('utf-8')
    ##
    fig2, ax2 = plt.subplots()
    ax2.plot_date(elec.index, (elec.iloc[0:].as_matrix()),
                  linestyle='-',
                  marker='None',
                  label='Base Demand')
    ax2.plot_date(elecChange.index, (elecChange.iloc[0:, 0].as_matrix()),
                  linestyle='-',
                  marker='None',
                  label='New Demand')
    ax2.xaxis.set_major_formatter(formatter)
    ax2.xaxis.set_major_locator(loc)
    ax2.legend()
    plt.grid(True)
    plt.ylabel('Electrical Demand (MWh)')
    plt.title(gspName.name + ' GSP Demand')

    buffer2 = BytesIO()
    canvas2 = pylab.get_current_fig_manager().canvas
    canvas2.draw()
    pilImage2 = PIL.Image.frombytes("RGB", canvas2.get_width_height(),
                                    canvas2.tostring_rgb())
    pilImage2.save(buffer2, "PNG")
    pylab.close()

    image_png2 = buffer2.getvalue()
    graphic2 = base64.b64encode(image_png2)
    graphic2 = graphic2.decode('utf-8')
    return render(
        request, 'electricHeat/show_plot_industrial.html', {
            'graphic': graphic,
            'graphic2': graphic2,
            'firm': firm,
            'peakCharge': peakCharge,
            'maxGSP': maxGSP,
            'maxGSPNew': maxGSPNew
        })
Пример #13
0
def storage_heater_plot(request):

    #get user input data from browser
    small = request.GET['small']
    medium = request.GET['medium']
    large = request.GET['large']
    smallHP = request.GET['smallHP']
    mediumHP = request.GET['mediumHP']
    largeHP = request.GET['largeHP']
    supplyPoint = request.GET['supplyPoint']
    m = int(request.GET['month'])
    d = request.GET['days']
    gspName = GSP.objects.get(idx=supplyPoint)

    if small == '':
        small = int(0)
    else:
        small = int(small)

    if smallHP == '':
        smallHP = int(0)
    else:
        smallHP = int(smallHP)

    if medium == '':
        medium = int(0)
    else:
        medium = int(medium)

    if mediumHP == '':
        mediumHP = int(0)
    else:
        mediumHP = int(mediumHP)

    if large == '':
        large = int(0)
    else:
        large = int(large)

    if largeHP == '':
        largeHP = int(0)
    else:
        largeHP = int(largeHP)

    if d == '':
        d = int(0)
    else:
        d = int(d)

    if m == 3 and d > 30:
        d = 30

    if m == 13:
        d = 365

    ##
    h40 = small / 2
    h60 = small / 2
    h100 = medium / 2
    h140 = medium / 2
    h160 = large

    if isinstance(h40, float):
        h40 = h40 + 0.5
        h60 = h60 - 0.5

    if isinstance(h100, float):
        h100 = h100 + 0.5
        h140 = h140 - 0.5

    numSmall = h40 + h60
    numMedium = h100 + h140
    numLarge = h160
    ##

    h40HP = smallHP / 2
    h60HP = smallHP / 2
    h100HP = mediumHP / 2
    h140HP = mediumHP / 2
    h160HP = largeHP

    if isinstance(h40HP, float):
        h40HP = h40HP + 0.5
        h60HP = h60HP - 0.5

    if isinstance(h100HP, float):
        h100HP = h100HP + 0.5
        h140HP = h140HP - 0.5

    numSmallHP = h40HP + h60HP
    numMediumHP = h100HP + h140HP
    numLargeHP = h160HP

    #change data type
    rawData = processData(supplyPoint)
    hourSmall, totalDemandSmall = format_inputs(rawData, h40, h60, 0, 0,
                                                0)  # output in Watts
    hourMedium, totalDemandMedium = format_inputs(rawData, 0, 0, h100, h140, 0)
    hourLarge, totalDemandLarge = format_inputs(rawData, 0, 0, 0, 0, h160)

    # add time element to data
    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')

    ##small sized property##

    proDataSmall = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandSmall / 1000)
    })
    dataSmall = proDataSmall.set_index('Time')
    d1, d2, smallS = month(m, dataSmall)
    reSmall = pd.DataFrame(smallS['Data'], index=smallS.index)
    reSmall.columns = ['Demand']
    finalDataSmall = reSmall.iloc[0:(24 * d)]
    chargeSmall = np.zeros(d * 24)
    indSmall = finalDataSmall / numSmall

    if numSmall != 0:
        for i in range(1, (d + 1)):
            intraSmall = pd.DataFrame(indSmall.iloc[((24 * i) - 24):(24 * i),
                                                    0].as_matrix(),
                                      index=list(range(0, 24)),
                                      columns=['Demand'])
            if i == 1:
                chg, chT, gone = storageHeater(intraSmall, 'Small',
                                               (19.3 * 0.2))
                chargeSmall[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()
            else:
                chg, chT, gone = storageHeater(intraSmall, 'Small',
                                               (19.3 * gone[0]))
                chargeSmall[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()
        smallChargeTotal = chargeSmall * numSmall
    else:
        smallChargeTotal = np.zeros(d * 24)

    ##medium sized property##

    proDataMedium = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandMedium / 1000)
    })
    dataMedium = proDataMedium.set_index('Time')
    d1, d2, mediumS = month(m, dataMedium)
    reMedium = pd.DataFrame(mediumS['Data'], index=mediumS.index)
    reMedium.columns = ['Demand']
    finalDataMedium = reMedium.iloc[0:(24 * d)]
    chargeMedium = np.zeros(d * 24)
    indMedium = finalDataMedium / numMedium

    if numMedium != 0:
        for i in range(1, (d + 1)):
            intraMedium = pd.DataFrame(indMedium.iloc[((24 * i) - 24):(24 * i),
                                                      0].as_matrix(),
                                       index=list(range(0, 24)),
                                       columns=['Demand'])
            if i == 1:
                chg, chT, gone = storageHeater(intraMedium, 'Medium',
                                               (19.3 * 0.2))
                chargeMedium[((24 * i) -
                              24):(24 * i)] = chT.iloc[0:, 0].as_matrix()
            else:
                chg, chT, gone = storageHeater(intraMedium, 'Medium',
                                               (19.3 * gone[0]))
                chargeMedium[((24 * i) -
                              24):(24 * i)] = chT.iloc[0:, 0].as_matrix()

        mediumChargeTotal = chargeMedium * numMedium
    else:
        mediumChargeTotal = np.zeros(d * 24)

    ##Large sized property##
    proDataLarge = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandLarge / 1000)
    })
    dataLarge = proDataLarge.set_index('Time')
    d1, d2, largeS = month(m, dataLarge)
    reLarge = pd.DataFrame(largeS['Data'], index=largeS.index)
    reLarge.columns = ['Demand']
    finalDataLarge = reLarge.iloc[0:(24 * d)]
    chargeLarge = np.zeros(d * 24)
    indLarge = finalDataLarge / numLarge

    if numLarge != 0:
        for i in range(1, (d + 1)):
            intraLarge = pd.DataFrame(indLarge.iloc[((24 * i) - 24):(24 * i),
                                                    0].as_matrix(),
                                      index=list(range(0, 24)),
                                      columns=['Demand'])
            if i == 1:
                chg, chT, gone = storageHeater(intraLarge, 'Large',
                                               (23.1 * 0.2))
                chargeLarge[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()
            else:
                chg, chT, gone = storageHeater(intraLarge, 'Large',
                                               (23.1 * gone[0]))
                chargeLarge[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()

        largeChargeTotal = chargeLarge * numLarge
    else:
        largeChargeTotal = np.zeros(d * 24)

    ###########################################################################################################
    ##heat pumps
    hourSmallHP, totalDemandSmallHP = format_inputs(rawData, h40HP, h60HP, 0,
                                                    0, 0)
    hourMediumHP, totalDemandMediumHP = format_inputs(rawData, 0, 0, h100HP,
                                                      h140HP, 0)
    hourLargeHP, totalDemandLargeHP = format_inputs(rawData, 0, 0, 0, 0,
                                                    h160HP)

    statHP = process_data_normal((gspStats.objects.filter(index=supplyPoint)),
                                 gspStats, 16)
    firm = statHP[0, 1]
    latitude = statHP[0, 2]
    longitude = statHP[0, 3]
    tmy_data, altitude = extract_weather(d, m, latitude, longitude)
    temp = tmy_data['DryBulb']

    ##Small Houses with Heat Pumps
    proDataSmallHP = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandSmallHP / 1000)
    })
    dataSmallHP = proDataSmallHP.set_index('Time')
    d1, d2, smallSHP = month(m, dataSmallHP)
    reSmallHP = pd.DataFrame(smallSHP['Data'], index=smallSHP.index)
    reSmallHP.columns = ['Demand']
    finalDataSmallHP = reSmallHP.iloc[0:(24 * d)]
    indSmallHP = finalDataSmallHP / numSmallHP
    demandSmallHP = np.zeros(d * 24)

    if numSmallHP != 0:
        for i in range(1, (d + 1)):
            demB = indSmallHP.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            demSmallB = np.zeros(24)
            for j in range(0, 24):
                demSmallB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
            demandSmallHP[((24 * i) - 24):(24 * i)] = demSmallB
        smallHPTotal = demandSmallHP * numSmallHP
    else:
        smallHPTotal = np.zeros(d * 24)

    ##Medium houses with heat pumps
    proDataMediumHP = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandMediumHP / 1000)
    })
    dataMediumHP = proDataMediumHP.set_index('Time')
    d1, d2, mediumSHP = month(m, dataMediumHP)
    reMediumHP = pd.DataFrame(mediumSHP['Data'], index=mediumSHP.index)
    reMediumHP.columns = ['Demand']
    finalDataMediumHP = reMediumHP.iloc[0:(24 * d)]
    indMediumHP = finalDataMediumHP / numMediumHP
    demandMediumHP = np.zeros(d * 24)

    if numMediumHP != 0:
        for i in range(1, (d + 1)):
            demB = indMediumHP.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            demMediumB = np.zeros(24)
            for j in range(0, 24):
                demMediumB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
            demandMediumHP[((24 * i) - 24):(24 * i)] = demMediumB
        mediumHPTotal = demandMediumHP * numMediumHP
    else:
        mediumHPTotal = np.zeros(d * 24)

    ##Large houses with heat pumps
    proDataLargeHP = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandLargeHP / 1000)
    })
    dataLargeHP = proDataLargeHP.set_index('Time')
    d1, d2, largeSHP = month(m, dataLargeHP)
    reLargeHP = pd.DataFrame(largeSHP['Data'], index=largeSHP.index)
    reLargeHP.columns = ['Demand']
    finalDataLargeHP = reLargeHP.iloc[0:(24 * d)]
    indLargeHP = finalDataLargeHP / numLargeHP
    demandLargeHP = np.zeros(d * 24)

    if numLargeHP != 0:
        for i in range(1, (d + 1)):
            demB = indLargeHP.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            demLargeB = np.zeros(24)
            for j in range(0, 24):
                demLargeB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
            demandLargeHP[((24 * i) - 24):(24 * i)] = demLargeB
        largeHPTotal = demandLargeHP * numLargeHP
    else:
        largeHPTotal = np.zeros(d * 24)

    #############################################################################################################
    electricalData = process_data_normal(
        (electricalGSP.objects.filter(GSP=supplyPoint)), electricalGSP, 3)
    electricalData = electricalData[0:, 2] / 2
    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': electricalData})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)
    elec = S['Data']
    elec = elec.iloc[0:(24 * d)]

    totCharge = (smallChargeTotal[0:] /
                 1000) + (mediumChargeTotal[0:] / 1000) + (
                     largeChargeTotal[0:] / 1000 + smallHPTotal[0:] / 1000 +
                     mediumHPTotal[0:] / 1000 + largeHPTotal[0:] / 1000)

    ##    if m==13:
    ##        totChargeNew=np.zeros(366*24)
    ##        totChargeNew[0:(365*24)]=totCharge
    ##        totChargeNew[(365*24):(366*24)]=totCharge[(364*24):(365*24)]

    elecChange = pd.DataFrame((elec + totCharge), index=elec.index)
    rating = np.zeros(d * 24)
    rating[0:] = firm

    peakChargeProxy = max(totCharge) * 1000
    peakCharge = round(peakChargeProxy)

    if max(elec.as_matrix()) > abs(min(elec.as_matrix())):
        maxGSP_proxy = max(elec.as_matrix())
    else:
        maxGSP_proxy = min(elec.as_matrix())

    maxGSP = round(maxGSP_proxy, 2)

    if max((elec + totCharge).as_matrix()) > abs(
            min((elec + totCharge).as_matrix())):
        maxGSPNew_proxy = max((elec + totCharge).as_matrix())
    else:
        maxGSPNew_proxy = min((elec + totCharge).as_matrix())

    maxGSPNew = round(maxGSPNew_proxy, 2)

    if d == 1:
        formatter = DateFormatter('%H-%M')
        rule = rrulewrapper(HOURLY, interval=6)
    elif d < 4:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=1)
    elif d < 10:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=2)
    elif d > 9 and d < 16:
        formatter = DateFormatter('%d-%m-%y')
        rule = rrulewrapper(DAILY, interval=3)
    elif d > 15 and d < 32:
        formatter = DateFormatter('%d-%m')
        rule = rrulewrapper(DAILY, interval=5)

    if m == 13:
        formatter = DateFormatter('%b-%Y')
        rule = rrulewrapper(MONTHLY, interval=3)

    loc = RRuleLocator(rule)
    fig, ax = plt.subplots()
    title = str(int(numSmall + numMedium +
                    numLarge)) + ' Homes with Storage Heaters \n' + str(
                        int(numSmallHP + numMediumHP +
                            numLargeHP)) + ' Homes with Heat Pumps'
    if max(totCharge > 1):
        ax.plot_date(elec.index, totCharge, linestyle='-', marker='None')
        plt.ylabel('Electrical Demand (MWh)')
    else:
        ax.plot_date(elec.index,
                     totCharge * (1000),
                     linestyle='-',
                     marker='None')
        plt.ylabel('Electrical Demand (kWh)')
    ax.xaxis.set_major_formatter(formatter)
    ax.xaxis.set_major_locator(loc)
    plt.grid(True)
    plt.title(title)

    buffer = BytesIO()
    canvas = pylab.get_current_fig_manager().canvas
    canvas.draw()
    pilImage = PIL.Image.frombytes("RGB", canvas.get_width_height(),
                                   canvas.tostring_rgb())
    pilImage.save(buffer, "PNG")
    pylab.close()

    image_png = buffer.getvalue()
    graphic = base64.b64encode(image_png)
    graphic = graphic.decode('utf-8')

    fig2, ax2 = plt.subplots()
    ax2.plot_date(elec.index, (elec.iloc[0:].as_matrix()),
                  linestyle='-',
                  marker='None',
                  label='Base Demand')
    ax2.plot_date(elecChange.index, (elecChange.iloc[0:, 0].as_matrix()),
                  linestyle='-',
                  marker='None',
                  label='New Demand')
    ax2.xaxis.set_major_formatter(formatter)
    ax2.xaxis.set_major_locator(loc)
    ax2.legend()
    plt.grid(True)
    plt.ylabel('Electrical Demand (MWh)')
    plt.title(gspName.name + ' GSP Demand')

    buffer2 = BytesIO()
    canvas2 = pylab.get_current_fig_manager().canvas
    canvas2.draw()
    pilImage2 = PIL.Image.frombytes("RGB", canvas2.get_width_height(),
                                    canvas2.tostring_rgb())
    pilImage2.save(buffer2, "PNG")
    pylab.close()

    image_png2 = buffer2.getvalue()
    graphic2 = base64.b64encode(image_png2)
    graphic2 = graphic2.decode('utf-8')

    return render(
        request, 'electricHeat/show_plot.html', {
            'graphic': graphic,
            'graphic2': graphic2,
            'firm': firm,
            'peakCharge': peakCharge,
            'maxGSP': maxGSP,
            'maxGSPNew': maxGSPNew
        })
Пример #14
0
def storage_heater_data(request):

    #get user input data from browser
    small = request.GET['small']
    medium = request.GET['medium']
    large = request.GET['large']
    smallHP = request.GET['smallHP']
    mediumHP = request.GET['mediumHP']
    largeHP = request.GET['largeHP']
    supplyPoint = request.GET['supplyPoint']
    m = int(request.GET['month'])
    d = request.GET['days']
    gspName = GSP.objects.get(idx=supplyPoint)

    if small == '':
        small = int(0)
    else:
        small = int(small)

    if smallHP == '':
        smallHP = int(0)
    else:
        smallHP = int(smallHP)

    if medium == '':
        medium = int(0)
    else:
        medium = int(medium)

    if mediumHP == '':
        mediumHP = int(0)
    else:
        mediumHP = int(mediumHP)

    if large == '':
        large = int(0)
    else:
        large = int(large)

    if largeHP == '':
        largeHP = int(0)
    else:
        largeHP = int(largeHP)

    if d == '':
        d = int(0)
    else:
        d = int(d)

    if m == 13:
        d = 366

    ##
    h40 = small / 2
    h60 = small / 2
    h100 = medium / 2
    h140 = medium / 2
    h160 = large

    if isinstance(h40, float):
        h40 = h40 + 0.5
        h60 = h60 - 0.5

    if isinstance(h100, float):
        h100 = h100 + 0.5
        h140 = h140 - 0.5

    numSmall = h40 + h60
    numMedium = h100 + h140
    numLarge = h160
    ##

    h40HP = smallHP / 2
    h60HP = smallHP / 2
    h100HP = mediumHP / 2
    h140HP = mediumHP / 2
    h160HP = largeHP

    if isinstance(h40HP, float):
        h40HP = h40HP + 0.5
        h60HP = h60HP - 0.5

    if isinstance(h100HP, float):
        h100HP = h100HP + 0.5
        h140HP = h140HP - 0.5

    numSmallHP = h40HP + h60HP
    numMediumHP = h100HP + h140HP
    numLargeHP = h160HP

    #change data type
    rawData = processData(supplyPoint)
    hourSmall, totalDemandSmall = format_inputs(rawData, h40, h60, 0, 0,
                                                0)  # output in Watts
    hourMedium, totalDemandMedium = format_inputs(rawData, 0, 0, h100, h140, 0)
    hourLarge, totalDemandLarge = format_inputs(rawData, 0, 0, 0, 0, h160)

    # add time element to data
    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')

    ##small sized property##

    proDataSmall = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandSmall / 1000)
    })
    dataSmall = proDataSmall.set_index('Time')
    d1, d2, smallS = month(m, dataSmall)
    reSmall = pd.DataFrame(smallS['Data'], index=smallS.index)
    reSmall.columns = ['Demand']
    finalDataSmall = reSmall.iloc[0:(24 * d)]
    chargeSmall = np.zeros(d * 24)
    indSmall = finalDataSmall / numSmall

    if numSmall != 0:
        for i in range(1, (d + 1)):
            intraSmall = pd.DataFrame(indSmall.iloc[((24 * i) - 24):(24 * i),
                                                    0].as_matrix(),
                                      index=list(range(0, 24)),
                                      columns=['Demand'])
            if i == 1:
                chg, chT, gone = storageHeater(intraSmall, 'Small',
                                               (19.3 * 0.2))
                chargeSmall[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()
            else:
                chg, chT, gone = storageHeater(intraSmall, 'Small',
                                               (19.3 * gone[0]))
                chargeSmall[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()
        smallChargeTotal = chargeSmall * numSmall
    else:
        smallChargeTotal = np.zeros(d * 24)

    ##medium sized property##

    proDataMedium = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandMedium / 1000)
    })
    dataMedium = proDataMedium.set_index('Time')
    d1, d2, mediumS = month(m, dataMedium)
    reMedium = pd.DataFrame(mediumS['Data'], index=mediumS.index)
    reMedium.columns = ['Demand']
    finalDataMedium = reMedium.iloc[0:(24 * d)]
    chargeMedium = np.zeros(d * 24)
    indMedium = finalDataMedium / numMedium

    if numMedium != 0:
        for i in range(1, (d + 1)):
            intraMedium = pd.DataFrame(indMedium.iloc[((24 * i) - 24):(24 * i),
                                                      0].as_matrix(),
                                       index=list(range(0, 24)),
                                       columns=['Demand'])
            if i == 1:
                chg, chT, gone = storageHeater(intraMedium, 'Medium',
                                               (19.3 * 0.2))
                chargeMedium[((24 * i) -
                              24):(24 * i)] = chT.iloc[0:, 0].as_matrix()
            else:
                chg, chT, gone = storageHeater(intraMedium, 'Medium',
                                               (19.3 * gone[0]))
                chargeMedium[((24 * i) -
                              24):(24 * i)] = chT.iloc[0:, 0].as_matrix()

        mediumChargeTotal = chargeMedium * numMedium
    else:
        mediumChargeTotal = np.zeros(d * 24)

    ##Large sized property##
    proDataLarge = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandLarge / 1000)
    })
    dataLarge = proDataLarge.set_index('Time')
    d1, d2, largeS = month(m, dataLarge)
    reLarge = pd.DataFrame(largeS['Data'], index=largeS.index)
    reLarge.columns = ['Demand']
    finalDataLarge = reLarge.iloc[0:(24 * d)]
    chargeLarge = np.zeros(d * 24)
    indLarge = finalDataLarge / numLarge

    if numLarge != 0:
        for i in range(1, (d + 1)):
            intraLarge = pd.DataFrame(indLarge.iloc[((24 * i) - 24):(24 * i),
                                                    0].as_matrix(),
                                      index=list(range(0, 24)),
                                      columns=['Demand'])
            if i == 1:
                chg, chT, gone = storageHeater(intraLarge, 'Large',
                                               (23.1 * 0.2))
                chargeLarge[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()
            else:
                chg, chT, gone = storageHeater(intraLarge, 'Large',
                                               (23.1 * gone[0]))
                chargeLarge[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()

        largeChargeTotal = chargeLarge * numLarge
    else:
        largeChargeTotal = np.zeros(d * 24)

    ###########################################################################################################
    ##heat pumps
    hourSmallHP, totalDemandSmallHP = format_inputs(rawData, h40HP, h60HP, 0,
                                                    0, 0)
    hourMediumHP, totalDemandMediumHP = format_inputs(rawData, 0, 0, h100HP,
                                                      h140HP, 0)
    hourLargeHP, totalDemandLargeHP = format_inputs(rawData, 0, 0, 0, 0,
                                                    h160HP)

    statHP = process_data_normal((gspStats.objects.filter(index=supplyPoint)),
                                 gspStats, 16)
    latitude = statHP[0, 2]
    longitude = statHP[0, 3]
    tmy_data, altitude = extract_weather(d, m, latitude, longitude)
    temp = tmy_data['DryBulb']

    ##Small Houses with Heat Pumps
    proDataSmallHP = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandSmallHP / 1000)
    })
    dataSmallHP = proDataSmallHP.set_index('Time')
    d1, d2, smallSHP = month(m, dataSmallHP)
    reSmallHP = pd.DataFrame(smallSHP['Data'], index=smallSHP.index)
    reSmallHP.columns = ['Demand']
    finalDataSmallHP = reSmallHP.iloc[0:(24 * d)]
    indSmallHP = finalDataSmallHP / numSmallHP
    demandSmallHP = np.zeros(d * 24)

    if numSmallHP != 0:
        for i in range(1, (d + 1)):
            demB = indSmallHP.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            demSmallB = np.zeros(24)
            for j in range(0, 24):
                demSmallB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
            demandSmallHP[((24 * i) - 24):(24 * i)] = demSmallB
        smallHPTotal = demandSmallHP * numSmallHP
    else:
        smallHPTotal = np.zeros(d * 24)

    ##Medium houses with heat pumps
    proDataMediumHP = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandMediumHP / 1000)
    })
    dataMediumHP = proDataMediumHP.set_index('Time')
    d1, d2, mediumSHP = month(m, dataMediumHP)
    reMediumHP = pd.DataFrame(mediumSHP['Data'], index=mediumSHP.index)
    reMediumHP.columns = ['Demand']
    finalDataMediumHP = reMediumHP.iloc[0:(24 * d)]
    indMediumHP = finalDataMediumHP / numMediumHP
    demandMediumHP = np.zeros(d * 24)

    if numMediumHP != 0:
        for i in range(1, (d + 1)):
            demB = indMediumHP.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            demMediumB = np.zeros(24)
            for j in range(0, 24):
                demMediumB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
            demandMediumHP[((24 * i) - 24):(24 * i)] = demMediumB
        mediumHPTotal = demandMediumHP * numMediumHP
    else:
        mediumHPTotal = np.zeros(d * 24)

    ##Large houses with heat pumps
    proDataLargeHP = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandLargeHP / 1000)
    })
    dataLargeHP = proDataLargeHP.set_index('Time')
    d1, d2, largeSHP = month(m, dataLargeHP)
    reLargeHP = pd.DataFrame(largeSHP['Data'], index=largeSHP.index)
    reLargeHP.columns = ['Demand']
    finalDataLargeHP = reLargeHP.iloc[0:(24 * d)]
    indLargeHP = finalDataLargeHP / numLargeHP
    demandLargeHP = np.zeros(d * 24)

    if numLargeHP != 0:
        for i in range(1, (d + 1)):
            demB = indLargeHP.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            tempB = temp.iloc[((24 * i) - 24):(24 * i)].as_matrix()
            demLargeB = np.zeros(24)
            for j in range(0, 24):
                demLargeB[j] = demB[j] / (tempB[j] * 0.04762 + 3.03283)
            demandLargeHP[((24 * i) - 24):(24 * i)] = demLargeB
        largeHPTotal = demandLargeHP * numLargeHP
    else:
        largeHPTotal = np.zeros(d * 24)

    #############################################################################################################
    electricalData = process_data_normal(
        (electricalGSP.objects.filter(GSP=supplyPoint)), electricalGSP, 3)
    electricalData = electricalData[0:, 2] / 2
    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')
    proData = pd.DataFrame({'Time': dS[0:8784], 'Data': electricalData})
    data = proData.set_index('Time')
    d1, d2, S = month(m, data)
    elec = S['Data']
    elec = elec.iloc[0:(24 * d)]

    totCharge = (smallChargeTotal[0:] /
                 1000) + (mediumChargeTotal[0:] / 1000) + (
                     largeChargeTotal[0:] / 1000 + smallHPTotal[0:] / 1000 +
                     mediumHPTotal[0:] / 1000 + largeHPTotal[0:] / 1000)
    elecChange = pd.DataFrame((elec + totCharge), index=elec.index)

    chargeOut = pd.DataFrame(totCharge, index=elec.index)
    finalData = chargeOut.reset_index()

    response = HttpResponse(content_type='text/csv')
    file = gspName.name + 'GSP_' + str(
        int(numSmall + numMedium + numLarge)) + '_storage_heaters_' + str(
            int(numSmallHP + numMediumHP + numLargeHP)) + '_heat_pumps.csv'
    response['Content-Disposition'] = 'attachment; filename="%s"' % file

    writer = csv.writer(response)

    with open('pv_generation.csv', 'w') as csvfile:
        writer.writerow(['', 'Electric Heat Demand(MWh)'])
        for i in range(0, len(finalData)):
            writer.writerow(finalData.iloc[i, 0:])

    return response
def calculateStorageHeaterDemand(supplyPoint, d, m, small, medium, large):
    h40 = small / 2
    h60 = small / 2
    h100 = medium / 2
    h140 = medium / 2
    h160 = large

    if isinstance(h40, float):
        h40 = h40 + 0.5
        h60 = h60 - 0.5

    if isinstance(h100, float):
        h100 = h100 + 0.5
        h140 = h140 - 0.5

    numSmall = h40 + h60
    numMedium = h100 + h140
    numLarge = h160

    rawData = processData(supplyPoint)
    hourSmall, totalDemandSmall = format_inputs(rawData, h40, h60, 0, 0,
                                                0)  # output in Watts
    hourMedium, totalDemandMedium = format_inputs(rawData, 0, 0, h100, h140, 0)
    hourLarge, totalDemandLarge = format_inputs(rawData, 0, 0, 0, 0, h160)

    # add time element to data
    dS = pd.date_range(start='2015-04-01', end='2016-04-01', freq='H')

    ##small sized property##

    proDataSmall = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandSmall / 1000)
    })
    dataSmall = proDataSmall.set_index('Time')
    d1, d2, smallS = month(m, dataSmall)
    reSmall = pd.DataFrame(smallS['Data'], index=smallS.index)
    reSmall.columns = ['Demand']
    finalDataSmall = reSmall.iloc[0:(24 * d)]
    chargeSmall = np.zeros(d * 24)
    indSmall = finalDataSmall / numSmall

    if numSmall != 0:
        for i in range(1, (d + 1)):
            intraSmall = pd.DataFrame(indSmall.iloc[((24 * i) - 24):(24 * i),
                                                    0].as_matrix(),
                                      index=list(range(0, 24)),
                                      columns=['Demand'])
            if i == 1:
                chg, chT, gone = storageHeater(intraSmall, 'Small',
                                               (19.3 * 0.2))
                chargeSmall[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()
            else:
                chg, chT, gone = storageHeater(intraSmall, 'Small',
                                               (19.3 * gone[0]))
                chargeSmall[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()
        smallChargeTotal = chargeSmall * numSmall
    else:
        smallChargeTotal = np.zeros(d * 24)

    ##medium sized property##

    proDataMedium = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandMedium / 1000)
    })
    dataMedium = proDataMedium.set_index('Time')
    d1, d2, mediumS = month(m, dataMedium)
    reMedium = pd.DataFrame(mediumS['Data'], index=mediumS.index)
    reMedium.columns = ['Demand']
    finalDataMedium = reMedium.iloc[0:(24 * d)]
    chargeMedium = np.zeros(d * 24)
    indMedium = finalDataMedium / numMedium

    if numMedium != 0:
        for i in range(1, (d + 1)):
            intraMedium = pd.DataFrame(indMedium.iloc[((24 * i) - 24):(24 * i),
                                                      0].as_matrix(),
                                       index=list(range(0, 24)),
                                       columns=['Demand'])
            if i == 1:
                chg, chT, gone = storageHeater(intraMedium, 'Medium',
                                               (19.3 * 0.2))
                chargeMedium[((24 * i) -
                              24):(24 * i)] = chT.iloc[0:, 0].as_matrix()
            else:
                chg, chT, gone = storageHeater(intraMedium, 'Medium',
                                               (19.3 * gone[0]))
                chargeMedium[((24 * i) -
                              24):(24 * i)] = chT.iloc[0:, 0].as_matrix()

        mediumChargeTotal = chargeMedium * numMedium
    else:
        mediumChargeTotal = np.zeros(d * 24)

    ##Large sized property##
    proDataLarge = pd.DataFrame({
        'Time': dS[0:8784],
        'Data': (totalDemandLarge / 1000)
    })
    dataLarge = proDataLarge.set_index('Time')
    d1, d2, largeS = month(m, dataLarge)
    reLarge = pd.DataFrame(largeS['Data'], index=largeS.index)
    reLarge.columns = ['Demand']
    finalDataLarge = reLarge.iloc[0:(24 * d)]
    chargeLarge = np.zeros(d * 24)
    indLarge = finalDataLarge / numLarge

    if numLarge != 0:
        for i in range(1, (d + 1)):
            intraLarge = pd.DataFrame(indLarge.iloc[((24 * i) - 24):(24 * i),
                                                    0].as_matrix(),
                                      index=list(range(0, 24)),
                                      columns=['Demand'])
            if i == 1:
                chg, chT, gone = storageHeater(intraLarge, 'Large',
                                               (23.1 * 0.2))
                chargeLarge[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()
            else:
                chg, chT, gone = storageHeater(intraLarge, 'Large',
                                               (23.1 * gone[0]))
                chargeLarge[((24 * i) - 24):(24 *
                                             i)] = chT.iloc[0:, 0].as_matrix()

        largeChargeTotal = chargeLarge * numLarge
    else:
        largeChargeTotal = np.zeros(d * 24)

    totCharge = (smallChargeTotal[0:] / 1000) + (
        mediumChargeTotal[0:] / 1000) + (largeChargeTotal[0:] / 1000)

    return totCharge