%function [] = monterey_example(example)
  
%*********************************************************************
%  Function to demonstrate group trajectory calculation in HOPS
%  Monterey Bay example
%*********************************************************************
%  example = 1 --> Coarse grid of particles
%  example = 2 --> Circle of particles near the center of the bay
%  example = 3 --> Disk of particles near the center of the bay
%*********************************************************************
  
default('example',1)

global u v

land_color  = [0 0.51 0] ;
water_color = [0.5 1 1] ;

solver  = 1 ;
abs_tol = 0.1 ;
rel_tol = 0 ;
lscale  = 1d5 ;

%---------------------------------------------------------------------  
%  Load velocities and grid information from MAT file
%--------------------------------------------------------------------- 

%if (load_it == 1) load 'HOPS.mat' end;
time = [72:144];     %Run Time
ntime = [1:740];   %Total 'archive' Time

%---------------------------------------------------------------------  
%  Specify particle initial positions
%---------------------------------------------------------------------  

switch example
 case 1
  x0 = [60:10:90,60:10:90];
  y0 = 60*ones(1,length(x0)/2);
  y0 = [y0,70*ones(1,length(x0)/2)];
  tail_length = length(time) ; 
  numparts = length(x0);
 case 2
  [x0,y0] = ip_circle(15*lscale,20*lscale,4*lscale,20) ;
  tail_length = length(time) ; 
 case 3
  [x0,y0] = ip_disk(18*lscale,30*lscale,2*lscale,200) ;
  tail_length = 0 ; 
end

%---------------------------------------------------------------------  
%  Integrate trajectories
%---------------------------------------------------------------------  

[x_traj,y_traj,t] = trajectories(x0,y0,time,hops.x,hops.y,ntime,solver,abs_tol,rel_tol) ;

%---------------------------------------------------------------------  
%  Animate trajectories
%---------------------------------------------------------------------  

box = [min(hops.x) max(hops.x) min(hops.y) max(hops.y)] ;
box = [40,100,40,100];

%close all ; 
clf
figure(1)

fill(hops.x_coast,hops.y_coast,water_color) ;
  
set(gcf,'color','white','Position',[100,100,650,800])
set(gca,'Color',land_color,'Clipping','off') ;

hold on ; axis equal ; axis tight ; axis(box)
  
for i=1:length(t)
  if i > 1 ; delete(h1), delete(h2), delete(h3); end

  uu = u(:,:,time(i)) ;
  vv = v(:,:,time(i)) ;

%  h(1) = title(num2str(dates(i))) ;

  h1 = quiver(hops.x2,hops.y2,uu, ...
              vv,3,'color','k') ; 

  for k = 1:numparts
   h2(k)  = plot(x_traj(k,1:i),y_traj(k,1:i),'r-');
   h3(k)  = plot(x_traj(k,i),y_traj(k,i),'k<');
   h4(k)  = plot(x_traj(k,1),y_traj(k,1),'k*');
   set(h2(k),'LineWidth',2)
   set(h3(k),'MarkerFaceColor','y','MarkerSize',10)
  end


  fprintf('  -->  Frame %2.0d   Time = %9.0d\n',i,time(i)) ;

  M(i) = getframe(gcf) ;
  
%  save_frame
end

%movie2avi(M,'example.avi','fps',5)
%movie(gcf,M,5,5)
