From 9b2e81ab78d65cedf9260b839ee370bdd4f1466c Mon Sep 17 00:00:00 2001
From: Lewin Probst <info@emirror.de>
Date: Fri, 26 Jul 2019 10:17:15 +0200
Subject: [PATCH] Added export function of startEndToa and its test.

---
 +tests/+startendtoa/save.m     | 74 +++++++++++++++++++++++++
 lib/+artoa/+save/startEndToa.m | 98 ++++++++++++++++++++++++++++++++++
 2 files changed, 172 insertions(+)
 create mode 100644 +tests/+startendtoa/save.m
 create mode 100644 lib/+artoa/+save/startEndToa.m

diff --git a/+tests/+startendtoa/save.m b/+tests/+startendtoa/save.m
new file mode 100644
index 0000000..9220516
--- /dev/null
+++ b/+tests/+startendtoa/save.m
@@ -0,0 +1,74 @@
+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, '..', 'data', '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, '..', 'data', '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, '..', 'data', 'HAFOS.soso'));
+
+%% Save data
+
+saveToFilename = fullfile(currentDirectory, '..', 'data', 'startEndToaOutput.trj4');
+
+success = artoa.save.startEndToa(saveToFilename, trajectory, soundsources);
+
+
+end
+
diff --git a/lib/+artoa/+save/startEndToa.m b/lib/+artoa/+save/startEndToa.m
new file mode 100644
index 0000000..0a657a7
--- /dev/null
+++ b/lib/+artoa/+save/startEndToa.m
@@ -0,0 +1,98 @@
+function [ success ] = startEndToa(pFilename, pTrajectory, pSoundSources)
+%STARTENDTOA Saves the given data in start end toa format.
+%   Saves start and ending position of the given float.
+%
+%   Parameters:
+%       pFilename (string) The destination filename.
+%       pSoundSources (struct) The sound sources loaded using
+%           artoa.load.soundsource function.
+%       pTrajectory (struct) The trajectory data according to the memory
+%           data format specification, see projects wiki.
+
+
+%% Initialize return variables
+success = false;
+
+%% Initialize variables required for processing and defaults
+noSoundSource = 'No SoSo';
+dataString = '';
+
+%% Parameter check
+
+parameterError = false;
+
+
+if (~isstring(pFilename) && ~ischar(pFilename))
+    warning([mfilename ': Given filename is neither a string nor a char!']);
+    parameterError = true;
+end
+if (~isstruct(pSoundSources))
+    warning([mfilename ': Given sound source data is not a struct!']);
+    parameterError = true;
+end
+if (~isstruct(pTrajectory))
+    warning([mfilename ': Given trajectory data is not a struct!']);
+    parameterError = true;
+end
+
+if (parameterError)
+    return;
+else
+    clear parameterError;
+end
+
+
+%% Collect data
+indexStart = find( ...
+    pTrajectory.toaData.toaDate == min(pTrajectory.toaData.toaDate) ...
+);
+indexEnd = find( ...
+    pTrajectory.toaData.toaDate == max(pTrajectory.toaData.toaDate) ...
+);
+
+% Collect toa
+startToa = pTrajectory.toaData.toa(indexStart);
+endToa = pTrajectory.toaData.toa(indexEnd);
+% Collect toa date
+startToaDate = pTrajectory.toaData.toaDate(indexStart);
+endToaDate = pTrajectory.toaData.toaDate(indexEnd);
+
+% Collect sound source names
+startSources = pTrajectory.toaData.soundSource(indexStart);
+endSources = pTrajectory.toaData.soundSource(indexEnd);
+
+% Replace non selected soundsources by default string
+startNotSelectedIndices = contains(startSources, '');
+startSources(startNotSelectedIndices) = {noSoundSource};
+endNotSelectedIndices = contains(endSources, '');
+endSources(endNotSelectedIndices) = {noSoundSource};
+
+for i = 1:length(startToa)
+    dataString = [ dataString ...
+        num2str(startToaDate(i)) ' ' ...
+        num2str(startToa(i)) ' ' ...
+        startSources{i} ' ' ...
+        num2str(endToaDate(i)) ' ' ...
+        num2str(endToa(i)) ' ' ...
+        endSources{i} newline ...
+    ];
+end
+
+
+%% Save to file
+saveToFile(pFilename, '', dataString);
+
+%% Update return variable
+success = true;
+
+
+%% Nested functions
+
+    function saveToFile(pFilename, pHeader, pData)
+        fid = fopen(pFilename, 'w');
+        fprintf(fid, pHeader);
+        fprintf(fid, pData);
+        fclose(fid);
+    end
+
+end
-- 
GitLab