% GUImakemap
% Function for the MODIS GUI
% Copyright by Jasmine S Nahorniak, Oregon State University.
% Feb 25 2000

function [coast,M]=GUImakemap(data,mins,maxs,coast,region,filetype);


% ******************************************************
% 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};

% ************************************************************
% 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);

ftype=char(filetype{choice});
if (ftype(1:4)=='flag' | ftype(1:4)=='qual'),  % flags or quality
  numflags=max(max(data{choice}))+1;
else % level 2 or level 3 file
  numflags=50;  % ie don't change number of colors
end
if isempty(bad),  % may be a flag or quality file
    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),
    clear mycolormap;
    mycolormap(1,:)=GUIcol2num(guilandcolors{1});     
    mycolormap(2:numflags+1,:)=eval([guicolormap{1} '(numflags)']);
    nmask=1;
  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

% make new figure

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

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

GUImakemapmenu(Gmapfig);

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


    eval(['axesm ' projec])

    GUIeditmap(Gmapfig);

    colormap(mycolormap);  
    set(Gmapfig,'UserData',nmask);
    %meshm(currentdata,[length(currentdata(1,:))/360 90 -180])  % global
    meshm(currentdata,[region{choice}(5) region{choice}(1) region{choice}(3)])
    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(['gridm ' gridchoice ';']);    
    set(mapax,'Box','off')
    setm(mapax,'fontcolor',axiscoln);    


    if axiscol(1:3)~='non',
      xlabel('Longitude','Color',axiscoln)
      ylabel('Latitude','Color',axiscoln)
      setm(gca,'parallellabel','on','meridianlabel','on')
    end
   

   if coastcol(1:3)=='non' | (region{choice}(2)==0 & region{choice}(3)==0 & ...
          region{choice}(5)==1),  % 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',    % animated globe
    M=GUIanimglobe;
else
    M=[];
end

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


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

  


      

