% MP_asc2mat_oldstyle_SFTRE
%                                                      
% Routine to read in respective files:
%   CTD and ENG in one file
%   ACM in another
%      possibility of several ACM files per profile if a long profile
%      also the possibility of no ACM or no CTD data
%      and this was originally written with the possibility of ACM and CTD data together.  
%
%  This is a combination of the old mvp2mat_etm.m
%  and the newer MMP_asc2mat.m
%
%  It assigns variable names and outputs (as Matlab-format data files)
%  raw*.mat to a user-specified data directory
%
% 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
%
%   To make the necessary input file hold.first.line in the cvt directory:
%
%       head -2 cvt*.dat > hold.first.line
%
% All of the cvt 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.
%
% The input files are called cvt????.dat
% The standard for output file name is  raw????.mat
%
% ETM 26 July 2000
% Fixed the date 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!!!)
%
%*************************************************************
% 
% 8 Apr 2002
% John has discovered that for VEX and SFTRE that are coming in soon
% with the "old" style MMP's ... switching aHy and aHz will work!
%
% SFTRE: If we simply say aHx = orig_aHx, and aHy=orig_aHz, 
% the following returns the true direction:
%    direction= -1 * atan2(aHx,aHy) + 0.0440
%
% This is in the form of our MMP direction routine with direction_sign =
% -1 and direction bias = 0.0440.  
%
%
% VEX:  it will also work with a y-z swap of the compass channels.  
% In this case, direction= +1 * atan2(Hx,Hz) + 0   looks to do the trick.
%*************************************************************


% Enter output directory  name:
disp([sprintf('\n'),'MAKE SURE YOU ARE IN THE DIRECTORY CONTAINING INPUT cvt FILES ',sprintf('\n')])
[outdir]=input('Into which directory do you want to put the output raw*.mat file? ','s');
disp(' ')

% Open hold.first.line (see above for how to create this)
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);


% Figure out which data (ACM,CTD,ENG) go together
havezero=input('What cvt file number 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=='=');



% Fix the date problem
%  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 have 2  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); ccond=cdat(:,2); ctemp=cdat(:,3); 
	cpres=cdat(:,4);

        evolt=eV1;
        orig_epres=epres;

%come up with engtime from ctd times ...  8 april 2002
%extract the profile start and stop times 

    j=find(psdate=='/');
    psdate(j)=' ';
    date=str2num(psdate);
    
    mon=date(1);
    day=date(2);   
    year=2000+date(3);
        
    j=find(pstart==':');
    pstart(j)=' ';
    start=str2num(pstart);
    
    hour=start(1);
    minute=start(2);
    second=start(3);
    
    startdaytime=datenum(year,mon,day,hour,minute,second);
    
    j=find(pedate=='/');
    pedate(j)=' ';
    date=str2num(pedate);
    
    mon=date(1);
    day=date(2);
    year=2000+date(3);
        
    j=find(pstop==':');
    pstop(j)=' ';
    stop=str2num(pstop);
    
    hour=stop(1);
    minute=stop(2);
    second=start(3);
    
    stopdaytime=datenum(year,mon,day,hour,minute,second);

    engtime=startdaytime + (stopdaytime-startdaytime)/length(epres) * [0:length(epres)-1];
    engtime=engtime';

        if ~isnan(cpres(60))
            epres(1)=cpres(60);
        else
            disp('cpres(60) is a NaN');
        end
        epres(end)=cpres(end);
        epres(end-1)=cpres(end);
	edpdt=gradient(epres)./gradient(engtime);
 %convert from dbars/day dbars/sec
        edpdt=edpdt/(24*60*60);
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('%4.4i',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);
        orig_Hx=aHx;
        orig_Hy=aHy;
        orig_Hz=aHz;
        aHx=orig_Hx;
        aHy=orig_Hz; 
        aHz=orig_Hy;
elseif (escans~=0 & cscans==0 & ascans~=0)	%ENG & 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);
  evolt=eV1;
  orig_epres=epres;

%come up with engtime from ctd times ...  8 april 2002
%extract the profile start and stop times 

    j=find(psdate=='/');
    psdate(j)=' ';
    date=str2num(psdate);
    
    mon=date(1);
    day=date(2);   
    year=2000+date(3);
        
    j=find(pstart==':');
    pstart(j)=' ';
    start=str2num(pstart);
    
    hour=start(1);
    minute=start(2);
    second=start(3);
    
    startdaytime=datenum(year,mon,day,hour,minute,second);
    
    j=find(pedate=='/');
    pedate(j)=' ';
    date=str2num(pedate);
    
    mon=date(1);
    day=date(2);
    year=2000+date(3);
        
    j=find(pstop==':');
    pstop(j)=' ';
    stop=str2num(pstop);
    
    hour=stop(1);
    minute=stop(2);
    second=start(3);
    
    stopdaytime=datenum(year,mon,day,hour,minute,second);

    engtime=startdaytime + (stopdaytime-startdaytime)/length(epres) * [0:length(epres)-1];
    engtime=engtime';

% no ctd data here so can't do this
%  if ~isnan(cpres(60))
%      epres(1)=cpres(60);
%  else
%      disp('cpres(60) is a NaN');
%  end
%  epres(end)=cpres(end);
%  epres(end-1)=cpres(end);
%  edpdt=gradient(epres)./gradient(engtime);


  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);
        orig_Hx=aHx;
        orig_Hy=aHy;
        orig_Hz=aHz;
        aHx=orig_Hx;
        aHy=orig_Hz; 
        aHz=orig_Hy;
end	%sorting out situation

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


% for this SFTRE_01 cruise only !!!!
% add 100 scans of CTD data at end of each trace
% equal to the last good value
% to get a flatline to do acm/ctd alignment with.

 cpres=[cpres' ones(1,100)*cpres(end)]';
 ctemp=[ctemp' ones(1,100)*ctemp(end)]';
 ccond=[ccond' ones(1,100)*ccond(end)]';

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 ',outdir,'/raw',sprintf('%4.4i',k),'.mat numfilesets file fileset Vab Vcd Vef Vgh orig_Hx orig_Hy orig_Hz aHx aHy aHz aTx aTy asnum ccond cpres csnum ctemp ecurr ept edpdt engtime orig_epres epres esnum evolt eV2 pedate psdate  pstart pstop startdaytime stopdaytime'])
clear edat cdat adat esnum epres orig_epres ecurr evolt eV1 eV2 ept csnum ccondecurr ctemp file
clear cpres asnum aVx aVy aVz aTx aTy orig_Hx orig_Hy orig_Hz aHx aHy aHz etrouble 
end	%looping on sets








