Import one result file as produced by the SIP256c SIP measuring device
    (Radic Research)

    Full settings dictionary: ::

        settings = {
            'filter_skip': (integer) skip dipoles we are interested in
            'quadrupole_mode': ['after'|'between'|'before'| 'all']
                               which dipoles to use from the file
        }


    Parameters
    ----------
    filename: string
        input filename, usually with the ending ".RES"
    settings: dict
        Settings for the data import, see code snippet above
    selection_mode: dict
        which voltage dipoles should be returned. Possible choices:
        "all"|"before"|"after"
    reciprocal: int|None
        If this is an integer, then assume this was a reciprocal measurement
        and the number denotes the largest RU number, N. Electrode numbers
        (a,b,m,n) will then be transformed to (N1 - a, N1 - b, N1 - m, N1 - n),
        with N1 = N + 1

    Returns
    -------
    sip_data: :py:pandas:`pandas.DataFrame`
        The data contained in a data frame
    electrodes : None
        No electrode positions are imported
    topography : None
        No topography is imported

def parse_radic_file(filename, settings, selection_mode="after",
                     reciprocal=None):
    """Import one result file as produced by the SIP256c SIP measuring device
    (Radic Research)

    Full settings dictionary: ::

        settings = {
            'filter_skip': (integer) skip dipoles we are interested in
            'quadrupole_mode': ['after'|'between'|'before'| 'all']
                               which dipoles to use from the file
        }


    Parameters
    ----------
    filename: string
        input filename, usually with the ending ".RES"
    settings: dict
        Settings for the data import, see code snippet above
    selection_mode: dict
        which voltage dipoles should be returned. Possible choices:
        "all"|"before"|"after"
    reciprocal: int|None
        If this is an integer, then assume this was a reciprocal measurement
        and the number denotes the largest RU number, N. Electrode numbers
        (a,b,m,n) will then be transformed to (N1 - a, N1 - b, N1 - m, N1 - n),
        with N1 = N + 1

    Returns
    -------
    sip_data: :py:pandas:`pandas.DataFrame`
        The data contained in a data frame
    electrodes : None
        No electrode positions are imported
    topography : None
        No topography is imported

    """
    try:
        with open(filename, 'r', encoding='latin-1') as fid:
            lines = fid.readlines()
    except:
        print('file not found', filename)
        import pdb
        pdb.set_trace()

    groups = itertools.groupby(
        lines,
        lambda line: line.startswith('Reading:')
    )

    # parse header
    group = next(groups)
    header_data = _parse_radic_header(group, dipole_mode='between')

    # parse readings
    reading_blocks = {}
    for key, group in groups:
        # determine reading number
        line = next(group)
        reading_nr = int(line[8: line.find('/')].strip())
        # print('reading nr', reading_nr)
        reading_blocks[reading_nr] = [x for x in next(groups)[1]]
        # print reading_blocks[reading_nr]

    # print(sorted(reading_blocks.keys()))
    # now parse the readings
    print('number of readings', len(reading_blocks))
    print('keys', sorted(reading_blocks.keys()))
    readings = {}
    for key in sorted(reading_blocks):
        # print('KEY/Reading', key)
        reading = reading_blocks[key]
        tmp = parse_reading(reading)
        # except Exception as e:
        #     print('Parsing of reading failed')
        #     print(''.join(reading))
        #     print('error message')
        #     print(e)
        #     exit()
        readings[key] = tmp
    # print('reading keys', sorted(readings.keys()))

    logging.debug('removing calibration reading')
    # remove calibration reading
    if 0 in readings:
        del(readings[0])

    # print('readings', readings)
    sip_data_raw = compute_quadrupoles(header_data, readings, settings)

    sip_data = pd.concat(sip_data_raw)

    if reciprocal is not None and isinstance(reciprocal, int):
        sip_data['a'] = (reciprocal + 1) - sip_data['a']
        sip_data['b'] = (reciprocal + 1) - sip_data['b']
        sip_data['m'] = (reciprocal + 1) - sip_data['m']
        sip_data['n'] = (reciprocal + 1) - sip_data['n']

    return sip_data, None, None