Skip to content
Snippets Groups Projects
readTsgDataLabview.m 6.4 KiB
Newer Older
function [error] = readTsgDataLabview( hTsgGUI, filename  )
% 
% Fonction de lecture des fichiers TSG issus du programme
% d'acquisition LabView XXX
%
% [error] = rdtsglabview( Fichier )
%
% Input
% hTsgGUI ............ Handel to the main user interface
% filename ........... Data filename
%
% Output
% error .............. 1: OK - -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( hTsgGUI, 'tsg_data');

% 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
    
  % 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' ) == 1
    
    % Lecture et decodage du fichier TSG LabView. En sortie
    % ------------------------------------------
    [date, time, lat, lon, sst, sss, cond, TsgRaw, sog, cog] =...
                decodeficlabview( fid, DELIMITER, ColNo, paraName, nPara ); 

    % Nombre de lignes du fichier et allocation de memoire
    % ----------------------------------------------------
    nblig = length(sst);
  
    % 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 );

    % Every variable are put in a structure
    % -------------------------------------
    blanc           = char(blanks(1)*ones(nblig,1));
    tsg.DAYD        = NaN*ones(nblig,1);
    tsg.DAYD(noNaN) = datenum(...
                    [char(date(noNaN)) blanc(noNaN) char(time(noNaN))],...
                    'dd/mm/yyyy HH:MM:SS');
                
    % 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
    
    %tsg.SSJT_FREQ = [];
    %tsg.CNDC_FREQ = [];
    
%     for i=1:length(TsgRaw)
%       if TsgRaw(i,:) == NaN
%         ssjt_freq = NaN;
%         ssjt_freq = NaN;
%       else
%         freq = sscanf(TsgRaw(i,:), '%4x%4x');
%         ssjt_freq = freq(1)/19 + 2100;
%         cndc_freq = sqrt(freq(2)*2100 + 6250000);
%       end
%       tsg.SSJT_FREQ =  [tsg.SSJT_FREQ;ssjt_freq];
%       tsg.CNDC_FREQ = [tsg.CNDC_FREQ;cndc_freq];
%     end
   

    % save original date
    % ------------------
    tsg.DATE       = datestr(  tsg.DAYD, 'yyyymmddHHMMSS' );
    tsg.LATX       = lat;
    tsg.LONX       = lon;
    tsg.SSJT       = sst;
    tsg.SSPS       = sss;
    % 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    = tsg.qc.active.Code * ones(nblig,1);
    
    % populate tsg.file structure
    % ---------------------------
    tsg.file.name = filename;

    % Save the data in the application GUI
    % ------------------------------------
    setappdata( hTsgGUI, 'tsg_data', tsg );

    % Clear the Workspace
    % -------------------
    clear date time lat lon sst sss cond condRaw sog cog
    
  end  % fin de boucle if strcmp(choix,'yes')
    
  % Fermeture du fichier
  % --------------------
  fclose( fid );

  % Display time to read file on console
  % ------------------------------------
  t = toc; fprintf('...done (%6.2f sec).\n\n',t);

  error = 1;
  
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