function cond_cal = make_cond_sum(cruiseid)
% MAKE_COND_SUM -  Makes conductivity bottle/ctd comparison summary file.
%
% CTD Calibration toolbox.
%
% USAGE:
%  [cond_struct] = make_cond_sumfile(cruiseid);
%
% INPUT: cruiseid: Cruise identification name. 
%   
% OUTPUT:  Automatically writes the summary file in the cruise database file.  
%          cond_cal: contains a structure with the same information
%               contained in the *.mat file
%
% 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.
%
% AUTHORS:Carlos Fonseca 
%         UM/CIMAS
%         Derrick Snowden
%         NOAA/AOML/PhOD
%         Tue May 11 11:23:37 EDT 2004
% CHANGELOG: 
%   23-Jul-2004, Version 1.0
%		* Initial version.
%
% DEPENDENCIES: SW package.
%               load_salts
%               load_btl
%               load_bl
%               load_cnv
%
%
% Flags for the temperature scale conversion (t90 to t68)
flag_conv1=0;
flag_conv2=0;
flag_message1=0;
flag_message2=0;
%
cruise_dir=cruiseid;
sd=['cd ',cruise_dir];
eval(sd);
load cond_db.mat
fname=[cruiseid,'_db.mat'];
   if exist(fname)==0
   error([fname,' not found. Run load_btl.m and load_oxy.m ']);
   return;
   end
ld_file=(['load ',fname]);
eval(ld_file);
   n=who('btl');
   if isempty(n)==1
   error(['btl not found. Run load_btl.m ']);
   return;
   end
   n=who('oxy');
   if isempty(n)==1
   error(['oxy not found. Run load_oxy.m ']);
   return;
   end
   n=who('bl');
   if isempty(n)==1
   error(['bl not found. Run load_bl.m ']);
   return;
   end
%
cd ..
cond_cal=cond_struct;
%disp(' 0 = use the upcast profile (btl file) to do the calibration (DEFAULT)' )
%disp(' 1 = use the downcast profile (cnv file) to do the calibration' )
%up_down=input('Do you want to use the downcast (cnv) or the upcast profile (btl): ');
%if (isempty(up_down)==1);
   % up_down=0;
    %end
cond_cal.cruiseid=cruiseid;
%
%information from .salt files
    stations= unique(vertcat(cond(:).station));
    cond_cal.stnnbr=vertcat(cond(:).station);
    cond_cal.castnbr=vertcat(cond(:).station);
    cond_cal.sampnbr=vertcat(cond(:).sample_bottle);
    cond_cal.btlnbr=vertcat(cond(:).niskin_bottle);
    cond_cal.niskinbtlpos=vertcat(cond(:).niskin_bottle); 
    cond_cal.btlsal=vertcat(cond(:).salinity);   
%
% extracting the fire order from the .bl file
     for i=1:1:length(cond_cal.btlnbr)
         i
       if (cond_cal.stnnbr(i)==0)
      cond_cal.fireorder(i,1)=NaN;
      else
      fire_index=find(bl(cond_cal.stnnbr(i)).niskinnumber==cond_cal.btlnbr(i))   
      cond_cal.fireorder(i,1)=bl(cond_cal.stnnbr(i)).fireorder(fire_index);
      end
     end 
% 
% keeping only the valid data
    jj_nan=find(isnan(cond_cal.fireorder)==0);
    cond_cal.fireorder=cond_cal.fireorder(jj_nan);
    cond_cal.stnnbr=cond_cal.stnnbr(jj_nan);
    cond_cal.castnbr=cond_cal.castnbr(jj_nan);
    cond_cal.sampnbr=cond_cal.sampnbr(jj_nan);
    cond_cal.btlnbr=cond_cal.btlnbr(jj_nan);
    cond_cal.niskinbtlpos=cond_cal.niskinbtlpos(jj_nan);
    cond_cal.btlsal=cond_cal.btlsal(jj_nan);
  %  
  % information from .btl files    
   for i=1:length(cond_cal.btlsal)
      sta_idx=cond_cal.stnnbr(i);
      fire_idx=cond_cal.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),'t68_pri',7);
       ctd_t1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'t68_sec',7);
       ctd_t2_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'sal_pri_pri',11);
       ctd_sal1_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'sal_sec_sec',11);
       ctd_sal2_idx=j;
       end
       if strncmp(btl(sta_idx).names(j),'t90_pri',7);
       ctd_t1_idx=j;
       flag_conv1=1;
        if flag_message1==0
        disp('Primary temperatures are given in T90.')
        disp('This program will convert to T68 to make the necessary calculations')
        flag_message1=1;
        end
       end
       if strncmp(btl(sta_idx).names(j),'t90_sec',7);
       ctd_t2_idx=j;
       flag_conv2=1;
        if flag_message2==0
        disp('Secondary temperatures are given in T90.')
        disp('This program will convert to T68 to make the necessary calculations')
        flag_message2=1;
        end  
       end
      end
      cond_cal.ctdprs(i,1)=btl(sta_idx).data(fire_idx,pr_idx);
      cond_cal.ctdcon1(i,1)=10*btl(sta_idx).data(fire_idx,ctd_c1_idx);
      cond_cal.ctdcon2(i,1)=10*btl(sta_idx).data(fire_idx,ctd_c2_idx);
      cond_cal.ctdsal1(i,1)=btl(sta_idx).data(fire_idx,ctd_sal1_idx);
      cond_cal.ctdsal2(i,1)=btl(sta_idx).data(fire_idx,ctd_sal2_idx);
      cond_cal.ctdtmp1(i,1)=btl(sta_idx).data(fire_idx,ctd_t1_idx);
       if flag_conv1==1;
       cond_cal.ctdtmp1(i,1)=t90tot68(cond_cal.ctdtmp1(i,1));   
       end
      cond_cal.ctdtmp2(i,1)=btl(sta_idx).data(fire_idx,ctd_t2_idx);
       if flag_conv2==1;
       cond_cal.ctdtmp2(i,1)=t90tot68(cond_cal.ctdtmp2(i,1));   
       end    
  % estimating dcdp term from the .cnv files
  %   
  % extracting the indexes for the .cnv files;
      for j=1:length(cnv(sta_idx).names);
          if strncmp(cnv(sta_idx).names(j),'prDM',4);
          cnv_pr_idx=j;
          end
          if strncmp(cnv(sta_idx).names(j),'c0S/m',5);
          cnv_c1_idx=j;
          end
          if strncmp(cnv(sta_idx).names(j),'c1S/m',5);
          cnv_c2_idx=j;
          end
      end     
      pressure=cnv(sta_idx).data(:,cnv_pr_idx);
      max_pressure_idx=find(pressure(:)==max(pressure));
      conductivity1=10*cnv(sta_idx).data(:,cnv_c1_idx);
      conductivity2=10*cnv(sta_idx).data(:,cnv_c2_idx); 
      pressure=pressure(1:max_pressure_idx);
      conductivity1=conductivity1(1:max_pressure_idx);
      conductivity2=conductivity2(1:max_pressure_idx);
      aux_dcdp1=weim(21,'hann',gradient(conductivity1))';
      aux_dcdp2=weim(21,'hann',gradient(conductivity2))';
      new_dcdp1=interp1(pressure,aux_dcdp1,cond_cal.ctdprs(i),'spline',NaN);
      new_dcdp2=interp1(pressure,aux_dcdp2,cond_cal.ctdprs(i),'spline',NaN);
         if isnan(new_dcdp1)==1
         cond_cal.dcdp1(i,1)=mean(aux_dcdp1);
         else
         cond_cal.dcdp1(i,1)=new_dcdp1;    
         end
         if isnan(new_dcdp2)==1
         cond_cal.dcdp2(i,1)=mean(aux_dcdp2);
         else
         cond_cal.dcdp2(i,1)=new_dcdp2;    
         end
  end
   cond_cal.btlcon1=sw_C3515*sw_cndr(cond_cal.btlsal,cond_cal.ctdtmp1,cond_cal.ctdprs);
   cond_cal.btlcon2=sw_C3515*sw_cndr(cond_cal.btlsal,cond_cal.ctdtmp2,cond_cal.ctdprs);
   sd=['cd ',cruise_dir];
   eval(sd);
   fname=[cruise_dir,'_db.mat'];
   savefile=['save ',fname,' cond_cal -append'];
   eval(savefile);
   cd .. 
   return