Read accelerometry data from leonardo txt files

    Args
    ----
    meta: dict
        Dictionary of meta data from header lines of lleo data files
    path_dir: str
        Parent directory containing lleo data files
    sample_f: int
        Return every `sample_f` data points

    Returns
    -------
    acc: pandas.DataFrame
        Dataframe containing accelerometry data on x, y, z axes [m/s^2]
    depth: pandas.DataFrame
        Dataframe containing depth data [m]
    prop: pandas.DataFrame
        Dataframe containing speed data from propeller
    temp: pandas.DataFrame
        Dataframe containing temperature data

def read_data(meta, path_dir, sample_f=1, decimate=False, overwrite=False):
    '''Read accelerometry data from leonardo txt files

    Args
    ----
    meta: dict
        Dictionary of meta data from header lines of lleo data files
    path_dir: str
        Parent directory containing lleo data files
    sample_f: int
        Return every `sample_f` data points

    Returns
    -------
    acc: pandas.DataFrame
        Dataframe containing accelerometry data on x, y, z axes [m/s^2]
    depth: pandas.DataFrame
        Dataframe containing depth data [m]
    prop: pandas.DataFrame
        Dataframe containing speed data from propeller
    temp: pandas.DataFrame
        Dataframe containing temperature data
    '''
    import os
    import pandas

    from . import utils

    def _generate_datetimes(date, time, interval_s, n_timestamps):
        '''Generate list of datetimes from date/time with given interval'''
        from datetime import datetime, timedelta
        import pandas

        # TODO problematic if both m/d d/m options
        fmts  = ['%Y/%m/%d %H%M%S',
                 '%d/%m/%Y %H%M%S',
                 '%m/%d/%Y %I%M%S %p',
                 '%d/%m/%Y %I%M%S %p',]

        for fmt in fmts:
            try:
                start = pandas.to_datetime('{} {}'.format(date,time), format=fmt)
            except:
                print('Date format {:18} incorrect, '
                      'trying next...'.format(fmt))
            else:
                print('Date format {:18} correct.'.format(fmt))
                break

        # Create datetime array
        datetimes = list()
        for i in range(n_timestamps):
            secs = interval_s*i
            datetimes.append(start + timedelta(seconds=secs))

        return datetimes


    def _read_data_file(meta, path_dir, param_str):
        '''Read single Little Leonardo txt data file'''
        import numpy
        import os
        import pandas

        from . import utils

        # Get path of data file and associated pickle file
        path_file = utils.find_file(path_dir, param_str, '.TXT')
        col_name = utils.posix_string(param_str)

        # Get number of header rows in file
        enc = utils.predict_encoding(path_file, n_lines=20)
        with open(path_file, 'r', encoding=enc) as f:
            n_header = utils.get_n_header(f)

        print('\nReading: {}'.format(col_name))

        data = numpy.genfromtxt(path_file, skip_header=n_header)

        interval_s = float(meta['parameters'][col_name]['Interval(Sec)'])
        date = meta['parameters'][col_name]['Start date']
        time = meta['parameters'][col_name]['Start time']

        # TODO review
        # Generate summed data if propeller sampling rate not 1
        if (col_name == 'propeller') and (interval_s < 1):
            print('Too high sampling interval, taking sums')
            # Sampling rate
            fs = int(1/interval_s)

            print('data before', data.max())
            # Drop elements to make divisible by fs for summing
            data = data[:-int(len(data)%fs)]

            # Reshape to 2D with columns `fs` in length to be summed
            data = data.reshape(fs, int(len(data)/fs))
            data = numpy.sum(data, axis=0)
            interval_s = 1

            print('data after', data.max())

        datetimes = _generate_datetimes(date, time, interval_s, len(data))
        data      = numpy.vstack((datetimes, data)).T
        df        = pandas.DataFrame(data, columns=['datetimes', col_name])

        return df

    # Get list of string parameter names for tag model
    param_names = utils.get_tag_params(meta['tag_model'])

    # Load pickle file exists and code unchanged
    pickle_file = os.path.join(path_dir, 'pydata_'+meta['experiment']+'.p')

    # Load or create pandas DataFrame with parameters associated with tag model
    if (os.path.exists(pickle_file)) and (overwrite is not True):
        data_df = pandas.read_pickle(pickle_file)
    else:
        first_col = True
        for name in param_names:
            next_df = _read_data_file(meta, path_dir, name)
            if first_col == False:
                data_df = pandas.merge(data_df, next_df, on='datetimes', how='left')
            else:
                data_df = next_df
                first_col = False
        print('')

        # Covert columns to `datetime64` or `float64` types
        data_df = data_df.apply(lambda x: pandas.to_numeric(x, errors='ignore'))

        # Save file to pickle
        data_df.to_pickle(pickle_file)

    # Return DataFrame with ever `sample_f` values
    return data_df.iloc[::sample_f,:]