% GUIrefresh
% Function for the MODIS GUI
% Copyright by Jasmine S Nahorniak, Oregon State University.
% March 13 2000

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

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

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

ednums=[];

lendata=length(data);
lennames=length(names);

% find other edited data sets
for i=1:lennames,
  if names{i}(1:2)=='ed';
    ednums(i)=str2num(names{i}(7:end));
  end
end
if ~isempty(ednums),
    newnum=max(ednums)+1;
else
    newnum=1;
end


% guess at region based on last choice
latN=str2num(get(findobj('Tag','EditText15'),'String'));
latS=str2num(get(findobj('Tag','EditText17'),'String'));
lonW=str2num(get(findobj('Tag','EditText16'),'String'));
lonE=str2num(get(findobj('Tag','EditText18'),'String'));

% this is to help with region determination
for i=1:lendata,
   datasize(i,:)=size(data{i});
end

%deg2pix=8192/360;   % this is a guess!!!!

% add edited data to the list
for i=lennames+1:lendata,      
   names{i}=['edited' num2str(newnum)];
   files{i}=['edited' num2str(newnum)];
   matrices{i}=['data{' num2str(i) '}'];
   notmasked=find(data{i}~=999999999);
   mins(i)=min(min(data{i}(notmasked)));
   maxs(i)=max(max(data{i}(notmasked)));
   nsize=size(data{i});
   sizes(i,:)=[nsize(1) nsize(2)];


   % this is now a *guess* at the region and file type for the edited data sets
   if i~=1,
      % find other matrix of same size to guess at region and file type
      samesize=find(datasize(1:i-1,1)==datasize(i,1) & datasize(1:i-1,2)==datasize(i,2));
      if ~isempty(samesize),
         region{i}=region{samesize(end)};
         filetype{i}=filetype{samesize(end)}; 
      else
         region{i}=[latN latS lonW lonE datasize(i,1)/(latN-latS)];
         filetype{i}='level 2';  % treat it like a level 2 file
      end
   else
      region{i}=[latN latS lonW lonE datasize(i,1)/(latN-latS)];
      filetype{i}='level 2'; % treat it like a level 2 file  
   end
   newnum=newnum+1;
end

% refresh matrix sizes, mins and maxs in case edited manually
for i=1:lendata,
   if mins(i)~=min(min(data{i})) | maxs(i)~=max(max(data{i})) | ...
        sizes(i,1)~=length(data{i}(:,1)) | sizes(i,2)~=length(data{i}(1,:)), 
     notmasked=find(data{i}~=999999999);
     mins(i)=min(min(data{i}(notmasked)));
     maxs(i)=max(max(data{i}(notmasked)));
     nsize=size(data{i});
     sizes(i,:)=[nsize(1) nsize(2)];
     noti=[1:i-1 i+1:lendata];
     samesize=find(datasize(noti,1)==datasize(i,1) & datasize(noti,2)==datasize(i,2));
     % this is a guess at the region 
     if ~isempty(samesize),
         region{i}=region{noti(samesize(end))}; 
      else
         region{i}=[latN latS lonW lonE datasize(i,1)/(latN-latS)];
      end
   end
end
  
  sizescell={};
  for i=1:lendata,
     sizescell{i}=['[' num2str(sizes(i,1)) ' x ' num2str(sizes(i,2)) ']'];
  end

  if lendata>0,
     newchoice=lendata;
  else
     newchoice=1;
  end 


     % 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);
    if lendata>0,   
     GUIselectdata(mins,maxs,region);
    else
     set(findobj('Tag','EditText1'),'String','');
     set(findobj('Tag','EditText6'),'String','');
    end


 % 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');
   set(findobj('Tag','Pushbutton16'),'Enable','on');

GUIbutton('Pushbutton11','refresh','off');
