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

fclose('all'); 
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];

Filtn=0;
%disp('Open Motion-Pak Campbell File')
%dfl=['/home/user1/bin/data/P5_',dfx];
%dfl=['/scratch2/day',dfx1,'/P5_',dfx];
%dfl=['/home/user1/bin/data_backup/day',dfx1,'/P5_',dfx];
dfl=['/home/jeh/fastex/P5_',dfx,';1'];
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';

%  X is toward bow
%  Y is toward port
%  Z is up

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
G=sqrt(mean(accx)^2+mean(accy)^2+mean(accz)^2);
accx=accx';accy=accy';accz=accz';ratex=ratex';ratey=ratey';ratez=ratez';
if accz(1) > 100, accz(1)=0; end;
for ii=2:length(accz);
   if accz(ii) > 100, 
      accz(ii) = accz(ii-1);
   end;
end;
clear z headr;

%figure;plot(tmotion(1000:2000),accx(1000:2000),tmotion(1000:2000),accy(1000:2000));title(['JD',dfx1,' Hr',dfx2,' UTC  Horizontal Accelerations']);ylabel('acceleration in m/s/s');xlabel('time in seconds')
%figure;plot(tmotion(1000:2000),accz(1000:2000));title(['JD',dfx1,' Hr',dfx2,' UTC  Acceleration Vertical']);ylabel('acceleration in m/s/s');xlabel('time in seconds')
%figure;plot(tmotion(1000:2000),ratex(1000:2000),tmotion(1000:2000),ratey(1000:2000),tmotion(1000:2000),ratez(1000:2000));title(['JD',dfx1,' Hr',dfx2,' UTC  Rotation Rates']);ylabel('rate of rotation in deg/s');xlabel('time in seconds')

%Cax=psd(accx(1:12000),8192);Cay=psd(accy(1:12000),8192);Caz=psd(accz(1:12000),8192);Crx=psd(ratex(1:12000),8192);Cry=psd(ratey(1:12000),8192);Crz=psd(ratez(1:12000),8192);
%Chh=psd(heading,2048);[Chh1,Fh]=specsmooth(Chh,1);
%[Cax1,FF]=specsmooth(Cax,20);[Cay1,FF]=specsmooth(Cay,20);[Caz1,FF]=specsmooth(Caz,20);
%[Crx1,FF]=specsmooth(Crx,20);[Cry1,FF]=specsmooth(Cry,20);[Crz1,FF]=specsmooth(Crz,20);
%figure;loglog(FF,Cax1./FF./FF,FF,Cay1./FF./FF,FF,Caz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Accelerations #1']);xlabel('Freq in Hz');ylabel('S(acc)')
%figure;loglog(FF,Crx1./FF./FF,FF,Cry1./FF./FF,FF,Crz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Rotation Rates #1']);xlabel('Freq in Hz');ylabel('S(rate)')

%Cax=psd(accx(12001:24000),8192);Cay=psd(accy(12001:24000),8192);Caz=psd(accz(12001:24000),8192);Crx=psd(ratex(12001:24000),8192);Cry=psd(ratey(12001:24000),8192);Crz=psd(ratez(12001:24000),8192);
%[Cax1,FF]=specsmooth(Cax,20);[Cay1,FF]=specsmooth(Cay,20);[Caz1,FF]=specsmooth(Caz,20);
%[Crx1,FF]=specsmooth(Crx,20);[Cry1,FF]=specsmooth(Cry,20);[Crz1,FF]=specsmooth(Crz,20);
%figure;loglog(FF,Cax1./FF./FF,FF,Cay1./FF./FF,FF,Caz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Accelerations #2']);xlabel('Freq in Hz');ylabel('S(acc)')
%figure;loglog(FF,Crx1./FF./FF,FF,Cry1./FF./FF,FF,Crz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Rotation Rates #2']);xlabel('Freq in Hz');ylabel('S(rate)')

%Cax=psd(accx(24001:36000),8192);Cay=psd(accy(24001:36000),8192);Caz=psd(accz(24001:36000),8192);Crx=psd(ratex(24001:36000),8192);Cry=psd(ratey(24001:36000),8192);Crz=psd(ratez(24001:36000),8192);
%[Cax1,FF]=specsmooth(Cax,20);[Cay1,FF]=specsmooth(Cay,20);[Caz1,FF]=specsmooth(Caz,20);
%[Crx1,FF]=specsmooth(Crx,20);[Cry1,FF]=specsmooth(Cry,20);[Crz1,FF]=specsmooth(Crz,20);
%figure;loglog(FF,Cax1./FF./FF,FF,Cay1./FF./FF,FF,Caz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Accelerations #3']);xlabel('Freq in Hz');ylabel('S(acc)')
%figure;loglog(FF,Crx1./FF./FF,FF,Cry1./FF./FF,FF,Crz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Rotation Rates #3']);xlabel('Freq in Hz');ylabel('S(rate)')

%Cax=psd(accx(36001:48000),8192);Cay=psd(accy(36001:48000),8192);Caz=psd(accz(36001:48000),8192);Crx=psd(ratex(36001:48000),8192);Cry=psd(ratey(36001:48000),8192);Crz=psd(ratez(36001:48000),8192);
%[Cax1,FF]=specsmooth(Cax,20);[Cay1,FF]=specsmooth(Cay,20);[Caz1,FF]=specsmooth(Caz,20);
%[Crx1,FF]=specsmooth(Crx,20);[Cry1,FF]=specsmooth(Cry,20);[Crz1,FF]=specsmooth(Crz,20);
%figure;loglog(FF,Cax1./FF./FF,FF,Cay1./FF./FF,FF,Caz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Accelerations #4']);xlabel('Freq in Hz');ylabel('S(acc)')
%figure;loglog(FF,Crx1./FF./FF,FF,Cry1./FF./FF,FF,Crz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Rotation Rates #4']);xlabel('Freq in Hz');ylabel('S(rate)')

%Cax=psd(accx(48001:60000),8192);Cay=psd(accy(48001:60000),8192);Caz=psd(accz(48001:60000),8192);Crx=psd(ratex(48001:60000),8192);Cry=psd(ratey(48001:60000),8192);Crz=psd(ratez(48001:60000),8192);
%[Cax1,FF]=specsmooth(Cax,20);[Cay1,FF]=specsmooth(Cay,20);[Caz1,FF]=specsmooth(Caz,20);
%[Crx1,FF]=specsmooth(Crx,20);[Cry1,FF]=specsmooth(Cry,20);[Crz1,FF]=specsmooth(Crz,20);
%figure;loglog(FF,Cax1./FF./FF,FF,Cay1./FF./FF,FF,Caz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Accelerations #5']);xlabel('Freq in Hz');ylabel('S(acc)')
%figure;loglog(FF,Crx1./FF./FF,FF,Cry1./FF./FF,FF,Crz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Rotation Rates #5']);xlabel('Freq in Hz');ylabel('S(rate)')

%Cax=psd(accx(60000:length(accx)),8192);Cay=psd(accy(60000:length(accx)),8192);Caz=psd(accz(60000:length(accx)),8192);Crx=psd(ratex(60000:length(accx)),8192);Cry=psd(ratey(60000:length(accx)),8192);Crz=psd(ratez(60000:length(accx)),8192);
%[Cax1,FF]=specsmooth(Cax,20);[Cay1,FF]=specsmooth(Cay,20);[Caz1,FF]=specsmooth(Caz,20);
%[Crx1,FF]=specsmooth(Crx,20);[Cry1,FF]=specsmooth(Cry,20);[Crz1,FF]=specsmooth(Crz,20);
%figure;loglog(FF,Cax1./FF./FF,FF,Cay1./FF./FF,FF,Caz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Accelerations #6']);xlabel('Freq in Hz');ylabel('S(acc)')
%figure;loglog(FF,Crx1./FF./FF,FF,Cry1./FF./FF,FF,Crz1./FF./FF,Fh,Chh1);title(['JD',dfx1,' Hr',dfx2,' UTC  Spectra of Rotation Rates #6']);xlabel('Freq in Hz');ylabel('S(rate)')

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