function [] = copyRfbToWorkspace(pIgnoreDataSection)
%COPYRFBTOWORKSPACE Copies rfb input data to the workspace.
%   Gets all required data from the artoaDataInput variable, prepares it
%   for calculations and operations and stores them in the artoaWorkspace
%   variable. The data structure can be found in the wiki as well.

global artoaDataInput artoaConfig artoaWorkspace;

%% Parameter check
if ~exist('pIgnoreDataSection', 'var')
    pIgnoreDataSection = false;
end

%% FLOAT DETAILS
artoaWorkspace.float = artoaDataInput.rfb.FLOAT;

%% SAT DATA
fnames = fieldnames(artoaDataInput.rfb.SAT_FORMAT);
artoaWorkspace.satData = struct();
for i = 1:length(fnames)
    artoaWorkspace.satData.(fnames{i}) = artoaDataInput.rfb.SAT_DATA( ...
        :, ...
        artoaDataInput.rfb.SAT_FORMAT.(fnames{i}) ...
    );
end

%% STOP IF DATA SHOULD BE IGNORED
if pIgnoreDataSection
    return;
end


%% RAFOS DATE

artoaWorkspace.rafosDate = artoa.convert.dmy2rd( ...
    artoaDataInput.rfb.DATA(:, artoaDataInput.rfb.VARIABLE_LIST.start_listen_day), ...
    artoaDataInput.rfb.DATA(:, artoaDataInput.rfb.VARIABLE_LIST.start_listen_mon), ...
    artoaDataInput.rfb.DATA(:, artoaDataInput.rfb.VARIABLE_LIST.start_listen_yr) ...
) ...
+ ...
artoa.convert.hms2rd( ...
    artoaDataInput.rfb.DATA(:, artoaDataInput.rfb.VARIABLE_LIST.start_listen_hr), ...
    artoaDataInput.rfb.DATA(:, artoaDataInput.rfb.VARIABLE_LIST.start_listen_min), ...
    artoaDataInput.rfb.DATA(:, artoaDataInput.rfb.VARIABLE_LIST.start_listen_sec) ...
);


%% TOA
artoaWorkspace.toaData = struct();
artoaWorkspace.toaData.status = zeros( ...
    size(artoaDataInput.rfb.DATA, 1) * length(artoaDataInput.rfb.VARIABLE_LIST.time_of_arrival), ...
    1 ...
);

artoaWorkspace.toaData.toa = [];
toaColumns = artoaDataInput.rfb.VARIABLE_LIST.time_of_arrival;
for i = 1:length(toaColumns)
    artoaWorkspace.toaData.toa = [ ...
        artoaWorkspace.toaData.toa; ...
        artoaDataInput.rfb.DATA(:, toaColumns(i)) ...
    ];
end

artoaWorkspace.toaData.correlationHeight = [];
correleationHeightColumns = artoaDataInput.rfb.VARIABLE_LIST.correlation_height;
for i = 1:length(correleationHeightColumns)
    artoaWorkspace.toaData.correlationHeight = [ ...
        artoaWorkspace.toaData.correlationHeight; ...
        artoaDataInput.rfb.DATA(:, correleationHeightColumns(i)) ...
    ];
end

artoaWorkspace.toaData.toaDate = repmat( ...
    artoaWorkspace.rafosDate, ...
    length(artoaDataInput.rfb.VARIABLE_LIST.time_of_arrival), ...
    1 ...
);

artoaWorkspace.toaData.soundSource = cell( ...
    size(artoaDataInput.rfb.DATA, 1) * length(artoaDataInput.rfb.VARIABLE_LIST.time_of_arrival), ...
    1 ...
);

artoaWorkspace.toaData.empiricalShift = zeros(size(artoaWorkspace.toaData.toa));

% Copy the original toaData object to the input, so that it easily can be
% restored later on
artoaDataInput.toaData = artoaWorkspace.toaData;

%% TEMPERATURE
artoaWorkspace.temperature = artoaDataInput.rfb.DATA(:, artoaDataInput.rfb.VARIABLE_LIST.temperature);
artoaWorkspace.statusTemperature = zeros(size(artoaDataInput.rfb.DATA, 1), 1);

%% PRESSURE
artoaWorkspace.pressure = artoaDataInput.rfb.DATA(:, artoaDataInput.rfb.VARIABLE_LIST.pressure);
artoaWorkspace.statusPressure = zeros(size(artoaDataInput.rfb.DATA, 1), 1);

%% SET DEFAULTS TO NaN
% toa
artoa.data.replaceValuesWithNaN( ...
    artoaWorkspace.toaData.toa, artoaConfig.defaults.toa ...
);
% temperature
artoa.data.replaceValuesWithNaN( ...
    artoaWorkspace.temperature, artoaConfig.defaults.temperature ...
);
% pressure
artoa.data.replaceValuesWithNaN( ...
    artoaWorkspace.pressure, artoaConfig.defaults.pressure ...
);

%% GET SOUNDSOURCES THAT ARE ALIVE DURING THE FLOAT MISSION
artoaWorkspace.filteredSoundsources = artoa.soundsources.filter( ...
    artoaDataInput.soundsources, min(artoaWorkspace.rafosDate), max(artoaWorkspace.rafosDate) ...
);

end

