function h = cbar_direct(clev, i0, hor_vert, titlestr, shrink, varargin)
% function h = cbar_direct(clev, i0, hor_vert, titlestr, shrink, ... )
%
%  Makes an INDexed contourF ColorBAR;
%  See cf_color.m
%
%  outputs:
%     h is the handle of the new colorbar object
%
%  inputs:
%     clev:  Vector of contour levels (boundaries) *used* in
%               the call to contourf.  It should start and
%               end with extremes, so that all data fall
%               within its limits.
%     i0:    Index in the colormap where the first range in
%               clev will fall.  Optional; default is 1.
%               Other values are used to pick a section of
%               a concatenated colormap.
%           Or, if i0 is a vector, it will be interpreted
%               directly as the set of indices in the colormap
%               that correspond to the ranges in clev.  In
%               this case, you must have
%               length(i0) = length(clev) - 1
%     hor_vert: orientation: default (i.e. missing) is vertical
%                  options are 'horiz' and 'vert'
%     titlestr: a label for the X or Y axis, as appropriate
%     shrink:   [sw sl], where sw is the thickness of the colorbar
%        as a fraction of what the colorbar command produces,
%        and sl is the length, also as a fraction of the original.
%        Default is [0.25, 0.7].  Default is set if shrink = [].
%        Alternative: [left bottom width height]; if shrink
%        is a 4-element vector, it will be interpreted as
%        an axes object 'Position' specification.  Units are
%        still the Matlab default, normalized.
%        Alternative: string 'Inches', followed by a 4-element
%        position vector [left bottom width height].
%        With this form, you will get a colorbar axes object
%        positioned in inches as specified, with no change to
%        any other axes object in the figure.
%     Any arguments following shrink (or the corresponding pair,
%        'Inches', [l b w h]) will be treated as
%        axes property-value pairs, and will be passed to
%        the colorbar axes object without checking.  Note that
%        to use this option, you must specify something for each
%        of the previous arguments.  titlestr can be '' and
%        shrink can be [] if you don't want them to do anything.
%
%  Note: like the Matlab colorbar command, cbar_direct *assumes*
%        that you want your colorbar below or to the side of
%        the current axes object, which it resizes and repositions
%        to make room for the colorbar.
%
%
%  Eric Firing  99/07/21
%  EF 2001/10/09  fixed the documentation
%     2001/10/22  added note; fixed bug in main axes resizing
%                 that showed up if 'Units' were not 'normalized'.
%     2002/01/11  added the 'Inches', [l,b,w,h] version of "shrink"
%


% Argument checking ------------------
if (nargin<1)
        help cbar_direct
        error('At least one argument is needed.')
end


% defaults ------------------------------
if nargin < 5,
   shrink = [];
end
if isempty(shrink), shrink = [0.25 0.7]; end
if nargin < 4,
   titlestr = '';
end
if nargin < 3,
   hor_vert = 'vert';
end
if nargin < 2,
   i0 = 1;
end

independent = 0;
if ischar(shrink),
   if isempty(varargin) | ~strncmp(shrink, 'Inches', 4),
      help cbar_direct
      error('Check shrink argument.')
   end
   independent = 1;
   pos_cbar = varargin{1};
   varargin(1) = [];
end


if hor_vert(1) == 'v',
   vert = 1;
elseif hor_vert(1) == 'h',
   vert = 0;
else
   help cbar_direct
   error(['orientation string given as ' hor_vert ...
         ' must start with h or v '])
end
% No checking of fourth argument: title string.
if ~ischar(shrink) & length(shrink) ~= 2 & length(shrink) ~= 4,
   help cbar_direct
   error('shrink (5th argument) must have 0, 2, or 4 elements.')
end



ha_main = gca;
if independent,
   ha_cbar = axes('Units', 'inches', 'Position', pos_cbar);
else
   %--------
   units = get(ha_main, 'units');
   set(ha_main, 'Units', 'normalized');
   pos = get(ha_main, 'Position');

   if vert == 1,
      stripe = 0.075; edge = 0.02; space = 0.05;
      new_pos = [pos(1) pos(2) pos(3)*(1-stripe-edge-space) pos(4)];
      pos_cbar = [pos(1)+(1-stripe-edge)*pos(3) pos(2) stripe*pos(3) pos(4)];
      % Above is Matlab positioning by colorbar.m; now adjust.
      pos_cbar = [pos_cbar(1), pos_cbar(2)+ 0.5*(1-shrink(2))*pos_cbar(4), ...
                  shrink(1)*pos_cbar(3), shrink(2)*pos_cbar(4)];
   else
      stripe = 0.075; edge = 0.0; space = 0.1;
      new_pos = [pos(1) pos(2)+(stripe+space)*pos(4) pos(3) (1-stripe-space)*pos(4)];
      pos_cbar = [pos(1) pos(2) pos(3) stripe*pos(4)];
      % Again, adjust.
      pos_cbar = [pos_cbar(1)+  0.5*(1-shrink(2))*pos_cbar(3), ...
                  pos_cbar(2)+ (1-shrink(1))*pos_cbar(4), ...
                  shrink(2)*pos_cbar(3), shrink(1)*pos_cbar(4)];
   end

   %---------

   set(ha_main, 'Position', new_pos);
   set(ha_main, 'Units', units);

   ha_cbar = axes('Position', pos_cbar);
end
set(ha_cbar, 'Tag', 'cbar_direct');

dclev = diff(clev);

marg = 0.5 * (dclev(2) + dclev(end-1));
trange = [ (clev(2)-marg), (clev(end-1) + marg)];

dT = diff(trange);

Y = [0;1];
X = trange(1):dT/100:trange(end);

if vert == 1,
   xx = X;
   X = Y.';
   Y = xx.';
   Z = Y(:, [1 1]);
   axis([0 1 trange]);
else
   Z = X([1 1], :);
   axis([trange 0 1]);
end

[cs, hs] = contourf(X, Y, Z, clev);
%cf_color(hs, clev, i0);
cdata_direct(hs, clev, i0);
set(ha_cbar,'TickDir', 'out', 'Layer', 'top')

if vert == 1,
   set(ha_cbar, 'XTick',[], 'YAxisLocation','right');
   set(ha_cbar, 'YTick', clev(2:(end-1)));
   ylabel(titlestr);
else
   set(ha_cbar, 'YTick',[]);
   set(ha_cbar, 'XTick', clev(2:(end-1)));
   xlabel(titlestr);
end

% Axes properties set via remaining input args:
if ~isempty(varargin)
   set(ha_cbar, varargin{:});
end

axes(ha_main);

h = ha_cbar;     % output argument

