---
title: "Flight Analysis"
output:
  html_notebook: 
    code_folding: hide
    toc: yes
    number_sections: yes
    toc_float: 
        collapsed: false
---

[Flight Summary](flight_summary.nb.html)

[Flight Statistics](flight_statistics.nb.html)

Data normalized across several flight logs, spanning from `r min(levels(flight_data$Flight))` to `r max(levels(flight_data$Flight))`, excepting NS54, NS55, and NS68 (NS54 is reserved for a flight still in development, and both NS55 and NS68 failed to record location data).

Due to a settings issue, before NS59, the cell tracker hardware stopped recording above 12.1 km (except for NS53). This limit is shown as a grey horizontal line in the figures depicting altitude.

```{r setup}
require(ggplot2)
require(directlabels)
require(scales)

source("setup.R")

flight_times <- read_flight_times("flight_times.txt")
flight_data <- read_flight_data("flight_data/", calculate_difftime = TRUE)
```

# Ascent Rate

## Ascent Rate vs Time of Year

```{r ascent_rate_vs_time_of_year, fig.height=7.4164079, fig.width=12}
ggplot(flight_data,
       aes(
           x = as.Date(format(DateTime, format = "%m-%d"), format = "%m-%d"),
           y = Ascent_Rate_m_s,
           colour = Flight
       )) +
    geom_point() + geom_path() +
    geom_hline(yintercept = 0, alpha = 0.2) +
    scale_x_date(
        date_breaks = "1 month",
        labels = date_format("%b"),
        limits = as.Date(c("01-01", "12-31"), format = "%m-%d"),
        expand = c(0, 0)
    ) +
    geom_dl(aes(label = gsub("NS", "", Flight)),
            method = list(cex = 0.8, dl.trans(y = y + 0.2), "top.bumpup")) + guides(colour = FALSE) +
    labs(title = "Ascent Rate vs Time of Year", x = "Time of Year", y = "Ascent Rate (m/s)")
```

## Ascent Rate After Burst

```{r ascent_rate_after_burst, fig.height=7.4164079, fig.width=12}
ggplot(flight_data,
       aes(x = burst_difftime / 60,
           y = Ascent_Rate_m_s,
           colour = Flight)) +
    geom_path() +
    xlim(-0.5, 3) +
    geom_dl(aes(label = gsub("NS", "", Flight)), method = list("smart.grid", "small.box.labels")) + guides(colour = FALSE) +
    labs(title = "Ascent Rate After Burst", x = "Time Since Burst (minutes)", y = "Ascent Rate (m/s)")
```

## Ascent Rate vs Altitude

```{r ascent_rate_vs_altitude, fig.height=7.4164079, fig.width=12}
ggplot(flight_data,
       aes(x = Altitude_m / 1000,
           y = Ascent_Rate_m_s,
           colour = Flight)) +
    geom_path() +
    geom_dl(aes(label = gsub("NS", "", Flight)),
            method = list("far.from.others.borders", "small.box.labels")) + guides(colour = FALSE) +
    labs(title = "Ascent Rate vs Altitude", x = "Altitude (km)", y = "Ascent Rate (m/s)")
```

# Ground Speed

## Ground Speed vs Time of Year

```{r ground_speed_vs_time_of_year, fig.height=7.4164079, fig.width=12}
ggplot(flight_data,
       aes(
           x = as.Date(format(DateTime, format = "%m-%d"), format = "%m-%d"),
           y = Ground_Speed_m_s,
           colour = Flight
       )) +
    geom_point() + geom_path() +
    scale_x_date(
        date_breaks = "1 month",
        labels = date_format("%b"),
        limits = as.Date(c("01-01", "12-31"), format = "%m-%d"),
        expand = c(0, 0)
    ) +
    geom_dl(aes(label = gsub("NS", "", Flight)),
            method = list(cex = 0.8, dl.trans(y = y + 0.2), "top.bumpup")) + guides(colour = FALSE) +
    labs(title = "Ground Speed vs Time of Year", x = "Time of Year", y = "Ground Speed (m/s)")
```

## Ground Speed vs Altitude

```{r ground_speed_vs_altitude, fig.height=7.4164079, fig.width=12}
ggplot(
    flight_data,
    aes(
        x = Altitude_m / 1000,
        y = Ground_Speed_m_s,
        colour = as.POSIXlt(DateTime)$yday
    )
) +
    geom_point() +
    scale_colour_gradient2(
        limits = c(0, 366),
        low = "green",
        mid = "red",
        midpoint = 366 / 2,
        high = "blue"
    ) +
    labs(
        title = "Ground Speed vs Altitude",
        x = "Altitude (km)",
        y = "Ground Speed (m/s)",
        colour = "Day of Year"
    ) +
    theme(
        legend.position = c(1, 1),
        legend.direction = "horizontal",
        legend.justification = "right"
    )
```

Green: late winter, Yellow: spring, Red: summer, Purple: autumn, Blue: early winter

# Ascent and Descent Durations

For flights with definite burst and landing times, we can extract the times it took for the balloon to ascend and descend from max altitude. 

```{r duration_ratios}
flight_durations <- data.frame()

for (current_flight in flight_times$Flight)
{
    if (!is.na(flight_times$Launch_Time[flight_times$Flight == current_flight]) &&
        !is.na(flight_times$Burst_Time[flight_times$Flight == current_flight]) &&
        !is.na(flight_times$Landing_Time[flight_times$Flight == current_flight]))
    {
        current_ascent_duration <-
            as.numeric(with(
                flight_times,
                difftime(Launch_Time[Flight == current_flight], Burst_Time[Flight == current_flight], units = "mins")
            )) * -1
        current_descent_duration <-
            as.numeric(with(
                flight_times,
                difftime(Landing_Time[Flight == current_flight], Burst_Time[Flight == current_flight], units = "mins")
            ))
        
        flight_durations <- rbind(
            flight_durations,
            data.frame(
                Flight = current_flight,
                Date = flight_times$Date[flight_times$Flight == current_flight],
                Ascent_Duration_min = current_ascent_duration,
                Descent_Duration_min = current_descent_duration,
                Ratio = current_ascent_duration / current_descent_duration
            )
        )
    }
}

rm(current_flight,
   current_ascent_duration,
   current_descent_duration)

rmarkdown::paged_table(flight_durations)
```

On average, ascent takes `r signif(mean(flight_durations$Ascent_Duration_min), digits = 2)` minutes, while descent takes `r signif(mean(flight_durations$Descent_Duration_min), digits = 2)` minutes, with a ratio of `r signif(mean(flight_durations$Ratio), digits = 2)`.

## Ascent Duration vs Descent Duration

```{r current_ascent_duration_vs_current_descent_duration, fig.height=12, fig.width=7.4164079}
ggplot(
    flight_durations,
    aes(
        x = Descent_Duration_min,
        y = Ascent_Duration_min,
        colour = as.POSIXlt(Date)$yday
    )
) +
    geom_point(size = 0.1) + geom_abline(slope = 1:3) +
    scale_colour_gradient2(
        limits = c(0, 366),
        low = "green",
        mid = "red",
        midpoint = 366 / 2,
        high = "blue"
    ) +
    geom_dl(aes(label = gsub("NS", "", Flight)), method = list(cex = 0.8)) +
    labs(
        title = "Ascent Duration vs Descent Duration",
        x = "Descent Duration (minutes)",
        y = "Ascent Duration (minutes)",
        colour = "Day of Year"
    ) +
    coord_fixed(ratio = 1) +
    scale_x_continuous(breaks = seq(
        from = 0,
        to = max(flight_data$Downrange_Distance_m),
        by = 10
    )) +
    scale_y_continuous(breaks = seq(
        from = 0,
        to = max(flight_data$Altitude_m),
        by = 10
    )) +
    theme(
        legend.position = c(1, 0.96),
        legend.direction = "horizontal",
        legend.justification = "right"
    )
```
Green: late winter, Yellow: spring, Red: summer, Purple: autumn, Blue: early winter

```{r teardown, include=FALSE}
rm(read_flight_times, read_flight_data)

rm(small.box.labels)
#rm(flight_times)
#rm(flight_data)
```