disp('nau_son')
fclose('all');
clear st1;clear st2;clear st3;clear jadson;
jhold=0; 
st3=zeros(6,60);

if hhh<10,
   e=['D:\hp_data\day',num2str(ddd),'\P0_',num2str(ddd),'0',num2str(hhh)];
else
   e=['D:\hp_data\day',num2str(ddd),'\P0_',num2str(ddd),num2str(hhh)];
end;
flist=fopen(e,'r');
if flist>0,	   				%if the file exists, 
   dmp=fgets(flist);			%read header lines and skip blanks
   dmp2=fgets(flist);		%read header lines and skip blanks
   clear dmp dmp2
	while feof(flist)==0,
      st1=fscanf(flist,'%g%*c%g%*c%g%*c%g%*c%g%*c%g%*c',[6,inf]);
      [nr1,nl1]=size(st1);      
   end;
	jk=ceil(length(st1)/1250);
   jade=str2num(num2str(ddd))+((st1(1,:)-st1(1,1))/60/60)/24;
	for j=1:jk
   	jl=1250*(j-1)+1;jj=min(length(st1)-1,1250*j);
   	st3(:,j+jhold)=median(st1(:,jl:jj)')';
   	jadson(j+jhold)=jade(jl);
	end;
	jhold=jhold+jk;
   clear st2 j nr1 nl1 jj jk jl
 
   uu=st1(3,:)/100;
   vv=st1(4,:)/100;
   ww=st1(5,:)/100;
   tt=st1(6,:)/50;
   u=st3(3,:)/100;
   v=st3(4,:)/100;
   w=st3(5,:)/100;
   speedson=st3(6,:)/50;
   Tsonic=(tt.*tt+(uu.*uu + ww.*ww)/2. + vv.*vv)/403-273.15;
   UU=-uu*cos(30/180*pi)-vv*sin(30/180*pi);		     	%to North
   VV=-uu*sin(30/180*pi)+vv*cos(30/180*pi);  		   %to West
   Tslow=(speedson.*speedson+(u.*u + w.*w)/2. + v.*v)/403-273.15;
   U=-u*cos(30/180*pi)-v*sin(30/180*pi);		     	%to North
   V=-u*sin(30/180*pi)+v*cos(30/180*pi);  		   %to West
   S=sqrt(u.*u+v.*v+w.*w);
   dir=atan2(-V,U)*180/pi+180;							%jeff coordinates, +boward, +portward, 180 means wind from
   dir=mod(dir+360,360);

   figure;plot(jadson,Tslow);xlabel('Jday (1999)');ylabel('Temperature (C)');Title('Sonic Temperature'); 
   figure;plot(jadson,U,jadson,V,jadson,w,jadson,S);xlabel('Jday (1999)');ylabel('Sonic Winds (m/s)');Title('Relative Wind Components (u,v,w) and Speed'); 
   figure;plot(jadson,dir);xlabel('Jday (1999)');ylabel('Sonic Wind Direction (deg)');Title('Relative Wind Direction');
   figure;plot(jadson,sqrt(U.^2+V.^2+w));xlabel('Jday (1999)');ylabel('Wind Speed m/s');Title('Sonic Relative Wind Speed');
   
   [Su,F]=psd2(detrend(UU),length(UU),20);
   [Sv,F]=psd2(detrend(VV),length(VV),20);
   [Sw,F]=psd2(detrend(ww),length(ww),20);
   [St,F]=psd2(detrend(Tsonic),length(Tsonic),20);
   [Sus,Fs]=specsmoo(Su,20);
   [Svs,Fs]=specsmoo(Sv,20);
   [Sws,Fs]=specsmoo(Sw,20);
   [Sts,Fs]=specsmoo(St,20);
   
   figure;loglog(Fs,Fs.*Sus,Fs,Fs.*Svs,Fs,Fs.*Sws,Fs,Fs.*Sts,Fs(35:60),(Fs(35:60).^(-2/3))./(Fs(50).^(-2/3)).*Sws(50).*Fs(50))
   title(['Sonic Spectra   day',num2str(ddd),'   hour ',num2str(hhh)]);
   xlabel('Frequency in Hz');
   ylabel('Spec*Freq');
   fclose('all');
   clear ans e flist jhold speedson st3 u v st1
   clear Su Sv Sw St F
end;   
