function [flights_out,cell_flights] = LEM_EQ_FAA_reserve(flights,cells,...
    launch_index,land_index,min_altitude,max_altitude,speed,hd,...
    start_time,max_delay,UAS_index,cell_flights)
% LEM_EQ_FAA_reserve - reserve trajectory for UAS flight
% On input:
%     flights (flights struct): flight info for scheduled flights
%     cells (cells struct): FAA cell info
%     launch_index (int): launch vertex index
%     land_index (int): land vertex index
%     min_altitude (float): minimum flight altitude
%     max_altitude (float): maximum flight altitude
%     speed (float): UAS speed
%     hd (float): headway distance
%     start_time (float): proposed launch time
%     max_delay (float): max allowed delay from desired start time
%     UAS_index (int): index of UAS making flight
%     cell_flights (cell flights struct): cell flight info
% On output:
%     flights_out (flights struct): updated flights
%     cell_flights (cell flights struct): updated cell flights info
% Call:
%     [fo,cf] =
%         LEM_EQ_FAA_reserve(flights,cells,24,68,467,534,30,6,30,300,3,cf);
% Author:
%     T. Henderson
%     UU
%     Spring 2021
%

flights_out = flights;

pt1 = cells.launch_sites(launch_index,:);
pt4 = cells.land_sites(land_index,:);
altitude = min_altitude + rand*(max_altitude-min_altitude);
min_start = start_time;
max_start = min_start + max_delay;
flight.UAS_index = UAS_index;
flight.speed = speed;
flight.start_time = min_start;
flight.start_interval = [min_start,max_start];
pt2 = pt1 + [0,0,altitude];
pt3 = pt4 + [0,0,altitude];
flight.path_vertexes = [pt1,pt2;pt2,pt3;pt3,pt4];
len1 = norm(pt2-pt1);
len2 = norm(pt3-pt2);
len3 = norm(pt4-pt3);
total_len = len1 + len2 + len3;
plan = zeros(3,4);
plan(1,1) = flight.start_time;
plan(1,2) = plan(1,1) + len1/flight.speed;
plan(1,3) = flight.speed;
plan(1,4) = 1;
plan(2,1) = plan(1,2);
plan(2,2) = plan(2,1) + len2/flight.speed;
plan(2,3) = flight.speed;
plan(2,4) = 2;
plan(3,1) = plan(2,2);
plan(3,2) = plan(3,1) + len3/flight.speed;
plan(3,3) = flight.speed;
plan(3,4) = 3;
flight.plan = plan;
flight.route = [pt1,pt2,plan(1:2),plan(1,3);...
    pt2,pt3,plan(2,1:2),plan(2,3);...
    pt3,pt4,plan(3,1:2),plan(3,3)];
flight.decon_time = 0;
flight.end_time = flight.start_time + total_len/flight.speed;
flight.launch_index = launch_index;
flight.land_index = land_index;
flight.telemetry = [pt1,0 0 1,flight.speed];
flight.type = 1;
flight.path = LEM_cell_els(cells,pt1(1:2),pt4(1:2));
tic
flight = LEM_deconflict(flights,flight,cell_flights,hd);
flight.decon_time = toc;
if flight.type~=0
    fcells = flight.path;
    num_fcells = length(fcells);
    for c = 1:num_fcells
        cell_flights(fcells(c)).flights = ...
            [cell_flights(fcells(c)).flights;length(flights)+1];
    end
end
flights_out = [flights_out;flight];
