def WriteInterpolatedForecastToDB(WellName, corpID, ForecastName, ForecastYear, scenarioName, GFO, UserName, results): import datetime as dt import pandas as pd from Model import ModelLayer as m header_corpID = '' Messages = [] for item in results.iterrows(): idx = item[0] UpdateDate = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S") if header_corpID != corpID: ForecastHeaderObj = m.ForecastHeaderRow(WellName, corpID, ForecastName, ForecastYear, scenarioName, [], GFO, '') Success, Message = ForecastHeaderObj.Write(UserName, UpdateDate) if not Success: Messages.append(Message) else: header_corpID = corpID Date_Key = item[1]['Date'].strftime('%m/%d/%Y') Gas_Production = item[1]['GasProduction'] Oil_Production = item[1]['OilProduction'] GasNF = item[1]['GasNF'] OilNF = item[1]['OilNF'] ForecastDataObj = m.ForecastDataRow(ForecastName, corpID, Date_Key, Gas_Production, Oil_Production, 0, GasNF, OilNF, 0, '') Success, Message = ForecastDataObj.Write(UserName, UpdateDate) if not Success: Messages.append(Message) return Success, Messages
def CallUpdateForecastHeader(): """Interface Package Description""" interface = { "HeaderName": str, "WellName": str, "CorpID": str, "ForecastName": str, "ForecastYear": str, "scenarioName": str, "GFO": str, "UpdateUser": str } pkg, success, msg = InitializePayload(request, interface) if success: HeaderName = pkg['HeaderName'] WellName = pkg['WellName'] CorpID = pkg['CorpID'] ForecastName = pkg['ForecastName'] ForecastYear = pkg['ForecastYear'] scenarioName = pkg['scenarioName'] GFO = pkg['GFO'] UserName = pkg['UpdateUser'] Arps = {} Arps['Di'] = '' Arps['qi'] = '' Arps['b'] = '' #Check CorpID if Wellname is passed and vice versa WellName, CorpID = iu.GetWellandCorpID(WellName, CorpID) ForecastHeaderRow = m.ForecastHeaderRow(WellName, CorpID, ForecastName, ForecastYear, scenarioName, Arps, GFO, '') success, msg = ForecastHeaderRow.Update(UserName, datetime.now()) output = ConfigureOutput('', success, msg) return output
def SOHA_WriteInternalForecasttoDB(df, ForecastName, ForecastYear, Production_Column_Name, User, GFO=True): #Part of to be deprecated methods to convert SoHa internal GFO data to standard from Model import BPXDatabase as bpx from Model import ModelLayer as m import datetime as dt from Model import QueryFile as qf Success = True Messages = [] try: config = m.GetConfig() DBObj = bpx.BPXDatabase(config['server'], config['database'], config['UID']) EDWObj = bpx.GetDBEnvironment('ProdEDW', 'OVERRIDE') wellname_list = df['WellName'].unique() wellname_list = list(wellname_list) if '' in wellname_list: wellname_list.remove('') count = 1 for name in wellname_list: monthly_df = df.query('WellName == @name') monthly_df = monthly_df.sort_values(by=['Date'], ascending=True) df_previous_row = (0, monthly_df.iloc[1]) nettingFactor = monthly_df['NettingFactor'].values[0] well_count = 1 header_corpid = '' for df_row in monthly_df.iterrows(): if well_count == 1: df_next_row = monthly_df.iloc[well_count] results = InterpolateDailyRatesFromMonthlyRates( CurrentMonthVal=df_row[1], NextMonthVal=df_next_row, GasRateField=Production_Column_Name) elif well_count != monthly_df.shape[0] and well_count != 1: df_next_row = monthly_df.iloc[well_count] results = InterpolateDailyRatesFromMonthlyRates( CurrentMonthVal=df_row[1], NextMonthVal=df_next_row, PreviousMonthVal=df_previous_row[1], GasRateField=Production_Column_Name) elif well_count == monthly_df.shape[0]: results = InterpolateDailyRatesFromMonthlyRates( CurrentMonthVal=df_row[1], PreviousMonthVal=df_previous_row[1], GasRateField=Production_Column_Name) for row in results.iterrows(): corpid_query = qf.EDWKeyQueryFromWellName([name]) corpid_results = EDWObj.Query(corpid_query) if not corpid_results[1].empty: CorpID = corpid_results[1].at[0, 'CorpID'] else: CorpID = name WellName = name Update_Date = dt.datetime.now().strftime( "%Y-%m-%d %H:%M:%S") Update_User = User if header_corpid != CorpID: #Create Header entry header_corpid = CorpID ForecastHeaderObj = m.ForecastHeaderRow( WellName, CorpID, ForecastName, ForecastYear, '', [], GFO, DBObj) Success, Message = ForecastHeaderObj.Write( Update_User, Update_Date) if not Success: Messages.append(Message) Date_Key = row[1]['Date'].strftime('%m/%d/%Y') Gas_Production = row[1]['GasProduction'] GasNF = row[1]['GasNF'] if Gas_Production >= 0 and Date_Key: ForecastDataObj = m.ForecastDataRow( ForecastName, CorpID, Date_Key, Gas_Production, 0, 0, GasNF, 0, 0, DBObj) Success, Message = ForecastDataObj.Write( Update_User, Update_Date) if not Success: Messages.append(Message) df_previous_row = df_row well_count = well_count + 1 callprogressbar(count, len(wellname_list)) count = count + 1 except Exception as ex: Success = False Messages.append('Error writing Forecast to Database. ' + str(ex)) return Success, Messages