Beispiel #1
0
def fetchSection1_5_1Context(appDbConnStr: str, startDt: dt.datetime, endDt: dt.datetime) -> dict:
    # get WR demand from recent Fin year start till this month
    # and WR demand from 2 years back fin year to last fin year
    # example: For Jan 21, we require data from 1-Apr-2019 to 31-Mar-2020 and 1-Apr-2020 to 31 Jan 21

    finYrStart = getFinYrDt(startDt)
    prevFinYrStart = getPrevFinYrDt(finYrStart)

    finYrName = '{0}-{1}'.format(finYrStart.year, (finYrStart.year+1) % 100)
    prevFinYrName = '{0}-{1}'.format(finYrStart.year-1, finYrStart.year % 100)
    mRepo = MetricsDataRepo(appDbConnStr)
    # get WR hourly demand values for this financial year
    wrDemVals = mRepo.getEntityMetricDailyData(
        'wr', 'Max Demand(MW)', finYrStart, endDt)
    wrPrevFinYrDemVals = mRepo.getEntityMetricDailyData(
        'wr', 'Max Demand(MW)', prevFinYrStart, finYrStart-dt.timedelta(days=1))

    # create plot image for demands of prev fin year and this fin year
    pltDemObjs = [{'MONTH': x["time_stamp"], 'colName': finYrName,
                   'val': x["data_value"]} for x in wrDemVals]
    pltDemObjsLastYear = [{'MONTH': x["time_stamp"],
                           'colName': prevFinYrName, 'val': x["data_value"]} for x in wrPrevFinYrDemVals]

    pltDataObjs = pltDemObjs + pltDemObjsLastYear

    pltDataDf = pd.DataFrame(pltDataObjs)
    pltDataDf = pltDataDf.pivot(
        index='MONTH', columns='colName', values='val')
    maxTs = pltDataDf.index.max()
    for rIter in range(pltDataDf.shape[0]):
        # check if Prev fin Yr data column is not Nan, if yes set this year data column
        lastYrDt = pltDataDf.index[rIter].to_pydatetime()
        if not pd.isna(pltDataDf[prevFinYrName].iloc[rIter]):
            thisYrTs = pd.Timestamp(addMonths(lastYrDt, 12))
            if thisYrTs <= maxTs:
                thisYrVal = pltDataDf[finYrName].loc[thisYrTs]
                pltDataDf.at[pd.Timestamp(lastYrDt), finYrName] = thisYrVal
    pltDataDf = pltDataDf[~pltDataDf[prevFinYrName].isna()]

    # save plot data as excel
    pltDataDf.to_excel("assets/plot_1_5_1.xlsx", index=True)

    # derive plot title
    pltTitle = 'WR seasonal demand (daily max.) Plot {0} to {1}'.format(
        prevFinYrName, finYrName)

    # create a plotting area and get the figure, axes handle in return
    fig, ax = plt.subplots(figsize=(7.5, 4.8))
    # set plot title
    ax.set_title(pltTitle)
    # set x and y labels
    ax.set_xlabel('MONTH')
    ax.set_ylabel('MW')

    # set x axis locator as month
    ax.xaxis.set_major_locator(mdates.MonthLocator())
    # set x axis formatter as month name
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))

    # ax.set_xlim(xmin=finYrStart)

    # plot data and get the line artist object in return
    laThisYr, = ax.plot(pltDataDf.index.values,
                        pltDataDf[finYrName].values, color='#ff0000')
    laThisYr.set_label(finYrName)

    laLastYear, = ax.plot(pltDataDf.index.values,
                          pltDataDf[prevFinYrName].values, color='#0000ff')
    laLastYear.set_label(prevFinYrName)

    # enable axis grid lines
    ax.yaxis.grid(True)
    ax.xaxis.grid(True)
    # enable legends
    ax.legend(bbox_to_anchor=(0.0, -0.3, 1, 0), loc='lower center',
              ncol=2, borderaxespad=0.)
    fig.subplots_adjust(bottom=0.25, top=0.8)
    fig.savefig('assets/section_1_5_1.png')

    secData: dict = {}
    return secData
Beispiel #2
0
def fetchReservoirMonthlyTableContext(
        appDbConnStr: str, startDt: dt.datetime,
        endDt: dt.datetime) -> IReservoirMonthlyDataRecord:
    # reservoirInfo = getReservoirsMappings()

    mRepo = MetricsDataRepo(appDbConnStr)
    finYrStartDt = getFinYrDt(startDt)
    mRepo = MetricsDataRepo(appDbConnStr)
    reservoirMonthlyVals = mRepo.getReservoirMonthlyData(finYrStartDt, endDt)
    reservoirMonthlyDf = pd.DataFrame(reservoirMonthlyVals)
    reservoirMonthlyDf['day'] = 1
    # loadDurationCurve = fetchSection2_1_LoadDurationCurve(appDbConnStr ,startDt, endDt)
    reservoirMonthlyDf['Date'] = pd.to_datetime(
        reservoirMonthlyDf[['year', 'month', 'day']])
    # reservoirMonthlyDf['Date'] = reservoirMonthlyDf['Date'].dt.strftime('%d-%m-%y')
    reservoirMonthlyDf = reservoirMonthlyDf.drop(['year', 'month', 'day'],
                                                 axis=1)
    reservoirMonthlyDf = reservoirMonthlyDf.pivot(index='Date',
                                                  columns='entity_tag',
                                                  values='level_max')
    reservoirMonthlyDf = reservoirMonthlyDf.rename(
        columns={
            'Gandhi Sagar Reservoir Level ': 'a_gandhi_sagar',
            'Indira sagar Reservoir Level': 'a_indira_sagar',
            'Omkareshwar Reservoir Level': 'a_omkareshwar',
            'Kadana Reservoir Level': 'b_kadana',
            'ssp': 'b_ssp',
            'Ukai Reservoir Level': 'b_ukai',
            'Koyna Reservoir Level ': 'c_koyna'
        })

    reservoirMonthlyDf = reservoirMonthlyDf.reindex(sorted(
        reservoirMonthlyDf.columns),
                                                    axis=1)
    reservoirMonthlyDf.reset_index(inplace=True)
    reservoirMonthlyDf['Date'] = reservoirMonthlyDf['Date'].dt.strftime(
        '%b %y')
    # reservoirMonthlyDf = reservoirMonthlyDf.drop(['Date'], axis=1)
    # print("testing")

    reservoirTableList: ISection_reservoir_table["schedule_drawal"] = []

    for i in reservoirMonthlyDf.index:
        reservoirTableRecord: IReservoirMonthlyDataRecord = {
            # 'date_time': dt.datetime.strftime(reservoirMonthlyDf['Date'][i], '%d-%m-%Y'),
            'date_time': reservoirMonthlyDf['Date'][i],
            'gandhi': reservoirMonthlyDf['a_gandhi_sagar'][i],
            'indira': round(reservoirMonthlyDf['a_indira_sagar'][i]),
            'omkare': round(reservoirMonthlyDf['a_omkareshwar'][i]),
            'kadana': round(reservoirMonthlyDf['b_kadana'][i]),
            'ssp': round(reservoirMonthlyDf['b_ssp'][i]),
            'ukai': round(reservoirMonthlyDf['b_ukai'][i]),
            'koyna': round(reservoirMonthlyDf['c_koyna'][i])
        }
        reservoirTableList.append(reservoirTableRecord)

    sectionData: ISection_reservoir_table = {
        "reservoir_table": reservoirTableList
    }

    return sectionData
Beispiel #3
0
def fetchSection1_3_aContext(appDbConnStr: str, startDt: dt.datetime, endDt: dt.datetime) -> ISection_1_3_a:
    constituentsInfos = getConstituentsMappings()

    constConfig = {}
    for c in constituentsInfos:
        constConfig[c["entity_tag"]] = c["display_name"]

    dataRecords = pd.DataFrame()
    mRepo = MetricsDataRepo(appDbConnStr)
    prevFinYrStartDt = getFinYrDt(startDt)
    # get WR Unrestricted demand hourly values for this month and prev yr month
    allEntityReqMuVals = mRepo.getAllEntityMetricMonthlyData(
        'Requirement (MU)', startDt, endDt)
    allEntityAvailMuVals = mRepo.getAllEntityMetricMonthlyData(
        'Consumption(MU)', startDt, endDt)

    allEntityReqMuDf = pd.DataFrame(allEntityReqMuVals)
    allEntityAvailMuDf = pd.DataFrame(allEntityAvailMuVals)
    allEntityAvailMuDf = allEntityAvailMuDf.rename(columns={
        'metric_value': 'Consumption(MU)'})
    allEntityReqMuDf = allEntityReqMuDf.rename(columns={
        'metric_value': 'Requirement (MU)'})
    tempList = allEntityAvailMuDf['Consumption(MU)']
    allEntityReqMuDf['Consumption(MU)'] = tempList

    allEntityReqMuDf['shortage'] = round(100 *
                                         (allEntityReqMuDf['Requirement (MU)'] -
                                          allEntityReqMuDf['Consumption(MU)']) /
                                         allEntityReqMuDf['Consumption(MU)'], 2)
    # print(allEntityReqMuDf)

    prevYrAllEntityReqMuVals = mRepo.getAllEntityMetricMonthlyData(
        'Requirement (MU)', prevFinYrStartDt, endDt)
    prevYrAllEntityAvailMuVals = mRepo.getAllEntityMetricMonthlyData(
        'Consumption(MU)', prevFinYrStartDt, endDt)

    prevYrAllEntityReqMuDf = pd.DataFrame(prevYrAllEntityReqMuVals)
    prevYrAllEntityAvailMuDf = pd.DataFrame(prevYrAllEntityAvailMuVals)
    prevYrAllEntityAvailMuDf = prevYrAllEntityAvailMuDf.rename(columns={
        'metric_value': 'Consumption(MU)'})
    prevYrAllEntityReqMuDf = prevYrAllEntityReqMuDf.rename(columns={
        'metric_value': 'Requirement (MU)'})
    tempList = prevYrAllEntityAvailMuDf['Consumption(MU)']
    prevYrAllEntityReqMuDf['Consumption(MU)'] = tempList

    prevYrAllEntityReqMuDf['shortage'] = round(100 *
                                               (prevYrAllEntityReqMuDf['Requirement (MU)'] -
                                                prevYrAllEntityAvailMuDf['Consumption(MU)']) /
                                               prevYrAllEntityAvailMuDf['Consumption(MU)'], 2)
    prevYrAllEntityReqMuDf.set_index('entity_tag')
    dataRecords = pd.merge(
        allEntityReqMuDf, prevYrAllEntityReqMuDf, on='entity_tag')

    newNames = []
    for rIter in range(dataRecords.shape[0]):
        row = dataRecords.iloc[rIter, :]
        if row['entity_tag'] in constConfig:
            newNames.append(constConfig[row['entity_tag']])
        else:
            newNames.append(np.nan)
    dataRecords['entity_tag'] = newNames

    energyReqAvailList: ISection_1_3_a["energy_req_avail"] = []

    for i in dataRecords.index:
        energyReq: ISection_1_3_a = {
            'entity': dataRecords['entity_tag'][i],
            'reqMu_X': round(dataRecords['Requirement (MU)_x'][i]),
            'availMu_X': round(dataRecords['Consumption(MU)_x'][i]),
            'shortage_X': round(dataRecords['shortage_x'][i], 2),
            'reqMu_Y': round(dataRecords['Requirement (MU)_y'][i]),
            'availMu_Y': round(dataRecords['Consumption(MU)_y'][i]),
            'shortage_Y': round(dataRecords['shortage_y'][i], 2)
        }
        energyReqAvailList.append(energyReq)
    
    prevFinYrDateStr = dt.datetime.strftime(prevFinYrStartDt, "%b %y")
    sectionData: ISection_1_3_a = {
        "energy_req_avail": energyReqAvailList,
        "recent_fin_month_name": prevFinYrDateStr
    }

    return sectionData
Beispiel #4
0
def fetchReservoirContext(appDbConnStr: str, startDt: dt.datetime,
                          endDt: dt.datetime) -> IReservoirSection:
    reservoirInfo = getReservoirsMappings()

    numPages = 0
    mRepo = MetricsDataRepo(appDbConnStr)
    prevFinYrStartDt = getFinYrDt(startDt)
    prevFinYear = dt.datetime.strftime(prevFinYrStartDt, "%Y")
    currYear = int(prevFinYear)

    # last financial year details
    lastFinYrStartDt = getPrevFinYrDt(prevFinYrStartDt)
    latsFinYrEndDt = prevFinYrStartDt - dt.timedelta(days=1)
    lastFinYrStarStr = dt.datetime.strftime(lastFinYrStartDt, "%Y")
    lastFinYrEndDt = int(lastFinYrStarStr) + 1

    for itr in reservoirInfo:
        metricList: List = []
        for entity in itr:
            if entity == 'entity_tag':
                continue
            else:
                metricList.append(
                    mRepo.getReservoirDailyData(itr['entity_tag'], itr[entity],
                                                prevFinYrStartDt, endDt))
        if len(metricList[0]) > 0:
            # create plot image for all the metrices
            pltDataObj: List = []
            for temp in range(len(metricList)):
                pltDataObj = pltDataObj + [{
                    'Date': x["time_stamp"],
                    'colName': x["metric_tag"],
                    'val': x["data_value"]
                } for x in metricList[temp]]
            pltDataDf = pd.DataFrame(pltDataObj)
            pltDataDf = pltDataDf.pivot(index='Date',
                                        columns='colName',
                                        values='val')
            pltDataDf.reset_index(inplace=True)

            # derive plot title
            pltTitle = '{0} {1}-{2}'.format(itr['entity_tag'], currYear,
                                            currYear + 1)

            # create a plotting area and get the figure, axes handle in return
            fig, ax = plt.subplots(figsize=(7.5, 4.5))

            # instantiate a second axes that shares the same x-axis
            ax2 = ax.twinx()
            # set plot title
            ax.set_title(pltTitle)
            # set y labels
            ax2.set_ylabel('MUs')
            ax.set_ylabel('Meter')

            # set x axis locator as month
            ax.xaxis.set_major_locator(mdates.MonthLocator())
            # set x axis formatter as month name
            ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))

            # set x axis locator as month
            ax2.xaxis.set_major_locator(mdates.MonthLocator())
            # set x axis formatter as month name
            ax2.xaxis.set_major_formatter(mdates.DateFormatter('%b'))

            # plot data and get the line artist object in return 'GEN.'

            clr = ['#00ccff', '#ff8533', '#ff0000', '#9900ff']
            for col in range(len(pltDataDf.columns) - 1):
                if pltDataDf.columns[col + 1] == 'GEN.':
                    last, = ax2.plot(pltDataDf['Date'],
                                     pltDataDf[pltDataDf.columns[col + 1]],
                                     color=clr[col],
                                     label=pltDataDf.columns[col + 1])
                else:
                    last, = ax.plot(pltDataDf['Date'],
                                    pltDataDf[pltDataDf.columns[col + 1]],
                                    color=clr[col],
                                    label=pltDataDf.columns[col + 1])
            # enable y axis grid lines
            ax.yaxis.grid(True)

            # Ensure a major tick for each week using (interval=1)
            ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))

            # enable legends
            ax2.legend()
            ax.legend(bbox_to_anchor=(0.0, -0.3, 1, 0),
                      loc='lower center',
                      ncol=3,
                      mode="expand",
                      borderaxespad=0.)
            fig.subplots_adjust(bottom=0.25, top=0.8)
            fig.savefig('assets/reservoir_section_{0}.png'.format(numPages))
            # fig.savefig('assets/reservoir_section/{0}-{1}.png'.format(itr['entity_tag'], currYear))
            # plt.close()
            numPages += 1

        prevFinYrMetricList: List = []
        for entity in itr:
            if entity == 'entity_tag':
                continue
            else:
                prevFinYrMetricList.append(
                    mRepo.getReservoirDailyData(itr['entity_tag'], itr[entity],
                                                lastFinYrStartDt,
                                                latsFinYrEndDt))
        if len(prevFinYrMetricList[0]) > 0:
            # create plot image for all the metrices
            lastFinYrPltDataObj: List = []
            for temp in range(len(prevFinYrMetricList)):
                lastFinYrPltDataObj = lastFinYrPltDataObj + [
                    {
                        'Date': x["time_stamp"],
                        'colName': x["metric_tag"],
                        'val': x["data_value"]
                    } for x in prevFinYrMetricList[temp]
                ]
            lastFinYrPltDataDf = pd.DataFrame(lastFinYrPltDataObj)
            lastFinYrPltDataDf = lastFinYrPltDataDf.pivot(index='Date',
                                                          columns='colName',
                                                          values='val')
            lastFinYrPltDataDf.reset_index(inplace=True)

            # derive plot title
            pltTitle = '{0} {1}-{2}'.format(itr['entity_tag'],
                                            lastFinYrStarStr, lastFinYrEndDt)

            # create a plotting area and get the figure, axes handle in return
            fig, ax = plt.subplots(figsize=(7.5, 4.5))

            # instantiate a second axes that shares the same x-axis
            ax2 = ax.twinx()
            # set plot title
            ax.set_title(pltTitle)
            # set y labels
            ax2.set_ylabel('MUs')
            ax.set_ylabel('Meter')

            # set x axis locator as month
            ax.xaxis.set_major_locator(mdates.MonthLocator())
            # set x axis formatter as month name
            ax.xaxis.set_major_formatter(mdates.DateFormatter('%b'))

            # set x axis locator as month
            ax2.xaxis.set_major_locator(mdates.MonthLocator())
            # set x axis formatter as month name
            ax2.xaxis.set_major_formatter(mdates.DateFormatter('%b'))

            # plot data and get the line artist object in return 'GEN.'

            clr = ['#00ccff', '#ff8533', '#ff0000', '#9900ff']
            for col in range(len(lastFinYrPltDataDf.columns) - 1):
                if lastFinYrPltDataDf.columns[col + 1] == 'GEN.':
                    last, = ax2.plot(
                        lastFinYrPltDataDf['Date'],
                        lastFinYrPltDataDf[lastFinYrPltDataDf.columns[col +
                                                                      1]],
                        color=clr[col],
                        label=lastFinYrPltDataDf.columns[col + 1])
                else:
                    last, = ax.plot(
                        lastFinYrPltDataDf['Date'],
                        lastFinYrPltDataDf[lastFinYrPltDataDf.columns[col +
                                                                      1]],
                        color=clr[col],
                        label=lastFinYrPltDataDf.columns[col + 1])
            # enable y axis grid lines
            ax.yaxis.grid(True)
            # Ensure a major tick for each week using (interval=1)
            ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))

            # enable legends
            ax2.legend()
            ax.legend(bbox_to_anchor=(0.0, -0.3, 1, 0),
                      loc='lower center',
                      ncol=3,
                      mode="expand",
                      borderaxespad=0.)
            fig.subplots_adjust(bottom=0.25, top=0.8)
            fig.savefig('assets/reservoir_section_{0}.png'.format(numPages))

            # fig.savefig('assets/reservoir_section/{0}-{1}.png'.format(itr['entity_tag'],
            #                                         lastFinYrEndDt))
            # plt.close()
            numPages += 1

    sectionData: IReservoirSection = {'num_plts_sec_reservoir': numPages}
    return sectionData