function [imagedata,bandAvail,varargout]=plotband(filename,pathname,band,displayAxis,makeplot,vartoplot,latlon,doflipud,dofliplr,box)
% Gets data from a MODIS file and plots based on the band
% Input: filename,pathname,band to plot, axis to display, 
%   whether to make a plot or not (0 or 1 value in makeplot)
%   variable to plot (e.g. radiance,reflectance)
%   whether to get lat,lon or not (0 or 1 value for latlon)
%   whether to flip image up/down (0 or 1 value for doflipud)
%   whether to flip image left/right (0 or 1 value for dofliplr)
%   optional tenth argument to plot a region
% Output: imagedata
%         bandAvail flag - true if daytime (contrast available)
%         optional output arguments - latitude and longitude
% Kevin Baggett
% Thu Jul 26 18:13:00 UTC 2001
bandAvail=0;
if latlon
    varStr=[' ''latitude'', ''longitude'' ,' vartoplot];
else
    varStr=vartoplot;
end
if filename(4:5)=='02'
        if nargin~=10
            s=['imagedata = modis_level1b_read([pathname filename],band,{' varStr '});'];
            eval(s);
        else
            s=['imagedata = modis_level1b_read([pathname filename],band,{' varStr '},box);'];
            eval(s);
        end
        visible=[1:19,26];
        if makeplot
			cc=colormap(gray);
            if ~ismember(band,visible)
                %flip colormap if infrared
				cc=(flipud(cc));
            end
            colormap(cc);
        end
elseif filename(4:5)=='03'
    %insert code for 03 here
end

if doflipud
    imagedata.mimage=flipud(imagedata.mimage);
end
if dofliplr
    imagedata.mimage=fliplr(imagedata.mimage);
end
    
if makeplot
    axes(displayAxis);
    cla;
    %check to see if there is a complex part in the data
    %occurred in MOD021KM.A2001152.1600.003.2001164070454.hdf file
    % for bands 21, 22, and 23 temperature - imagesc crashes if complex
    indx=find(imag(imagedata.mimage)~=0);
    if ~isempty(indx)
	imagedata.mimage(indx)=real(imagedata.mimage(indx));
    end
    imagesc(imagedata.mimage);
    set(gca,'XTick',[],'YTick',[]);
    title(['Channel ' num2str(band)]);
    cmin=min(min(imagedata.mimage));
    cmax=max(max(imagedata.mimage));
    %cmin and cmax will not be different if nighttime and visible
    if cmin~=cmax
        set(gca,'CLim',[cmin,cmax]);
        c=colorbar;
	    set(c,'Units','Pixels');
        label=get(c,'XLabel');
        %strip off quotes
        temp=vartoplot(2:length(vartoplot)-1);
        if strcmp(temp,'temperature')
            set(label,'String',[temp ' (K)']);
        elseif strcmp(temp,'radiance')
            set(label,'String',[temp ' (W/m^2/micron/ster)']);
        else
            set(label,'String',temp);
        end
        bandAvail=1;
    end
 end

 if latlon & nargout==4 %interpolate lat/lon arrays
    %cast to double
    if strcmp(vartoplot,'')
        [varargout{1},varargout{2}]=interpLatLon(double(imagedata.latitude),double(imagedata.longitude),box);
    else
        varargout{1}=imagedata.latitude;
        varargout{2}=imagedata.longitude;
    end
    % if bandAvail==0 for Band 1, this means that this is a nighttime case (no visible bands)
    % this also means that MODIS scans from south to north ("inverted image")
    % lat and lon arrays thus need to be flipped to provide correct output for textOutput menu 
    % routine for the nighttime case
    if ~bandAvail & strcmp(vartoplot,'')
        if ~doflipud
            varargout{1}=flipud(varargout{1});
        end
        if ~dofliplr
            varargout{2}=fliplr(varargout{2});
        end
    else
        if doflipud
            varargout{1}=flipud(varargout{1});
            varargout{2}=flipud(varargout{2});
        end
        if dofliplr
            varargout{1}=fliplr(varargout{1});
            varargout{2}=fliplr(varargout{2});
        end
    end
    imagedata=rmfield(imagedata,'latitude');
    imagedata=rmfield(imagedata,'longitude');
end
