Read header info of the windaq file

def _rdheader(fp):
    """
    Read header info of the windaq file
    """
    tag = None
    # The '2' tag indicates the end of tags.
    while tag != 2:
        # For each header element, there is a tag indicating data type,
        # followed by the data size, followed by the data itself. 0's
        # pad the content to the nearest 4 bytes. If data_len=0, no pad.
        tag = struct.unpack('>H', fp.read(2))[0]
        data_size = struct.unpack('>H', fp.read(2))[0]
        pad_len = (4 - (data_size % 4)) % 4
        pos = fp.tell()
        # Currently, most tags will be ignored...
        # storage method
        if tag == 1001:
            storage_method = fs = struct.unpack('B', fp.read(1))[0]
            storage_method = {0:'recording', 1:'manual', 2:'online'}[storage_method]
        # fs, unit16
        elif tag == 1003:
            fs = struct.unpack('>H', fp.read(2))[0]
        # sensor type
        elif tag == 1007:
            # Each byte contains information for one channel
            n_sig = data_size
            channel_data = struct.unpack('>%dB' % data_size, fp.read(data_size))
            # The documentation states: "0 : Channel is not used"
            # This means the samples are NOT saved.
            channel_map = ((1, 1, 'emg'),
                           (15, 30, 'goniometer'), (31, 46, 'accelerometer'),
                           (47, 62, 'inclinometer'),
                           (63, 78, 'polar_interface'), (79, 94, 'ecg'),
                           (95, 110, 'torque'), (111, 126, 'gyrometer'),
                           (127, 142, 'sensor'))
            sig_name = []
            # The number range that the data lies between gives the
            # channel
            for data in channel_data:
                # Default case if byte value falls outside of channel map
                base_name = 'unknown'
                # Unused channel
                if data == 0:
                    n_sig -= 1
                    break
                for item in channel_map:
                    if item[0] <= data <= item[1]:
                        base_name = item[2]
                        break
                existing_count = [base_name in name for name in sig_name].count(True)
                sig_name.append('%s_%d' % (base_name, existing_count))
        # Display scale. Probably not useful.
        elif tag == 1009:
            # 100, 500, 1000, 2500, or 8500uV
            display_scale = struct.unpack('>I', fp.read(4))[0]
        # sample format, uint8
        elif tag == 3:
            sample_fmt = struct.unpack('B', fp.read(1))[0]
            is_signed = bool(sample_fmt >> 7)
            # ie. 8 or 16 bits
            bit_width = sample_fmt & 127
        # Measurement start time - seconds from 1.1.1970 UTC
        elif tag == 101:
            n_seconds = struct.unpack('>I', fp.read(4))[0]
            base_datetime = datetime.datetime.utcfromtimestamp(n_seconds)
            base_date = base_datetime.date()
            base_time = base_datetime.time()
        # Measurement start time - minutes from UTC
        elif tag == 102:
            n_minutes = struct.unpack('>h', fp.read(2))[0]
        # Go to the next tag
        fp.seek(pos + data_size + pad_len)
    header_size = fp.tell()
    # For interpreting the waveforms
    fields = {'fs':fs, 'n_sig':n_sig, 'sig_name':sig_name,
              'base_time':base_time, 'base_date':base_date}
    # For reading the signal samples
    file_fields = {'header_size':header_size, 'n_sig':n_sig,
                   'bit_width':bit_width, 'is_signed':is_signed}
    return fields, file_fields