Exemple #1
0
        def triad_csv(supply_source):
            if supply_source is None or \
                    supply_source.mpan_core.startswith('99'):
                return [''] * 19

            duos.duos_vb(supply_source)
            triad.triad_bill(supply_source)

            bill = supply_source.supplier_bill
            values = [supply_source.mpan_core]
            for i in range(1, 4):
                triad_prefix = 'triad-actual-' + str(i)
                for suffix in [
                        '-date', '-msp-kw', '-status', '-laf', '-gsp-kw']:
                    values.append(bill[triad_prefix + suffix])

            suffixes = ['gsp-kw', 'rate', 'gbp']
            values += [bill['triad-actual-' + suf] for suf in suffixes]
            return values
Exemple #2
0
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
Exemple #3
0
def mpan_bit(sess, supply, is_import, num_hh, eras, chunk_start, chunk_finish):
    mpan_core_str = ''
    llfc_code = ''
    sc_str = ''
    supplier_contract_name = ''
    gsp_kwh = ''
    for era in eras:
        mpan_core = era.imp_mpan_core if is_import else era.exp_mpan_core
        if mpan_core is None:
            continue
        mpan_core_str = mpan_core
        if is_import:
            supplier_contract_name = era.imp_supplier_contract.name
            llfc = era.imp_llfc
            sc = era.imp_sc
        else:
            supplier_contract_name = era.exp_supplier_contract.name
            llfc = era.exp_llfc
            sc = era.exp_sc
        llfc_code = llfc.code
        sc_str = str(sc)
        if llfc.is_import and era.pc.code == '00' and \
                supply.source.code not in ('gen') and \
                supply.dno_contract.name != '99':
            if gsp_kwh == '':
                gsp_kwh = 0

            if chunk_start > era.start_date:
                block_start = chunk_start
            else:
                block_start = era.start_date

            if hh_before(chunk_finish, era.finish_date):
                block_finish = chunk_finish
            else:
                block_finish = era.finish_date

            supply_source = computer.SupplySource(
                sess, block_start, block_finish, forecast_date, era, is_import,
                None, caches)

            duos.duos_vb(supply_source)

            gsp_kwh += sum(
                [datum['gsp-kwh'] for datum in supply_source.hh_data])

    md = 0
    sum_kwh = 0
    non_actual = 0
    date_at_md = None
    kvarh_at_md = None
    num_na = 0

    for datum in sess.query(HhDatum).join(Channel).join(Era).filter(
            Era.supply_id == supply.id, Channel.imp_related == is_import,
            Channel.channel_type == 'ACTIVE',
            HhDatum.start_date >= chunk_start,
            HhDatum.start_date <= chunk_finish):
        hh_value = float(datum.value)
        hh_status = datum.status
        if hh_value > md:
            md = hh_value
            date_at_md = datum.start_date
            kvarh_at_md = sess.query(
                cast(func.max(HhDatum.value), Float)).join(
                Channel).join(Era).filter(
                Era.supply == supply,
                Channel.imp_related == is_import,
                Channel.channel_type != 'ACTIVE',
                HhDatum.start_date == date_at_md).one()[0]

        sum_kwh += hh_value
        if hh_status != 'A':
            non_actual = non_actual + hh_value
            num_na = num_na + 1

    kw_at_md = md * 2
    if kvarh_at_md is None:
        kva_at_md = 'None'
    else:
        kva_at_md = (kw_at_md ** 2 + (kvarh_at_md * 2) ** 2) ** 0.5

    num_bad = str(num_hh - sess.query(HhDatum).join(Channel).join(Era).filter(
        Era.supply_id == supply.id, Channel.imp_related == is_import,
        Channel.channel_type == 'ACTIVE', HhDatum.start_date >= chunk_start,
        HhDatum.start_date <= chunk_finish).count() + num_na)

    date_at_md_str = '' if date_at_md is None else hh_format(date_at_md)

    return ','.join(str(val) for val in [
        llfc_code, mpan_core_str, sc_str, supplier_contract_name, sum_kwh,
        non_actual, gsp_kwh, kw_at_md, date_at_md_str, kva_at_md, num_bad])
Exemple #4
0
def content():
    sess = None
    try:
        sess = db.session()

        march_finish = datetime.datetime(year, 4, 1, tzinfo=pytz.utc) - HH
        march_start = datetime.datetime(year, 3, 1, tzinfo=pytz.utc)

        yield ", ".join(
            (
                "Site Code",
                "Site Name",
                "Displaced TRIAD 1 Date",
                "Displaced TRIAD 1 MSP kW",
                "Displaced TRIAD LAF",
                "Displaced TRIAD 1 GSP kW",
                "Displaced TRIAD 2 Date",
                "Displaced TRIAD 2 MSP kW",
                "Displaced TRIAD 2 LAF",
                "Displaced TRIAD 2 GSP kW",
                "Displaced TRIAD 3 Date",
                "Displaced TRIAD 3 MSP kW",
                "Displaced TRIAD 3 LAF",
                "Displaced TRIAD 3 GSP kW",
                "Displaced GSP kW",
                "Displaced Rate GBP / kW",
                "GBP",
            )
        ) + "\n"

        forecast_date = computer.forecast_date()

        if site_id is None:
            sites = (
                sess.query(Site)
                .join(SiteEra)
                .join(Era)
                .join(Supply)
                .join(Source)
                .filter(
                    Source.code.in_(("gen", "gen-net")),
                    Era.start_date <= march_finish,
                    or_(Era.finish_date == null(), Era.finish_date >= march_start),
                )
                .distinct()
            )
        else:
            site = Site.get_by_id(sess, site_id)
            sites = sess.query(Site).filter(Site.id == site.id)

        for site in sites:
            for site_group in site.groups(sess, march_start, march_finish, True):
                if site_group.start_date > march_start:
                    chunk_start = site_group.start_date
                else:
                    chunk_start = march_start

                if not site_group.finish_date < march_finish:
                    chunk_finish = march_finish
                else:
                    continue

                yield '"' + site.code + '","' + site.name + '"'

                displaced_era = computer.displaced_era(sess, site_group, chunk_start, chunk_finish)
                if displaced_era is None:
                    continue

                site_ds = computer.SiteSource(
                    sess, site, chunk_start, chunk_finish, forecast_date, None, caches, displaced_era
                )
                duos.duos_vb(site_ds)
                triad.triad_bill(site_ds)

                bill = site_ds.supplier_bill
                values = []
                for i in range(1, 4):
                    triad_prefix = "triad-actual-" + str(i)
                    for suffix in ["-date", "-msp-kw", "-laf", "-gsp-kw"]:
                        values.append(bill[triad_prefix + suffix])

                values += [bill["triad-actual-" + suf] for suf in ["gsp-kw", "rate", "gbp"]]

                for value in values:
                    if isinstance(value, datetime.datetime):
                        yield "," + hh_format(value)
                    else:
                        yield "," + str(value)
                yield "\n"
    except:
        yield traceback.format_exc()
    finally:
        if sess is not None:
            sess.close()