# Path: calliope/backend/pyomo/util.py
# @memoize
# def get_param(backend_model, var, dims):
#     """
#     Get an input parameter held in a Pyomo object, or held in the defaults
#     dictionary if that Pyomo object doesn't exist.
# 
#     Parameters
#     ----------
#     backend_model : Pyomo model instance
#     var : str
#     dims : single value or tuple
# 
#     """
#     try:
#         return getattr(backend_model, var)[dims]
#     except AttributeError:  # i.e. parameter doesn't exist at all
#         logger.debug(
#             "get_param: var {} and dims {} leading to default lookup".format(var, dims)
#         )
#         return backend_model.__calliope_defaults[var]
#     except KeyError:  # try removing timestep
#         try:
#             if len(dims) > 2:
#                 return getattr(backend_model, var)[dims[:-1]]
#             else:
#                 return getattr(backend_model, var)[dims[0]]
#         except KeyError:  # Static default value
#             logger.debug(
#                 "get_param: var {} and dims {} leading to default lookup".format(
#                     var, dims
#                 )
#             )
#             return backend_model.__calliope_defaults[var]
# 
# @memoize
# def get_timestep_weight(backend_model):
#     """
#     Get the total number of years this model considers, by summing all
#     timestep resolution with timestep weight (a weight/resolution of 1 = 1 hour)
#     and divide it by number of hours in the year. Weight/resolution will almost
#     always be 1 per step, unless time clustering/masking/resampling has taken place.
#     """
#     time_res = [po.value(i) for i in backend_model.timestep_resolution.values()]
#     weights = [po.value(i) for i in backend_model.timestep_weights.values()]
#     return sum(np.multiply(time_res, weights)) / 8760
# 
# def loc_tech_is_in(backend_model, loc_tech, model_set):
#     """
#     Check if set exists and if loc_tech is in the set
# 
#     Parameters
#     ----------
#     loc_tech : string
#     model_set : string
#     """
# 
#     if hasattr(backend_model, model_set) and loc_tech in getattr(
#         backend_model, model_set
#     ):
#         return True
#     else:
#         return False


import pyomo.core as po
from calliope.backend.pyomo.util import get_param, get_timestep_weight, loc_tech_is_in
            return _cost
        else:
            return 0

    cost_energy_cap = backend_model.energy_cap[node, tech] * get_param(
        backend_model, "cost_energy_cap", (cost, node, tech)
    )

    cost_storage_cap = _get_investment_cost("storage_cap")
    cost_resource_cap = _get_investment_cost("resource_cap")
    cost_resource_area = _get_investment_cost("resource_area")

    cost_om_annual_investment_fraction = get_param(
        backend_model, "cost_om_annual_investment_fraction", (cost, node, tech)
    )
    cost_om_annual = get_param(backend_model, "cost_om_annual", (cost, node, tech))

    if loc_tech_is_in(backend_model, (node, tech), "units_index"):
        cost_of_purchase = (
            get_param(backend_model, "cost_purchase", (cost, node, tech))
            * backend_model.units[node, tech]
        )
    elif loc_tech_is_in(backend_model, (node, tech), "purchased_index"):
        cost_of_purchase = (
            get_param(backend_model, "cost_purchase", (cost, node, tech))
            * backend_model.purchased[node, tech]
        )
    else:
        cost_of_purchase = 0


# Path: calliope/backend/pyomo/util.py
# @memoize
# def get_param(backend_model, var, dims):
#     """
#     Get an input parameter held in a Pyomo object, or held in the defaults
#     dictionary if that Pyomo object doesn't exist.
# 
#     Parameters
#     ----------
#     backend_model : Pyomo model instance
#     var : str
#     dims : single value or tuple
# 
#     """
#     try:
#         return getattr(backend_model, var)[dims]
#     except AttributeError:  # i.e. parameter doesn't exist at all
#         logger.debug(
#             "get_param: var {} and dims {} leading to default lookup".format(var, dims)
#         )
#         return backend_model.__calliope_defaults[var]
#     except KeyError:  # try removing timestep
#         try:
#             if len(dims) > 2:
#                 return getattr(backend_model, var)[dims[:-1]]
#             else:
#                 return getattr(backend_model, var)[dims[0]]
#         except KeyError:  # Static default value
#             logger.debug(
#                 "get_param: var {} and dims {} leading to default lookup".format(
#                     var, dims
#                 )
#             )
#             return backend_model.__calliope_defaults[var]
# 
# @memoize
# def get_timestep_weight(backend_model):
#     """
#     Get the total number of years this model considers, by summing all
#     timestep resolution with timestep weight (a weight/resolution of 1 = 1 hour)
#     and divide it by number of hours in the year. Weight/resolution will almost
#     always be 1 per step, unless time clustering/masking/resampling has taken place.
#     """
#     time_res = [po.value(i) for i in backend_model.timestep_resolution.values()]
#     weights = [po.value(i) for i in backend_model.timestep_weights.values()]
#     return sum(np.multiply(time_res, weights)) / 8760
# 
# def loc_tech_is_in(backend_model, loc_tech, model_set):
#     """
#     Check if set exists and if loc_tech is in the set
# 
#     Parameters
#     ----------
#     loc_tech : string
#     model_set : string
#     """
# 
#     if hasattr(backend_model, model_set) and loc_tech in getattr(
#         backend_model, model_set
#     ):
#         return True
#     else:
#         return False


# Path: calliope/backend/pyomo/constraints/costs.py
import pyomo.core as po
from calliope.backend.pyomo.util import get_param, get_timestep_weight, loc_tech_is_in

            return _cost
        else:
            return 0

    cost_energy_cap = backend_model.energy_cap[node, tech] * get_param(
        backend_model, "cost_energy_cap", (cost, node, tech)
    )

    cost_storage_cap = _get_investment_cost("storage_cap")
    cost_resource_cap = _get_investment_cost("resource_cap")
    cost_resource_area = _get_investment_cost("resource_area")

    cost_om_annual_investment_fraction = get_param(
        backend_model, "cost_om_annual_investment_fraction", (cost, node, tech)
    )
    cost_om_annual = get_param(backend_model, "cost_om_annual", (cost, node, tech))

    if loc_tech_is_in(backend_model, (node, tech), "units_index"):
        cost_of_purchase = (
            get_param(backend_model, "cost_purchase", (cost, node, tech))
            * backend_model.units[node, tech]
        )
    elif loc_tech_is_in(backend_model, (node, tech), "purchased_index"):
        cost_of_purchase = (
            get_param(backend_model, "cost_purchase", (cost, node, tech))
            * backend_model.purchased[node, tech]
        )
    else:
        cost_of_purchase = 0


    ts_weight = get_timestep_weight(backend_model)