function [xadv yadv zadv action]=advectParticles3dfloat2(X,Y,Z,zcoord,U,V,t,x0,y0,z0,zpark,parkTime,telemetryTimes,profileSpeed)
%[xadv yadv]=advectParticles3dfloat2(X,Y,Z,zcoord,U,V,t,x0,y0,t0,z0,...
%	[zpark=-1e3,parkTime=17*3600,telemetryTimes=[0 14]*60,profileSpeed=0.08])
%
% advect float following sequence: 
% telemetry1->floatdown->park->floatup->telemetry2->floatdown->park->floatup->telemetry2->floatdown->...
% sequence repeats untill the end of available U and V data
%
% X, Y 		-- [npoints] x y grid in lat lon
% Z    		-- [size(X)] bathymetry
% zcoord    	-- vertical grid
% U, V 		-- [npoints,ntimes] speed in cm/s
% t    		-- [ntimes,1] time of U, V data (days)
% x0, y0, t0 	-- [1x1] x, y and time of intitial particle locations
% zpark 	-- [1x1] parking depth in meters (e.g. -1e3)
% parkTime	-- [1x1]
% telemetryTimes -- [2,1] period of time for surface telemetry (seconds)
% profileSpeed  -- profile speed in m/s
%
% SF 2010
% SF, 2011 use inter2 for simple grids
% SF, Feb 2012 modified to use JPL ROMS for Ken

%keyboard

if ~exist('profileSpeed'),profileSpeed=0.08;end			%m/s
if ~exist('telemetryTimes'),telemetryTimes=[0 14]*60;end	%seconds
if ~exist('zpark'),zpark=-1e3;end				%m
if ~exist('parkTime'),parkTime=17*3600;end	%length of time spend parked (s)

xadv=nan(size(x0,1),length(t));
yadv=nan(size(y0,1),length(t));
zadv=nan(size(z0,1),length(t));

xadv(:,1)=x0;
yadv(:,1)=y0;
zadv(:,1)=z0;

%intial action in the loop of telemetry1-floatdown-park-floatup-telemetry2
action='telemetry';
actionTime=telemetryTimes(1);	%timelimit on the current action

for i=1:length(t)-1
  dt=(t(i+1)-t(i))*24*3600;

%  disp(i)
  if ~isnan(xadv(:,i)+yadv(:,i))&~isnan(interp2(X,Y,squeeze(Z(:,:)),xadv(:,i),yadv(:,i)))% check if in the water
    if strcmp(action,'floatup')
      w=profileSpeed;
      [xadv(:,i+1),yadv(:,i+1),zadv(:,i+1)]=advectp2(X,Y,Z,zcoord,squeeze(U(:,:,:,i)),squeeze(V(:,:,:,i)),w,dt,...
            xadv(:,i),yadv(:,i),zadv(:,i));

      %if on surface, switch to telemetry
  %    zsurf=interp2(X,Y,squeeze(Z(1,:,:)),xadv(:,i),yadv(:,i));
      zsurf=zcoord(1);
      if zadv(:,i+1)>=zsurf;
        zadv(:,i+1)=zsurf;
        action='telemetry';
        actionTime=telemetryTimes(2);
      end

    elseif strcmp(action,'floatdown')
      w=-profileSpeed;
      [xadv(:,i+1),yadv(:,i+1),zadv(:,i+1)]=advectp2(X,Y,Z,zcoord,squeeze(U(:,:,:,i)),squeeze(V(:,:,:,i)),w,dt,...
          xadv(:,i),yadv(:,i),zadv(:,i));

      %if on the bottom or at the park depth, switch to drift
      zbottom=interp2(X,Y,Z,xadv(:,i+1),yadv(:,i+1));
      if (zadv(:,i+1)<=zbottom)
        zadv(:,i+1)=zbottom;
        action='park';
        actionTime=parkTime;
      elseif (zadv(:,i+1)<=zpark)
        zadv(:,i+1)=zpark;
        action='park';
        actionTime=parkTime;
      end

    elseif strcmp(action,'telemetry')
      w=0;
      [xadv(:,i+1),yadv(:,i+1),zadv(:,i+1)]=advectp2(X,Y,Z,zcoord,squeeze(U(:,:,:,i)),squeeze(V(:,:,:,i)),w,dt,...
          xadv(:,i),yadv(:,i),zadv(:,i));

      actionTime=actionTime-dt;
      if actionTime<=0;
        action='floatdown';
        actionTime=inf;
      end

    elseif strcmp(action,'park')
      w=0;
      zbottom=interp2(X,Y,Z,xadv(:,i),yadv(:,i));
      if zadv(:,i)==zbottom  
        xadv(:,i+1)=xadv(:,i);
        yadv(:,i+1)=yadv(:,i);
        zadv(:,i+1)=zadv(:,i);
      else
        [xadv(:,i+1),yadv(:,i+1),zadv(:,i+1)]=advectp2(X,Y,Z,zcoord,squeeze(U(:,:,:,i)),squeeze(V(:,:,:,i)),w,dt,...
            xadv(:,i),yadv(:,i),zadv(:,i));
      end

      actionTime=actionTime-dt;
      if actionTime<=0;
        action='floatup';
        actionTime=inf;
      end

    else
      error(['Unknown action: ' action])
    end
  end
end

