%make oxygen and conductivity match files the same length for each station.
%The oxygen bottle samples have fewer samples than the salinity bottle
%samples for some stations.
%
%Also print out the table for LaTeX
if 0,
for ss=0:length(unique(cond_match_cal.stnnbr))-1
    
    jj=find(cond_match_cal.stnnbr==ss);
    kk=find(oxy_match_cal.stnnbr==ss);
    
        
    %%%%%%%%%%%%%%Create variables to be equal lengths%%%%%%%%%%%%%%%%%%%%%
    %salts
    btl_sal=nan*[1:length(jj)]';    
    sal_nisk=btl_sal;
    sal_qual=btl_sal;
    ctd_sal=btl_sal;
    ctd_stem=btl_sal;
    ctd_prs=btl_sal;
    ctd_dph=btl_sal;
    
    %oxygen
    btl_oxy=btl_sal;
    oxy_nisk=btl_sal;
    oxy_qual=btl_sal;
    ctd_oxy=btl_sal;
    ctd_otem=btl_sal;
    
    %%%%%%%%%%%%%%%%%Add salts data to the variables%%%%%%%%%%%%%%%%%%%%%%%
    btl_sal(1:length(cond_match_cal.btlsal(jj)))=cond_match_cal.btlsal(jj);
    sal_nisk(1:length(cond_match_cal.niskinbtlpos(jj)))=cond_match_cal.niskinbtlpos(jj);
    sal_qual(1:length(cond_match_cal.quality1(jj)))=cond_match_cal.quality1(jj);
    ctd_sal(1:length(cond_match_cal.ctdsal(jj)))=cond_match_cal.ctdsal(jj);
    ctd_stem(1:length(cond_match_cal.ctdtmp(jj)))=cond_match_cal.ctdtmp(jj);
    ctd_prs(1:length(cond_match_cal.ctdprs(jj)))=cond_match_cal.ctdprs(jj);
    ctd_dph(1:length(cond_match_cal.ctdprs(jj)))=sw_dpth(cond_match_cal.ctdprs(jj),cond_match_cal.latitude(jj));
    
    %Add oxygen data to the variables
    btl_oxy(1:length(oxy_match_cal.btloxy(kk)))=oxy_match_cal.btloxy(kk);
    oxy_nisk(1:length(oxy_match_cal.niskinbtlpos(kk)))=oxy_match_cal.niskinbtlpos(kk);
    oxy_qual(1:length(oxy_match_cal.quality1(kk)))=oxy_match_cal.quality1(kk);
    ctd_oxy(1:length(oxy_match_cal.ctdoxyumolkg(kk)))=oxy_match_cal.ctdoxyumolkg(kk);
    ctd_otem(1:length(oxy_match_cal.ctdtmp(kk)))=oxy_match_cal.ctdtmp(kk);
    
    time=([1:length(ss),1:12]);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    
    ll=ss+1;
    for ii=1:24
    
        sn=find(sal_nisk==ii);
        on=find(oxy_nisk==ii);
    
            if isempty(on)
                oxy_aux(ii,ll)=-999;
                onisk_aux(ii,ll)=-999;
                oqual_aux(ii,ll)=-999;
                ctdoxy(ii,ll)=-999;
                ctdotem(ii,ll)=-999;
            else
                oxy_aux(ii,ll)=btl_oxy(on);
                onisk_aux(ii,ll)=oxy_nisk(on);
                oqual_aux(ii,ll)=oxy_qual(on);
                ctdoxy(ii,ll)=ctd_oxy(on);
                ctdotem(ii,ll)=ctd_otem(on);
                
            end;
        
            if isempty(sn)
                sal_aux(ii,ll)=-999;
                snisk_aux(ii,ll)=-999;
                squal_aux(ii,ll)=-999;
                ctdsal(ii,ll)=-999;
                ctdstem(ii,ll)=-999;
                ctdprs(ii,ll)=-999;
                ctddph(ii,ll)=-999;
                stt(ii,ll)=ss;
            else
                sal_aux(ii,ll)=btl_sal(sn);
                snisk_aux(ii,ll)=sal_nisk(sn);
                squal_aux(ii,ll)=sal_qual(sn);
                ctdsal(ii,ll)=ctd_sal(sn);
                ctdstem(ii,ll)=ctd_stem(sn);
                ctdprs(ii,ll)=ctd_prs(sn);
                ctddph(ii,ll)=ctd_dph(sn);
                stt(ii,ll)=ss;
            end;
            nisk(ii,ll)=ii;
            
     end;
   

end;
%clear useless variables
clear ss ii jj kk ll sn on btl_sal btl_oxy sal_nisk oxy_nisk sal_qual oxy_qual
clear ctd_sal ctd_oxy ctd_prs ctd_dph ctd_stem ctd_otem
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                      Create btl sum table LaTex                         %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


fid=fopen('woce_btl_sum.txt','w');
fprintf(fid,'%s','\tiny');
fprintf(fid,'\n');
fprintf(fid,'%s','\begin{landscape}');
fprintf(fid,'\n');
fprintf(fid,'%s','\begin{longtable}{llllllllllllllllllllll}');
fprintf(fid,'\n');    
fprintf(fid,'%s','\caption{Abaco Cruise -- WOCE Bottle Summary File\label{woce_btl} }\\');
fprintf(fid,'\n');   
fprintf(fid,'%s','\hline');
fprintf(fid,'\n');    
fprintf(fid,'%s','SHIP/CRS & WOCE &     &      &       & BTL\# &      & UTC ');
fprintf(fid,'%s',' &      &     &       & CTD & CTD  & CTD & SAL  & BTL & SAL  & CTD & OXY  & BTL & OXY\\');
fprintf(fid,'\n');    
fprintf(fid,'%s','EXPOCODE & SECT & STN & CAST & BTL\# & Flag  & DATE & TIME ');
fprintf(fid,'%s','&  LAT & LON & DEPTH & PRS & TMP  & SAL & FLAG & SAL & FLAG & OXY & FLAG & OXY & FLAG\\');
fprintf(fid,'\n');    
fprintf(fid,'%s','\hline');
fprintf(fid,'\n'); 
fprintf(fid,'\n'); 

  expocode='WBTSNSRHB';
  sect_id='AB1202';
  cast_no=1;
  btl_flag=2;
  sal_flag=2;
  oxy_flag=2;
  
  for i=1:length(stt),
      for j=1:24;
          date=datestr(btl(i).time(1),'yyyymmdd');
          time=datestr(btl(i).time(1),'HHMM');
          lat=cnv_cal(i).latitude;
          lon=-1*cnv_cal(i).longitude;
      fprintf(fid,'%s%s\t %s%s\t %d%s\t %d%s\t %d%s\t %d%s\t %s%s\t %s%s\t %2.3f%s%s\t %2.3f%s%s\t',...
          expocode,'&',sect_id,'&',stt(j,i),'&',cast_no,'&',nisk(j,i),'&',btl_flag,'&',date,'&',time,'&',lat,'N','&',lon,'W','&');
      fprintf(fid,'%4.0f%s\t %4.0f%s\t %2.4f%s\t %2.4f%s\t %d%s\t %2.4f%s\t %d%s\t %3.1f%s\t %d%s\t %3.1f%s\t %d%s\n',...
          ctddph(j,i),'&',ctdprs(j,i),'&',ctdstem(j,i),'&',ctdsal(j,i),'&',sal_flag,'&',sal_aux(j,i),'&',squal_aux(j,i),'&',...
          ctdoxy(j,i),'&',oxy_flag,'&',oxy_aux(j,i),'&',oqual_aux(j,i),'\\');
      end;
      clear date time lat lon
  end;
  
fprintf(fid,'\n');    
fprintf(fid,'%s','\hline');
fprintf(fid,'\n');          
fprintf(fid,'%s','\end{longtable}');
fprintf(fid,'\n');
fprintf(fid,'%s','\end{landscape}');

fclose(fid); 