Newer
Older
function [error] = readTsgDataLabview( hMainFig, filename )
% readTsgDataLabview( hMainFig, filename )
% This function read TSG files from acquisition software LabView
% Usage:
% [error] = readTsgDataLabview( hMainFig, filename )
% Input:
% hMainFig ........... Handle to the main user interface
% filename ........... Data filename
%
% error .............. 1 : OK
% 0 : Selection canceled
% -1 : an error occured
% Description of columns from Labview files header:
% Date
% Pc Date - RMC_Date - ZDA_Date
% Pc Time - RMC_Time - ZDA_Time - GLL_Time
% Position

jacques.grelet_ird.fr
committed
% RMC_Latmn - RMC_Lonmn - RMC_Latdec - RMC_Londec -
% GGA_Latdec - GGA_Londec - GLL_Latdec - GLL_Londec
% SBE21_Temp1 - SBE21_Sal - SBE21_StdDev - SBE21_Cond - SBE21_Raw
% Heading speed and direction
% RMC_Sog - RMC_Cog - 3026L_Cog - 3026L_Sog
% RMC_Status - GGA_Hog - VTG True Track - T - Magn Track - M -
% Ground spd- N - Ground Spd - K - dd - tete - ttt
% SBE21_AD1,SBE21_AD2
% Focntions appelees : choixparametres, decodeficlabview, readTsgIniLabview
%
% 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 = ',';

jacques.grelet_ird.fr
committed
% Ouverture du fichier '.CSV'
% ---------------------------
fid = fopen( filename, 'r');
% Teste si le fichier existe
% --------------------------
if fid ~= -1

jacques.grelet_ird.fr
committed
% 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 );

jacques.grelet_ird.fr
committed

jacques.grelet_ird.fr
committed
% Lecture et decodage du fichier TSG LabView. En sortie
% ------------------------------------------
[date, time, lat, lon, sst, sss, sssStd, cond, TsgRaw, sog, cog, flow] =...

jacques.grelet_ird.fr
committed
decodeficlabview( fid, DELIMITER, ColNo, paraName, nPara );
Yves Gouriou
committed
% Nombre de lignes du fichier
% ---------------------------
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
% 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

jacques.grelet_ird.fr
committed
% see http://www.seabird.com/pdf_documents/manuals/21_022.pdf
% section 4: data output format

jacques.grelet_ird.fr
committed
% initialize and set <data>_FREQ to NaN
% -------------------------------------
ssjt_freq = nan(nbrecords,1);
cndc_freq = nan(nbrecords,1);

jacques.grelet_ird.fr
committed
% loop on all TsgRaw data
% -----------------------
% remove leading and trailing white space when string contain NaN
% ---------------------------------------------------------------
if ~strcmp(strtrim(TsgRaw(i,:)), 'NaN')

jacques.grelet_ird.fr
committed
% get frenquencies from string
% ----------------------------
[freq, count, errmsg] = sscanf(TsgRaw(i,:), '%4x%4x%*s');

jacques.grelet_ird.fr
committed
% compute frenquencies
% --------------------
if isempty( errmsg )
if ~isempty( freq )
ssjt_freq(i) = freq(1)/19 + 2100;
cndc_freq(i) = sqrt(freq(2)*2100 + 6250000);
end
else
sprintf( '\n Function readTsgDataLabview' );
sprintf( 'Problem reading frequency %s \n', errmsg );
sprintf( 'Records # %d\n', i );
Yves Gouriou
committed
end
end
end

jacques.grelet_ird.fr
committed
% Indices where dates and hours are correct (no NaN)
% --------------------------------------------------
Yves Gouriou
committed
noNaN = find( strcmp( date, 'NaN') == 0 & strcmp( time, 'NaN') == 0 );

jacques.grelet_ird.fr
committed
Yves Gouriou
committed
% 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 data. If date or time are incorrect data are deleted
% ------------------------------------------------------------------

jacques.grelet_ird.fr
committed
tsg.DATE = deblank( datestr(tsg.DAYD,...
tsg.preference.date_format_variable) );
Yves Gouriou
committed
tsg.LATX = lat(noNaN);
tsg.LONX = lon(noNaN);
tsg.SSJT = sst(noNaN);
tsg.SSPS = sss(noNaN);

jacques.grelet_ird.fr
committed
if isempty( find(isnan(tsg.LATX) == 0) )
warndlg( '...No latitude ', 'LabView error dialog');
error = -1;
return;
end
if isempty( find(isnan(tsg.LONX) == 0) )
warndlg( '...No longitude', 'LabView error dialog');
error = -1;
return;
end

jacques.grelet_ird.fr
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(flow) && ~isempty( find( isnan(flow) == 0))
tsg.FLOW = flow(noNaN);
if ~isempty( ssjt_freq ) && ~isempty( find( isnan(ssjt_freq) == 0))
tsg.SSJT_FREQ = ssjt_freq(noNaN);
end
if ~isempty( cndc_freq ) && ~isempty( find( isnan(cndc_freq) == 0))
tsg.CNDC_FREQ = cndc_freq(noNaN);
Yves Gouriou
committed
% Set active code to NOCONTROL
% ----------------------------

jacques.grelet_ird.fr
committed
tsg.qc.active.Code = tsg.qc.hash.NO_CONTROL.code;
Yves Gouriou
committed
% 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 );
Yves Gouriou
committed
% populate tsg.file structure
% ---------------------------
[tsg.file.pathstr, tsg.file.name, tsg.file.ext] = ...
Yves Gouriou
committed
tsg.file.type = 'LABVIEW';

jacques.grelet_ird.fr
committed
% check if .ini file exist in data
% --------------------------------
file_ini = fullfile(tsg.file.pathstr, strcat(tsg.file.name, '.ini'));
% check if filename exist and should be open in read mode
% -------------------------------------------------------
file_ini_id = fopen(file_ini, 'r');
if (file_ini_id ~= -1)
% display information on command window
% --------------------------------------
fprintf('\n...reading %s : \n', file_ini);

jacques.grelet_ird.fr
committed
% Save the data in the application GUI
% ------------------------------------
setappdata( hMainFig, 'tsg_data', tsg );
% read .ini file
% --------------
readTsgIniLabview(hMainFig, file_ini_id);
% Get the tsg struct from the application GUI
% -------------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');
end
Yves Gouriou
committed
% Clear the Workspace
% -------------------
clear date time lat lon sst sss cond condRaw sog cog

jacques.grelet_ird.fr
committed
% Keep somme information for the log file
% ---------------------------------------
tsg.report.tsgfile = filename;

jacques.grelet_ird.fr
committed
tsg.report.nodate = nbrecords - length(noNaN);
% Save the data in the application GUI
% ------------------------------------
setappdata( hMainFig, 'tsg_data', tsg );

jacques.grelet_ird.fr
committed
% Perform somme automatic tests
% -----------------------------
automaticQC( hMainFig )
Yves Gouriou
committed
% Close the file
% --------------
fclose( fid );
% Display time to read file on console
% ------------------------------------
t = toc; fprintf('...done (%6.2f sec).\n\n',t);
Yves Gouriou
committed
Yves Gouriou
committed
error = 1;
else
Yves Gouriou
committed
Yves Gouriou
committed
% Gestion d'erreur All Date and Time at NaN
% -----------------------------------------
msg_error = {['TSG_LABVIEW file_read : ' filename];...

jacques.grelet_ird.fr
committed
' '; '...no valid Date and Time'};
Yves Gouriou
committed
warndlg( msg_error, 'LabView error dialog');
error = -1;
return;
Yves Gouriou
committed
Yves Gouriou
committed
end
Yves Gouriou
committed
else
% Parameter selection has been canceled in lbvParameterChoice
% -----------------------------------------------------------
error = -1;
end % fin de boucle if strcmp(choix,'ok')

jacques.grelet_ird.fr
committed
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;

jacques.grelet_ird.fr
committed