function [success] = save()
%SAVE Test function for saving a RFC 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');

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

track = 2;

% get global variables that have been imported from the track data
global TEMP_REF TEMP_DATE TEMP_DEL TEMP_SEL TEMP PRES_DEL PRES_SEL PRES
global TRAJ afoffbeg afoffend

% IFLAG MESSAGE_NUMBER MESSAGE_DATE TEMP PRES LAT LON u v w
lineCount = size(TEMP_REF, 1);
data = [];
data(:, 1) = repmat(999, lineCount, 1);
data(:, 2) = TEMP_REF;
data(:, 3) = TEMP_DATE;
data(:, 4) = repmat(-9.99, lineCount, 1);
indices = (~TEMP_DEL&(TEMP_SEL~=0));
data(indices, 4) = TEMP(indices);
data(:, 5) = repmat(-999, lineCount, 1);
indices = (~PRES_DEL&(PRES_SEL~=0));
data(indices, 5) = PRES(indices);
data(:, 6) = repmat(999, lineCount, 1);
data(:, 7) = repmat(999, lineCount, 1);
data(:, 8) = repmat(999, lineCount, 1);
data(:, 9) = NaN(1, lineCount);
data(:, 10) = repmat(999, lineCount, 1);


%% RFC without trajectory
saveToFilename = fullfile(currentDirectory, '..', 'testdata', 'output', 'testOutput.rfc');

success = artoa.save.rfc(saveToFilename, rfb.FLOAT, data);

%% Setup trajectory object

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;
% The toaData is not required for this test.
%trajectory.toaData = TRAJ(track).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;

% create soundSourceCombination struct
sourceCombination = struct();
for i = 1:length(TRAJ(track).comb)
    sourceCombination(i).sources = TRAJ(track).comb(i).sources;
    sourceCombination(i).begin = TRAJ(track).comb(i).begin;
    sourceCombination(i).end = TRAJ(track).comb(i).end;
    sourceCombination(i).referencePosition = TRAJ(track).comb(i).refpos;
    sourceCombination(i).soundSpeed = TRAJ(track).comb(i).soundspeed;
end

% add it to the trajectory
trajectory.soundSources = sourceCombination;



%% RFC including trajectory
saveToFilename = fullfile(currentDirectory, '..', 'testdata', 'output', 'testOutputIncludingTrajectory.rfc');

success = success && artoa.save.rfc(saveToFilename, rfb.FLOAT, data, trajectory);

end

