% simplemean_srilanka.m: calculate mean currents in
% Sri Lanka region using a simple binned average

% find the subset of the data with drogue attached
% and in the months of May, June, July, August or September
gd=gregoriand(t);
q=find(gd(:,2)>=5 & gd(:,2)<=9 & drogue==1);
x1=u(q);x2=v(q);lon2=lon(q);lat2=lat(q);

% calculate grid of 1/2 degree bins, with 1/4 degree overlap
dLon=.5; dLat=.5;
Lon=ceil(min(lon2)):dLon/2:floor(max(lon2));
Lat=ceil(min(lat2)):dLat/2:floor(max(lat2));

% pre-allocate memory
Xbar=NaN*ones(length(Lat),length(Lon),2);
NumObs=NaN*ones(length(Lat),length(Lon),1);

% calculate mean and standard error in each bin
for i=1:length(Lat);
    q=find(lat2>=Lat(i)-dLat/2 & lat2<=Lat(i)+dLat/2);
    fprintf('%d of %d\n',i,length(Lat));
    for j=1:length(Lon);
        q2=find(lon2(q)>=Lon(j)-dLon/2 & lon2(q)<=Lon(j)+dLon/2);
        if (length(q2)>4)
            Ubar(i,j,1)=mean(x1(q(q2)));
            Ubar(i,j,2)=std(x1(q(q2)))/sqrt(length(q2)/4/5);
            Vbar(i,j,1)=mean(x2(q(q2)));
            Vbar(i,j,2)=std(x2(q(q2)))/sqrt(length(q2)/4/5);
            NumObs(i,j)=length(q2)/4;
        end;
    end;
end;

% plot results where there are at least 5 drifter-days of data
ubar=Ubar(:,:,1); vbar=Vbar(:,:,1);
lonmat=ones(length(Lat),1)*Lon; 
latmat=Lat'*ones(1,length(Lon));
q=find(NumObs>5); 
quiver(lonmat(q),latmat(q),ubar(q),vbar(q),0);
mapaxis([70 85 0 20]);
coasts(3,1,[],'k');

title('Mean currents, 1 May-30 Sept','fontsize',12)
