def hh(data_source): ssp_rate_set = data_source.supplier_rate_sets['ssp-rate'] sbp_rate_set = data_source.supplier_rate_sets['sbp-rate'] try: system_price_cache = data_source.caches['system_price'] except KeyError: data_source.caches['system_price'] = {} system_price_cache = data_source.caches['system_price'] try: future_funcs = data_source.caches['future_funcs'] except KeyError: future_funcs = {} data_source.caches['future_funcs'] = future_funcs db_id = get_non_core_contract_id('system_price') try: future_funcs[db_id] except KeyError: future_funcs[db_id] = { 'start_date': None, 'func': create_future_func(1, 0)} for h in data_source.hh_data: try: sbp, ssp = system_price_cache[h['start-date']] except KeyError: db_id = get_non_core_contract_id('system_price') h_start = h['start-date'] rates = data_source.hh_rate(db_id, h_start, 'gbp_per_nbp_mwh') try: rdict = rates[key_format(h_start)] sbp = rdict['sbp'] / 1000 ssp = rdict['ssp'] / 1000 system_price_cache[h_start] = (sbp, ssp) except KeyError: raise BadRequest( "For the System Price rate script at " + hh_format(h_start) + " the rate cannot be found.") except TypeError: raise BadRequest( "For the System Price rate script at " + hh_format(h_start) + " the rate 'rates_gbp_per_mwh' has the problem: " + traceback.format_exc()) h['sbp'] = sbp h['sbp-gbp'] = h['nbp-kwh'] * sbp sbp_rate_set.add(sbp) h['ssp'] = ssp h['ssp-gbp'] = h['nbp-kwh'] * ssp ssp_rate_set.add(ssp)
def hh(data_source, run="RF"): try: bsuos_cache = data_source.caches["bsuos"][run] except KeyError: try: top_cache = data_source.caches["bsuos"] except KeyError: top_cache = data_source.caches["bsuos"] = {} try: bsuos_cache = top_cache[run] except KeyError: bsuos_cache = top_cache[run] = {} for h in data_source.hh_data: try: h["bsuos-rate"] = bsuos_rate = bsuos_cache[h["start-date"]] except KeyError: h_start = h["start-date"] db_id = get_non_core_contract_id("bsuos") rates = data_source.hh_rate(db_id, h_start)["rates_gbp_per_mwh"] try: bsuos_price_dict = rates[key_format(h_start)] bsuos_price = _find_price(run, bsuos_price_dict) except KeyError: ds = rates.values() bsuos_price = sum(_find_price(run, d) for d in ds) / len(ds) h["bsuos-rate"] = bsuos_rate = bsuos_cache[h_start] = ( float(bsuos_price) / 1000) h["bsuos-kwh"] = h["nbp-kwh"] h["bsuos-gbp"] = h["nbp-kwh"] * bsuos_rate
def hh(data_source): try: cache = data_source.caches["rcrc"] except KeyError: cache = data_source.caches["rcrc"] = {} for hh in data_source.hh_data: try: hh["rcrc-rate"] = rcrc = cache[hh["start-date"]] except KeyError: h_start = hh["start-date"] db_id = get_non_core_contract_id("rcrc") rates = data_source.hh_rate(db_id, h_start)["rates"] try: hh["rcrc-rate"] = rcrc = cache[h_start] = ( float(rates[key_format(h_start)]) / 1000 ) except KeyError: try: dt = h_start - relativedelta(days=3) hh["rcrc-rate"] = rcrc = cache[h_start] = ( float(rates[key_format(dt)]) / 1000 ) except KeyError: raise BadRequest( "For the RCRC rate script at " + hh_format(dt) + " the rate cannot be found." ) hh["rcrc-kwh"] = hh["nbp-kwh"] hh["rcrc-gbp"] = hh["nbp-kwh"] * rcrc
def hh(data_source): bill = data_source.supplier_bill try: cache = data_source.caches['rcrc'] except KeyError: cache = {} data_source.caches['rcrc'] = cache rate_set = data_source.supplier_rate_sets['rcrc-rate'] for hh in data_source.hh_data: try: hh['rcrc-gbp-per-kwh'] = rcrc = cache[hh['start-date']] except KeyError: h_start = hh['start-date'] db_id = get_non_core_contract_id('rcrc') rates = data_source.hh_rate(db_id, h_start)['rates'] try: hh['rcrc-gbp-per-kwh'] = rcrc = cache[h_start] = float( rates[key_format(h_start)]) / 1000 except KeyError: try: dt = h_start - relativedelta(days=3) hh['rcrc-gbp-per-kwh'] = rcrc = cache[h_start] = float( rates[key_format(dt)]) / 1000 except KeyError: raise BadRequest("For the RCRC rate script at " + hh_format(dt) + " the rate cannot be found.") rate_set.add(rcrc) bill['rcrc-kwh'] += hh['nbp-kwh'] bill['rcrc-gbp'] += hh['nbp-kwh'] * rcrc
def hh(data_source): bill = data_source.supplier_bill try: cache = data_source.caches['rcrc'] except KeyError: cache = {} data_source.caches['rcrc'] = cache rate_set = data_source.supplier_rate_sets['rcrc-rate'] for hh in data_source.hh_data: try: hh['rcrc-gbp-per-kwh'] = rcrc = cache[hh['start-date']] except KeyError: h_start = hh['start-date'] db_id = get_non_core_contract_id('rcrc') rates = data_source.hh_rate(db_id, h_start, 'rates') try: hh['rcrc-gbp-per-kwh'] = rcrc = cache[h_start] = \ rates[h_start.strftime("%d %H:%M Z")] / 1000 except KeyError: raise BadRequest( "For the RCRC rate script at " + hh_format(h_start) + " the rate cannot be found.") rate_set.add(rcrc) bill['rcrc-kwh'] += hh['nbp-kwh'] bill['rcrc-gbp'] += hh['nbp-kwh'] * rcrc
def hh(data_source): rate_set = data_source.supplier_rate_sets['bsuos-rate'] bill = data_source.supplier_bill try: bsuos_cache = data_source.caches['bsuos'] except KeyError: data_source.caches['bsuos'] = {} bsuos_cache = data_source.caches['bsuos'] try: future_funcs = data_source.caches['future_funcs'] except KeyError: future_funcs = {} data_source.caches['future_funcs'] = future_funcs db_id = get_non_core_contract_id('bsuos') try: future_funcs[db_id] except KeyError: future_funcs[db_id] = { 'start_date': None, 'func': create_future_func(1, 0)} for h in data_source.hh_data: try: h['bsuos-gbp-per-kwh'] = bsuos_rate = bsuos_cache[h['start-date']] except KeyError: h_start = h['start-date'] db_id = get_non_core_contract_id('bsuos') rates = data_source.hh_rate(db_id, h_start, 'rates_gbp_per_mwh') try: h['bsuos-gbp-per-kwh'] = bsuos_rate = bsuos_cache[h_start] = \ float(rates[h_start.strftime("%d %H:%M Z")]) / 1000 except KeyError: raise BadRequest( "For the BSUoS rate script at " + hh_format(h_start) + " the rate cannot be found.") except TypeError as e: raise BadRequest( "For the BSUoS rate script at " + hh_format(h_start) + " the rate 'rates_gbp_per_mwh' has the problem: " + str(e)) bill['bsuos-kwh'] += h['nbp-kwh'] h['bsuos-gbp'] = h['nbp-kwh'] * bsuos_rate bill['bsuos-gbp'] += h['bsuos-gbp'] rate_set.add(bsuos_rate)
def hh(data_source): rate_set = data_source.supplier_rate_sets['tlm'] try: cache = data_source.caches['tlms'] except KeyError: cache = {} data_source.caches['tlms'] = cache try: future_funcs = data_source.caches['future_funcs'] except KeyError: future_funcs = {} data_source.caches['future_funcs'] = future_funcs db_id = get_non_core_contract_id('tlms') try: future_funcs[db_id] except KeyError: future_funcs[db_id] = {'start_date': None, 'func': tlms_future} for h in data_source.hh_data: try: h['tlm'] = tlm = cache[h['start-date']] except KeyError: h_start = h['start-date'] db_id = get_non_core_contract_id('tlms') rates = data_source.hh_rate(db_id, h_start, 'tlms') try: h['tlm'] = tlm = cache[h_start] = \ rates[h_start.strftime("%d %H:%M Z")] except KeyError: raise BadRequest( "For the TLMs rate script at " + hh_format(h_start) + " the rate cannot be found.") except TypeError as e: raise BadRequest( "For the TLMs rate script at " + hh_format(h_start) + " the rate 'tlms' has the problem: " + str(e)) rate_set.add(tlm) h['nbp-kwh'] = h['gsp-kwh'] * tlm
def hh(data_source, run="DF"): gsp_group_code = data_source.gsp_group_code use_runs = RUNS[RUNS.index(run):] try: cache = data_source.caches["tlms"] except KeyError: cache = data_source.caches["tlms"] = {} for h in data_source.hh_data: try: h["tlm"] = tlm = cache[h["start-date"]][gsp_group_code][run] except KeyError: h_start = h["start-date"] db_id = get_non_core_contract_id("tlms") rates = data_source.hh_rate(db_id, h_start)["tlms"] key = key_format(h_start) try: rate = rates[key] except KeyError: rate = sorted(rates.items())[-1][1] try: gsp_rate = rate[gsp_group_code] except KeyError: gsp_rate = sorted(rate.items())[-1][1] tlm = None for use_run in use_runs: try: h["tlm"] = tlm = float(gsp_rate[use_run]["off_taking"]) except KeyError: pass if tlm is None: h["tlm"] = tlm = float( sorted(gsp_rate.items())[-1][1]["off_taking"]) try: rates_cache = cache[h["start-date"]] except KeyError: rates_cache = cache[h["start-date"]] = {} try: gsp_cache = rates_cache[gsp_group_code] except KeyError: gsp_cache = rates_cache[gsp_group_code] = {} gsp_cache[run] = tlm h["nbp-kwh"] = h["gsp-kwh"] * tlm
def hh(data_source, run='DF'): rate_set = data_source.supplier_rate_sets['tlm'] gsp_group_code = data_source.gsp_group_code try: cache = data_source.caches['tlms'] except KeyError: cache = data_source.caches['tlms'] = {} for h in data_source.hh_data: try: h['tlm'] = tlm = cache[h['start-date']][gsp_group_code][run] except KeyError: h_start = h['start-date'] db_id = get_non_core_contract_id('tlms') rates = data_source.hh_rate(db_id, h_start)['tlms'] key = key_format(h_start) try: rate = rates[key] except KeyError: rate = sorted(rates.items())[-1][1] try: gsp_rate = rate[gsp_group_code] except KeyError: gsp_rate = sorted(rate.items())[-1][1] try: h['tlm'] = tlm = float(gsp_rate[run]['off_taking']) except KeyError: h['tlm'] = tlm = float( sorted(gsp_rate.items())[-1][1]['off_taking']) try: rates_cache = cache[h['start-date']] except KeyError: rates_cache = cache[h['start-date']] = {} try: gsp_cache = rates_cache[gsp_group_code] except KeyError: gsp_cache = rates_cache[gsp_group_code] = {} gsp_cache[run] = tlm rate_set.add(tlm) h['nbp-kwh'] = h['gsp-kwh'] * tlm
def hh(data_source, run='RF'): rate_set = data_source.supplier_rate_sets['bsuos-rate'] try: bsuos_cache = data_source.caches['bsuos'][run] except KeyError: try: top_cache = data_source.caches['bsuos'] except KeyError: top_cache = data_source.caches['bsuos'] = {} try: bsuos_cache = top_cache[run] except KeyError: bsuos_cache = top_cache[run] = {} for h in data_source.hh_data: try: h['bsuos-gbp-per-kwh'] = bsuos_rate = bsuos_cache[h['start-date']] except KeyError: h_start = h['start-date'] db_id = get_non_core_contract_id('bsuos') rates = data_source.hh_rate(db_id, h_start)['rates_gbp_per_mwh'] try: bsuos_prices = rates[key_format(h_start)] except KeyError: bsuos_prices = sorted(rates.items())[-1][1] try: bsuos_price = bsuos_prices[run] except KeyError: try: bsuos_price = bsuos_prices['RF'] except KeyError: try: bsuos_price = bsuos_prices['SF'] except KeyError: bsuos_price = bsuos_prices['II'] h['bsuos-gbp-per-kwh'] = bsuos_rate = bsuos_cache[h_start] = float( bsuos_price) / 1000 h['bsuos-gbp'] = h['nbp-kwh'] * bsuos_rate rate_set.add(bsuos_rate)
def _generator(sess2, hh_date): try: return datum_cache[hh_date] except KeyError: ct_tz = pytz.timezone('Europe/London') ct_dt = ct_tz.normalize(hh_date.astimezone(ct_tz)) utc_is_month_end = (hh_date + HH).day == 1 and hh_date.day != 1 ct_is_month_end = (ct_dt + HH).day == 1 and ct_dt.day != 1 utc_decimal_hour = hh_date.hour + hh_date.minute / 60 ct_decimal_hour = ct_dt.hour + ct_dt.minute / 60 utc_bank_holidays = chellow.computer.hh_rate( sess2, caches, chellow.bank_holidays.db_id, hh_date, 'bank_holidays') if utc_bank_holidays is None: raise BadRequest( "\nCan't find bank holidays for " + str(hh_date)) utc_bank_holidays = utc_bank_holidays[:] for i in range(len(utc_bank_holidays)): utc_bank_holidays[i] = utc_bank_holidays[i][5:] utc_is_bank_holiday = hh_date.strftime("%m-%d") in \ utc_bank_holidays g_cv_id = get_non_core_contract_id('g_cv') cv = chellow.computer.ion_rs(sess2, caches, g_cv_id, hh_date)[ hh_date.day-1]['SW'] hh = { 'status': 'E', 'hist-start': hh_date - relativedelta(years=years_back), 'start-date': hh_date, 'ct-day': ct_dt.day, 'utc-month': hh_date.month, 'utc-day': hh_date.day, 'utc-decimal-hour': utc_decimal_hour, 'utc-year': hh_date.year, 'utc-hour': hh_date.hour, 'utc-minute': hh_date.minute, 'ct-year': ct_dt.year, 'ct-month': ct_dt.month, 'ct-decimal-hour': ct_decimal_hour, 'ct-day-of-week': ct_dt.weekday(), 'utc-day-of-week': hh_date.weekday(), 'utc-is-bank-holiday': utc_is_bank_holiday, 'utc-is-month-end': utc_is_month_end, 'ct-is-month-end': ct_is_month_end, 'cv': cv} datum_cache[hh_date] = MappingProxyType(hh) return datum_cache[hh_date]
def hh(data_source): ssp_rate_set = data_source.supplier_rate_sets['ssp-rate'] sbp_rate_set = data_source.supplier_rate_sets['sbp-rate'] for h in data_source.hh_data: try: sbp, ssp = data_source.caches['system_price'][h['start-date']] except KeyError: try: system_price_cache = data_source.caches['system_price'] except KeyError: system_price_cache = data_source.caches['system_price'] = {} db_id = get_non_core_contract_id('system_price') h_start = h['start-date'] rates = data_source.hh_rate(db_id, h_start)['gbp_per_nbp_mwh'] try: try: rdict = rates[key_format(h_start)] except KeyError: rdict = rates[key_format(h_start - Timedelta(days=3))] sbp = float(rdict['sbp'] / 1000) ssp = float(rdict['ssp'] / 1000) system_price_cache[h_start] = (sbp, ssp) except KeyError: raise BadRequest("For the System Price rate script at " + hh_format(h_start) + " the rate cannot be found.") except TypeError: raise BadRequest( "For the System Price rate script at " + hh_format(h_start) + " the rate 'rates_gbp_per_mwh' has the problem: " + traceback.format_exc()) h['sbp'] = sbp h['sbp-gbp'] = h['nbp-kwh'] * sbp sbp_rate_set.add(sbp) h['ssp'] = ssp h['ssp-gbp'] = h['nbp-kwh'] * ssp ssp_rate_set.add(ssp)
def hh(data_source, provider="APXMIDP"): try: cache = data_source.caches["bmarketidx"] except KeyError: cache = data_source.caches["bmarketidx"] = {} for hh in data_source.hh_data: try: hh["bmarketidx-rate"] = cache[hh["start-date"]][provider] except KeyError: h_start = hh["start-date"] db_id = get_non_core_contract_id("bmarketidx") rates = data_source.hh_rate(db_id, h_start)["rates"] try: idxs = cache[h_start] except KeyError: idxs = cache[h_start] = {} try: rate = rates[h_start] except KeyError: try: rate = sorted(rates.items())[-1][1] except KeyError: raise BadRequest( f"For the bmarketidx rate script at " f"{hh_format(h_start)} the rate cannot be found." ) try: idx = rate[provider] except KeyError: try: idx = sorted(rate.items())[0][1] except KeyError: raise BadRequest( f"For the bmarketidx rate script at " f"{hh_format(h_start)} a rate cannot be found for the " f"provider {provider}." ) hh["bmarketidx-rate"] = idxs[provider] = float(idx)
def hh(data_source): for h in data_source.hh_data: try: sbp, ssp = data_source.caches["system_price"][h["start-date"]] except KeyError: try: system_price_cache = data_source.caches["system_price"] except KeyError: system_price_cache = data_source.caches["system_price"] = {} db_id = get_non_core_contract_id("system_price") h_start = h["start-date"] rates = data_source.hh_rate(db_id, h_start)["gbp_per_nbp_mwh"] try: try: rdict = rates[key_format(h_start)] except KeyError: rdict = rates[key_format(h_start - Timedelta(days=3))] sbp = float(rdict["sbp"] / 1000) ssp = float(rdict["ssp"] / 1000) system_price_cache[h_start] = (sbp, ssp) except KeyError: raise BadRequest( "For the System Price rate script at " + hh_format(h_start) + " the rate cannot be found." ) except TypeError: raise BadRequest( "For the System Price rate script at " + hh_format(h_start) + " the rate 'rates_gbp_per_mwh' has the problem: " + traceback.format_exc() ) h["sbp"] = sbp h["sbp-gbp"] = h["nbp-kwh"] * sbp h["ssp"] = ssp h["ssp-gbp"] = h["nbp-kwh"] * ssp
def datum_range(sess, caches, years_back, start_date, finish_date): try: return caches['g_engine']['datum'][years_back, start_date, finish_date] except KeyError: try: g_engine_cache = caches['g_engine'] except KeyError: caches['g_engine'] = g_engine_cache = {} try: d_cache = g_engine_cache['datum'] except KeyError: g_engine_cache['datum'] = d_cache = {} datum_list = [] g_cv_id = get_non_core_contract_id('g_cv') for dt in hh_range(start_date, finish_date): hist_date = dt - relativedelta(years=years_back) ct_dt = to_ct(dt) utc_is_month_end = (dt + HH).day == 1 and dt.day != 1 ct_is_month_end = (ct_dt + HH).day == 1 and ct_dt.day != 1 utc_decimal_hour = dt.hour + dt.minute / 60 ct_decimal_hour = ct_dt.hour + ct_dt.minute / 60 bhs = chellow.computer.hh_rate( sess, caches, chellow.bank_holidays.get_db_id(), dt, 'bank_holidays') if bhs is None: raise BadRequest( "Can't find bank holidays for " + str(dt)) bank_holidays = [b[5:] for b in bhs] utc_is_bank_holiday = dt.strftime("%m-%d") in bank_holidays ct_is_bank_holiday = ct_dt.strftime("%m-%d") in bank_holidays cv = float( chellow.computer.ion_rs( sess, caches, g_cv_id, hist_date)[hist_date.day - 1]['SW']) / 3.6 datum_list.append( MappingProxyType( { 'hist_start': hist_date, 'start_date': dt, 'ct_day': ct_dt.day, 'utc_month': dt.month, 'utc_day': dt.day, 'utc_decimal_hour': utc_decimal_hour, 'utc_year': dt.year, 'utc_hour': dt.hour, 'utc_minute': dt.minute, 'ct_year': ct_dt.year, 'ct_month': ct_dt.month, 'ct_decimal_hour': ct_decimal_hour, 'ct_day_of_week': ct_dt.weekday(), 'utc_day_of_week': dt.weekday(), 'utc_is_bank_holiday': utc_is_bank_holiday, 'ct_is_bank_holiday': ct_is_bank_holiday, 'utc_is_month_end': utc_is_month_end, 'ct_is_month_end': ct_is_month_end, 'status': 'X', 'kwh': 0, 'hist_kwh': 0, 'cv': cv, 'correction_factor': CORRECTION_FACTOR, 'units_code': 'M3', 'units_factor': 1, 'units_consumed': 0})) datum_tuple = tuple(datum_list) d_cache[years_back, start_date, finish_date] = datum_tuple return datum_tuple
def __init__(self, sess, start_date, finish_date, forecast_date, g_era, caches, g_bill): self.sess = sess self.caches = caches self.forecast_date = forecast_date self.start_date = start_date self.finish_date = finish_date times = get_times(sess, caches, start_date, finish_date, forecast_date) self.years_back = times['years-back'] self.history_start = times['history-start'] self.history_finish = times['history-finish'] self.problem = '' self.bill = defaultdict(int, {'problem': ''}) self.hh_data = [] self.rate_sets = defaultdict(set) self.g_bill = g_bill if self.g_bill is not None: self.g_bill_start = g_bill.start_date self.g_bill_finish = g_bill.finish_date self.is_last_g_bill_gen = \ not self.g_bill_finish < self.start_date and not \ self.g_bill_finish > self.finish_date self.g_era = g_era self.g_supply = g_era.g_supply self.mprn = self.g_supply.mprn self.g_exit_zone_code = self.g_supply.g_exit_zone.code self.g_ldz_code = self.g_supply.g_exit_zone.g_ldz.code self.g_dn_code = self.g_supply.g_exit_zone.g_ldz.g_dn.code self.account = g_era.account self.g_contract = g_era.g_contract self.consumption_info = '' if self.years_back == 0: hist_g_eras = [self.g_era] else: hist_g_eras = sess.query(GEra).filter( GEra.g_supply == self.g_supply, GEra.start_date <= self.history_finish, or_(GEra.finish_date == null(), GEra.finish_date >= self.history_start)).order_by( GEra.start_date).all() if len(hist_g_eras) == 0: hist_g_eras = sess.query(GEra).filter( GEra.g_supply == self.g_supply).order_by( GEra.start_date).limit(1).all() g_cv_id = get_non_core_contract_id('g_cv') hist_map = {} for i, hist_g_era in enumerate(hist_g_eras): if self.history_start > hist_g_era.start_date: chunk_start = self.history_start else: if i == 0: chunk_start = self.history_start else: chunk_start = hist_g_era.start_date chunk_finish = hh_min(hist_g_era.finish_date, self.history_finish) if self.g_bill is None: read_list = [] read_keys = set() pairs = [] prior_pres_g_reads = iter( sess.query(GRegisterRead).join(GBill).join(BillType).join( GRegisterRead.pres_type).filter( GReadType.code.in_(ACTUAL_READ_TYPES), GBill.g_supply == self.g_supply, GRegisterRead.pres_date < chunk_start, BillType.code != 'W').order_by( GRegisterRead.pres_date.desc())) prior_prev_g_reads = iter( sess.query(GRegisterRead).join(GBill).join(BillType).join( GRegisterRead.prev_type).filter( GReadType.code.in_(ACTUAL_READ_TYPES), GBill.g_supply == self.g_supply, GRegisterRead.prev_date < chunk_start, BillType.code != 'W').order_by( GRegisterRead.prev_date.desc())) next_pres_g_reads = iter( sess.query(GRegisterRead).join(GBill).join(BillType).join( GRegisterRead.pres_type).filter( GReadType.code.in_(ACTUAL_READ_TYPES), GBill.g_supply == self.g_supply, GRegisterRead.pres_date >= chunk_start, BillType.code != 'W').order_by( GRegisterRead.pres_date)) next_prev_g_reads = iter( sess.query(GRegisterRead).join(GBill).join(BillType).join( GRegisterRead.prev_type).filter( GReadType.code.in_(ACTUAL_READ_TYPES), GBill.g_supply == self.g_supply, GRegisterRead.prev_date >= chunk_start, BillType.code != 'W').order_by( GRegisterRead.prev_date)) for is_forwards in (False, True): if is_forwards: pres_g_reads = next_pres_g_reads prev_g_reads = next_prev_g_reads read_list.reverse() else: pres_g_reads = prior_pres_g_reads prev_g_reads = prior_prev_g_reads prime_pres_g_read = None prime_prev_g_read = None while True: while prime_pres_g_read is None: try: pres_g_read = next(pres_g_reads) except StopIteration: break pres_date = pres_g_read.pres_date pres_msn = pres_g_read.msn read_key = '_'.join([str(pres_date), pres_msn]) if read_key in read_keys: continue pres_g_bill = sess.query(GBill).join( BillType).filter( GBill.g_supply == self.g_supply, GBill.finish_date >= pres_g_read.g_bill.start_date, GBill.start_date <= pres_g_read.g_bill.finish_date, BillType.code != 'W').order_by( GBill.issue_date.desc(), BillType.code).first() if pres_g_bill != pres_g_read.g_bill: continue value = sess.query( cast(GRegisterRead.pres_value, Float)).filter( GRegisterRead.g_bill == pres_g_bill, GRegisterRead.pres_date == pres_date, GRegisterRead.msn == pres_msn).scalar() prime_pres_g_read = { 'date': pres_date, 'value': value, 'msn': pres_msn } read_keys.add(read_key) while prime_prev_g_read is None: try: prev_g_read = next(prev_g_reads) except StopIteration: break prev_date = prev_g_read.prev_date prev_msn = prev_g_read.msn read_key = '_'.join([str(prev_date), prev_msn]) if read_key in read_keys: continue prev_g_bill = sess.query(GBill).join( BillType).filter( GBill.g_supply == self.g_supply, GBill.finish_date >= prev_g_read.g_bill.start_date, GBill.start_date <= prev_g_read.g_bill.finish_date, BillType.code != 'W').order_by( GBill.issue_date.desc(), BillType.code).first() if prev_g_bill != prev_g_read.g_bill: continue value = sess.query( cast(GRegisterRead.prev_value, Float)).filter( GRegisterRead.g_bill == prev_g_bill, GRegisterRead.prev_date == prev_date, GRegisterRead.msn == prev_msn).scalar() prime_prev_g_read = { 'date': prev_date, 'value': value, 'msn': prev_msn } read_keys.add(read_key) if prime_pres_g_read is None and \ prime_prev_g_read is None: break elif prime_pres_g_read is None: read_list.append(prime_prev_g_read) prime_prev_g_read = None elif prime_prev_g_read is None: read_list.append(prime_pres_g_read) prime_pres_g_read = None else: if is_forwards: if prime_prev_g_read['date'] == \ prime_pres_g_read['date'] or \ prime_pres_g_read['date'] < \ prime_prev_g_read['date']: read_list.append(prime_pres_g_read) prime_pres_g_read = None else: read_list.append(prime_prev_g_read) prime_prev_g_read = None else: if prime_prev_g_read['date'] == \ prime_pres_g_read['date'] or \ prime_prev_g_read['date'] > \ prime_pres_g_read['date']: read_list.append(prime_prev_g_read) prime_prev_g_read = None else: read_list.append(prime_pres_g_read) prime_pres_g_read = None if len(read_list) > 1: if is_forwards: aft_read = read_list[-2] fore_read = read_list[-1] else: aft_read = read_list[-1] fore_read = read_list[-2] if aft_read['msn'] == fore_read['msn']: num_hh = (fore_read['date'] - aft_read['date'] ).total_seconds() / (30 * 60) units = fore_read['value'] - aft_read['value'] if units < 0: digits = int(math.log10( aft_read['value'])) + 1 units = 10**digits + units pairs.append({ 'start-date': aft_read['date'], 'units': units / num_hh }) if not is_forwards or ( is_forwards and read_list[-1]['date'] > chunk_finish): break self.consumption_info += 'read list - \n' + dumps(read_list) \ + "\n" hhs = _find_hhs(sess, caches, hist_g_era, pairs, chunk_start, chunk_finish, g_cv_id, self.g_ldz_code) hist_map.update(hhs) self.consumption_info += 'pairs - \n' + dumps(pairs) else: g_bills = [] for cand_bill in sess.query(GBill).join(GBatch) \ .join(BillType).filter( GBill.g_supply == self.g_supply, GBill.g_reads.any(), GBatch.g_contract == self.g_contract, GBill.start_date <= chunk_finish, GBill.finish_date >= chunk_start, BillType.code != 'W').order_by( GBill.issue_date.desc(), GBill.start_date): can_insert = True for g_bill in g_bills: if not cand_bill.start_date > g_bill.finish_date \ and not cand_bill.finish_date < \ g_bill.start_date: can_insert = False break if can_insert: g_bills.append(cand_bill) for g_bill in g_bills: units_consumed = 0 for prev_value, pres_value in sess.query( cast(GRegisterRead.prev_value, Float), cast( GRegisterRead.pres_value, Float)).filter(GRegisterRead.g_bill == g_bill): units_diff = pres_value - prev_value if units_diff < 0: total_units = 10**len(str(int(prev_value))) c_units = total_units - prev_value + pres_value if c_units < abs(units_diff): units_diff = c_units units_consumed += units_diff bill_s = (g_bill.finish_date - g_bill.start_date + timedelta(minutes=30)).total_seconds() hh_units_consumed = units_consumed / (bill_s / (60 * 30)) cf = float(hist_g_era.correction_factor) g_unit = hist_g_era.g_unit unit_code, unit_factor = g_unit.code, float(g_unit.factor) for hh_date in hh_range(caches, g_bill.start_date, g_bill.finish_date): cv, avg_cv = find_cv(sess, caches, g_cv_id, hh_date, self.g_ldz_code) hist_map[hh_date] = { 'unit_code': unit_code, 'unit_factor': unit_factor, 'units_consumed': hh_units_consumed, 'correction_factor': cf, 'calorific_value': cv, 'avg_cv': avg_cv } for d in datum_range(sess, self.caches, self.years_back, start_date, finish_date): h = d.copy() hist_start = h['hist_start'] h.update(hist_map.get(hist_start, {})) h['kwh'] = h['units_consumed'] * h['unit_factor'] * \ h['correction_factor'] * h['calorific_value'] / 3.6 h['kwh_avg'] = h['units_consumed'] * h['unit_factor'] * \ h['correction_factor'] * h['avg_cv'] / 3.6 h['ug_rate'] = float( get_file_rates( self.caches, 'g_ug', h['start_date'])['ug_gbp_per_kwh'][self.g_exit_zone_code]) self.hh_data.append(h)
def __init__( self, sess, start_date, finish_date, forecast_date, g_era, caches, g_bill ): self.sess = sess self.caches = caches self.forecast_date = forecast_date self.start_date = start_date self.finish_date = finish_date self.bill_hhs = {} times = get_times(sess, caches, start_date, finish_date, forecast_date) self.years_back = times["years-back"] self.history_start = times["history-start"] self.history_finish = times["history-finish"] self.problem = "" self.bill = defaultdict(int, {"problem": ""}) self.hh_data = [] self.rate_sets = defaultdict(set) self.g_bill = g_bill if self.g_bill is not None: self.g_bill_start = g_bill.start_date self.g_bill_finish = g_bill.finish_date self.is_last_g_bill_gen = ( not self.g_bill_finish < self.start_date and not self.g_bill_finish > self.finish_date ) self.g_era = g_era self.g_supply = g_era.g_supply self.mprn = self.g_supply.mprn self.g_exit_zone_code = self.g_supply.g_exit_zone.code self.g_ldz_code = self.g_supply.g_exit_zone.g_ldz.code self.g_dn_code = self.g_supply.g_exit_zone.g_ldz.g_dn.code self.account = g_era.account self.g_reading_frequency = g_era.g_reading_frequency self.g_reading_frequency_code = self.g_reading_frequency.code self.g_contract = g_era.g_contract self.consumption_info = "" if self.years_back == 0: hist_g_eras = [self.g_era] else: hist_g_eras = ( sess.query(GEra) .filter( GEra.g_supply == self.g_supply, GEra.start_date <= self.history_finish, or_( GEra.finish_date == null(), GEra.finish_date >= self.history_start, ), ) .order_by(GEra.start_date) .all() ) if len(hist_g_eras) == 0: hist_g_eras = ( sess.query(GEra) .filter(GEra.g_supply == self.g_supply) .order_by(GEra.start_date) .limit(1) .all() ) g_cv_id = get_non_core_contract_id("g_cv") hist_map = {} for i, hist_g_era in enumerate(hist_g_eras): if self.history_start > hist_g_era.start_date: chunk_start = self.history_start else: if i == 0: chunk_start = self.history_start else: chunk_start = hist_g_era.start_date chunk_finish = hh_min(hist_g_era.finish_date, self.history_finish) if self.g_bill is None: self.consumption_info += _no_bill_kwh( sess, caches, self.g_supply, chunk_start, chunk_finish, hist_g_era, g_cv_id, self.g_ldz_code, hist_map, forecast_date, ) else: _bill_kwh( sess, self.caches, self.g_supply, hist_g_era, chunk_start, chunk_finish, g_cv_id, hist_map, self.g_ldz_code, ) for d in datum_range( sess, self.caches, self.years_back, start_date, finish_date ): h = d.copy() hist_start = h["hist_start"] h.update(hist_map.get(hist_start, {})) h["kwh"] = ( h["units_consumed"] * h["unit_factor"] * h["correction_factor"] * h["calorific_value"] / 3.6 ) h["kwh_avg"] = ( h["units_consumed"] * h["unit_factor"] * h["correction_factor"] * h["avg_cv"] / 3.6 ) h["ug_rate"] = float( get_file_rates(self.caches, "g_ug", h["start_date"])["ug_gbp_per_kwh"][ self.g_exit_zone_code ] ) self.hh_data.append(h) self.bill_hhs[d["start_date"]] = {}