function cond_match = make_cond_sumfile(cruiseid)
% MAKE_COND_SUMFILE - makes conductivity bottle/ctd comparison summary file
%
% CTD Calibration toolbox.
%
% USAGE
% 	cond_match = make_cond_sumfile(cruiseid);
%
% INPUT
%	cruiseid	Cruise identification name
%   
% OUTPUT
% 	cond_match	combined bottle sample and ctd trigger information
%
% DESCRIPTION:  This function tries to merge the *.btl files from the Seabird 
%  and the conductivity files from the  salinity samples
%  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. Automatically writes the
%  summary file in the cruise database file.  
%

% AUTHORS:Carlos Fonseca 
%         UM/CIMAS
%         Derrick Snowden
%         NOAA/AOML/PhOD
%         Tue May 11 11:23:37 EDT 2004
%
% 
% VERSION: 
%	$Revision: 1.9 $
%	$Date: 2008/04/19 21:13:20 $
% 	$Id: make_cond_sumfile.m,v 1.9 2008/04/19 21:13:20 duncombe Exp duncombe $
% 
% CHANGELOG: 
%   23-Jul-2004, Version 1.0
%	* Initial version.
%	27-Sep-2004  DPS 
%	*  (sample_struct): Merged the cond_struct and 
%	sample_struct functions into one.
%  2007/12/12 CMDR
%  	* Get cruiseid from a file containing the identifier in the data
%  	directory.
%	* test for secondary sensors present
% 	* change name of function sw_C3515 to sw_c3515 (matlab complains about 
%	  case insensitive match) 
%  2008/02/05 CMDR
%	* toggle more
%  2008/02/18 CMDR
%	* comments and style
%  2008/04/19 CMDR
%	* if there was information missing fro the btl file, eg no btl
%	file for that station, the processing would continue regardless
%	and use old information already in the variables. Fixed by
%	putting in a test and skip the appropriate code. 
%

% DEPENDENCIES: SW package.
%               load_salts
%               load_btl
%               load_bl
%               deloop
%%%%%%% CMDR - also neutral density package (gamma_n.m)
%
% Flags for the temperature scale conversion (t90 to t68)
flag_conv1=0;
flag_conv2=0;
flag_message1=0;
flag_message2=0;
%
format long

%%%%%%%%%%% CMDR read cruiseid if we can
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
   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('salts');
if isempty(n)==1
	error(['salts variable not found in the cruise database. Run load_salts.m ']);
	return;
end

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

% Initialize an empty bottle sample data structure.
cond_match = sample_struct;
cond_match.cruiseid=cruiseid;

% We are trying to match the salinity bottle samples with the corresponding ctd 
% data.  Loop through the good samples and find the matching cnv/btl/bl data as 
% appropriate.
% Information from salt files.  Currently this is a kluge for testing.
stations= unique(vertcat(salts.station));
cond_match.stnnbr=vertcat(salts.station);
cond_match.castnbr=vertcat(salts.station);
cond_match.sampnbr=vertcat(salts.sample_bottle);
cond_match.btlnbr=vertcat(salts.niskin_bottle);
cond_match.niskinbtlpos=vertcat(salts.niskin_bottle); 
cond_match.btlsal=vertcat(salts.salinity);   

% extracting the fire order from the bl  structure
for i=1:1:length(cond_match.btlnbr),
    for j=1:1:length(bl),
      if (cond_match.stnnbr(i)==bl(j).station), 
		% & cond_match.castnbr(i)==bl(j).cast)   
	  sta_idx=j;
      end;
    end;
    fire_index=find(bl(sta_idx).niskinnumber==cond_match.btlnbr(i));
    if isempty(fire_index),
	disp(['No matching bl entry for the current sample.  ', ...
		'Likely, no confirmation bit.']);
        disp(['Station ',num2str(cond_match.stnnbr(i)), ...
		' Niskin # ',num2str(cond_match.btlnbr(i))])
        disp('This sample will be disregarded')
        disp('Hit any key to continue')
        pause
        cond_match.fireorder(i,1)=NaN;
    else
	% TODO: The autosal analysis may be done even if bottle trips 
	% were not confirmed by the fish. How do we handle this.  
	% Alter subsequent programs accordingly.
	cond_match.fireorder(i,1)=bl(sta_idx).fireorder(fire_index);
    end;
end;
%end  

% keeping only the valid data
jj_nan=find(isnan(cond_match.fireorder)==0);
cond_match.fireorder=cond_match.fireorder(jj_nan);
cond_match.stnnbr=cond_match.stnnbr(jj_nan);
cond_match.castnbr=cond_match.castnbr(jj_nan);
cond_match.sampnbr=cond_match.sampnbr(jj_nan);
cond_match.btlnbr=cond_match.btlnbr(jj_nan);
cond_match.niskinbtlpos=cond_match.niskinbtlpos(jj_nan);
cond_match.btlsal=cond_match.btlsal(jj_nan);
%
% information from .btl files  
 disp('**************************************');
 disp('Extracting information from .btl files');
 disp('**************************************');  
% information from .btl files

badstns=[];
for i=1:length(cond_match.btlsal)
%% instead of ...
%    for j=1:1:length(btl),
%      if cond_match.stnnbr(i)==btl(j).station,
%	%& cond_match.castnbr(i)==bl(j).cast      
%	  sta_idx=j;
%      end;
%    end;
%% why dont we just ...
	sta_idx=find(cond_match.stnnbr(i)==[btl.station]);
%%
    fire_idx=cond_match.fireorder(i);

	% extracting the indexes for the .btl files;
	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),'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; % for j=1:length(btl(sta_idx).names),

        cond_match.latitude(i,1)=btl(sta_idx).lat;
        cond_match.longitude(i,1)=btl(sta_idx).lon;
        cond_match.ctdupprs(i,1)=btl(sta_idx).data(fire_idx,pr_idx);
	cond_match.ctdupcon1(i,1)=10*btl(sta_idx).data(fire_idx,ctd_c1_idx);
	%% CMDR test for sec sensors
	if exist('ctd_c2_idx'),
	   cond_match.ctdupcon2(i,1)=10*btl(sta_idx).data(fire_idx,ctd_c2_idx);
	end;
	cond_match.ctduptmp1(i,1)=btl(sta_idx).data(fire_idx,ctd_t1_idx);
	%% CMDR test for sec sensors
        if exist('ctd_c2_idx'),
	    cond_match.ctduptmp2(i,1)=btl(sta_idx).data(fire_idx,ctd_t2_idx);
	end;
	if flag_conv1==1,
	    cond_match.ctduptmp1(i,1)=t68tot90(cond_match.ctduptmp1(i,1));   
	end;
	%% CMDR test for sec sensors
        if exist('ctd_c2_idx'),
	    cond_match.ctdtmp2(i,1)=btl(sta_idx).data(fire_idx,ctd_t2_idx);
	end;
	if flag_conv2==1;
	    cond_match.ctduptmp2(i,1)=t68tot90(cond_match.ctduptmp2(i,1));   
    	end;

    end; % for i=1:length(cond_match.btlsal)
%

%calculating the salinity
    cond_match.ctdupsal1=sw_salt(cond_match.ctdupcon1/sw_c3515, ...
		cond_match.ctduptmp1,cond_match.ctdupprs);
%%%%%%%%%% CMDR test for sec sensors
    t2=cond_match.ctduptmp2;
    if exist('t2'), 
	cond_match.ctdupsal2=sw_salt(cond_match.ctdupcon2/sw_c3515, ...
		cond_match.ctduptmp2,cond_match.ctdupprs);
	end;
%%%%%%%%%%

%calculating the neutral density (gamma n)
station_list=unique(cond_match.stnnbr);
for i=1:length(station_list),
    aux=find(cond_match.stnnbr==station_list(i));
    [g,dg_lo,dg_hi] = gamma_n(cond_match.ctdupsal1(aux), ...
		t90tot68(cond_match.ctduptmp1(aux)), ...
		cond_match.ctdupprs(aux),cond_match.longitude(aux(1)), ...
		cond_match.latitude(aux(1)));
    cond_match.ctdupgamman1(aux,1)=g;
%%%%%%%%%% CMDR test for sec sensors
    sal2=cond_match.ctdupsal2;
    if exist('sal2'),
        [g,dg_lo,dg_hi] = gamma_n(cond_match.ctdupsal2(aux), ...
		t90tot68(cond_match.ctduptmp2(aux)), ...
		cond_match.ctdupprs(aux),cond_match.longitude(aux(1)), ...
		cond_match.latitude(aux(1)));
        cond_match.ctdupgamman2(aux,1)=g;  
    end; % if exist('cond_match.ctdupsal2'),
%%%%%%%%%%
end; % for i=1:length(station_list),

%    
% Interpolating using the cnv files
% estimating dcdp term from the .cnv files
%   
%
% 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(cond_match.stnnbr);
    for j=1:1:length(cnv_dlp)
        if cond_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; % if new_station~=new_station_old
        end; % if cond_match.stnnbr(i)==cnv_dlp(j).station
    end; % for j=1:1:length(cnv_dlp)

    press_idx=round(cond_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);
         cond1_aux=10*cnv_dlp(sta_idx).condpri(1:max_pressure_idx);
         temp1_aux=cnv_dlp(sta_idx).temppri(1:max_pressure_idx);
         cond2_aux=10*cnv_dlp(sta_idx).condsec(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);
         cond1_aux=10*cnv_dlp(sta_idx).condpri(1:100);
         temp1_aux=cnv_dlp(sta_idx).temppri(1:100);
    	 %% CMDR test sec sensors
	 if ~isempty(cnv_dlp(sta_idx).condsec),
             cond2_aux=10*cnv_dlp(sta_idx).condsec(1:100);
         end;
         if ~isempty(cnv_dlp(sta_idx).tempsec),
             temp2_aux=cnv_dlp(sta_idx).tempsec(1:100);
         end;
    elseif (isempty(lim_idx)==1)
	 %% CMDR do we really need to know if we passed here? 
	 % disp('passed here') 
         press_aux=cnv_dlp(sta_idx).pressure( ...
			max_pressure_idx-100:max_pressure_idx);
         cond1_aux=10*cnv_dlp(sta_idx).condpri(max_pressure_idx-100:max_pressure_idx);
         temp1_aux=cnv_dlp(sta_idx).temppri(max_pressure_idx-100:max_pressure_idx);
%%%%%%%%%%% CMDR test sec sensors
	if ~isempty(cnv_dlp(sta_idx).condsec),
         cond2_aux=10*cnv_dlp(sta_idx).condsec(max_pressure_idx-100:max_pressure_idx);
         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);
         cond1_aux=10*cnv_dlp(sta_idx).condpri(1:max_pressure_idx);
         temp1_aux=cnv_dlp(sta_idx).temppri(1:max_pressure_idx);
%%%%%%%%%%% CMDR test sec sensors
	if ~isempty(cnv_dlp(sta_idx).condsec),
         cond2_aux=10*cnv_dlp(sta_idx).condsec(1:max_pressure_idx);
         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);
         cond1_aux=10*cnv_dlp(sta_idx).condpri(1:lim_idx+100);
         temp1_aux=cnv_dlp(sta_idx).temppri(1:lim_idx+100);
%%%%%%%%%%% CMDR test sec sensors
	if ~isempty(cnv_dlp(sta_idx).condsec),
         cond2_aux=10*cnv_dlp(sta_idx).condsec(1:lim_idx+100);
         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);
          cond1_aux=10*cnv_dlp(sta_idx).condpri(lim_idx-100:max_pressure_idx);
          temp1_aux=cnv_dlp(sta_idx).temppri(lim_idx-100:max_pressure_idx);
%%%%%%%%%%% CMDR test sec sensors
	if ~isempty(cnv_dlp(sta_idx).condsec),
          cond2_aux=10*cnv_dlp(sta_idx).condsec(lim_idx-100:max_pressure_idx);
          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);
         cond1_aux=10*cnv_dlp(sta_idx).condpri(lim_idx-100:lim_idx+100);
         temp1_aux=cnv_dlp(sta_idx).temppri(lim_idx-100:lim_idx+100);
%%%%%%%%%%% CMDR test sec sensors
	if ~isempty(cnv_dlp(sta_idx).condsec),
         cond2_aux=10*cnv_dlp(sta_idx).condsec(lim_idx-100:lim_idx+100);
         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 sec 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 sec 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);
	end;
%%%%%%%%%%% 
%%%%%%%% CMDR test sec sensors, this is a bad kluj to get this over with! We
%%%%%%%% are left with an empty wariable for a secondary sensor where other 
%%%%%%%% wariables for secondary sensors do not exist. Should figure out a 
%%%%%%%% way to deal with missing secondary sensors.
	if ~exist('gamma_n_aux2'), gamma_n_aux2=[]; end;
        if ~isreal(gamma_n_aux1) | ~isreal(gamma_n_aux2)
        error('Problems found in the .cnv file. Please check this station before continue')
        end
        
%    if sta_idx==12
%        keyboard
%    end
%%%% Interpolating the variables
  % making the vectors distinct for interp.
      if (length(gamma_n_aux1)>35);
      gamma_n_aux1=weim(35,'hann',gamma_n_aux1); 
      gamma_n_aux2=weim(35,'hann',gamma_n_aux2);
      elseif (length(gamma_n_aux1)>17 & length(gamma_n_aux1)<35)
      gamma_n_aux1=weim(17,'hann',gamma_n_aux1); 
      gamma_n_aux2=weim(17,'hann',gamma_n_aux2);    
      else
      gamma_n_aux1=weim(7,'hann',gamma_n_aux1); 
      gamma_n_aux2=weim(7,'hann',gamma_n_aux2);    
      end 
      if sta_idx==72
        gamma_n_aux2=gamma_n_aux1;  
      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);
    %%%%%%
    new_press(i)=interp1(gamma_n_aux1,press_aux,cond_match.ctdupgamman1(i),'linear',NaN);
    new_cond(i)=interp1(gamma_n_aux1,cond1_aux,cond_match.ctdupgamman1(i),'linear',NaN);
    new_temp(i)=interp1(gamma_n_aux1,temp1_aux,cond_match.ctdupgamman1(i),'linear',NaN);
%%%%%%%%%% CMDR test for sec sensors
if exist('cond2_aux'),
    new_cond2(i)=interp1(gamma_n_aux2,cond2_aux,cond_match.ctdupgamman2(i),'linear',NaN);
    new_temp2(i)=interp1(gamma_n_aux2,temp2_aux,cond_match.ctdupgamman2(i),'linear',NaN);
end;
%%%%%%%%%% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% CMDR the following code has been shuffled around to group the sensors,
%%%%%%%      so we can have one test.
% Estimating  DCDP
   % aux_dcdp1=weim(11,'hann',gradient(cond1_aux))';
% % interpolating  DCDP    
% 	new_dcdp1=interp1(gamma_n_aux1,aux_dcdp1,cond_match.ctdupgamman1(i),'spline',NaN);
% 
% 	if isnan(new_dcdp1)==1
% 		cond_match.dcdp1(i,1)=mean(aux_dcdp1);
% 	else
% 		cond_match.dcdp1(i,1)=new_dcdp1;    
% 	end
%%%%%%%%%% CMDR test for sec sensors
% if exist('cond2_aux'),
% 	%aux_dcdp2=weim(11,'hann',gradient(cond2_aux))';
% 	new_dcdp2=interp1(gamma_n_aux2,aux_dcdp1,cond_match.ctdupgamman2(i),'spline',NaN);
% 	if isnan(new_dcdp2)==1
% 		cond_match.dcdp2(i,1)=mean(aux_dcdp2);
% 	else
% 		cond_match.dcdp2(i,1)=new_dcdp2;    
% 	end
% end;
%%%%%%%%%% 
 %End DCDP
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 end
 disp('**************************************************');
 disp('Interpolating upcast info on downcast using sigma ')
 disp('**************************************************');
 cond_match.ctddotmp1=new_temp';
 cond_match.ctddocon1=new_cond';
%%%%%%%%% CMDR test for sec sensors
if exist('new_temp2'),
 cond_match.ctddotmp2=new_temp2';
 cond_match.ctddocon2=new_cond2';
end; 
%%%%%%%%% 
 cond_match.ctddoprs=new_press';
 
 %using the surface values of the upcast ( case missing )
% primary sensor
aux=find(isnan(cond_match.ctddocon1)==1);
cond_match.ctddotmp1(aux)=cond_match.ctduptmp1(aux);
cond_match.ctddocon1(aux)=cond_match.ctdupcon1(aux);
cond_match.ctddoprs(aux)=cond_match.ctdupprs(aux);
%
%aux=find(cond_match.ctddoprs<0);
%cond_match.ctddotmp1(aux)=cond_match.ctduptmp1(aux);
%cond_match.ctddocon1(aux)=cond_match.ctdupcon1(aux);
%cond_match.ctddoprs(aux)=cond_match.ctdupprs(aux);
% secondary Sensor
%%%%%%%%%% CMDR test for sec sensors
if exist('cond_match.ctddocon2'),
aux=find(isnan(cond_match.ctddocon2)==1);

cond_match.ctddotmp2(aux)=cond_match.ctduptmp2(aux);
cond_match.ctddocon2(aux)=cond_match.ctdupcon2(aux);
end;
%%%%%%%%%% 
%aux=find(cond_match.ctddoprs<0);
%cond_match.ctddotmp2(aux)=cond_match.ctduptmp2(aux);
%cond_match.ctddocon2(aux)=cond_match.ctdupcon2(aux);
disp('*************************************************');
disp('Matching interpolated information with autosal files');
disp('*************************************************');
% updating the fields for save
%%%%%%%%% CMDR rearrange code and test for sec sensors
cond_match.ctdprs=cond_match.ctdupprs;
cond_match.ctdtmp1=cond_match.ctduptmp1;
cond_match.ctdcon1=cond_match.ctdupcon1;
cond_match.ctdsal1=sw_salt(cond_match.ctdcon1/sw_c3515,cond_match.ctdtmp1,cond_match.ctdprs);
cond_match.btlcon1=sw_c3515*sw_cndr(cond_match.btlsal,cond_match.ctdtmp1,cond_match.ctdprs);
%
%if exist('cond_match.ctdupcon2'),
if isfield(cond_match,'ctdupcon2'),
cond_match.ctdtmp2=cond_match.ctduptmp2;
cond_match.ctdcon2=cond_match.ctdupcon2;
cond_match.ctdsal2=sw_salt(cond_match.ctdcon2/sw_c3515,cond_match.ctdtmp2,cond_match.ctdprs);
cond_match.btlcon2=sw_c3515*sw_cndr(cond_match.btlsal,cond_match.ctdtmp2,cond_match.ctdprs);
end;
%%%%%%%%%%  CMDR end edit
%
cond_match.quality1(1:length(cond_match.stnnbr),1)=2;
cond_match.quality2(1:length(cond_match.stnnbr),1)=2;
%Save the conductivity summary structure.
save(db_file,'cond_match','-append');

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

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

return;

