disp('openmoti.m')
% Inputs campbell data accelerations and angular rates 
% Computes one-minute averages of these variables and saves these to a file.
%  Note:  hhh=hour

if hhh<10,
   dfx2=['0',num2str(hhh)];
else,
   dfx2=num2str(hhh);
end;

if ddd<10,
   dfx1=['00',num2str(ddd)];
elseif ddd<100,
   dfx1=['0',num2str(ddd)];
else,
   dfx1=num2str(ddd);
end;

dfx=[dfx1 dfx2];

%disp('Open Motion-Pak Campbell File')
%dfl=['/cdrom/DAY',dfx1,'/P5_',dfx,';1'];
dfl=[drive,'\DAY',dfx1,'\P5_',dfx];

flist=fopen(dfl,'r');%disp(dfl)
headr=fgetl(flist);%disp(headr)
headr=fgetl(flist);
z=fscanf(flist,'%g,%g,%g,%g,%g,%g,%g,%g',[8,inf]);

tm1=z(1,:);tm2=z(2,:);tm2=tm2./10000;tmotion=tm1+tm2;clear tm1;clear tm2;
tmotion=tmotion-tmotion(1);tmotion=0:1/20:(length(tmotion)-1)/20;tmotion=tmotion';

%  As mounted:  motion-pak
%  Xm is toward port
%  Ym is toward bow
%  Zm is down

%  We desire the coordinate system such that
%  +X is toward bow
%  +Y is toward port
%  +Z is up
%  +rotation about the x-axis is port up (phi = roll) 
%  +rotation about the y-axis is bow down (theta = pitch)
%  +rotation about the z-axis is bow to port

accx=+z(7,:)./3.747.*9.81;              %forward is positive 
accy=+z(6,:)./3.749.*9.81;              %to port is positive
accz=-z(8,:)./3.754.*9.81;              %up is positive
%ratex=+z(4,:)/0.049888/180*pi;          %port up is positive phi
%ratey=+z(3,:)/0.050343/180*pi;          %bow down is positive theta
%ratez=+z(5,:)/0.050084/180*pi;          %bow to starboard is positive psi
ratex=+z(4,:)/0.024993/180*pi;          %port up is positive phi
ratey=+z(3,:)/0.025016/180*pi;          %bow down is positive theta
%ratez=+z(5,:)/0.024985/180*pi;          %bow to starboard is positive psi (left-handed)
ratez=-z(5,:)/0.024985/180*pi;          %bow to port is positive psi (right-handed)

accx=accx.*1.017;
accy=accy.*1.017;
accz=accz.*1.017;

G=sqrt(mean(accx)^2+mean(accy)^2+mean(accz)^2);

accx=accx';accy=accy';ratex=ratex';ratey=ratey';ratez=ratez';tmotion=tmotion';

ycheck=find(accz>50);

for mm=length(accz):-1:1;
   if accz(mm)>50,
      accz(mm)=0;
   else;
      break;
   end;
end;

for mm=1:1:length(accz);
   if accz(mm)>50,
      accz(mm)=0;
   else;
      break;
   end;
end;

ycheck=find(accz>50);

if ~isempty(ycheck),
   xcheck=ycheck(1:length(ycheck)-1).*0;
   for iii=2:length(ycheck);
      xcheck(iii-1)=ycheck(iii)-ycheck(iii-1);
   end;
   for iii=1:length(xcheck);
      if xcheck(iii)==2,
         ycheck=[ycheck(1:iii) ycheck(iii)+1 ycheck(iii+1:length(ycheck))];
         xcheck=[xcheck(1:iii-1) 1 1 xcheck(iii+1:length(xcheck))];
      end;
   end;
      
   wcheck=find(xcheck>1);
   wcheck=[0 wcheck];
   if wcheck==0,
      wcheck=[0 length(ycheck)];
   end;

   par=zeros(1,length(wcheck));
   
   if length(wcheck)>2,
      if ycheck(1) > 20 & ycheck(wcheck(2)+1) - ycheck(wcheck(2)) > 20,
         par(1)=20;
      else;
         par(1)=min(ycheck(1),ycheck(wcheck(2)+1)-ycheck(wcheck(2)))-1;
         if par(1)==0 | par(1)==1,
            par(1)=2;
         end;
      end;
     % disp([num2str(1),'  ',num2str(par(1))])
   
      for ii=2:length(wcheck)-1,
         if ycheck(wcheck(ii)+1)-ycheck(wcheck(ii)) > 20 & ycheck(wcheck(ii+1)+1) - ycheck(wcheck(ii+1)) > 20,
            par(ii)=20;
         else;
            par(ii)=min(ycheck(wcheck(ii)+1)-ycheck(wcheck(ii)),ycheck(wcheck(ii+1)+1)-ycheck(wcheck(ii+1)))-1;
            if par(ii)==0 | par(ii)==1,
               par(ii)=2;
            end;
         end;
      %disp([num2str(ii),'  ',num2str(par(ii))])
      end;

      if ycheck(wcheck(length(wcheck))+1) - ycheck(wcheck(length(wcheck))) > 20 & length(accz) - ycheck(length(ycheck)) > 20,
         par(length(wcheck))=20;
      else;
         par(length(wcheck))=min(ycheck(wcheck(length(wcheck))+1) - ycheck(wcheck(length(wcheck))),length(accz) - ycheck(length(ycheck)))-1;
         if par(length(par))==0 | par(length(par))==1,
            par(length(par))=2;
         end;
      %disp([num2str(length(par)),'  ',num2str(par(length(par)))])
      end;
      
   elseif length(wcheck)==2,
      if length(ycheck)==wcheck(2),
         if ycheck(1) > 20 & length(accz) - ycheck(length(ycheck)) > 20,
            par(1)=20;
         else;
            par(1)=min(ycheck(1),length(accz) - ycheck(length(ycheck)))-1;
            if par(1)==0 | par(1)==1,
               par(1)=2;
            end;
         end;
      else,
         if ycheck(1) > 20 & ycheck(wcheck(2)+1) - ycheck(wcheck(2)) > 20,
            par(1)=20;
         else;
            par(1)=min(ycheck(1),ycheck(wcheck(2)+1)-ycheck(wcheck(2)))-1;
            if par(1)==0 | par(1)==1,
               par(1)=2;
            end;
         end;
         if ycheck(wcheck(2)+1) - ycheck(wcheck(2)) > 20 & length(accz)-ycheck(length(ycheck)) > 20,
            par(2)=20;
         else;
            par(2)=min(ycheck(wcheck(2)+1)-ycheck(wcheck(2)),length(accz)-ycheck(length(ycheck)))-1;
            if par(2)==0 | par(2)==1,
               par(2)=2;
            end;
         end;
      end;
   end;
   
   for ii=1:length(wcheck)-1,
      vcheck=[accz(ycheck(wcheck(ii)+1)-par(ii):ycheck(wcheck(ii)+1)-1) accz(ycheck(wcheck(ii+1))+1:ycheck(wcheck(ii+1))+par(ii))];
      ucheck=[tmotion(ycheck(wcheck(ii)+1)-par(ii):ycheck(wcheck(ii)+1)-1) tmotion(ycheck(wcheck(ii+1))+1:ycheck(wcheck(ii+1))+par(ii))];
      tcheck=[tmotion(ycheck(wcheck(ii)+1)-par(ii):ycheck(wcheck(ii+1))+par(ii))];
      scheck=spline(ucheck,vcheck,tcheck);
      accz(ycheck(wcheck(ii)+1)-par(ii):ycheck(wcheck(ii+1))+par(ii))=scheck;
      %figure(ii);plot(tmotion(ycheck(wcheck(ii)+1)-par(ii):ycheck(wcheck(ii+1))+par(ii)),accz(ycheck(wcheck(ii)+1)-par(ii):ycheck(wcheck(ii+1))+par(ii)),'o',ucheck,vcheck,tcheck,scheck,'.');
   end;
   
   if length(wcheck)>2,
      vcheck=[accz(ycheck(wcheck(length(wcheck))+1)-par(length(par)):ycheck(wcheck(length(wcheck))+1)-1) accz(ycheck(length(ycheck))+1:ycheck(length(ycheck))+par(length(par)))];
      ucheck=[tmotion(ycheck(wcheck(length(wcheck))+1)-par(length(par)):ycheck(wcheck(length(wcheck))+1)-1) tmotion(ycheck(length(ycheck))+1:ycheck(length(ycheck))+par(length(par)))];
      tcheck=[tmotion(ycheck(wcheck(length(wcheck))+1)-par(length(par)):ycheck(length(ycheck))+par(length(par)))];
      scheck=spline(ucheck,vcheck,tcheck);
      accz(ycheck(wcheck(length(wcheck))+1)-par(length(par)):ycheck(length(ycheck))+par(length(par)))=scheck;
      %figure(length(wcheck));plot(tmotion(ycheck(wcheck(length(wcheck))+1)-par(length(par)):ycheck(length(ycheck))+par(length(par))),accz(ycheck(wcheck(length(wcheck))+1)-par(length(par)):ycheck(length(ycheck))+par(length(par))),'o',ucheck,vcheck,tcheck,scheck,'.');
   end;
   if length(wcheck)==2 & length(ycheck)~=wcheck(2),
      vcheck=[accz(ycheck(wcheck(2)+1)-par(2):ycheck(wcheck(2)+1)-1) accz(ycheck(length(ycheck))+1:ycheck(length(ycheck))+par(2))];
      ucheck=[tmotion(ycheck(wcheck(2)+1)-par(2):ycheck(wcheck(2)+1)-1) tmotion(ycheck(length(ycheck))+1:ycheck(length(ycheck))+par(2))];
      tcheck=[tmotion(ycheck(wcheck(2)+1)-par(2):ycheck(length(ycheck))+par(2))];
      scheck=spline(ucheck,vcheck,tcheck);
      accz(ycheck(wcheck(2)+1)-par(2):ycheck(length(ycheck))+par(2))=scheck;
      %figure(2);plot(tmotion(ycheck(wcheck(2)+1)-par(2):ycheck(length(ycheck))+par(2)),accz(ycheck(wcheck(2)+1)-par(2):ycheck(length(ycheck))+par(2)),'o',ucheck,vcheck,tcheck,scheck,'.');
   end;
end;
clear vcheck ucheck tcheck scheck xcheck ycheck wcheck z

accz=accz';

clear headr dfx2 dfx1 dfx dfl G Filtn Cax Cax1 Cay Cay1 Caz Caz1 Crx Crx1 Cry Cry1 Crz Crz1 FF ans ii

fclose(flist);