% ------------------------------------------------------------------------------
% Convert NetCDF trajectory file contents in CSV format.
%
% SYNTAX :
%   nc_traj_adj_2_csv or nc_traj_adj_2_csv(6900189, 7900118)
%
% INPUT PARAMETERS :
%   varargin : WMO number of floats to process
%
% OUTPUT PARAMETERS :
%
% EXAMPLES :
%
% SEE ALSO :
% AUTHORS  : Jean-Philippe Rannou (Altran)(jean-philippe.rannou@altran.com)
% ------------------------------------------------------------------------------
% RELEASES :
%   11/02/2015 - RNU - creation
%   06/28/2021 - RNU - updated to convert TRAJ 3.1 and 3.2 formats
% ------------------------------------------------------------------------------
function nc_traj_adj_2_csv(varargin)

% top directory of the NetCDF files to convert
DIR_INPUT_NC_FILES = 'C:\Users\jprannou\_DATA\OUT\nc_output_decArgo\';
% DIR_INPUT_NC_FILES = 'C:\Users\jprannou\_DATA\OUT\nc_output_decArgo_rtqc\';
% DIR_INPUT_NC_FILES = 'C:\Users\jprannou\_DATA\Conversion_en_3.1_20210913\IN\';


% default list of floats to convert
FLOAT_LIST_FILE_NAME = 'C:\Users\jprannou\_RNU\DecArgo_soft\lists\tmp.txt';
FLOAT_LIST_FILE_NAME = 'C:\Users\jprannou\_RNU\DecArgo_soft\lists\_apex_apf11_with_missing_O2_timestamp.txt';

% directory to store the log file
DIR_LOG_FILE = 'C:\Users\jprannou\_RNU\DecArgo_soft\work\log\';

% measurement codes initialization
init_measurement_codes;

% default values initialization
init_default_values;


if (nargin == 0)
   floatListFileName = FLOAT_LIST_FILE_NAME;
   
   % floats to process come from floatListFileName
   if ~(exist(floatListFileName, 'file') == 2)
      fprintf('ERROR: File not found: %s\n', floatListFileName);
      return
   end
   
   fprintf('Floats from list: %s\n', floatListFileName);
   floatList = load(floatListFileName);
else
   % floats to process come from input parameters
   floatList = cell2mat(varargin);
end

% create and start log file recording
if (nargin == 0)
   [pathstr, name, ext] = fileparts(floatListFileName);
   name = ['_' name];
else
   name = sprintf('_%d', floatList);
end

logFile = [DIR_LOG_FILE '/' 'nc_traj_adj_2_csv' name '_' datestr(now, 'yyyymmddTHHMMSS') '.log'];
diary(logFile);
tic;

% process the floats
nbFloats = length(floatList);
for idFloat = 1:nbFloats
   
   floatNum = floatList(idFloat);
   floatNumStr = num2str(floatNum);
   fprintf('%03d/%03d %s\n', idFloat, nbFloats, floatNumStr);
   
   ncFileDir = [DIR_INPUT_NC_FILES '/' num2str(floatNum) '/'];
   
   if (exist(ncFileDir, 'dir') == 7)
      
      % convert trajectory file
      ncFiles = dir([ncFileDir sprintf('%d_*traj.nc', floatNum)]);
      for idFile = 1:length(ncFiles)
         
         ncFileName = ncFiles(idFile).name;
         ncFilePathName = [ncFileDir '/' ncFileName];
         
         outputFileName = [ncFileName(1:end-3) '.csv'];
         outputFilePathName = [ncFileDir outputFileName];
         nc_traj_adj_2_csv_file(ncFilePathName, outputFilePathName, 0);
      end
      
      ncAuxFileDir = [DIR_INPUT_NC_FILES '/' num2str(floatNum) '/auxiliary/'];
      
      if (exist(ncAuxFileDir, 'dir') == 7)
         
         % convert auxiliary trajectory file
         ncFiles = dir([ncAuxFileDir sprintf('%d_*traj_aux.nc', floatNum)]);
         if (~isempty(ncFiles))
            trajFileName = ncFiles(1).name;
            trajFilePathName = [ncAuxFileDir trajFileName];
            
            outputFileName = [trajFileName(1:end-3) '.csv'];
            outputFilePathName = [ncAuxFileDir outputFileName];
            nc_traj_adj_2_csv_file(trajFilePathName, outputFilePathName, 1);
         end
      end
   else
      fprintf('WARNING: Directory not found: %s\n', ncFileDir);
   end
end

ellapsedTime = toc;
fprintf('done (Elapsed time is %.1f seconds)\n', ellapsedTime);

diary off;

return

% ------------------------------------------------------------------------------
% Convert one NetCDF trajectory file contents in CSV format.
%
% SYNTAX :
%  nc_traj_adj_2_csv_file(a_inputPathFileName, a_outputPathFileName, a_auxfileFlag)
%
% INPUT PARAMETERS :
%   a_inputPathFileName  : input NetCDF file path name
%   a_outputPathFileName : output CSV file path name
%   a_auxfileFlag        : 1 if it is an auxiliary file, 0 otherwise
%
% OUTPUT PARAMETERS :
%
% EXAMPLES :
%
% SEE ALSO :
% AUTHORS  : Jean-Philippe Rannou (Altran)(jean-philippe.rannou@altran.com)
% ------------------------------------------------------------------------------
% RELEASES :
%   11/02/2015 - RNU - creation
%   06/28/2021 - RNU - updated to convert TRAJ 3.1 and 3.2 formats
% ------------------------------------------------------------------------------
function nc_traj_adj_2_csv_file(a_inputPathFileName, a_outputPathFileName, a_auxfileFlag)

% sort the parameters according to a given list
FIX_WITH_ERROR_ELLIPSE = 1;


% check the NetCDF file
if ~(exist(a_inputPathFileName, 'file') == 2)
   fprintf('File not found : %s\n', a_inputPathFileName);
   return
end

if (~a_auxfileFlag)
   
   % open NetCDF file
   fCdf = netcdf.open(a_inputPathFileName, 'NC_NOWRITE');
   if (isempty(fCdf))
      fprintf('ERROR: Unable to open NetCDF input file: %s\n', a_inputPathFileName);
      return
   end
   
   formatVersion = str2double(netcdf.getVar(fCdf, netcdf.inqVarID(fCdf, 'FORMAT_VERSION'))');
   
   netcdf.close(fCdf);
   
   if (formatVersion == 3.1)
      
      % convert TRAJ 3.1 file
      if (~FIX_WITH_ERROR_ELLIPSE)
         nc_traj_adj_2_csv_file_3_1(a_inputPathFileName, a_outputPathFileName, a_auxfileFlag);
      else
         nc_traj_adj_2_csv_file_3_1_with_err_ellipse(a_inputPathFileName, a_outputPathFileName, a_auxfileFlag);
      end
      
   elseif (formatVersion == 3.2)
      
      % convert TRAJ 3.2 file
      if (~FIX_WITH_ERROR_ELLIPSE)
         nc_traj_adj_2_csv_file_3_2(a_inputPathFileName, a_outputPathFileName);
      else
         nc_traj_adj_2_csv_file_3_2_with_err_ellipse(a_inputPathFileName, a_outputPathFileName);
      end
      
   else
      fprintf('File format version is %g (3.1 or 3.2 expected) => exit\n', ...
         formatVersion, a_inputPathFileName);
      return
   end
else
   
   % convert TRAJ_AUX file
   nc_traj_adj_2_csv_file_3_1(a_inputPathFileName, a_outputPathFileName, a_auxfileFlag);
   
end

return

% ------------------------------------------------------------------------------
% Convert one NetCDF trajectory 3.1 file contents in CSV format.
%
% SYNTAX :
%  nc_traj_adj_2_csv_file_3_1(a_inputPathFileName, a_outputPathFileName, a_auxfileFlag)
%
% INPUT PARAMETERS :
%   a_inputPathFileName  : input NetCDF file path name
%   a_outputPathFileName : output CSV file path name
%   a_auxfileFlag        : 1 if it is an auxiliary file, 0 otherwise
%
% OUTPUT PARAMETERS :
%
% EXAMPLES :
%
% SEE ALSO :
% AUTHORS  : Jean-Philippe Rannou (Altran)(jean-philippe.rannou@altran.com)
% ------------------------------------------------------------------------------
% RELEASES :
%   11/02/2015 - RNU - creation
% ------------------------------------------------------------------------------
function nc_traj_adj_2_csv_file_3_1(a_inputPathFileName, a_outputPathFileName, a_auxfileFlag)

% QC flag values (char)
global g_decArgo_qcStrDef;
global g_decArgo_qcStrUnused2;


% input and output file names
[~, inputName, inputExt] = fileparts(a_inputPathFileName);
[outputPath, outputName, outputExt] = fileparts(a_outputPathFileName);
inputFileName = [inputName inputExt];
if (~a_auxfileFlag)
   ourputFileName = [outputName '_3.1' outputExt];
else
   ourputFileName = [outputName outputExt];
end
outputPathFileName = [outputPath '/' ourputFileName];
fprintf('Converting: %s to %s\n', inputFileName, ourputFileName);

% read the trajectory file contents
[nMeasData, nCycleData, historyData] = read_file_traj_3_1(a_inputPathFileName, 1, a_auxfileFlag);

% create CSV file
fidOut = fopen(outputPathFileName, 'wt');
if (fidOut == -1)
   fprintf('ERROR: Unable to create output file: %s\n', outputPathFileName);
   return
end

% compute parameter variable names and output formats

% if (length(nMeasData.paramNameList) <= 3)
%
%    paramNames = [];
%    adjParamNames = [];
%
%    for idParam = 1:length(nMeasData.paramNameList)
%       paramName  = char(nMeasData.paramNameList(idParam));
%       paramQcName  = char(nMeasData.paramQcNameList(idParam));
%
%       paramNames = [paramNames sprintf('; %s', paramName)];
%       paramNames = [paramNames sprintf('; %s', paramQcName)];
%    end
%
%    for idParam = 1:length(nMeasData.adjParamNameList)
%       adjParamName  = char(nMeasData.adjParamNameList(idParam));
%       adjParamQcName  = char(nMeasData.adjParamQcNameList(idParam));
%
%       adjParamNames = [adjParamNames sprintf('; %s', adjParamName)];
%       adjParamNames = [adjParamNames sprintf('; %s', adjParamQcName)];
%    end
%
% else

paramNames = [];
paramFormats = [];
paramQcNames = [];
paramQcFormats = [];

for idParam = 1:length(nMeasData.paramNameList)
   paramName  = nMeasData.paramNameList{idParam};
   paramDataNbDim = nMeasData.paramDataNbDim(idParam);
   paramQcName  = nMeasData.paramQcNameList{idParam};
   
   if (paramDataNbDim == 1)
      paramNames = [paramNames sprintf('; %s', paramName)];
      paramQcNames = [paramQcNames sprintf('; %s', paramQcName)];
      
      paramFormat = nMeasData.paramDataFormat{idParam};
      paramFormats = [paramFormats '; ' paramFormat];
      paramQcFormats = [paramQcFormats '; %c'];
   else
      for id = 1:paramDataNbDim
         paramNames = [paramNames sprintf('; %s#%d', paramName, id)];
         paramQcNames = [paramQcNames sprintf('; %s#%d', paramQcName, id)];
         
         paramFormat = nMeasData.paramDataFormat{idParam};
         paramFormats = [paramFormats '; ' paramFormat];
      end
      paramQcFormats = [paramQcFormats '; %c'];
   end
end

adjParamNames = [];
adjParamFormats = [];
adjParamQcNames = [];
adjParamQcFormats = [];

for idParam = 1:length(nMeasData.adjParamNameList)
   adjParamName  = nMeasData.adjParamNameList{idParam};
   adjParamDataNbDim = nMeasData.adjParamDataNbDim(idParam);
   adjParamQcName  = nMeasData.adjParamQcNameList{idParam};
   
   if (adjParamDataNbDim == 1)
      adjParamNames = [adjParamNames sprintf('; %s', adjParamName)];
      adjParamQcNames = [adjParamQcNames sprintf('; %s', adjParamQcName)];
      
      adjParamFormat = nMeasData.adjParamDataFormat{idParam};
      adjParamFormats = [adjParamFormats '; ' adjParamFormat];
      adjParamQcFormats = [adjParamQcFormats '; %c'];
   else
      for id = 1:adjParamDataNbDim
         adjParamNames = [adjParamNames sprintf('; %s#%d', adjParamName, id)];
         adjParamQcNames = [adjParamQcNames sprintf('; %s#%d', adjParamQcName, id)];
         
         adjParamFormat = nMeasData.adjParamDataFormat{idParam};
         adjParamFormats = [adjParamFormats '; ' adjParamFormat];
         adjParamQcFormats = [adjParamQcFormats '; %c'];
      end
   end
end
% end

paramNames = [paramNames repmat(';', 1, length(nMeasData.paramNameList)+1)];
if (~a_auxfileFlag)
   fprintf(fidOut, 'WMO;Type;Cy#;N_CYCLE var or Meas. code;JULD;;;JULD_ADJ;;;Lon;Lat;;; %s\n', [paramNames adjParamNames]);
else
   fprintf(fidOut, 'WMO;Type;Cy#;N_CYCLE var or Meas. code;JULD;;;JULD_ADJ;; %s\n', [paramNames adjParamNames]);
end

if (~isempty(nCycleData))
   cycles = unique([nCycleData.cycleNumberIndex; nMeasData.cycleNumber]);
else
   cycles = unique([nMeasData.cycleNumber]);
   cycles(find(cycles == 99999)) = [];
end
for cycleNumber = -1:max(cycles)
   
   if (~isempty(nCycleData))
      
      % print N_CYCLE data
      idCy = find(nCycleData.cycleNumberIndex == cycleNumber);
      
      if (~isempty(idCy))
         if (isfield(nCycleData, 'juldDescentStart'))
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DESCENT_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDescentStart(idCy)), ...
               nCycleData.juldDescentStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_STABILIZATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstStab(idCy)), ...
               nCycleData.juldFirstStabStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DESCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDescentEnd(idCy)), ...
               nCycleData.juldDescentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_PARK_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldParkStart(idCy)), ...
               nCycleData.juldParkStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_PARK_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldParkEnd(idCy)), ...
               nCycleData.juldParkEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DEEP_DESCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDeepDescentEnd(idCy)), ...
               nCycleData.juldDeepDescentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DEEP_PARK_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDeepParkStart(idCy)), ...
               nCycleData.juldDeepParkStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_ASCENT_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldAscentStart(idCy)), ...
               nCycleData.juldAscentStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_ASCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldAscentEnd(idCy)), ...
               nCycleData.juldAscentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_TRANSMISSION_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldTransmissionStart(idCy)), ...
               nCycleData.juldTransmissionStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_MESSAGE; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstMessage(idCy)), ...
               nCycleData.juldFirstMessageStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_LOCATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstLocation(idCy)), ...
               nCycleData.juldFirstLocationStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_LAST_LOCATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldLastLocation(idCy)), ...
               nCycleData.juldLastLocationStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_LAST_MESSAGE; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldLastMessage(idCy)), ...
               nCycleData.juldLastMessageStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_TRANSMISSION_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldTransmissionEnd(idCy)), ...
               nCycleData.juldTransmissionEndStatus(idCy));
            if (nCycleData.juldClockOffset(idCy) ~= 999999)
               fprintf(fidOut, '%s; CYCLE; %d; CLOCK_OFFSET; %s\n', ...
                  nMeasData.platformNumber, cycleNumber, ...
                  format_time_dec_argo(nCycleData.juldClockOffset(idCy)*24));
            else
               fprintf(fidOut, '%s; CYCLE; %d; CLOCK_OFFSET; %d\n', ...
                  nMeasData.platformNumber, cycleNumber, ...
                  nCycleData.juldClockOffset(idCy));
            end
            fprintf(fidOut, '%s; CYCLE; %d; GROUNDED; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.grounded(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; REPRESENTATIVE_PARK_PRESSURE; %.1f; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.representativeParkPressure(idCy), ...
               nCycleData.representativeParkPressureStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CONFIG_MISSION_NUMBER; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.configMissionNumber(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndex(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX_ADJUSTED; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndexAdj(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; DATA_MODE; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.dataMode(idCy));
         else
            fprintf(fidOut, '%s; CYCLE; %d; CONFIG_MISSION_NUMBER; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.configMissionNumber(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndex(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; DATA_MODE; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.dataMode(idCy));
         end
      end
   end
   
   % print N_MEASUREMENT data
   idMeas = find(nMeasData.cycleNumber == cycleNumber);
   
   for idM = 1:length(idMeas)
      
      if (nMeasData.measCode(idMeas(idM)) == 99999)
         continue
      end
      
      if (~isempty(nMeasData.paramData))
         paramData = nMeasData.paramData(idMeas(idM), :);
         paramQcData = nMeasData.paramQcData(idMeas(idM), :);
         paramQcData(find(paramQcData == -1)) = str2num(g_decArgo_qcStrUnused2);
         paramQcData = num2str(paramQcData')';
         paramQcData(find(paramQcData == g_decArgo_qcStrUnused2)) = g_decArgo_qcStrDef;
      end
      
      if (~isempty(nMeasData.adjParamData))
         paramDataAdj = nMeasData.adjParamData(idMeas(idM), :);
         paramQcDataAdj = nMeasData.adjParamQcData(idMeas(idM), :);
         paramQcDataAdj(find(paramQcDataAdj == -1)) = str2num(g_decArgo_qcStrUnused2);
         paramQcDataAdj = num2str(paramQcDataAdj')';
         paramQcDataAdj(find(paramQcDataAdj == g_decArgo_qcStrUnused2)) = g_decArgo_qcStrDef;
      end
      
      if (~a_auxfileFlag)
         if (~isempty(nMeasData.paramData))
            if (~isempty(nMeasData.adjParamData))
               if (isempty(nMeasData.juldAdj))
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; ; ; ; %8.3f; %7.3f; %c; %c; %s' paramFormats paramQcFormats ';' adjParamFormats adjParamQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     nMeasData.longitude(idMeas(idM)), ...
                     nMeasData.latitude(idMeas(idM)), ...
                     nMeasData.positionAccuracy(idMeas(idM)), ...
                     nMeasData.positionQc(idMeas(idM)), ...
                     deblank(nMeasData.positionSat(idMeas(idM))), ...
                     paramData, paramQcData, ...
                     paramDataAdj, paramQcDataAdj);
               else
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c; %8.3f; %7.3f; %c; %c; %s' paramFormats paramQcFormats ';' adjParamFormats adjParamQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                     nMeasData.juldAdjStatus(idMeas(idM)), ...
                     nMeasData.juldAdjQc(idMeas(idM)), ...
                     nMeasData.longitude(idMeas(idM)), ...
                     nMeasData.latitude(idMeas(idM)), ...
                     nMeasData.positionAccuracy(idMeas(idM)), ...
                     nMeasData.positionQc(idMeas(idM)), ...
                     deblank(nMeasData.positionSat(idMeas(idM))), ...
                     paramData, paramQcData, ...
                     paramDataAdj, paramQcDataAdj);
               end
            else
               if (isempty(nMeasData.juldAdj))
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; ; ; ; %8.3f; %7.3f; %c; %c; %s' paramFormats paramQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     nMeasData.longitude(idMeas(idM)), ...
                     nMeasData.latitude(idMeas(idM)), ...
                     nMeasData.positionAccuracy(idMeas(idM)), ...
                     nMeasData.positionQc(idMeas(idM)), ...
                     deblank(nMeasData.positionSat(idMeas(idM))), ...
                     paramData, paramQcData);
               else
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c; %8.3f; %7.3f; %c; %c; %s' paramFormats paramQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                     nMeasData.juldAdjStatus(idMeas(idM)), ...
                     nMeasData.juldAdjQc(idMeas(idM)), ...
                     nMeasData.longitude(idMeas(idM)), ...
                     nMeasData.latitude(idMeas(idM)), ...
                     nMeasData.positionAccuracy(idMeas(idM)), ...
                     nMeasData.positionQc(idMeas(idM)), ...
                     deblank(nMeasData.positionSat(idMeas(idM))), ...
                     paramData, paramQcData);
               end
            end
         else
            if (isempty(nMeasData.juldAdj))
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; ; ; ; %8.3f; %7.3f; %c; %c; %s\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))));
            else
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c; %8.3f; %7.3f; %c; %c; %s\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                  nMeasData.juldAdjStatus(idMeas(idM)), ...
                  nMeasData.juldAdjQc(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))));
            end
         end
      else
         if (~isempty(nMeasData.paramData))
            if (~isempty(nMeasData.adjParamData))
               if (isempty(nMeasData.juldAdj))
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c' paramFormats paramQcFormats ';' adjParamFormats adjParamQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     paramData, paramQcData, ...
                     paramDataAdj, paramQcDataAdj);
               else
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c' paramFormats paramQcFormats ';' adjParamFormats adjParamQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                     nMeasData.juldAdjStatus(idMeas(idM)), ...
                     nMeasData.juldAdjQc(idMeas(idM)), ...
                     paramData, paramQcData, ...
                     paramDataAdj, paramQcDataAdj);
               end
            else
               if (isempty(nMeasData.juldAdj))
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c' paramFormats paramQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     paramData, paramQcData);
               else
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c' paramFormats paramQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                     nMeasData.juldAdjStatus(idMeas(idM)), ...
                     nMeasData.juldAdjQc(idMeas(idM)), ...
                     paramData, paramQcData);
               end
            end
         else
            if (isempty(nMeasData.juldAdj))
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)));
            else
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                  nMeasData.juldAdjStatus(idMeas(idM)), ...
                  nMeasData.juldAdjQc(idMeas(idM)));
            end
         end
      end
   end
   
   if (~isempty(idMeas))
      fprintf(fidOut, '%s\n', ...
         nMeasData.platformNumber);
   end
end

% print HISTORY data
nHistory = 0;
idVal = find(strcmp(historyData, 'HISTORY_INSTITUTION') == 1, 1);
if (~isempty(idVal))
   [~, nHistory] = size(historyData{idVal+1});
end

for idH = 1:nHistory
   for id = 1:2:length(historyData)
      histoName = historyData{id};
      histoValue = historyData{id+1};
      if (isnumeric(histoValue))
         fprintf(fidOut, ' %s; HISTORY; %d; %s; %g\n', ...
            nMeasData.platformNumber, idH, ...
            strtrim(histoName), ...
            histoValue(idH));
      elseif (strcmp(histoName, 'HISTORY_INDEX_DIMENSION'))
         fprintf(fidOut, ' %s; HISTORY; %d; %s; %c\n', ...
            nMeasData.platformNumber, idH, ...
            strtrim(histoName), ...
            strtrim(histoValue(idH)));
      else
         fprintf(fidOut, ' %s; HISTORY; %d; %s; %s\n', ...
            nMeasData.platformNumber, idH, ...
            strtrim(histoName), ...
            strtrim(histoValue(:, idH)'));
      end
   end
end

fclose(fidOut);

return

% ------------------------------------------------------------------------------
% Convert one NetCDF trajectory 3.1 file contents in CSV format.
%
% SYNTAX :
%  nc_traj_adj_2_csv_file_3_1_with_err_ellipse(a_inputPathFileName, a_outputPathFileName, a_auxfileFlag)
%
% INPUT PARAMETERS :
%   a_inputPathFileName  : input NetCDF file path name
%   a_outputPathFileName : output CSV file path name
%   a_auxfileFlag        : 1 if it is an auxiliary file, 0 otherwise
%
% OUTPUT PARAMETERS :
%
% EXAMPLES :
%
% SEE ALSO :
% AUTHORS  : Jean-Philippe Rannou (Altran)(jean-philippe.rannou@altran.com)
% ------------------------------------------------------------------------------
% RELEASES :
%   08/23/2021 - RNU - creation
% ------------------------------------------------------------------------------
function nc_traj_adj_2_csv_file_3_1_with_err_ellipse(a_inputPathFileName, a_outputPathFileName, a_auxfileFlag)

% QC flag values (char)
global g_decArgo_qcStrDef;
global g_decArgo_qcStrUnused2;


% input and output file names
[~, inputName, inputExt] = fileparts(a_inputPathFileName);
[outputPath, outputName, outputExt] = fileparts(a_outputPathFileName);
inputFileName = [inputName inputExt];
if (~a_auxfileFlag)
   ourputFileName = [outputName '_3.1' outputExt];
else
   ourputFileName = [outputName outputExt];
end
outputPathFileName = [outputPath '/' ourputFileName];
fprintf('Converting: %s to %s\n', inputFileName, ourputFileName);

% read the trajectory file contents
[nMeasData, nCycleData, historyData] = read_file_traj_3_1(a_inputPathFileName, 1, a_auxfileFlag);

% create CSV file
fidOut = fopen(outputPathFileName, 'wt');
if (fidOut == -1)
   fprintf('ERROR: Unable to create output file: %s\n', outputPathFileName);
   return
end

% compute parameter variable names and output formats

% if (length(nMeasData.paramNameList) <= 3)
%
%    paramNames = [];
%    adjParamNames = [];
%
%    for idParam = 1:length(nMeasData.paramNameList)
%       paramName  = char(nMeasData.paramNameList(idParam));
%       paramQcName  = char(nMeasData.paramQcNameList(idParam));
%
%       paramNames = [paramNames sprintf('; %s', paramName)];
%       paramNames = [paramNames sprintf('; %s', paramQcName)];
%    end
%
%    for idParam = 1:length(nMeasData.adjParamNameList)
%       adjParamName  = char(nMeasData.adjParamNameList(idParam));
%       adjParamQcName  = char(nMeasData.adjParamQcNameList(idParam));
%
%       adjParamNames = [adjParamNames sprintf('; %s', adjParamName)];
%       adjParamNames = [adjParamNames sprintf('; %s', adjParamQcName)];
%    end
%
% else

paramNames = [];
paramFormats = [];
paramQcNames = [];
paramQcFormats = [];

for idParam = 1:length(nMeasData.paramNameList)
   paramName  = nMeasData.paramNameList{idParam};
   paramDataNbDim = nMeasData.paramDataNbDim(idParam);
   paramQcName  = nMeasData.paramQcNameList{idParam};
   
   if (paramDataNbDim == 1)
      paramNames = [paramNames sprintf('; %s', paramName)];
      paramQcNames = [paramQcNames sprintf('; %s', paramQcName)];
      
      paramFormat = nMeasData.paramDataFormat{idParam};
      paramFormats = [paramFormats '; ' paramFormat];
      paramQcFormats = [paramQcFormats '; %c'];
   else
      for id = 1:paramDataNbDim
         paramNames = [paramNames sprintf('; %s#%d', paramName, id)];
         paramQcNames = [paramQcNames sprintf('; %s#%d', paramQcName, id)];
         
         paramFormat = nMeasData.paramDataFormat{idParam};
         paramFormats = [paramFormats '; ' paramFormat];
      end
      paramQcFormats = [paramQcFormats '; %c'];
   end
end

adjParamNames = [];
adjParamFormats = [];
adjParamQcNames = [];
adjParamQcFormats = [];

for idParam = 1:length(nMeasData.adjParamNameList)
   adjParamName  = nMeasData.adjParamNameList{idParam};
   adjParamDataNbDim = nMeasData.adjParamDataNbDim(idParam);
   adjParamQcName  = nMeasData.adjParamQcNameList{idParam};
   
   if (adjParamDataNbDim == 1)
      adjParamNames = [adjParamNames sprintf('; %s', adjParamName)];
      adjParamQcNames = [adjParamQcNames sprintf('; %s', adjParamQcName)];
      
      adjParamFormat = nMeasData.adjParamDataFormat{idParam};
      adjParamFormats = [adjParamFormats '; ' adjParamFormat];
      adjParamQcFormats = [adjParamQcFormats '; %c'];
   else
      for id = 1:adjParamDataNbDim
         adjParamNames = [adjParamNames sprintf('; %s#%d', adjParamName, id)];
         adjParamQcNames = [adjParamQcNames sprintf('; %s#%d', adjParamQcName, id)];
         
         adjParamFormat = nMeasData.adjParamDataFormat{idParam};
         adjParamFormats = [adjParamFormats '; ' adjParamFormat];
         adjParamQcFormats = [adjParamQcFormats '; %c'];
      end
   end
end
% end

paramNames = [paramNames repmat(';', 1, length(nMeasData.paramNameList)+1)];
if (~a_auxfileFlag)
   fprintf(fidOut, 'WMO;Type;Cy#;N_CYCLE var or Meas. code;JULD;;;JULD_ADJ;;;Lon;Lat;ErrMaj;ErrMin;ErrAng;;; %s\n', [paramNames adjParamNames]);
else
   fprintf(fidOut, 'WMO;Type;Cy#;N_CYCLE var or Meas. code;JULD;;;JULD_ADJ;; %s\n', [paramNames adjParamNames]);
end

if (~isempty(nCycleData))
   cycles = unique([nCycleData.cycleNumberIndex; nMeasData.cycleNumber]);
else
   cycles = unique([nMeasData.cycleNumber]);
   cycles(find(cycles == 99999)) = [];
end
for cycleNumber = -1:max(cycles)
   
   if (~isempty(nCycleData))
      
      % print N_CYCLE data
      idCy = find(nCycleData.cycleNumberIndex == cycleNumber);
      
      if (~isempty(idCy))
         if (isfield(nCycleData, 'juldDescentStart'))
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DESCENT_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDescentStart(idCy)), ...
               nCycleData.juldDescentStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_STABILIZATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstStab(idCy)), ...
               nCycleData.juldFirstStabStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DESCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDescentEnd(idCy)), ...
               nCycleData.juldDescentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_PARK_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldParkStart(idCy)), ...
               nCycleData.juldParkStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_PARK_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldParkEnd(idCy)), ...
               nCycleData.juldParkEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DEEP_DESCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDeepDescentEnd(idCy)), ...
               nCycleData.juldDeepDescentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DEEP_PARK_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDeepParkStart(idCy)), ...
               nCycleData.juldDeepParkStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_ASCENT_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldAscentStart(idCy)), ...
               nCycleData.juldAscentStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_ASCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldAscentEnd(idCy)), ...
               nCycleData.juldAscentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_TRANSMISSION_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldTransmissionStart(idCy)), ...
               nCycleData.juldTransmissionStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_MESSAGE; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstMessage(idCy)), ...
               nCycleData.juldFirstMessageStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_LOCATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstLocation(idCy)), ...
               nCycleData.juldFirstLocationStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_LAST_LOCATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldLastLocation(idCy)), ...
               nCycleData.juldLastLocationStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_LAST_MESSAGE; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldLastMessage(idCy)), ...
               nCycleData.juldLastMessageStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_TRANSMISSION_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldTransmissionEnd(idCy)), ...
               nCycleData.juldTransmissionEndStatus(idCy));
            if (nCycleData.juldClockOffset(idCy) ~= 999999)
               fprintf(fidOut, '%s; CYCLE; %d; CLOCK_OFFSET; %s\n', ...
                  nMeasData.platformNumber, cycleNumber, ...
                  format_time_dec_argo(nCycleData.juldClockOffset(idCy)*24));
            else
               fprintf(fidOut, '%s; CYCLE; %d; CLOCK_OFFSET; %d\n', ...
                  nMeasData.platformNumber, cycleNumber, ...
                  nCycleData.juldClockOffset(idCy));
            end
            fprintf(fidOut, '%s; CYCLE; %d; GROUNDED; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.grounded(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; REPRESENTATIVE_PARK_PRESSURE; %.1f; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.representativeParkPressure(idCy), ...
               nCycleData.representativeParkPressureStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CONFIG_MISSION_NUMBER; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.configMissionNumber(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndex(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX_ADJUSTED; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndexAdj(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; DATA_MODE; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.dataMode(idCy));
         else
            fprintf(fidOut, '%s; CYCLE; %d; CONFIG_MISSION_NUMBER; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.configMissionNumber(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndex(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; DATA_MODE; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.dataMode(idCy));
         end
      end
   end
   
   % print N_MEASUREMENT data
   idMeas = find(nMeasData.cycleNumber == cycleNumber);
   
   for idM = 1:length(idMeas)
      
      if (nMeasData.measCode(idMeas(idM)) == 99999)
         continue
      end
      
      if (~isempty(nMeasData.paramData))
         paramData = nMeasData.paramData(idMeas(idM), :);
         paramQcData = nMeasData.paramQcData(idMeas(idM), :);
         paramQcData(find(paramQcData == -1)) = str2num(g_decArgo_qcStrUnused2);
         paramQcData = num2str(paramQcData')';
         paramQcData(find(paramQcData == g_decArgo_qcStrUnused2)) = g_decArgo_qcStrDef;
      end
      
      if (~isempty(nMeasData.adjParamData))
         paramDataAdj = nMeasData.adjParamData(idMeas(idM), :);
         paramQcDataAdj = nMeasData.adjParamQcData(idMeas(idM), :);
         paramQcDataAdj(find(paramQcDataAdj == -1)) = str2num(g_decArgo_qcStrUnused2);
         paramQcDataAdj = num2str(paramQcDataAdj')';
         paramQcDataAdj(find(paramQcDataAdj == g_decArgo_qcStrUnused2)) = g_decArgo_qcStrDef;
      end
      
      if (~a_auxfileFlag)
         if (~isempty(nMeasData.paramData))
            if (~isempty(nMeasData.adjParamData))
               if (isempty(nMeasData.juldAdj))
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; ; ; ; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s' paramFormats paramQcFormats ';' adjParamFormats adjParamQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     nMeasData.longitude(idMeas(idM)), ...
                     nMeasData.latitude(idMeas(idM)), ...
                     nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                     nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                     nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                     nMeasData.positionAccuracy(idMeas(idM)), ...
                     nMeasData.positionQc(idMeas(idM)), ...
                     deblank(nMeasData.positionSat(idMeas(idM))), ...
                     paramData, paramQcData, ...
                     paramDataAdj, paramQcDataAdj);
               else
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s' paramFormats paramQcFormats ';' adjParamFormats adjParamQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                     nMeasData.juldAdjStatus(idMeas(idM)), ...
                     nMeasData.juldAdjQc(idMeas(idM)), ...
                     nMeasData.longitude(idMeas(idM)), ...
                     nMeasData.latitude(idMeas(idM)), ...
                     nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                     nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                     nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                     nMeasData.positionAccuracy(idMeas(idM)), ...
                     nMeasData.positionQc(idMeas(idM)), ...
                     deblank(nMeasData.positionSat(idMeas(idM))), ...
                     paramData, paramQcData, ...
                     paramDataAdj, paramQcDataAdj);
               end
            else
               if (isempty(nMeasData.juldAdj))
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; ; ; ; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s' paramFormats paramQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     nMeasData.longitude(idMeas(idM)), ...
                     nMeasData.latitude(idMeas(idM)), ...
                     nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                     nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                     nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                     nMeasData.positionAccuracy(idMeas(idM)), ...
                     nMeasData.positionQc(idMeas(idM)), ...
                     deblank(nMeasData.positionSat(idMeas(idM))), ...
                     paramData, paramQcData);
               else
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s' paramFormats paramQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                     nMeasData.juldAdjStatus(idMeas(idM)), ...
                     nMeasData.juldAdjQc(idMeas(idM)), ...
                     nMeasData.longitude(idMeas(idM)), ...
                     nMeasData.latitude(idMeas(idM)), ...
                     nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                     nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                     nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                     nMeasData.positionAccuracy(idMeas(idM)), ...
                     nMeasData.positionQc(idMeas(idM)), ...
                     deblank(nMeasData.positionSat(idMeas(idM))), ...
                     paramData, paramQcData);
               end
            end
         else
            if (isempty(nMeasData.juldAdj))
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; ; ; ; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))));
            else
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                  nMeasData.juldAdjStatus(idMeas(idM)), ...
                  nMeasData.juldAdjQc(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))));
            end
         end
      else
         if (~isempty(nMeasData.paramData))
            if (~isempty(nMeasData.adjParamData))
               if (isempty(nMeasData.juldAdj))
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c' paramFormats paramQcFormats ';' adjParamFormats adjParamQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     paramData, paramQcData, ...
                     paramDataAdj, paramQcDataAdj);
               else
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c' paramFormats paramQcFormats ';' adjParamFormats adjParamQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                     nMeasData.juldAdjStatus(idMeas(idM)), ...
                     nMeasData.juldAdjQc(idMeas(idM)), ...
                     paramData, paramQcData, ...
                     paramDataAdj, paramQcDataAdj);
               end
            else
               if (isempty(nMeasData.juldAdj))
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c' paramFormats paramQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     paramData, paramQcData);
               else
                  fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c' paramFormats paramQcFormats '\n'], ...
                     nMeasData.platformNumber, ...
                     idMeas(idM), ...
                     nMeasData.cycleNumber(idMeas(idM)), ...
                     get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                     julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                     nMeasData.juldStatus(idMeas(idM)), ...
                     nMeasData.juldQc(idMeas(idM)), ...
                     julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                     nMeasData.juldAdjStatus(idMeas(idM)), ...
                     nMeasData.juldAdjQc(idMeas(idM)), ...
                     paramData, paramQcData);
               end
            end
         else
            if (isempty(nMeasData.juldAdj))
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)));
            else
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %s; %c; %c\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                  nMeasData.juldAdjStatus(idMeas(idM)), ...
                  nMeasData.juldAdjQc(idMeas(idM)));
            end
         end
      end
   end
   
   if (~isempty(idMeas))
      fprintf(fidOut, '%s\n', ...
         nMeasData.platformNumber);
   end
end

% print HISTORY data
nHistory = 0;
idVal = find(strcmp(historyData, 'HISTORY_INSTITUTION') == 1, 1);
if (~isempty(idVal))
   [~, nHistory] = size(historyData{idVal+1});
end

for idH = 1:nHistory
   for id = 1:2:length(historyData)
      histoName = historyData{id};
      histoValue = historyData{id+1};
      if (isnumeric(histoValue))
         fprintf(fidOut, ' %s; HISTORY; %d; %s; %g\n', ...
            nMeasData.platformNumber, idH, ...
            strtrim(histoName), ...
            histoValue(idH));
      elseif (strcmp(histoName, 'HISTORY_INDEX_DIMENSION'))
         fprintf(fidOut, ' %s; HISTORY; %d; %s; %c\n', ...
            nMeasData.platformNumber, idH, ...
            strtrim(histoName), ...
            strtrim(histoValue(idH)));
      else
         fprintf(fidOut, ' %s; HISTORY; %d; %s; %s\n', ...
            nMeasData.platformNumber, idH, ...
            strtrim(histoName), ...
            strtrim(histoValue(:, idH)'));
      end
   end
end

fclose(fidOut);

return

% ------------------------------------------------------------------------------
% Convert one NetCDF trajectory file contents in CSV format.
%
% SYNTAX :
%  nc_traj_adj_2_csv_file_3_2(a_inputPathFileName, a_outputPathFileName)
%
% INPUT PARAMETERS :
%   a_inputPathFileName  : input NetCDF file path name
%   a_outputPathFileName : output CSV file path name
%
% OUTPUT PARAMETERS :
%
% EXAMPLES :
%
% SEE ALSO :
% AUTHORS  : Jean-Philippe Rannou (Altran)(jean-philippe.rannou@altran.com)
% ------------------------------------------------------------------------------
% RELEASES :
%   06/28/2021 - RNU - creation
% ------------------------------------------------------------------------------
function nc_traj_adj_2_csv_file_3_2(a_inputPathFileName, a_outputPathFileName)

% QC flag values (char)
global g_decArgo_qcStrDef;
global g_decArgo_qcStrUnused2;


% input and output file names
[~, inputName, inputExt] = fileparts(a_inputPathFileName);
[outputPath, outputName, outputExt] = fileparts(a_outputPathFileName);
inputFileName = [inputName inputExt];
ourputFileName = [outputName '_3.2' outputExt];
outputPathFileName = [outputPath '/' ourputFileName];
fprintf('Converting: %s to %s\n', inputFileName, ourputFileName);

% read the trajectory file contents
[nMeasData, nCycleData, calibrationData, historyData] = read_file_traj_3_2(a_inputPathFileName);

% create CSV file
fidOut = fopen(outputPathFileName, 'wt');
if (fidOut == -1)
   fprintf('ERROR: Unable to create output file: %s\n', outputPathFileName);
   return
end

% compute parameter variable names and output formats
paramNames = [];
paramNums = [];
paramFormats = [];
paramQcNums = [];
paramQcFormats = [];
paramDataModeNums = [];
paramDataModeFormats = [];

for idParam = 1:length(nMeasData.paramNameList)
   paramName  = nMeasData.paramNameList{idParam};
   paramNum  = nMeasData.paramNum{idParam};
   paramDataNbDim = nMeasData.paramDataNbDim(idParam);
   paramQcName  = nMeasData.paramQcNameList{idParam};
   paramQcNum  = nMeasData.paramQcNum{idParam};
   paramDataModeNum  = nMeasData.paramDataModeNum{idParam};
   
   if (paramDataNbDim == 1)
      paramNames = [paramNames sprintf('; %s', paramName)];
      paramNums = [paramNums sprintf('; %s', paramNum)];
      paramQcNums = [paramQcNums sprintf('; %s', paramQcNum)];
      paramDataModeNums = [paramDataModeNums sprintf('; %s', paramDataModeNum)];
      
      paramFormat = nMeasData.paramDataFormat{idParam};
      paramFormats = [paramFormats '; ' paramFormat];
      paramQcFormats = [paramQcFormats '; %c'];
      paramDataModeFormats = [paramDataModeFormats '; %c'];
   else
      for id = 1:paramDataNbDim
         paramNames = [paramNames sprintf('; %s#%d', paramName, id)];
         paramNums = [paramNums sprintf('; %s', paramNum)];
         
         paramFormat = nMeasData.paramDataFormat{idParam};
         paramFormats = [paramFormats '; ' paramFormat];
      end
      paramQcNums = [paramQcNums sprintf('; %s', paramQcNum)];
      paramDataModeNums = [paramDataModeNums sprintf('; %s', paramDataModeNum)];
      paramQcFormats = [paramQcFormats '; %c'];
      paramDataModeFormats = [paramDataModeFormats '; %c'];
   end
end

adjParamNames = [];
adjParamNums = [];
adjParamFormats = [];
adjParamQcNames = [];
adjParamQcNums = [];
adjParamQcFormats = [];

for idParam = 1:length(nMeasData.adjParamNameList)
   adjParamName  = nMeasData.adjParamNameList{idParam};
   adjParamNum  = nMeasData.adjParamNum{idParam};
   adjParamDataNbDim = nMeasData.adjParamDataNbDim(idParam);
   adjParamQcName  = nMeasData.adjParamQcNameList{idParam};
   adjParamQcNum  = nMeasData.adjParamQcNum{idParam};
   
   if (adjParamDataNbDim == 1)
      adjParamNames = [adjParamNames sprintf('; %s', adjParamName)];
      adjParamNums = [adjParamNums sprintf('; %s', adjParamNum)];
      adjParamQcNums = [adjParamQcNums sprintf('; %s', adjParamQcNum)];
      
      adjParamFormat = nMeasData.adjParamDataFormat{idParam};
      adjParamFormats = [adjParamFormats '; ' adjParamFormat];
      adjParamQcFormats = [adjParamQcFormats '; %c'];
   else
      for id = 1:adjParamDataNbDim
         adjParamNames = [adjParamNames sprintf('; %s#%d', adjParamName, id)];
         adjParamNums = [adjParamNums sprintf('; %s', adjParamNum)];
         
         adjParamFormat = nMeasData.adjParamDataFormat{idParam};
         adjParamFormats = [adjParamFormats '; ' adjParamFormat];
      end
      adjParamQcNums = [adjParamQcNums sprintf('; %s', adjParamQcNum)];
      adjParamQcFormats = [adjParamQcFormats '; %c'];
   end
end

adjErrorParamNames = [];
adjErrorParamFormats = [];

for idParam = 1:length(nMeasData.adjErrorParamNameList)
   adjErrorParamName  = nMeasData.adjErrorParamNameList{idParam};
   adjErrorParamDataNbDim = nMeasData.adjErrorParamDataNbDim(idParam);
   
   if (adjErrorParamDataNbDim == 1)
      adjErrorParamNames = [adjErrorParamNames sprintf('; %s', adjErrorParamName)];
      
      adjErrorParamFormat = nMeasData.adjErrorParamDataFormat{idParam};
      adjErrorParamFormats = [adjErrorParamFormats '; ' adjErrorParamFormat];
   else
      for id = 1:adjErrorParamDataNbDim
         adjErrorParamNames = [adjErrorParamNames sprintf('; %s#%d', adjErrorParamName, id)];
         
         adjErrorParamFormat = nMeasData.adjErrorParamDataFormat{idParam};
         adjErrorParamFormats = [adjErrorParamFormats '; ' adjErrorParamFormat];
      end
   end
end

fprintf(fidOut, 'WMO;Type;Cy#;N_CYCLE var or Meas. code;JULD;Ss;Qc;M;JULD_ADJ;Ss;Qc;Lon;Lat;Cl;Qc;Sat %s\n', ...
   [paramNums ';' paramQcNums ';' paramDataModeNums ';' adjParamNums ';' adjParamQcNums]);

paramNames = [paramNames ...
   ';;' repmat('Qc;', 1, length(nMeasData.paramNameList)) ...
   ';' repmat('M;', 1, length(nMeasData.paramNameList))];
adjParamNames = [adjParamNames ...
   ';;' repmat('Q;', 1, length(nMeasData.adjParamNameList))];
fprintf(fidOut, 'WMO;Type;Cy#;N_CYCLE var or Meas. code;JULD;Ss;Qc;M;JULD_ADJ;Ss;Qc;Lon;Lat;Cl;Qc;Sat %s\n', [paramNames adjParamNames adjErrorParamNames]);

if (~isempty(nCycleData))
   cycles = unique([nCycleData.cycleNumberIndex; nMeasData.cycleNumber]);
else
   cycles = unique([nMeasData.cycleNumber]);
   cycles(find(cycles == 99999)) = [];
end
for cycleNumber = -1:max(cycles)
   
   if (~isempty(nCycleData))
      
      % print N_CYCLE data
      idCy = find(nCycleData.cycleNumberIndex == cycleNumber);
      
      if (~isempty(idCy))
         if (isfield(nCycleData, 'juldDescentStart'))
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DESCENT_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDescentStart(idCy)), ...
               nCycleData.juldDescentStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_STABILIZATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstStab(idCy)), ...
               nCycleData.juldFirstStabStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DESCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDescentEnd(idCy)), ...
               nCycleData.juldDescentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_PARK_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldParkStart(idCy)), ...
               nCycleData.juldParkStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_PARK_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldParkEnd(idCy)), ...
               nCycleData.juldParkEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DEEP_DESCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDeepDescentEnd(idCy)), ...
               nCycleData.juldDeepDescentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DEEP_PARK_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDeepParkStart(idCy)), ...
               nCycleData.juldDeepParkStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_ASCENT_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldAscentStart(idCy)), ...
               nCycleData.juldAscentStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_ASCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldAscentEnd(idCy)), ...
               nCycleData.juldAscentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_TRANSMISSION_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldTransmissionStart(idCy)), ...
               nCycleData.juldTransmissionStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_MESSAGE; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstMessage(idCy)), ...
               nCycleData.juldFirstMessageStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_LOCATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstLocation(idCy)), ...
               nCycleData.juldFirstLocationStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_LAST_LOCATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldLastLocation(idCy)), ...
               nCycleData.juldLastLocationStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_LAST_MESSAGE; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldLastMessage(idCy)), ...
               nCycleData.juldLastMessageStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_TRANSMISSION_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldTransmissionEnd(idCy)), ...
               nCycleData.juldTransmissionEndStatus(idCy));
            if (nCycleData.juldClockOffset(idCy) ~= 999999)
               fprintf(fidOut, '%s; CYCLE; %d; CLOCK_OFFSET; %s\n', ...
                  nMeasData.platformNumber, cycleNumber, ...
                  format_time_dec_argo(nCycleData.juldClockOffset(idCy)*24));
            else
               fprintf(fidOut, '%s; CYCLE; %d; CLOCK_OFFSET; %d\n', ...
                  nMeasData.platformNumber, cycleNumber, ...
                  nCycleData.juldClockOffset(idCy));
            end
            fprintf(fidOut, '%s; CYCLE; %d; GROUNDED; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.grounded(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; REPRESENTATIVE_PARK_PRESSURE; %.1f; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.representativeParkPressure(idCy), ...
               nCycleData.representativeParkPressureStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CONFIG_MISSION_NUMBER; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.configMissionNumber(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndex(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX_ADJUSTED; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndexAdj(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; DATA_MODE; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.dataMode(idCy));
         else
            fprintf(fidOut, '%s; CYCLE; %d; CONFIG_MISSION_NUMBER; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.configMissionNumber(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndex(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; DATA_MODE; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.dataMode(idCy));
         end
      end
   end
   
   % print N_MEASUREMENT data
   idMeas = find(nMeasData.cycleNumber == cycleNumber);
   
   for idM = 1:length(idMeas)
      
      if (nMeasData.measCode(idMeas(idM)) == 99999)
         continue
      end
      
      if (~isempty(nMeasData.paramData))
         paramData = nMeasData.paramData(idMeas(idM), :);
         paramQcData = nMeasData.paramQcData(idMeas(idM), :);
         paramQcData(find(paramQcData == -1)) = str2num(g_decArgo_qcStrUnused2);
         paramQcData = num2str(paramQcData')';
         paramQcData(find(paramQcData == g_decArgo_qcStrUnused2)) = g_decArgo_qcStrDef;
      end
      
      if (~isempty(nMeasData.paramDataMode))
         paramDataMode = nMeasData.paramDataMode(:, idMeas(idM));
      end
      
      if (~isempty(nMeasData.adjParamData))
         paramDataAdj = nMeasData.adjParamData(idMeas(idM), :);
         paramQcDataAdj = nMeasData.adjParamQcData(idMeas(idM), :);
         paramQcDataAdj(find(paramQcDataAdj == -1)) = str2num(g_decArgo_qcStrUnused2);
         paramQcDataAdj = num2str(paramQcDataAdj')';
         paramQcDataAdj(find(paramQcDataAdj == g_decArgo_qcStrUnused2)) = g_decArgo_qcStrDef;
      end
      
      if (~isempty(nMeasData.adjErrorParamData))
         paramDataAdjError = nMeasData.adjErrorParamData(idMeas(idM), :);
      end
      
      if (~isempty(nMeasData.paramData))
         if (~isempty(nMeasData.adjParamData))
            if (isempty(nMeasData.juldAdj))
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; ; ; ; %8.3f; %7.3f; %c; %c; %s' paramFormats ';' paramQcFormats ';' paramDataModeFormats ';' adjParamFormats ';' adjParamQcFormats ';' adjErrorParamFormats '\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.juldDataMode(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))), ...
                  paramData, paramQcData, paramDataMode, ...
                  paramDataAdj, paramQcDataAdj, paramDataAdjError);
            else
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; %s; %c; %c; %8.3f; %7.3f; %c; %c; %s' paramFormats ';' paramQcFormats ';' paramDataModeFormats ';' adjParamFormats ';' adjParamQcFormats ';' adjErrorParamFormats '\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.juldDataMode(idMeas(idM)), ...
                  julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                  nMeasData.juldAdjStatus(idMeas(idM)), ...
                  nMeasData.juldAdjQc(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))), ...
                  paramData, paramQcData, paramDataMode, ...
                  paramDataAdj, paramQcDataAdj, paramDataAdjError);
            end
         else
            if (isempty(nMeasData.juldAdj))
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; ; ; ; %8.3f; %7.3f; %c; %c; %s' paramFormats ';' paramQcFormats ';' paramDataModeFormats ';' '\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.juldDataMode(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))), ...
                  paramData, paramQcData, paramDataMode);
            else
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; %s; %c; %c; %8.3f; %7.3f; %c; %c; %s' paramFormats ';' paramQcFormats ';' paramDataModeFormats ';' '\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.juldDataMode(idMeas(idM)), ...
                  julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                  nMeasData.juldAdjStatus(idMeas(idM)), ...
                  nMeasData.juldAdjQc(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))), ...
                  paramData, paramQcData, paramDataMode);
            end
         end
      else
         if (isempty(nMeasData.juldAdj))
            fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; ; ; ; %8.3f; %7.3f; %c; %c; %s\n'], ...
               nMeasData.platformNumber, ...
               idMeas(idM), ...
               nMeasData.cycleNumber(idMeas(idM)), ...
               get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
               julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
               nMeasData.juldStatus(idMeas(idM)), ...
               nMeasData.juldQc(idMeas(idM)), ...
               nMeasData.juldDataMode(idMeas(idM)), ...
               nMeasData.longitude(idMeas(idM)), ...
               nMeasData.latitude(idMeas(idM)), ...
               nMeasData.positionAccuracy(idMeas(idM)), ...
               nMeasData.positionQc(idMeas(idM)), ...
               deblank(nMeasData.positionSat(idMeas(idM))));
         else
            fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; %s; %c; %c; %8.3f; %7.3f; %c; %c; %s\n'], ...
               nMeasData.platformNumber, ...
               idMeas(idM), ...
               nMeasData.cycleNumber(idMeas(idM)), ...
               get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
               julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
               nMeasData.juldStatus(idMeas(idM)), ...
               nMeasData.juldQc(idMeas(idM)), ...
               nMeasData.juldDataMode(idMeas(idM)), ...
               julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
               nMeasData.juldAdjStatus(idMeas(idM)), ...
               nMeasData.juldAdjQc(idMeas(idM)), ...
               nMeasData.longitude(idMeas(idM)), ...
               nMeasData.latitude(idMeas(idM)), ...
               nMeasData.positionAccuracy(idMeas(idM)), ...
               nMeasData.positionQc(idMeas(idM)), ...
               deblank(nMeasData.positionSat(idMeas(idM))));
         end
      end
   end
   
   if (~isempty(idMeas))
      fprintf(fidOut, '%s\n', ...
         nMeasData.platformNumber);
   end
end

% print CALIBRATION data
if (0)
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_PARAMETER') == 1, 1);
   calibParam = calibrationData{idVal+1};
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_EQUATION') == 1, 1);
   calibEquation = calibrationData{idVal+1};
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_COEFFICIENT') == 1, 1);
   calibCoef = calibrationData{idVal+1};
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_COMMENT') == 1, 1);
   calibComment = calibrationData{idVal+1};
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_DATE') == 1, 1);
   calibDae = calibrationData{idVal+1};
   
   [~, nParam, nCalib] = size(calibParam);
   
   for idC = 1:nCalib
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_PARAMETER', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibParam(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_EQUATION', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibEquation(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_COEFFICIENT', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibCoef(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_COMMENT', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibComment(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_DATE', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibDae(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
   end
   
   % print HISTORY data
   nHistory = 0;
   idVal = find(strcmp(historyData, 'HISTORY_INSTITUTION') == 1, 1);
   if (~isempty(idVal))
      [~, nHistory] = size(historyData{idVal+1});
   end
   
   for idH = 1:nHistory
      for id = 1:2:length(historyData)
         histoName = historyData{id};
         histoValue = historyData{id+1};
         if (isnumeric(histoValue))
            fprintf(fidOut, ' %s; HISTORY; %d; %s; %g\n', ...
               nMeasData.platformNumber, idH, ...
               strtrim(histoName), ...
               histoValue(idH));
         elseif (strcmp(histoName, 'HISTORY_INDEX_DIMENSION'))
            fprintf(fidOut, ' %s; HISTORY; %d; %s; %c\n', ...
               nMeasData.platformNumber, idH, ...
               strtrim(histoName), ...
               strtrim(histoValue(idH)));
         else
            fprintf(fidOut, ' %s; HISTORY; %d; %s; %s\n', ...
               nMeasData.platformNumber, idH, ...
               strtrim(histoName), ...
               strtrim(histoValue(:, idH)'));
         end
      end
   end
end

fclose(fidOut);

return

% ------------------------------------------------------------------------------
% Convert one NetCDF trajectory file contents in CSV format.
%
% SYNTAX :
%  nc_traj_adj_2_csv_file_3_2_with_err_ellipse(a_inputPathFileName, a_outputPathFileName)
%
% INPUT PARAMETERS :
%   a_inputPathFileName  : input NetCDF file path name
%   a_outputPathFileName : output CSV file path name
%
% OUTPUT PARAMETERS :
%
% EXAMPLES :
%
% SEE ALSO :
% AUTHORS  : Jean-Philippe Rannou (Altran)(jean-philippe.rannou@altran.com)
% ------------------------------------------------------------------------------
% RELEASES :
%   06/28/2021 - RNU - creation
% ------------------------------------------------------------------------------
function nc_traj_adj_2_csv_file_3_2_with_err_ellipse(a_inputPathFileName, a_outputPathFileName)

% QC flag values (char)
global g_decArgo_qcStrDef;
global g_decArgo_qcStrUnused2;


% input and output file names
[~, inputName, inputExt] = fileparts(a_inputPathFileName);
[outputPath, outputName, outputExt] = fileparts(a_outputPathFileName);
inputFileName = [inputName inputExt];
ourputFileName = [outputName '_3.2' outputExt];
outputPathFileName = [outputPath '/' ourputFileName];
fprintf('Converting: %s to %s\n', inputFileName, ourputFileName);

% read the trajectory file contents
[nMeasData, nCycleData, calibrationData, historyData] = read_file_traj_3_2(a_inputPathFileName);

% create CSV file
fidOut = fopen(outputPathFileName, 'wt');
if (fidOut == -1)
   fprintf('ERROR: Unable to create output file: %s\n', outputPathFileName);
   return
end

% compute parameter variable names and output formats
paramNames = [];
paramNums = [];
paramFormats = [];
paramQcNums = [];
paramQcFormats = [];
paramDataModeNums = [];
paramDataModeFormats = [];

for idParam = 1:length(nMeasData.paramNameList)
   paramName  = nMeasData.paramNameList{idParam};
   paramNum  = nMeasData.paramNum{idParam};
   paramDataNbDim = nMeasData.paramDataNbDim(idParam);
   paramQcName  = nMeasData.paramQcNameList{idParam};
   paramQcNum  = nMeasData.paramQcNum{idParam};
   paramDataModeNum  = nMeasData.paramDataModeNum{idParam};
   
   if (paramDataNbDim == 1)
      paramNames = [paramNames sprintf('; %s', paramName)];
      paramNums = [paramNums sprintf('; %s', paramNum)];
      paramQcNums = [paramQcNums sprintf('; %s', paramQcNum)];
      paramDataModeNums = [paramDataModeNums sprintf('; %s', paramDataModeNum)];
      
      paramFormat = nMeasData.paramDataFormat{idParam};
      paramFormats = [paramFormats '; ' paramFormat];
      paramQcFormats = [paramQcFormats '; %c'];
      paramDataModeFormats = [paramDataModeFormats '; %c'];
   else
      for id = 1:paramDataNbDim
         paramNames = [paramNames sprintf('; %s#%d', paramName, id)];
         paramNums = [paramNums sprintf('; %s', paramNum)];
         
         paramFormat = nMeasData.paramDataFormat{idParam};
         paramFormats = [paramFormats '; ' paramFormat];
      end
      paramQcNums = [paramQcNums sprintf('; %s', paramQcNum)];
      paramDataModeNums = [paramDataModeNums sprintf('; %s', paramDataModeNum)];
      paramQcFormats = [paramQcFormats '; %c'];
      paramDataModeFormats = [paramDataModeFormats '; %c'];
   end
end

adjParamNames = [];
adjParamNums = [];
adjParamFormats = [];
adjParamQcNames = [];
adjParamQcNums = [];
adjParamQcFormats = [];

for idParam = 1:length(nMeasData.adjParamNameList)
   adjParamName  = nMeasData.adjParamNameList{idParam};
   adjParamNum  = nMeasData.adjParamNum{idParam};
   adjParamDataNbDim = nMeasData.adjParamDataNbDim(idParam);
   adjParamQcName  = nMeasData.adjParamQcNameList{idParam};
   adjParamQcNum  = nMeasData.adjParamQcNum{idParam};
   
   if (adjParamDataNbDim == 1)
      adjParamNames = [adjParamNames sprintf('; %s', adjParamName)];
      adjParamNums = [adjParamNums sprintf('; %s', adjParamNum)];
      adjParamQcNums = [adjParamQcNums sprintf('; %s', adjParamQcNum)];
      
      adjParamFormat = nMeasData.adjParamDataFormat{idParam};
      adjParamFormats = [adjParamFormats '; ' adjParamFormat];
      adjParamQcFormats = [adjParamQcFormats '; %c'];
   else
      for id = 1:adjParamDataNbDim
         adjParamNames = [adjParamNames sprintf('; %s#%d', adjParamName, id)];
         adjParamNums = [adjParamNums sprintf('; %s', adjParamNum)];
         
         adjParamFormat = nMeasData.adjParamDataFormat{idParam};
         adjParamFormats = [adjParamFormats '; ' adjParamFormat];
      end
      adjParamQcNums = [adjParamQcNums sprintf('; %s', adjParamQcNum)];
      adjParamQcFormats = [adjParamQcFormats '; %c'];
   end
end

adjErrorParamNames = [];
adjErrorParamFormats = [];

for idParam = 1:length(nMeasData.adjErrorParamNameList)
   adjErrorParamName  = nMeasData.adjErrorParamNameList{idParam};
   adjErrorParamDataNbDim = nMeasData.adjErrorParamDataNbDim(idParam);
   
   if (adjErrorParamDataNbDim == 1)
      adjErrorParamNames = [adjErrorParamNames sprintf('; %s', adjErrorParamName)];
      
      adjErrorParamFormat = nMeasData.adjErrorParamDataFormat{idParam};
      adjErrorParamFormats = [adjErrorParamFormats '; ' adjErrorParamFormat];
   else
      for id = 1:adjErrorParamDataNbDim
         adjErrorParamNames = [adjErrorParamNames sprintf('; %s#%d', adjErrorParamName, id)];
         
         adjErrorParamFormat = nMeasData.adjErrorParamDataFormat{idParam};
         adjErrorParamFormats = [adjErrorParamFormats '; ' adjErrorParamFormat];
      end
   end
end

fprintf(fidOut, 'WMO;Type;Cy#;N_CYCLE var or Meas. code;JULD;Ss;Qc;M;JULD_ADJ;Ss;Qc;Lon;Lat;ErrMaj;ErrMin;ErrAng;Acc;Qc;Sat %s\n', ...
   [paramNums ';' paramQcNums ';' paramDataModeNums ';' adjParamNums ';' adjParamQcNums]);

paramNames = [paramNames ...
   ';;' repmat('Qc;', 1, length(nMeasData.paramNameList)) ...
   ';' repmat('M;', 1, length(nMeasData.paramNameList))];
adjParamNames = [adjParamNames ...
   ';;' repmat('Q;', 1, length(nMeasData.adjParamNameList))];
fprintf(fidOut, 'WMO;Type;Cy#;N_CYCLE var or Meas. code;JULD;Ss;Qc;M;JULD_ADJ;Ss;Qc;Lon;Lat;ErrMaj;ErrMin;ErrAng;Acc;Qc;Sat %s\n', [paramNames adjParamNames adjErrorParamNames]);

if (~isempty(nCycleData))
   cycles = unique([nCycleData.cycleNumberIndex; nMeasData.cycleNumber]);
else
   cycles = unique([nMeasData.cycleNumber]);
   cycles(find(cycles == 99999)) = [];
end
for cycleNumber = -1:max(cycles)
   
   if (~isempty(nCycleData))
      
      % print N_CYCLE data
      idCy = find(nCycleData.cycleNumberIndex == cycleNumber);
      
      if (~isempty(idCy))
         if (isfield(nCycleData, 'juldDescentStart'))
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DESCENT_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDescentStart(idCy)), ...
               nCycleData.juldDescentStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_STABILIZATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstStab(idCy)), ...
               nCycleData.juldFirstStabStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DESCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDescentEnd(idCy)), ...
               nCycleData.juldDescentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_PARK_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldParkStart(idCy)), ...
               nCycleData.juldParkStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_PARK_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldParkEnd(idCy)), ...
               nCycleData.juldParkEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DEEP_DESCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDeepDescentEnd(idCy)), ...
               nCycleData.juldDeepDescentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_DEEP_PARK_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldDeepParkStart(idCy)), ...
               nCycleData.juldDeepParkStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_ASCENT_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldAscentStart(idCy)), ...
               nCycleData.juldAscentStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_ASCENT_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldAscentEnd(idCy)), ...
               nCycleData.juldAscentEndStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_TRANSMISSION_START; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldTransmissionStart(idCy)), ...
               nCycleData.juldTransmissionStartStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_MESSAGE; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstMessage(idCy)), ...
               nCycleData.juldFirstMessageStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_FIRST_LOCATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldFirstLocation(idCy)), ...
               nCycleData.juldFirstLocationStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_LAST_LOCATION; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldLastLocation(idCy)), ...
               nCycleData.juldLastLocationStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_LAST_MESSAGE; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldLastMessage(idCy)), ...
               nCycleData.juldLastMessageStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; JULD_TRANSMISSION_END; %s; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               julian_2_gregorian_dec_argo(nCycleData.juldTransmissionEnd(idCy)), ...
               nCycleData.juldTransmissionEndStatus(idCy));
            if (nCycleData.juldClockOffset(idCy) ~= 999999)
               fprintf(fidOut, '%s; CYCLE; %d; CLOCK_OFFSET; %s\n', ...
                  nMeasData.platformNumber, cycleNumber, ...
                  format_time_dec_argo(nCycleData.juldClockOffset(idCy)*24));
            else
               fprintf(fidOut, '%s; CYCLE; %d; CLOCK_OFFSET; %d\n', ...
                  nMeasData.platformNumber, cycleNumber, ...
                  nCycleData.juldClockOffset(idCy));
            end
            fprintf(fidOut, '%s; CYCLE; %d; GROUNDED; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.grounded(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; REPRESENTATIVE_PARK_PRESSURE; %.1f; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.representativeParkPressure(idCy), ...
               nCycleData.representativeParkPressureStatus(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CONFIG_MISSION_NUMBER; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.configMissionNumber(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndex(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX_ADJUSTED; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndexAdj(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; DATA_MODE; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.dataMode(idCy));
         else
            fprintf(fidOut, '%s; CYCLE; %d; CONFIG_MISSION_NUMBER; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.configMissionNumber(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; CYCLE_NUMBER_INDEX; %d\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.cycleNumberIndex(idCy));
            fprintf(fidOut, '%s; CYCLE; %d; DATA_MODE; %c\n', ...
               nMeasData.platformNumber, cycleNumber, ...
               nCycleData.dataMode(idCy));
         end
      end
   end
   
   % print N_MEASUREMENT data
   idMeas = find(nMeasData.cycleNumber == cycleNumber);
   
   for idM = 1:length(idMeas)
      
      if (nMeasData.measCode(idMeas(idM)) == 99999)
         continue
      end
      
      if (~isempty(nMeasData.paramData))
         paramData = nMeasData.paramData(idMeas(idM), :);
         paramQcData = nMeasData.paramQcData(idMeas(idM), :);
         paramQcData(find(paramQcData == -1)) = str2num(g_decArgo_qcStrUnused2);
         paramQcData = num2str(paramQcData')';
         paramQcData(find(paramQcData == g_decArgo_qcStrUnused2)) = g_decArgo_qcStrDef;
      end
      
      if (~isempty(nMeasData.paramDataMode))
         paramDataMode = nMeasData.paramDataMode(:, idMeas(idM));
      end
      
      if (~isempty(nMeasData.adjParamData))
         paramDataAdj = nMeasData.adjParamData(idMeas(idM), :);
         paramQcDataAdj = nMeasData.adjParamQcData(idMeas(idM), :);
         paramQcDataAdj(find(paramQcDataAdj == -1)) = str2num(g_decArgo_qcStrUnused2);
         paramQcDataAdj = num2str(paramQcDataAdj')';
         paramQcDataAdj(find(paramQcDataAdj == g_decArgo_qcStrUnused2)) = g_decArgo_qcStrDef;
      end
      
      if (~isempty(nMeasData.adjErrorParamData))
         paramDataAdjError = nMeasData.adjErrorParamData(idMeas(idM), :);
      end
      
      if (~isempty(nMeasData.paramData))
         if (~isempty(nMeasData.adjParamData))
            if (isempty(nMeasData.juldAdj))
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; ; ; ; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s' paramFormats ';' paramQcFormats ';' paramDataModeFormats ';' adjParamFormats ';' adjParamQcFormats ';' adjErrorParamFormats '\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.juldDataMode(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))), ...
                  paramData, paramQcData, paramDataMode, ...
                  paramDataAdj, paramQcDataAdj, paramDataAdjError);
            else
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; %s; %c; %c; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s' paramFormats ';' paramQcFormats ';' paramDataModeFormats ';' adjParamFormats ';' adjParamQcFormats ';' adjErrorParamFormats '\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.juldDataMode(idMeas(idM)), ...
                  julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                  nMeasData.juldAdjStatus(idMeas(idM)), ...
                  nMeasData.juldAdjQc(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))), ...
                  paramData, paramQcData, paramDataMode, ...
                  paramDataAdj, paramQcDataAdj, paramDataAdjError);
            end
         else
            if (isempty(nMeasData.juldAdj))
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; ; ; ; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s' paramFormats ';' paramQcFormats ';' paramDataModeFormats ';' '\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.juldDataMode(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))), ...
                  paramData, paramQcData, paramDataMode);
            else
               fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; %s; %c; %c; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s' paramFormats ';' paramQcFormats ';' paramDataModeFormats ';' '\n'], ...
                  nMeasData.platformNumber, ...
                  idMeas(idM), ...
                  nMeasData.cycleNumber(idMeas(idM)), ...
                  get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
                  julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
                  nMeasData.juldStatus(idMeas(idM)), ...
                  nMeasData.juldQc(idMeas(idM)), ...
                  nMeasData.juldDataMode(idMeas(idM)), ...
                  julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
                  nMeasData.juldAdjStatus(idMeas(idM)), ...
                  nMeasData.juldAdjQc(idMeas(idM)), ...
                  nMeasData.longitude(idMeas(idM)), ...
                  nMeasData.latitude(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
                  nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
                  nMeasData.positionAccuracy(idMeas(idM)), ...
                  nMeasData.positionQc(idMeas(idM)), ...
                  deblank(nMeasData.positionSat(idMeas(idM))), ...
                  paramData, paramQcData, paramDataMode);
            end
         end
      else
         if (isempty(nMeasData.juldAdj))
            fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; ; ; ; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s\n'], ...
               nMeasData.platformNumber, ...
               idMeas(idM), ...
               nMeasData.cycleNumber(idMeas(idM)), ...
               get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
               julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
               nMeasData.juldStatus(idMeas(idM)), ...
               nMeasData.juldQc(idMeas(idM)), ...
               nMeasData.juldDataMode(idMeas(idM)), ...
               nMeasData.longitude(idMeas(idM)), ...
               nMeasData.latitude(idMeas(idM)), ...
               nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
               nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
               nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
               nMeasData.positionAccuracy(idMeas(idM)), ...
               nMeasData.positionQc(idMeas(idM)), ...
               deblank(nMeasData.positionSat(idMeas(idM))));
         else
            fprintf(fidOut, ['%s; MEAS #%04d; %3d; %s; %s; %c; %c; %c; %s; %c; %c; %8.3f; %7.3f; %g; %g; %g; %c; %c; %s\n'], ...
               nMeasData.platformNumber, ...
               idMeas(idM), ...
               nMeasData.cycleNumber(idMeas(idM)), ...
               get_meas_code_name(nMeasData.measCode(idMeas(idM))), ...
               julian_2_gregorian_dec_argo(nMeasData.juld(idMeas(idM))), ...
               nMeasData.juldStatus(idMeas(idM)), ...
               nMeasData.juldQc(idMeas(idM)), ...
               nMeasData.juldDataMode(idMeas(idM)), ...
               julian_2_gregorian_dec_argo(nMeasData.juldAdj(idMeas(idM))), ...
               nMeasData.juldAdjStatus(idMeas(idM)), ...
               nMeasData.juldAdjQc(idMeas(idM)), ...
               nMeasData.longitude(idMeas(idM)), ...
               nMeasData.latitude(idMeas(idM)), ...
               nMeasData.positioAxErrEllMajor(idMeas(idM)), ...
               nMeasData.positioAxErrEllMinor(idMeas(idM)), ...
               nMeasData.positioAxErrEllAngle(idMeas(idM)), ...
               nMeasData.positionAccuracy(idMeas(idM)), ...
               nMeasData.positionQc(idMeas(idM)), ...
               deblank(nMeasData.positionSat(idMeas(idM))));
         end
      end
   end
   
   if (~isempty(idMeas))
      fprintf(fidOut, '%s\n', ...
         nMeasData.platformNumber);
   end
end

if (0)
   % print CALIBRATION data
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_PARAMETER') == 1, 1);
   calibParam = calibrationData{idVal+1};
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_EQUATION') == 1, 1);
   calibEquation = calibrationData{idVal+1};
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_COEFFICIENT') == 1, 1);
   calibCoef = calibrationData{idVal+1};
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_COMMENT') == 1, 1);
   calibComment = calibrationData{idVal+1};
   idVal = find(strcmp(calibrationData, 'SCIENTIFIC_CALIB_DATE') == 1, 1);
   calibDae = calibrationData{idVal+1};
   
   [~, nParam, nCalib] = size(calibParam);
   
   for idC = 1:nCalib
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_PARAMETER', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibParam(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_EQUATION', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibEquation(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_COEFFICIENT', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibCoef(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_COMMENT', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibComment(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
      fprintf(fidOut, ' %s; CALIBRATION; %d; SCIENTIFIC_CALIB_DATE', ...
         nMeasData.platformNumber, idC);
      for idP = 1:nParam
         fprintf(fidOut, '; %s', ...
            strtrim(calibDae(:, idP, idC)'));
      end
      fprintf(fidOut, '\n');
   end
   
   % print HISTORY data
   nHistory = 0;
   idVal = find(strcmp(historyData, 'HISTORY_INSTITUTION') == 1, 1);
   if (~isempty(idVal))
      [~, nHistory] = size(historyData{idVal+1});
   end
   
   for idH = 1:nHistory
      for id = 1:2:length(historyData)
         histoName = historyData{id};
         histoValue = historyData{id+1};
         if (isnumeric(histoValue))
            fprintf(fidOut, ' %s; HISTORY; %d; %s; %g\n', ...
               nMeasData.platformNumber, idH, ...
               strtrim(histoName), ...
               histoValue(idH));
         elseif (strcmp(histoName, 'HISTORY_INDEX_DIMENSION'))
            fprintf(fidOut, ' %s; HISTORY; %d; %s; %c\n', ...
               nMeasData.platformNumber, idH, ...
               strtrim(histoName), ...
               strtrim(histoValue(idH)));
         else
            fprintf(fidOut, ' %s; HISTORY; %d; %s; %s\n', ...
               nMeasData.platformNumber, idH, ...
               strtrim(histoName), ...
               strtrim(histoValue(:, idH)'));
         end
      end
   end
end

fclose(fidOut);

return
