function [jyx,ymax,xmax,zmax,dmax]=quad_peak_2d(iifig,iinn,yax,xax,z,data1)
%
% Call [jyx,ymax,xmax,zmax,dmax]=quad_peak_2d(iifig,iinn,yax,xax,z,data1);
%
ny=length(yax(:)); nx=length(xax(:)); 
if all(size(z) ~= [ny,nx]), 
	disp(['User error: bad sizes for yax,xax,z ',int2str([ny,nx,size(z)])]);
	return
end
%
[zmax0,ii]=max(z(:));
iiy=mod(ii-1,ny) + 1;
iix=floor((ii-1)/ny) + 1;
iiym=[iiy-1:iiy+1]; 
if iiym(1)<1, iiym(1)=ny; end
if iiym(3)>ny, iiym(3)=1; end
iixm=[iix-1:iix+1]; 
if iixm(1)<1, iixm(1)=nx; end
if iixm(3)>nx, iixm(3)=1; end
%
zmat=z(iiym,iixm);
cc=zmat(2,2);
a1=.5*(zmat(2,3)+zmat(2,1)) - cc;
b1=.5*(zmat(2,3)-zmat(2,1));
a2=.5*(zmat(3,2)+zmat(1,2)) - cc;
b2=.5*(zmat(3,2)-zmat(1,2));
d33=zmat(3,3) - (a1+a2+b1+b2+cc);
d11=zmat(1,1) - (a1+a2-b1-b2+cc);
d31=-zmat(3,1) + (a1+b1+cc+a2-b2);
d13=-zmat(1,3) + (a1-b1+cc+a2+b2);
dd=.25*(d33+d11+d31+d13);
%
den=dd^2 - 4*a1*a2;
xmax=(2*a2*b1 - b2*dd)/den;
ymax=(2*a1*b2 - b1*dd)/den;
zmax=a1*xmax^2 + b1*xmax + a2*ymax^2 + b2*ymax + dd*xmax*ymax + cc;
% jyx contains interpolate indices of peak;
jyx=[iiy + ymax,iix + xmax];
% Convert from unit coordinate system to actual:
dx=xax(2)-xax(1);
dy=yax(2)-yax(1);
%xmax,ymax,iix,xax(iix),xmax*dx,dx,
xmax=xax(iix) + xmax*dx;
ymax=yax(iiy) + ymax*dy;
% TEMP:
%%xmax=xax(iix); ymax=yax(iiy);
%xmax,ymax,
%
if nargin>5,
	if ~all(size(data1) == [ny,nx]), 
		disp(['User error: bad sizes for yax,xax,data1 ',int2str([ny,nx,size(data1)])]);
		return
	end
	if iinn,
%        if iiy>ny | iix>nx, keyboard; end
		dmax=data1(iiy,iix);
	else
		zmat=data1(iiym,iixm);
		cc=zmat(2,2);
		a1=.5*(zmat(2,3)+zmat(2,1)) - cc;
		b1=.5*(zmat(2,3)-zmat(2,1));
		a2=.5*(zmat(3,2)+zmat(1,2)) - cc;
		b2=.5*(zmat(3,2)-zmat(1,2));
		d33=zmat(3,3) - (a1+a2+b1+b2+cc);
		d11=zmat(1,1) - (a1+a2-b1-b2+cc);
		d31=-zmat(3,1) + (a1+b1+cc+a2-b2);
		d13=-zmat(1,3) + (a1-b1+cc+a2+b2);
		dd=.25*(d33+d11+d31+d13);
%
		den=dd^2 - 4*a1*a2;
		dmax=a1*xmax^2 + b1*xmax + a2*ymax^2 + b2*ymax + dd*xmax*ymax + cc;
	end
else
	dmax=[];
end
%
if iifig,
	figure(101); clf;
	imagesc(xax,yax,z); hold on;
	plot_cfill(xmax,ymax); colormap(jet);
    disp([xmax,ymax,zmax,dmax]);
	keyboard;
end
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
