% extract drogued and slip-corrected undrogued drifters along 23W and 10W
% and slip-corrected Argo floats

if (1) %~exist('v')
    
    % load Argo floats
    load drifters_floats_tafront lon lat t id u v drogue Wx Wy
    % drop drifters from this data set (will be loaded later)
    q=find(drogue>-1); truncatedrift;
    bestA=1.143e-2 + 7e-4;
    q=find(drogue==-1);
    u(q)=u(q)-bestA*Wx(q);
    v(q)=v(q)-bestA*Wy(q);
    u2=u; v2=v; id2=id; t2=t; Wx2=Wx; Wy2=Wy;
    lon2=lon; lat2=lat;
    drogue2=drogue(q);
    
    % load drifters
    load /data/lumpkin/surfdrift/world lon lat t id Wx Wy u_orig v_orig sst
    load /data/lumpkin/surfdrift/drogue_2013_03_13 drogue
    
    q=find(lon<-30 | lon>-8);
    truncatedrift;
    q=find(lat<-20 | lat>25);
    truncatedrift;
    
    % discard drifters with "drogue status uncertain"
    q=find(drogue==0.5);
    truncatedrift;
    
    u=u_orig; v=v_orig; clear *orig;
    
    % remove the slip. 
    %% Drogued: 7e-4 m/s per m/s of wind. (Niiler & Paduan, 1995)
    %% Undrogued: 7e-4+7.9e-3=8.6e-3 (", Pazan & Niiler 2000)  
    q=find(drogue==1);
    u(q)=u(q)-7e-4*Wx(q);
    v(q)=v(q)-7e-4*Wy(q); 
    q=find(drogue==0);  
    %u(q)=u(q)-8.6e-3*Wx(q);
    %v(q)=v(q)-8.6e-3*Wy(q);
    bestA=1.6396e-2 + 7e-4;
    u(q)=u(q)-bestA*Wx(q);
    v(q)=v(q)-bestA*Wy(q);
    
    q=find(isnan(u)); truncatedrift;
    clear q;

    u=[u;u2]; v=[v;v2]; id=[id;id2]; t=[t;t2];
    drogue=[drogue;drogue2];
    lon=[lon;lon2]; lat=[lat;lat2];
     
    
else
    load drifters_tafront lon lat t id u v drogue
end;

% calculate mean U and V for 23W and 10W
Lon=[[-30 -20]; [-12 -8]];  % longitude bands for average
dLat=1;
Lat=-20:dLat/2:25; 

% store mean velocity values. 
%  U( Lat x Lon band x 3).  
%   Third index: 1=data, 2=std. dev., 3=num obs (days)
U=NaN*ones(length(Lat),size(Lon,2),3);
V=U;
FracNoDrogue=NaN*ones(length(Lat),size(Lon,2));
Ud=U; Vd=V; % mean speed of drogued drifters
Uu=U; Vu=V; % mean speed of undrogued, corrected drifters

for i=1:length(Lat);
    q=find(lat>=Lat(i)-dLat/2 & lat<=Lat(i)+dLat/2);
    for j=1:size(Lon,2);
        
        qq=find(lon(q)>=Lon(j,1) & lon(q)<=Lon(j,2));
        qqd=find(lon(q)>=Lon(j,1) & lon(q)<=Lon(j,2) & drogue(q)==1);
        qqu=find(lon(q)>=Lon(j,1) & lon(q)<=Lon(j,2) & drogue(q)==0);
        
        if (length(qq)>=12)
            
            ft=t(q(qq))/365.24;  
            fx=lon(q(qq))-mean(Lon(j,:));
            fy=lat(q(qq))-Lat(i);
            model=[1+0*ft,sin(ft*2*pi),cos(ft*2*pi) ...
                    sin(ft*4*pi) cos(ft*4*pi) fx fy]; 
                
            qu=pinv(model)*u(q(qq));
            U(i,j,1)=qu(1); 
            U(i,j,2)=std(u(q(qq)));
            U(i,j,3)=length(qq)/4;  % drifter-days 

            qu=pinv(model)*v(q(qq));
            V(i,j,1)=qu(1);
            V(i,j,2)=std(v(q(qq)));
            V(i,j,3)=length(qq)/4;
            
            FracNoDrogue(i,j)=length(qqu)/length(qq);
            
            ft=t(q(qqd))/365.24;  
            fx=lon(q(qqd))-mean(Lon(j,:));
            fy=lat(q(qqd))-Lat(i);
            model=[1+0*ft,sin(ft*2*pi),cos(ft*2*pi) ...
                    sin(ft*4*pi) cos(ft*4*pi) fx fy];  
            qu=pinv(model)*u(q(qqd));
            Ud(i,j,1)=qu(1); 
            Ud(i,j,2)=std(u(q(qqd)));
            Ud(i,j,3)=length(qqd)/4;  % drifter-days  
            qu=pinv(model)*v(q(qqd));
            Vd(i,j,1)=qu(1);
            Vd(i,j,2)=std(v(q(qqd)));
            Vd(i,j,3)=length(qqd)/4;
            
            ft=t(q(qqu))/365.24;  
            fx=lon(q(qqu))-mean(Lon(j,:));
            fy=lat(q(qqu))-Lat(i);
            model=[1+0*ft,sin(ft*2*pi),cos(ft*2*pi) ...
                    sin(ft*4*pi) cos(ft*4*pi) fx fy];  
            qu=pinv(model)*u(q(qqu));
            Uu(i,j,1)=qu(1); 
            Uu(i,j,2)=std(u(q(qqu)));
            Uu(i,j,3)=length(qqu)/4;  % drifter-days  
            qu=pinv(model)*v(q(qqu));
            Vu(i,j,1)=qu(1);
            Vu(i,j,2)=std(v(q(qqu)));
            Vu(i,j,3)=length(qqu)/4;
        end;
        
        
    end;
end;

clear ans i j q qq

save drifters_tafront

            



