% calc_tmi_ssst - calculate TMI SST (40S-40N)
% interpolated to the drifter positions lon, lat, t
% (julian days since 1/1/79).
% The resulting data field is called sst_tmi
%   TMI daily SST begins on 1 January 1998,
%   julian drifter day 6941

q=find(t>=juliand([1998 1 1 0 0 0]));   % earliest TMI time
% convert t (jul.drifter days) to julian days
T_tmi=round(julian(gregoriand(t(q))));
T_tmi=min(T_tmi):max(T_tmi);

if (max(lon)>180);
    lonny=lon;
    q=find(lon>180);
    lon(q)=lon(q)-360;
end;
T=round(julian(gregoriand(t)));
sst2=NaN*ones(size(t));
for i=1:length(T_tmi);
    fprintf('%d of %d\n',i,length(T_tmi))
    q2=find(T==T_tmi(i));
    if (~isempty(q2))
        [lon_tmi,lat_tmi,t_tmi,sst_tmi]= ...
            load_tmi_sst([min(lon)-.5 max(lon)+.5], ...
            [min(lat)-.5 max(lat)+.5],T_tmi(i));
        sst2(q2)=interp2(lon_tmi,lat_tmi,sst_tmi, ...
            lon(q2),lat(q2),'linear');
    end;
end;
sst_tmi=sst2; 
clear sst2 i q q2 T lon_tmi lat_tmi t_tmi T_tmi;

if exist('lonny');
    lon=lonny;
    clear lonny;
end;


