function [FLT]=plotxy_floats(fname,hname,movie_file,Nrec);

%
% PLOTXY_FLOATS:  Plots ROMS floats tracjetory on XY-section
%
% [FLT]=plotxy_floats(fname,hname,movie_file,Nrec)
%
% This function plots and animates ROMS floats trajectory on a
% horizontal section.
%
% On Input:
%
%    fname       ROMS floats NetCDF file name (character string)
%    hname       ROMS history NetCDF file name (character string)
%    movie_file  Output movie file name (*.avi or *.flc)
%    Nrec        Plots every Nrec time records (OPTIONAL)
%
%
% On Output:
%
%    FLT         Float data tracjectory (structure array)
%

% svn $Id$
%===========================================================================%
%  Copyright (c) 2002-2009 The ROMS/TOMS Group                              %
%    Licensed under a MIT/X style license                                   %
%    See License_ROMS.txt                           Hernan G. Arango        %
%===========================================================================%

AVI_MOVIE=strfind(movie_file,'.avi');
if isempty(AVI_MOVIE),
  AVI_MOVIE=0;
end,


FLC_MOVIE=strfind(movie_file,'.flc');
if isempty(FLC_MOVIE),
  FLC_MOVIE=0;
end,

FLT.movie_file=movie_file;

if (nargin < 4),
  Nrec=1;
end,

%---------------------------------------------------------------------------
%  Read in application grid data. 
%---------------------------------------------------------------------------

char1=nc_read(hname,'spherical');
if (upper(char1) == 'T'),
  FLT.spherical=1;
else,
  FLT.spherical=0;
end,

if (FLT.spherical),
  Xr=nc_read(hname,'lon_rho');
  Yr=nc_read(hname,'lat_rho');
else,  
  Xr=nc_read(hname,'x_rho').*0.001;
  Yr=nc_read(hname,'y_rho').*0.001;
end,  

bath=nc_read(hname,'h');

got_mask=0;

[vname,nvars]=nc_vname(hname);
for n=1:nvars,
  name=deblank(vname(n,:));
  switch (name)
    case 'mask_rho'
      got_mask=1;
      rmask=nc_read(hname,name);
  end,
end,

if (~got_mask),
  rmask=ones(size(Xr));
end,

FLT.Xr=Xr;
FLT.Yr=Yr;
FLT.bath=bath;
FLT.rmask=rmask;
FLT.got_mask=got_mask;

clear char1 name nvars vname

%---------------------------------------------------------------------------
%  Read in floats data. 
%---------------------------------------------------------------------------

FLT.time=nc_read(fname,'ocean_time');
FLT.time=FLT.time./86400;

FLT.Xgrid=nc_read(fname,'Xgrid');
FLT.Ygrid=nc_read(fname,'Ygrid');

if (FLT.spherical),
  FLT.lon=nc_read(fname,'lon');
  FLT.lat=nc_read(fname,'lat');
else,  
  FLT.x=nc_read(fname,'x').*0.001;
  FLT.y=nc_read(fname,'y').*0.001;
end,  

[FLT.nfloats,FLT.ntimes]=size(FLT.Xgrid);

%---------------------------------------------------------------------
%  Draw trajectories.
%---------------------------------------------------------------------

fig=figure;

if (AVI_MOVIE),                          % AVI movie

  aviobj=avifile(movie_file);
  
  for n=1:Nrec:FLT.ntimes,

    if (got_mask),
      pcolorjw(Xr,Yr,rmask);
      colormap([0 0 0; 1 1 1]);
      mark='ro';
    else,
      contoura(Xr,Yr,bath,20);
      mark='ko';
    end,
    title(['Time = ',num2str(FLT.time(n),'%10.5f'), '  days']);
    hold on;

    if (FLT.spherical),
      han=plot(FLT.lon(:,n),FLT.lat(:,n),mark);
    else,
      han=plot(FLT.x(:,n),FLT.y(:,n),mark);
    end,
    set(han(1),'markersize',5)
    hold off;

    F=getframe(fig);

    aviobj=addframe(aviobj,F);
  
  end,
  close(fig);
  aviobj=close(aviobj);

end,

if (FLC_MOVIE),                          % FLC movie
  
  axis manual
  set(gca,'nextplot','replacechildren');

  ic=0;
  
  for n=1:Nrec:FLT.ntimes,

    ic=ic+1;

    hold off;
    
    if (got_mask),
      pcolorjw(Xr,Yr,rmask);
      colormap([0 0 0; 1 1 1]);
      mark='ro';
    else,
      han=contour(Xr,Yr,bath,20);
      mark='ko';
    end,
    title(['Time = ',num2str(FLT.time(n),'%10.5f'), '  days' ...
	   '  Record = ',num2str(n)]);
    hold on;
  
    if (FLT.spherical),
      han=plot(FLT.lon(:,n),FLT.lat(:,n),mark);
    else,
      han=plot(FLT.x(:,n),FLT.y(:,n),mark);
    end,
    
%   set(han(1),'markersize',5)
  
    F=getframe(fig);

    outfile=sprintf('floats%3.3d.ppm',ic);
  
    print('-dppmraw', outfile);
  
  end,

  nx=560;                                % default Matlab window size
  ny=480;

  eval('!ls floats???.ppm > allppm.list');
  eval(['!rm ' movie_file]);
  cmd=['!ppm2fli -g ' int2str(nx) 'x' int2str(ny)];
  cmd=[cmd  ' -N allppm.list ' movie_file];
  eval(cmd);
  unix('rm allppm.list');
  unix('rm *.ppm');
  
end,

return


