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

% 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;
Dsst=NaN*ones(length(Lat),length(Lon),2);

for looploop=1:2;

    if (looploop==1)
        eval(['load ',dataloc,'loopers lon lat sst id']);
    else
        eval(['load ',dataloc,'nonloopers lon lat sst id']);
    end;

q=find(lon<min(LonR) | lon >max(LonR));
truncatedrift;
q=find(lat<min(LatR) | lat>max(LatR));
truncatedrift;

% calculate dsst, the one-day change in SST
dsst=NaN*ones(size(sst));
ID=unique(id);
for i=1:length(ID);
    a=find(id==ID(i));
    if (length(a)>=4)
        for j=3:length(a)-2
            dsst(a(j))=sst(a(j+2))-sst(a(j-2));
        end;
    end;
end;

q=find(isnan(dsst)); truncatedrift;
clf; colormapanom;

% calculate spatially-averaged dsst
for i=1: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)>10)
            Dsst(i,j,looploop)=mean(dsst(q(q2)));
        end;
    end;
    patchmap(Lon,Lat,Dsst(:,:,looploop));
    caxis([-.25 .25])
    pause(.1);
end;

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

    end;  % loop over looploop

   
subplot(311);
patchmap(Lon,Lat,Dsst(:,:,1));
mapaxis([min(Lon) max(Lon) min(Lat) max(Lat)]);
caxis([-.1 .1]);
coasts(3,1,[],'k');
colorbarvert;
title('Loopers')

subplot(312);
patchmap(Lon,Lat,Dsst(:,:,2));
mapaxis([min(Lon) max(Lon) min(Lat) max(Lat)]);
caxis([-.1 .1]);
coasts(3,1,[],'k');
colorbarvert;
title('Nonloopers')

subplot(313);
patchmap(Lon,Lat,Dsst(:,:,1)-Dsst(:,:,2));
mapaxis([min(Lon) max(Lon) min(Lat) max(Lat)]);
caxis([-.1 .1]);
coasts(3,1,[],'k');
colorbarvert;
title('Loopers minus Nonloopers')

load bathyworld lon2 lat2 z2
for i=1:3;
    subplot(3,1,i);
    hold on;
    l=contour(lon2,lat2,z2,[-4000 -4000],[.5 .5 .5]);
    l=contour(lon2,lat2,z2,[-200 -200],[.25 .25 .25]);
    hold off;
end;
