Here’s a Python script that retrieves all the fields in the JSON file and outputs to tab-delimited files. Comment out the uninteresting columns.
# Dump1090.py
# Python program to retrieve JSON records from Dump1090 and write them to a file
# Mark Moerman - March 2021
#
# Requires Dictor from https://pypi.org/project/dictor/
#
import json
import urllib.request
import time
import datetime
import csv
import math
from dictor import dictor
from math import radians, sin, cos, sqrt, asin
#
# Download URL for raw json object - change to your local network address
url = "http://192.168.12.132/dump1090-fa/data/aircraft.json"
#
def haversine(lat1, lon1, lat2, lon2):
R = 6372.8 # Earth radius in kilometers
dLat = radians(lat2 - lat1)
dLon = radians(lon2 - lon1)
lat1 = radians(lat1)
lat2 = radians(lat2)
a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2
c = 2 * asin(sqrt(a))
return R * c
#
def truncate(number, decimals=0):
"""
Returns a value truncated to a specific number of decimal places.
"""
if not isinstance(decimals, int):
raise TypeError("decimal places must be an integer.")
elif decimals < 0:
raise ValueError("decimal places has to be 0 or more.")
elif decimals == 0:
return math.trunc(number)
#
factor = 10.0 ** decimals
return math.trunc(number * factor) / factor
#
aircraftCount = 10000000 # A large number to force initial file write
homeLat = 42.5048833 # Used for calculating distance from your home to the aircraft
homeLon = -96.4609833
#
while True:
data = urllib.request.urlopen(url).read().decode()
obj = json.loads(data)
obj_timestamp = obj['now']
for item in obj['aircraft']:
# Change this number of seconds since the aircraft was last seen to reduce stale records
# Aircraft can stay in the JSON file for up to 300 seconds after the last time it was seen
# It should be around one second more than the sleep time between successive reads of the JSON file
if item['seen'] < 2.1:
if aircraftCount > 128000: # Change this number to the approximate number of lines you would like in a file
aircraftCount = 0
# Create an output file and write a heading line
outputFile = 'R:/Dump1090/Logs/All-' + str(int(obj_timestamp)) + '.txt' # Yes, I'm a Windows person
dump1090log = open(outputFile, mode='w', newline='')
dump1090log_writer = csv.writer(dump1090log,delimiter='\t')
dump1090log_writer.writerow([\
'timestamp',\
'iso_time',\
'hex',\
'flight',\
'alt_baro',\
'alt_geom',\
'gs',\
'ias',\
'tas',\
'mach',\
'track',\
'track_rate',\
'roll',\
'mag_heading',\
'true_heading',\
'baro_rate',\
'geom_rate',\
'squawk',\
'emergency',\
'category',\
'nav_qnh',\
'nav_altitude_mcp',\
'nav_altitude_fms',\
'nav_heading',\
'nav_modes',\
'lat',\
'lon',\
'nic',\
'rc',\
'seen_pos',\
'version',\
'nic_baro',\
'nac_p',\
'nac_v',\
'sil',\
'sil_type',\
'gva',\
'sda',\
'mlat',\
'mlatProvided',\
'tisb',\
'tisbProvided',\
'messages',\
'seen',\
'rssi',\
'distance'])
#
lastSeenPos = float(dictor(item, 'seen_pos', default='-1'))
# Evaluate the number of seconds since the aircraft provided a new position
# Because seen_pos from the JSON file is not a required field, the default for lastSeenPos is set to -1
# Excluding the -1 records keeps positionless messages out of the log
# Setting the left side of the evaluation to (lastSeenPos >= -2) will include these messages
# Setting the right side of the evaluation to around one second more than the sleep time reduces the number of new messages without position updates
if (lastSeenPos >= 0) and (lastSeenPos < 2.1):
# Added shorthand fields to indicate whether MLAT or TISB data was provided
if not item['mlat']:
mlatProvided = 'F'
else:
mlatProvided = 'T'
if not item['tisb']:
tisbProvided = 'F'
else:
tisbProvided = 'T'
# Added field that provides human readable timestamps from the UNIX epoch
ISOtime = datetime.datetime.fromtimestamp(obj['now']).isoformat("T", "milliseconds")
#
aircraftLat = float(dictor(item, 'lat', default='-1'))
aircraftLon = float(dictor(item, 'lon', default='-1'))
# Added field to calculate the aircraft distance from your home lat and lon
if aircraftLat != -1:
aircraftDistance = truncate((float(haversine(homeLat, homeLon,aircraftLat, aircraftLon)) / 1.609344), 1)
else:
aircraftDistance = -1
#
altitudeBaro = dictor(item, 'alt_baro', default='')
if altitudeBaro == 'ground':
altitudeBaro = '0'
#
dump1090log_writer.writerow([\
obj_timestamp,\
ISOtime,\
dictor(item, 'hex', default='FFFFFF'),\
dictor(item, 'flight', default=''),\
altitudeBaro,\
dictor(item, 'alt_geom', default=''),\
dictor(item, 'gs', default=''),\
dictor(item, 'ias', default=''),\
dictor(item, 'tas', default=''),\
dictor(item, 'mach', default=''),\
dictor(item, 'track', default=''),\
dictor(item, 'track_rate', default=''),\
dictor(item, 'roll', default=''),\
dictor(item, 'mag_heading', default=''),\
dictor(item, 'true_heading', default=''),\
dictor(item, 'baro_rate', default=''),\
dictor(item, 'geom_rate', default=''),\
dictor(item, 'squawk', default=''),\
dictor(item, 'emergency', default=''),\
dictor(item, 'category', default=''),\
dictor(item, 'nav_qnh', default=''),\
dictor(item, 'nav_altitude_mcp', default=''),\
dictor(item, 'nav_altitude_fms', default=''),\
dictor(item, 'nav_heading', default=''),\
dictor(item, 'nav_modes', default=''),\
dictor(item, 'lat', default=''),\
dictor(item, 'lon', default=''),\
dictor(item, 'nic', default=''),\
dictor(item, 'rc', default=''),\
dictor(item, 'seen_pos', default='-1'),\
dictor(item, 'version', default=''),\
dictor(item, 'nic_baro', default=''),\
dictor(item, 'nac_p', default=''),\
dictor(item, 'nac_v', default=''),\
dictor(item, 'sil', default=''),\
dictor(item, 'sil_type', default=''),\
dictor(item, 'gva', default=''),\
dictor(item, 'sda', default=''),\
dictor(item, 'mlat', default='NONE'),\
mlatProvided,\
dictor(item, 'tisb', default='NONE'),\
tisbProvided,\
dictor(item, 'messages', default=''),\
dictor(item, 'seen', default=''),\
dictor(item, 'rssi', default=''),\
aircraftDistance])
aircraftCount = aircraftCount + 1
time.sleep(1.1) # Change the number of seconds until the next retrieval of the JSON file
```