function dc=cntoura(x,y,a,flag,units,ci)
% DUM=CNTOURA(X,Y,A,FLAG,UNITS,CI)
% make contour plot of A, as a function of X and Y
% negative contours have dashed lines,
% same as cntourx but with automatic determination of contour intervals
%
% FLAG  0: do not mark contours
%       1: mark contours automatically
%       2: mark contours manually
%       3: mark contours manually, output contour interval
%       4: for subplot(22x)
%
% UNITS string containing units of contours
%
% CI    countour interval
%
% D. Menemenlis 1 apr 93
% modified from /data17/diana/Barry/barry7/mscripts/cntourx.m
% 8 mar 95 added FLAG and UNITS

% check arguments

if nargin < 3
  error('need at least three arguments')
elseif nargin < 4
  flag=1;                               % mark contours automatically
  units='';                             % no units
elseif nargin < 5
  if isstr(flag)
    units=flag;
    flag=1;
  else
    units='';
  end
end

% some variavle initialization

mm=minmax(a);
col='k';
if nargin > 5
  dc = ci;
else
  dc=(mm(2)-mm(1))/5;
end

% find a 1 digit number close to the desired contour interval
if dc~=0
  if nargin < 6
    dc=str2num(sprintf(' %.0e',dc));
  end
  ac1=-dc/2:-dc:mm(1);
  ac2=dc/2:dc:mm(2);
else
  ac1=0;
  ac2=[];
end

if length(ac1)==1, ac1=[ac1 ac1]; end
if length(ac2)==1, ac2=[ac2 ac2]; end

minc=inf; maxc=-inf;
if ~isempty(ac1),
  c=contour(x,y,a,ac1,[col,'--']);
  if ishold, holdflag=1;
  else, hold on, holdflag=0; end
  if ~isempty(c)
    if flag == 1
      clabel2(c)
    elseif flag == 2 | flag == 3
      clabel2(c,'manual')
    end
  end
  minc=min(ac1); maxc=max(ac1);
end
if ~isempty(ac2),
  c=contour(x,y,a,ac2,col);
  if ~holdflag, hold off, end
  if ~isempty(c)
    if flag == 1
      clabel2(c)
    elseif flag == 2 | flag == 3
      clabel2(c,'manual')
    end
  end
  minc=min([ac2 minc]); maxc=max([ac2 maxc]);
end

xy=axis;
%$$$ text(xy(1)-.08*(xy(2)-x(1)),xy(3)-.16*(xy(4)-xy(3)),...
%$$$     ['contour interval: ' num2str(dc)]);

str = ['contours from ' num2str(minc) ' to ' ...
        num2str(maxc) ' by ' num2str(dc) ' ' units];
if flag == 3
  dc = str;
elseif flag == 4        % for subplot(22x)
  
    set(gca,'fontsize',9),
    text ( 1, -.2, str, 'units', 'normalized', ...
      'HorizontalAlignment','right','fontsize',7);
  
else                    % pick location of contour infor automatically
  
  tmp=get (gca,'position');

  if tmp(4) > .8        % 1 vertical plot

    text ( 1, -.105, str, 'units', 'normalized', ...
        'HorizontalAlignment','right');

  elseif tmp(4) > .3    % 2 vertical plot

    text ( 1, -.22, str, 'units', 'normalized', ...
        'HorizontalAlignment','right','fontsize',8);

  elseif tmp(4) > .2    % 3 vertical plot

    set(gca,'fontsize',8),
    text ( 1, -.15, str, 'units', 'normalized', ...
        'HorizontalAlignment','right','fontsize',8);

  else                  % 4 vertical plot

    set(gca,'fontsize',8),
    text ( 1, -.15, str, 'units', 'normalized', ...
        'HorizontalAlignment','right','fontsize',8);

  end
end
