% demo_direct.m
%
% This is a simple example of using indexed colors in
% contourf plots, and adding colorbars. It will be improved.
%
% It illustrates the use of:
%     mk_cmap        to make any of several colormap types
%     cdata_direct   to change a contourf plot to direct mapping
%     cbar_direct       to make a corresponding colorbar
%
% Note: cdata_direct replaces cf_color.
%
% EF 2001/10/22


testdata1 = [-6:6]';
testdata2 = [-60:3:60]';

dd1 = repmat(testdata1, 1, 3);
dd2 = repmat(testdata2, 1, 3);

% Contourf levels: data ranges corresponding to each
% color.  Note the use of large and small values at
% the ends, so that all data values will fall between
% the boundaries.
clev1 = [-999 -4:2:4 999];
clev2 = [-999 -50:10:50 999];

nclev1 = length(clev1);
nclev2 = length(clev2);

% Make each colormap segment 1 shorter than the number
% of clev entries, so that there is one color for each
% clev *interval*.
% "jet" is a good general-purpose map.
cmap1 = mk_cmap(nclev1 - 1, 'jet');
% "hsv" really should be reserved for periodic quantities,
% such as angles, but we will use it here anyway.
cmap2 = mk_cmap(nclev2 - 1, 'hsv');
cmap3 = mk_cmap(64, 'jet'); % We will use only
                            %  selected indices of this one.

% Concatenate the colormap segments.
cmap = [cmap1; cmap2; cmap3];

% Here are the indices into the color map corresponding
% to the two sets of contour level ranges.
indices1 =  1:size(cmap1, 1);
indices2 =  indices1(end) +  (1:size(cmap2, 1));
% And here are all the indices into cmap3.
indices3 =  indices2(end) + (1:size(cmap3, 1));

clf
colormap(cmap)

subplot(2,2,1)
[cmat1, h1] = contourf(dd1, clev1);
set(h1, 'EdgeColor', 'none');
% In the following calls to cdata_direct and cbar_direct,
% we could use indices1 instead of indices1(1); the
% result would be identical.
cdata_direct(h1, clev1, indices1(1));
hcb1 = cbar_direct(clev1, indices1(1), 'vert', 'data 1');
set(findobj(hcb1, 'Type', 'patch'), 'EdgeColor', 'none');
title('Color contours, no edges')


subplot(2,2,2)
[cmat2, h2] = contourf(dd2, clev2);
cdata_direct(h2, clev2, indices2(1));
hcb2 = cbar_direct(clev2, indices2(1), 'vert', 'data2');
% Thin out the colorbar ticks and labels.
ticks = get(hcb2, 'YTick');
set(hcb2, 'YTick', ticks(2:4:end));
title('Color areas with black edges, hsv map')

subplot(2,2,3)
% Re-use data set 1.
[cmat3, h3] = contourf(dd1, clev1);
% Now let's reverse the order of the colors, compared to
% the first plot. We use a vector as the third argument,
% instead of a scalar.
cdata_direct(h3, clev1, indices1(end:-1:1));
hcb3 = cbar_direct(clev1, indices1(end:-1:1), 'v', 'data 1');
title('Same data and map as top plot, but reverse the colors')

% The following example uses indices in a somewhat trickier
% way, to produce an effective colormap segment that is a
% subset of the colors in an actual segment.  We use double
% indexing--indices into the array of indices that define
% a colormap segment.
subplot(2,2,4)
[cmat4, h4] = contourf(dd2, clev2);
set(h1, 'EdgeColor', 'none');
% Let's use every fourth color from first to last, but
% with a gap in the middle to separate positive from
% negative.
n_colors_needed = length(clev2) - 1;
n_colors_avail = length(indices3);
% Generate the indices into the third colormap segment--that is,
% indices into the indices3 array.
ii = 1:4:(n_colors_needed * 4);
ii((n_colors_needed/2 + 1):end) = ii((n_colors_needed/2 + 1):end) ...
                                 - ii(end) + n_colors_avail;
cdata_direct(h4, clev2, indices3(ii));
hcb4 = cbar_direct(clev2, indices3(ii), 'vert', 'data 2');
set(findobj(hcb1, 'Type', 'patch'), 'EdgeColor', 'none');
title('Subset of a large colormap segment')

%% Note: making a long colormap segment (e.g., the default jet(64))
%  means that the first and last colors are quite different
%  (darker, in the case of jet) than the first and last
%  colors of a short map; compare the first and last colors
%  from the upper left and lower right panels, both of which
%  are made using "jet", and both of which use the first and
%  last colors.


% Put all the axes components--box, ticks, labels--on top.
% This is done automatically for the colorbar axes.
% It should probably be the default for the all axes
% objects, but it is not.
set(findobj(gcf, 'Type', 'axes'), 'Layer', 'top')

orient tall
print -dpsc demo_direct.ps

