load sst_data.mat
%use only the equatorial part of the data
ssteq=sstnp(16:40,:,:);
loneq=lonnp;
lateq=latnp(16:40);

%contour the sst for the winter of 1982-1983
figure
subplot(2,1,1)
contourf(loneq,lateq,ssteq(:,:,26))
colorbar
xlabel('Longitude')
ylabel('Latitude')
title('SST anomaly winter DJF 1982-1983')

subplot(2,1,2)
contourf(loneq,lateq,ssteq(:,:,27))
colorbar
xlabel('Longitude')
ylabel('Latitude')
title('SST anomaly winter DJF 1983-1984')


%reshape the data matrix to get ready to do the EOF
data=reshape(ssteq,[25*81 44]);

%replace all NaN's with zeros.
idata=isnan(data);
data(idata)=0;

%perform the eof
[V,L,U,frac]=eof_svd(data);

%reshape again to get the spatial patterns
eof=reshape(V,[25 81 44]);

%plot the first EOF and the times series

figure
subplot(2,1,1)
contourf(loneq,lateq,eof(:,:,1))
colorbar
xlabel('Longitude')
ylabel('Latitude')
title('First EOF')

subplot(2,1,2)
plot(time,U(:,1))
title('First PC')
xlabel('year')

