import copy


def get_annual_el_load(data_array, type, annual_demand=None):
    """
    Get annual electrical load profile based on measurement data for
    non-residential buildings (900 seconds timestep)

    Parameters
    ----------
    data_array : np.array
        ND-Array with data
        900 seconds timestep; length for one year (365 days)
        starting on monday (00:00:00)
        first column: metal 1 (smoother profile)
        second column: metal 2 (more fluctuation)
        third column: warehouse
    type : str
        Type of chosen measured profile. Options:
        - 'metal_1' : Metal company with smooth profile
        - 'metal_2' : Metal company with fluctuation in profile
        - 'warehouse' : Warehouse
    annual_demand : float, optional
        Annual el. energy demand in kWh to rescale profile (default: None).
        If set to None, does not perform rescaling.

    Returns
    -------
    el_load_curve : np.array
        Numpy array with el. power curve in Watt
    """

    assert type in ['metal_1', 'metal_2', 'warehouse'], 'You need to define a valid type'

    #  Type dictionary; Type as key; column number as value
    dict_type = {'metal_1': 0, 'metal_2': 1, 'warehouse': 2}

    #  Get column index
    column_idx = dict_type[type]

    #  Extract weekly array depending on type
    el_load_curve = copy.deepcopy(data_array[:, column_idx])

    #  Rescale profile
    if annual_demand is not None:
        assert annual_demand > 0, 'Annual el. demand must be larger than 0!'

        #  Convert to energy values in kWh
        energy_curve = el_load_curve * 900 / (3600 * 1000)

        #  Conversion factor
        con_fac = annual_demand / sum(energy_curve)

        el_load_curve *= con_fac

    return el_load_curve