NewProducer-Copy0



In [1]:
cd git/SolarDataRESTfulAPI/


/root/git/SolarDataRESTfulAPI

In [11]:
import json
import pandas as pd
import InfluxDBInterface
import time
reload(InfluxDBInterface)
from ElasticsearchInterface import ESinterface
import IoTtoolkit
reload(IoTtoolkit)

DataLink = InfluxDBInterface.InfluxDBInterface("influxInterfaceCredentials2.json")

LogDB = DataLink.databases[u'SolarLogdata']
ProductionDB = DataLink.databases[u'SolarProductionSites']
AreaDB = DataLink.databases[u'SolarProductionAreas']
Test = DataLink.databases[u'test']

es = ESinterface()

In [27]:
def AreaProductionAlgorithm(SitesProductionBuf):
    dfPower = SitesProductionBuf.Data[PowerStreams]
    dfEnergy = SitesProductionBuf.Data[EnergyStreams]
    AreaProduction = pd.DataFrame()
    
    
    AreaProduction["Power"] = dfPower.sum(1)
    AreaProduction["nSumedPower"] = dfPower.count(1)
    #AreaProduction["PmaxSumed"]
    
    Pmax = pd.DataFrame(list(sites.loc["Pmax"]),index=list(sites.loc["online_since"]/1000),columns = ["Pmax"])#dfPower #AreaProduction
    Pmax.sort_index(inplace=True)
    Pmax["Pmax"] *= 1000
    Pmax["nMax"] =1

    #Save the period.
    Start = AreaProduction.index[0]
    Stop = AreaProduction.index[-1]
    
    #Add Pmax
    AreaProduction = AreaProduction.join(Pmax.cumsum(),how="outer")
    
    #Calculate PmaxSum
    df = pd.DataFrame(sites.loc["Pmax"]).T
    df = df.reindex_axis(sorted(df.columns), axis=1)
    df2 = dfPower.reindex_axis(sorted(dfPower.columns), axis=1)
    
    AreaProduction["PmaxSum"] = pd.DataFrame(df2.notnull().values * df.values, index = df2.index, columns = df.columns).sum(1)*1000
    
    #Add Energy and nSummedEnergy 
    AreaProduction = AreaProduction.join(pd.DataFrame(dfEnergy.ffill().sum(1),columns = ["Energy"]),how="outer")
    AreaProduction["nSumedEnergy"] = dfEnergy.count(1)
    
    
    #Cut out the part we need.
    AreaProduction.fillna(method="ffill", inplace = True)
    AreaProduction = AreaProduction.loc[Start:Stop]
    
    return AreaProduction

In [28]:
areas = es.GetHitsAsDataFrame("solar-area-index","meta-data")

#for area in areas: 
    #CalculateAreaProduction(area)
area = areas.iloc[:,3]


Got 19 Hits:

In [152]:
#def CalculateAreaProduction(area):

sites = es.GetHitsMatchingPropDict("solar-sites-index","meta-data",json.loads(area["query"]))

SitesProduction = IoTtoolkit.Feed()

PowerStreams = SitesProduction.CombineStreamsFromMulipleSources("Power",ProductionDB,sites.columns,"Power",Compressed=False)
EnergyStreams = SitesProduction.CombineStreamsFromMulipleSources("Energy",ProductionDB,sites.columns,"Energy",Compressed=True)
SitesProductionBuf = SitesProduction.GetBuffer()
SitesProductionBuf.Size = 10000
SitesProductionBuf.Seek(0)


while not SitesProductionBuf.EOF:
    AreaProduction = AreaProductionAlgorithm(SitesProductionBuf)
    Test.Replace(area.name,AreaProduction)
    SitesProductionBuf.Next()


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-152-bd9de84c19e9> in <module>()
     14 while not SitesProductionBuf.EOF:
     15     AreaProduction = AreaProductionAlgorithm(SitesProductionBuf)
---> 16     Test.Replace(area.name)
     17     SitesProductionBuf.Next()

TypeError: Replace() takes at least 3 arguments (2 given)
Got 6 Hits:

In [151]:
area


Out[151]:
u'449eda38-0141-11e4-a510-f23c9173ce4a'

In [ ]: