"""
collection of functions for interacting with UBCS/LCP channel archiver
"""

def get_all_archivers():
    """
    returns all available archivers
    """
    pass

def get_all_pvnames_in_archiver(archiver):
    """
    returns names of all PV names in a give archiver
    """
    pass

def read_pv(pvname, archiver = 'LaserLab', start=None, end=None):
    """
    reads file generated by Channel Archiver and return all values between time minimim (tmin) and time maximum (tmax)
    """
    from sys import path
    path.append('/net/femto/C/All Projects/APS/Instrumentation/Software/Lauecollect/')
    from channel_archiver import channel_archiver

    time, value = channel_archiver(archiver).history(pvname, start, end)
    return time, value

def read_pv_time():
    """np.where(np.array(t)<=tt)[0][-
        np.where(np.array(t)>=tt)[0][0]
        """
    pass

def get_archived_PV(archiver, pvname, timestamps):
    """takes a one process variable name and a list of time stamps and returns corresponding values as a dictionary.

    Parameters
    ----------
    pvname (string)
    timestamps (list)
    exclude (list)

    Returns
    -------
    data (dict)

    Examples
    --------
    """
    from numpy import where, array, diff, mean
    timestamps = array(timestamps)
    dt = mean(diff(timestamps))
    t,value = read_pv(pvname = pvname, archiver = archiver, start=timestamps[0] - +5*dt, end=timestamps[-1]+5*dt)
    from scipy.interpolate import UnivariateSpline
    spl = UnivariateSpline(t, value)
    return timestamps, spl(timestamps)


def get_archived_PVs(archiver, pv_names, timestamps):
    """take a list of process variables and a list of time stamps and returns corresponding values as a dictionary.

    Parameters
    ----------
    pv_names (list)
    timestamps (list)
    exclude (list)

    Returns
    -------
    data (dict)

    Examples
    --------
    """
    result = {}
    for pv_name in pv_names:
        t,value = get_archived_PV(archiver = archiver, pvname = pv_name, timestamps = timestamps)
        result[pv_name] = value
    result['time'] = timestamps
    return result
