function [success] = save()
%SAVE Test function for saving a start end toa file.
% Uses a test rfb data file from the data folder. Uses only dummy fields
% for saving, because the actual algorithm for creating the data is not yet
% implemented.

[currentDirectory, ~, ~] = fileparts(mfilename('fullpath'));
dataFileName = fullfile(currentDirectory, '..', 'testdata', 'input', '0272.rfb');

% Select trajectory number
track = 2;

rfb = artoa.load.rfb(dataFileName);
% load test mat file exported from old version of artoa3
load(fullfile(currentDirectory, '..', 'testdata', 'input', 'float_0272_globals.mat'));

% get global variables that have been imported from the mat file
global TOA_DATE TOA TOA_SEL SOSOREF afoffbeg afoffend

% convert global variables to toaData format
toa = struct();
toa.status = zeros(length(TOA), 1);
toa.status(TOA_SEL > 0) = 1; % marks that the toa has been selected
toa.toa = TOA;
toa.toaDate = TOA_DATE;
toa.soundSource = cell(length(TOA), 1);
toa.soundSource(:) = {''};
for i = 1:length(toa.soundSource)
    if TOA_SEL(i) == 0
        continue;
    end
    toa.soundSource{i} = strtrim(SOSOREF(TOA_SEL(i), :));
end

%% Setup trajectory
trajectory = struct();
trajectory.trackingMethod = TRAJ(track).tmethod;
trajectory.interval = TRAJ(track).interval;
trajectory.gapSize = TRAJ(track).gapsize;
trajectory.interpolationMethod = TRAJ(track).imethod;
trajectory.soundSpeedMethod = TRAJ(track).ssmethod;
trajectory.dopplerCorrection = TRAJ(track).doppler;
trajectory.velocityMethod = TRAJ(track).vmethod;
trajectory.status = TRAJ(track).status;
trajectory.toaData = toa;
trajectory.latitude = TRAJ(track).lat;
trajectory.longitude = TRAJ(track).lon;
trajectory.date = TRAJ(track).date;
trajectory.pressure = TRAJ(track).pres;
trajectory.temperature = TRAJ(track).temp;
trajectory.iflag = TRAJ(track).iflag;
trajectory.clockError = TRAJ(track).clockerror;
trajectory.misfits = TRAJ(track).misfits;
trajectory.residualsPlotted = TRAJ(track).plotresiduals;
trajectory.velocityTotal = TRAJ(track).vtot;
trajectory.velocityLatitude = TRAJ(track).vlat;
trajectory.velocityLongitude = TRAJ(track).vlon;
trajectory.velocityVertical = TRAJ(track).vvert;
trajectory.velocityVerticalDate = TRAJ(track).vvert_date;
trajectory.additionalFloatOffsetBegin = afoffbeg;
trajectory.additionalFloatOffsetEnd = afoffend;

%% Setup sound sources
soundsources = artoa.load.soundsources( ...
    fullfile(currentDirectory, '..', 'testdata', 'input', 'HAFOS.soso') ...
);

%% Save data

saveToFilename = fullfile(currentDirectory, '..', 'testdata', 'output', 'startEndToaOutput.trj4');

success = artoa.save.startEndToa(saveToFilename, trajectory, soundsources);


end

