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

[Flight Analysis](flight_analysis.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(ggmap)
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)
```

# Summary

## Flight Times

This table shows launch time, burst time, burst altitude, landing time, and masses of each flight.

```{r flight_times, fig.height=7.4164079, fig.width=12}
rmarkdown::paged_table(read.csv("flight_times.txt"))
```

## Map

Usually, the program launches balloons from the parking lot of [Clear Spring Elementary School in Clear Spring, MD](https://www.google.com/maps/place/Clear+Spring+Elementary+School/@39.6567399,-77.9355616,17z/data=!4m5!3m4!1s0x89ca1fdddeb353d9:0x71cb1b22b302c98d!8m2!3d39.656708!4d-77.9341766). However, flight predictions sometimes force launches from further west to avoid landing in either Baltimore or the Chesapeake Bay.

```{r coordinates_map, fig.height=10, fig.width=12}
# get data excluding NS69 (eclipse launch in South Carolina)
maryland_flights <- flight_data[flight_data$Flight != "NS69",]

# get map tile
if (!exists("map"))
{
    map <-
        get_map(location = c(
            min(maryland_flights$Longitude),
            min(maryland_flights$Latitude),
            max(maryland_flights$Longitude),
            max(maryland_flights$Latitude)
        ),
        maptype = "roadmap")
}

# plot base layer to map tile
ggmap(map,
      base_layer = ggplot(maryland_flights,
                          aes(
                              x = Longitude, y = Latitude, colour = Flight
                          )),
      extent = "normal") +
    geom_point() + geom_path() +
    geom_dl(aes(label = gsub("NS", "", Flight)),
            method = list("far.from.others.borders", "small.box.labels")) + guides(colour = FALSE) +
    labs(title = "Maryland Flights", x = "Longitude", y = "Latitude")
```

[D. Kahle and H. Wickham. ggmap: Spatial Visualization with ggplot2. The R Journal, 5(1), 144-161.](http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf)

## Altitude vs Downrange Distance

```{r altitude_vs_downrange_distance, fig.height=7.4164079, fig.width=12}
ggplot(flight_data,
       aes(
           x = Downrange_Distance_m / 1000,
           y = Altitude_m / 1000,
           colour = Flight
       )) +
    geom_point(size = 0.75) + geom_path(alpha = 0.3) +
    coord_fixed(ratio = 3) +
    scale_x_continuous(breaks = seq(
        from = 0,
        to = max(flight_data$Downrange_Distance_m),
        by = 30
    )) +
    scale_y_continuous(breaks = seq(
        from = 0,
        to = max(flight_data$Altitude_m),
        by = 10
    )) +
    geom_hline(yintercept = 12.1, alpha = 0.2) +
    geom_dl(aes(label = gsub("NS", "", Flight)),
            method = list("far.from.others.borders", "small.box.labels")) + guides(colour = FALSE) +
    labs(title = "Altitude vs Downrange Distance", x = "Downrange Distance (km)", y = "Altitude (km)")
```
axes to 1:3 scale

# Altitude

## Altitude vs Date

```{r altitude_vs_date, fig.height=7.4164079, fig.width=12}
ggplot(flight_data,
       aes(x = DateTime,
           y = Altitude_m / 1000,
           colour = Flight)) +
    geom_point() + geom_path() +
    geom_hline(yintercept = 12.1, alpha = 0.2) +
    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 = "Altitude vs Date", x = "Date", y = "Altitude (km)")
```

## Altitude vs Time of Year

```{r altitude_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 = Altitude_m / 1000,
           colour = Flight
       )) +
    geom_point() + geom_path() +
    geom_hline(yintercept = 12.100, 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 = "Altitude vs Time of Year", x = "Time of Year", y = "Altitude (km)")
```

## Altitude vs Time After Launch

```{r altitude_vs_time_after_launch, fig.height=7.4164079, fig.width=12}
ggplot(flight_data,
       aes(
           x = launch_difftime / 60,
           y = Altitude_m / 1000,
           colour = Flight
       )) +
    geom_point(size = 0.75) + geom_path() +
    geom_hline(yintercept = 12.100, alpha = 0.2) +
    geom_dl(aes(label = gsub("NS", "", Flight)),
            method = list("far.from.others.borders", "small.box.labels")) + guides(colour = FALSE) +
    labs(title = "Altitude vs Time After Launch", x = "Time After Launch (minutes)", y = "Altitude (km)")
```

# Downrange Distance

Using a geodesic, we can calculate distance between coordinate points to get downrange distance. Downrange distance represents the distance from the coordinates of the launch site to the coordinates of the data point. 

## Downrange Distance vs Time of Year

```{r downrange_distance_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 = Downrange_Distance_m / 1000,
           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 = "Downrange Distance vs Time of Year", x = "Time of Year", y = "Downrange Distance (km)")
```

## Downrange Distance vs Time After Launch

```{r downrange_distance_vs_time_after_launch, fig.height=7.4164079, fig.width=12}
ggplot(
    flight_data,
    aes(
        x = launch_difftime / 60,
        y = Downrange_Distance_m / 1000,
        colour = Flight
    )
) +
    geom_point(size = 0.75) + geom_path() +
    geom_dl(aes(label = gsub("NS", "", Flight)),
            method = list("far.from.others.borders", "small.box.labels")) + guides(colour = FALSE) +
    labs(title = "Downrange Distance vs Time After Launch", x = "Time After Launch (minutes)", y = "Downrange Distance (km)")
```

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

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

rm(maryland_flights)
#rm(map)
```