Exemple #1
0
def getPrevFinYrDt(t: dt.datetime):
    t2 = addMonths(t, -1)
    if not t2.month == 4:
        t2 = addMonths(t2, -1)
        while not t2.month == 4:
            t2 = addMonths(t2, -1)
        return t2
    return t2
Exemple #2
0
def fetchSection1_1_hydroContext(appDbConnStr: str, startDt: dt.datetime,
                                 endDt: dt.datetime) -> ISection_1_1_hydro:
    mRepo = MetricsDataRepo(appDbConnStr)
    # get hydro mu
    hydroMuVals = mRepo.getEntityMetricDailyData("wr", "Hydro(MU)", startDt,
                                                 endDt)
    cgsHrdroMuVals = mRepo.getEntityMetricDailyData("wr", "CGS Hydro(Mus)",
                                                    startDt, endDt)

    maxHydroMuObj = hydroMuVals[0]
    tot_month_hydro_gen_mu = 0
    for h, c in zip(hydroMuVals, cgsHrdroMuVals):
        tot_month_hydro_gen_mu += h["data_value"] + c["data_value"]
        iterationSum = h["data_value"] + c["data_value"]
        if iterationSum > maxHydroMuObj["data_value"]:
            maxHydroMuObj["data_value"] = iterationSum
    avg_month_hydro_gen_mu = tot_month_hydro_gen_mu / len(hydroMuVals)
    max_month_hydro_gen_mu = round(maxHydroMuObj["data_value"], 2)
    max_month_hydro_gen_mu_date = dt.datetime.strftime(
        maxHydroMuObj["time_stamp"], "%d-%b-%Y")

    # get hydro mu for last year
    hydroMuLastYrVals = mRepo.getEntityMetricDailyData("wr", "Hydro(MU)",
                                                       addMonths(startDt, -12),
                                                       addMonths(endDt, -12))
    cgsHrdroMuLastYrVals = mRepo.getEntityMetricDailyData(
        "wr", "CGS Hydro(Mus)", addMonths(startDt, -12), addMonths(endDt, -12))
    tot_last_year_hydro_gen_mu = 0
    for h, c in zip(hydroMuLastYrVals, cgsHrdroMuLastYrVals):
        tot_last_year_hydro_gen_mu += h["data_value"] + c["data_value"]

    tot_last_year_hydro_gen_mu_perc = round(
        100 * (tot_month_hydro_gen_mu - tot_last_year_hydro_gen_mu) /
        tot_last_year_hydro_gen_mu, 2)

    tot_last_year_hydro_gen_mu_perc_str = ""
    if tot_last_year_hydro_gen_mu_perc < 0:
        tot_last_year_hydro_gen_mu_perc_str = "reduced by {0}%".format(
            -1 * tot_last_year_hydro_gen_mu_perc)
    else:
        tot_last_year_hydro_gen_mu_perc_str = "increased by {0}%".format(
            tot_last_year_hydro_gen_mu_perc)

    secData: ISection_1_1_hydro = {
        "tot_month_hydro_gen_mu": round(tot_month_hydro_gen_mu),
        "tot_last_year_hydro_gen_mu": round(tot_last_year_hydro_gen_mu),
        "tot_last_year_hydro_gen_mu_perc_str":
        tot_last_year_hydro_gen_mu_perc_str,
        "avg_month_hydro_gen_mu": round(avg_month_hydro_gen_mu),
        "max_month_hydro_gen_mu": round(max_month_hydro_gen_mu),
        "max_month_hydro_gen_mu_date": max_month_hydro_gen_mu_date
    }
    return secData
def fetchSection1_1_2Context(appDbConnStr: str, startDt: dt.datetime,
                             endDt: dt.datetime) -> ISection_1_1_2:

    mRepo = MetricsDataRepo(appDbConnStr)
    # get WR Unrestricted demand hourly values for this month and prev yr month
    wrDemVals = mRepo.getEntityMetricHourlyData('wr', 'Demand(MW)', startDt,
                                                endDt)

    wrPeakDemDf = pd.DataFrame(wrDemVals)
    wrPeakDemDf = wrPeakDemDf.pivot(index='time_stamp',
                                    columns='metric_name',
                                    values='data_value')

    lastYrStartDt = addMonths(startDt, -12)
    lastYrEndDt = addMonths(endDt, -12)
    # last_yr_month_name = dt.datetime.strftime(lastYrStartDt, "%b %y")

    wrLastYrDemVals = mRepo.getEntityMetricHourlyData('wr', 'Demand(MW)',
                                                      lastYrStartDt,
                                                      lastYrEndDt)

    wrLastYrPeakDemDf = pd.DataFrame(wrLastYrDemVals)
    wrLastYrPeakDemDf = wrLastYrPeakDemDf.pivot(index='time_stamp',
                                                columns='metric_name',
                                                values='data_value')

    wr_peak_dem = round(wrPeakDemDf['Demand(MW)'].max())
    maxPeakDemDt = wrPeakDemDf['Demand(MW)'].idxmax()
    wr_peak_dem_time_str = "{0} Hrs on {1}".format(
        dt.datetime.strftime(maxPeakDemDt, "%H:%M"),
        dt.datetime.strftime(maxPeakDemDt, "%d-%b-%y"))

    wr_peak_dem_last_yr = round(wrLastYrPeakDemDf['Demand(MW)'].max())

    wr_peak_dem_perc_inc = 100 * \
        (wr_peak_dem - wr_peak_dem_last_yr)/wr_peak_dem_last_yr
    wr_peak_dem_perc_inc = round(wr_peak_dem_perc_inc, 2)

    wr_avg_dem = round(wrPeakDemDf['Demand(MW)'].mean())
    wr_avg_dem_last_yr = round(wrLastYrPeakDemDf['Demand(MW)'].mean())
    wr_avg_dem_perc_inc = round(
        100 * (wr_avg_dem - wr_avg_dem_last_yr) / wr_avg_dem_last_yr, 2)
    secData: ISection_1_1_2 = {
        'wr_peak_dem_met': wr_peak_dem,
        'wr_peak_dem_time_str': wr_peak_dem_time_str,
        'wr_peak_dem_perc_inc': wr_peak_dem_perc_inc,
        'wr_last_year_peak_dem': wr_peak_dem_last_yr,
        'wr_avg_dem': wr_avg_dem,
        'wr_avg_dem_last_yr': wr_avg_dem_last_yr,
        'wr_avg_dem_perc_inc': wr_avg_dem_perc_inc
    }
    return secData
Exemple #4
0
def fetchSection2_1_LoadDurationCurve(appDbConnStr: str, startDt: dt.datetime, endDt: dt.datetime) -> dict:
    
    mRepo = MetricsDataRepo(appDbConnStr)

    currentMonthMW = mRepo.getEntityMetricHourlyData('wr','Demand(MW)', startDt , endDt)
    df = pd.DataFrame(currentMonthMW)
    currentMonthMWVals = deriveDurationVals(df['data_value'],100)

    pastMonthMW = mRepo.getEntityMetricHourlyData('wr','Demand(MW)',addMonths(startDt,-1) , addMonths(endDt,-1))
    df = pd.DataFrame(pastMonthMW)
    pastMonthMWVals = deriveDurationVals(df['data_value'],100)

    pastYearMW = mRepo.getEntityMetricHourlyData('wr','Demand(MW)',addMonths(startDt,-12) , addMonths(endDt,-12))
    df = pd.DataFrame(pastYearMW)
    pastYearMWVals = deriveDurationVals(df['data_value'],100)

    pltTitle = 'Load Duration Curve {0}, {1} & {2}'.format(startDt.strftime('%b-%y') , addMonths(startDt,-1).strftime('%b-%y') , addMonths(startDt,-12).strftime('%b-%y'))

    fig, ax = plt.subplots(figsize=(7.5, 4.5))

    ax.set_title(pltTitle)
    ax.set_ylabel('Demand met (MW)')
    ax.set_xlabel('% of time')

    # ax.xaxis.set_major_locator(mdates.DayLocator(interval=2))
    # ax.xaxis.set_major_formatter(mdates.DateFormatter('%d'))

    
    ax.plot( currentMonthMWVals['perc_exceeded'],currentMonthMWVals['bins'], color='red',label=dt.datetime.strftime(startDt, '%b-%y'))
    ax.plot( pastMonthMWVals['perc_exceeded'] , pastMonthMWVals['bins'],color='blue', label=addMonths(startDt,-1).strftime('%b-%y'))
    ax.plot( pastYearMWVals['perc_exceeded'] , pastYearMWVals['bins'], color='green', label=addMonths(startDt,-12).strftime('%b-%y'))


    ax.yaxis.grid(True)
    ax.xaxis.grid(True)
    ax.legend( loc='best',
                    ncol=4, borderaxespad=0.)


    plt.xticks(np.arange(0,110,10))
    ax.set_xlim(xmin=0 , xmax= 100)
    ax.set_ylim(ymin=35000, ymax=65000)
    fig.subplots_adjust(bottom=0.25, top=0.8)
    
    ax.set_facecolor("#cbffff")

    fig.savefig('assets/section_2_1_loadDurationCurve.png')
    # plt.show()
    # plt.close()

    secData: dict = {}
    return secData
Exemple #5
0
def fetchSection1_1_4Context(appDbConnStr: str, startDt: dt.datetime,
                             endDt: dt.datetime) -> ISection_1_1_4:
    monthDtObj = dt.datetime(startDt.year, startDt.month, 1)
    month_name = dt.datetime.strftime(startDt, "%b %y")
    mRepo = MetricsDataRepo(appDbConnStr)
    # get WR Unrestricted demand hourly values for this month and prev yr month
    wrConMuVals = mRepo.getEntityMetricDailyData('wr', 'Requirement (MU)',
                                                 startDt, endDt)
    wrConMuDf = pd.DataFrame(wrConMuVals)
    wrConMuDf = wrConMuDf.pivot(index='time_stamp',
                                columns='metric_name',
                                values='data_value')

    lastYrStartDt = addMonths(startDt, -12)
    lastYrEndDt = addMonths(endDt, -12)
    last_yr_month_name = dt.datetime.strftime(lastYrStartDt, "%b %y")
    wrLastYrConMuVals = mRepo.getEntityMetricDailyData('wr',
                                                       'Requirement (MU)',
                                                       lastYrStartDt,
                                                       lastYrEndDt)
    wrLastYrConMuDf = pd.DataFrame(wrLastYrConMuVals)
    wrLastYrConMuDf = wrLastYrConMuDf.pivot(index='time_stamp',
                                            columns='metric_name',
                                            values='data_value')

    wr_tot_req_mu = round(wrConMuDf['Requirement (MU)'].sum())
    wr_avg_req_mu = round(wrConMuDf['Requirement (MU)'].mean())
    wr_max_req_mu = round(wrConMuDf['Requirement (MU)'].max())
    wrMaxConsMuDate = wrConMuDf['Requirement (MU)'].idxmax()
    wr_avg_req_mu_last_yr = round(wrLastYrConMuDf['Requirement (MU)'].mean())

    wr_max_req_mu_date = "{0}".format(
        dt.datetime.strftime(wrMaxConsMuDate, "%d-%b-%y"))

    wr_avg_req_mu_perc_inc = round(
        100 * (wr_avg_req_mu - wr_avg_req_mu_last_yr) / wr_avg_req_mu_last_yr,
        2)
    secData: ISection_1_1_4 = {
        'wr_tot_req_mu': wr_tot_req_mu,
        'wr_avg_req_mu': wr_avg_req_mu,
        'wr_max_req_mu': wr_max_req_mu,
        'wr_max_req_mu_date': wr_max_req_mu_date,
        'wr_avg_req_mu_perc_inc': wr_avg_req_mu_perc_inc,
        'wr_avg_req_mu_last_yr': wr_avg_req_mu_last_yr
    }
    return secData
Exemple #6
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
Exemple #7
0
def fetchSection1_1_WindSolarContext(
        appDbConnStr: str, startDt: dt.datetime,
        endDt: dt.datetime) -> ISection_1_1_wind_solar:
    mRepo = MetricsDataRepo(appDbConnStr)
    # get solar mu
    solarMuVals = mRepo.getEntityMetricDailyData("wr", "Solar(MU)", startDt,
                                                 endDt)
    cgsSolarMuVals = mRepo.getEntityMetricDailyData("wr", "CGS Solar(Mus)",
                                                    startDt, endDt)

    tot_month_solar_gen_mu = 0
    for s, c in zip(solarMuVals, cgsSolarMuVals):
        tot_month_solar_gen_mu += s["data_value"] + c["data_value"]
    avg_month_solar_gen_mu = tot_month_solar_gen_mu / len(solarMuVals)

    # get solar mu for last year
    solarMuLastYrVals = mRepo.getEntityMetricDailyData("wr", "Solar(MU)",
                                                       addMonths(startDt, -12),
                                                       addMonths(endDt, -12))

    cgsSolarMuLastYrVals = mRepo.getEntityMetricDailyData(
        "wr", "CGS Solar(Mus)", addMonths(startDt, -12), addMonths(endDt, -12))

    tot_last_year_solar_gen_mu = 0
    for s, c in zip(solarMuLastYrVals, cgsSolarMuLastYrVals):
        tot_last_year_solar_gen_mu += s["data_value"] + c["data_value"]

    tot_last_year_solar_gen_mu_perc = round(
        100 * (tot_month_solar_gen_mu - tot_last_year_solar_gen_mu) /
        tot_last_year_solar_gen_mu, 2)

    tot_last_year_solar_gen_mu_perc_str = ""
    if tot_last_year_solar_gen_mu_perc < 0:
        tot_last_year_solar_gen_mu_perc_str = "reduced by {0}%".format(
            -1 * tot_last_year_solar_gen_mu_perc)
    else:
        tot_last_year_solar_gen_mu_perc_str = "increased by {0}%".format(
            tot_last_year_solar_gen_mu_perc)

    solarMwVals = mRepo.getEntityMetricHourlyData("wr", "Solar(MW)", startDt,
                                                  endDt)
    maxSolarMwObj = solarMwVals[0]
    for v in solarMwVals:
        if v["data_value"] > maxSolarMwObj["data_value"]:
            maxSolarMwObj = v
    max_month_solar_gen_mw = maxSolarMwObj["data_value"]
    max_month_solar_gen_mw_date = '{0} at {1} hrs'.format(
        dt.datetime.strftime(maxSolarMwObj["time_stamp"], "%d %b %Y"),
        dt.datetime.strftime(maxSolarMwObj["time_stamp"], "%H:%M"))

    # get wind mu
    windMuVals = mRepo.getEntityMetricDailyData("wr", "Wind(MU)", startDt,
                                                endDt)
    cgsWindMuVals = mRepo.getEntityMetricDailyData("wr", "CGS Wind(Mus)",
                                                   startDt, endDt)
    tot_month_wind_gen_mu = 0
    for w, c in zip(windMuVals, cgsWindMuVals):
        tot_month_wind_gen_mu += w["data_value"] + c["data_value"]
    avg_month_wind_gen_mu = tot_month_wind_gen_mu / len(windMuVals)

    # get wind mu for last year
    windMuLastYrVals = mRepo.getEntityMetricDailyData("wr", "Wind(MU)",
                                                      addMonths(startDt, -12),
                                                      addMonths(endDt, -12))
    cgsWindMuLastYrVals = mRepo.getEntityMetricDailyData(
        "wr", "CGS Wind(Mus)", addMonths(startDt, -12), addMonths(endDt, -12))
    tot_last_year_wind_gen_mu = 0
    for w, c in zip(windMuLastYrVals, cgsWindMuLastYrVals):
        tot_last_year_wind_gen_mu += w["data_value"] + c["data_value"]

    tot_last_year_wind_gen_mu_perc = round(
        100 * (tot_month_wind_gen_mu - tot_last_year_wind_gen_mu) /
        tot_last_year_wind_gen_mu, 2)

    tot_last_year_wind_gen_mu_perc_str = ""
    if tot_last_year_wind_gen_mu_perc < 0:
        tot_last_year_wind_gen_mu_perc_str = "reduced by {0}%".format(
            -1 * tot_last_year_wind_gen_mu_perc)
    else:
        tot_last_year_wind_gen_mu_perc_str = "increased by {0}%".format(
            tot_last_year_wind_gen_mu_perc)

    windMwVals = mRepo.getEntityMetricHourlyData("wr", "Wind(MW)", startDt,
                                                 endDt)
    maxWindMwObj = windMwVals[0]
    for v in windMwVals:
        if v["data_value"] > maxWindMwObj["data_value"]:
            maxWindMwObj = v
    max_month_wind_gen_mw = maxWindMwObj["data_value"]
    max_month_wind_gen_mw_date = '{0} at {1} hrs'.format(
        dt.datetime.strftime(maxWindMwObj["time_stamp"], "%d %b %Y"),
        dt.datetime.strftime(maxWindMwObj["time_stamp"], "%H:%M"))

    # create dataframe for solar and wind addition
    resDf = pd.DataFrame(windMwVals + solarMwVals)
    resDf = resDf.pivot(index="time_stamp",
                        columns="metric_name",
                        values="data_value")
    resDf["Renewable"] = resDf["Wind(MW)"] + resDf["Solar(MW)"]
    max_month_ren_gen_mw = resDf["Renewable"].max()
    maxRenDt = resDf["Renewable"].idxmax().to_pydatetime()
    max_month_ren_gen_mw_date = '{0} at {1} hrs'.format(
        dt.datetime.strftime(maxRenDt, "%d %b %Y"),
        dt.datetime.strftime(maxRenDt, "%H:%M"))
    secData: ISection_1_1_wind_solar = {
        'tot_month_wind_gen_mu': round(tot_month_wind_gen_mu),
        'avg_month_wind_gen_mu': round(avg_month_wind_gen_mu, 1),
        'tot_last_year_wind_gen_mu_perc_str':
        tot_last_year_wind_gen_mu_perc_str,
        'tot_last_year_wind_gen_mu': round(tot_last_year_wind_gen_mu),
        'max_month_wind_gen_mw': round(max_month_wind_gen_mw),
        'max_month_wind_gen_mw_date': max_month_wind_gen_mw_date,
        'tot_month_solar_gen_mu': round(tot_month_solar_gen_mu),
        'avg_month_solar_gen_mu': round(avg_month_solar_gen_mu, 1),
        'tot_last_year_solar_gen_mu_perc_str':
        tot_last_year_solar_gen_mu_perc_str,
        'tot_last_year_solar_gen_mu': round(tot_last_year_solar_gen_mu),
        'max_month_solar_gen_mw': round(max_month_solar_gen_mw),
        'max_month_solar_gen_mw_date': max_month_solar_gen_mw_date,
        'max_month_ren_gen_mw': round(max_month_ren_gen_mw),
        'max_month_ren_gen_mw_date': max_month_ren_gen_mw_date
    }
    return secData
def fetchSection1_4_2Context(appDbConnStr: str, startDt: dt.datetime,
                             endDt: dt.datetime) -> ISection_1_4_2:
    monthName = dt.datetime.strftime(startDt, "%b %y")
    mRepo = MetricsDataRepo(appDbConnStr)
    # get WR demand hourly values for this month and prev yr month
    wrDemVals = mRepo.getEntityMetricHourlyData('wr', 'Demand(MW)', startDt,
                                                endDt)
    demVals = [x['data_value'] for x in wrDemVals]
    wr_max_dem = max(demVals)
    wrMaxDemDt = wrDemVals[demVals.index(wr_max_dem)]['time_stamp']
    wr_max_dem_date_str = dt.datetime.strftime(wrMaxDemDt, "%d-%b-%y")
    wr_avg_dem = sum(demVals) / len(demVals)
    wrMaxDemTimestampStr = dt.datetime.strftime(wrMaxDemDt,
                                                "%d-%b-%y %H:%M") + " hrs"

    lastYrStartDt = addMonths(startDt, -12)
    lastYrEndDt = addMonths(endDt, -12)
    monthNameLastYear = dt.datetime.strftime(lastYrStartDt, "%b %y")
    wrLastYrDemVals = mRepo.getEntityMetricHourlyData('wr', 'Demand(MW)',
                                                      lastYrStartDt,
                                                      lastYrEndDt)
    demVals = [x['data_value'] for x in wrLastYrDemVals]
    wr_max_dem_last_year = max(demVals)
    wr_max_dem_date_str_last_year = dt.datetime.strftime(
        wrLastYrDemVals[demVals.index(wr_max_dem_last_year)]['time_stamp'],
        "%d-%b-%y")
    wr_avg_dem_last_year = sum(demVals) / len(demVals)

    wr_avg_dem_perc_change_last_year = round(
        100 * (wr_avg_dem - wr_avg_dem_last_year) / wr_avg_dem_last_year, 2)
    wr_max_dem_perc_change_last_year = round(
        100 * (wr_max_dem - wr_max_dem_last_year) / wr_max_dem_last_year, 2)

    prevMonthStartDt = addMonths(startDt, -1)
    prevMonthEndDt = addMonths(endDt, -1)
    prev_month_name = dt.datetime.strftime(prevMonthStartDt, "%b %y")
    wrPrevMonthDemVals = mRepo.getEntityMetricHourlyData(
        'wr', 'Demand(MW)', prevMonthStartDt, prevMonthEndDt)
    demVals = [x['data_value'] for x in wrPrevMonthDemVals]
    wr_max_dem_prev_month = max(demVals)
    wr_max_dem_date_str_prev_month = dt.datetime.strftime(
        wrPrevMonthDemVals[demVals.index(wr_max_dem_prev_month)]['time_stamp'],
        "%d-%b-%y")
    wr_avg_dem_prev_month = sum(demVals) / len(demVals)

    wr_avg_dem_perc_change_prev_month = round(
        100 * (wr_avg_dem - wr_avg_dem_prev_month) / wr_avg_dem_prev_month, 2)
    wr_max_dem_perc_change_prev_month = round(
        100 * (wr_max_dem - wr_max_dem_prev_month) / wr_max_dem_prev_month, 2)

    # create plot image for demands of prev yr, prev month, this month
    pltDemObjs = [{
        'Date': convertDtToDayNum(x["time_stamp"]),
        'colName': monthName,
        'val': x["data_value"]
    } for x in wrDemVals]
    pltDemObjsLastYear = [{
        'Date': convertDtToDayNum(x["time_stamp"]),
        'colName': monthNameLastYear,
        'val': x["data_value"]
    } for x in wrLastYrDemVals]
    pltDemObjsPrevMonth = [{
        'Date': convertDtToDayNum(x["time_stamp"]),
        'colName': prev_month_name,
        'val': x["data_value"]
    } for x in wrPrevMonthDemVals]
    pltDataObjs = pltDemObjs + pltDemObjsLastYear + pltDemObjsPrevMonth

    pltDataDf = pd.DataFrame(pltDataObjs)
    pltDataDf = pltDataDf.pivot(index='Date', columns='colName', values='val')
    pltDataDf.reset_index(inplace=True)
    pltDataDf["Date"] = [math.floor(x) for x in pltDataDf["Date"]]
    pltDataDf = pltDataDf.groupby(by="Date").max()
    # save plot data as excel
    pltDataDf.to_excel("assets/plot_1_4_2.xlsx", index=True)

    # derive plot title
    pltTitle = 'Demand met {0}, {1} & {2} \n Max. {3} MW on dt. {4} \n Average Load Growth {5}{6} against last year'.format(
        monthName, prev_month_name, monthNameLastYear,
        format(round(wr_max_dem), ","), wrMaxDemTimestampStr,
        wr_avg_dem_perc_change_last_year, "%")

    # create a plotting area and get the figure, axes handle in return
    fig, ax = plt.subplots(figsize=(7.5, 4.5))
    # set plot title
    ax.set_title(pltTitle)
    # set x and y labels
    ax.set_xlabel('Date')
    ax.set_ylabel('MW')
    ax.set_facecolor("#c6d9f1")
    fig.patch.set_facecolor('#fac090')
    # plot data and get the line artist object in return
    laThisMonth, = ax.plot(pltDataDf.index.values,
                           pltDataDf[monthName].values,
                           color='#ff0000')
    laThisMonth.set_label(monthName)

    laLastYear, = ax.plot(pltDataDf.index.values,
                          pltDataDf[monthNameLastYear].values,
                          color='#00ff00')
    laLastYear.set_label(monthNameLastYear)

    laPrevMonth, = ax.plot(pltDataDf.index.values,
                           pltDataDf[prev_month_name].values,
                           color='#A52A2A')
    laPrevMonth.set_label(prev_month_name)

    ax.set_xlim((1, 31), auto=True)
    # enable y axis grid lines
    ax.yaxis.grid(True)
    # enable legends
    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/section_1_4_2.png')

    secData: ISection_1_4_2 = {
        'prev_month_name': prev_month_name,
        'wr_max_dem': round(wr_max_dem),
        'wr_max_dem_date_str': wr_max_dem_date_str,
        'wr_avg_dem': round(wr_avg_dem),
        'wr_max_dem_last_year': round(wr_max_dem_last_year),
        'wr_max_dem_date_str_last_year': wr_max_dem_date_str_last_year,
        'wr_avg_dem_last_year': round(wr_avg_dem_last_year),
        'wr_avg_dem_perc_change_last_year': wr_avg_dem_perc_change_last_year,
        'wr_max_dem_perc_change_last_year': wr_max_dem_perc_change_last_year,
        'wr_max_dem_prev_month': round(wr_max_dem_prev_month),
        'wr_max_dem_date_str_prev_month': wr_max_dem_date_str_prev_month,
        'wr_avg_dem_prev_month': round(wr_avg_dem_prev_month),
        'wr_avg_dem_perc_change_prev_month': wr_avg_dem_perc_change_prev_month,
        'wr_max_dem_perc_change_prev_month': wr_max_dem_perc_change_prev_month
    }
    return secData
def fetchSection1_11_windPLF(appDbConnStr: str, startDt: dt.datetime,
                             endDt: dt.datetime) -> ISection_1_11_PLFCUF:
    constituentsInfos = getREConstituentsMappings()
    mRepo = MetricsDataRepo(appDbConnStr)

    numOfDays = (endDt - startDt).days + 1

    soFarHighestAllEntityGenVals = mRepo.getSoFarHighestAllEntityData(
        'soFarHighestWindGen', addMonths(startDt, -1))
    soFarHighestGenLookUp = {}
    for v in soFarHighestAllEntityGenVals:
        soFarHighestGenLookUp[v['constituent']] = {
            'value': v['data_value'],
            'ts': v['data_time']
        }

    dispRows: List[IPLFCUFDataRow] = []
    for cIter in range(len(constituentsInfos)):
        constInfo = constituentsInfos[cIter]

        if (math.isnan(constInfo['windCapacity'])):
            continue

        maxGenData = mRepo.getEntityMetricHourlyData(constInfo["entity_tag"],
                                                     "Wind(MW)", startDt,
                                                     endDt)

        if constInfo['entity_tag'] == 'central':
            windEnerConsumption = mRepo.getEntityMetricDailyData(
                'wr', 'CGS Wind(Mus)', startDt, endDt)
        elif constInfo['entity_tag'] == 'wr':
            windEnerConsumption = mRepo.getEntityMetricDailyData(
                'wr', "Wind(MU)", startDt, endDt)
            cgsWindEnerConsumption = mRepo.getEntityMetricDailyData(
                'wr', 'CGS Wind(Mus)', startDt, endDt)
            for w, c in zip(windEnerConsumption, cgsWindEnerConsumption):
                w["data_value"] += c["data_value"]
        else:
            windEnerConsumption = mRepo.getEntityMetricDailyData(
                constInfo['entity_tag'], 'Wind(MU)', startDt, endDt)

        energyConsumption = mRepo.getEntityMetricDailyData(
            constInfo['entity_tag'], 'Consumption(MU)', startDt, endDt)

        maxGenDf = pd.DataFrame(maxGenData)
        maxGenDf = maxGenDf.pivot(index='time_stamp',
                                  columns='metric_name',
                                  values='data_value')
        windEnerConsumptionSum = pd.DataFrame(windEnerConsumption).groupby(
            'entity_tag').sum().iloc[0]['data_value']

        if (len(energyConsumption) == 0):
            # This is for central sector as we dont have consumption data
            # calculate mu from mw
            # df = pd.DataFrame(maxGenData)
            # average = df.groupby('entity_tag').mean()
            # windEnerConsumptionSumDf = average * 0.024 * numOfDays #To Calculate Avg MU from MW
            # windEnerConsumptionSum = windEnerConsumptionSumDf.iloc[0]['data_value']
            EnerConsumptionSum = 0
            penetrationLevel = 0
        else:
            EnerConsumptionSum = pd.DataFrame(energyConsumption).groupby(
                'entity_tag').sum().iloc[0]['data_value']
            penetrationLevel = round(
                (windEnerConsumptionSum / EnerConsumptionSum) * 100, 2)

        maxWind = maxGenDf["Wind(MW)"].max()
        maxWindDt = maxGenDf["Wind(MW)"].idxmax()

        plf = (windEnerConsumptionSum *
               1000) / (int(constInfo['windCapacity']) * 24 * numOfDays)
        cuf = maxWind * 100 / (int(constInfo['windCapacity']))

        prevHighestWindObj = soFarHighestGenLookUp[constInfo["entity_tag"]]
        newHighestWind = maxWind
        newHighestWindTime = maxWindDt.to_pydatetime()

        if newHighestWind < prevHighestWindObj["value"]:
            newHighestWind = prevHighestWindObj["value"]
            newHighestWindTime = prevHighestWindObj["ts"]

        mRepo.insertSoFarHighest(constInfo['entity_tag'],
                                 "soFarHighestWindGen", startDt,
                                 newHighestWind, newHighestWindTime)
        # soFarHighestAllEntityGenVals = mRepo.getSoFarHighestAllEntityData(
        # 'soFarHighestWindGen', startDt)
        # soFarHighestGenLookUp = {}
        # for v in soFarHighestAllEntityGenVals:
        #     soFarHighestGenLookUp[v['constituent']] = {
        #     'value': v['data_value'], 'ts': v['data_time']}
        so_far_high_gen_str = str(
            round(newHighestWind)) + ' on ' + dt.datetime.strftime(
                newHighestWindTime,
                '%d-%b-%Y') + ' at ' + dt.datetime.strftime(
                    newHighestWindTime, '%H:%S')

        const_display_row: IPLFCUFDataRow = {
            'entity': constInfo['display_name'],
            'capacityMW': round(constInfo['windCapacity']),
            'maxgenerationMW': round(maxWind),
            'soFarHighestGenMW': so_far_high_gen_str,
            'energyGeneration': round(windEnerConsumptionSum),
            'energyConsumption': round(EnerConsumptionSum),
            'penetration': penetrationLevel,
            'plf': round(plf * 100),
            'cuf': round(cuf)
        }
        dispRows.append(const_display_row)

    secData: ISection_1_11_PLFCUF = {"so_far_hig_win_gen_plf": dispRows}
    return secData
Exemple #10
0
def fetchSection1_1_1Context(appDbConnStr: str, startDt: dt.datetime,
                             endDt: dt.datetime) -> ISection_1_1_1:
    monthDtObj = dt.datetime(startDt.year, startDt.month, 1)
    month_name = dt.datetime.strftime(startDt, "%b' %y")
    full_month_name = dt.datetime.strftime(startDt, "%B %Y")
    mRepo = MetricsDataRepo(appDbConnStr)
    # get WR Unrestricted demand hourly values for this month and prev yr month
    wrDemVals = mRepo.getEntityMetricHourlyData('wr', 'Demand(MW)', startDt,
                                                endDt)
    wrLoadSheddingVals = mRepo.getEntityMetricHourlyData(
        'wr', 'Load Shedding(MW)', startDt, endDt)
    wrUnResDemDf = pd.DataFrame(wrDemVals + wrLoadSheddingVals)
    wrUnResDemDf = wrUnResDemDf.pivot(index='time_stamp',
                                      columns='metric_name',
                                      values='data_value')
    wrUnResDemDf['UnresDem'] = wrUnResDemDf['Demand(MW)'] + \
        wrUnResDemDf['Load Shedding(MW)']

    lastYrStartDt = addMonths(startDt, -12)
    lastYrEndDt = addMonths(endDt, -12)
    last_yr_month_name = dt.datetime.strftime(lastYrStartDt, "%b %y")
    wrLastYrDemVals = mRepo.getEntityMetricHourlyData('wr', 'Demand(MW)',
                                                      lastYrStartDt,
                                                      lastYrEndDt)
    wrLastYrLoadSheddingVals = mRepo.getEntityMetricHourlyData(
        'wr', 'Load Shedding(MW)', lastYrStartDt, lastYrEndDt)
    wrLastYrUnResDemDf = pd.DataFrame(wrLastYrDemVals +
                                      wrLastYrLoadSheddingVals)
    wrLastYrUnResDemDf = wrLastYrUnResDemDf.pivot(index='time_stamp',
                                                  columns='metric_name',
                                                  values='data_value')
    wrLastYrUnResDemDf['UnresDem'] = wrLastYrUnResDemDf['Demand(MW)'] + \
        wrLastYrUnResDemDf['Load Shedding(MW)']

    wr_max_unres_dem = round(wrUnResDemDf['UnresDem'].max())
    maxUnresDemDt = wrUnResDemDf['UnresDem'].idxmax()
    wr_max_unres_dem_time_str = "{0} Hrs on {1}".format(
        dt.datetime.strftime(maxUnresDemDt, "%H:%M"),
        dt.datetime.strftime(maxUnresDemDt, "%d-%b-%y"))

    wr_max_unres_dem_last_yr = round(wrLastYrUnResDemDf['UnresDem'].max())

    wr_max_unres_dem_perc_inc = 100 * \
        (wr_max_unres_dem - wr_max_unres_dem_last_yr)/wr_max_unres_dem_last_yr
    wr_max_unres_dem_perc_inc = round(wr_max_unres_dem_perc_inc, 2)

    wr_avg_unres_dem = round(wrUnResDemDf['UnresDem'].mean())
    wr_avg_unres_dem_last_yr = round(wrLastYrUnResDemDf['UnresDem'].mean())
    wr_avg_unres_dem_perc_inc = round(
        100 * (wr_avg_unres_dem - wr_avg_unres_dem_last_yr) /
        wr_avg_unres_dem_last_yr, 2)
    secData: ISection_1_1_1 = {
        'monthDtObj': monthDtObj,
        'month_name': month_name,
        'full_month_name': full_month_name,
        'last_yr_month_name': last_yr_month_name,
        'wr_max_unres_dem': wr_max_unres_dem,
        'wr_max_unres_dem_time_str': wr_max_unres_dem_time_str,
        'wr_max_unres_dem_perc_inc': wr_max_unres_dem_perc_inc,
        'wr_max_unres_dem_last_yr': wr_max_unres_dem_last_yr,
        'wr_avg_unres_dem': wr_avg_unres_dem,
        'wr_avg_unres_dem_last_yr': wr_avg_unres_dem_last_yr,
        'wr_avg_unres_dem_perc_inc': wr_avg_unres_dem_perc_inc
    }
    return secData
def fetchSection1_3_bContext(appDbConnStr: str, startDt: dt.datetime,
                             endDt: dt.datetime) -> ISection_1_3_b:
    constituentsInfos = getConstituentsMappings()
    mRepo = MetricsDataRepo(appDbConnStr)

    soFarHighestAllEntityReqVals = mRepo.getSoFarHighestAllEntityData(
        'soFarHighestRequirement', addMonths(startDt, -1))
    soFarHighestReqLookUp = {}
    for v in soFarHighestAllEntityReqVals:
        soFarHighestReqLookUp[v['constituent']] = {
            'value': v['data_value'],
            'ts': v['data_time']
        }

    soFarHighestAllEntityAvailVals = mRepo.getSoFarHighestAllEntityData(
        'soFarHighestAvailability', addMonths(startDt, -1))
    soFarHighestAvailLookUp = {}
    for v in soFarHighestAllEntityAvailVals:
        soFarHighestAvailLookUp[v['constituent']] = {
            'value': v['data_value'],
            'ts': v['data_time']
        }
    dispRows: List[ISoFarHighestDataRow] = []
    for cIter in range(len(constituentsInfos)):
        constInfo = constituentsInfos[cIter]
        availData = mRepo.getEntityMetricHourlyData(constInfo["entity_tag"],
                                                    "Demand(MW)", startDt,
                                                    endDt)
        loadSheddingData = mRepo.getEntityMetricHourlyData(
            constInfo["entity_tag"], "Load Shedding(MW)", startDt, endDt)
        availReqData = availData + loadSheddingData
        availReqDataDf = pd.DataFrame(availReqData)
        availReqDataDf = availReqDataDf.pivot(index='time_stamp',
                                              columns='metric_name',
                                              values='data_value')
        availReqDataDf.reset_index(inplace=True)
        availReqDataDf["Requirement"] = availReqDataDf["Demand(MW)"] + \
            availReqDataDf["Load Shedding(MW)"]
        maxReq = availReqDataDf["Requirement"].max()
        maxReqDt = availReqDataDf["time_stamp"].loc[
            availReqDataDf["Requirement"].idxmax()]
        maxAvail = availReqDataDf["Demand(MW)"].max()
        maxAvailDt = availReqDataDf["time_stamp"].loc[
            availReqDataDf["Demand(MW)"].idxmax()]
        maxShortagePerc = round(100 * (maxReq - maxAvail) / maxAvail, 2)

        prevHighestReqObj = soFarHighestReqLookUp[constInfo["entity_tag"]]
        newHighestReq = maxReq
        newHighestReqTime = maxReqDt.to_pydatetime()
        if newHighestReq < prevHighestReqObj["value"]:
            newHighestReq = prevHighestReqObj["value"]
            newHighestReqTime = prevHighestReqObj["ts"]

        prevHighestAvailObj = soFarHighestAvailLookUp[constInfo["entity_tag"]]
        newHighestAvail = maxAvail
        newHighestAvailTime = maxAvailDt.to_pydatetime()
        if newHighestAvail < prevHighestAvailObj["value"]:
            newHighestAvail = prevHighestAvailObj["value"]
            newHighestAvailTime = prevHighestAvailObj["ts"]
        newHighestShortagePerc = round(
            100 * (newHighestReq - newHighestAvail) / newHighestAvail, 2)
        newHighestAvailTime = newHighestAvailTime
        newHighestReqTime = newHighestReqTime
        mRepo.insertSoFarHighest(constInfo['entity_tag'],
                                 "soFarHighestAvailability", startDt,
                                 newHighestAvail, newHighestAvailTime)
        mRepo.insertSoFarHighest(constInfo['entity_tag'],
                                 "soFarHighestRequirement", startDt,
                                 newHighestReq, newHighestReqTime)

        const_display_row: ISoFarHighestDataRow = {
            'entity':
            constInfo['display_name'],
            'peakReqMW':
            round(maxReq),
            'peakAvailMW':
            round(maxAvail),
            'shortage_X':
            maxShortagePerc,
            'highestReqMW':
            round(newHighestReq),
            'highestAvailMW':
            round(newHighestAvail),
            'shortage_Y':
            newHighestShortagePerc,
            'highestReqMWDateStr':
            'Max on {0} at {1} hrs'.format(
                dt.datetime.strftime(newHighestReqTime, "%d.%m.%y"),
                dt.datetime.strftime(newHighestReqTime, "%H:%M")),
            'highestAvailMWDateStr':
            'Max on {0} at {1} hrs'.format(
                dt.datetime.strftime(newHighestAvailTime, "%d.%m.%y"),
                dt.datetime.strftime(newHighestAvailTime, "%H:%M"))
        }
        dispRows.append(const_display_row)

    secData: ISection_1_3_b = {"so_far_hig_req_avail": dispRows}
    return secData
Exemple #12
0
from src.utils.addMonths import addMonths
import datetime as dt

initConfigs()
filesSheet = getFileMappings()

startDt = dt.datetime(2021, 1, 1)
endDt = dt.datetime(2021, 1, 1)

targetDt = startDt
while targetDt <= endDt:
    print('processing for {0}'.format(targetDt))
    for eachrow in filesSheet:
        print(eachrow['file_type'])
        excelFilePath = getExcelFilePath(eachrow, targetDt)
        if eachrow['file_type'] == 'iex_dam_data':
            iexDamService(excelFilePath)
        # if eachrow['file_type'] == 'iex_rtm_data':
        #     statesDailyService(statesConfigSheet, excelFilePath)
        # if eachrow['file_type'] == 'iex_gtam_data':
        #     linesGenService(statesConfigSheet, excelFilePath)
        # if eachrow['file_type'] == 'pxi_dam_data':
        #     statesHourlyService(statesConfigSheet, excelFilePath)
        # if eachrow['file_type'] == 'pxi_rtm_data':
        #     reservoirService(excelFilePath)
        # if eachrow['file_type'] == 'wbes_rtm_iex_data':
        #     gujREGenerationService(excelFilePath)
        # if eachrow['file_type'] == 'wbes_rtm_pxi_data':
        #     gujREGenerationService(excelFilePath)
    targetDt = addMonths(targetDt, 1)
    def getReportContextObj(self, monthDt: dt.datetime) -> IReportCxt:
        """get the report context object for populating the weekly report template
        Args:
            monthDt (dt.datetime): month date object
        Returns:
            IReportCxt: report context object
        """
        # create context for weekly report
        reportContext: IReportCxt = {}

        startDt = dt.datetime(monthDt.year, monthDt.month, 1)
        endDt = addMonths(startDt, 1) - dt.timedelta(days=1)
        if self.sectionCtrls["1_1_1"]:
            # get section 1.1.1 data
            try:
                secData_1_1_1 = fetchSection1_1_1Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_1_1)
                print("section 1_1_1 context setting complete")
            except Exception as err:
                print("error while fetching section 1_1_1")
                print(err)

        if self.sectionCtrls["1_1_2"]:
            # get section 1.1.2 data
            try:
                secData_1_1_2 = fetchSection1_1_2Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_1_2)
                print("section 1_1_2 context setting complete")
            except Exception as err:
                print("error while fetching section 1_1_2")
                print(err)

        if self.sectionCtrls["1_1_3"]:
            # get section 1.1.2 data
            try:
                secData_1_1_3 = fetchSection1_1_3Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_1_3)
                print("section 1_1_3 context setting complete")
            except Exception as err:
                print("error while fetching section 1_1_3")

        if self.sectionCtrls["1_1_4"]:
            # get section 1.1.2 data
            try:
                secData_1_1_4 = fetchSection1_1_4Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_1_4)
                print("section 1_1_4 context setting complete")
            except Exception as err:
                print("error while fetching section 1_1_4")
                print(err)

        # get section 1.1 volt data
        if self.sectionCtrls["1_1_volt"]:
            try:
                secData_1_1_volt = fetchSection1_1_voltContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_1_volt)
                print("section 1_1_volt context setting complete")
            except Exception as err:
                print("error while fetching section 1_1_volt")
                print(err)

        if self.sectionCtrls["1_1_freq"]:
            # get section 1.1.freq data
            try:
                secData_1_1_freq = fetchSection1_1_freq_Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_1_freq)
                print("section 1_1_freq context setting complete")
            except Exception as err:
                print("error while fetching section 1_1_freq")
                print(err)

        if self.sectionCtrls["1_1_hydro"]:
            # get section 1.1.hydro data
            try:
                secData_1_1_hydro = fetchSection1_1_hydroContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_1_hydro)
                print("section 1_1_hydro context setting complete")
            except Exception as err:
                print("error while fetching section 1_1_hydro")
                print(err)

        if self.sectionCtrls["1_1_wind_solar"]:
            # get section 1.1.wind_solar data
            try:
                secData_1_1_wind_solar = fetchSection1_1_WindSolarContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_1_wind_solar)
                print("section 1_1_wind_solar context setting complete")
            except Exception as err:
                print("error while fetching section 1_1_wind_solar")
                print(err)

        # get section 1.3.a data
        if self.sectionCtrls["1_3_a"]:
            try:
                secData_1_3_a: ISection_1_3_a = fetchSection1_3_aContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_3_a)
                print("section 1_3_a context setting complete")
            except Exception as err:
                print("error while fetching section 1_3_a")
                print(err)

        # get section 1.3.b data
        if self.sectionCtrls['1_3_b']:
            try:
                secData_1_3_b: List[ISection_1_3_b] = fetchSection1_3_bContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_3_b)
                print('section_1_3_b context setting complete')
            except Exception as err:
                print("error while fetching section 1_3_b")
                print(err)

        if self.sectionCtrls["1_4_1"]:
            # get section 1.4.1 data
            try:
                secData_1_4_1 = fetchSection1_4_1Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_4_1)
                print("section 1_4_1 context setting complete")
            except Exception as err:
                print("error while fetching section 1_4_1")
                print(err)

        if self.sectionCtrls["1_4_2"]:
            # get section 1.4.2 data
            try:
                secData_1_4_2 = fetchSection1_4_2Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_4_2)
                print("section 1_4_2 context setting complete")
            except Exception as err:
                print("error while fetching section 1_4_2")
                print(err)

        if self.sectionCtrls["1_5_1"]:
            # get section 1.5.1 data
            try:
                secData_1_5_1 = fetchSection1_5_1Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_5_1)
                print("section 1_5_1 context setting complete")
            except Exception as err:
                print("error while fetching section 1_5_1")
                print(err)

        if self.sectionCtrls["1_5_2"]:
            # get section 1.5.2 data
            try:
                secData_1_5_2 = fetchSection1_5_2Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_5_2)
                print("section 1_5_2 context setting complete")
            except Exception as err:
                print("error while fetching section 1_5_2")
                print(err)

        if self.sectionCtrls["1_5_3"]:
            # get section 1.5.3 data
            try:
                secData_1_5_3 = fetchSection1_5_3Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_5_3)
                print("section 1_5_3 context setting complete")
            except Exception as err:
                print("error while fetching section 1_5_3")
                print(err)

        if self.sectionCtrls["1_6_1"]:
            # get section 1.6.1 data
            try:
                secData_1_6_1 = fetchSection1_6_1Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_6_1)
                print("section 1_6_1 context setting complete")
            except Exception as err:
                print("error while fetching section 1_6_1")
                print(err)

        if self.sectionCtrls["1_7_1"]:
            # get section 1.7.1 data
            try:
                secData_1_7_1 = fetchSection1_7_1Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_7_1)
                print("section 1_7_1 context setting complete")
            except Exception as err:
                print("error while fetching section 1_7_1")
                print(err)

        if self.sectionCtrls["1_7_2"]:
            # get section 1.7.2 data
            try:
                secData_1_7_2 = fetchSection1_7_2Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_7_2)
                print("section 1_7_2 context setting complete")
            except Exception as err:
                print("error while fetching section 1_7_2")
                print(err)

        if self.sectionCtrls["1_7_3"]:
            # get section 1.7.3 data
            try:
                secData_1_7_3 = fetchSection1_7_3Context(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_7_3)
                print("section 1_7_3 context setting complete")
            except Exception as err:
                print("error while fetching section 1_7_3")
                print(err)

        if self.sectionCtrls["1_9"]:
            # get section 1.9 data
            try:
                secData_1_9 = fetchSection1_9Context(self.appDbConStr, startDt,
                                                     endDt)
                reportContext.update(secData_1_9)
                print("section 1_9 context setting complete")
            except Exception as err:
                print("error while fetching section 1_9")
                print(err)
        if self.sectionCtrls["1_10"]:
            # get section 1.10 data

            try:
                secData_1_10 = fetchSection1_10Context(self.outageDbConnStr,
                                                       startDt, endDt)
                reportContext.update(secData_1_10)
                print("section 1_10 context setting complete")
            except Exception as err:
                print("error while fetching section 1_10")
                print(err)
        if self.sectionCtrls["1_11_solar_plf"]:
            try:
                secData_1_11_solarplf = fetchSection1_11_solarPLF(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_11_solarplf)
                print("section 1_11_solar_plf context setting complete")
            except Exception as err:
                print("error while fetching section 1_11_solar_plf")
                print(err)

        if self.sectionCtrls["1_11_wind_plf"]:
            try:
                secData_1_11_windplf = fetchSection1_11_windPLF(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_11_windplf)
                print("section 1_11_wind_plf context setting complete")
            except Exception as err:
                print("error while fetching section 1_11_wind_plf")
                print(err)

        if self.sectionCtrls["1_11_solar"]:
            # get section 1.9 data
            try:
                secData_1_11_solar = fetchSection1_11_SolarContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_11_solar)
                print("section 1_11_solar context setting complete")
            except Exception as err:
                print("error while fetching section 1_11_solar")
                print(err)

        if self.sectionCtrls["1_11_wind"]:
            # get section 1.11.wind.a data
            try:
                secData_1_11_wind = fetchSection1_11_Wind(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_11_wind)
                print("section 1_11_wind context setting complete")
            except Exception as err:
                print("error while fetching section 1_11_wind")
                print(err)
        if self.sectionCtrls["1_11_gen_curve"]:

            try:
                secData_1_11_GenCurve = fetchSection1_11_GenerationCurve(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_11_GenCurve)
                print("section 1_11_GenCurve context setting complete")
            except Exception as err:
                print("error while fetching section 1_11_GenCurve")
                print(err)
        if self.sectionCtrls["1_11_wind_c"]:
            # get section 1.11.wind.c data
            try:
                secData_1_11_wind_c = fetchSection1_11_wind_cContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_11_wind_c)
                print("section 1_11_wind_c context setting complete")
            except Exception as err:
                print("error while fetching section 1_11_wind_c")
                print(err)

        if self.sectionCtrls["1_11_solar_c"]:
            # get section 1.11.wind.c data
            try:
                secData_1_11_solar_c = fetchSection1_11_solar_cContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_11_solar_c)
                print("section 1_11_solar_c context setting complete")
            except Exception as err:
                print("error while fetching section 1_11_solar_c")
                print(err)

        if self.sectionCtrls["1_11_solarGen"]:
            try:
                secData_1_11_solarGen = fetchSection1_11_SolarGen(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_11_solarGen)
                print("section 1_11_solar_gen context setting complete")
            except Exception as err:
                print("error while fetching section 1_11_solar_gen")
                print(err)

        if self.sectionCtrls["1_11_loadCurve"]:
            try:
                secData_1_11_loadCurve = fetchSection1_11_LoadCurve(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_1_11_loadCurve)
                print("section 1_11_loadCurve context setting complete")
            except Exception as err:
                print("error while fetching section 1_11_loadCurve")
                print(err)
        if self.sectionCtrls["reservoir"]:
            # get section reservoir data
            try:
                secData_reservoir = fetchReservoirContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_reservoir)
                print("section reservoir context setting complete")
            except Exception as err:
                print("error while fetching section reservoir")
                print(err)
        # get reservoir section table data
        if self.sectionCtrls["reservoir_table"]:
            # get section reservoir data
            try:
                secData_reservoir = fetchReservoirMonthlyTableContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_reservoir)
                print("section hydro reservoir table context setting complete")
            except Exception as err:
                print("error while fetching section hydro reservoir table")
                print(err)
        # get section 1.12 inter regional data
        if self.sectionCtrls["1_12"]:
            try:
                secData_1_12 = fetchSection1_12Context(self.appDbConStr,
                                                       startDt, endDt)
                reportContext.update(secData_1_12)
                print("section 1_12 context setting complete")
            except Exception as err:
                print("error while fetching section 1_12")
                print(err)
        if self.sectionCtrls["1_13"]:
            try:
                from src.config.appConfig import getJsonConfig
                appConfig = getJsonConfig()
                filePath = appConfig['rrasFilePath']
                secData_1_13 = fetchSection1_13Context(self.appDbConStr,
                                                       filePath, startDt,
                                                       endDt)
                reportContext.update(secData_1_13)
                print("section 1_13 context setting complete")
            except Exception as err:
                print("error while fetching section 1_13")
                print(err)
        if self.sectionCtrls["2_1"]:
            try:
                secData_2_1 = fetchSection2_1(self.appDbConStr, startDt, endDt)
                reportContext.update(secData_2_1)
                print("section 2_1 context setting complete")
            except Exception as err:
                print("error while fetching section 2_1")
                print(err)

        # get section 2_3_max data
        if self.sectionCtrls["2_3_Max"]:
            try:
                secData_2_3_Max = fetchSection2_3_MaxContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_2_3_Max)
                print("section 2_3_Max context setting complete")
            except Exception as err:
                print("error while fetching section 2_3_Max")
                print(err)

        # get section 2_3_min data
        if self.sectionCtrls["2_3_Min"]:
            try:
                secData_2_3_Min = fetchSection2_3_MinContext(
                    self.appDbConStr, startDt, endDt)
                reportContext.update(secData_2_3_Min)
                print("section 2_3_Min context setting complete")
            except Exception as err:
                print("error while fetching section 2_3_Min")
                print(err)

        return reportContext
Exemple #14
0
def fetchSection1_10Context(appDbConnStr: str, startDt: dt.datetime,
                            endDt: dt.datetime) -> ISection_1_10:
    mRepo = MetricsDataRepo(appDbConnStr)
    plannedMonthlyData = mRepo.getOutageData('PLANNED', startDt, endDt)
    forcedMonthlyData = mRepo.getOutageData('FORCED', startDt, endDt)
    totalMonthlyData = calculateTotal(plannedMonthlyData, forcedMonthlyData)

    plannedMonthlyDataLastMonth = mRepo.getOutageData(
        'PLANNED', addMonths(startDt, -1), startDt - dt.timedelta(days=1))
    forcedMonthlyDataLastMonth = mRepo.getOutageData(
        'FORCED', addMonths(startDt, -1), startDt - dt.timedelta(days=1))

    totalMonthlyDataLastMonth = calculateTotal(plannedMonthlyDataLastMonth,
                                               forcedMonthlyDataLastMonth)

    monthArray: list[IOutageDetails] = []
    prevMonthArray: list[IOutageDetails] = []

    monthDates = list(plannedMonthlyData.keys())
    prevMonthDates = list(totalMonthlyDataLastMonth.keys())

    for idx, date in enumerate(monthDates):
        dateFormat = dt.datetime.strptime(str(date), '%Y%m%d')

        dayDetail: IOutageDetails = {
            'idx': idx,
            'date': dateFormat,
            'planned': plannedMonthlyData[date],
            'forced': forcedMonthlyData[date],
            'total': totalMonthlyData[date]
        }
        monthArray.append(dayDetail)
    mx = {
        'idx': max(len(monthDates), len(prevMonthDates)),
        'date': 'Max',
        'planned': max(plannedMonthlyData.values()),
        'forced': max(forcedMonthlyData.values()),
        'total': max(totalMonthlyData.values())
    }
    monthArray.append(mx)

    avg = {
        'idx': max(len(monthDates), len(prevMonthDates)) + 1,
        'date': 'AVG',
        'planned': round(mean(plannedMonthlyData.values())),
        'forced': round(mean(forcedMonthlyData.values())),
        'total': round(mean(totalMonthlyData.values()))
    }
    monthArray.append(avg)
    monthDf = pd.DataFrame(monthArray)

    for idx, date in enumerate(prevMonthDates):
        if idx < len(monthDates):
            dateFormat = dt.datetime.strptime(str(monthDates[idx]), '%Y%m%d')
        else:
            dateFormat = ''
        dayDetail: IOutageDetails = {
            'idx': idx,
            'total_pre': totalMonthlyDataLastMonth[date]
        }
        prevMonthArray.append(dayDetail)
    mx = {
        'idx': max(len(monthDates), len(prevMonthDates)),
        'total_pre': max(totalMonthlyDataLastMonth.values())
    }
    prevMonthArray.append(mx)

    avg = {
        'idx': max(len(monthDates), len(prevMonthDates)) + 1,
        'total_pre': round(mean(totalMonthlyDataLastMonth.values()))
    }
    prevMonthArray.append(avg)
    prevMonthDf = pd.DataFrame(prevMonthArray)
    resultDf = monthDf.merge(prevMonthDf,
                             left_on='idx',
                             right_on='idx',
                             how='outer')
    resultDf = resultDf.sort_values(by=['idx'])
    # resultDf['date'] = resultDf['date'].dt.strftime()
    resultDf['dateStr'] = resultDf['date'].apply(
        lambda x: x.strftime('%d-%b-%Y') if type(x) is dt.datetime else x)

    # if(len(monthDates) < len(prevMonthDates)):
    #     itr = len(monthDates)
    #     while itr <= len(prevMonthDates)-1:
    #         date = prevMonthDates[itr]
    #         dateFormat = dt.datetime.strptime(str(date),'%Y%m%d')
    #         dayDetail:IOutageDetails = {
    #             'date': dateFormat,
    #             'planned': 0,
    #             'forced': 0,
    #             'total': 0,
    #             'total_pre':totalMonthlyDataLastMonth[date]
    #         }
    #         monthArray.append(dayDetail)
    #         itr = itr + 1

    # for eachDay in monthArray:
    #     eachDay['date'] = dt.datetime.strftime(eachDay['date'],"%d-%b-%Y")

    sectionData: ISection_1_10 = {
        "generation_outages": resultDf.to_dict('records')
    }

    monthLabel = 'Total - ' + dt.datetime.strftime(startDt, '%b-%Y')
    preMonthLabel = 'Total - ' + dt.datetime.strftime(addMonths(startDt, -1),
                                                      '%b-%Y')

    graphDf = resultDf[~pd.isna(resultDf['date'])]
    graphDf = graphDf.iloc[:-2]
    graphDf.to_excel("assets/generation_outage.xlsx", index=False)
    pltTitle = 'Unit Outage {0}'.format(monthLabel)

    fig, ax = plt.subplots(figsize=(7.5, 5.5))

    ax.set_title(pltTitle)
    ax.set_ylabel('MW')
    ax.set_xlabel('Date')

    fig.patch.set_facecolor('#c0d9f1')

    ax.xaxis.set_major_locator(mdates.DayLocator(interval=2))
    ax.xaxis.set_major_formatter(mdates.DateFormatter("%d-%b-%Y"))

    ax.plot(graphDf['date'],
            graphDf['planned'],
            color='#00ccff',
            label='Planned')
    ax.plot(graphDf['date'],
            graphDf['forced'],
            color='#ff8533',
            label='Forced')
    ax.plot(graphDf['date'],
            graphDf['total'],
            color='#ff0000',
            label=monthLabel)
    ax.plot(graphDf['date'],
            graphDf['total_pre'],
            color='#9900ff',
            label=preMonthLabel)
    ax.yaxis.grid(True)
    ax.legend(bbox_to_anchor=(0.0, -0.46, 1, 0),
              loc='lower left',
              ncol=4,
              borderaxespad=0.)

    plt.xticks(rotation=90)
    # ax.set_xlim( allDatesArray[0], allDatesArray[1] )
    fig.subplots_adjust(bottom=0.25, top=0.8)

    fig.savefig('assets/section_1_10_generation_outage.png')
    # plt.show()
    # plt.close()
    return sectionData