function [q,h]  = plotframe2ez(amrdata,mq,plottype,sdir,sval,...
    cvalues,uservariablefile,mapc2pfile)

% PLOTFRAME2EZ for plotting simple 2d plots
%
%   PLOTFRAME2EZ is a functional version of plotframe1 and can be used in
%   conjunction with other 2d and 3d plotting routines without overwriting
%   global variables from those routines.
%
%   [Q,X] = PLOTFRAME2EZ(AMRDATA,MQ,PLOTTYPE,USERVARIABLEFILE,MAPC2PFILE)
%   plots the data stored in AMRDATA, using the variables MQ,PLOTSTYLE,
%   USERVARIABLEFILE and MAPC2PFILE to specify the type of plot to show.
%   AMRDATA is created with the READAMRDATA command, and is a structure
%   containing grid information.  See READAMRDATA.  MQ is a scalar which
%   specifies which variable (i.e. column) of the amrdata.data matrix to
%   read.  USERVARIABLEFILE is a string specifing any user defined function
%   which the data is passed through.  It has the syntax :
%
%             data = amrdata.data';
%             q = feval(UserVariableFile,data);
%
%   If USERVARIABLEFILE is the empty string ('') then q is set to data(:,mq).
%
%   Finally, if the user has set mappedgrid = 1, then a function 'mapc2p' is
%   called to map the Cartesian data to physical data.  MAPC2P should
%   be the name of a function on the Matlab path with the syntax
%
%         [xp,yp] = mapc2p(xc,yc)
%
%    NOTE : It is not assumed that this function has the name 'mapc2p',
%    because this would conflict with the mapc2p used for any 3d
%    routines.
%
%    PLOTFRAME2EZ returns the 2d Q, X and Y data.
%
%   [Q,X,Y] = PLOTFRAME2EZ(AMRDATA,MQ,PLOTSTYLE) does not use a
%        user variable file or a mapped grid file.
%
%   [Q,X,Y] = PLOTFRAME2EZ(AMRDATA,MQ) sets PLOTSTYLE = 'r-';
%
%   [Q,X,Y] = PLOTFRAME2EZ(AMRDATA) sets MQ = 1, PLOTSTYLE = 'r-'
%
%   [Q,X,Y,P] = PLOTFRAME2EZ(....) returns a handle P to the 1d line plot.
%
%    Example :
%
%        % To call this routine from an afterframe.m file
%        % ... <afterframe>...
%        hold on;
%        dir = './results1d/';
%        dim = 1;
%        [amrdata1d,t1d] = readamrdata(dim,Frame,dir);
%        userfile = '';  % UserVariableFile
%        mapc2pfile = 'mapc2p1d';
%        [q1d,x1d] = plotframe1ez(amrdata1d,mq,'b-',userfile,mapc2pfile);
%        hold off;
%
%    See also SETPLOT, MappedGrid, READAMRDATA.

if (length(amrdata) == 0)
  disp('*** plotframe2ez : Input ''amrdata'' is empty');
  q = [];
  xc = [];
  yc = [];
  h = [];
  return;
end;

% if (length(amrdata) > 1)
%   disp('*** plotframe2ez : length(amrdata) > 1; plotframe2ez does not ');
%   disp('*** support multiple grids yet.');
%   return;
% end;

if (nargin < 8)
  mapc2pfile = '';
  if (nargin < 7)
    uservariablefile = '';
    if (nargin < 6)
      cv = [];
      if (nargin < 5)
	error('plotframe2ez : not enough arguments supplied');
      end;
    end;
  end;
end;
uservariable = ~isempty(uservariablefile);
mappedgrid = ~(strcmp(mapc2pfile,'') == 1);

% ------------------------------------------------------------
% This code was taken from plotframe2.m
% ------------------------------------------------------------

qmin = [];
qmax = [];
ncells = [];


% ---------------------------------------------------------
% Check to make sure that slices have been set up right
% ---------------------------------------------------------

% assume that the slice is added to slice 1
slices = get_slices(sdir);

if (length(slices) == 0)
  error('You must have at least one slice in direction ''sdir''');
end;

% Pick a slice; I don't think it matters, since we set sval below.
slice = slices{1};
ngrids3d = 0;
for level = 1:length(slice),
  pvec = slice{level};
  ngrids3d = ngrids3d + length(pvec);
end;

ngrids = length(amrdata);  % length of structure array
for ng = 1:ngrids,

  gridno = amrdata(ng).gridno + ngrids3d;  % WARNING!!!
  level = amrdata(ng).level;

  % Set block number for multi-block calculations.
  set_blocknumber(gridno);

  mx = amrdata(ng).mx;
  my = amrdata(ng).my;

  xlow = amrdata(ng).xlow;
  ylow = amrdata(ng).ylow;

  dx = amrdata(ng).dx;
  dy = amrdata(ng).dy;

  xedge = xlow + (0:mx)*dx;
  yedge = ylow + (0:my)*dy;

  xcenter = xedge(1:mx) + dx/2;
  ycenter = yedge(1:my) + dy/2;

  % for compatibility with old matlab41/plotframe2 convention:
  x = xcenter;
  y = ycenter;

  % read q data:
  data = amrdata(ng).data;

  data = data';

  if (uservariable)
    % User has supplied a function to convert original q variables to
    % the variable which is to be plotted, e.g. Mach number, entropy.
    qdata = feval(uservariablefile,data);
    q = reshape(qdata,mx,my);
  else
    q = reshape(data(:,mq),mx,my);
  end

  amrdata(ng).q = q;

  % q must be permuted so that it matches the dimensions of xcm,ycm,zcm
  % created by meshgrid.
  if (plottype == 3)
    % Schleieren plot;  we plot the gradient, not the values.
    [qx,qy] = gradient(q,dx,dy);
    qs = sqrt(qx.^2 + qy.^2);
    qmesh = qs';
  else
    qmesh = q';
  end;

  % minimum over all grids at this time, but not necessarily on slice
  % shown.
  qmin = min([qmin,min(min(q))]);
  qmax = max([qmax,max(max(q))]);

  % keep count of how many cells at this refinement level:
  if length(ncells) < level
    ncells(level) = 0;
  end;
  ncells(level) = ncells(level) + mx*my;

  % -----------------------------------------------
  % plot commands go here
  if plottype <= 3

    % Add amr patch or manifold into current plot.
    % sval = 0;  % is an input value
    zcenter = [sval sval];
    zedge = [sval sval];
    sdir = 'z';
    qmesh = qmesh;
    snum = 1;   % only one slice in 2d plot
    % only mask patches underneath if we are plotting a Manifold
    manifold = false;
    maskflag = true;  % since we are viewing in 3d.
    add_patch2slice(sdir,sval,snum,xcenter,ycenter,zcenter, ...
	xedge,yedge,zedge,qmesh,level,...
	cvalues,mappedgrid,manifold,maskflag,ng);
  end;  % end of plotting for PlotType == 3

end % loop on ng (plot commands for each grid)

% --------------------------------------------------------------------
% end of code taken from plotframe2.m
% --------------------------------------------------------------------


if (nargout == 2)
  % send out plot handle.
  h = p;
end;
