function [x,y,npt]=graph_input(tsize,xlim,ylim,xlab,ylab,iiij,iinewfig,dotcol)
%
% Call [x,y]=graph_input(tsize,xlim,ylim,xlab,ylab,iiij,iinewfig,dotcol);
%
% tsize 		= [width,height] of plot;
% xlim,ylim		= [xmin,xmax] of plot;
% xlab,ylab		= x,y labels
% iiij			= 1 for vertical axis inverted;
%
% Uses ginput to get a function y(x).
%
if nargin<6, iiij=0; end
if nargin<7, iinewfig=1; end
if nargin<8, dotcol='r'; end
%
if iinewfig, figure; else; hold on; end
%
if ~isempty(xlim) & ~isempty(ylim),
    xmin=min(xlim); xmax=max(xlim); ymin=min(ylim); ymax=max(ylim);
    plot(xmin,ymin); hold on; plot(xmax,ymax);
    grid on;
else
    ax=axis; xmin=ax(1); xmax=ax(2); ymin=ax(3); ymax=ax(4);
end
if ~isempty(xlab), xlabel(xlab); end
if ~isempty(ylab), ylabel(ylab); end
if ~isempty(iiij),
    if iiij==1, axis('ij'); elseif iiij==0, axis('xy'); end;
end
if ~isempty(tsize),
    plot_set3(gca,1.5,'Arial',14,1);
    plot_size(gca,'in',tsize(1),tsize(2));
end
%
disp(' ');
if ~isempty(xlab) | ~isempty(ylab),
    disp(['Enter pairs of (',xlab,',',ylab,') points followed by <CR>']);
else
    disp(['Enter pairs of points followed by <CR>']);
end
disp(['   Press d or D instead of mouse to delete a point.']);
disp(['   Press n or N instead of mouse to enter a NaN point.']);
disp(['   To increase range of plot box, click outside plot area.']);
disp(' ');
%
done=0; npt=0; x=zeros(100,1); y=x; hp=[];
while done==0
	[xi,yi,button]=ginput(1);
    xy=[xi,yi];
	if isempty(button),                 % <CR> to end input;
		done=1;
    elseif any(button==[68,100]),       % 'd' or 'D' to delete closest point;
        [dmin,ndel]=min(sqrt((x(1:npt)-xi).^2 + (y(1:npt)-yi).^2));
        if ~isempty(ndel), 
            iik=[1:ndel-1,ndel+1:npt]; npt=npt-1;
            x(1:npt)=x(iik); y(1:npt)=y(iik);
        end
    elseif any(button==[78,110]),             % 'n' or 'N' to enter NaN;
        npt=npt + 1;
        x(npt)=xy(1); y(npt)=NaN;
    elseif button==1,
		iich=0;
		if xy(1)<xmin, xmin=xmin - (xmax-xmin); iich=1; end
		if xy(1)>xmax, xmax=xmax + (xmax-xmin); iich=1; end
		if xy(2)<ymin, ymin=ymin - (ymax-ymin); iich=1; end
		if xy(2)>ymax, ymax=ymax + (ymax-ymin); iich=1; end
		if iich, 
			axis([xmin,xmax,ymin,ymax]); 
		else
			npt=npt+1;
			x(npt)=xy(1); y(npt)=xy(2);
		end
	end
    if ~isempty(hp); delete(hp); end
	hp=plot_cfill(x(1:npt),y(1:npt),'o',dotcol,8,'-',2);
end
x=x(1:npt); y=y(1:npt);
	
%
% 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.
