function pclaw_animate(path,frames)
%PCLAW_ANIMATE Visualize saved pclaw computation.
%   PCLAW_ANIMATE(PATH,FRAMES) loads a data files generated by
%   the particle method PARTICLECLAW for scalar conservation and
%   balance laws in one space dimension.
%   PATH is a string providing where to find the files, FRAME is
%   a list of integers stating the numbers of the data file. If
%   PATH is omitted, the current path is chosen. If FRAMES is
%   omitted, all files are used.
%
%   Version 1.0
%   Copyright (c) 2008 Benjamin Seibold and Yossi Farjoun
%   http://math.mit.edu/~seibold/research/particleclaw
%   http://arxiv.org/abs/0809.0726

%===============================================================================
switch nargin % handle parameters
  case 0
    path = '.'; frames = inf;
  case 1
    if isa(path,'char')
        frames = inf;
    else
        frames = path; path = '.';
    end
end
%-------------------------------------------------------------------------------
if frames==inf % find all data files in folder
    filet = dir([path,'/pclaw.t*']);
    nrfiles = length(filet);
    frames = zeros(nrfiles,1);
    for i = 1:nrfiles
        filename = filet(i).name;
        frames(i) = str2num(filename(find(filename=='t')+1:end));
        filename = sprintf('%s/pclaw.q%04d',path,frames(i));
        if not(exist(filename,'file')), frames(i) = nan; end
    end
    frames = frames(not(isnan(frames)));
end
frames = frames(:)';
%-------------------------------------------------------------------------------
% visualization
clf
clear x0
for frame = frames
    [x,u,t] = pclaw_load(path,frame);
    if isnan(t)
        warning(sprintf('Frame %d could not be loaded.',frame))
    else
        if not(exist('x0','var'))
            x0 = x; u0 = u;
            xbox = [min(x0) max(x0)];
            ubox = [min(u0),max(u0)];
            ubox = ubox*[1.1 -.1;-.1 1.1];
        end
        clf
        set(gcf,'DoubleBuffer','on');
        plot(x0,u0,'b.-',x,u,'r.-')
        axis([xbox ubox])
        title(sprintf('t=%0.2f',t))
        drawnow
    end
end

%===============================================================================
% Copyright (c) 2008 Benjamin Seibold and Yossi Farjoun
% 
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction for non-commercial purposes, including
% without limitation the rights to use, copy, modify, merge, publish, and/or
% distribute copies of the Software, and to permit persons to whom the
% Software is furnished to do so, subject to the following conditions:
% 
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software, and credit has to be
% given to the authors in publications that are in any form based on this
% Software.
% 
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
% THE SOFTWARE.
%===============================================================================

