function animate(filename,pathname,bands,displayAxis,playTimes,playDirection,doflipud,dofliplr,rect,txtMessage)
%Animates through all the bands in a MODIS file
%Input: filename, pathname, an array of bands selected, axis to display on
%       number of times to play movie, the direction to play
%       optional txtMessage field to display when stepping through
%Kevin Baggett
%Thu Jul 26 22:30:25 UTC 2001

switch playDirection
    case 'Forward'
        bandStart=1;
        bandIncrement=1;
        bandEnd=length(bands);
    case 'Backward'
        bandStart=length(bands);
        bandIncrement=-1;
        bandEnd=1;
    case 'Step Forward'
        bandStart=1;
        bandIncrement=1;
        bandEnd=length(bands);
        if nargin==10
            set(txtMessage,'Visible','on');
        end
    case 'Step Backward'
        bandStart=length(bands);
        bandIncrement=-1;
        bandEnd=1;
        if nargin==10
            set(txtMessage,'Visible','on');
        end
end

axes(displayAxis);
set(displayAxis,'Visible','On');
for bandNum=bandStart:bandIncrement:bandEnd
    if filename(4:5)=='02'
      [imagedata] = modis_level1b_read([pathname filename],bands(bandNum),{'''raw'''},rect);
	elseif h.FileName(4:5)=='03'
      %insert code for 03 here
	end
    if doflipud
        imagedata.mimage=flipud(imagedata.mimage);
    end
    if dofliplr
        imagedata.mimage=fliplr(imagedata.mimage);
    end
	imagesc(imagedata.mimage);
	set(gca,'XTick',[],'YTick',[]);
	colormap(gray(264));
    colorbar;
	title(['Channel ' num2str(bands(bandNum))]);
    if strcmp(playDirection,'Step Forward')|strcmp(playDirection,'Step Backward')
        if bandNum~=bandEnd
            waitforbuttonpress; %wait except for last time through loop
        end
    else
        M(bandNum) = getframe(gca,[0 0 380 380]);
    end
end

if strcmp(playDirection,'Forward')|strcmp(playDirection,'Backward')
    set(displayAxis,'Visible','Off');
	try
        playBands=[playTimes-1,bandStart:bandIncrement:bandEnd];
        movie(M,playBands,0.75); %error occurs if closing movie prematurely
	catch
	end
end
movie2avi(M,'temp.avi');
if nargin==10
    set(txtMessage,'Visible','off');
end