function [error] = tsg_readBucketData( hTsgGUI, filename)
% Function to read Bucket data in ASCII format. 
%
% Input
% -----
% hTsgGUI ............ Handel to the main user interface
% filename ........... Data filename
%
% Output
% ------
% error .............. 1: OK - -1 : an error occured
%
% The data are store using setappdata - Variable name : 'bucketASCII'
%
% Caution : replace the fill-value with NaN
% $Id$

% Open the file
% -------------
fid = fopen( filename, 'r' );

error = -1;
if fid ~= -1

    % Read the file - skip 2 lines
    % ----------------------------
    fgetl( fid ); fgetl( fid );
    bucketData = fscanf(fid,'%d %d %d %d %d %d %f %f %f', [9 Inf])';
    
    % Every variable are put in a structure
    % -------------------------------------
    bucketASCII.TIME      = datenum(bucketData(:,1), bucketData(:,2), ...
                                    bucketData(:,3), bucketData(:,4), ...
                                    bucketData(:,5), bucketData(:,6));
    bucketASCII.LATITUDE  = bucketData(:,7);
    bucketASCII.LONGITUDE = bucketData(:,8);
    bucketASCII.PSAL      = bucketData(:,9);
    bucketASCII.PSAL_QC   = zeros(size(bucketASCII.PSAL));

    % Save the data in the application GUI
    % ------------------------------------
    setappdata( hTsgGUI, 'bucketASCII', bucketASCII );
    
    % Clear the Workspace
    % -------------------
    clear bucketData
    
    % Close the file
    % --------------
    fclose( fid );
    
    % Everything OK
    % -------------
    error = 1;
end
