disp('read_son_GasExIII_2008.m')
fclose('all');
clear st1;clear st2;clear st3;clear jadson;

if ddd>99,
   jd=num2str(ddd);
else   
   jd=['0',num2str(ddd)];
end;

jhold=0; 
jax=1;
jaxx=1;
st1=[];temp=[];
z=zeros(6,36000);
jam=hhh;
if jam<10,
   hr=['0',num2str(jam)];
else
   hr=num2str(jam);
end;

dfl=['C:\Data\AMMA\day',jd,'\son008' jd hr '_raw.txt'];

disp(dfl)
flist=fopen(dfl,'r');

if flist<0
    st1=zeros(9,36000);
end;

if flist>0,	   				%if the file exists,
    while feof(flist)==0,  
        
        try
            temp =textscan(flist,'%2f%2f%3f %*4c %f %f %f %f %f %*3c %*[^\n]','delimiter', ',', 'headerlines', 1,  'emptyvalue', NaN,'treatAsEmpty', 'Sonic');
            st1=[st1 cell2mat(temp)'];
        catch
%             for ll=2:8
%                 if length(temp{1,ll})~=length(temp{1,1})
%                     temp{1,ll}(length(temp{1,1}))=NaN;
%                 end;
%             end;
        
        for ll=1:8
             [l8,c8]=size(temp{1,8});
             if length(temp{1,ll})~=l8
                 temp{1,ll}(l8+1)=[];
             end;
         end;
        if isempty(temp{1,1});
%             temp{1,8}=temp{1,8}';
            temp=[];
        end;
         st1=[st1 cell2mat(temp)'];
        end;     
        
    end;     
end;   

st1(:,any(isnan(st1),1))=[]; %remove lines containing NaNs (bad lines, too long...)

% ii=find(st1(8,:)==0);st1(:,ii)=[];  %delete bad lines;
tref=0:1/10:35999/10;
soniclength=length(st1)

tsons=st1(1,:)*60+st1(2,:)+st1(3,:)/1000; %real raw time in s
if length(tsons)<30000
    if tsons(1)<10 | tsons(length(tsons))>3550
        st1=st1;
    else st1=zeros(8,36000);
    end;
end;
    
    
tsons=st1(1,:)*60+st1(2,:)+st1(3,:)/1000; %real raw time in s

tmotion=0:1/10:35999/10;
tmotion=tmotion';


u=st1(5,:);  %m/s
v=st1(6,:);  %m/s
W=st1(7,:);  %m/s

try
    u=despike_gasexIII(u);  %m/s
    v=despike_gasexIII(v);  %m/s
    W=despike_gasexIII(W);  %m/s
catch
    u=u;v=v;W=W;
end

% % % **** Sonic configuration for GASEX 2008****************************
% % % R3		%Sonic model
% % % 10		%frequency (Hz)
% % % 0		%rotated 30 deg = 1 (rot=AXIS mode)
% % % 1		%Sonic output. 0=speed of sound, 1=Temperature (degC)
% % %  ********************************************************************
Tsonic=st1(8,:); %Temperature in degC
% 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
U=u;		   %to North
V=v;  		   %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);

mU=mean(U);
mV=mean(V);
mW=mean(W);

for ii=1:length(U);
   if U(ii)>35,
      U(ii)=mU;
   end;
   if V(ii)>35,
      V(ii)=mV;
   end;
   if W(ii)>35,
      W(ii)=mW;
   end;
end;
   
clear vul v uul u tzilch tzero sul st3 speedson prtit prt_it plotit nr1 npz np nl1 jl jk jj jhold jd jazx jaxx jax jam jadson jade j_st j_en j hul hr hll flist
clear f e dmp2 dmp backchk ans Tul Tll S Rtul Rtll Rsul Rsll Rlul Rlll st1 st2 u v
clear mU mV mW


su=(1:36000).*0;
sv=(1:36000).*0;
sw=(1:36000).*0;
st=(1:36000).*0;
su(1)=U(1);
sv(1)=V(1);
sw(1)=W(1);
st(1)=Tsonic(1);

if length(U)>36000
    U=U(1:36000);
    V=V(1:36000);
    W=W(1:36000);
    Tsonic=Tsonic(1:36000);
    tsons=tsons(1:36000);
end
[test, m, n] =unique(tsons);

if tsons(1)==0
   tsons=0:0.1:3599.9;
   [test, m, n] =unique(tsons);
end
% su=interpft(U,36000);su=su';ii=find(~isnan(su));msu=mean(su(ii));ii=find(isnan(su));su(ii)=msu;   %interpolation on the time base tmotion  
% sv=interpft(V,36000);sv=sv';ii=find(~isnan(sv));msv=mean(sv(ii));ii=find(isnan(sv));sv(ii)=msv;      %and replace the NaN by the mean.
% sw=interpft(W,36000);sw=sw';ii=find(~isnan(sw));msw=mean(sw(ii));ii=find(isnan(sw));sw(ii)=msw;
% st=interpft(Tsonic,36000);st=st';ii=find(~isnan(st));mst=mean(st(ii));ii=find(isnan(st));st(ii)=mst;
su=interp1(tsons(m),U(m),tref,'spline');su=su';ii=find(~isnan(su));msu=mean(su(ii));ii=find(isnan(su));su(ii)=msu;   %interpolation on the time base tmotion  
sv=interp1(tsons(m),V(m),tref,'spline');sv=sv';ii=find(~isnan(sv));msv=mean(sv(ii));ii=find(isnan(sv));sv(ii)=msv;      %and replace the NaN by the mean.
sw=interp1(tsons(m),W(m),tref,'spline');sw=sw';ii=find(~isnan(sw));msw=mean(sw(ii));ii=find(isnan(sw));sw(ii)=msw;
st=interp1(tsons(m),Tsonic(m),tref,'spline');st=st';ii=find(~isnan(st));mst=mean(st(ii));ii=find(isnan(st));st(ii)=mst;

%%%%%%%%%  file hhh=oo always starts late by 6-7s. Fix the interpolation issues at beginning of hhh 00 %%%%%%%%%%%%%%%%%%%%%%
if hhh==00
    t1=floor(tsons(1)*10);
    su(1:t1+5)=su(t1+10);
    sv(1:t1+5)=sv(t1+10);
    sw(1:t1+5)=sw(t1+10);
    st(1:t1+5)=st(t1+10);
end;    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
    

    
U=su';
V=sv';
W=sw';
Tsonic=st';

tmotion=0:1/10:35999/10;


% % %05/13/08 after checking all sonic files, I saved the starting and end
% % %point of bad interpolation (when data gap is too big...)
% % % The following replace the crazy values by the median of the signal
% % Cdref=load('C:\Data\CRUISES\GasExIII\data\CorrectPbsMotocrr.prn');
% % ii=find(ddd==Cdref(:,1) & hhh==Cdref(:,2));
% % for ll=1:length(ii)
% %     U(Cdref(ii(ll),3):Cdref(ii(ll),4))=nanmedian(setdiff(U,U(Cdref(ii(ll),3):Cdref(ii(ll),4))));
% %     V(Cdref(ii(ll),3):Cdref(ii(ll),4))=nanmedian(setdiff(V,V(Cdref(ii(ll),3):Cdref(ii(ll),4))));
% %     W(Cdref(ii(ll),3):Cdref(ii(ll),4))=nanmedian(setdiff(W,W(Cdref(ii(ll),3):Cdref(ii(ll),4))));
% %     Tsonic(Cdref(ii(ll),3):Cdref(ii(ll),4))=nanmedian(setdiff(Tsonic,Tsonic(Cdref(ii(ll),3):Cdref(ii(ll),4))));
% % end;
% % 
% % try
% %     U=despike_gasexIII(U);  %m/s
% %     V=despike_gasexIII(V);  %m/s
% %     W=despike_gasexIII(W);  %m/s
% % catch
% %     U=U;V=V;W=W;
% % end    