diff --git a/+tests/+ric/save.m b/+tests/+ric/save.m new file mode 100644 index 0000000000000000000000000000000000000000..f4f275de780173f1c9e2a323bae6e1b0fe6267f4 --- /dev/null +++ b/+tests/+ric/save.m @@ -0,0 +1,24 @@ +function [success] = save() +%SAVE Test function for saving a RIC 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); + +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); + +saveToFilename = fullfile(currentDirectory, '..', 'data', 'testOutput.ric'); + +success = artoa.save.ric(saveToFilename, num2str(rfb.FLOAT.floatname), num2str(rfb.FLOAT.cycle(1)), data); + +end + diff --git a/lib/+artoa/+convert/rafosJulianDay.m b/lib/+artoa/+convert/rafosJulianDay.m new file mode 100644 index 0000000000000000000000000000000000000000..6272c983fdbdc73c7773976651415dfd0c8638f3 --- /dev/null +++ b/lib/+artoa/+convert/rafosJulianDay.m @@ -0,0 +1,14 @@ +function [ddmmyyyy] = rafosJulianDay(pRafosJulianDay) +%RAFOSJULIANDAY Converts the given rafos julian day to DDMMYYYY. +% +% Parameters: +% pRafosJulianDay (double) The input time format. + +offset = 2451545; + +convertedTime = artoa.vendor.greg_0h(pRafosJulianDay + offset); + +ddmmyyyy = [convertedTime(:, 3) convertedTime(:, 2) convertedTime(:, 1)]; + +end + diff --git a/lib/+artoa/+data/+load/rfb.m b/lib/+artoa/+load/rfb.m similarity index 91% rename from lib/+artoa/+data/+load/rfb.m rename to lib/+artoa/+load/rfb.m index dcacdce4136c739595bcb812559df5e948bf87dc..39b261b9fc87d2c4cfa763a4cf5dd59712a7cec6 100755 --- a/lib/+artoa/+data/+load/rfb.m +++ b/lib/+artoa/+load/rfb.m @@ -31,6 +31,6 @@ else clear parameter_error; end -rfbfile = artoa.data.load.tomlini(pFilename); +rfbfile = artoa.load.tomlini(pFilename); end diff --git a/lib/+artoa/+data/+load/tomlini.m b/lib/+artoa/+load/tomlini.m similarity index 100% rename from lib/+artoa/+data/+load/tomlini.m rename to lib/+artoa/+load/tomlini.m diff --git a/lib/+artoa/+save/ric.m b/lib/+artoa/+save/ric.m new file mode 100644 index 0000000000000000000000000000000000000000..37c115e523b00b3c3c0ab944596472be34699336 --- /dev/null +++ b/lib/+artoa/+save/ric.m @@ -0,0 +1,107 @@ +function [ success ] = ric(pFilename, pFloatname, pCurrentCycle, pData) +%RIC Saves the given data to the filename in RIC format. +% +% Parameters: +% pFilename (string) The destination filename. +% pFloatname (string) The name of the float. +% pCurrentCycle (string) The current cycle. +% pData (matrix) The data that should be written to the RIC file. +% Each line contains one set of data. The matrix format needs to +% be the following: +% TIME LAT LON TEMP PRES +% The TIME needs to be in julian rafos day format. + + +%% Initialize return variables +success = false; + +%% Initialize variables required for processing + +%% Parameter check + +parameterError = false; + +if (~ischar(pFilename)) + warning([mfilename ': Given filename is not a string!']); + parameterError = true; +end +if (~ischar(pFloatname)) + warning([mfilename ': Given float name is not a string!']); + parameterError = true; +end +if (~ischar(pCurrentCycle)) + warning([mfilename ': Given current cycle is not a string!']); + parameterError = true; +end +if (size(pData, 2) ~= 5) + warning([mfilename ': Given data has ' site(pData, 2) ' columns, but 5 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 + +%% Generate file format + +offsetHour = 0.0000000001; +offsetRafosJulianDay = 2440000; + +fileString = ''; + +for i = 1:size(pData, 1) + ddmmyyyy = artoa.convert.rafosJulianDay(pData(i, 1)); + ddmmyyyy(3) = mod(ddmmyyyy(3), 100); + hour = 24 * (pData(i, 1) - floor(pData(i, 1))) + offsetHour; + minute = round(hour - floor(hour)); + second = round(minute - floor(minute)); + hour = floor(hour); + minute = round(minute); + fileString = [ ... + fileString ... + pFloatname ' - cycle ' pCurrentCycle ... + ' ' ... + num2str(ddmmyyyy(3), '%2.2i') ... + num2str(ddmmyyyy(2), '%2.2i') ... + num2str(ddmmyyyy(1), '%2.2i') ... + num2str(hour, '%2.2i') ... + num2str(minute, '%2.2i') ... + num2str(second, '%2.2i') ... + num2str(pData(i, 2), '%7.3f') ... + num2str(pData(i, 3), '%8.3f') ... + ' 0' ... + ' -999.000' ... + ' -999.000' ... + ' ' ... + num2str(pData(i, 4), '%7.2f') ... + ' ' ... + num2str(pData(i, 5), '%7.2f') ... + ' ' ... + num2str((pData(i, 1) + offsetRafosJulianDay), '%8.0f') ... + '.\n' ... + ]; +end + +%% Save to file + +fid = fopen(pFilename, 'w'); +fprintf(fid, fileString); +fclose(fid); + +%% Update return variable + +success = true; + +end \ No newline at end of file diff --git a/lib/+artoa/+vendor/greg_0h.m b/lib/+artoa/+vendor/greg_0h.m new file mode 100644 index 0000000000000000000000000000000000000000..455b0058983faf642b8a335865cac0facb32426b --- /dev/null +++ b/lib/+artoa/+vendor/greg_0h.m @@ -0,0 +1,70 @@ +function [gtime]=greg_0h(jourjul) +% GREGORIAN Converts Julian day numbers to corresponding Gregorian calendar dates +% Formally, Julian days start and end at noon. +% In this convention, Julian day 2440000 begins at +% 00 hours, May 23, 1968. +% +% ========================================== +% greg_0h.m = contraire de jul_0h.m +% ========================================== +% +% Usage: [gtime]=greg_0h(jourjul) +% +% jourjul... input decimal Julian day number +% +% gtime is a six component Gregorian time vector +% i.e. gtime=[yyyy mo da hr mi sec] +% gtime=[1989 12 6 7 23 23.356] +% +% yr........ year (e.g., 1979) +% mo........ month (1-12) +% d........ corresponding Gregorian day (1-31) +% h........ decimal hours +% + +% Revised T.Terre + +% +% Ajout TT pour pb de conversion 07 00 00 == retourne en 06 59 60 ? +% Ca permet d'avoir la date juste a la dizaine de us pres. +% 10/05/2001 +% + fac = 10^9; + jourjul= round(fac*jourjul+0.5)/fac; + +% Fin ajout TT + + secs=rem(jourjul,1)*24*3600; + +% +% Ajout TT pour pb de conversion 07 00 00 == retourne en 06 59 60 ? +% Ca permet d'avoir la date juste a la dizaine de us pres. +% 10/05/2001 +% + secs = round(fac*secs + 0.5)/fac; + +% Fin ajout TT + + j = floor(jourjul) - 1721119; + in = 4*j -1; + y = floor(in/146097); + j = in - 146097*y; + in = floor(j/4); + in = 4*in +3; + j = floor(in/1461); + d = floor(((in - 1461*j) +4)/4); + in = 5*d -3; + m = floor(in/153); + d = floor(((in - 153*m) +5)/5); + y = y*100 +j; + mo=m-9; + yr=y+1; + i=(m<10); + mo(i)=m(i)+3; + yr(i)=y(i); + + hour=floor(secs/3600); + min=floor(rem(secs,3600)/60); + sec=rem(secs,60); + + gtime=[yr(:) mo(:) d(:) hour(:) min(:) sec(:)];