% GUImask
% Function for the MODIS GUI
% Copyright by Jasmine S Nahorniak, Oregon State University.
% Dec 20 2000

function [data,names,files,mins,maxs,matrices,sizes,region,filetype]= ...
         GUImask(data,oldnames,oldfiles,oldmins,oldmaxs,oldmatrices,oldsizes,oldregion,oldfiletype);

GUIbutton('Pushbutton2','WORKING','on');

% get the data choice (selected by user)
mainfig=findobj('Name','MODIS GUI');
choice=get(findobj(mainfig,'Tag','Listbox1'),'Value');
% get the land mask(s) choice
lmask=get(findobj(gcbf,'Tag','Listbox1'),'Value')-1;
% get the general mask(s) choice
gmask=get(findobj(gcbf,'Tag','Listbox2'),'Value')-1;

names=oldnames;
files=oldfiles;
mins=oldmins;
maxs=oldmaxs;
matrices=oldmatrices;
sizes=oldsizes;
region=oldregion;
filetype=oldfiletype;

landval=NaN;
maskval=999999999;

good='n';
% see if matrix sizes match and at least one mask used
if (lmask>0 | gmask>0),
  good='y';
  if lmask>0,
    for q=1:length(lmask),
      if size(data{lmask(q)})~=size(data{choice}),
        good='n';
        fprintf(1, '%s\n', 'Error: Matrix sizes do not match.');
      end
    end
  end
  if gmask>0,
    for q=1:length(gmask),
      if size(data{gmask(q)})~=size(data{choice}),
        fprintf(1, '%s\n', 'Error: Matrix sizes do not match.');
        good='n';
      end
    end
  end
end

if good=='y',  % at least one mask is used and all are of right size(s)



lendata=length(data);
i=lendata+1;
data{i}=data{choice};

% mask the data
   if gmask>0,  % general mask
     for q=1:length(gmask),
      gmaski=find(data{gmask(q)}>0);   % find all flags turned on
      data{i}(gmaski)=maskval;
     end
   end
   if lmask>0,  % land mask
     for q=1:length(lmask),
      lmaski=find(data{lmask(q)}>0);   % find all flags turned on
      data{i}(lmaski)=landval;
     end
   end



% add masked data to the list
   
   % add spaces to the end of the masked file names
   if lmask>0,
    for zz=1:length(lmask),
       if zz<length(lmask),
         lnames{zz}=[names{lmask(zz)} ' and '];
         lfiles{zz}=[files{lmask(zz)} ' and '];
       else
         lnames{zz}=[names{lmask(zz)} ' '];
         lfiles{zz}=[files{lmask(zz)} ' '];
       end
    end
   end
   if gmask>0,
    for zz=1:length(gmask),
       if zz<length(gmask),
         gnames{zz}=[names{gmask(zz)} ' and '];
         gfiles{zz}=[files{gmask(zz)} ' and '];
       else
         gnames{zz}=[names{gmask(zz)} ' '];
         gfiles{zz}=[files{gmask(zz)} ' '];
       end
    end
   end

   if lmask>0,
    if gmask>0,  % use both masks
     names{i}=[names{choice} ': land masked with ' lnames{:} ' and general masked with ' gnames{:}];
     files{i}=[files{choice} ': land masked with ' lfiles{:} ' and general masked with ' gfiles{:}];
    else        % land mask only
     names{i}=[names{choice} ': land masked with ' lnames{:}];
     files{i}=[files{choice} ': land masked with ' lfiles{:}];
    end
   elseif gmask>0, % general mask only
     names{i}=[names{choice} ': general masked with ' gnames{:}];
     files{i}=[files{choice} ': general masked with ' gfiles{:}];
   end

   matrices{i}=['data{' num2str(i) '}'];
   notmasked=find(data{i}~=maskval & ~isempty(data{i}));
   mins(i)=min(min(data{i}(notmasked)));
   maxs(i)=max(max(data{i}(notmasked)));
   nsize=size(data{i});
   sizes(i,:)=[nsize(1) nsize(2)];
   region{i}=region{choice};
   filetype{i}=filetype{choice};  

  
  sizescell={};
  for k=1:lendata+1,
     sizescell{k}=['[' num2str(sizes(k,1)) ' x ' num2str(sizes(k,2)) ']'];
  end

  
  newchoice=lendata+1;
  
     % put in data list box and x/y data boxes 
     set(findobj('Tag','Listbox1'),'String',files,'Value',newchoice);
     set(findobj('Tag','Listbox3'),'String',sizescell,'Value',newchoice);
     set(findobj('Tag','Listbox4'),'String',matrices,'Value',newchoice);
     set(findobj('Tag','Listbox5'),'String',files,'Value',newchoice);
     set(findobj('Tag','Listbox6'),'String',files,'Value',newchoice);
     GUIselectdata(mins,maxs,region);


 % enable all appropriate buttons
   set(findobj('Tag','Pushbutton9'),'Enable','on');
   set(findobj('Tag','Pushbutton10'),'Enable','on');
   set(findobj('Tag','Pushbutton4'),'Enable','on');
   set(findobj('Tag','Pushbutton6'),'Enable','on');
   set(findobj('Tag','Pushbutton15'),'Enable','on');
   set(findobj('Tag','Pushbutton5'),'Enable','on');
   set(findobj('Tag','Pushbutton3'),'Enable','on');
   set(findobj('Tag','Pushbutton12'),'Enable','on');
   set(findobj('Tag','Pushbutton7'),'Enable','on');
   set(findobj('Tag','Pushbutton14'),'Enable','on');


  


end  % if good=='y'


GUIbutton('Pushbutton2','GO','off');

close(gcbf);
