Decode aircraft altitude

    Args:
        msg (string): 28 bytes hexadecimal message string

    Returns:
        int: altitude in feet
505247
def altitude(msg):
    

    tc = typecode(msg)

    if tc<5 or tc==19 or tc>22:
        raise RuntimeError("%s: Not a position message" % msg)

    if tc>=5 and tc<=8:
        # surface position, altitude 0
        return 0

    msgbin = common.hex2bin(msg)
    q = msgbin[47]
    if q:
        n = common.bin2int(msgbin[40:47]+msgbin[48:52])
        alt = n * 25 - 1000
        return alt
    else:
        return None
1099648
def _ka_decode(self, msg):
        """"""
        return {'keypad_areas': [ord(x)-0x31 for x in msg[4:4+Max.KEYPADS.value]]}
353247
def altcode(msg):
    

    if df(msg) not in [0, 4, 16, 20]:
        raise RuntimeError("Message must be Downlink Format 0, 4, 16, or 20.")

    # Altitude code, bit 20-32
    mbin = hex2bin(msg)

    mbit = mbin[25]   # M bit: 26
    qbit = mbin[27]   # Q bit: 28


    if mbit == '0':         # unit in ft
        if qbit == '1':     # 25ft interval
            vbin = mbin[19:25] + mbin[26] + mbin[28:32]
            alt = bin2int(vbin) * 25 - 1000
        if qbit == '0':     # 100ft interval, above 50175ft
            C1 = mbin[19]
            A1 = mbin[20]
            C2 = mbin[21]
            A2 = mbin[22]
            C4 = mbin[23]
            A4 = mbin[24]
            # _ = mbin[25]
            B1 = mbin[26]
            # D1 = mbin[27]     # always zero
            B2 = mbin[28]
            D2 = mbin[29]
            B4 = mbin[30]
            D4 = mbin[31]

            graystr =  D2 + D4 + A1 + A2 + A4 + B1 + B2 + B4 + C1 + C2 + C4
            alt = gray2alt(graystr)

    if mbit == '1':         # unit in meter
        vbin = mbin[19:25] + mbin[26:31]
        alt = int(bin2int(vbin) * 3.28084)  # convert to ft

    return alt
730041
def recv_msg(self):
        ''''''
        if self._index >= self._count:
            return None
        m = self._msgs[self._index]
        type = m.get_type()
        self._index += 1
        self.percent = (100.0 * self._index) / self._count
        self.messages[type] = m
        self._timestamp = m._timestamp

        if self._flightmode_index < len(self._flightmodes):
            (mode, tstamp, t2) = self._flightmodes[self._flightmode_index]
            if m._timestamp >= tstamp:
                self.flightmode = mode
                self._flightmode_index += 1

        self.check_param(m)
        return m
1311801
def _zs_decode(self, msg):
        """"""
        status = [_status_decode(int(x, 16)) for x in msg[4:4+Max.ZONES.value]]
        return {'zone_statuses': status}
69688
def read_altitude(self, sealevel_pa=101325.0):
        """"""
        # Calculation taken straight from section 3.6 of the datasheet.
        pressure = float(self.read_pressure())
        altitude = 44330.0 * (1.0 - pow(pressure / sealevel_pa, (1.0/5.255)))
        self.logger.debug('Altitude {0} m', altitude)
        return altitude
752347
def decode_bridged_message(rx_data):
    

    while array('B', rx_data)[5] == constants.CMDID_SEND_MESSAGE:
        rsp = create_message(constants.NETFN_APP + 1,
                             constants.CMDID_SEND_MESSAGE, None)
        decode_message(rsp, rx_data[6:])
        check_completion_code(rsp.completion_code)
        rx_data = rx_data[7:-1]

        if len(rx_data) < 6:
            break
    return rx_data
487271
def ic45(msg):
    
    d = hex2bin(data(msg))
    if d[9] == '0':
        return None

    ic = bin2int(d[10:12])
    return ic
372278
def _handle_message(self, data):
        

        try:
            data = data.decode('utf-8')
        except:
            raise InvalidMessageError('Decode failed for message: {0}'.format(data))

        if data is not None:
            data = data.lstrip('\0')

        if data is None or data == '':
            raise InvalidMessageError()

        msg = None
        header = data[0:4]

        if header[0] != '!' or header == '!KPM':
            msg = self._handle_keypad_message(data)

        elif header == '!EXP' or header == '!REL':
            msg = self._handle_expander_message(data)

        elif header == '!RFX':
            msg = self._handle_rfx(data)

        elif header == '!LRR':
            msg = self._handle_lrr(data)

        elif header == '!AUI':
            msg = self._handle_aui(data)

        elif data.startswith('!Ready'):
            self.on_boot()

        elif data.startswith('!CONFIG'):
            self._handle_config(data)

        elif data.startswith('!VER'):
            self._handle_version(data)

        elif data.startswith('!Sending'):
            self._handle_sending(data)

        return msg
272937
def wind44(msg):
    
    d = hex2bin(data(msg))

    status = int(d[4])
    if not status:
        return None

    speed = bin2int(d[5:14])   # knots
    direction = bin2int(d[14:23]) * 180.0 / 256.0  # degree

    return round(speed, 0), round(direction, 1)
659340
def _decode_received(self, msg):
        """"""
        if not isinstance(msg, six.binary_type):
            # already decoded - do nothing
            return msg
        # only decode from utf-8 if message is not binary data
        type = six.byte2int(msg[0:1])
        if type >= 48:  # no binary
            return msg.decode('utf-8')
        # binary message, don't try to decode
        return msg
15400
def version(msg):
    
    tc = typecode(msg)

    if tc != 31:
        raise RuntimeError("%s: Not a status operation message, expecting TC = 31" % msg)

    msgbin = common.hex2bin(msg)
    version = common.bin2int(msgbin[72:75])

    return version
256951
def tas53(msg):
    
    d = hex2bin(data(msg))

    if d[33] == '0':
        return None

    tas = bin2int(d[34:46]) * 0.5   # kts
    return round(tas, 1)
1041178
def nic_b(msg):
    
    tc = typecode(msg)

    if tc < 9 or tc > 18:
        raise RuntimeError("%s: Not a airborne position message, expecting 8<TC<19" % msg)

    msgbin = common.hex2bin(msg)
    nic_b = int(msgbin[39])

    return nic_b
668905
def p45(msg):
    
    d = hex2bin(data(msg))
    if d[26] == '0':
        return None
    p = bin2int(d[27:38])    # hPa
    return p
1071208
def _as_decode(self, msg):
        """"""
        return {'armed_statuses': [x for x in msg[4:12]],
                'arm_up_states': [x for x in msg[12:20]],
                'alarm_states': [x for x in msg[20:28]]}
1274942
def ias60(msg):
    
    d = hex2bin(data(msg))

    if d[12] == '0':
        return None

    ias = bin2int(d[13:23])    # kts
    return ias