How to use the calliope.analysis.util.subset_sum_squeeze function in calliope

To help you get started, we’ve selected a few calliope examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github calliope-project / calliope / calliope / analysis / plotting.py View on Github external
)

        # array flow for storage tracks stored energy. carrier_flow is
        # charge/discharge (including resource consumed for supply_plus techs)
        elif var == 'storage':
            array_flow = _get_reindexed_array('storage')
            carrier_flow = (array_prod.sum('carriers') + array_con.sum('carriers') - resource_con)
            carrier_flow = subset_sum_squeeze(carrier_flow, subset, sum_dims, squeeze)

        elif var == 'resource_con':
            array_flow = resource_con

        else:
            array_flow = _get_reindexed_array(var)

        array_flow = subset_sum_squeeze(array_flow, subset, sum_dims, squeeze)

        if 'timesteps' not in array_flow.dims or len(array_flow.dims) > 2:
            e = exceptions.ModelError
            raise e('Cannot plot timeseries for variable `{}` with subset `{}`'
                    'and `sum_dims: {}`'.format(var, subset, sum_dims))

        for tech in array_flow.techs.values:
            tech_dict = {'techs': tech}
            if not array_flow.loc[tech_dict].sum():
                continue
            # We allow transmisison tech information to show up in some cases
            if 'techs_transmission' in dataset and tech in dataset.techs_transmission.values:
                base_tech = 'transmission'
                color = dataset.colors.loc[{'techs': tech.split(':')[0]}].item()
                name = dataset.names.loc[{'techs': tech.split(':')[0]}].item()
                if var in carriers:
github calliope-project / calliope / calliope / analysis / plotting / timeseries.py View on Github external
'unmet_demand', index=['locs', 'carriers'], fillna=0
                ).loc[dict(carriers=var)],
                subset, sum_dims, squeeze=False
            )

    # array flow for storage tracks stored energy. carrier_flow is
    # charge/discharge (including resource consumed for supply_plus techs)
    elif var == 'storage':
        array_flow = _get_reindexed_array('storage')

        if 'resource_eff' in dataset.data_vars:
            resource_eff = _get_reindexed_array('resource_eff', fillna=1)
        else:
            resource_eff = 1

        charge = subset_sum_squeeze(
            -array_con.sum('carriers') + resource_con * resource_eff,
            subset, sum_dims, squeeze=False
        )
        discharge = -subset_sum_squeeze(
            array_prod.sum('carriers'), subset, sum_dims, squeeze=False
        )

    elif var == 'resource_con':
        array_flow = resource_con

    else:
        array_flow = _get_reindexed_array(var)

    array_flow = subset_sum_squeeze(array_flow, subset, sum_dims, squeeze)

    err_details = ' (variable: `{}`, subset: `{}`, sum_dims: `{}`.'.format(var, subset, sum_dims)
github calliope-project / calliope / calliope / analysis / plotting / timeseries.py View on Github external
# array flow for storage tracks stored energy. carrier_flow is
    # charge/discharge (including resource consumed for supply_plus techs)
    elif var == 'storage':
        array_flow = _get_reindexed_array('storage')

        if 'resource_eff' in dataset.data_vars:
            resource_eff = _get_reindexed_array('resource_eff', fillna=1)
        else:
            resource_eff = 1

        charge = subset_sum_squeeze(
            -array_con.sum('carriers') + resource_con * resource_eff,
            subset, sum_dims, squeeze=False
        )
        discharge = -subset_sum_squeeze(
            array_prod.sum('carriers'), subset, sum_dims, squeeze=False
        )

    elif var == 'resource_con':
        array_flow = resource_con

    else:
        array_flow = _get_reindexed_array(var)

    array_flow = subset_sum_squeeze(array_flow, subset, sum_dims, squeeze)

    err_details = ' (variable: `{}`, subset: `{}`, sum_dims: `{}`.'.format(var, subset, sum_dims)

    if 'timesteps' not in array_flow.dims and 'datesteps' not in array_flow.dims:
        raise ValueError(
            '`timesteps` not in plotting data, cannot proceed' + err_details
github calliope-project / calliope / calliope / analysis / plotting.py View on Github external
if fillna is not None:
                return formatted_array.reindex(**reindexer).fillna(fillna)
            else:
                return formatted_array.reindex(**reindexer)

        if hasattr(model, 'results'):
            array_prod = _get_reindexed_array('carrier_prod', index=['locs', 'techs', 'carriers'], fillna=0)
            array_con = _get_reindexed_array('carrier_con', index=['locs', 'techs', 'carriers'], fillna=0)
            resource_con = _get_reindexed_array('resource_con', fillna=0)

        # carrier flow is a combination of carrier_prod, carrier_con and
        # carrier_export for a given energy carrier
        if var in carriers:
            array_flow = (array_prod.loc[dict(carriers=var)] + array_con.loc[dict(carriers=var)])
            if 'carrier_export' in dataset:
                export_flow = subset_sum_squeeze(
                    _get_reindexed_array(
                        'carrier_export', index=['locs', 'techs', 'carriers'], fillna=0
                    ).loc[dict(carriers=var)],
                    subset, sum_dims, squeeze
                )
            if 'unmet_demand' in dataset:
                unmet_flow = subset_sum_squeeze(
                    _get_reindexed_array(
                        'unmet_demand', index=['locs', 'carriers'], fillna=0
                    ).loc[dict(carriers=var)],
                    subset, sum_dims, squeeze=False
                )

        # array flow for storage tracks stored energy. carrier_flow is
        # charge/discharge (including resource consumed for supply_plus techs)
        elif var == 'storage':
github calliope-project / calliope / calliope / analysis / plotting.py View on Github external
subset, sum_dims, squeeze
                )
            if 'unmet_demand' in dataset:
                unmet_flow = subset_sum_squeeze(
                    _get_reindexed_array(
                        'unmet_demand', index=['locs', 'carriers'], fillna=0
                    ).loc[dict(carriers=var)],
                    subset, sum_dims, squeeze=False
                )

        # array flow for storage tracks stored energy. carrier_flow is
        # charge/discharge (including resource consumed for supply_plus techs)
        elif var == 'storage':
            array_flow = _get_reindexed_array('storage')
            carrier_flow = (array_prod.sum('carriers') + array_con.sum('carriers') - resource_con)
            carrier_flow = subset_sum_squeeze(carrier_flow, subset, sum_dims, squeeze)

        elif var == 'resource_con':
            array_flow = resource_con

        else:
            array_flow = _get_reindexed_array(var)

        array_flow = subset_sum_squeeze(array_flow, subset, sum_dims, squeeze)

        if 'timesteps' not in array_flow.dims or len(array_flow.dims) > 2:
            e = exceptions.ModelError
            raise e('Cannot plot timeseries for variable `{}` with subset `{}`'
                    'and `sum_dims: {}`'.format(var, subset, sum_dims))

        for tech in array_flow.techs.values:
            tech_dict = {'techs': tech}
github calliope-project / calliope / calliope / analysis / plotting.py View on Github external
array_con = _get_reindexed_array('carrier_con', index=['locs', 'techs', 'carriers'], fillna=0)
            resource_con = _get_reindexed_array('resource_con', fillna=0)

        # carrier flow is a combination of carrier_prod, carrier_con and
        # carrier_export for a given energy carrier
        if var in carriers:
            array_flow = (array_prod.loc[dict(carriers=var)] + array_con.loc[dict(carriers=var)])
            if 'carrier_export' in dataset:
                export_flow = subset_sum_squeeze(
                    _get_reindexed_array(
                        'carrier_export', index=['locs', 'techs', 'carriers'], fillna=0
                    ).loc[dict(carriers=var)],
                    subset, sum_dims, squeeze
                )
            if 'unmet_demand' in dataset:
                unmet_flow = subset_sum_squeeze(
                    _get_reindexed_array(
                        'unmet_demand', index=['locs', 'carriers'], fillna=0
                    ).loc[dict(carriers=var)],
                    subset, sum_dims, squeeze=False
                )

        # array flow for storage tracks stored energy. carrier_flow is
        # charge/discharge (including resource consumed for supply_plus techs)
        elif var == 'storage':
            array_flow = _get_reindexed_array('storage')
            carrier_flow = (array_prod.sum('carriers') + array_con.sum('carriers') - resource_con)
            carrier_flow = subset_sum_squeeze(carrier_flow, subset, sum_dims, squeeze)

        elif var == 'resource_con':
            array_flow = resource_con
github calliope-project / calliope / calliope / analysis / plotting / timeseries.py View on Github external
charge = subset_sum_squeeze(
            -array_con.sum('carriers') + resource_con * resource_eff,
            subset, sum_dims, squeeze=False
        )
        discharge = -subset_sum_squeeze(
            array_prod.sum('carriers'), subset, sum_dims, squeeze=False
        )

    elif var == 'resource_con':
        array_flow = resource_con

    else:
        array_flow = _get_reindexed_array(var)

    array_flow = subset_sum_squeeze(array_flow, subset, sum_dims, squeeze)

    err_details = ' (variable: `{}`, subset: `{}`, sum_dims: `{}`.'.format(var, subset, sum_dims)

    if 'timesteps' not in array_flow.dims and 'datesteps' not in array_flow.dims:
        raise ValueError(
            '`timesteps` not in plotting data, cannot proceed' + err_details
        )

    if len(array_flow.dims) > 2:
        if 'costs' in array_flow.dims:
            err_details = err_details + (' Try subsetting to select a cost class, '
                "e.g. subset={'costs': ['monetary']}.")
        raise ValueError(
            'Too many dimensions to plot: `{}`'.format(array_flow.dims) + err_details
        )
github calliope-project / calliope / calliope / analysis / plotting / capacity.py View on Github external
def _get_var_data(
        cap, model, dataset, visible, subset, sum_dims, squeeze,
        locations, orientation):
    if 'systemwide' in cap:
        array_cap = subset_sum_squeeze(dataset[cap], subset)
        if 'costs' in array_cap.dims and len(array_cap['costs']) == 1:
            array_cap = array_cap.squeeze('costs')
        elif 'costs' in array_cap.dims and len(array_cap['costs']) > 1:
            raise ValueError(
                'Cannot plot {} without subsetting to pick one cost type '
                'of interest'.format(cap)
            )
        if 'carriers' not in subset.keys():
            array_cap = array_cap.sortby('carriers')

    else:
        array_cap = model.get_formatted_array(cap).reindex(locs=locations)
        array_cap = subset_sum_squeeze(array_cap, subset, sum_dims, squeeze)

    if len(array_cap.dims) > 2:
        raise ValueError(