% plot the percent of drifters that enter a particular region,
% after passing through a grid point

clear all;

dataloc='/WSmounts/phoddat/share/lumpkin/surfdrift/spin/spinpaper2/';

eval(['load ',dataloc,'nonloopers.mat lon lat t id'])


% lon, lat range of study
LonR=[-25 50];
LatR=[-50 -25];

dL=3;
Lon=min(LonR)+dL/2:dL:max(LonR)-dL/2;
Lat=min(LatR)+dL/2:dL:max(LatR)-dL/2;

polyx=[-90 -90 0 0 20 20];
polyy=[0 -90 -90 -30 -30 0];

% define grid
%dL=1;
%Lon=-100:dL:20; Lat=-15:dL:40;
%Lon=-80:dL:-55; Lat=5:dL:20;
Frac=zeros(length(Lat),length(Lon));
NumObs=Frac;

colormap jet;
for i=1:length(Lat);
    fprintf('%d of %d\n',i,length(Lat));
    q=find(lat>=Lat(i)-dL/2 & lat<Lat(i)+dL/2);
    for j=1:length(Lon);
        q2=find(lon(q)>=Lon(j)-dL/2 & lon(q)<Lon(j)+dL/2);
        if (length(q2))
            ID=unique(id(q(q2)));
            InPoly=zeros(length(ID),1);
            for k=1:length(ID);
                qq=find(id==ID(k));
                % only want future, not past
                qq2=find(lon(qq)>=Lon(j)-dL/2 & lon(qq)<Lon(j)+dL/2 & ...
                    lat(qq)>=Lat(i)-dL/2 & lat(qq)<Lat(i)+dL/2);
                qq=qq(qq2(1):length(qq));
                if length(find(isinpoly(lon(qq),lat(qq),polyx,polyy))) 
                    InPoly(k)=1;
                end;
                Frac(i,j)=sum(InPoly)/length(ID); 
                NumObs(i,j)=length(ID);
            end;
        end;
    end;
    patchmap(Lon,Lat,Frac);
    pause(.1);
end;

coasts(3,1,[],'k');
mapaxis([min(Lon) max(Lon) min(Lat) max(Lat)]);
colorbarvert;

