def hh_time_2010_04_01(ds, hh): bill = ds.supplier_bill dno_cache = ds.caches['dno'][ds.dno_contract.name] try: tariff_bands_cache = dno_cache['tariff_bands'] except KeyError: dno_cache['tariff_bands'] = {} tariff_bands_cache = dno_cache['tariff_bands'] try: tariff_bands = tariff_bands_cache[ds.llfc_code] except KeyError: tariff_bands_cache[ds.llfc_code] = {} tariff_bands = tariff_bands_cache[ds.llfc_code] try: tariff, band = tariff_bands[hh['start-date']] except KeyError: tariff = None for llfcs, tf in ds.hh_rate( ds.dno_contract.id, hh['start-date'], 'tariffs').iteritems(): if ds.llfc_code in [cd.strip() for cd in llfcs.split(',')]: tariff = tf break if tariff is None: raise UserException( "For the DNO " + ds.dno_code + " and timestamp " + hh_format(hh['start-date']) + " the LLFC '" + ds.llfc_code + "' can't be found in the 'tariffs' section.") band = 'green' if ds.dno_code == '14': if hh['ct-day-of-week'] < 5: if 16 <= hh['ct-decimal-hour'] < 19: band = 'red' elif 7 < hh['ct-decimal-hour'] < 21: band = 'amber' elif ds.dno_code == '20': if hh['ct-day-of-week'] < 5: if 16 < hh['ct-decimal-hour'] < 19: band = 'red' elif 9 <= hh['ct-decimal-hour'] <= 20: band = 'amber' elif ds.dno_code == '22': if hh['ct-day-of-week'] > 4: if 16 < hh['ct-decimal-hour'] <= 19: band = 'amber' else: if 17 <= hh['ct-decimal-hour'] < 19: band = 'red' elif 7 < hh['ct-decimal-hour'] <= 21: band = 'amber' else: raise UserException("DNO code not recognized.") tariff_bands[hh['start-date']] = (tariff, band) if hh['ct-decimal-hour'] == 23.5 and not ds.is_displaced: bill['duos-fixed-days'] += 1 rate = tariff['gbp-per-mpan-per-day'] ds.supplier_rate_sets['duos-fixed-rate'].add(rate) bill['duos-fixed-gbp'] += rate bill['duos-availability-days'] += 1 kva = ds.sc ds.supplier_rate_sets['duos-availability-kva'].add(kva) rate = tariff['gbp-per-kva-per-day'] ds.supplier_rate_sets['duos-availability-rate'].add(rate) bill['duos-availability-gbp'] += rate * kva if hh['ct-is-month-end'] and not ds.is_displaced: month_to = hh['start-date'] month_from = month_to - relativedelta(months=1) + HH md_kva = 0 days_in_month = 0 for dsc in computer.get_data_sources(ds, month_from, month_to): for datum in dsc.hh_data: md_kva = max( md_kva, ( datum['msp-kw'] ** 2 + max( datum['imp-msp-kvar'], datum['exp-msp-kvar']) ** 2) ** 0.5) if datum['utc-decimal-hour'] == 0: days_in_month += 1 excess_kva = max(md_kva - ds.sc, 0) if 'excess-gbp-per-kva-per-day' in tariff: rate = tariff['excess-gbp-per-kva-per-day'] ds.supplier_rate_sets['duos-excess-availability-kva'].add( excess_kva) rate = tariff['excess-gbp-per-kva-per-day'] ds.supplier_rate_sets['duos-excess-availability-rate'].add(rate) bill['duos-excess-availability-days'] += days_in_month bill['duos-excess-availability-gbp'] += rate * excess_kva * \ days_in_month
def triad_bill(data_source, rate_period='monthly'): bill = data_source.supplier_bill for hh in (h for h in data_source.hh_data if h['utc-is-month-end']): month_finish = hh['start-date'] month_start = month_finish + HH - relativedelta(months=1) month_num = month_start.month # Get start of last financial year financial_year_start = month_start while financial_year_start.month != 4: financial_year_start -= relativedelta(months=1) last_financial_year_start = financial_year_start - \ relativedelta(years=1) financial_year_finish = financial_year_start + \ relativedelta(years=1) - HH triad_dates = [] earliest_triad = None for dt in data_source.hh_rate( db_id, last_financial_year_start, 'triad_dates'): triad_dates.append(dt + relativedelta(years=1)) if earliest_triad is None or dt < earliest_triad: earliest_triad = dt est_triad_kws = [] for t_date in triad_dates: for ds in computer.get_data_sources( data_source, t_date, t_date, financial_year_start): duos.duos_vb(ds) est_triad_kws.append(ds.hh_data[0]) if data_source.site is None: era = data_source.supply.find_era_at( data_source.sess, earliest_triad) if era is None or era.get_channel( data_source.sess, data_source.is_import, 'ACTIVE') is None: est_triad_kw = 0.85 * max( datum['msp-kwh'] for datum in data_source.hh_data) * 2 for est_datum in est_triad_kws: est_datum['msp-kw'] = est_triad_kw est_datum['gsp-kw'] = est_datum['msp-kw'] * \ est_datum['laf'] triad_calc( bill, 'triad-estimate', est_triad_kws, financial_year_start, financial_year_finish, data_source, month_start) est_triad_gbp = bill['triad-estimate-rate'] * \ bill['triad-estimate-gsp-kw'] if rate_period == 'monthly': total_intervals = 12 est_intervals = 1 bill['triad-estimate-months'] = est_intervals else: dt = financial_year_start total_intervals = 0 while dt <= financial_year_finish: total_intervals += 1 dt += relativedelta(days=1) est_intervals = 0 for ds in computer.get_data_sources( data_source, month_start, month_finish): for h in ds.hh_data: if h['utc-decimal-hour'] == 0: est_intervals += 1 bill['triad-estimate-days'] = est_intervals bill['triad-estimate-gbp'] = est_triad_gbp / total_intervals * \ est_intervals if month_num == 3: triad_kws = [] for t_date in data_source.hh_rate( db_id, month_start, 'triad_dates'): try: ds = iter( computer.get_data_sources( data_source, t_date, t_date)).next() if data_source.supplier_contract is None or \ ds.supplier_contract == \ data_source.supplier_contract: duos.duos_vb(ds) thh = ds.hh_data[0] else: thh = { 'hist-start': t_date, 'msp-kw': 0, 'status': 'before contract', 'laf': 'before contract', 'gsp-kw': 0} except StopIteration: thh = { 'hist-start': t_date, 'msp-kw': 0, 'status': 'before start of supply', 'laf': 'before start of supply', 'gsp-kw': 0} triad_kws.append(thh) triad_calc( bill, 'triad-actual', triad_kws, financial_year_start, financial_year_finish, data_source, month_start) bill['triad-actual-gbp'] = bill['triad-actual-rate'] * \ bill['triad-actual-gsp-kw'] era = data_source.supply.find_era_at( data_source.sess, month_finish) est_intervals = 0 interval = relativedelta(months=1) if \ rate_period == 'monthly' else relativedelta(days=1) dt = month_finish while era is not None and dt > financial_year_start: est_intervals += 1 dt -= interval if hh_after(dt, era.finish_date): era = data_source.supply.find_era_at(data_source.sess, dt) if rate_period == 'monthly': bill['triad-all-estimates-months'] = est_intervals else: bill['triad-all-estimates-days'] = est_intervals bill['triad-all-estimates-gbp'] = est_triad_gbp / \ total_intervals * est_intervals * -1
def ccl(data_source): rate_set = data_source.supplier_rate_sets["ccl-rate"] if data_source.supply.find_era_at(data_source.sess, data_source.finish_date + HH) is None: sup_end = data_source.finish_date else: sup_end = None try: cache = data_source.caches["ccl"] except: data_source.caches["ccl"] = {} cache = data_source.caches["ccl"] try: future_funcs = data_source.caches["future_funcs"] except KeyError: future_funcs = {} data_source.caches["future_funcs"] = future_funcs try: future_funcs[db_id] except KeyError: future_funcs[db_id] = {"start_date": None, "func": create_future_func(1, 0)} if data_source.bill is None: for hh in data_source.hh_data: if hh["utc-is-month-end"] or hh["start-date"] == sup_end: month_finish = hh["start-date"] kwh = 0 gbp = 0 month_start = datetime.datetime(month_finish.year, month_finish.month, 1, tzinfo=pytz.utc) for ds in computer.get_data_sources(data_source, month_start, month_finish): for datum in ds.hh_data: try: rate = cache[datum["start-date"]] except KeyError: cache[datum["start-date"]] = data_source.hh_rate(db_id, datum["start-date"], "ccl_rate") rate = cache[datum["start-date"]] rate_set.add(rate) kwh += datum["msp-kwh"] gbp += datum["msp-kwh"] * rate if kwh > 999: hh["ccl-kwh"] = kwh hh["ccl-gbp"] = gbp elif data_source.is_last_bill_gen: if data_source.pc_code in ["03", "04"]: threshold = 2999 else: threshold = 999 kwh = 0 gbp = 0 for ds in computer.get_data_sources(data_source, data_source.bill_start, data_source.bill_finish): for hh in ds.hh_data: try: rate = cache[hh["start-date"]] except KeyError: cache[hh["start-date"]] = data_source.hh_rate(db_id, hh["start-date"], "ccl_rate") rate = cache[hh["start-date"]] rate_set.add(rate) kwh += hh["msp-kwh"] gbp += hh["msp-kwh"] * rate if kwh > threshold: data_source.hh_data[-1]["ccl-kwh"] = kwh data_source.hh_data[-1]["ccl-gbp"] = gbp