function ovlay(figs,tsize,cols,marksize,styles,lwidth,iigrid,iisub,iivg)
%
% Call ovlay(figs,tsize,cols,marksize,styles,lwidth,iigrid,iisub,iivg)
%
% figs      = figure numbers to overlay;
% tsize     = plot dimensions [width,heigth] ([]=size of existing plot);
% cols      = colors to loop through ([]=keep same ones; default='rbgmck');
% marksize  = size of marker symbols ([]=keep same ones; 0 for line);
% style     = line styles ([]=keep same ones; default='-');
% lwidth    = line widths ([]=keep same ones; default=1.5);
% iigrid    = overlay grid;
% iisub     = 1 to subtract two plots (or make set a figure number negative);
% iivg      = 1 for viewgraph font and font size;
%
if nargin<1, figs=[]; end
if nargin<2, tsize=[]; end
if nargin<3, cols='rbgmck'; end
if nargin<4, marksize=[]; end
if nargin<5, styles='-'; end
if nargin<6, lwidth=1.5; end
if nargin<7 | isempty(iigrid), iigrid=1; end;
if nargin<8 | isempty(iisub), iisub=0; end
if nargin<9 | isempty(iivg), iivg=1; end
%
if isempty(figs),
	figs=str2num(input('Enter figure numbers ','s'));
    if length(figs==1) & figs(1)<0, figs=[gcf+figs(1)-1:gcf], end
	if isempty(figs), figs=[gcf-1,gcf]; end
end
if any(figs<0), iisub=1; figs=abs(figs); end
%
nax=0; ax=NaN*ones(length(figs),4);
ntext=0; nfig=length(figs);
for jfig=1:nfig,
    figure(figs(jfig));
    ax(jfig,:)=axis;
    nax=nax + 1;
    if isempty(tsize), pos=get(gca,'Position'); tsize=pos(3:4); end
    ch=get(gca,'Children');
	if jfig==1,
		xlab=get(get(gca,'Xlabel'),'String');
		ylab=get(get(gca,'Ylabel'),'String');
        back_col=get(gca,'Color');
	end
    nline=0; 
    for jch=length(ch):-1:1,        % Go in reverse order to keep line plots the same;
		parms=get(ch(jch));
		if strcmp(parms.Type,'line') & length(parms.XData)>0,
			nline=nline + 1;
			D(jfig,nline).XData=parms.XData;
            D(jfig,nline).YData=parms.YData;
            D(jfig,nline).Color=parms.Color;
            D(jfig,nline).LineStyle=parms.LineStyle;
            D(jfig,nline).LineWidth=parms.LineWidth;
            D(jfig,nline).Marker=parms.Marker;
            D(jfig,nline).MarkerEdgeColor=parms.MarkerEdgeColor;
            D(jfig,nline).MarkerFaceColor=parms.MarkerFaceColor;
            D(jfig,nline).MarkerSize=parms.MarkerSize;
%		elseif strcmp(parms.Type,'line'),
		end
		if strcmp(parms.Type,'text') & ~iscell(parms.String),
			ntext=ntext + 1;
			TEXT.str{ntext}=parms.String;
            if ntext==1, 
                TEXT.ver=parms.VerticalAlignment;
                TEXT.hor=parms.HorizontalAlignment;
                TEXT.pos=parms.Position;
            else,
                TEXT.pos=max(TEXT.pos,parms.Position);
            end
%		elseif strcmp(parms.Type,'line'),
		end
	end
    nline_fig(jfig)=nline;
end
%
% Plot lines all on one figure:
ncol=length(cols); nsty=length(styles); nwid=length(lwidth); nm=length(marksize);
if iisub,
% Subtract plot 1 - plot 2:
    if nfig~=2 | length(D(1,1).YData)~=length(D(2,1).YData), 
        disp('ERROR: YData of first two lines not same size: ');
        disp(size(D(1,1).YData)); disp(size(D(2,1).YData));
    end
    if ~all(nline_fig==1), disp('Warning: Subtraction expected nline=1 on each figure!!!'); end
    D(1,1).YData=D(1,1).YData - D(2,1).YData;
    nline_fig=[1 0]; ax(1,3)=min(D(1,1).YData); ax(1,4)=max(D(1,1).YData);
    if ntext>0, TEXT.pos(2)=ax(1,3); end
end
figure
jj=0;
for jfig=1:nfig,
	for jline=1:nline_fig(jfig),
%        disp([jfig,jline,nline_fig(jfig),length(D(jfig,jline).XData)]);
        jj=jj + 1;
        if ncol==0, col=D(jfig,jline).Color; else, jcol=mod(jj-1,ncol) + 1; col=cols(jcol); end
        if nsty==0, sty=D(jfig,jline).LineStyle; else, jsty=mod(jj-1,nsty) + 1; sty=styles(jsty); end
        if sty=='d', sty='-:'; end
        if nwid==0, lwid=D(jfig,jline).LineWidth; else, jwid=mod(jj-1,nwid) + 1; lwid=lwidth(jwid); end
        if nm==0, msize=D(jfig,jline).MarkerSize; else, jmark=mod(jj-1,nm) + 1; msize=marksize(jmark); end
        if msize==0, marker='none'; msize=5; else, marker=D(jfig,jline).Marker; end
		plot_cfill(D(jfig,jline).XData,D(jfig,jline).YData,marker,col,msize,sty,lwid);
		hold on;
	end
end
%
if iisub==0,
	ii=[1:nax];
	ax_out=[min(ax(ii,1)),max(ax(ii,2)),min(ax(ii,3)),max(ax(ii,4))];
	axis(ax_out);
else
    axis('tight');
end
xlabel(xlab); ylabel(ylab); set(gca,'Color',back_col);
if iivg,
	plot_set3(gca,2,'Arial',14,1);
else
	plot_set3(gca,1,'Times',11,0);
end
plot_size(gca,'in',tsize(1),tsize(2));
if iigrid, grid on; end
if ntext>0,
    text(TEXT.pos(1),TEXT.pos(2),TEXT.str,'VerticalAlignment',TEXT.ver,'HorizontalAl',TEXT.hor);
end
zoom on;
%
% 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.
