% MMP_asc2mat
% Routine to read in respective files of ACM, CTD and ENG data 
% from each MMP profile: A*.TXT, C*.TXT, E*.TXT 
% assign variable names and output as Matlab-format data files raw*.mat
% to a user-specified data directory
%
% Input: a series of .TXT files created by MRL's unpacking program unpk2_07 
% Output: a series of raw*.mat files
%
%MFC & JMT 02/2002

  
% assign the proper sort of slash, for PC & Unix
arch=computer;
if (strcmp(arch,'PCWIN')) 
   slsh='\';
else
   slsh='/';
end


stas=input('Enter the range of profiles to process i.e. [3:6] :   ');
disp([sprintf('\n'),'Enter the name of the input file directory']);
direc=input('holding the ascii A*.TXT, C*.TXT and E*.TXT files  :  ','s');
disp([sprintf('\n'),'Now enter the name of the output file directory']);
outdir=input('set up to hold the new matlab format raw*.mat files :  ','s');


% the main data loop

for mm=1:length(stas)

%define the input data file names

  efile=['E',sprintf('%7.7i',(stas(mm)))];
  afile=['A',sprintf('%7.7i',(stas(mm)))];
  cfile=['C',sprintf('%7.7i',(stas(mm)))];

%now load the respective files and assign the variables

%  ENGINEERING FILE

  eval(['load ',direc,slsh,efile,'.TXT']);
  eng=eval(efile);
  disp([sprintf('\n'),'Processing ENG file:  ',efile]);
       esnum=[1:1:length(eng)]; 
       epres=eng(:,9); 
       ecurr=eng(:,7);
       evolt=eng(:,8); 
       edpdt=eng(:,10);
       emo=eng(:,1);
       eday=eng(:,2);
       eyr=eng(:,3);
       ehr=eng(:,4);
       emin=eng(:,5);
       esec=eng(:,6);
	       engtime=datenum(eyr,emo,eday,ehr,emin,esec);
%
       psdate=datestr(engtime(1),2);
       pedate=datestr(engtime(end),2);      
       pstart=datestr(engtime(1),13);
       pstop=datestr(engtime(end),13);


%  CTD FILE

  eval(['load ',direc,slsh,cfile,'.TXT']);
  ctd=eval(cfile);
  disp(['Processing CTD file:  ',cfile]);


       csnum=[1:1:length(ctd)];
       ccond=ctd(:,1);
       ctemp=ctd(:,2); 
       cpres=ctd(:,3);


%  ACM FILE

   eval(['load ',direc,slsh,afile,'.TXT']);
   acm=eval(afile);
   disp(['Processing ACM file:  ',afile]);

        asnum=[1:1:length(eng)];
        aTx=acm(:,1); 
        aTy=acm(:,2);
        aHx=acm(:,3);
	aHy=acm(:,4);
        aHz=acm(:,5); 
        Vab=acm(:,6); 
        Vcd=acm(:,7); 
        Vef=acm(:,8); 
        Vgh=acm(:,9);



clear A* C* E*

eval(['save ',outdir,slsh,'raw',sprintf('%4.4i',stas(mm)),'.mat psdate pedate pstart pstop esnum epres ecurr evolt edpdt engtime csnum ccond ctemp cpres asnum Vab Vcd Vef Vgh aTx aTy aHx aHy aHz'])

clear eng ctd acm esnum epres ecurr evolt edpdt csnum ccond ctemp 
clear cpres asnum Vab Vcd Vef Vgh aTy aHx aHy aHz engtime


end	%looping on sets

