Skip to content
Snippets Groups Projects
Commit 4fcd8a35 authored by leprob001's avatar leprob001
Browse files

Added function to save data as .trj4 files and its test.

parent ad44221e
No related branches found
No related tags found
No related merge requests found
function [success] = save()
%SAVE Test function for saving a TRJ4 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');
rfb = artoa.load.rfb(dataFileName);
header = "Created in 2019" + newline + "a multiline test header";
data = [];
data(:, 1) = rfb.DATA(1:13, rfb.VARIABLE_LIST.time_of_arrival(1));
data(:, 2) = str2num(cell2mat(rfb.SAT_DATA(:, 1)));
data(:, 3) = str2num(cell2mat(rfb.SAT_DATA(:, 2)));
data(:, 4) = rfb.DATA(1:13, rfb.VARIABLE_LIST.temperature);
data(:, 5) = rfb.DATA(1:13, rfb.VARIABLE_LIST.pressure);
data(:, 6) = rfb.DATA(1:13, rfb.VARIABLE_LIST.pressure);
data(:, 7) = rfb.DATA(1:13, rfb.VARIABLE_LIST.temperature);
saveToFilename = fullfile(currentDirectory, '..', 'data', 'testOutput.trj4');
success = artoa.save.trj4(saveToFilename, header, data);
end
function [ success ] = trj4(pFilename, pHeader, pData)
%TRJ4 Saves the given data to the filename in TRJ4 format.
%
% Parameters:
% pFilename (string) The destination filename.
% pHeader (string) The header that will be prepended to the file. Can
% be a multiline string/string array.
% pData (matrix) The data that should be written to the TRJ4 file.
% Each line contains one set of data. The matrix format needs to
% be the following:
% TIME LAT LON TEMP PRES VLAT VLON
%% Initialize return variables
success = false;
%% Initialize variables required for processing
headerLineIndicator = "# ";
%% Parameter check
parameterError = false;
if (~isstring(pFilename) && ~ischar(pFilename))
warning([mfilename ': Given filename is not a string!']);
parameterError = true;
end
if (~isstring(pHeader))
warning([mfilename ': Given header is not a string!']);
parameterError = true;
end
if (size(pData, 2) ~= 7)
warning([mfilename ': Given data has ' site(pData, 2) ' columns, but 7 expected!']);
parameterError = true;
end
if (parameterError)
return;
else
clear parameterError;
end
%% Replace all NaN with default values
defaultValue = -999;
for i = 1:size(pData, 2)
nanIndex = find(isnan(pData(:, i)));
if ~isempty(nanIndex)
pData(nanIndex, i) = repmat(defaultValue, 1, size(pData, 1));
end
clear nanIndex;
end
%% Prepare header and add it to the file string
preparedHeader = splitlines(pHeader);
fileString = '';
for i = 1:length(preparedHeader)
fileString = fileString + headerLineIndicator + preparedHeader{i} + newline;
end
%% Generate file format
for i = 1:size(pData, 1)
line = { ...
num2str(pData(i, 1), '%7.2f'),
num2str(pData(i, 2), '%8.3f'),
num2str(pData(i, 3), '%8.3f'),
num2str(pData(i, 4), '%7.1f'),
num2str(pData(i, 5), '%7.2f'),
num2str(pData(i, 6), '%8.3f'),
num2str(pData(i, 7), '%8.3f'),
};
fileString = fileString + strjoin(line) + newline;
end
%% Save to file
fid = fopen(pFilename, 'w');
fprintf(fid, fileString);
fclose(fid);
%% Update return variable
success = true;
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment