def _multicolumn_file_load(fname):  #MR31102017
    with open(fname, 'r') as f:
        header = f.readline().split(',')
        header = [x.replace('#', '').strip() for x in header]
    data = uti_io.read_ascii_data_cols(fname,
                                       '\t',
                                       _i_col_start=0,
                                       _i_col_end=-1,
                                       _n_line_skip=1)
    d = {}
    for i, k in enumerate(header):
        k_no_units = k.split('[')[0].strip()
        units = k.split('[')[1].split(']')[0].strip()
        d[k_no_units] = {
            'data': data[i],
            'label': k,
            'units': units,
        }
    # data, mode, allrange, arLabels, arUnits
    return d, None, [], [], []
def _traj_file_load(fname, traj_axis='x'):  #MR20160725
    nLinesHead = 1
    hlp = []
    with open(fname, 'r') as f:
        for i in range(nLinesHead):
            hlp.append(f.readline())

    data = uti_io.read_ascii_data_cols(fname,
                                       '\t',
                                       _i_col_start=0,
                                       _i_col_end=10,
                                       _n_line_skip=nLinesHead)

    mode = 3
    allrange = [
        data[5][0],  # z-coordinate begin
        data[5][-1],  # z-coordinate end
        len(data[0]),  # number of points
        min(data[1]),  # x-coordinate begin
        max(data[1]),  # x-coordinate end
        1,
        min(data[3]),  # y-coordinate begin
        max(data[3]),  # y-coordinate end
        1,
    ]
    arLabels = [
        'Longitudinal Position', 'Horizontal Position', 'Vertical Position'
    ]
    arUnits = ['m', 'm', 'm', 'm']
    if traj_axis == 'x':
        data = data[1]
        arLabels.append('Horizontal Coordinate')
    elif traj_axis == 'y':
        data = data[3]
        arLabels.append('Vertical Coordinate')
    else:
        raise ValueError(
            'Parameter "axis" has wrong value: {}. Allowed values are "x" and "y"'
            .format(traj_axis))

    return data, mode, allrange, arLabels, arUnits
def _file_load(_fname, _read_labels=1):  #MR20160725
    nLinesHead = 11
    hlp = []
    #with open(_fname,'r') as f: hlp = f.readlines(nLinesHead)
    with open(_fname, 'r') as f:
        for i in range(nLinesHead):
            hlp.append(f.readline())

    #ne, nx, ny, ns    = [   int(hlp[i].replace('#','').split()[0]) for i in [3,6,9,10] ]
    ne, nx, ny = [int(hlp[i].replace('#', '').split()[0]) for i in [3, 6, 9]]
    ns = 1
    testStr = hlp[nLinesHead - 1]
    if testStr[0] == '#':
        ns = int(testStr.replace('#', '').split()[0])
    else:
        nLinesHead -= 1

    e0, e1, x0, x1, y0, y1 = [
        float(hlp[i].replace('#', '').split()[0]) for i in [1, 2, 4, 5, 7, 8]
    ]

    #data = np.squeeze(np.loadtxt(_fname, dtype=np.float64)) #get data from file (C-aligned flat)
    data = uti_io.read_ascii_data_cols(_fname,
                                       '\t',
                                       _i_col_start=0,
                                       _i_col_end=0,
                                       _n_line_skip=nLinesHead)[0]

    allrange = e0, e1, ne, x0, x1, nx, y0, y1, ny

    arLabels = [
        'Photon Energy', 'Horizontal Position', 'Vertical Position',
        'Intensity'
    ]
    arUnits = ['eV', 'm', 'm', 'ph/s/.1%bw/mm^2']

    if _read_labels:

        arTokens = hlp[0].split(' [')
        arLabels[3] = arTokens[0].replace('#', '')
        arUnits[3] = ''
        if len(arTokens) > 1:
            arUnits[3] = arTokens[1].split('] ')[0]
        #print(arLabels[3], arUnits[3])

        for i in range(3):
            arTokens = hlp[i * 3 + 1].split()
            nTokens = len(arTokens)
            nTokensLabel = nTokens - 3
            nTokensLabel_mi_1 = nTokensLabel - 1
            strLabel = ''
            for j in range(nTokensLabel):
                strLabel += arTokens[j + 2]
                if j < nTokensLabel_mi_1: strLabel += ' '
            arLabels[i] = strLabel
            arUnits[i] = arTokens[nTokens - 1].replace('[',
                                                       '').replace(']', '')

    m = _enum('T', 'V', 'H', 'E', 'HV', 'EV', 'EH', 'EHV')

    if ne == 1 and nx == 1 and ny == 1: mode = m.T
    if ne == 1 and nx == 1 and ny > 1: mode = m.V
    if ne == 1 and nx > 1 and ny == 1: mode = m.H
    if ne > 1 and nx == 1 and ny == 1: mode = m.E
    if ne == 1 and nx > 1 and ny > 1: mode = m.HV
    if ne > 1 and nx == 1 and ny > 1: mode = m.EV
    if ne > 1 and nx > 1 and ny == 1: mode = m.EH
    if ne > 1 and nx > 1 and ny > 1: mode = m.EHV

    #print(mode)
    return data, mode, allrange, arLabels, arUnits