using StatsBase

"""
    smd_out = isolatesmd(smd::SMD, indices::Union{Vector, UnitRange})

Isolate the requested localizations from `smd`.

# Description.
Make a copy of `smd` and isolate only those localizations corresponding to the
indices.
"""
function isolatesmd(smd::SMD, indices::Union{Vector, UnitRange})
    # Determine the length of vector fields and ensure consistency with the
    # provided indices.
    arraylength = length(smd.framenum)
    if maximum(indices) > arraylength
        error(string("Input `indices` contains values greater than the ", 
            "length of `smd.framenum`"))
    end

    # Loop through the fields in `smd` and isolate the appropriate indices,
    # leaving fields with a different length than `arraylength` unchanged.
    smd_out = deepcopy(smd)
    for field in fieldnames(SMD)
        currentfield = getfield(smd_out, field)
        if length(currentfield) == arraylength
            setfield!(smd_out, field, currentfield[indices])
        end
    end

    return smd_out
end


"""
    smd = isolatesmd(smd::SMD, keepbool::Vector{Bool})

Isolate the requested localizations from `smd`.

# Description.
Make a copy of `smd` and isolate only those localizations corresponding to the
true values of `keepbool`.
"""
function isolatesmd(smd::SMD, keepbool::AbstractArray{Bool})
    # Determine the length of vector fields and ensure consistency with the
    # provided `keepbool`.
    arraylength = length(smd.framenum)
    if length(keepbool) > arraylength
        error(string("Input `keepbool` is longer than the ", 
            "length of `smd.framenum`"))
    end

    # Loop through the fields in `smd` and isolate the appropriate keepbool,
    # leaving fields with a different length than `arraylength` unchanged.
    smd_out = deepcopy(smd)
    for field in fieldnames(SMD)
        currentfield = getfield(smd_out, field)
        if length(currentfield) == arraylength
            setfield!(smd_out, field, currentfield[keepbool])
        end
    end

    return smd_out
end