import pickle


def load_pickle(filename, channel=-1, verbose=0):
    """
    Load Joerg's pickle files.

    Parameters
    ----------
    filepath: string
        The full path and name of the file to load.
    channel: int
        The data channel. If negative all channels are selected.
    verbose: int
        if > 0 show detailed error/warning messages

    Returns
    -------
    data: 1-D or 2-D array
        If channel is negative, a 2-D array with data of all channels is returned,
        where first dimension is time and second dimension is channel number.
        Otherwise an 1-D array with the data of that channel is returned.
    samplerate: float
        The sampling rate of the data in Hz.
    unit: string
        The unit of the data.
    """
    with open(filename, 'rb') as f:
        data = pickle.load(f)
    if verbose > 0:
        print( 'loaded %s' % filename)
    time = data['time_trace']
    samplerate = 1000.0 / (time[1] - time[0])
    if channel >= 0:
        if channel >= data.shape[1]:
            raise IndexError('invalid channel number %d requested' % channel)
        data = data[:, channel]
        return data['raw_data'][:, channel], samplerate, 'mV'
    return data['raw_data'], samplerate, 'mV'