function varargout=gap_interp(cruiseid,staa,stab)
% GAP_INTERP - Removes NaNs in the profiles and interpolates to remove gaps
% in the data. This is done in the despiking routines.  
%
% CTD Calibration toolbox
%
% USAGE - cnv_gterp=gap_interp(cruiseid,staa,stab);
%
% OUTPUT - cnv_gterp with no NaN's!
%
% DESCRIPTION:  This is just a quick way when you want a quick way to look 
%  at the bottle data comparisons with the sensors (make_oxy_sumfile has 
%  issues with NaN's in the profiles).

% AUTHORS: Jay Hooper
%          UM/CIMAS
%          Thur August 2 16:25:51 GMT 2012


if nargin==0, cruiseid=getcruiseid; end;
if isempty(cruiseid), cruiseid=getcruiseid; end;
%%%%%%%%%%%
% Retrieve the necessary preferences for this cruise
group = group_name(cruiseid);

% cruise_dir is the main calibration data directory 
if ispref(group,'cal_data_dir'),
	cruise_dir=getpref(group,'cal_data_dir');
else
	error(['The cal_data_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
end;

% Load data from the cruise database
db_file = fullfile(cruise_dir,[cruiseid '_db.mat']);
if exist(db_file)==2
    ld_file=(['load ',db_file,' cnv_fill']);
    eval(ld_file)
else
	error(['The cruise data base file was not found on the path.  ' db_file]);
end

n=who('cnv_fill');
if isempty(n),
    error(['cnv_fill not found. run cnv_fill.m ']);
    return;
end;


% we need to be very sure which order the stations are being accessed, so 
% set up aux_stations so that we do not need to rely on any arbitrary order
% that the variable structure might have been created in.
[aux_stations,ind_stns]=sort(vertcat(cnv_ds1.station));

disp('****************************************** ')
disp('DESPIKE Secondary SENSORS')
disp('****************************************** ')
disp('The data found in cnv goes from') 
disp(['the station ',num2str(min(aux_stations)),' to the station ', ...
	num2str(max(aux_stations)),'.'] ) 
disp('****************************************** ')
%
%%%%%%
if ~exist('staa'), staa=input('Input first station: '); end;
if ~exist('stab'), stab=input('Input last station: '); end;
if isempty(staa), staa=min([cnv_ds1.station]); end;
if isempty(stab), stab=max([cnv_ds1.station]); end;
%%%%%%%

% Assumes stations are in "order" in cnv data array (which they should be
% since they were loaded with a "dir" command that puts things in order.
% And it turns out they may not be in the expected order. So aux_stations 
% has been rearranged (sorted) above so that we know the order.
sta_min_idx=find(staa==aux_stations);
sta_max_idx=find(stab==aux_stations);

for i=ind_stns(sta_min_ids:sta_mas_idx).',
    
cnv_gterp(i)=cnv_fill(i);



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%% OXYGEN %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% primary oxv despiking: linearly interpolate over zeros; WOCE flag to 6
    % All good O2
    	jj=find(cnv_gterp(i).oxvpri~=0 & cnv_gterp(i).oxvpri>=0);
	% O2==0 ;  all pr >= first good O2 ; all pr <= last good O2
    % but only if there are good oxygens
    if ~isempty(jj),
      ii=find(abs(cnv_gterp(i).oxvpri==0 | cnv_gterp(i).oxvpri<0) | isnan(cnv_gterp(i).oxvpri)   & ...                             
          cnv_gterp(i).pressure >= cnv_gterp(i).pressure(jj(1)) & ...         
          cnv_gterp(i).pressure <= cnv_gterp(i).pressure(jj(length(jj))));
      if length(ii) > 0,
	if isfield(cnv_gterp,'oxvpri'),
          cnv_gterp(i).oxvpri(ii)=interp1(cnv_gterp(i).pressure(jj), ...
		  cnv_gterp(i).oxvpri(jj),cnv_gterp(i).pressure(ii));          
	end;
    if isfield(cnv_gterp,'oxmllpri'),
          cnv_gterp(i).oxmllpri(ii)=interp1(cnv_gterp(i).pressure(jj), ...
		  cnv_gterp(i).oxmllpri(jj),cnv_gterp(i).pressure(ii));    
    end;
    if isfield(cnv_gterp,'oxmmpri'),
          cnv_gterp(i).oxmmpri(ii)=interp1(cnv_gterp(i).pressure(jj), ...
		  cnv_gterp(i).oxmmpri(jj),cnv_gterp(i).pressure(ii));    
    end;
          for n=1:length(ii);
              dum=int2str(cnv_gterp(i).quality(ii(n)));
              if dum(6)=='2'
                  cnv_gterp(i).quality(ii(n))=cnv_gterp(i).quality(ii(n))+40;
              end; % if dum(6)=='2'
%%%%%%%%%%%%%%%%%%%%%%%%%%%% JH qq %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
              dm=int2str(cnv_gterp(i).quality1oxy(ii(n)));
              if dm == '2'
                  cnv_gterp(i).quality1oxy(ii(n))=cnv_gterp(i).quality1oxy(ii(n))+4;
              end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%% JH qq %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              
          end; % for n=1:length(ii);
      end; % if length(ii) > 0,
    end; % if ~isempty(jj),

% Secondary oxv despiking: linearly interpolate over zeros; WOCE flag to 6
    % All good O2
    	jj=find(cnv_gterp(i).oxvsec~=0 & cnv_gterp(i).oxvsec>=0);
	% O2==0 ;  all pr >= first good O2 ; all pr <= last good O2
    % but only if there are good oxygens
    if ~isempty(jj),
      ii=find(abs(cnv_gterp(i).oxvsec==0 | cnv_gterp(i).oxvsec<0) | isnan(cnv_gterp(i).oxvsec) & ...                             
          cnv_gterp(i).pressure >= cnv_gterp(i).pressure(jj(1)) & ...         
          cnv_gterp(i).pressure <= cnv_gterp(i).pressure(jj(length(jj))));
      if length(ii) > 0,
        if isfield(cnv_gterp,'oxvsec'),
            cnv_gterp(i).oxvsec(ii)=interp1(cnv_gterp(i).pressure(jj), ...
            cnv_gterp(i).oxvsec(jj),cnv_gterp(i).pressure(ii));             
        end;
        if isfield(cnv_gterp,'oxmllsec'),            
            cnv_gterp(i).oxmllsec(ii)=interp1(cnv_gterp(i).pressure(jj), ...
            cnv_gterp(i).oxmllsec(jj),cnv_gterp(i).pressure(ii));   
        end;
        if isfield(cnv_gterp,'oxmmsec'),
            cnv_gterp(i).oxmmsec(ii)=interp1(cnv_gterp(i).pressure(jj), ...
            cnv_gterp(i).oxmmsec(jj),cnv_gterp(i).pressure(ii));            
        end;
          for n=1:length(ii);
              dum=int2str(cnv_gterp(i).quality(ii(n)));
              if dum(6)=='2'
                  cnv_gterp(i).quality(ii(n))=cnv_gterp(i).quality(ii(n))+40;                 
              end; % if dum(6)=='2'
%%%%%%%%%%%%%%%%%%%%%%%%%%%% JH qq %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              
              dm=int2str(cnv_gterp(i).quality2oxy(ii(n)));
              if dm == '2'
                  cnv_gterp(i).quality2oxy(ii(n))=cnv_gterp(i).quality2oxy(ii(n))+4;
              end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%% JH qq %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%              
          end; % for n=1:length(ii);
      end; % if length(ii) > 0,          
    end; % if ~isempty(jj),

end; % end station loop

% recompute sigma    
    cnv_gterp(i).sigmasec=sw_pden(cnv_gterp(i).salsec, cnv_gterp(i).tempsec, cnv_gterp(i).pressure,0)-1000.;
    cnv_gterp(i).sigmapri=sw_pden(cnv_gterp(i).salpri, cnv_gterp(i).temppri, cnv_gterp(i).pressure,0)-1000.;

% recompute conductivity (so that conductivity would give the same S)
    cnv_gterp(i).condsec=sw_c3515*sw_cndr(cnv_gterp(i).salsec,cnv_gterp(i).tempsec,cnv_gterp(i).pressure)/10;
    cnv_gterp(i).condpri=sw_c3515*sw_cndr(cnv_gterp(i).salpri,cnv_gterp(i).temppri,cnv_gterp(i).pressure)/10;

%Save the secondary despike summary structure.
save(db_file,'cnv_gterp','-append');

if nargout==0
	assignin('caller','ans',cnv_gterp);
else
	varargout{1}=cnv_gterp;
end;
     
return;






























%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%interpolate of nans and bad values for temperature and conductivity   
     for i=1:26;
         %cnv_dlp2=cnv_gterp;
     kk=find(cnv_gterp(i).tempsec~=0 & cnv_gterp(i).tempsec>=0);
 
	% sal==0 ;  all pr >= first good sal ; all pr <= last good sal
    % but only if there are good dalinity
    if ~isempty(kk),
      hh=find(abs(cnv_gterp(i).tempsec==0 | cnv_gterp(i).tempsec<0) | isnan(cnv_gterp(i).tempsec)   & ...                             
          cnv_gterp(i).pressure >= cnv_gterp(i).pressure(kk(1)) & ...         
          cnv_gterp(i).pressure <= cnv_gterp(i).pressure(kk(length(kk))));
      if length(hh) > 0,
	if isfield(cnv_gterp,'tempsec'),
          cnv_gterp(i).tempsec(hh)=interp1(cnv_gterp(i).pressure(kk), ...
		  cnv_gterp(i).tempsec(kk),cnv_gterp(i).pressure(hh));
	end;
          for n=1:length(hh);
              dum=int2str(cnv_gterp(i).quality(hh(n)));
              if dum(6)=='2'
                  cnv_gterp(i).quality(hh(n))=cnv_gterp(i).quality(hh(n))+40;
              end; % if dum(6)=='2'
              %
          end; % for n=1:length(ii);
      end; % if length(ii) > 0,
    end; % if ~isempty(jj),
     end;
     clear i kk hh n;
     for i=1:26;
         %cnv_gterp=cnv_gterp;
     kk=find(cnv_gterp(i).temppri~=0 & cnv_gterp(i).temppri>=0);
 
	% sal==0 ;  all pr >= first good sal ; all pr <= last good sal
    % but only if there are good dalinity
    if ~isempty(kk),
      hh=find(abs(cnv_gterp(i).temppri==0 | cnv_gterp(i).temppri<0) | isnan(cnv_gterp(i).temppri)   & ...                             
          cnv_gterp(i).pressure >= cnv_gterp(i).pressure(kk(1)) & ...         
          cnv_gterp(i).pressure <= cnv_gterp(i).pressure(kk(length(kk))));
      if length(hh) > 0,
	if isfield(cnv_gterp,'temppri'),
          cnv_gterp(i).temppri(hh)=interp1(cnv_gterp(i).pressure(kk), ...
		  cnv_gterp(i).temppri(kk),cnv_gterp(i).pressure(hh));
	end;
          for n=1:length(hh);
              dum=int2str(cnv_gterp(i).quality(hh(n)));
              if dum(6)=='2'
                  cnv_gterp(i).quality(hh(n))=cnv_gterp(i).quality(hh(n))+40;
              end; % if dum(6)=='2'
          end; % for n=1:length(ii);
      end; % if length(ii) > 0,
    end; % if ~isempty(jj),
     end;
     
     
     
     for i=1:26;
         %cnv_dlp2=cnv_gterp;
     kk=find(cnv_gterp(i).condpri~=0 & cnv_gterp(i).condpri>=0);
 
	% sal==0 ;  all pr >= first good sal ; all pr <= last good sal
    % but only if there are good dalinity
    if ~isempty(kk),
      hh=find(abs(cnv_gterp(i).condpri==0 | cnv_gterp(i).condpri<0) | isnan(cnv_gterp(i).condpri)   & ...                             
          cnv_gterp(i).pressure >= cnv_gterp(i).pressure(kk(1)) & ...         
          cnv_gterp(i).pressure <= cnv_gterp(i).pressure(kk(length(kk))));
      if length(hh) > 0,
	if isfield(cnv_gterp,'condpri'),
          cnv_gterp(i).condpri(hh)=interp1(cnv_gterp(i).pressure(kk), ...
		  cnv_gterp(i).condpri(kk),cnv_gterp(i).pressure(hh));
	end;
          for n=1:length(hh);
              dum=int2str(cnv_gterp(i).quality(hh(n)));
              if dum(6)=='2'
                  cnv_gterp(i).quality(hh(n))=cnv_gterp(i).quality(hh(n))+40;
              end; % if dum(6)=='2'
          end; % for n=1:length(ii);
      end; % if length(ii) > 0,
    end; % if ~isempty(jj),
     end;
     clear i kk hh n;
     
     for i=1:26;
         %cnv_gterp=cnv_gterp;
     kk=find(cnv_gterp(i).condsec~=0 & cnv_gterp(i).condsec>=0);
 
	% sal==0 ;  all pr >= first good sal ; all pr <= last good sal
    % but only if there are good dalinity
    if ~isempty(kk),
      hh=find(abs(cnv_gterp(i).condsec==0 | cnv_gterp(i).condsec<0) | isnan(cnv_gterp(i).condsec)   & ...                             
          cnv_gterp(i).pressure >= cnv_gterp(i).pressure(kk(1)) & ...         
          cnv_gterp(i).pressure <= cnv_gterp(i).pressure(kk(length(kk))));
      if length(hh) > 0,
	if isfield(cnv_gterp,'condsec'),
          cnv_gterp(i).condsec(hh)=interp1(cnv_gterp(i).pressure(kk), ...
		  cnv_gterp(i).condsec(kk),cnv_gterp(i).pressure(hh));
	end;
          for n=1:length(hh);
              dum=int2str(cnv_gterp(i).quality(hh(n)));
              if dum(6)=='2'
                  cnv_gterp(i).quality(hh(n))=cnv_gterp(i).quality(hh(n))+40;
              end; % if dum(6)=='2'
          end; % for n=1:length(ii);
      end; % if length(ii) > 0,
    end; % if ~isempty(jj),
     end;

