#!/usr/bin/env python
# vim: tabstop=4:softtabstop=4:shiftwidth=4:expandtab:

# Explanations of various codes found in an ACARS message compiled from
# various sources around the net...
# http://acarsonline.pbworks.com/w/page/1286730/Message%20Labels
# https://github.com/brmlab/acarsdec/blob/master/datasets/acars_mls.txt
# https://raw.githubusercontent.com/gat3way/rtl_acars_ng/master/datasets/acars_mls.txt
# http://www.cosmic.com.au/acarsanalyser/v3/datares/MsgLabelsData.asp

arinc620 = {
    '00': ['Emergency SitRep', 'Hijack Situation Report'],
    '10': ['British Airways ARR (ETA) Report', 'American Airlines/Eagle Frequency Change', 'Fedex ATIS Request Message', 'British Airways FST Report'],
    '11': ['Inrange Arrival Report', 'Fedex In Range Arrival Report'],
    '13': ['CMU loadsheet uplink'],
    '14': ['General Aviation Free Text'],
    '15': ['General Aviation Position Report'],
    '16': ['Fedex Position Report-AUTPOS', 'General Aviation Weather Request'],
    '17': ['Northwest Airlines Position/Weather Report'],
    '18': ['General aviation weather report'],
    '19': ['ATIS report'],
    '1C': ['Flight plan request'],
    '1L': ['Atlantic Coast Airlines/United Express QF OFF message'],
    '1M': ['Estimated Time of Arrival report'],
    '20': ['SAS Initialization'],
    '21': ['SAS Takeoff Data calculation'],
    '23': ['SAS Loadsheet Uplink'],
    '24': ['Northwest Airlines Position/Weather Request'],
    '25': ['SAS Special Load Notification'],
    '26': ['SAS SIGMET message'],
    '27': ['SA Weather Request'],
    '28': ['Weather Request'],
    '29': ['SAS Flow Message'],
    '2S': ['Weather Request'],
    '2U': ['Weather'],
    '30': ['UPS Position Report'],
    '31': ['Northwest Airlines Weather Report'],
    '36': ['Northwest Airlines In Range Message'],
    '39': ['Northwest Airlines arrival/free text message'],
    '3F': ['UPS ETA Downlink Message'],
    '3G': ['UPS Free Text Message Format'],
    '3J': ['UPS Downlink Message'],
    '3S': ['UPS Downlink Message'],
    '3U': ['UPS Uplink acknowledgement'],
    '41': ['SAS Flow Message'],
    '46': ['Pirep'],
    '47': ['747-400 Airway Position Downlink'],
    '48': ['Misc Msgs'],
    '49': ['Air Canada Status Reports'],
    '4A': ['Air Canada latest new format'],
    '4M': ['Cargo Info', 'Air Canada Uplink to Cockpit Printer-specific message'],
    '4P': ['Weather report/forecast'],
    '4Q': ['Air Canada departure format'],
    '4R': ['OFF Air Canada-Specific Message Format'],
    '4S': ['Weather Request'],
    '4T': ['ETA Report'],
    '4X': ['Air Canada Frequency List'],
    '50': ['Possible HFDL Msg router'],
    '51': ['Ground GMT Request/Update'],
    '52': ['Ground UTC Request/Update'],
    '53': ['Reserved Spare'],
    '54': ['ACARS Frequency Uplink', 'Aircrew Initiated Voice Contact Request', 'Voice Go-Ahead'],
    '57': ['Alternate Aircrew Initiated Position Report'],
    '58': ['Aircraft Tracking Control', 'Aircraft Tracking Periodic Position Report'],
    '5D': ['ATIS Request'],
    '5P': ['Temporary Suspension of ACARS Service (reponse to Q6)'],
    '5R': ['Aircraft initiated position report'],
    '5U': ['Downlink Weather Request'],
    '5V': ['VDL Switch Advisory'],
    '5Y': ['ETA Revision', 'Diversion'],
    '5Z': ['Airline designated downlink'],
    '7A': ['Aircraft Initiated Engine Data', 'Takeoff Thrust Report'],
    '7B': ['Misc Aircrew Initiated Msgs'],
    '80': ['Delta/Canadian specific 3C03 position report format', 'Airline Defined'],
    '81': ['Airline Defined'],
    '82': ['Airline Defined'],
    '83': ['Airline Defined'],
    '84': ['S.A.S. Free Text Message', 'Airline Defined'],
    '85': ['Airline Defined'],
    '86': ['Airline Defined'],
    '87': ['Airline Defined'],
    '88': ['Airline Defined'],
    '89': ['Airline Defined'],
    '8A': ['SAS Out report'],
    '8B': ['SAS Off report'],
    '8C': ['SAS On report'],
    '8D': ['SAS In report'],
    '8E': ['SAS Out/return in report'],
    '8G': ['SAS Take off data calculation request'],
    '8H': ['SAS Loadsheet request/confirmation'],
    '8I': ['SAS Flightplan request'],
    '8J': ['SAS Crewlist request'],
    '8X': ['Uplink ATIS Information'],
    '8Z': ['Avionics unable to process data'],
    '99': ['Emergency Situation Report'],
    ':;': ['Command Acft Xcvr change freq'],
    '::': ['Reserved'],
    ':}': ['POA to AOA Retune'],
    'A0': ['ATIS Facilities Notification'],
    'A1': ['Deliver Oceanic Clearance'],
    'A2': ['Unassigned'],
    'A3': ['Deliver Departure Clearance'],
    'A4': ['Acknowledge Departure Clearance'],
    'A5': ['Request Position Report'],
    'A6': ['Request ADS Report'],
    'A7': ['Forward Free Text to Aircraft'],
    'A8': ['Deliver Departure Slot'],
    'A9': ['Deliver ATIS Information'],
    'AA': ['ATC Communications'],
    'AB': ['Terminal Weather Information for Pilots'],
    'AC': ['Pushback Clearance Uplink'],
    'AD': ['Expected Taxi Clearance'],
    'AE': ['Unassigned'],
    'AF': ['CPC Command Response'],
    'AG': ['Unassigned'],
    'AW': ['ATS Wind Data'],
    'B0': ['ATS Facility Notification'],
    'B1': ['Flight Plan Oceanic Clearance Request'],
    'B2': ['Oceanic Clearance Readback'],
    'B3': ['Request Departure Clearance'],
    'B4': ['Acknowledge Departure Clearance'],
    'B5': ['Provide Position Report'],
    'B6': ['Provide ADS Report'],
    'B7': ['Forward Free Text to ADS'],
    'B8': ['Request Departure Slot'],
    'B9': ['Request ATIS information', 'Flight Plan Information Receipt'],
    'BA': ['ATC Communications'],
    'BB': ['Terminal Weather Information for Pilots'],
    'BC': ['Pushback Clearance Request'],
    'BD': ['Expected Taxi Clearance Request'],
    'BE': ['CPC Log-on/Log-off Request'],
    'BF': ['CPC Wilco'],
    'BG': ['Unassigned'],
    'BW': ['ATS Wind Response'],
    'C#': ['Message to Cockpit Printer #'],
    'C0': ['Uplink message to all printers'],
    'C1': ['Uplink message to printer 1'],
    'C2': ['Uplink message to printer 2'],
    'C3': ['Uplink message to printer 3'],
    'CA': ['Printer Error'],
    'CB': ['Printer Busy'],
    'CC': ['Printer Local', 'Printer in Test Mode'],
    'CD': ['Printer out of Paper', 'Printer No Paper'],
    'CE': ['Printer Buffer Overrun'],
    'CF': ['Printer Reserved Status'],
    'DL': ['Reserved for Data Loading'],
    'D' : ['De-Icing Downlinks'],
    'DI': ['De-Icing Uplinks'],
    'EI': ['Internet Email Message'],
    'E1': ['Internet Email Message'],
    'E2': ['Internet'],
    'EL': ['Engine Monitoring Unit Message Downlink'],
    'ER': ['Engine Monitoring Unit Message Downlink'],
    'F3': ['Dedicated Transceiver Advisory'],
    'H1': ['Message to/from terminal', 'Messages from Avionic Subsystem', 'OAT', 'OAX'],
    'H2': ['Meteorological Report'],
    'H3': ['Icing Report'],
    'H4': ['Meteorological Report Configuration Downlink'],
    'HF': ['HF Report'],
    'HX': ['Undelivered Uplink report'],
    'KB': ['Loopback Request/Response'],
    'LB': ['Cabin E-Logbook'],
    'LC': ['Cabin E-Logbook'],
    'LS': ['Technical (Cockpit) E-Logbook'],
    'LT': ['Technical (Cockpit) E-Logbook'],
    'M1': ['IATA Departure Msg', 'IATA Departure Message'],
    'M2': ['IATA Arrival Msg', 'USR', 'User Defined Message'],
    'M3': ['IATA Return to Ramp Message'],
    'M4': ['IATA Return from Airborne Message'],
    'P0': ['AMS-Protected Downlink Message, Test System Destination'],
    'P6': ['Unassigned'],
    'P7': ['AMS-Protected Uplink ATS Message'],
    'P8': ['AMS-Protected Uplink AMS-Specific Message'],
    'P9': ['AMS-Protected Uplink AOC Message'],
    'PA': ['AMS-Protected Downlink ATS Message'],
    'PB': ['AMS-Protected Downlink AMS-Specific Message'],
    'PC': ['AMS-Protected Downlink AOC Message'],
    'Q0': ['ACARS Link Test'],
    'Q1': ['ETA Departure/Arrival Report'],
    'Q2': ['ETA Report'],
    'Q3': ['GMT Clock Update'],
    'Q4': ['Voice Circuit Busy - Reply to 54'],
    'Q5': ['Unable to process uplinked messages'],
    'Q6': ['Voice-to-ACARS change over-Joining ACARS'],
    'Q7': ['Delay message'],
    'Q8': ['Reserved'],
    'Q9': ['Reserved'],
    'QA': ['Out/fuel report', 'Departure Report'],
    'QB': ['Off report'],
    'QC': ['On Report'],
    'QD': ['Arrival Report / In Fuel'],
    'QE': ['Out/fuel destination report'],
    'QF': ['OFF (Off/destination report) / Out Fuel'],
    'QG': ['Returning to Gate Report'],
    'QH': ['Out Report'],
    'QK': ['Landing report'],
    'QL': ['Arrival Report'],
    'QM': ['Arrival Info Report'],
    'QN': ['Diversion Report'],
    'QP': ['OUT report'],
    'QQ': ['OFF report'],
    'QR': ['ON report'],
    'QS': ['IN report'],
    'QT': ['OUT/Return IN report'],
    'QU': ['Unassigned'],
    'QV': ['Autotune Reject'],
    'QW': ['Unassigned'],
    'QX': ['Intercept, airborne subsystem unable to process'],
    'QY': ['Unassigned'],
    'QZ': ['Unassigned'],
    'RA': ['Command Acft terminal to TX data'],
    'RB': ['Response of aircraft terminal to RA uplink message'],
    'RE': ['Refuel'],
    'S1': ['VHF Network Statistics Report'],
    'S2': ['VHF Performance Report'],
    'S3': ['LRU Configuration Report'],
    'SA': ['Media Advisory'],
    'SQ': ['Uplink Squitter'],
    'UP': ['Message Acknowledgement'],
    'WP': ['Potable Water Remote Pre-selection'],
    '_D': ['General Response'],
    '_J': ['No info to TX'],
 }


# Label '5Z' is airline-defined. United Airlines seems to put a
# code at the beginning of the text
united_5z = {
    '/10': 'PIREP',
    '/AI': 'Auto Init',
    '/B1': 'Request Weight and Balance',
    '/B3': 'Request Runway Data',
    '/B6': 'Runway Data Resp',
    '/C11': 'International PIREP',
    '/C3': 'Off message',
    '/C4': 'Flight dispatch',
    '/C5': 'Maintenance Message',
    '/C6': 'Customer Service',
    '/CD': 'Weight and Balance',
    '/CG': 'Request Pre-departure clearance (PDC)',
    '/CM': 'Crew Scheduling',
    '/CS': 'Customer Service',
    '/D3': 'Holding pattern message',
    '/D6': 'From-to +date',
    '/D7': 'From-to+Alternate+Time',
    '/DS': 'Late message',
    '/EO': 'In range',
    '/ET': 'Expire Time',
    '/M4': 'Request Maintenance Required Determination',
    '/PB': 'Request Crew List',
    '/PN': 'Problem Note?',
    '/PW': 'Position weather',
    '/R3': 'Request howgozit message',
    '/R4': 'Request the latest POSBD',
    '/RL': 'Request Release',
    '/TC': 'From-to-fuel',
    '/W1': 'Request weather - City',
    '/WB': 'From-to',
    '/71': 'Maintenance Issues',
}


abbrevs = {
    'ADNS': 'ARINC Data Network Service',
    'AL': 'Flight Level',
    'AML': 'Aircraft Maintenace Log',
    'AO': 'Pax Cnt in zone A (FC)',
    'APRCH': 'Approach',
    'ARINC': 'Aeronautical Radio Inc.',
    'ATIS': 'Automatic Terminal Information Service',
    'CFDIU': 'Combined Flight Data Indicator Unit',
    'CG': 'Center of Gravity',
    'CI': 'Cost Index',
    'CL': 'Cruise Level',
    'CR': 'Company Route',
    'CW': 'Cruise wind',
    'CZ': 'Cruise speed',
    'DA': 'Departure Aerodrome',
    'DFDAU': 'Digital Flight Data Acquisition Unit',
    'DG': 'Drag factor',
    'DP': 'Dew point',
    'DS': 'Destination Station',
    'DX': 'Long Distance',
    'EO': 'Estimated Time Over',
    'ET': 'Estimated Time',
    'FB': 'Amount of fuel boarded',
    'FC': 'Estimated further clearance',
    'FD': 'Fuel over destination',
    'FDR': 'Flight Data Recorder',
    'FF': 'Fuel flow factor (lb/hr)',
    'FI': 'Flight Identification',
    'FL': 'Flight Level',
    'FMC': 'Flight Management Computer',
    'FO': 'First Officer',
    'FOB': 'Fuel On Board',
    'FQY': 'Frequency',
    'FR': 'Fault Report Prefix',
    'FRM': 'Maintenance codes',
    'FX': 'En Route Fix',
    'GL': 'Geographical Location',
    'HD': 'Aircraft Heading',
    'IC': 'Aircraft Icing',
    'IN': 'Time In',
    'IR': 'In VHF range and Instrument Rated',
    'LA': 'Identification of officer landing aircraft',
    'LT': 'Light',
    'MA': 'Message Assurance',
    'MN': 'Maintenance',
    'MRD': "Maintenance Requirements Determination",
    'MT': 'Maximum takeoff power computation',
    'MU': 'Management Unit',
    'N1': 'Speed of Turbine 1',
    'N2': 'Speed of Turbine 2',
    'NA': 'Navigation Aid',
    'NL': 'Number of Landings',
    'NON-FDE': 'Non-critical Faults, Aircraft is Airworthy',
    'NP': 'Next report point',
    'OA': 'Pax Cnt in zone A (FC)',
    'OB': 'Pax Cnt in zone B (Biz)',
    'OC': 'Pax Cnt in zone C (Econ)',
    'OF': 'Time off',
    'ON': 'Time on',
    'OP': 'Oil Pressure',
    'OS': 'Other supplementary information',
    'OT': 'Time out',
    'OT': 'Time out',
    'OV': 'Present Location',
    'PB': 'Persons onboard',
    'PD': 'Point of Departure',
    'PIREP': "Pilot's Report",
    'PLTOKL': 'Internal Addressing within Computer System',
    'PW': 'Position Weather',
    'QN': 'Altimeter Settings',
    'RD': 'Departure Runway',
    'RF': 'Request Flight Level',
    'RGS': 'Remote Ground Station',
    'RI': 'Return In Time',
    'RL': 'Request Cruising Level',
    'RM': 'Remarks',
    'RO': 'Return On Time',
    'RQ': 'Request',
    'RT': 'Route Information',
    'SA': 'Alternative Aerodrome',
    'SI': 'Special communication addressing instruction',
    'SK': 'Sky Conditions',
    'SL': 'SELCAL code',
    'SP': 'Significant Point',
    'ST': 'Standard Takeoff Power',
    'TA': 'Static Air Temperature',
    'TB': 'Turbulence',
    'TD': 'Takeoff Temperature',
    'TEI': 'Text Element Identifiers',
    'TE': 'Time En Route',
    'TM': 'Surface Air Temperature',
    'TO': 'Time Over',
    'TP': 'Transmission Path',
    'TRIM': 'Required Compensation of MAC',
    'V1': 'Velocity 1',
    'V2': 'Velocity 2',
    'VR': 'Rotate',
    'VR': 'Runway Visual Range',
    'WCHR': 'Wheelchair',
    'WL': 'Weather',
    'WP': 'Waypoint',
    'WR': 'Warning',
    'WV': 'Wind Information',
    'WXRQ': 'Weather Request',
    'WX': 'Weather',
    'XPDR': 'Transponder',
    'ZW': 'Zero Fuel Weight',
}


h1types = {
    "A0": "ATS Facility Notification (AFN)",
    "A1": "Oceanic Clearance",
    "A2": "Unassigned",
    "A3": "Departure Clearance Response",
    "A4": "Flight Systems Message",
    "A5": "Assigned",
    "A6": "Request ADS Reports",
    "A7": "Free Text",
    "A8": "Deliver Departure Slot",
    "A9": "ATIS report",
    "AA": "ATCComm",
    "AB": "TWIP Report",
    "AC": "Pushback Clearance",
    "AD": "Expected Taxi Clearance",
    "AE": "Unassigned",
    "AF": "CPC Command/Response",
    "B0": "ATS Facility Notification",
    "B1": "Request Oceanic Clearance",
    "B2": "Oceanic Clearance Readback",
    "B3": "Request Departure Clearance",
    "B4": "Departure Clearance Readback",
    "B5": "Reserved",
    "B6": "Provide ADS Report",
    "B7": "Free Text",
    "B8": "Request Departure Slot",
    "B9": "Request ATIS Report",
    "BA": "ATCComm",
    "BB": "Request TWIP Report",
    "BC": "Pushback Clearance Request",
    "BD": "Expected Taxi Clearance Request",
    "BE": "CPC Aircraft Log-On/Off Request",
    "BF": "CPC WILCO/UNABLE Response",
    "CF": "Central Fault Display",
    "DF": "Digital Flight Data Acquisition Unit",
    "EC": "Engine Display System",
    "EI": "Engine Report",
    "H1": "HF Data Radio - Left",
    "H2": "HF Data Radio - Right",
    "H3": "Icing Report",
    "HD": "HF Data Radio - Selected",
    "M1": "Flight Management Computer - Left",
    "M2": "Flight Management Computer - Right",
    "M3": "Flight Management Computer - Center",
    "MD": "Flight Management Computer - Selected",
    "PS": "Keyboard/Display Unit",
    "S1": "SDU - Left",
    "S2": "SDU - Right",
    "SD": "SDU - Selected",
    "T0": "Cabin Terminal Message (Undefined)",
    "T1": "Cabin Terminal Messages",
    "T2": "Cabin Terminal Messages",
    "T3": "Cabin Terminal Messages",
    "T4": "Cabin Terminal Messages",
    "T5": "Cabin Terminal Messages",
    "T6": "Cabin Terminal Messages",
    "T7": "Cabin Terminal Messages",
    "T8": "Cabin Terminal Messages",
    "WO": "Weather Observation Report"
}
