#!/usr/bin/env python import zipfile import re import string import StringIO import sys class parsed_file: def __init__(self, file, fields): self.file = file self.fields = fields self.rti = fields[fields.keys()[0]]['RTI'] def __iter__(self): return(self) def next(self): line = self.file.readline() if not line: raise StopIteration rti = line[self.rti[0]:self.rti[1]] fields = self.fields[rti] record = {'RTI': rti} for desc in fields.keys(): (start, end, type, just) = fields[desc] record[desc] = string.strip(line[start:end]) return(record) class ATA100zip: format_re = re.compile('^([LR]) +([A-Z]+) *([0-9]+) +([0-9]+) {1,3}([^ ]+)? +([^\n\r]+[^ \n\r])') rti_descs = ('RECORD TYPE INDICATOR.', 'RECORD TYPE INDICATOR', 'RECORD IDENTIFIER') rti_re = re.compile('^ +([A-Z0-9]+)(:| - ) *([A-Z- ]+[^ ]) *\r\n') def __init__(self, collection_filename): self.collection = zipfile.ZipFile(collection_filename, mode='r') self.filenames = self.collection.namelist() self.filenames_lc = map(string.lower, self.filenames) self.fields_d = None def real_filename(self, filename): return(self.filenames[self.filenames_lc.index(string.lower(filename))]) def _open(self, filename): return(StringIO.StringIO(self.collection.read(self.real_filename(filename)))) def fields(self, typename): format_filename = 'subscriber_file_formats/%s_rf.txt' % (typename) #print self.real_filename(format_filename) format_file = self._open(self.real_filename(format_filename)) fields_d = {} rti = None while 1: line = format_file.readline() if not line: break #line = string.strip(line) match = self.format_re.match(line) if not match: continue (just, type, length_s, start_s, element, desc) = match.groups() if desc in self.rti_descs: while 1: rti_line = format_file.readline() rti_match = self.rti_re.match(rti_line) if rti_match: (rti, sep, rti_desc) = rti_match.groups() fields_d[rti] = {} desc = 'RTI' # Standardize this a bit. break start = int(start_s) - 1 end = start + int(length_s) suffix = '' while fields_d[rti].has_key(desc + suffix): if suffix == '': suffix = '-A' else: suffix = '-' + '%c' % (ord(suffix[-1]) + 1) desc += suffix fields_d[rti][desc] = (start, end, type, just) return(fields_d) def parse(self, typename): return(parsed_file( self._open('%s.txt' % (typename)), self.fields(typename), )) def seconds_to_degrees(seconds): if seconds[-1] in ['S', 'W']: return(-float(seconds[:-1])/3600.0) else: return(float(seconds[:-1])/3600.0) if __name__ == '__main__': args = sys.argv if not len(args) == 2: print 'usage: %s [ATA-100 zip filename]' % (args[0]) sys.exit(1) data = ATA100zip(args[1]) parsed_data = data.parse('APT') for record_type in parsed_data.fields.keys(): print record_type for field_name in parsed_data.fields[record_type]: print '\t' + field_name interesting_airports = ['LAF', 'BMG'] interesting_entry = 0 for record in parsed_data: rti = record['RTI'] if rti == 'APT': ident = record['LOCATION IDENTIFIER'] if not ident in interesting_airports: interesting_entry = 0 continue interesting_entry = 1 print ident, print record['LOCATION IDENTIFIER'], print record['ASSOCIATED CITY NAME'], print record['ASSOCIATED STATE POST OFFICE CODE'], print seconds_to_degrees(record['AIRPORT REFERENCE POINT LONGITUDE (SECONDS)']), print seconds_to_degrees(record['AIRPORT REFERENCE POINT LATITUDE (SECONDS)']) elif not interesting_entry: continue elif rti == 'RWY': print '\t', print record['RUNWAY IDENTIFICATION'], print record['PHYSICAL RUNWAY LENGTH (NEAREST FOOT)'], print record['PHYSICAL RUNWAY WIDTH (NEAREST FOOT)']