function [cmin, cmax, c] = get_clev(nlev, minmax, varargin)
%--------------------------------------------------------------------------
% 
% get_clev.m -- 
% 
% Author          : Jaccard Pierre
% Created On      : 2000/09/06 08:55:33
% Last Modified By: Jaccard Pierre
% Last Modified On: 2000/09/07 07:25:35
% Directory       : /dataOS/jaccard/codas/codas3c/gfi/matlab/matlab50/
% ----------------------------------------------------------------------- +
% VERSION CONTROL: 
% 
%   $Author$
%   $Date$
%   $Id$
%   $Name$
%   $Revision$
%   $State$
%   $Log$
% 
%--------------------------------------------------------------------------

% Arguments

[ units ] = parsearg( varargin, ...
		'units' , [ 1, 2, 5]);

% The minimum of next larger range must be included into units. If not, a
% value of 9.9 with units [1,2,5] will be closer 5 instead of 10.

units = [ units, 10*min(units) ];

% Calculate space between levels and normalize it between 1 and 10

delta = (max(minmax) - min(minmax))/nlev;
nlog  = floor(log10(delta));
nd    = delta/(10^nlog);

% Find closest unit:
j  = Inf;
ic = -1;
for i=1:length(units),
	if abs(nd - units(i)) < j,
		ic = i;
		j = abs(nd - units(i));
	end
end

% Generate output variables
c     = units(ic) * (10^nlog);
i = floor(min(minmax)/c);
cmin = c * i;
i = ceil(max(minmax)/c);
cmax = c * i;

return;





