%MMP_raw_summary.m
%This program takes the raw mmp data files in matlab format 
%  from a user specified directory and extracts summary information
%
% Note that input files are assumed to be called rawNNNN.mat where NNNN
% is the profile number, and the file structure assumes UNIX protocols.

%
%  copyright John Toole  Woods Hole Oceanographic Institution
%  February, 2002

stas=input('Which stations do you want to process? eg. [8:24]  ');
[directory]=input('What directory are those files in?  ','s');

disp(['The next two entries define a pressure interval for estimating the profiling speed ']);
minp=input('Enter the min pressure for the interval ');
maxp=input('Enter the max pressure for the interval ');

[outfilename]=input('Enter the output file name you want: ','s');

for h=1:length(stas)

disp(['attempting to load ',directory,'/raw',sprintf('%4.4i',stas(h)),'.mat']);

file=[directory,'/raw',sprintf('%4.4i',stas(h)),'.mat'];

if exist(file)==2;

eval(['load ',directory,'/raw',sprintf('%4.4i',stas(h)),'.mat']);

files(h)=stas(h);

j=find(psdate=='/');
psdate(j)=' ';
date=str2num(psdate);

month=date(1);
day=date(2);
year=date(3)+2000;
if(date(3)==0);
year=2000;
end
if(date(3)==9);
year=1999;
end


j=find(pstart==':');
pstart(j)=' ';
start=str2num(pstart);

hour=start(1);
minute=start(2);
second=start(3);

starttime(h)=datenum(year,month,day,hour,minute,second);

j=find(pedate=='/');
pedate(j)=' ';
date=str2num(pedate);

month=date(1);
day=date(2);
year=date(3)+2000;
if(date(3)==0);
year=2000;
end
if(date(3)==9);
year=1999;
end

j=find(pstop==':');
pstop(j)=' ';
stop=str2num(pstop);

hour=stop(1);
minute=stop(2);
second=stop(3);

endtime(h)=datenum(year,month,day,hour,minute,second);

neng(h)=length(epres);

if(exist('Vab')==1);
nacm(h)=length(Vab);
else
nacm(h)=0;
end

if(exist('cpres')==1);
nctd(h)=length(cpres);

j=find(cpres>10 & cpres<5000);
cpmin(h)=min(cpres(j));
cpmax(h)=max(cpres(j));
else
nctd(h)=0;
cpmin(h)=nan;
cpmax(h)=nan;
end

j=find(epres>10 & epres<5000);
epmin(h)=min(epres(j));
epmax(h)=max(epres(j));


j=find(epres>=minp & epres<=maxp);
jlen=length(j);
if(jlen>2);
espeed(h)=(epres(j(1))-epres(j(jlen)))/((engtime(j(1))-engtime(j(jlen)))*86400.);
avecur(h)=mean(ecurr(j));
avevolt(h)=mean(evolt(j));
%aveV2(h)=mean(eV2(j));

else
espeed(h)=nan;
avecur(h)=nan;
avevolt(h)=nan;
%aveV2(h)=nan;
end

clear cpres;
clear Vab;

else   %if file doesn't exist
files(h)=nan; 
starttime(h)=nan;  
endtime(h)=nan;  
neng(h)=nan;  
nctd(h)=nan;  
nacm(h)=nan;  
cpmin(h)=nan;  
cpmax(h)=nan;  
epmin(h)=nan;  
epmax(h)=nan;  
avecur(h)=nan; 
espeed(h)=nan;
avevolt(h)=nan;
%aveV2(h)=nan;

end   %if file exists

end	%end looping on files

eval(['save ',outfilename,' files starttime endtime neng nctd nacm cpmin cpmax epmin epmax espeed avevolt avecur']);


