%This matlab script takes all the .dat files in the directory,
%takes the first line of their header and compares them and
%bins them into files that should be read together, files with
%identical start dates and start times

%All the files in the directory should have been stripped
%of their commas by the time we run this file; this file
%is looking for files with 10 lines of alpha numeric 
%header followed by number fields, multiple fields separated
%by two blank lines and a single line of alpha numeric label.

%Gwyneth E. Packard 7 August 1997

% MFC  23 July 2000
% The input files are called cvt????.dat here for weddell_9900
% which I think has been the case for quite some time
% The standard for output file name is now raw*.mat

% ETM 26 July 2000
% Fixed the data problem that comes about from the year field not
% being two digits when > 2000 in the MVP ascii header conversion.
% the date string might be 4/6/0, and it needs to be 4/6/00. 
% (I checked and 4/6/10 is correctly interpreted as 2010, NOT 1910!!!)
%*************************************************************

fid=fopen('hold.first.line','r');
counter=1;
for i=1:10000
line=fgetl(fid);
if line
switch line(1)
  case 'C'
  header(counter).line=line;
  counter=counter+1;
  case [-1]
  break
end	%switch
end	%if line exists
end	%looping over file lines

complines=char(header.line);

havezero=input('What cvt file is the first in the directory?');
if havezero==0
dozero=0; 
jump=-1; 
else
dozero=havezero;
jump=havezero-1;
end

DEFAULT=header(1).line;
counter=1;
call(1).list=[dozero];	%*******file name dependent*************
for i=2:size(complines,1)
switch DEFAULT
  case {header(i).line}
  %we have a match so associate 1 & 2
  call(counter).list=[call(counter).list i+jump];
  otherwise
  %no match, change default
  counter=counter+1;
  call(counter).list=[i+jump];
  DEFAULT=header(i).line;
end	%end switch
end	%end looping over header lines

clear DEFAULT ans complines counter fid header i line
clear jump dozero havezero

save filebins

numfilesets=size(call,2);
fileset=input('What file set do we want to start with?');
disp(['The files in this set are: ', num2str(call(fileset).list)])
fileset=input('Verify, enter this fileset again if correct');
for k=fileset:numfilesets,	%for as many sets of files as exist
for j=1:length(call(k).list) %for as many files as are in the set
%************file name dependent line - edit here**********************
disp(['Loading file ',sprintf('%4.4i',call(k).list(j))])
eval(['fid=fopen(''cvt',sprintf('%4.4i',call(k).list(j)),'.dat'',''r'');']) 
blank=fgetl(fid);
for i=1:10,
line=fgetl(fid);
pointers=find(line=='=');
%
%  In the header lines, if the date is less than 8 characters (11/29/99), the
%  end will be whatever follows it on the line, so 4/12/99 will have one space 
%  at the end; 2/15/0 would have 2 spaces; and 1/2/0, would have2 spaces 
%  and the following letter, so you have to check string(end) and get rid
%  of it, if it's there.
%
%  The other issue is that for dates in 2000, the year is only 1 char,
%  and matlab's datenum interprets this as 000.  So to allow for years 2001, 
%  and so on, 0 has to be INSERTED after the slash.
%
switch line(1:9)
  case 'CTD Start'
    cdate=line(pointers(1)+2:pointers(1)+9); 
     locs= find(cdate == '/');
     if (cdate(locs(2)+2) == ' ')
        cdate(locs(2)+2)=cdate(locs(2)+1);
        cdate(locs(2)+1)='0';
       if isempty(str2num(cdate(end)))      
         cdate(end) = ' ';
       end
      end
    ctime=line(pointers(2)+2:end);
  case 'ACM Start'
     adate=line(pointers(1)+2:pointers(1)+9); 
     locs= find(adate == '/');
      if (adate(locs(2)+2) == ' ')
       adate(locs(2)+2)=adate(locs(2)+1);
       adate(locs(2)+1)='0';
       if isempty(str2num(adate(end))) 
         adate(end) = ' ';
       end
      end
    atime=line(pointers(2)+2:end);
  case 'P Start D'
    psdate=line(pointers(1)+2:pointers(1)+9);
     locs= find(psdate == '/');
     if (psdate(locs(2)+2) == ' ')
       psdate(locs(2)+2)=psdate(locs(2)+1);
       psdate(locs(2)+1)='0';
       if isempty(str2num(psdate(end))) 
        psdate(end) = ' ';
       end
     end
    pstart=line(pointers(2)+2:end);
  case 'P Stop Da'
    pedate=line(pointers(1)+2:pointers(1)+9);
     locs= find(pedate == '/');
     if (pedate(locs(2)+2) == ' ')
      pedate(locs(2)+2)=pedate(locs(2)+1);
      pedate(locs(2)+1)='0';
       if isempty(str2num(pedate(end))) 
         pedate(end) = ' ';
       end
     end
    pstop=line(pointers(2)+2:end);
  case 'Engineeri'
     escans=line(pointers(1)+2:end);
  case 'CTD Scans'
     cscans=line(pointers(1)+2:end);
  case 'ACM Scans'
     ascans=line(pointers(1)+2:end);
  case 'File Numb'
     file(j)=str2num(line(pointers(1)+2:end));

  end	%switch case
end	%looping on header lines

escans=str2num(escans);cscans=str2num(cscans);ascans=str2num(ascans);

%if there were scans for same variable in last file, need to add 
%these new ones to an existing matrix
if exist('edat'); e=size(edat,1)+1; f=size(edat,1)+escans; 
else; e=1; f=escans; end
if exist('cdat'); c=size(cdat,1)+1; d=size(cdat,1)+cscans;
else; c=1; d=cscans; end
if exist('adat'); a=size(adat,1)+1; b=size(adat,1)+ascans;
else; a=1; b=ascans; end

if (escans~=0 & cscans~=0)	%engineering and CTD data
 tmpdat=fscanf(fid,'%5i %f %f %f %f %f',[6,escans])';
 %edat(e:f,:)=fscanf(fid,'%5i %f %f %f %f %f',[6,escans])';
 edat(e:e+size(tmpdat,1)-1,:)=tmpdat;
 clear tmpdat
  blank=fgetl(fid);
  blank=fgetl(fid);
  line=fgetl(fid);
disp('i get into eng & ctd');
  tmpdat=fscanf(fid,'%f %f %f %f',[4,cscans])';
  %cdat(c:d,:)=fscanf(fid,'%f %f %f %f',[4,cscans])';
  cdat(c:c+size(tmpdat,1)-1,:)=tmpdat;
  clear tmpdat
	esnum=edat(:,1); epres=edat(:,2); ecurr=edat(:,3);
        eV1=edat(:,4); eV2=edat(:,5); ept=edat(:,6);
	csnum=cdat(:,1); ccon=cdat(:,2); ctemp=cdat(:,3); 
	cpres=cdat(:,4);
elseif (escans~=0 & cscans==0 & ascans==0)	%engineering data
disp('i get into eng only')
  tmptroub=fscanf(fid,'%5i %f %f %f %f %f',[6,escans])';
  %etrouble(e:f,:)=fscanf(fid,'%5i %f %f %f %f %f',[6,escans])';
  etrouble(e:e+size(tmptroub,1)-1,:)=tmptroub;
  clear tmptroub
  disp(['File cvt',sprintf('%3.3i',call(k).list(j)),'.dat has only engineering data'])
elseif (escans==0 & cscans==0 & ascans~=0)	%ACM data
disp('i get into the acm only') 
  tmpdat=fscanf(fid,'%f %f %f %f %f %f %f %f %f %f',[10,ascans])';
  %adat(a:b,:)=fscanf(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f',[13,ascans])';
  adat(a:a+size(tmpdat,1)-1,:)=tmpdat;
  clear tmpdat
disp('i get adat assigned')
	asnum=adat(:,1); aTx=adat(:,2); aTy=adat(:,3); aHx=adat(:,4);
	aHy=adat(:,5); aHz=adat(:,6); Vab=adat(:,7); Vcd=adat(:,8); 
        Vef=adat(:,9); Vgh=adat(:,10);
elseif (escans~=0 & cscans==0 & ascans~=0)	%ACM data
disp('i get into eng & acm')
  tmpdat=fscanf(fid,'%5i %f %f %f %f %f',[6,escans])';
  %edat(e:f,:)=fscanf(fid,'%5i %f %f %f %f %f',[6,escans])';
  edat(e:e+size(tmpdat,1)-1,:)=tmpdat;
  clear tmpdat
	esnum=edat(:,1); epres=edat(:,2); ecurr=edat(:,3);
        eV1=edat(:,4); eV2=edat(:,5); ept=edat(:,6);
  blank=fgetl(fid);
  line=fgetl(fid);
  tmpdat=fscanf(fid,'%f %f %f %f %f %f %f %f %f %f',[10,ascans])';
  %adat(a:b,:)=fscanf(fid,'%f %f %f %f %f %f %f %f %f %f %f %f',[12,ascans])';
  adat(a:a+size(tmpdat,1)-1,:)=tmpdat;
  clear tmpdat
	asnum=adat(:,1); aTx=adat(:,2); aTy=adat(:,3); aHx=adat(:,4);
	aHy=adat(:,5); aHz=adat(:,6); Vab=adat(:,7); Vcd=adat(:,8); 
        Vef=adat(:,9); Vgh=adat(:,10);
end	%sorting out situation

closefile=fclose(fid);
end	%looping on files in set

clear line blank i pointers escans cscans ascans fid 
%eval(['save ../matfmt/cvt',sprintf('%4.4i',k),'.mat'])
% we now call these output files matfmt/raw*.mat
eval(['save ../matfmt_new/raw',sprintf('%4.4i',k),'.mat'])
clear edat cdat adat esnum epres ecurr eV1 eV2 ept csnum ccon ctemp file
clear cpres asnum aVx aVy aVz aTx aTy aHx aHy aHz etrouble 
end	%looping on sets

