disp('opensons.m')
% Inputs data from ASCII data files
%  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];

%  Open Sonic file		
dfl=[drive,'\DAY',dfx1,'\P0','_',dfx];%disp(dfl)
%dfl=['e:\DAY',dfx1,'\P6','_',dfx];%disp(dfl)   %ASGAMAGE Sonic channel is 6

flist=fopen(dfl,'r');
headr=fgetl(flist);
headr=fgetl(flist);
%disp('Open Sonic File')
%z=fscanf(flist,'%g,%g,%g%g%g%g%g',[7,inf]);    %AFTER day 304 ASGAMAGE
%z=fscanf(flist,'%g,%g,%g%g%g%g',[6,inf]);   ASGAMAGE

z=fscanf(flist,'%g,%g,%g,%g,%g,%g',[6,inf]);      %    FASTEX
z=z(:,1:length(z)-1);
t1=z(1,:)-z(1,1);t2=z(2,:)/10000-z(2,1)/10000;
tsons=t1+t2;clear t1 t2;
tsons=tsons-tsons(1);tsons=0:1/20.8325:(length(tsons))/20.8325;tsons=tsons';

u=z(3,:)/100;
v=z(4,:)/100;
w=z(5,:)/100;
Tsonic=z(6,:)/50;

clear z

if abs(u(1))>35,
   u(1)=mean(u);
end;
if abs(v(1))>35,
   v(1)=mean(v);
end;
if abs(w(1))>35,
   w(1)=mean(w);
end;

for ii=2:length(u);
   if abs(u(ii))>35,
      u(ii)=u(ii-1);
   end;
   if abs(v(ii))>35,
      v(ii)=v(ii-1);
   end;
   if abs(w(ii))>35,
      w(ii)=w(ii-1);
   end;
end;

%Tsonic=(Tsonic.*Tsonic+(u.*u + w.*w)/2. + v.*v)/403.-273.15;
Tsonic=(Tsonic.*Tsonic+((u + w).*(u + w))/2. + v.*v)/403.-273.15;

sx=-u*cos(30/180*pi)-v*sin(30/180*pi);     %+ from stern to bow (from South to North)
sy=-u*sin(30/180*pi)+v*cos(30/180*pi);     %+ from starboard to port (from East to West)

%Reldir_1=atan2(mean(sx(1:firstq)),mean(sy(1:firstq)));
%Reldir_2=atan2(mean(sx(firstq+1:seconq)),mean(sy(firstq+1:seconq)));
%Reldir_3=atan2(mean(sx(seconq+1:thirdq)),mean(sy(seconq+1:thirdq)));
%Reldir_4=atan2(mean(sx(thirdq+1:fourtq)),mean(sy(thirdq+1:fourtq)));
%Reldir_5=atan2(mean(sx(fourtq+1:fifthq)),mean(sy(fourtq+1:fifthq)));
%Reldir_6=atan2(mean(sx(fifthq+1:sixthq)),mean(sy(fifthq+1:sixthq)));

Un=mean(sx).*cos(Mhead)-mean(-sy).*sin(Mhead) + Msog.*cos(Mcog);   % + From South to North
Ue=mean(sx).*sin(Mhead)+mean(-sy).*cos(Mhead) + Msog.*sin(Mcog);   % + From West to East

%Un_1=mean(sx(1:firstq)).*cos(Mhead_1)-mean(-sy(1:firstq)).*sin(Mhead_1)-Msog_1.*cos(Mcog_1);   %to north
%Un_2=mean(sx(firstq+1:seconq)).*cos(Mhead_2)-mean(-sy(firstq+1:seconq)).*sin(Mhead_2)-Msog_2.*cos(Mcog_2);
%Un_3=mean(sx(seconq+1:thirdq)).*cos(Mhead_3)-mean(-sy(seconq+1:thirdq)).*sin(Mhead_3)-Msog_3.*cos(Mcog_3);
%Un_4=mean(sx(thirdq+1:fourtq)).*cos(Mhead_4)-mean(-sy(thirdq+1:fourtq)).*sin(Mhead_4)-Msog_4.*cos(Mcog_4);
%Un_5=mean(sx(fourtq+1:fifthq)).*cos(Mhead_5)-mean(-sy(fourtq+1:fifthq)).*sin(Mhead_5)-Msog_5.*cos(Mcog_5);
%Un_6=mean(sx(fifthq+1:sixthq)).*cos(Mhead_6)-mean(-sy(fifthq+1:sixthq)).*sin(Mhead_6)-Msog_6.*cos(Mcog_6);
%Ue_1=mean(sx(1:firstq)).*sin(Mhead_1)+mean(-sy(1:firstq)).*cos(Mhead_1)-Msog_1.*sin(Mcog_1);   %to east
%Ue_2=mean(sx(firstq+1:seconq)).*sin(Mhead_2)+mean(-sy(firstq+1:seconq)).*cos(Mhead_2)-Msog_2.*sin(Mcog_2);
%Ue_3=mean(sx(seconq+1:thirdq)).*sin(Mhead_3)+mean(-sy(seconq+1:thirdq)).*cos(Mhead_3)-Msog_3.*sin(Mcog_3);
%Ue_4=mean(sx(thirdq+1:fourtq)).*sin(Mhead_4)+mean(-sy(thirdq+1:fourtq)).*cos(Mhead_4)-Msog_4.*sin(Mcog_4);
%Ue_5=mean(sx(fourtq+1:fifthq)).*sin(Mhead_5)+mean(-sy(fourtq+1:fifthq)).*cos(Mhead_5)-Msog_5.*sin(Mcog_5);
%Ue_6=mean(sx(fifthq+1:sixthq)).*sin(Mhead_6)+mean(-sy(fifthq+1:sixthq)).*cos(Mhead_6)-Msog_6.*sin(Mcog_6);

%Twindir_1=atan2(-Ue_1,-Un_1).*180/pi;if Twindir_1<0,Twindir_1=Twindir_1+360;end;
%Twindir_2=atan2(-Ue_2,-Un_2).*180/pi;if Twindir_2<0,Twindir_2=Twindir_2+360;end;
%Twindir_3=atan2(-Ue_3,-Un_3).*180/pi;if Twindir_3<0,Twindir_3=Twindir_3+360;end;
%Twindir_4=atan2(-Ue_4,-Un_4).*180/pi;if Twindir_4<0,Twindir_4=Twindir_4+360;end;
%Twindir_5=atan2(-Ue_5,-Un_5).*180/pi;if Twindir_5<0,Twindir_5=Twindir_5+360;end;
%Twindir_6=atan2(-Ue_6,-Un_6).*180/pi;if Twindir_6<0,Twindir_6=Twindir_6+360;end;

%disp([num2str(ddd),'   'num2str(hhh),'   U = ',num2str(sqrt( (mean(sx).^2)+(mean(sy).^2)+(mean(w).^2) ))])

%%%%%%%%%%%%%%%%%%%  ASGAMAGE  %%%%%%%%%%%%%%%%%%%%%%%%%%%

%ssx=(1:length(H2O)).*0;ssx(1)=sx(1);
%ssy=(1:length(H2O)).*0;ssy(1)=sy(1);
%ssz=(1:length(H2O)).*0;ssz(1)=w(1);
%sst=(1:length(H2O)).*0;sst(1)=Tsonic(1);
%jj=2;

%for ii=2:length(H2O);
%   while tsons(jj) < tcamp(ii) & jj < length(sx);
%      jj=jj+1;
%   end;
%   ssx(ii)=(sx(jj)-sx(jj-1))/(tsons(jj)-tsons(jj-1))*(tcamp(ii)-tsons(jj-1))+sx(jj-1);
%   ssy(ii)=(sy(jj)-sy(jj-1))/(tsons(jj)-tsons(jj-1))*(tcamp(ii)-tsons(jj-1))+sy(jj-1);
%   ssz(ii)=(w(jj)-w(jj-1))/(tsons(jj)-tsons(jj-1))*(tcamp(ii)-tsons(jj-1))+w(jj-1);
%   sst(ii)=(Tsonic(jj)-Tsonic(jj-1))/(tsons(jj)-tsons(jj-1))*(tcamp(ii)-tsons(jj-1))+Tsonic(jj-1);
%end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

su=sx;sv=sy;sw=w;clear u v w sx sy

clear specsize tb pp mm ii ans Wstream Ts Vstream Ustream headr dfx dfx1 dfx2
fclose(flist);