function hbar=cbar(h, ostring, dcbar, dlabel,vk, ...
 p1, v1, p2, v2, p3, v3, p4, v4, p5, v5)
%function hbar=cbar(h, ostring, dcbar, dlabel)
% Create a reference color bar for plot with handle h.  Parameter dcbar
% gives the units per color bar block.  Label the color bar every dlabel 
% units (dlabel must be n*dcbar where n is an integer).  ostring is a string
% which determines orientation of the colorbar, 'h' for horizontal, 
% 'v' for vertical.
%
% Return a handle hbar, to the colorbar.
%
% You can add up to 5 property values for the colorbar:
% e.g. hbar = cbar(gca, 'v',dcbar, dlabel, 'fontsize', 10);

% The function works by shrinking the width of axes(h) and slapping
% the colorbar on the right hand side of axes(h).
% Warning: This routine will change the aspect ratio of axes(h).

% ClH 3/29/95 Matlab 4.1
%
% modified by:
% DAF 3/30/95 Matlab 4.2c adding option for putting colorbar along 
%     either axis
%

%..........................................................................
% Error trapping.

if nargin < 5
  error('COLORBAR: Incorrect number of input arguments');
end

nprop = (nargin-5)/2;
if (nprop - fix(nprop))
  error('COLORBAR: Incorrect number of property value arguments');
end

dblocktic = dlabel/dcbar;

if (dblocktic - round(dblocktic))
  error('COLORBAR: dlabel must be an integer multiple of dcbar');
end
%..........................................................................
% Set 'x' or 'y' prefix for axes properties 
% based on choice of colorbar orientation (eg, 'xtick' vs 'ytick')
%
if (ostring=='v')
   alab = 'y';
   alab2 = 'x';
elseif (ostring=='h')
   alab = 'x';
   alab2 = 'y';
else
   error('COLORBAR: ostring must be a string of either ''h'' or ''v''');
end

%..........................................................................
% Get the position of the figure and make the figure smaller to be able to
% add the colorbar.  

h1 = gca;                % will reset gca to h1 at end of routine

pos = get(h, 'position');
%[cmin, cmax] = caxis;    % Get color limits on this figure.
caxis([min(vk) max(vk)]);    % Get color limits on this figure.
[cmin, cmax] = caxis;
cbfac = 20;          % Let width of colorbar be 1/20 of width/length of figure.

if alab=='y'
   wcb = pos(3)/cbfac;         % width of color bar
   scb = pos(3)/(.5*cbfac);   % Let x space before color bar be 2wcb. 
else
   wcb = pos(4)/cbfac;      % height of color bar
   scb = pos(4)/(cbfac*.5);   % set y space between bar and xlabels.
end

if (alab=='y')
  set(gca, 'position', [pos(1) pos(2) pos(3)-wcb-scb pos(4)]);
else
  set(gca, 'position', [pos(1) pos(2)+wcb+scb pos(3) pos(4)-wcb-scb]);
end

%..........................................................................
% Create the axis for the colorbar.  Set it to be invisible until the end
% so that it doesn't flash around while it's being fixed.

if alab=='y'
      hbar = axes('position', [pos(1)+pos(3)-wcb pos(2) wcb pos(4)], ...
   'visible', 'off');
else
      hbar = axes('position', [pos(1) pos(2) pos(3) wcb], ...
   'visible', 'off');
end

%..........................................................................
% Plot the colorbar.

v = cmin:dcbar:cmax;
if alab=='y'
  pcolor([1 2], v, [v;v]');
else 
  pcolor(v,[1 2],[v;v]);
end

%..........................................................................
% Set colorbar axes, ticks, etc.

% Set axes limits

eval(['set(hbar, ''',alab,'lim'', [cmin cmax]);'])

% Set axes ticks.  Put the tick labels on the center of the blocks.
eval(['set(hbar, ''',alab2,'tick'', [], ''',alab,'tick'', [cmin:dcbar:cmax]);'])
eval([alab,'ticlab = get(hbar, ''',alab,'ticklabels'');']);
eval(['set(hbar, ''',alab,'tick'', [cmin:dlabel:cmax],''' ...
   alab,'ticklabels'',',alab, ...
          'ticlab(1:dblocktic:length(',alab,'ticlab),:));']);

%..........................................................................
% Set the user properties.

if nprop
  str='';
  for k = 1:nprop-1
    str = [str,'p',num2str(k),',v',num2str(k),','];
  end
  str = [str,'p',num2str(nprop),',v',num2str(nprop)];
  eval(['set(hbar,',str,');']);
end

%..........................................................................
% All done.

set(hbar, 'visible', 'on');
axes(h1);                     % gca was set to h1 when routine was called

%..........................................................................
%..........................................................................


