From a8cf3da7452c60ff2984ddfcc9d1d8dee1dcfe11 Mon Sep 17 00:00:00 2001 From: nzkarit Date: Wed, 23 May 2018 17:14:17 +1200 Subject: [PATCH] FR24 CSV import support complete --- ADSB_Encoder.py | 3 +++ FR24csv.py | 34 ++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/ADSB_Encoder.py b/ADSB_Encoder.py index 637f295..3bacd2e 100755 --- a/ADSB_Encoder.py +++ b/ADSB_Encoder.py @@ -105,8 +105,10 @@ def manyPlanes(arguments): row['longitude'] = float(row['longitude']) if not 'altitude' in row.keys(): row['altitude'] = arguments.altitude + print('here') else: row['altitude'] = float(row['altitude']) + print('there') if not 'capability' in row.keys(): row['capability'] = arguments.capability if not 'typecode' in row.keys(): @@ -121,6 +123,7 @@ def manyPlanes(arguments): row['surface'] = arguments.surface logger.debug('Row from CSV: %s' % (row)) modes = ModeS() + print(row['altitude']) (df17_even, df17_odd) = modes.df17_pos_rep_encode(row['capability'], row['icao'], row['typecode'], row['surveillancestatus'], row['nicsupplementb'], row['altitude'], row['time'], row['latitude'], row['longitude'], row['surface']) ppm = PPM() diff --git a/FR24csv.py b/FR24csv.py index 0856f45..d4bb15a 100644 --- a/FR24csv.py +++ b/FR24csv.py @@ -17,6 +17,16 @@ def argParser(): parser.add_argument('--csv', '--csvfile', '--in', '--input', action='store', type=str, dest='csvfile', help='The name of the FR24 CSV file', required=True) return parser.parse_args() +def reverseCSV(csvfile): + """Reverse a CSV. Returns a dictionary of the CSV""" + data = [] + with open(csvfile, newline='') as csvfilein: + reader = csv.DictReader(csvfilein, delimiter=',') + for row in reader: + data.append(row) + csvfilein.close() + return reversed(data) + def main(): global cfg cfg = configparser.ConfigParser() @@ -27,19 +37,19 @@ def main(): csvFilename = 'fr24.csv' time = 0 - + #Need to reverse the FR24 CSV as it is in reverse order i.e. the most recent record is row 2 and the first ADS-B message of the flight is the last row in the CSV + data = reverseCSV(arguments.csvfile) with open(csvFilename, 'w', newline='') as csvfileout: - output = csv.writer(csvfileout, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) - output.writerow(['time', 'icao', 'latitude', 'longitude', 'altitude']) - with open(arguments.csvfile, newline='') as csvfilein: - reader = csv.DictReader(csvfilein, delimiter=',') - for row in reader: - if time == 0: - time = int(row['Timestamp']) - rowtime = int(row['Timestamp']) - time - newrow = [rowtime, hex(arguments.icao), 'lat', 'long', row['Altitude']] - output.writerow([newrow]) - print(newrow) + fieldnames = ['timestamp', 'icao', 'latitude', 'longitude', 'altitude'] + output = csv.DictWriter(csvfileout, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL, fieldnames=fieldnames) + output.writeheader() + for row in data: + if time == 0: + time = int(row['Timestamp']) + rowtime = int(row['Timestamp']) - time + position = row['Position'].split(',') + newrow = {'timestamp':rowtime, 'icao':hex(arguments.icao), 'latitude':position[0], 'longitude':position[1], 'altitude':row['Altitude']} + output.writerow(newrow) csvfileout.close()