Sunday 5 May 2019

Parsing NASR data: NAV file


Let's start with defining the fields that values we want to extract from data file. Notice that this for the time being, only some basic data we want to extract so, dictionary contains only one 'subdictionary' NAV1. For more information refer to file description.

 navaid_fields = {'NAV1': {'NAVAID_FAC_TYPE': (20, 9),
                          'NAVAID_NAME': (30, 43),
                          'NAVAID_LAT': (14, 372),
                          'NAVAID_LON': (14, 397),
                          'MAG_VAR': (5, 480),
                          'MAG_VAR_EPOCH_YEAR': (4, 485),
                          'CHANNEL': (4, 530),
                          'FREQUENCY': (6, 534)}
                 }


The class that parses NAV data file is similar to those described in the previous post, so I am not going to explain it in details:


class NAVDataParser(NASRLineBaseParser):
    def __init__(self, fields_dict):
        NASRLineBaseParser.__init__(self, fields_dict)
        NAVDataParser.extracted_data['NAVAID_LAT_DD'] = ''
        NAVDataParser.extracted_data['NAVAID_LON_DD'] = ''

    def data2csv(self, raw_line, writer):
        rec_type = raw_line[0:4]  # Check what is record type
        if rec_type == 'NAV1':
            NAVDataParser.extracted_data = self.parse_line(raw_line, navaid_fields['NAV1'])

            navaid_lat_dd = dmsh_hyphens_delimited2dd(NAVDataParser.extracted_data.get('NAVAID_LAT'))
            navaid_lon_dd = dmsh_hyphens_delimited2dd(NAVDataParser.extracted_data.get('NAVAID_LON'))

            NAVDataParser.extracted_data['NAVAID_LAT_DD'] = navaid_lat_dd
            NAVDataParser.extracted_data['NAVAID_LON_DD'] = navaid_lon_dd

            writer.writerow(NAVDataParser.extracted_data)

And, finally to saving extracted data to CSV file:


def navaid2csv(input_file, output_file):
    navaid_data = NAVDataParser(navaid_fields['NAV1'])

    fields = [key for key in navaid_data.extracted_data]

    with open(input_file, 'r') as nasr_file:
        with open(output_file, 'w', newline='') as result_file:
            writer = csv.DictWriter(result_file, fieldnames=fields, delimiter=';')
            writer.writeheader()

            for line in nasr_file:
                navaid_data.data2csv(line, writer)



No comments:

Post a Comment