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

%
% PLOTXZ_FLOATS:  Plots ROMS floats tracjetory on XZ-section
%
% [FLT]=plotxz_floats(fname,hname,movie_file)
%
% This function plots and animates ROMS floats trajectory on a
% vertical XZ-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)
%
%
% 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;

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

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

h=nc_read(hname,'h');

if (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,  
  
rmask=ones(size(Xr));
[vname,nvars]=nc_vname(hname);
for n=1:nvars,
  name=deblank(vname(n,:));
  switch (name)
    case 'mask_rho'
      rmask=nc_read(hname,name);
  end,
end,

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 (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);

%  Determine floats starting positions.

for n=1:FLT.nfloats,
  [ind]=find(~isnan(FLT.Xgrid(n,:)));
  FLT.Xo=FLT.Xgrid(n,ind(1));
  FLT.Yo=YLT.Ygrid(n,ind(1));
end,

%  Interpolate bathymetry at the initial starting position.

[Lp,Mp]=size(h);
[x,y]=ndgrd(0:Lp-1,0:Mp-1);
FLT.bottom=-griddata(x,y,h,FLT.Xo,FLT.Yo);

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

fig=figure;

if (AVI_MOVIE),                          % AVI movie

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

    pcolorjw(Xr,Yr,rmask);
    colormap([0 0 0; 1 1 1]);
    title(['Time = ',num2str(FLT.time(n),'%10.5f'), '  days']);
    hold on;

    if (spherical),
      h=plot(FLT.lon(:,n),FLT.lat(:,n),'ro');
    else,
      h=plot(FLT.x(:,n),FLT.y(:,n),'ro');
    end,
    set(h(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');
 
  for n=1:FLT.ntimes,

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

    outfile=sprintf('floats%3.3d.ppm',n);
  
    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


