function res = LEM_test_SF(roads,launch_sites,land_sites,num_flights,b,...
    flex,speed,hd)
% LEM_sim1_LBSD - basic small-scale simulation of lane based method
% On input:
%     roads (roads struct): road info
%       .vertexes (nx3 array): x,y,z values
%       .edges (mx2 array): from - to vertex indexes
%     launch_sites (px1 vector): road vertex indexes for launch sites
%     land_sites (qx1 vector): road vertex indexes for land sites
%     num_flights (int): number of flights
%     b (Boolean): make movie
% On output:
%     res (results struct): results info
%       .roads (roads struct): roads info
%       .airways (airways struct): airways info
%       .requests (requests struct): LBSD requests
%       .reservations (reservations struct): reservations info
%       .flights (flights struct): flights info
%       .failures_LBSD (n by 1 vector): 0 if LBSD flight success; else 1
%       .flights_cells (flights struct): cells flight info
%       .cells (cells struct): FAA-NASA cells corresponding to LBSD layout
%       .requests_cells (requests_cells struct): FAA-NASA flight requests 
%       .P (nx5 array): performance info for LBSD
%         col 1 (float): flight ID
%         col 2 (float): delay (actual_start - requested_min_start)
%         col 3 (float): flight duration
%         col 4 (float): deconflict time
%         col 5 (float): failed to schedule
% Call:
%     load('east_bench_roads_small.mat');
%     res = LEM_sim2_LBSD(east_bench_roads_small,[1:10],[240:249],10,1);
% Author:
%     T. Henderson
%     UU
%     Fall 2020
%

MIN_LANE_LENGTH = 50;
MIN_ALTITUDE = 467;
MAX_ALTITUDE = 534;
FAILED = 0;

res = [];

roads = LEM_conn_roads(roads);

%rng('default');
airways = LEM_gen_airways(roads,launch_sites,land_sites,MIN_LANE_LENGTH,...
    MIN_ALTITUDE,MAX_ALTITUDE);
%hd = 1;   % headway distance
t_min = 0;
t_max = 240;
%t_max = 100;
launch_time_interval = flex;
%launch_time_interval = 120;
min_speed = speed;
max_speed = speed;

requests = LEM_gen_requests_LBSD(t_min,t_max,airways,num_flights,...
    launch_time_interval,[min_speed,max_speed]);
[reservations,flights] = LEM_requests2reservations(airways,requests,hd);

% if b==1
%     LEM_run_flights2(airways,flights,1,1,'sim1_LBSD');
% end

if b==1
del_t = 0.1;
for f = 1:num_flights
    f_out = LEM_gen_traj(flights(f),del_t);
    flights(f).traj = f_out.traj;
end

    clf
    plot3(airways.vertexes(:,1),airways.vertexes(:,2),...
        airways.vertexes(:,3),'ko');
    hold on
    for f = 1:length(flights)
        pts1 = flights(f).route(:,1:3);
        pts2 = flights(f).route(:,4:6);
        num_pts = length(pts1(:,1));
        for p = 1:num_pts
            plot3([pts1(p,1),pts2(p,1)],[pts1(p,2),pts2(p,2)],[pts1(p,3),...
                pts2(p,3)],'r');
        end
    end
end

P_LBSD = LEM_performance(flights);

res.roads = roads;
res.airways = airways;
res.requests = requests;
res.reservations = reservations;
res.flights = flights;
failures_LBSD = zeros(num_flights,1);
for f = 1:num_flights
    if flights(f).type==FAILED
        failures_LBSD(f) = 1;
    end
end
res.failures_LBSD = failures_LBSD;
res.mp4 = [];
res.P_LBSD = P_LBSD;
res.P_cells = [];
