function [varargout]=ww3mov(s,delay,varargin)
%WW3MOV    Create movie from a WaveWatch III hindcast GRiB1/2 file
%
%    Usage:    mov=ww3mov('file')
%              [mov1,...,movN]=ww3mov('file')
%              [...]=ww3mov('file',delay)
%              [...]=ww3mov('file',delay,rng)
%              [...]=ww3mov('file',delay,rng,fgcolor,bgcolor)
%              [...]=ww3mov('file',delay,rng,fgcolor,bgcolor,cmap)
%              [...]=ww3mov('file',delay,rng,fgcolor,bgcolor,cmap,ax)
%              [...]=ww3mov(s,...)
%
%    Description:
%     MOV=WW3MOV('FILE') creates a Matlab movie of the WaveWatch III
%     handcast data contained in the GRiB file FILE.  MOV is the movie
%     struct that can then be converted to an AVI file using MOVIE2AVI.
%     There is a 1/3 second delay between each frame by default (see next
%     Usage form to adjust this).  If FILE is omitted or is empty then a
%     GUI is presented for GRiB file selection.  If no output is assigned
%     then WW3MOV will "play" the data.
%
%     [MOV1,...,MOVN]=WW3MOV('FILE') returns the movies for each data type
%     in FILE (eg for wind data there is a movie for each component).
%
%     [...]=WW3MOV('FILE',DELAY) specifies the delay between the plotting
%     of each time step in seconds.  The default DELAY is 0.33s.
%
%     [...]=WW3MOV('FILE',DELAY,RNG) sets the colormap limits of the data.
%     The default is dependent on the datatype: [0 15] for significant wave
%     heights and wind speed, [0 20] for wave periods, [0 360] for wave &
%     wind direction, & [-15 15] for u & v wind components.
%
%     [...]=WW3MOV('FILE',DELAY,RNG,FGCOLOR,BGCOLOR) specifies foreground
%     and background colors of the movie.  The default is 'w' for FGCOLOR &
%     'k' for BGCOLOR.  Note that if one is specified and the other is not,
%     an opposing color is found using INVERTCOLOR.  The color scale is
%     also changed so the noise clip is at BGCOLOR.
%
%     [...]=WW3MOV('FILE',DELAY,RNG,FGCOLOR,BGCOLOR,CMAP) alters the
%     colormap to CMAP.  The default is HSV for wave & wind direction and
%     FIRE for everything else.  The FIRE colormap is adjusted to best
%     match the background color.
%
%     [...]=WW3MOV('FILE',DELAY,RNG,FGCOLOR,BGCOLOR,CMAP,AX) sets the axes
%     drawn in.  This is useful for subplots, guis, etc.  The default
%     creates a new figure.
%
%     [...]=WW3MOV(S,...) creates a movie using the WaveWatch III data
%     contained in the structure S created by WW3STRUCT.
%
%    Notes:
%     - Requires that the njtbx toolbox is installed!
%
%    Examples:
%     % Calling WW3MOV with no args lets you graphically choose a file:
%     mov=ww3mov;
%
%     % Save as an avi file:
%     movie2avi(mov,'filename.avi');
%
%     % Compress on linux (if you have mencoder available)
%     unixcompressavi('filename.avi');
%
%    See also: WW3MAPMOV, MOVIE2AVI, UNIXCOMPRESSAVI, WW3STRUCT, WW3REC,
%              WW3CAT, PLOTWW3, PLOTWW3TS, WW3MAP, WW3UV2SA, WW3BAZ2AZ

%     Version History:
%        June 15, 2010 - initial version
%        July  2, 2010 - adjusted oneliner description
%        Aug. 30, 2010 - fix documentation
%        Feb. 15, 2012 - use ww3struct, doc update
%        May   4, 2012 - allow struct input, fix no input case
%        Jan. 15, 2014 - updated See also list
%        Feb.  5, 2014 - doc update, update for colormap input
%
%     Written by Garrett Euler (ggeuler at wustl dot edu)
%     Last Updated Feb.  5, 2014 at 00:40 GMT

% todo:

% check nargin
error(nargchk(0,7,nargin));

% check ww3 input
if(nargin==0) % gui selection of grib file
    % attempt reading in first record of file
    % - this does the gui & checks file is valid
    s=ww3struct([],1);
    if(~isscalar(s))
        error('seizmo:ww3mov:badWW3',...
            'WW3MOV can only handle 1 file!');
    end
    read=true;
elseif(ischar(s)) % filename given
    % attempt reading in first record of file
    % - this does the gui & checks file is valid
    s=ww3struct(s,1);
    read=true;
elseif(isstruct(s))
    valid={'path' 'name' 'description' 'units' 'data' ...
        'lat' 'lon' 'time' 'latstep' 'lonstep' 'timestep'};
    if(~isscalar(s) || any(~ismember(valid,fieldnames(s))))
        error('seizmo:ww3mov:badWW3',...
            'S must be a scalar struct generated by WW3STRUCT!');
    end
    read=false;
else
    error('seizmo:ww3mov:badWW3',...
        'FILE must be a string!');
end

% get number of time steps
if(read)
    h=mDataset(fullfile(s.path,s.name));
    nrecs=numel(h{'time'}(:));
    close(h);
else
    nrecs=numel(s.time);
end

% check delay
if(nargin<2 || isempty(delay)); delay=0.33; end
if(~isreal(delay) || ~isscalar(delay) || delay<0)
    error('seizmo:ww3mov:badDelay',...
        'DELAY must be a positive scalar in seconds!');
end

% only make movie if output
makemovie=false;
if(nargout); makemovie=true; end

% make initial plot
ax=plotww3(ww3rec(s,1),varargin{:});
varargin{5}=ax;
fh=get(ax,'parent');
if(iscell(fh)); fh=cell2mat(fh); end
for j=1:numel(fh)
    if(makemovie); varargout{j}=getframe(fh(j)); end
end

% now loop over records and plot them
for i=2:nrecs
    pause(delay);
    if(read); t=ww3struct(fullfile(s.path,s.name),i);
    else t=ww3rec(s,i);
    end
    if(any(~ishghandle(ax,'axes')))
        error('seizmo:ww3mov:userClose',...
            'Axes disappeared! Did someone turn off the lights?');
    end
    plotww3(t,varargin{:});
    drawnow;
    for j=1:numel(fh)
        if(makemovie); varargout{j}(i)=getframe(fh(j)); end
    end
end

end
