disp('read_son_day')
fclose('all');
clear st1;clear st2;clear st3;clear jadson;
jd=input('julian day, ddd   ','s');
plotit=input('plot plots? 0=n, 1=y   ');
prtit=input('append master data file? 0=n, 1=y   ');
hr='00';
jhold=0; 
jax=1;
jaxx=1;
backchk=0;
st1=zeros(6,1);
st2=zeros(6,75000);
for jam=0:23,						%cycle thru 24 hourly stats files
   if jam<10,
      hr=['0'num2str(jam)];
   else
      hr=num2str(jam);
   end;
   e=['D:\hp_data\day',jd,'\P0_' jd hr];
	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
		
      while feof(flist)==0,
         st1=fscanf(flist,'%g%*c%g%*c%g%*c%g%*c%g%*c%g%*c',[6,inf]);
	      if backchk==0,
   	      tzilch=jam;
      	   backchk=1;
         	tzero=st1(1);
      	end;
       
      	[nr1,nl1]=size(st1);      
			if nr1==6
            st2(:,jax:jax+nl1-1)=st1; 
      		jax=jax+nl1;
			else
   			x=fgets(flist);
      	end;
   	end;

		jam   
		jk=ceil(jax/1250);
		jade=str2num(num2str(jd))+(tzilch+(st2(1,:)-tzero)/60/60)/24;

		for j=1:jk
   		jl=1250*(j-1)+1;jj=min(jax-1,1250*j);
   		st3(:,j+jhold)=median(st2(:,jl:jj)')';
   		jadson(j+jhold)=jade(jl);
		end;
		jhold=jhold+jk;
   	clear st2;clear st1;
   	jax=1;
	end;   
end;

u=st3(3,:)/100;
v=st3(4,:)/100;
w=st3(5,:)/100;
speedson=st3(6,:)/50;
Tsonic=(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);
np=jhold;

if prtit==1;
  prt_jas_son;
end;

if plotit==1;
%  set some plot limits
Tll=-25;
hll=70;
uul=10;
vul=10;
Tul=-0;
hul=100;
sul=12;
Rlul=30;
Rlll=-100;
Rtul=300;
Rtll=125;
Rsul=1000;
Rsll=0;
%plot T profiles
j_st=jadson(1);
j_en=jadson(np);
figure;plot(jadson(1:np),Tsonic(1:np));xlabel('Jday (1999)');ylabel('Temperature (C)');Title('Sonic Temperature'); 
figure;plot(jadson(1:np),U(1:np),jadson(1:np),V(1:np),jadson(1:np),w(1:np),jadson(1:np),S(1:np));xlabel('Jday (1999)');ylabel('Sonic Winds (m/s)');Title('Relative Wind Components (u,v,w) and Speed'); 
figure;plot(jadson(1:np),dir(1:np));xlabel('Jday (1999)');ylabel('Sonic Wind Direction (deg)');Title('Relative Wind Direction');axis([j_st j_en 0 360]);
figure;plot(jadson(1:np),sqrt(U(1:np).^2+V(1:np).^2+w(1:np)));xlabel('Jday (1999)');ylabel('Wind Speed m/s');Title('Sonic Relative Wind Speed');%axis([j_st j_en 0 360]);
end; 
