def splitList(dbconn, start_time, end_time, hex_or_flight, date, debug, reporter,
              logToDB, printJSON, quiet, numRecs):
    orderSql = "order by %s, report_epoch" % hex_or_flight
    cur = pr.queryReportsDB(dbconn, myStartTime=start_time, myEndTime=end_time,
                         postSql=orderSql, printQuery=debug, myReporter=reporter)
    data = pr.readReportsDB(cur, numRecs)
    oldplane = None
    eventlist = []
    #
    # Split up into a separate list for each flight
    #
    while data:
        for plane in data:
            if not oldplane or getattr(oldplane, hex_or_flight) != getattr(plane, hex_or_flight):
                if oldplane:
                    pl = buildRec(eventlist, dbconn, hex_or_flight, date)
                    if not quiet:
                        if printJSON:
                            print(pl.to_JSON())
                        else:
                            first_ts = time.strftime("%Y-%m-%d %H:%M:%S",
                                                     time.localtime(pl.time_first_seen))
                            last_ts = time.strftime("%Y-%m-%d %H:%M:%S",
                                                    time.localtime(pl.time_last_seen))
                            print(getattr(pl, hex_or_flight), " seen between ", first_ts,
                                  " and ", last_ts)
                    if logToDB:
                        pl.logToDB(dbconn, debug)
                        dbconn.commit()
                eventlist = []
                eventlist.append(plane)
                oldplane = plane
            else:
                eventlist.append(plane)
        data = pr.readReportsDB(cur, numRecs)

    if eventlist:
        pl = buildRec(eventlist, dbconn, hex_or_flight, date)
        if not quiet:
            if printJSON:
                print(pl.to_JSON())
            else:
                first_ts = time.strftime("%Y-%m-%d %H:%M:%S",
                                         time.localtime(pl.time_first_seen))
                last_ts = time.strftime("%Y-%m-%d %H:%M:%S",
                                        time.localtime(pl.time_last_seen))
                print(getattr(pl, hex_or_flight), " seen between ", first_ts,
                      " and ", last_ts)
        if logToDB:
            pl.logToDB(dbconn, debug)
            dbconn.commit()
def readReporterFromFile(inputfile):
    """
    Read Reporter  from a handbuilt text file

    Args:
        inputfile: Pathname of file

    Returns:
        An Reporter object

        Very basic - no error checking whatsoever! File format is:
            Line 1: Name of reporter (no more than 10 chars)
            Line 2: reporter type piaware or mutability, although this field isn't used by anything yet
            Line 3: Lat/lon of reporter location, comma separated.
            Line 4: URL to access the reporter, e.g. http://planereporter/dump1090/data/aircraft.json

    """
    reporter = {}
    name = inputfile.readline().strip('\n')
    mytype = inputfile.readline().strip('\n')
    coords = inputfile.readline().split(",")
    lat = float(coords[0].strip())
    lon = float(coords[1].strip())
    url = inputfile.readline().strip('\n')
    reporter = pr.Reporter(name=name, mytype=mytype, lat=lat, lon=lon,
                           url=url, location="")

    return reporter
def buildRec(eventlist, dbconn, hex_or_flight, date):
    firstpl = eventlist[0]
    lastpl = eventlist[-1]
    if hex_or_flight == 'hex':
        daily_plane_seen = pr.DailyPlanesSeen(date_seen=date, hex=firstpl.hex,
                                              time_first_seen=firstpl.time,
                                              time_last_seen=lastpl.time,
                                              reporter=firstpl.reporter)
        return daily_plane_seen

    if hex_or_flight == 'flight':
        daily_flight_seen = pr.DailyFlightsSeen(date_seen=date, flight=firstpl.flight,
                                              time_first_seen=firstpl.time,
                                              time_last_seen=lastpl.time,
                                              reporter=firstpl.reporter)
        return daily_flight_seen

    return None
def analyseList(eventlist, dbconn, airport, logToDB=False, debug=False, printJSON=False, quiet=False):
    """
    Attempts to determine if event was a takeoff or landing

    Args:
        eventlist: A list of planereports ordered by time
        dbconn: a psycopg2 connection to a Postgres DB. Will be used to log
        events.
        logToDB: boolean to trigger logging events to a DB
        debug: Boolean for printing debug statements

    Returns:
        Nothing
    """
    firstplane = eventlist[0]
    lastplane = eventlist[-1]
    if firstplane.altitude >= lastplane.altitude:
        event = "landed at "
    elif firstplane.altitude < lastplane.altitude:
        event = "took off at"
    elif firstplane.vert_rate < -200 and lastplane.vert_rate > 100:
        if not quiet:
            print("Starting vert_rate", firstplane.vert_rate, "altitude",
                  firstplane.altitude, firstplane.speed, "at",
                  time.strftime("%F %H:%M:%S", time.localtime(firstplane.time)),
                  "ending vert_rate", lastplane.vert_rate, "altitude",
                  lastplane.altitude, lastplane.speed, "at",
                  time.strftime("%F %H:%M:%S", time.localtime(lastplane.time)))
        event = "bump and go"
    else:
        event = "dunno what to call this"

    airport_event = pr.AirportDailyEvents(airport=airport,
                                          event_time=lastplane.time,
                                          type_of_event=event[0],
                                          flight=lastplane.flight, hex=lastplane.hex)
    if not quiet:
        if printJSON:
            print(airport_event.to_JSON())
        else:
            print("Plane", lastplane.hex, "as flight", lastplane.flight, event,
                  time.strftime("%F %H:%M:%S", time.localtime(lastplane.time)))


    if logToDB:
        airport_event.logToDB(dbconn, printQuery=debug)
        dbconn.commit()
    '--track-plane',
    dest='track_plane',
    action="store_true",
    help=
    "Go through each of a plane's position reports, and toss out the ones that are \
                    not at a sensible distance from the reports on either side",
    default=False)

args = parser.parse_args()

if not args.db_conf:
    print("A valid URL db configuration file is needed!")
    exit(-1)
else:
    yesterday = datetime.date.today() - timedelta(1)
    dbconn = pr.connDB(args.db_conf)
    reporter = pr.readReporter(dbconn, args.reporter)
    if not args.start_time:
        args.start_time = yesterday.strftime("%F") + " 00:00:00"
    if not args.end_time:
        args.end_time = yesterday.strftime("%F") + " 23:59:59"

    postSql = " or altitude < 0 or speed > %s " % args.maxSpeed
    cur = pr.queryReportsDB(dbconn,
                            myhex=args.hexcodes,
                            myStartTime=args.start_time,
                            myEndTime=args.end_time,
                            myflight=args.flights,
                            minDistance=args.minDistance,
                            maxDistance=args.maxDistance,
                            myReporter=args.reporter,
def analyseList(eventlist,
                dbconn,
                airport,
                runway,
                logToDB=False,
                debug=False,
                printJSON=False,
                quiet=False):
    """
    Attempts to determine if event was a takeoff or landing

    Args:
        eventlist: A list of planereports ordered by time
        dbconn: a psycopg2 connection to a Postgres DB. Will be used to log
        events.
        logToDB: boolean to trigger logging events to a DB
        debug: Boolean for printing debug statements

    Returns:
        Nothing
    """
    firstplane = eventlist[0]
    lastplane = eventlist[-1]
    middleplane = eventlist[int(len(eventlist) / 2)]
    #
    # Are we actually using this runway, or are we crossing it?
    # Use runway heading and middleplane heading to find out.
    #

    otherheading = runway.heading - 180
    if otherheading < 0:
        otherheading += 360
    if not checkbearing(middleplane.track,
                        runway.heading, 4) and not checkbearing(
                            middleplane.track, otherheading, 4):
        return
    if firstplane.altitude >= lastplane.altitude:
        event = "landed at"
    elif firstplane.altitude < lastplane.altitude:
        event = "took off at"
    else:
        event = "dunno what to call this"

    airport_event = pr.AirportDailyEvents(airport=airport,
                                          event_time=lastplane.time,
                                          type_of_event=event[0],
                                          flight=lastplane.flight,
                                          hex=lastplane.hex,
                                          runway=runway.name)

    if not quiet:
        if printJSON:
            print(airport_event.to_JSON())
        else:
            print("Plane", lastplane.hex, "as flight", lastplane.flight, event,
                  time.strftime("%F %H:%M:%S", time.localtime(lastplane.time)),
                  "on runway", runway.name)

    if logToDB:
        airport_event.logToDB(dbconn, printQuery=debug)
        dbconn.commit()
                    separated by commas when there are multiple instances",
                    default=None)

args = parser.parse_args()

if not args.db_conf:
    print("A valid URL db configuration file is needed!")
    exit(1)

if not args.airport:
    print("An Airport is needed!")
    exit(1)
else:
    if not args.start_time:
        args.start_time = datetime.date.today().strftime("%F") + " 00:00:00"
    dbconn = pr.connDB(args.db_conf)
    reporter = pr.readReporter(dbconn, args.reporter, printQuery=args.debug)
    airport = pr.readAirport(dbconn, args.airport, printQuery=args.debug)
    runways = pr.readRunways(dbconn, args.airport, printQuery=args.debug)
    for runway in runways:
        if args.debug:
            print(runway.to_JSON())
        if not args.runways or args.runways == runway.name:
            cur = pr.queryReportsDB(dbconn, myhex=args.hexcodes, myStartTime=args.start_time, \
                                    myEndTime=args.end_time,
                                    myflight=args.flights, maxAltitude=(
                                        int(args.committed_height) + airport.altitude),
                                    minAltitude=(airport.altitude - 150), myReporter=args.reporter,
                                    reporterLocation=reporter.location, printQuery=args.debug, \
                                    runways=runway.runway_area,
                                    postSql=" order by hex, report_epoch")
MAPY = 850000

lats, lons, alts = [], [], []
drawableslst = []
annolist = []

time_slices = []
time_slices.append([])
time_slc_idx = 0
max_dist = 0.0
max_alt = 0.0

reporter = pr.Reporter(name="",
                       type="",
                       lon=args.longitude,
                       lat=args.latitude,
                       location="",
                       url="",
                       mytype="")

inputfile = pr.openFile(args.datafile)
data = pr.readFromFile(inputfile)

if data:
    first_time = data[0].time
    next_time = first_time + args.sec_per_frame

while data:
    for plane in data:
        if plane.time >= next_time:
            # Catch case where there's a long time without action
Example #9
0
    help="print events in JSON format (default is to print as text)")
parser.add_argument('-d',
                    '--date',
                    dest='date',
                    default=False,
                    help="The date we're interested in for the flights")
parser.add_argument('-q',
                    '--quiet',
                    action="store_true",
                    dest='quiet',
                    default=False,
                    help="Keep the noise to a minimum")

args = parser.parse_args()

dbconn = pr.connDB(args.db_conf)
if not dbconn:
    print("Can't make connection to db")
    exit(1)

if not args.date:
    args.date = str(date.today())

reporter = pr.readReporter(dbconn, args.reporter)
if not reporter:
    print("Unable to read reporter from DB!")
    exit(1)
print(reporter.to_JSON())

sql = '''
 select max(ST_Distance(a.reporter_location, b.report_location)) as max_dist,max(b.altitude)
Example #10
0
args = parser.parse_args()

if not args.datafile:
    print("Need a data filename")
    exit(1)

if not args.outfile:
    print("Need an output filename")
    exit(1)

xx, yy, zz, bearings, timedeltas = [], [], [], [], []
lastpos = ""
lasttime = -1

inputfile = pr.openFile(args.datafile)
data = pr.readFromFile(inputfile)

while data:
    for plane in data:
        if plane.report_location != lastpos:
            lastpos = plane.report_location
            if lasttime == -1:
                lasttime = plane.time - 1
            if (plane.time - lasttime) > 0:
                xx.append(plane.lon)
                yy.append(plane.lat)
                zz.append(plane.altitude)
                bearings.append(plane.track)
                timedeltas.append(plane.time - lasttime)
                lasttime = plane.time
Example #11
0
if not args.datafile:
    print("Need a data filename")
    exit(1)

xx, yy  = [], []

attrs = args.attrs.split(',')
zz={}
for i in attrs:
    zz[i] = []

if 'distance' in attrs and (not args.latitude or not args.longitude):
    print("Need location(lat, lon) to calculate distance")
    exit(1)

inputfile = pr.openFile(args.datafile)
data = pr.readFromFile(inputfile)
time_start = data[0].time
while data:
    for plane in data:
        xx.append(plane.time - time_start)
        for i in attrs:
            if i == 'distance':
                zz[i].append(pr.haversine(plane.lon, plane.lat, args.longitude, args.latitude) / 1000.0)
            else:
                zz[i].append(getattr(plane, i))

    data = pr.readFromFile(inputfile)
time_end = xx[-1] + time_start

start_time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time_start))
Example #12
0
                    default=False,
                    help="Turn on debug mode")
parser.add_argument(
    '-f',
    '--file',
    dest='datafile',
    help=
    "A file to load data from to populate a database (only makes sense when a DB Conf file is specified)"
)

args = parser.parse_args()

if not args.db_conf:
    print("A valid URL db configuration file is needed!")
    exit(1)

if not args.datafile:
    print("A valid airport file is needed!")
    exit(1)

dbconn = pr.connDB(args.db_conf)
inputfile = pr.openFile(args.datafile)

airport = pr.readAirportFromFile(inputfile)

airport.logToDB(dbconn, update=args.update, printQuery=args.debug)
dbconn.commit()

if args.debug:
    print(airport.to_JSON())
Example #13
0

args = parser.parse_args()

dbconn = None

if not args.db_conf and (args.logToDB or args.update):
    print("A valid URL db configuration file is needed!")
    exit(1)

if not args.datafile:
    print("A datafile is required!")
    exit(1)

if args.db_conf:
    dbconn = pr.connDB(args.db_conf)

#if not args.airport:
#    print("An Airport is needed!")
#    exit(1)

inputfile = pr.openFile(args.datafile)

for line in inputfile:
    record = line.split()
    if len(record) > 0:
        if record[0] == "1" or record[0] == "16" or record[0] == "17":
            while len(record) > 5:
                if record[4] == args.airport:
                    airport, runways, record = buildAirportRunways(inputfile, record)
                    airportrec = pr.Airport(icao=airport['icao'], iata=airport['iata'], name=airport['name'],
                    help="The file to load the reporter data from")
parser.add_argument('-l', '--log-to-db', dest='logToDB', action="store_true",
                    default=False, help="Log the data to the DB")


args = parser.parse_args()

dbconn = False

if not args.db_conf and (args.logToDB or args.update):
    print("A valid URL db configuration file is needed to log data to the database!")
    exit(1)

if not args.datafile:
    print("A valid reporter file is needed!")
    exit(1)

if args.db_conf:
    dbconn = pr.connDB(args.db_conf)

inputfile = pr.openFile(args.datafile)

reporter = readReporterFromFile(inputfile)

if args.logToDB or args.update:
    reporter.logToDB(dbconn, update=args.update, printQuery=args.debug)
    dbconn.commit()

if args.debug:
    print(reporter.to_JSON())
Example #15
0
parser.add_argument(
    '--min-speed',
    dest='minSpeed',
    help=
    "The aircraft has to be at a speed greater than or equal than this (Units are in km/h)",
    type=float)

args = parser.parse_args()

if not args.db_conf:
    print("A valid URL db configuration file is needed!")
    exit(1)
else:
    if not args.start_time:
        args.start_time = datetime.date.today().strftime("%F") + " 00:00:00"
    dbconn = pr.connDB(args.db_conf)

    if args.reporter:
        reporter = pr.readReporter(dbconn, args.reporter)
    else:
        reporter = pr.Reporter(name=None,
                               mytype=None,
                               lon=None,
                               lat=None,
                               url=None,
                               location=None)

    cur = pr.queryReportsDB(dbconn,
                            myhex=args.hexcodes,
                            myStartTime=args.start_time,
                            myEndTime=args.end_time,
#! /usr/bin/env python3
#
#
import time
import argparse
import PlaneReport as pr


parser = argparse.ArgumentParser(
    description="Read a bunch of VRS archive files (from unzipped daily archive) and convert them to our JSON format")
parser.add_argument('filenames', metavar='N', nargs='+',
                    help="A list of filenames to be opened")

args = parser.parse_args()

if not args.filenames:
    print("One or more files are needed!")
    exit(-1)

for fn in args.filenames:
    fh = pr.openFile(fn)
    pos_reports = pr.readVRSFromFile(fh)
    for pl in pos_reports:
        print(pl.to_JSON())
    fh.close()
Example #17
0
                    '--db-conf-file',
                    dest='db_conf',
                    help="A yaml file containing the DB connection parameters")

args = parser.parse_args()

if (not args.latitude and not args.reporter) or (not args.longitude
                                                 and not args.reporter):
    print("Need a location of some sort - either reporter or lat/lon pair")
    exit(1)

if not args.db_conf:
    print("Need a dbconfig file to read database!")
    exit(1)

dbconn = pr.connDB(args.db_conf)

if args.latitude and args.longitude:
    point = Point(args.longitude, args.latitude)
    reporter = pr.Reporter(name="",
                           type="",
                           lon=args.longitude,
                           lat=args.latitude,
                           location=point.wkb_hex,
                           url="",
                           mytype="")
else:
    reporter = pr.readReporter(dbconn,
                               key=args.reporter,
                               printQuery=args.debug)
Example #18
0
parser.add_argument('-v', '--vrs-fmt', action='store_true', dest='vrs_fmt',
                    help="URL refers to VRS adsbexchange.com", default=False)


args = parser.parse_args()

reporter = None
dbconn = None

if not args.dump1090url and not args.db_conf and not args.datafile:
    print("A valid URL or a valid filename or db connection is needed!")
    exit(-1)

if args.db_conf:
    dbconn = pr.connDB(args.db_conf)
    reporter = pr.readReporter(dbconn, key=args.reporter, printQuery=args.debug)

if not args.db_conf and (args.lat and args.lon):
    reporter = pr.Reporter(name='bodge', lat=args.lat, lon=args.lon, url='',
                           location="", mytype='')
#if args.datafile and not args.db_conf:
#    print("When specifying an input file, a database connection is needed")
#    exit(-1)

if not args.datafile:
    #
    # Set up the acquisition loop
    #
    samps_taken = 0
    while samps_taken < args.num_samps or args.num_samps < 0:
args = parser.parse_args()

if not args.db_conf:
    print("A valid URL db configuration file is needed!")
    exit(1)

if not args.date:
    print("A date is required!")
    exit(1)

if not (args.getFlights or args.getPlanes):
    print("At least one of --planes or --flights is required!")
    exit(1)

    
start_time = args.date + " 00:00:00"
end_time = args.date + " 23:59:59"

dbconn = pr.connDB(args.db_conf)

if args.getFlights:
    splitList(dbconn, start_time, end_time, 'flight', args.date, args.debug, args.reporter,
              args.logToDB, args.printJSON, args.quiet, args.numRecs)
if args.getPlanes:    
    splitList(dbconn, start_time, end_time, 'hex', args.date, args.debug, args.reporter,
              args.logToDB, args.printJSON, args.quiet, args.numRecs)

if args.logToDB:
    dbconn.commit()