% MKPOS_ED  Make eddy center positions for use in referencing tracer tows.
%
% Interpolate centers to 0600Z on days when tracer tows ended.

hh = 6;
mmlist = [8 9 9 9 9 9 9 9];
daylist = [31 1 2 3 4 5 6 8]; % note: skipped 9/7 due to Nate

ef1 = load('edfit_52484.mat');
ef2 = load('edfit_52486.mat');
meth = 'cubic';
meth = 'spline';
meth = 'linear';

extr = NaN;
extr = 'extrap';

clear mld lat lon

ofile = ['../adcp/positions/traj_drifter.dat'];
oid = fopen(ofile,'w');
fprintf(oid,'%%Date           Time    Lat     Longitude\n');

for ii = 1:length(daylist),
  mld(ii) = datenum(2005,mmlist(ii),daylist(ii),hh,0,0);
  lat1(ii) = interp1(ef1.mld_mid,ef1.lat_fit,mld(ii),meth,extr);
  lon1(ii) = interp1(ef1.mld_mid,ef1.lon_fit,mld(ii),meth,extr);
  lat2(ii) = interp1(ef2.mld_mid,ef2.lat_fit,mld(ii),meth,extr);
  lon2(ii) = interp1(ef2.mld_mid,ef2.lon_fit,mld(ii),meth,extr);
end

bad = find(isnan(lat1)|isnan(lon1));
gd = find(finite(lat1)&finite(lon1));
for ii = bad,
  lat1(ii) = interp1(mld(gd),lat1(gd),mld(ii),meth,'extrap');
  lon1(ii) = interp1(mld(gd),lon1(gd),mld(ii),meth,'extrap');
end

bad = find(isnan(lat2)|isnan(lon2));
gd = find(finite(lat2)&finite(lon2));
for ii = bad,
  lat2(ii) = interp1(mld(gd),lat2(gd),mld(ii),meth,'extrap');
  lon2(ii) = interp1(mld(gd),lon2(gd),mld(ii),meth,'extrap');
end

  lat = mean([lat1' lat2']');
  lon = mean([lon1' lon2']');

for ii = 1:length(daylist),
  fprintf(oid,'%s     %s    %7.4f %8.4f\n',datestr(mld(ii),1),...
	    datestr(mld(ii),'HHMM'),lat(ii),lon(ii));
end

fclose(oid);

subplot(221); plot(mld,lat,'ko','markerfacecolor','m');
title(['magenta: interpolated at center tow times']);
subplot(223); plot(mld,lon,'ko','markerfacecolor','m');
title(['extrapolation: ' meth]);
subplot(122); plot(-lon,lat,'ko','markerfacecolor','m');


