def fetchIexGdamTableContext(appDbConnStr: str, startDt: dt.datetime, endDt: dt.datetime) -> ISection_1_4: mRepo = MetricsDataRepo(appDbConnStr) # get iex rtm data for the range between start date and end date '' iexGdamMcvVals = mRepo.getIexGdamBlockWiseData('MCV (MW)', startDt, endDt) iexGdamMcpVals = mRepo.getIexGdamBlockWiseData('MCP (Rs/MWh) ', startDt, endDt) iexGdamMcvDf = pd.DataFrame(iexGdamMcvVals) iexGdamMcpDf = pd.DataFrame(iexGdamMcpVals) tableDf = iexGdamMcvDf.groupby(['time_stamp']).mean() tableDf = tableDf.rename(columns={'data_value': 'avg_mcv_data'}) tableDf.reset_index(inplace = True) minDf = iexGdamMcvDf.groupby(['time_stamp']).min() minDf.reset_index(inplace = True) minDf = minDf.rename(columns={'data_value': 'min_mcv_data'}) tableDf = tableDf.merge(minDf[['min_mcv_data', 'time_stamp']], on = 'time_stamp') maxDf = iexGdamMcvDf.groupby(['time_stamp']).max() maxDf = maxDf.rename(columns={'data_value': 'max_mcv_data'}) maxDf.reset_index(inplace = True) tableDf = tableDf.merge(maxDf[['max_mcv_data', 'time_stamp']], on = 'time_stamp') minMcpDf = iexGdamMcpDf.groupby(['time_stamp']).min() minMcpDf.reset_index(inplace = True) minMcpDf = minMcpDf.rename(columns={'data_value': 'min_mcp_data'}) minMcpDf['min_mcp_data'] = minMcpDf['min_mcp_data']/1000 tableDf = tableDf.merge(minMcpDf[['min_mcp_data', 'time_stamp']], on = 'time_stamp') maxMcpDf = iexGdamMcpDf.groupby(['time_stamp']).max() maxMcpDf.reset_index(inplace = True) maxMcpDf = maxMcpDf.rename(columns={'data_value': 'max_mcp_data'}) maxMcpDf['max_mcp_data'] = maxMcpDf['max_mcp_data']/1000 tableDf = tableDf.merge(maxMcpDf[['max_mcp_data', 'time_stamp']], on = 'time_stamp') avgMcpDf = iexGdamMcpDf.groupby(['time_stamp']).mean() avgMcpDf.reset_index(inplace = True) avgMcpDf = avgMcpDf.rename(columns={'data_value': 'avg_mcp_data'}) avgMcpDf['avg_mcp_data'] = avgMcpDf['avg_mcp_data']/1000 tableDf = tableDf.merge(avgMcpDf[['avg_mcp_data', 'time_stamp']], on = 'time_stamp') tableDf['rtm_energy'] = tableDf['avg_mcv_data']*24/1000 iexGdamTableList: ISection_1_4()["iex_rtm_table"] = [] for i in tableDf.index: iexGdamDailyRecord: IiexGdamRecord = { 'date_time': dt.datetime.strftime(tableDf['time_stamp'][i], '%d-%m-%Y'), 'min_mcv': round(tableDf['min_mcv_data'][i]), 'max_mcv': round(tableDf['max_mcv_data'][i]), 'avg_mcv': round(tableDf['avg_mcv_data'][i]), 'min_mcp': round(tableDf['min_mcp_data'][i], 2), 'max_mcp': round(tableDf['max_mcp_data'][i], 2), 'avg_mcp': round(tableDf['avg_mcp_data'][i], 2), 'rtm_energy': round(tableDf['rtm_energy'][i], 2) } iexGdamTableList.append(iexGdamDailyRecord) reportDt = dt.datetime(endDt.year, endDt.month, endDt.day) reportDt = dt.datetime.strftime(reportDt, '%Y-%m-%d') secData: ISection_1_4 = { 'iex_gdam_table': iexGdamTableList } return secData
def fetchIexGdamGraphContext(appDbConnStr: str, startDt: dt.datetime, endDt: dt.datetime) -> bool: mRepo = MetricsDataRepo(appDbConnStr) # get iex rtm data for the range between start date and end date startDt = endDt - dt.timedelta(days=1) iexGdamMcvVals = mRepo.getIexGdamBlockWiseData('MCV (MW)', startDt, endDt) iexGdamMcpVals = mRepo.getIexGdamBlockWiseData('MCP (Rs/MWh) ', startDt, endDt) for itr in range(len(iexGdamMcvVals)): iexGdamMcvVals[itr]['metric_name'] = 'GDAM MCV(MW)' iexGdamMcvVals[itr]['time_stamp'] = itr + 1 for itr in range(len(iexGdamMcpVals)): iexGdamMcpVals[itr]['metric_name'] = 'GDAM MCP(Rs/KWH)' iexGdamMcpVals[itr]['time_stamp'] = itr + 1 # create plot image for demands of prev yr, prev month, this month iexGdamMcvObjs = [{ 'Date': x["time_stamp"], 'colName': x['metric_name'], 'val': x["data_value"] } for x in iexGdamMcvVals] iexGdamMcpObjs = [{ 'Date': x["time_stamp"], 'colName': x['metric_name'], 'val': x["data_value"] } for x in iexGdamMcpVals] pltDataObjs = iexGdamMcvObjs + iexGdamMcpObjs pltDataDf = pd.DataFrame(pltDataObjs) pltDataDf = pltDataDf.pivot(index='Date', columns='colName', values='val') pltDataDf['GDAM MCP(Rs/KWH)'] = pltDataDf['GDAM MCP(Rs/KWH)'] / 1000 # derive plot title # pltTitle = 'GDAM MCP & MCV Data as per IEX Data' dateStr = startDt.strftime("%d-%m-%Y") pltTitle = 'GDAM MCP & MCV Data as per IEX Data for {0}'.format(dateStr) # 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('Rs/KWH') ax.set_ylabel('MWH') ax.set_facecolor("#474747") # set y axis limit # fig.patch.set_facecolor('#d9ccff') clr = ['#66b3ff', '#df80ff', '#ff6666', '#00b359'] # set x xis manually x_test = [ 1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96 ] # plot data and get the line artist object in return laThisMonth, = ax.plot(pltDataDf.index.values, pltDataDf['GDAM MCV(MW)'].values, color='#66b3ff') laThisMonth.set_label('GDAM MCV(MW)') laLastYear, = ax2.plot(pltDataDf.index.values, pltDataDf['GDAM MCP(Rs/KWH)'].values, color='#df80ff') laLastYear.set_label('GDAM MCP(Rs/KWH)') ax.set_xlim((1, 96), auto=True) ax.set_xticks([ 1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96 ]) # ax.set_xlim((1, 31), auto=True) # enable legends ax.legend(bbox_to_anchor=(0.0, -0.3, 1, 0), loc='best', ncol=3, mode="expand", borderaxespad=0.) ax2.legend(bbox_to_anchor=(0.0, -0.3, 1, 0), loc='lower right', ncol=3, mode="expand", borderaxespad=0.) fig.subplots_adjust(bottom=0.25, top=0.8) fig.savefig('assets/section_1_3_b.png') plt.close() return True