# 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]
# 
# 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


from calliope.backend.pyomo.util import (
    get_param,
    loc_tech_is_in,
)
    """
    Set maximum export. All exporting technologies.

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{carrier_{export}}(loc::tech::carrier, timestep)
            \\leq export_{cap}(loc::tech)
            \\quad \\forall loc::tech::carrier \\in locs::tech::carriers_{export},
            \\forall timestep \\in timesteps

    If the technology is defined by integer units, not a continuous capacity,
    this constraint becomes:

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{carrier_{export}}(loc::tech::carrier, timestep)
            \\leq export_{cap}(loc::tech) \\times
            \\boldsymbol{operating_{units}}(loc::tech, timestep)

    """

    if loc_tech_is_in(backend_model, (node, tech), "operating_units_index"):
        operating_units = backend_model.operating_units[node, tech, timestep]
    else:
        operating_units = 1


# 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]
# 
# 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/export.py
from calliope.backend.pyomo.util import (
    get_param,
    loc_tech_is_in,
)

    """
    Set maximum export. All exporting technologies.

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{carrier_{export}}(loc::tech::carrier, timestep)
            \\leq export_{cap}(loc::tech)
            \\quad \\forall loc::tech::carrier \\in locs::tech::carriers_{export},
            \\forall timestep \\in timesteps

    If the technology is defined by integer units, not a continuous capacity,
    this constraint becomes:

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{carrier_{export}}(loc::tech::carrier, timestep)
            \\leq export_{cap}(loc::tech) \\times
            \\boldsymbol{operating_{units}}(loc::tech, timestep)

    """

    if loc_tech_is_in(backend_model, (node, tech), "operating_units_index"):
        operating_units = backend_model.operating_units[node, tech, timestep]
    else:
        operating_units = 1


    export_max = get_param(backend_model, "export_max", (node, tech))