function oxy_match = make_oxy_sumfile_beckman(cruiseid)
% MAKE_OXY_SUMFILE -  makes oxygen bottle/ctd comparison summary file
%
% CTD Calibration toolbox.
%
% USAGE:
% 	oxy_match = make_oxy_sumfile_beckman(cruiseid);
%
% INPUT
%	cruiseid	cruise identification name. 
%   
% OUTPUT
%	oxy_match	combined bottle sample and ctd trigger information
%	Automatically writes two files  
%          basically with the same fields in *.mat and *.asc formats
%          oxy_struct: contains a structure with the same information
%               contained in the *.mat file
%
% DESCRIPTION:  This function tries to merge the downcast information (*.cnv) files from the Seabird 
%  and the *.oxy files from the AOML titration system
%  and create a summary file containing coincident measurements from both
%  systems.  The data for a whole cruise is processed at once so if you want
%  less data you'll have to subset afterwards.
%
% BUGS: 
%  1. Not really a bug but it's based on the AOML *.oxy files.  These files
%  can vary slightly in terms of what each field means, based on operator 
%  preference. The results of read_oxy contain a field called depth which is 
%  usu. either the niskin bottle position on the rosette or the firing order.  
%  Position is preferrable if you think about it at the beginning of the 
%  cruise.
%

% AUTHORS:Carlos Fonseca 
%         UM/CIMAS
%         Derrick Snowden
%         NOAA/AOML/PhOD
%         Tue May 11 11:23:37 EDT 2004
%
% VERSION: 
%	$Revision: 1.6 $
%	$Date: 2008/04/30 18:36:27 $
%	$Id: make_oxy_sumfile_beckman.m,v 1.6 2008/04/30 18:36:27 duncombe Exp duncombe $
% 
% CHANGELOG: 
%   23-Jul-2004, Version 1.0
%		* Initial version.
%  2008/04/10 CMDR
%	* incorporate changes from make_oxy_sumfile.m
%
% DEPENDENCIES: SW package
%               load_oxy
%               load_btl
%               load_bl
%               deloop
%

flag_message1=0;
flag_message2=0;
flag_conv1=0;
flag_conv2=0;
%
%%%%%%%%%%% CMDR
if nargin==0, 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
	load(db_file);
else
	error(['The cruise data base file was not found on the path.  ' ...
		db_file]);
end

% Check for the necessary variables in the cruise database.
n=who('btl');
if isempty(n)==1
	error(['btl variable not found in the cruise database. ' ...
		'Run load_btl.m ']);
	return;
end
n=who('bl');
if isempty(n)==1
	error(['bl variable not found in the cruise database. ' ...
		'Run load_bl.m ']);
	return;
end
n=who('cnv_dlp');
if isempty(n)==1
	error(['cnv_dlp variable not found in the cruise database. ' ...
		'Run deloop.m ']);
	return;
end
n=who('oxy');
if isempty(n)==1
	error(['oxy variable not found in the cruise database. ' ...
		'Run load_oxy.m ']);
	return;
end;

%%%%%%%%  CMDR  turn off more if it is on
morestate=get(0,'more');
more off;
%%%%%%%%

oxy_match=oxy_struct;
oxy_match.cruiseid=cruiseid;
%information from .oxy files
    stations= unique(vertcat(oxy.station));
    oxy_match.stnnbr=vertcat(oxy.station);
    oxy_match.sampnbr=vertcat(oxy.sample_bottle);
    oxy_match.btlnbr=vertcat(oxy.depth);
    oxy_match.niskinbtlpos=vertcat(oxy.depth);    
% extracting the fire order from the .bl file
    for i=1:1:length(oxy_match.btlnbr)
       for j=1:1:length(bl)
         if (oxy_match.stnnbr(i)==bl(j).station)    
		% & cond_cal.castnbr(i)==bl(j).cast)   
	     sta_idx=j;
         end   
       end
     fire_index=find(bl(sta_idx).niskinnumber==oxy_match.btlnbr(i));
      if isempty(fire_index)
	   disp(['No matching bl entry for the current sample.  ' ...
			'Likely, no confirmation bit.']);
           disp(['Station ',num2str(oxy_match.stnnbr(i)),' Niskin # ',...
			num2str(oxy_match.btlnbr(i))]);
           disp('This sample will be disregarded')
           disp('Hit any key to continue')
           pause
           oxy_match.fireorder(i,1)=NaN; 
      
      else
      oxy_match.fireorder(i,1)=bl(sta_idx).fireorder(fire_index);
      end;
    end;
% end of extracting the fire order
% converting the oxygen values to ml/l
    oxy_match.btloxy=vertcat(oxy.oxygen)*0.02239;  
  
 % keeping only the valid data
    jj_nan=find(isnan(oxy_match.fireorder)==0);
    oxy_match.fireorder=oxy_match.fireorder(jj_nan);
    oxy_match.stnnbr=oxy_match.stnnbr(jj_nan);
    oxy_match.sampnbr=oxy_match.sampnbr(jj_nan);
    oxy_match.btlnbr=oxy_match.btlnbr(jj_nan);
    oxy_match.niskinbtlpos=oxy_match.niskinbtlpos(jj_nan);
    oxy_match.btloxy=oxy_match.btloxy(jj_nan);
% 

% information from .btl files  
 disp('**************************************');
 disp('Extracting information from .btl files');
 disp('**************************************');
  for i=1:length(oxy_match.btloxy)
	%sta_idx=cond_cal.stnnbr(i);
     for j=1:1:length(btl)
      if oxy_match.stnnbr(i)==btl(j).station,
		% & cond_cal.castnbr(i)==bl(j).cast      
	  sta_idx=j;
      end   
     end
    fire_idx=oxy_match.fireorder(i);
    
      for j=1:length(btl(sta_idx).names);
       if strncmp(btl(sta_idx).names(j),'pressure',8);
       pr_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'con_pri',7);
       ctd_c1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'con_sec',7);
       ctd_c2_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'sbedo2_mll_pri',14);
       ctd_oxy1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'sbedo2_mll_sec',14);
       ctd_oxy2_idx=j;
       end
       %%%%% YSI Beckman - AB01
       if strncmp(btl(sta_idx).names(j),'do2_pri',7);
       ctd_oxy1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'do2_sec',7);
       ctd_oxy2_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'oxc_pri',7);
       ctd_oxc1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'oxc_sec',7);
       ctd_oxc2_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'oxt_pri',7);
       ctd_oxt1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'oxt_sec',7);
       ctd_oxt2_idx=j;
       end
       %%%%%%%%%%%%%%%%%%%%%%%%
       if strncmp(btl(sta_idx).names(j),'sbeox_volt_pri',14);
       ctd_oxv1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'sbeox_volt_sec',14);
       ctd_oxv2_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'sbeox_dOCdt_pri',15);
       ctd_dvdt1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'sbeox_dOCdt_sec',15);
       ctd_dvdt2_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'t90_pri',7);
       ctd_t1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'t90_sec',7);
       ctd_t2_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'t68_pri',7);
       ctd_t1_idx=j;
       flag_conv1=1;
        if flag_message1==0
        disp('Primary temperatures are given in T68.')
        disp(['This program will convert to T90 to make the ' ...
		'necessary calculations']);
        flag_message1=1;
        end
       end
       if strncmp(btl(sta_idx).names(j),'t68_sec',7);
       ctd_t2_idx=j;
       flag_conv2=1;
        if flag_message2==0
        disp('Secondary temperatures are given in T68.')
        disp(['This program will convert to T90 to make the ' ...
		necessary calculations']);
        flag_message2=1;
        end  
       end
      end
      oxy_match.latitude(i,1)=btl(sta_idx).lat;
      oxy_match.longitude(i,1)=btl(sta_idx).lon;
      oxy_match.ctdupprs(i,1)=btl(sta_idx).data(fire_idx,pr_idx);
      oxy_match.ctdupcon1(i,1)=10*btl(sta_idx).data(fire_idx,ctd_c1_idx);
%%%%%%%%%% CMDR test for variables
      if exist('ctd_oxy1_idx'),
        oxy_match.ctdupoxy1(i,1)=btl(sta_idx).data(fire_idx,ctd_oxy1_idx);
      end;
      oxy_match.ctdupoxc1(i,1)=btl(sta_idx).data(fire_idx,ctd_oxc1_idx);
      oxy_match.ctdupoxct1(i,1)=btl(sta_idx).data(fire_idx,ctd_oxt1_idx);

      oxy_match.ctduptmp1(i,1)=btl(sta_idx).data(fire_idx,ctd_t1_idx);
      if flag_conv1==1,
        oxy_match.ctduptmp1(i,1)=t68tot90(oxy_match.ctduptmp1(i,1));   
      end;
%%%%%%%%%% CMDR group statements and test for secondary sensors
if exist('ctd_c2_idx'),
      oxy_match.ctdupcon2(i,1)=10*btl(sta_idx).data(fire_idx,ctd_c2_idx);
end;
if exist('ctd_oxy2_idx'),
      oxy_match.ctdupoxy2(i,1)=btl(sta_idx).data(fire_idx,ctd_oxy2_idx);
end;
if exist('ctd_oxc2_idx'),
      oxy_match.ctdupoxc2(i,1)=btl(sta_idx).data(fire_idx,ctd_oxc2_idx);
end;
if exist('ctd_oxt2_idx'),
      oxy_match.ctdupoxct2(i,1)=btl(sta_idx).data(fire_idx,ctd_oxt2_idx);
end;
if exist('ctd_t2_idx'),
      oxy_match.ctduptmp2(i,1)=btl(sta_idx).data(fire_idx,ctd_t2_idx);
end;
if isfield(oxy_match,'ctduptmp2'),
      if flag_conv2==1,
        oxy_match.ctduptmp2(i,1)=t68tot90(oxy_match.ctduptmp2(i,1));   
      end;
end;
%%%%%%%%%
end; % end of extracting information from btl file
  
     %calculating the salinity and the potential density
     oxy_match.ctdupsal1= ...
	sw_salt(oxy_match.ctdupcon1/sw_c3515, ...
		oxy_match.ctduptmp1, ...
		oxy_match.ctdupprs);
    %oxy_match.ctdupsigflu1=sw_pden(oxy_match.ctdupsal1,oxy_match.ctduptmp1,oxy_match.ctdupprs,oxy_match.ctdupprs)-1000;
%%%%%%%%%% CMDR test for secondary sensors
	if isfield(oxy_match,'ctduptmp2'),
     		oxy_match.ctdupsal2=sw_salt(oxy_match.ctdupcon2/sw_c3515, ...
			oxy_match.ctduptmp2,oxy_match.ctdupprs);
	end;
    % oxy_match.ctdupsigflu2=sw_pden(oxy_match.ctdupsal2,oxy_match.ctduptmp2,oxy_match.ctdupprs,oxy_match.ctdupprs)-1000;   
    %%%% Fixing the units -ab01
    %  oxy_match.ctdupoxy1=ox_units(oxy_match.ctdupoxy1...
    %  ,oxy_match.ctdupsal1,sw_ptmp(oxy_match.ctduptmp1,oxy_match.ctdupsal1,oxy_match.ctdupprs,0),...
    %  'mmk2mll');
    %  oxy_match.ctdupoxy2=ox_units(oxy_match.ctdupoxy2...
    %  ,oxy_match.ctdupsal2,sw_ptmp(oxy_match.ctduptmp2,oxy_match.ctdupsal2,oxy_match.ctdupprs,0),...
    %  'mmk2mll');
    %
    % calculating the neutral density (gamma n)
     station_list=unique(oxy_match.stnnbr);
     for i=1:length(station_list)
          aux=find(oxy_match.stnnbr==station_list(i));
          [g,dg_lo,dg_hi] = gamma_n( ...
		oxy_match.ctdupsal1(aux), ...
		t90tot68(oxy_match.ctduptmp1(aux)), ...
		oxy_match.ctdupprs(aux), ...
		oxy_match.longitude(aux(1)), ...
		oxy_match.latitude(aux(1)));
     	  oxy_match.ctdupgamman1(aux,1)=g;
          %%%%% CMDR test for secondary sensors
	  if isfield(oxy_match,'ctdupsal2'),
     		[g,dg_lo,dg_hi] = gamma_n(oxy_match.ctdupsal2(aux), ...
			t90tot68(oxy_match.ctduptmp2(aux)), ...
			oxy_match.ctdupprs(aux), ...
			oxy_match.longitude(aux(1)), ...
			oxy_match.latitude(aux(1)));
     		oxy_match.ctdupgamman2(aux,1)=g;  
     	end; 
     end; 
% end of calculating the neutral density
 %%%%%

  % Interpolating using the cnv files
 disp('**************************************');
 disp('Extracting information from delooped files');
 disp('**************************************');
 %
 %Finding the indexes for the variables 
 %
 new_station_old=0;
 for i=1:length(oxy_match.stnnbr);
       for j=1:1:length(cnv_dlp)
          if oxy_match.stnnbr(i)==cnv_dlp(j).station,
		%  & cond_match.castnbr(i)==bl(j).cast      
	      sta_idx=j;
          new_station=j;
           if new_station~=new_station_old
           disp(['Working on station ',num2str(cnv_dlp(j).station)]) 
           new_station_old=new_station;
           end
          end   
      end
      press_idx=round(oxy_match.ctdupprs(i));
      max_pressure_idx=find( ...
	cnv_dlp(sta_idx).pressure==max(cnv_dlp(sta_idx).pressure));
      lim_idx=find(round(cnv_dlp(sta_idx).pressure)==press_idx);
   % Defining the subset to the the interpolations 
        if(press_idx<=10 & max_pressure_idx<100); % surface bottle  
        press_aux=cnv_dlp(sta_idx).pressure(1:max_pressure_idx);
        oxc_aux=cnv_dlp(sta_idx).oxcpri(1:max_pressure_idx);
        oxc2_aux=cnv_dlp(sta_idx).oxcsec(1:max_pressure_idx);
        oxct_aux=cnv_dlp(sta_idx).oxctpri(1:max_pressure_idx);
        oxct2_aux=cnv_dlp(sta_idx).oxctsec(1:max_pressure_idx);
        cond1_aux=10*cnv_dlp(sta_idx).condpri(1:max_pressure_idx);
        cond2_aux=10*cnv_dlp(sta_idx).condsec(1:max_pressure_idx);
        oxy_aux=cnv_dlp(sta_idx).oxmllpri(1:max_pressure_idx);
        oxy2_aux=cnv_dlp(sta_idx).oxmllsec(1:max_pressure_idx);
        temp1_aux=cnv_dlp(sta_idx).temppri(1:max_pressure_idx);
        temp2_aux=cnv_dlp(sta_idx).tempsec(1:max_pressure_idx);     
        elseif(press_idx<=10 & max_pressure_idx>=100); % surface bottle 
         press_aux=cnv_dlp(sta_idx).pressure(1:100);
        oxc_aux=cnv_dlp(sta_idx).oxcpri(1:100);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxcsec),
        oxc2_aux=cnv_dlp(sta_idx).oxcsec(1:100);
end;
        oxct_aux=cnv_dlp(sta_idx).oxctpri(1:100);
if ~isempty(cnv_dlp(sta_idx).oxctsec),
        oxct2_aux=cnv_dlp(sta_idx).oxctsec(1:100);
end;
        cond1_aux=10*cnv_dlp(sta_idx).condpri(1:100);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).condsec),
        cond2_aux=10*cnv_dlp(sta_idx).condsec(1:100);
end;
%%%%%%%%%% CMDR test for variables
if ~isempty(cnv_dlp(sta_idx).oxmllpri),
        oxy_aux=cnv_dlp(sta_idx).oxmllpri(1:100);
end;
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxmllsec),
        oxy2_aux=cnv_dlp(sta_idx).oxmllsec(1:100);
end;
        temp1_aux=cnv_dlp(sta_idx).temppri(1:100);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).tempsec),
        temp2_aux=cnv_dlp(sta_idx).tempsec(1:100);     
end;
        elseif (isempty(lim_idx)==1)
         disp('passed here') 
        press_aux=cnv_dlp(sta_idx).pressure(max_pressure_idx-100:max_pressure_idx);
        oxc_aux=cnv_dlp(sta_idx).oxcpri(max_pressure_idx-100:max_pressure_idx);
        oxct_aux=cnv_dlp(sta_idx).oxctpri(max_pressure_idx-100:max_pressure_idx);
%%%%%%%%%% CMDR test for secondary sensors
        if ~isempty(cnv_dlp(sta_idx).oxcsec),
oxc2_aux=cnv_dlp(sta_idx).oxcsec(max_pressure_idx-100:max_pressure_idx);
end;
        if ~isempty(cnv_dlp(sta_idx).oxctsec),
oxct2_aux=cnv_dlp(sta_idx).oxctsec(max_pressure_idx-100:max_pressure_idx);
end;
        cond1_aux=10*cnv_dlp(sta_idx).condpri(max_pressure_idx-100:max_pressure_idx);
%%%%%%%%%% CMDR test for secondary sensors
        if ~isempty(cnv_dlp(sta_idx).condsec),
cond2_aux=10*cnv_dlp(sta_idx).condsec(max_pressure_idx-100:max_pressure_idx);
end;
        oxy_aux=cnv_dlp(sta_idx).oxmllpri(max_pressure_idx-100:max_pressure_idx);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxmllsec),
        oxy2_aux=cnv_dlp(sta_idx).oxmllsec(max_pressure_idx-100:max_pressure_idx);
end;
        temp1_aux=cnv_dlp(sta_idx).temppri(max_pressure_idx-100:max_pressure_idx);
if ~isempty(cnv_dlp(sta_idx).tempsec),
        temp2_aux=cnv_dlp(sta_idx).tempsec(max_pressure_idx-100:max_pressure_idx);  
end;
        elseif (lim_idx-100 <= 0 & lim_idx+100 > max_pressure_idx); % a bottle shallower than 30 db
         press_aux=cnv_dlp(sta_idx).pressure(1:max_pressure_idx);
        oxc_aux=cnv_dlp(sta_idx).oxcpri(1:max_pressure_idx);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxcsec),
        oxc2_aux=cnv_dlp(sta_idx).oxcsec(1:max_pressure_idx);
end;
        oxct_aux=cnv_dlp(sta_idx).oxctpri(1:max_pressure_idx);
if ~isempty(cnv_dlp(sta_idx).oxctsec),
        oxct2_aux=cnv_dlp(sta_idx).oxctsec(1:max_pressure_idx);
end;
        cond1_aux=10*cnv_dlp(sta_idx).condpri(1:max_pressure_idx);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).condsec),
        cond2_aux=10*cnv_dlp(sta_idx).condsec(1:max_pressure_idx);
end;
%%%%%%%%%% CMDR test for variables
if ~isempty(cnv_dlp(sta_idx).oxmllpri),
        oxy_aux=cnv_dlp(sta_idx).oxmllpri(1:max_pressure_idx);
end;
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxmllsec),
        oxy2_aux=cnv_dlp(sta_idx).oxmllsec(1:max_pressure_idx);
end;
        temp1_aux=cnv_dlp(sta_idx).temppri(1:max_pressure_idx);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).tempsec),
        temp2_aux=cnv_dlp(sta_idx).tempsec(1:max_pressure_idx);     
end;
        elseif (lim_idx-100 <= 0 & lim_idx+100 <= max_pressure_idx); % a bottle shallower than 30 db
        press_aux=cnv_dlp(sta_idx).pressure(1:lim_idx+100);
        oxc_aux=cnv_dlp(sta_idx).oxcpri(1:lim_idx+100);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxcsec),
        oxc2_aux=cnv_dlp(sta_idx).oxcsec(1:lim_idx+100);
end;
        oxct_aux=cnv_dlp(sta_idx).oxctpri(1:lim_idx+100);
if ~isempty(cnv_dlp(sta_idx).oxctsec),
        oxct2_aux=cnv_dlp(sta_idx).oxctsec(1:lim_idx+100);
end;
        cond1_aux=10*cnv_dlp(sta_idx).condpri(1:lim_idx+100);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).condsec),
        cond2_aux=10*cnv_dlp(sta_idx).condsec(1:lim_idx+100);
end;
%%%%%%%%%% CMDR test for variables
if ~isempty(cnv_dlp(sta_idx).oxmllpri),
        oxy_aux=cnv_dlp(sta_idx).oxmllpri(1:lim_idx+100);
end;
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxmllsec),
        oxy2_aux=cnv_dlp(sta_idx).oxmllsec(1:lim_idx+100);
end;
        temp1_aux=cnv_dlp(sta_idx).temppri(1:lim_idx+100);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).tempsec),
        temp2_aux=cnv_dlp(sta_idx).tempsec(1:lim_idx+100);       
end;
        elseif (lim_idx+100 > max_pressure_idx)%bottom bottle
        press_aux=cnv_dlp(sta_idx).pressure(lim_idx-100:max_pressure_idx);
        oxc_aux=cnv_dlp(sta_idx).oxcpri(lim_idx-100:max_pressure_idx);
%%%%%%%%%% CMDR test for secondary sensors
        if ~isempty(cnv_dlp(sta_idx).oxcsec),
        oxc2_aux=cnv_dlp(sta_idx).oxcsec(lim_idx-100:max_pressure_idx);
	end;
        oxct_aux=cnv_dlp(sta_idx).oxctpri(lim_idx-100:max_pressure_idx);
        if ~isempty(cnv_dlp(sta_idx).oxctsec),
        oxct2_aux=cnv_dlp(sta_idx).oxctsec(lim_idx-100:max_pressure_idx);
	end;
        cond1_aux=10*cnv_dlp(sta_idx).condpri(lim_idx-100:max_pressure_idx);
%%%%%%%%%% CMDR test for secondary sensors
        if ~isempty(cnv_dlp(sta_idx).condsec),
        cond2_aux=10*cnv_dlp(sta_idx).condsec(lim_idx-100:max_pressure_idx);
	end;
%%%%%%%%%% CMDR test for variables
if ~isempty(cnv_dlp(sta_idx).oxmllpri),
        oxy_aux=cnv_dlp(sta_idx).oxmllpri(lim_idx-100:max_pressure_idx);
end;
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxmllsec),
        oxy2_aux=cnv_dlp(sta_idx).oxmllsec(lim_idx-100:max_pressure_idx);
end;
        temp1_aux=cnv_dlp(sta_idx).temppri(lim_idx-100:max_pressure_idx);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).tempsec),
        temp2_aux=cnv_dlp(sta_idx).tempsec(lim_idx-100:max_pressure_idx);      
end;
        else % any other bottle
        press_aux=cnv_dlp(sta_idx).pressure(lim_idx-100:lim_idx+100);
        oxc_aux=cnv_dlp(sta_idx).oxcpri(lim_idx-100:lim_idx+100);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxcsec),
        oxc2_aux=cnv_dlp(sta_idx).oxcsec(lim_idx-100:lim_idx+100);
end;
        oxct_aux=cnv_dlp(sta_idx).oxctpri(lim_idx-100:lim_idx+100);
if ~isempty(cnv_dlp(sta_idx).oxctsec),
        oxct2_aux=cnv_dlp(sta_idx).oxctsec(lim_idx-100:lim_idx+100);
end;
        cond1_aux=10*cnv_dlp(sta_idx).condpri(lim_idx-100:lim_idx+100);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).condsec),
        cond2_aux=10*cnv_dlp(sta_idx).condsec(lim_idx-100:lim_idx+100);
end;
%%%%%%%%%% CMDR test for variables
if ~isempty(cnv_dlp(sta_idx).oxmllpri),
        oxy_aux=cnv_dlp(sta_idx).oxmllpri(lim_idx-100:lim_idx+100);
end;
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).oxmllsec),
        oxy2_aux=cnv_dlp(sta_idx).oxmllsec(lim_idx-100:lim_idx+100);
end;
        temp1_aux=cnv_dlp(sta_idx).temppri(lim_idx-100:lim_idx+100);
%%%%%%%%%% CMDR test for secondary sensors
if ~isempty(cnv_dlp(sta_idx).tempsec),
        temp2_aux=cnv_dlp(sta_idx).tempsec(lim_idx-100:lim_idx+100);     
end;
        end
%%%%%%%%%%  
%%%%% Calculating the salinity
        sal_aux1=sw_salt(cond1_aux/sw_c3515,temp1_aux,press_aux); 
%%%%%%%%%% CMDR test for secondary sensors
        if exist('cond2_aux'),
        	sal_aux2=sw_salt(cond2_aux/sw_c3515,temp2_aux,press_aux); 
	end;
%%%%%%%%%%
%%%%%%% Calculating the neutral density
        gamma_n_aux1=gamma_n(sal_aux1,t90tot68(temp1_aux), ...
		press_aux,cnv_dlp(sta_idx).longitude, ...
		cnv_dlp(sta_idx).latitude);
%%%%%%%%%% CMDR test for secondary sensors
        if exist('sal_aux2'),
        gamma_n_aux2=gamma_n(sal_aux2,t90tot68(temp2_aux),press_aux,cnv_dlp(sta_idx).longitude,cnv_dlp(sta_idx).latitude);
        if ~isreal(gamma_n_aux1) | ~isreal(gamma_n_aux2)
        error(['Problems found in the .cnv file. Please check this ' ...
		'station before continue']);
        end;
	end;
%%%%%%%%%%
%%%% Interpolating the variables
  % making the vectors distinct for interp.
      gamma_n_aux1=weim(35,'hann',gamma_n_aux1);
%%%%%%%%%% CMDR test for secondary sensors
      if exist('gamma_n_aux2'),
      	gamma_n_aux2=weim(35,'hann',gamma_n_aux2);
	end;

        % diff_aux1=diff(gamma_n_aux1);
        % diff_aux2=diff(gamma_n_aux2);
        % aux=find(abs(diff_aux1)>=1e-5 | abs(diff_aux2)>=1e-5);
        % gamma_n_aux1=gamma_n_aux1(aux);
        % press_aux=press_aux(aux);
        % cond1_aux=cond1_aux(aux);
        % temp1_aux=temp1_aux(aux);
        % gamma_n_aux2=gamma_n_aux2(aux);
        % cond2_aux=cond2_aux(aux);
        % temp2_aux=temp2_aux(aux);
   %%%%%%
%%%%%%%%%% CMDR reorder statements and test for secondary sensors
    new_c(i)=interp1(gamma_n_aux1,oxc_aux,oxy_match.ctdupgamman1(i),'linear',NaN);
    new_ct(i)=interp1(gamma_n_aux1,oxct_aux,oxy_match.ctdupgamman1(i),'linear',NaN);
    new_press(i)=interp1(gamma_n_aux1,press_aux,oxy_match.ctdupgamman1(i),'linear',NaN);
    new_cond(i)=interp1(gamma_n_aux1,cond1_aux,oxy_match.ctdupgamman1(i),'linear',NaN);
    new_temp(i)=interp1(gamma_n_aux1,temp1_aux,oxy_match.ctdupgamman1(i),'linear',NaN);
%%%%%%%%%% CMDR test for variables
if exist('oxy_aux'),
    new_oxy(i)=interp1(gamma_n_aux1,oxy_aux,oxy_match.ctdupgamman1(i),'linear',NaN);
end;
%

	if exist('gamma_n_aux2'),
    new_c2(i)=interp1(gamma_n_aux2,oxc2_aux,oxy_match.ctdupgamman2(i),'linear',NaN);
    new_ct2(i)=interp1(gamma_n_aux2,oxct2_aux,oxy_match.ctdupgamman2(i),'linear',NaN);
    new_cond2(i)=interp1(gamma_n_aux2,cond2_aux,oxy_match.ctdupgamman2(i),'linear',NaN);
    new_temp2(i)=interp1(gamma_n_aux2,temp2_aux,oxy_match.ctdupgamman2(i),'linear',NaN);
    new_oxy2(i)=interp1(gamma_n_aux2,oxy2_aux,oxy_match.ctdupgamman2(i),'linear',NaN);
	end;
%%%%%%%%%%
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 disp('**************************************************');
 disp('Interpolating upcast info on downcast using sigma ')
 disp('**************************************************');
%%%%%%%%% CMDR rearrange code and test for sec sensors
if exist('new_oxy'),
 oxy_match.ctddooxy1=new_oxy';
end;
 oxy_match.ctddooxc1=new_c';
 oxy_match.ctddooxct1=new_ct';
 oxy_match.ctddotmp1=new_temp';
 oxy_match.ctddocon1=new_cond';
%
	if exist('gamma_n_aux2'),
 oxy_match.ctddooxy2=new_oxy2';
 oxy_match.ctddooxc2=new_c2';
 oxy_match.ctddooxct2=new_ct2';
 oxy_match.ctddotmp2=new_temp2';
 oxy_match.ctddocon2=new_cond2';
	end;
%%%%%%%%%%%%%
 oxy_match.ctddoprs=new_press';
 
 %using the surface values of the upcast ( case missing )
% primary sensor
aux=find(isnan(oxy_match.ctddooxc1)==1);
%%%%%%%%% CMDR test for variables
if isfield(oxy_match,'ctdupoxy1'),
	oxy_match.ctddooxy1(aux)=oxy_match.ctdupoxy1(aux);
end;
oxy_match.ctddooxc1(aux)=oxy_match.ctdupoxc1(aux);
oxy_match.ctddooxct1(aux)=oxy_match.ctdupoxct1(aux);
oxy_match.ctddotmp1(aux)=oxy_match.ctduptmp1(aux);
oxy_match.ctddocon1(aux)=oxy_match.ctdupcon1(aux);
oxy_match.ctddoprs(aux)=oxy_match.ctdupprs(aux);
%aux=find(oxy_match.ctddoprs<0);
%oxy_match.ctddooxy1(aux)=oxy_match.ctdupoxy1(aux);
%oxy_match.ctddooxv1(aux)=oxy_match.ctdupoxv1(aux);
%oxy_match.ctddotmp1(aux)=oxy_match.ctduptmp1(aux);
%oxy_match.ctddocon1(aux)=oxy_match.ctdupcon1(aux);
%oxy_match.ctddoprs(aux)=oxy_match.ctdupprs(aux);

% secondary Sensor
%%%%%%%%%% CMDR test for sec sensors
if isfield(oxy_match,'ctddooxv2'),
aux=find(isnan(oxy_match.ctddooxc2)==1);
oxy_match.ctddooxy2(aux)=oxy_match.ctdupoxy2(aux);
oxy_match.ctddooxc2(aux)=oxy_match.ctdupoxc2(aux);
oxy_match.ctddooxct2(aux)=oxy_match.ctdupoxct2(aux);
oxy_match.ctddotmp2(aux)=oxy_match.ctduptmp2(aux);
oxy_match.ctddocon2(aux)=oxy_match.ctdupcon2(aux);
end;
%%%%%%%%%%
%aux=find(oxy_match.ctddoprs<0);
%oxy_match.ctddooxy2(aux)=oxy_match.ctdupoxy2(aux);
%oxy_match.ctddooxv2(aux)=oxy_match.ctdupoxv2(aux);
%oxy_match.ctddotmp2(aux)=oxy_match.ctduptmp2(aux);
%oxy_match.ctddocon2(aux)=oxy_match.ctdupcon2(aux);
disp('*************************************************');
disp('Matching interpolated information with .oxy files')
disp('*************************************************');
% updating the fields for save
oxy_match.ctdprs=oxy_match.ctddoprs;
oxy_match.ctdtmp1=oxy_match.ctddotmp1;
oxy_match.ctdcon1=oxy_match.ctddocon1;
oxy_match.ctdoxc1=oxy_match.ctddooxc1;
oxy_match.ctdoxct1=oxy_match.ctddooxct1;
if isfield(oxy_match,'ctddooxy1'),
	oxy_match.ctdoxy1=oxy_match.ctddooxy1;
else
	disp('Hey man, we never set the oxy_match.ctdoxy1. Wassup with that?');
end;
oxy_match.ctdsal1=sw_salt(oxy_match.ctdcon1/sw_c3515, ...
	oxy_match.ctdtmp1,oxy_match.ctdprs);
oxy_match.ctddocdt1=weim(31,'hann',gradient(oxy_match.ctdoxc1))';

%%%%%%%%%% CMDR test for sec sensors
if isfield(oxy_match,'ctddooxc2'),
oxy_match.ctdtmp2=oxy_match.ctddotmp2;
oxy_match.ctdcon2=oxy_match.ctddocon2;
oxy_match.ctdoxc2=oxy_match.ctddooxc2;
oxy_match.ctdoxct2=oxy_match.ctddooxct2;
oxy_match.ctddocdt2=weim(31,'hann',gradient(oxy_match.ctdoxc2))';
oxy_match.ctdoxy2=oxy_match.ctddooxy2;  
oxy_match.ctdsal2=sw_salt(oxy_match.ctdcon2/sw_c3515, ...
	oxy_match.ctdtmp2,oxy_match.ctdprs);
end;
% Save the oxygen summary structure.
save(db_file,'oxy_match','-append');

%%%%%%%%  CMDR  return more to the original state
more(morestate);
%%%%%%%%

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

return;
 
