% Function to plot MODIS level 2 data
% Copyright by Jasmine S Nahorniak, Oregon State University. 
% June 20 2000

function [coast,M]=GUIlevel2plot(data,mins,maxs,coast,region,filetype,l2lat,l2lon);


% ******************************************************
% retrieve all of the defaults

GUIdefaults;

% projection
projec=guiprojection{1};

% colormap
mycolormap(1,:)=GUIcol2num(guilandcolors{1});   % land color
mycolormap(2,:)=GUIcol2num(guimaskcolors{1});  % mask color
mycolormap(3:52,:)=eval([guicolormap{1} '(50)']);
nmask=2;

% colorbar
colbar=guicolorbar{1};

% coastline
coastcol=guicoastline{1};
coastline=GUIcol2num(coastcol);

% backgroundcolor
bgcol=GUIcol2num(guibackgroundcolors{1});

% axis/label color
axiscol=guiaxiscolors{1};
axiscoln=GUIcol2num(axiscol);

% grid choice
gridchoice=guigrid{1};

% *****************************************************************
% see if lat/lon data exists
if ~isempty(l2lat),
  dolatlon='y';  % have lat/lon data
else
  dolatlon='n';
end

% ************************************************************
% replace numbers lower than the minimum value with the min value,
% and higher numbers with the highest value.


choice=get(findobj('Tag','Listbox1'),'Value');
currentdata=data{choice};
bad=find(currentdata<mins(choice) & currentdata~=999999999);
currentdata(bad)=mins(choice);
clear bad;
bad=find(currentdata>maxs(choice) & currentdata~=999999999);
currentdata(bad)=maxs(choice);
clear bad;



% ************************************************************
% replace NaN's (land) and masked data
% with a number lower than the minimum value.
% this is so the graphics colors work.
   
bad=find(isnan(currentdata));
masked=find(currentdata==999999999);
numflags=50;  % ie don't change number of colors

if isempty(bad),  % is a flag or quality file or level2  
  clear mycolormap; 
  if isempty(masked),
    mycolormap(1:numflags,:)=eval([guicolormap{1} '(numflags)']);
    nmask=0;
  else
    currentdata(masked)=mins(choice)-(maxs(choice)-mins(choice))/(numflags-1);
    mycolormap(1,:)=GUIcol2num(guimaskcolors{1});       
    mycolormap(2:numflags+1,:)=eval([guicolormap{1} '(numflags)']);
    nmask=1;
  end
else
  if isempty(masked),
    nmask=1;
    clear mycolormap;
    mycolormap(1,:)=GUIcol2num(guilandcolors{1});       
    mycolormap(2:numflags+1,:)=eval([guicolormap{1} '(numflags)']);
  else          
    currentdata(masked)=mins(choice)-(maxs(choice)-mins(choice))/49;    
  end
  currentdata(bad)=mins(choice)-2*(maxs(choice)-mins(choice))/49;
end
clear bad masked;


% *************************************************************
% find filename for title
 
files=get(findobj('Tag','Listbox1'),'String');
cellfile=files{choice};
classfile=class(cellfile);
if classfile(1:4)=='char'
  file=cellfile;
else
  file=cat(1,cellfile{:});  % convert to string
end
good=find(file~='_');  % remove underscores from filenames
file=file(good);

% ******************************************************************
% load coastline data (called 'coast')
if isempty(coast),
  eval(['load  ' guicoastdata])
  dot=find(guicoastdata=='.');
  slash=find(guicoastdata=='/' | guicoastdata=='\');
  if isempty(slash),
     slash=0;
  end
  dataname=guicoastdata(slash(end)+1:dot-1);
  eval(['coast= ' dataname ';'])
end



% *************************************************************
% make map

Gmapfig=figure;
posit=get(Gmapfig,'Position');
set(Gmapfig,'Units','normalized','Position',[0.5 0.5 0.5 0.37]);


set(Gmapfig,'Color',bgcol);
mapax=gca;
set(mapax,'Color',bgcol, ...
          'XColor',bgcol,'YColor',bgcol,'ZColor',bgcol);
set(mapax,'Tag','Mapaxes');
hold on;
colormap(mycolormap);
set(Gmapfig,'UserData',nmask);

if dolatlon=='y',
 
 GUImakemapmenu(Gmapfig);
  if projec(1:3)=='ani',
   doanim='y';
   projec='globe';
  else
   doanim='n';
  end

  set(Gmapfig,'Tag','Mapfigure');
  eval(['axesm ' projec])
  GUIeditmap(Gmapfig);

  surfm(data{l2lat},data{l2lon},currentdata);
  %avlat=mean(mean(data{l2lat}));
  %set(gca,'DataAspectRatio',[1/cos(avlat*pi/180) 1 1])
  setm(mapax,'fontcolor',axiscoln);

else

  doanim='n';
  GUImakel2mapmenu(Gmapfig);

  set(Gmapfig,'Tag','Level2figure');
  GUIeditl2map(Gmapfig);
  step=floor((region{choice}(4)-region{choice}(3))/(length(currentdata(1,:))-1));
  surf([region{choice}(3):step:region{choice}(4)], ...
       [region{choice}(2):step:region{choice}(1)],currentdata);
  set(gca,'DataAspectRatio',[100 100 1])   % must 100 100 1 not 111 to work in matlab r12
  set(mapax,'XColor',axiscoln,'YColor',axiscoln)

end
      
       
    view(2)
    shading flat
    
    if colbar(1:3)=='ver',
       hcb=colorbar('vert'); 
       pause(0.05); 
       set(hcb,'Tag','Colorbar','YTickLabelMode','auto');  % need this to work in matlab r12
       clear hcb;
    elseif colbar(1:3)=='hor',
       hcb=colorbar('horiz');
       pause(0.05);
       set(hcb,'Tag','Colorbar','XTickLabelMode','auto');  % need this to work in matlab r12
       clear hcb;
    end 
    eval(['grid ' gridchoice ';']);    
    set(mapax,'Box','off')    
    

    if axiscol(1:3)~='non',
      xlabel('Longitude','Color',axiscoln)
      ylabel('Latitude','Color',axiscoln)
      if dolatlon=='y',
         setm(gca,'parallellabel','on','meridianlabel','on')
      end
    end
     
     if coastcol(1:3)=='non' | dolatlon~='y',  % no coastlines or level 2 data    
          coasth=[];
     else      
       coasth=plotm(coast(:,2),coast(:,1), ...
             'LineStyle','-','Color',coastline,'Clipping','on');
     end    
     set(coasth,'Tag','Coastline') 

   
   % put default title on figure unless title given in edit figure box  
   currtitle=get(get(gca,'Title'),'String');
   if isempty(currtitle),
      hh=title(file,'Color',axiscoln);
      set(hh,'Tag','Maptitle');
   end

if doanim=='y' & dolatlon=='y',    % animated globe
    M=GUIanimglobe;
else
    M=[];
end


% *******************************************



GUIfigurenum('PopupMenu1',{'Mapfigure'; 'Level2figure'});



