diff --git a/src/app.Rmd b/src/app.Rmd index 7cf976f..d2b4869 100644 --- a/src/app.Rmd +++ b/src/app.Rmd @@ -39,7 +39,7 @@ ui <- fluidPage( hr(), h4("Batch Analysis"), - numericInput("batch_size", "Number of flights to analyze:", value = 10, min = 2, max = 50), + numericInput("batch_size", "Days of flights to analyze:", value = 5, min = 1, max = 30), actionButton("batch_analyze", "Run Batch Analysis", class = "btn-warning"), hr(), @@ -144,6 +144,7 @@ server <- function(input, output, session) { data.frame( Index = i, ICAO24 = dep[["ICAO24"]] %||% NA, + #FIXME Callsign, Origin, Destination Callsign = dep[["callsign"]] %||% NA, Origin = dep[["estDepartureAirport"]] %||% NA, Destination = dep[["estArrivalAirport"]] %||% NA, @@ -248,28 +249,9 @@ server <- function(input, output, session) { status("Running batch analysis...") tryCatch({ - all_trajectories <- list() - n_departures <- min(length(rv$departures), input$batch_size) withProgress(message = 'Analyzing flights', value = 0, { - for (i in 1:n_departures) { - dep <- rv$departures[[i]] - icao24 <- dep[["ICAO24"]] - dep_time <- dep[["departure_time"]] - - incProgress(1/n_departures, detail = paste("Processing", icao24)) - - if (is.null(dep_time)) next - - # Use calculate_trajectory_params from main.Rmd - params <- calculate_trajectory_params(icao24, dep_time, rv$creds) - - if (!is.null(params)) { - all_trajectories[[length(all_trajectories) + 1]] <- params - } - - Sys.sleep(0.3) - } + all_trajectories <- getAircraftTrajectories(rv$current_icao, time = Sys.time(), creds, days = input$batch_size) }) if (length(all_trajectories) > 0) { diff --git a/src/main.Rmd b/src/main.Rmd index 1001590..18d15e5 100644 --- a/src/main.Rmd +++ b/src/main.Rmd @@ -52,6 +52,8 @@ getAircraftTrack <- function(icao, time, creds) { } return(NULL) } + + ``` ## Trajectory Conversion Functions @@ -86,7 +88,6 @@ getTrajFromRoute <- function(route_df) { # Calculate trajectory characteristics # Input: either route_df (data.frame with lat/lon) or trj (trajr object) # format: "row" for batch analysis (one row per flight), "table" for single flight display -# FIXME for batch analysis: use the same aircraft calculateTrajectoryStats <- function(input, icao = NULL, format = "row") { # Determine if input is route_df or trj if (inherits(input, "Trajectory")) { @@ -165,6 +166,21 @@ calculate_trajectory_params <- function(icao, departure_time, creds) { return(NULL) }) } + +getAircraftTrajectories <- function(icao, time, creds, days = 5){ + tracks <- list() + for (i in 0: (days-1)) { + flights <- getFlights(icao,time - days(i),creds) + for (f in flights){ + track <- calculate_trajectory_params(icao, f[["departure_time"]], creds) + if (!is.null(track)){ + tracks[[length(tracks)+1]] <- track + } + Sys.sleep(0.5) # API courtesy + } + } + return(tracks) +} ``` ## Statistical Helper Functions @@ -313,7 +329,10 @@ generateInterpretation <- function(trajectory_stats_df) { # Set eval=TRUE to run this demo # Get credentials -creds <- getCredentials() +creds <- getCredentials( + client_id = Sys.getenv('OPENSKY_CLIENT_ID'), + client_secret = Sys.getenv('OPENSKY_CLIENT_SECRET') +) # Get departures from Frankfurt airport time_now <- Sys.time() @@ -343,7 +362,7 @@ if (length(departures) > 0) { xlab = "Time (Unix)", ylab = "Height (Meter)") # Get summary - print(getRouteSummary(route_df, icao)) + print(calculateTrajectoryStats(route_df, icao)) # Plot trajectory trj <- getTrajFromRoute(route_df)