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);

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 + w).*(u + w))/2. + v.*v)/403.-273.15;

firstt=fix(length(Tsonic)/6*1);
secont=fix(length(Tsonic)/6*2);
thirdt=fix(length(Tsonic)/6*3);
fourtt=fix(length(Tsonic)/6*4);
fiftht=fix(length(Tsonic)/6*5);
sixtht=fix(length(Tsonic)/6*6);

Tsonic_1=mean(Tsonic(1:firstt));
Tsonic_2=mean(Tsonic(firstt+1:secont));
Tsonic_3=mean(Tsonic(secont+1:thirdt));
Tsonic_4=mean(Tsonic(thirdt+1:fourtt));
Tsonic_5=mean(Tsonic(fourtt+1:fiftht));
Tsonic_6=mean(Tsonic(fiftht+1:sixtht));

T=[Tsonic_1
   Tsonic_2
   Tsonic_3
   Tsonic_4
   Tsonic_5
   Tsonic_6];

sfl=['save d:\temp\t_',dfl(14:18),'.prn T -ascii'];
disp(sfl)
eval(sfl)
