function plotCloudMaskSingleBit(cloudmask, byteBit, plotString, figNum, axLims)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% function plotCloudMaskSingleBit(cloudmask, byteBit, plotString, figNum, axLims)
%
%   Displays only the selected bit/byte pairs from the cloudmask
%   struct (read in with readModisCloudMask.m)
%
% REQUIRED Input:
%    cloudmask (struct)     MODIS cloud mask data read in by 
%                           readModisCloudMask.m
%    byteBit Array with column 1 for Byte # and column 2 for Bit #
%            and a row for each byte/bit combination to plot.
%            For fields with multiple bits, any one of the bits can 
%            be listed. 
%
% OPTIONAL Input:  
%    plotString String to include at the beginning of each 
%               figure title (usually, the date and time)
%
%    figNum     Figure number for first figure (default 1)
%    axLims     axis limits to display on plot:
%                  [minX maxX minY maxY] coordinates
%
% NOTE: To run all bytes at once is memory-intensive.  I can run 
%       bytes 1-4 at once, but run out of memory if I try to 
%       do 1-6 at once.  It is best to do bytes 5 & 6 in a separate
%       run of plotCloudMask.  Also, clear all other variables
%       and figures before running
%
% Sample run statements:
%   cloudmask = readModisCloudMask(...
%               'MOD35_L2.A2001048.1650.002.2001049201758.hdf', [1:4]);
%   plotCloudMask(cloudmask, [1 1])
%   plotCloudMask(cloudmask,[1 1; 2 3])
%   plotCloudMask(cloudmask,[1 2; 1 4],'2001 048 1650', 1)
%
% Suzanne Wetzel Seemann
% swetzel@ssec.wisc.edu
% April 2001
%
% update 24 April 2001: added optional input argument axLims
%                       created this new version (...SingleBit)
%                       to plot only one byte/bit 
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Error check inputs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 if (nargin == 2)
      plotString = ' ';
      figNum = 1;
 elseif nargin == 3
      figNum = 1;
 end

 byteList = unique(byteBit(:,1));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BYTE 1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 if any(byteList == 1)

   byteInd = find(byteBit(:,1) == 1);
   bits = byteBit(byteInd,2);

   if any(bits == 1 | bits == 2) 
     figure(figNum); %clf; 
        imagesc(cloudmask.byte1.mask);
        cmap = [1 0 0 ;1.0 1.0 1.0; 1.0 1.0 0; 0 0.8 0.4; 0 0 1.0];
        colormap(cmap); caxis([-1 3]); title([ plotString ' \newlineByte1, Bits 1 & 2: Cloud Mask']);
        c=colorbar; %a = get(gcf,'Children');
        set(c,'Units','pixels');
        set(c,'ytick',[-0.58 -0.45 0.3 1.0 1.8 1.95 2.6 2.75]);
        set(c,'yticklabel',{'Determined','Not','Cloudy','Uncertain','Clear','Probably','Clear','Confident'});
        set(c,'ticklength',[0 0]);
        set(gca,'YTick',[]); set(gca,'XTick',[]);
        if nargin > 4
           axis(axLims);
        end
     figNum = figNum + 1;
   end

     %figure(figNum); clf; imagesc(cloudmask.byte1.confidenceQA);
     %cmap = [0 0 0;1 0 0; 1 0.5 0; 1 1 0; 0 0.8 0.2; 0 0 1; 0.5 0 0.5; 1 1 1];       
     %colormap(cmap); caxis([0 7]); colorbar;
     %title([ plotString ' \newlineQA Byte1, Bits 1, 2, & 3: Cloud Mask Confidence QA']);
     %colorbar; a = get(gcf,'Children');
     %set(a(2),'ytick',[0.3 0.55 1.3 2.2 3.1 4.0 4.8 5.7 6.6]);
     %set(a(2),'yticklabel',{'Confident','Not','1','2','3','4','5','6','Confident'});
     %set(a(2),'ticklength',[0 0]);
     %set(gca,'YTick',[]); set(gca,'XTick',[]);
     %figNum = figNum + 1;
     %if nargin > 4
     %   axis(axLims);
     %end

   if any(bits == 6 | bits == 7) 
     figure(figNum); clf; imagesc(cloudmask.byte1.landWater);
     cmap = [1 0 0; 0 0 1; 1 1 1; 1 1 0; 0 0.8 0.4]; 
     colormap(cmap); caxis([-1 3]); title([ plotString '\newline Byte1, Bit 6: Land/Water']);
     c=colorbar; a = get(gcf,'Children');
     set(c,'Units','pixels');
     set(a(2),'ytick',[-0.65 -0.5 0.2 1.05 1.85 2.65]);
     set(a(2),'yticklabel',{'Useful','Not','Water','Coastal','Desert','Land'});
     set(a(2),'ticklength',[0 0]);
     set(gca,'YTick',[]); set(gca,'XTick',[]);
     figNum = figNum + 1;
     if nargin > 4
        axis(axLims);
     end
   end

   if any(bits == 0) 
     figure(figNum); clf; imagesc(cloudmask.byte1.flag);
     cmap = [1 0 0; 0 0 1]; colormap(cmap); caxis([0 2]); 
     title([ plotString ' \newlineByte1, Bit 0: Flag']);
     c=colorbar; a = get(gcf,'Children');
     set(c,'Units','pixels');
     set(a(2),'ytick',[0.38 0.55 1.5]);
     set(a(2),'yticklabel',{'Determined','Not','Determined'});
     set(a(2),'ticklength',[0 0]);
     set(gca,'YTick',[]); set(gca,'XTick',[]);
     if nargin > 4
        axis(axLims);
     end
   end

   if any(bits == 3) 
     figure(figNum); clf; imagesc(cloudmask.byte1.dayOrNight);
     cmap = [1 0 0; 1 1 1; 0 0 1]; colormap(cmap); caxis([-1 1]); 
     title([ plotString ' \newlineByte1, Bit 3: Day Or Night']);
     c=colorbar; a = get(gcf,'Children');
     set(c,'Units','pixels');
     set(a(2),'ytick',[-0.8 -0.65 0.05 0.7]);
     set(a(2),'yticklabel',{'Useful','Not','Night','Day'});
     set(a(2),'ticklength',[0 0]);
     set(gca,'YTick',[]); set(gca,'XTick',[]);
     if nargin > 4
        axis(axLims);
     end
   end

   if any(bits == 4) 
     figure(figNum); clf; imagesc(cloudmask.byte1.sunglint);
     cmap = [1 0 0; 1 1 1; 0 0 1]; colormap(cmap); caxis([-1 1]); 
     title([ plotString '\newline Byte1, Bit 4: Sunglint']);
     c=colorbar; a = get(gcf,'Children');
     set(c,'Units','pixels');
     set(a(2),'ytick',[-0.8 -0.65 0.05 0.7]);
     set(a(2),'yticklabel',{'Useful','Not','Yes','No'});
     set(a(2),'ticklength',[0 0]);
     set(gca,'YTick',[]); set(gca,'XTick',[]);
     if nargin > 4
        axis(axLims);
     end
   end

   if any(bits == 5) 
     figure(figNum); clf;imagesc(cloudmask.byte1.snowIce);
     cmap = [1 0 0; 1 1 1; 0 0 1]; colormap(cmap); caxis([-1 1]); 
     title([ plotString '\newline Byte1, Bit 5: Snow/Ice']);
     c=colorbar; a = get(gcf,'Children');
     set(c,'Units','pixels');
     set(a(2),'ytick',[-0.8 -0.65 0.05 0.7]);
     set(a(2),'yticklabel',{'Useful','Not','Yes','No'});
     set(a(2),'ticklength',[0 0]);
     set(gca,'YTick',[]); set(gca,'XTick',[]);
     figNum = figNum + 1;
     if nargin > 4
        axis(axLims);
     end
   end

 end % byte 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BYTE 2
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 if any(byteList == 2)

   byteInd = find(byteBit(:,1) == 2);
   bits = byteBit(byteInd,2);

     %% 23 April: Removed 'Adjacent Cloud Detected' field (bit 4) because
     %%           it is not used in the cloud mask
     byte2Labels = {'Non-cloud obstruction flag','Thin cirrus detected, solar', ...
     'Shadow found','Thin cirrus detected, IR', ' ',...
     'Cloud Flag, IR threshold','High cloud flag, CO2 test',...
     'High cloud flag, 6.7 \mum test'};

     for i = 1:length(bits)
	figure(figNum); clf;
	eval(['imagesc(cloudmask.byte2.bit' num2str(bits(i)) ');']);
        cmap = [1 0 0; 1 1 1; 0 0 0]; colormap(cmap); caxis([-1 1]); 
	title([plotString ' Byte 2, Bit ' num2str(bits(i)) '\newline' ...
	       char(byte2Labels(bits(i)+1))]);
	c=colorbar; a = get(gcf,'Children');
        set(c,'Units','pixels');
        set(a(2),'ytick',[-0.8 -0.65 0.04 0.7]);
        set(a(2),'yticklabel',{'Applied','Not','Yes','No'});
        set(a(2),'ticklength',[0 0]);
        set(gca,'YTick',[]); set(gca,'XTick',[]);
        if nargin > 4
           axis(axLims);
        end
	figNum = figNum + 1;
     end

  end % byte 2

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BYTE 3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 if any(byteList == 3)

   byteInd = find(byteBit(:,1) == 3);
   bits = byteBit(byteInd,2);
 
     %% 23 April: Removed '0.935/0.87 reflectance test', '3.7-3.9 \mum test'
     %%           fields(bits 6,7) because they are not used in the cloud mask
     byte3Labels = {'High cloud flag, 1.38 \mum test', ...
     'High cloud flag, 3.7-12 \mum test', ...
     'Cloud flag, IR temperature difference','Cloud flag, 3.7-11 \mum test',...
     'Cloud flag, visible reflectance test', ...
     'Cloud flag, visible reflectance ratio test', ' ', ' '};

     for i = 1:length(bits)
        figure(figNum); clf; 
	eval(['imagesc(cloudmask.byte3.bit' num2str(bits(i)) ');']);
        cmap = [1 0 0; 1 1 1 ;0 0 0]; colormap(cmap); caxis([-1 1]); 
	title([plotString ' Byte 3, Bit ' num2str(bits(i)) '\newline' ...
	       char(byte3Labels(bits(i)+1))]);
	c=colorbar; a = get(gcf,'Children');
        set(c,'Units','pixels');
        set(a(2),'ytick',[-0.8 -0.65 0.04 0.7]);
        set(a(2),'yticklabel',{'Applied','Not','Yes','No'});
        set(a(2),'ticklength',[0 0]);
        set(gca,'YTick',[]); set(gca,'XTick',[]);
        if nargin > 4
           axis(axLims);
        end
        figNum = figNum + 1;
     end

  end % byte 3

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BYTE 4
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 if any(byteList == 4)

   byteInd = find(byteBit(:,1) == 4);
   bits = byteBit(byteInd,2);

     %% 23 April: Removed 'Cloud flag, temporal consistency'
     %%           (bit 0) because it is not used in the cloud mask
     byte4Labels = {' ','Cloud flag, spatial variability', ...
     'Final confidence confirmation test', ...
     'Cloud flag, night water spatial variability', ...
     'Suspended dust flag'};

     for i = 1:length(bits)
        figure(figNum); clf; 
	eval(['imagesc(cloudmask.byte4.bit' num2str(bits(i)) ');']);
        cmap = [1 0 0; 1 1 1 ;0 0 0]; colormap(cmap); caxis([-1 1]); 
	title([plotString ' Byte 4, Bit ' num2str(bits(i)) '\newline' ...
	       char(byte4Labels(bits(i)+1))]);
	colorbar; a = get(gcf,'Children');
        set(a(2),'ytick',[-0.8 -0.65 0.04 0.7]);
        set(a(2),'yticklabel',{'Applied','Not','Yes','No'});
        set(a(2),'ticklength',[0 0]);
        set(gca,'YTick',[]); set(gca,'XTick',[]);
        if nargin > 4
           axis(axLims);
        end
        figNum = figNum + 1;
     end

  end % byte 4

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% BYTES 5 & 6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 if any(byteList == 5) | any(byteList == 6)

     figure(figNum); clf;  
     imagesc(cloudmask.visibleTest250m); hold on;  
     cmap = [1 0 0; 1 1 1; 0 0 0]; colormap(cmap); caxis([-1 1]); 
     title(['  ' plotString ' Bytes 5 & 6 \newline250m CloudFlag - Visible Tests']);
     colorbar; a = get(gcf,'Children');
     set(a(2),'ytick',[-0.7 -0.6 0 0.7]);
     set(a(2),'yticklabel',{'Applied','Not','Cloud','No Cloud'});
     set(a(2),'ticklength',[0 0]);
     set(gca,'YTick',[]); set(gca,'XTick',[]);
     figNum = figNum + 1;
     if nargin > 4
        axis(axLims);
     end

  %   figure(figNum); clf; imagesc(cloudmask.sumVisibleTest250m);  
  %   caxis([0 16]);
  %   colormap(fliplr(jet(16))); cc = colormap; 
  %   cc(1,:) = [1 1 1]; colormap(cc);
  %   colorbar; a = get(gcf,'Children');
  %   set(a(2),'ytick',[0 2:2:16]);
  %   set(a(2),'yticklabel',{'All Cloud','2','4','6','8','10','12','14','No Cloud'});
  %   set(a(2),'ticklength',[0 0]);
  %   set(gca,'YTick',[]); set(gca,'XTick',[]);
  %   title(['                 ' plotString ...
  %     ' Bytes 5 & 6 \newlineSum of all 250m CloudFlag Elements in each 1km -- no QA']);
  %   figNum = figNum + 1;
  %   if nargin > 4
  %      axis(axLims);
  %   end

  end % bytes 5,6

