function sbtl=read_xlsx_o2_all(nfsamplexlsx,nbtl,profile2get)
% This function read chimical analyses (salinity and oxygen)
% from US191 IMAGO Excel file
% return a structure sbtl(itab) where iprof index is determined by 100*Stat+Cast
%
% J Grelet - PANDORA cruise - IRD-US191 - July 2012
% A.Ganachaud-Dec 2014
% A.Ganachaud-Mar 2015 - coded for profiles, not necessarily stations
% ----------------------------------------------------------
% variables with prefix s (= sample)

% synthax error, corrected by glazeO - 14/01/2015 nfsample=nfsamplexxlsx;
nfsample=nfsamplexlsx;
%matlab R2012 did not want to read sheet names on .xlsx and data on xls
%but Ok on xlsx with R2014

% open xls info and get informations
% ----------------------------------
[~, sheet] = xlsfinfo( nfsample);

% pressure, line 25 to 35, column 16 (P) pour rosette 11 bouteilles
% pressure, line 25 to 36, column 16 (P) pour rosette 12 bouteilles
% pressure, line 25 to 46, column 16 (P) pour rosette 22 bouteilles
% ----------------
lines    = 25:25+nbtl-1;

% get sheets name and number in Excel file
% ----------------------------------------
% correction glazeO 14/01/2015 match = regexp( sheet, '^station\s*(\d+)', 'tokens');
match = regexp( sheet, '^station\s*(\d+)', 'tokens');
if ~isempty(match)
    gprofiles = find(cellfun('isempty', match) == 0);
else
    disp('No oxygen profile, check your Excel file...');
    return
end

% for debug only
% --------------
%     if isempty(last_station)
%       last_station = length(stations);
%     end
last_profile=str2double(match{max(gprofiles)}{1});
% allocate array size
% -------------------
data = nan(length(lines), 16, last_profile);

% loop over each excel sheet
% add a regex to loop over "station xx" header only
% --------------------------
for itab = gprofiles   % loop over profiles (tabs) in this sheet
    
    % extract station number from sheet name
    % --------------------------------------
    profile = str2double(match{itab}{1});
    
    if ~isempty(itab)
        % read Excel worksheet in raw mode (unprocessed cell content) which
        % contains both numeric and text data, empty cell is fillwith NaN
        % -----------------------------------------------------------------
        fprintf('Read profile %d\n', profile)
        % modif glazeo le 15/01/2015
        [~,~,raw] = xlsread( nfsample, sheet{itab} );
        
        %use data(ii,icol,station) for compatibility with JG/Cadyliac system
        
         sbtl(itab).profileid=profile;
         for ii = 1:length(lines)
            %fill sbtl stat/cast for all lines 
            sbtl(itab).stat(ii)=floor(profile/100);
            sbtl(itab).cast(ii)=profile-100*sbtl(itab).stat(ii);
    
            % get only double value, set to Nan for other cell
            % ------------------------------------------------
            % num btl column 3
            icol=3;
            value = raw{lines(ii),icol};
            if ~isempty(value) && ~ischar(value)
                data(ii,icol,itab) = value;
            else
                data(ii,icol,itab) = NaN;
            end
            sbtl(itab).btlid(ii)=data(ii,icol,itab);
            
            % salc (salinity) column 5 (E)
            icol=5;
            value = raw{lines(ii),icol};
            if ~isempty(value) && ~ischar(value)
                data(ii,icol,itab) = value;
            else
                data(ii,icol,itab) = NaN;
            end
            sbtl(itab).sali(ii)=data(ii,icol,itab);
            
            % tmp0 (T prelevement) column 4
            icol=4;
            value = raw{lines(ii),icol};
            if ~isempty(value) && ~ischar(value)
                data(ii,icol,itab) = value;
            else
                data(ii,icol,itab) = NaN;
            end
            sbtl(itab).temp(ii)=data(ii,icol,itab);
            
            % flask number (numero flacon) column 10
            icol=10;
            value = raw{lines(ii),icol};
            if ~isempty(value) && ~ischar(value)
                data(ii,icol,itab) = value;
            else
                data(ii,icol,itab) = NaN;
            end
            sbtl(itab).flask(ii)=data(ii,icol,itab);
            
            % oxygen (ml/l) column 14 (O)
            icol=14;
            value = raw{lines(ii),icol};
            if ~isempty(value) && ~ischar(value)
                data(ii,icol,itab) = value;
            else
                data(ii,icol,itab) = NaN;
            end
            sbtl(itab).o2ml(ii)=data(ii,icol,itab);
            
            % depth (db): 16 column Nan si pas de bouteilles
            icol=16;
            value = raw{lines(ii),icol};
            if ~isempty(value) && ~ischar(value)
                data(ii,icol,itab) = value;
            else
                data(ii,icol,itab) = NaN;
            end
            sbtl(itab).pres(ii)=data(ii,icol,itab);
            
        end
        if profile >= last_profile
            break
        end
        sbtl(itab).btlid=sbtl(itab).btlid(:);
        sbtl(itab).sali=sbtl(itab).sali(:);
        sbtl(itab).temp=sbtl(itab).temp(:);
        sbtl(itab).flask=sbtl(itab).flask(:);
        sbtl(itab).o2ml=sbtl(itab).o2ml(:);
        sbtl(itab).pres=sbtl(itab).pres(:);
        
    end
end %  end of loop over stations in this sheet

if 1
    %test:
    for itab=1
        if ~isempty(sbtl(itab).pres)
            disp(['Profile:', num2str(sbtl(itab).profileid)])
            for id=1:24
                disp(sprintf('btlid %2d pres %4d flask %3d temp %4.2f sali % 4.2f o2ml %4.2f',...
                    sbtl(itab).btlid(id),sbtl(itab).pres(id),sbtl(itab).flask(id),sbtl(itab).temp(id),sbtl(itab).sali(id),sbtl(itab).o2ml(id)))
            end
        end
    end
end