Skip to content
Snippets Groups Projects
readTsgDataLabview.m 8.39 KiB
Newer Older
function [error] = readTsgDataLabview( hMainFig, filename  )
% 
% Fonction de lecture des fichiers TSG issus du programme
% d'acquisition LabView XXX
%
% [error] = rdtsglabview( Fichier )
%
% Input
% hMainFig ............ Handel to the main user interface
% filename ........... Data filename
%
% Output
% error .............. 1 : OK
%                      0 : Selection canceled
%                     -1 : an error occured
%
% Liste des intitules des colonnes dans les entetes des fichiers Labview :
% Date
%     Pc Date  - RMC_Date - ZDA_Date
% Heure
%     Pc Time - RMC_Time - ZDA_Time - GLL_Time
% Position
%     RMC_Latmn - RMC_Lonmn - RMC_Latdec - RMC_Londec - 
%     GGA_Latdec - GGA_Londec - GLL_Latdec - GLL_Londec
% Mesures
%     SBE21_Temp1 - SBE21_Sal - SBE21_Cond - SBE21_Raw
% Vitesse et cap
%     RMC_Sog - RMC_Cog - 3026L_Cog - 3026L_Sog
% Autre
%     RMC_Status - GGA_Hog - VTG True Track - T - Magn Track - M -
%     Ground spd- N - Ground Spd - K - dd - tete - ttt


% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');
% Get fieldnames of tsg structure
% -------------------------------
tsgNames     = fieldnames(tsg);
nbFieldNames = length( tsgNames );

% Display read file info on console
% ---------------------------------
fprintf('\nREAD_LABVIEW_FILE\n'); tic;

% Chaque colonne du fichier est separee par DELIMITER
% ---------------------------------------------------
DELIMITER = ',';
  
% Ouverture du fichier '.CSV'
% ---------------------------
fid = fopen( filename, 'r');

% Teste si le fichier existe
% --------------------------
if fid ~= -1
    
  % Display more info about read file on console
  % --------------------------------------------
  fprintf('...reading %s : ', filename);

  % Choix des parametres qui seront utilises par TSG-QC
  % ColNo : structure des numeros de colonnes a conserver
  % ---------------------------------------------------
  [choix, ColNo, paraName, nPara] = choixparametres( fid, DELIMITER );
  
  if strcmp( choix, 'ok' )
    
    % Lecture et decodage du fichier TSG LabView. En sortie
    % ------------------------------------------
    [date, time, lat, lon, sst, sss, cond, TsgRaw, sog, cog] =...
Yves Gouriou's avatar
Yves Gouriou committed
                    decodeficlabview( fid, DELIMITER, ColNo, paraName, nPara ); 
    % Nombre de lignes du fichier
    % ---------------------------
    nbrecords = length(sst);
    % decode tsg raw data
    % -------------------
    % The SBE 21 outputs data in raw, hexadecimal form as described below.
    % SBE 21 Format (F1) - ttttccccrrrrrruuuvvvwwwxxx (use this format if you
    % will be using SEASAVE to acquire real-time data and/or SBE Data
    % Processing to process the data)
    % where
    % tttt = primary temperature
    % cccc = conductivity
    % rrrrrr = remote temperature (from SBE 38 or SBE 3 remote sensor)
    % uuu, vvv, www, xxx = voltage outputs 0, 1, 2, and 3 respectively
    % # = attention character
    % nnnn = lineal sample count (0, 1, 2, etc.)
    %
    % Calculation of the parameter from the data is described below (use the decimal
    % equivalent of the hex data in the equations).
    %
    % 1. Temperature
    % temperature frequency (Hz) = ( tttt / 19 ) + 2100
    % 2. Conductivity
    % conductivity frequency (Hz) = square root [ ( cccc * 2100 ) + 6250000 ]
    % 3. SBE 3 secondary temperature (if SBE3=Y)
    % SBE 3 temperature frequency (Hz) = rrrrrr / 256
    % 4. SBE 38 secondary temperature (if SBE38=Y)
    % SBE 38 temperature psuedo frequency (Hz) = rrrrrr / 256
    % 5. External voltage 0 (if 1 or more external voltages defined with SVx)
    % external voltage 0 (volts) = uuu / 819
    % 6. External voltage 1 (if 2 or more external voltages defined with SVx)
    % external voltage 1 (volts) = vvv / 819
    % 7. External voltage 2 (if 3 or more external voltages defined with SVx)
    % external voltage 2 (volts) = www / 819
    % 8. External voltage 3 (if 4 external voltages defined with SVx)
    % external voltage 3 (volts) = xxx / 819
    
    % see http://www.seabird.com/pdf_documents/manuals/21_022.pdf
    % section 4: data output format
    
    % initialize and set <data>_FREQ to NaN
    % -------------------------------------
    tsg.SSJT_FREQ = nan(nbrecords,1);
    tsg.CNDC_FREQ = nan(nbrecords,1);
    % loop on all TsgRaw data
    % -----------------------
    for i=1:nbrecords
      
      % remove leading and trailing white space when string contain NaN
      % ---------------------------------------------------------------
      if ~strcmp(strtrim(TsgRaw(i,:)), 'NaN')
        
        % get frenquencies from string
        % ----------------------------
        freq = sscanf(TsgRaw(i,:), '%4x%4x%*');
        
        % compute frenquencies
        % --------------------
        if ~isempty( freq )
          tsg.SSJT_FREQ(i) = freq(1)/19 + 2100;
          tsg.CNDC_FREQ(i) =  sqrt(freq(2)*2100 + 6250000);
        end
  
    % Indice des lignes pour lesquelles les dates
    % ou les heures ne sont pas a NaN
    % -------------------------------------------
    noNaN = find( strcmp( date, 'NaN') == 0 & strcmp( time, 'NaN') == 0 );
    if ~isempty( noNaN )
      % Every variable are put in a structure
      % -------------------------------------
      blanc = char( blanks(1) * ones(length(noNaN),1));

      tsg.DAYD = datenum( [char(date(noNaN)) blanc char(time(noNaN))],...
        'dd/mm/yyyy HH:MM:SS');

      % Save original date
      % ------------------
      tsg.DATE       = deblank( datestr(  tsg.DAYD, 'yyyymmddHHMMSS' ) );
      tsg.LATX       = lat(noNaN);
      tsg.LONX       = lon(noNaN);
      tsg.SSJT       = sst(noNaN);
      tsg.SSPS       = sss(noNaN);
Yves Gouriou's avatar
Yves Gouriou committed
      if ~isempty(cond) && ~isempty( find(isnan(cond) == 0)) 
        tsg.CNDC     = cond(noNaN);
      end
      if ~isempty(sog) && ~isempty( find( isnan(sog) == 0))
        tsg.SPDC     = sog(noNaN);
      end
      if ~isempty( tsg.SSJT_FREQ )
        tsg.SSJT_FREQ  = tsg.SSJT_FREQ(noNaN);
      end
      if ~isempty( tsg.CNDC_FREQ )
        tsg.CNDC_FREQ  = tsg.CNDC_FREQ(noNaN);
      end

      % Set active code to NOCONTROL
      % ----------------------------
      tsg.qc.active.Code = get(tsg.qc.hash, 'NO_CONTROL', 'code');

      % Set salinity QC (SSPS_QC) par NOCONTROL code
      % --------------------------------------------
      tsg.SSPS_QC = castByteQC( tsg.qc.active.Code, noNaN );
      tsg.SSJT_QC = castByteQC( tsg.qc.active.Code, noNaN );
      [tsg.file.pathstr, tsg.file.name, tsg.file.ext, tsg.file.versn] = ...
        fileparts(filename);
      tsg.file.type = 'LABVIEW';

      % Clear the Workspace
      % -------------------
      clear date time lat lon sst sss cond condRaw sog cog
      
      % Keep somme information for the log file
      % ---------------------------------------
      tsg.report.tsgfile  = filename;
      tsg.report.nodate   = nbrecords - length(noNaN); 
      % Save the data in the application GUI
      % ------------------------------------
      setappdata( hMainFig, 'tsg_data', tsg );
      
      % Perform somme automatic tests
      % -----------------------------
      automaticQC( hMainFig )

      % Close the file
      % --------------
      fclose( fid );

      % Display time to read file on console
      % ------------------------------------
      t = toc; fprintf('...done (%6.2f sec).\n\n',t);
      % Gestion d'erreur All Date and Time at NaN
      % -----------------------------------------
      msg_error = ['TSG_LABVIEW file_read : ' filename];
      warndlg( msg_error, 'LabView error dialog');
      sprintf('...no valid Date and Time %s\n', filename);
      error = -1;
      return;
    % Parameter selection has been canceled in lbvParameterChoice
    % -----------------------------------------------------------
    error = -1;

  end  % fin de boucle if strcmp(choix,'ok')
  
else

  % Gestion d'erreur ouverture de fichier
  % -------------------------------------
  msg_error = ['TSG_LABVIEW file_lecture : Open file error : ' filename];
  warndlg( msg_error, 'LabView error dialog');
  sprintf('...cannot locate %s\n', filename);
  error = -1;
  return;
    
end