% Function for the MODIS GUI
% GUIloadlevel2
% Copyright by Jasmine S Nahorniak, Oregon State University. 
% June 14 2000

function [data,names,files,mins,maxs,matrices,sizes,region,filetype]=GUIloadlevel2(data,names,files,mins,maxs,matrices,sizes,region,filetype)

GUIbutton('Pushbutton1','LOADING','on');

% get selected parameters from the GUI

h=findobj(gcbf,'Style','checkbox','Value',1,'Visible','on');
%boxes=char(get(h,'Tag'));
%params=str2num(boxes(:,9:end));
params=get(h,'UserData');
% make sure params is a cell array
classparams=class(params);
if classparams(1:4)=='char'
  paramtemp{1}=params;
  params=paramtemp;
end


mainGUI=findobj('Name','MODIS GUI');

% get the path
pathh=findobj(mainGUI,'Tag','EditText4');
path=get(pathh,'String');
if path(end)~='/',
   path(end+1)='/';
end

% get the selected file 
filelisth=findobj(mainGUI,'Tag','Listbox2');
filelist=get(filelisth,'String');
fileselect=get(filelisth,'Value');  % selected file
filecell=filelist(fileselect,:);
classfile=class(filecell);
if classfile(1:4)=='char'
  newfile=filecell;
else
  newfile=cat(1,filecell{:});   % convert cell to string
end
pathfile=[path newfile];

% get the file type (should be level 2)
mainfig=findobj('Name','MODIS GUI');
alltypes=get(findobj(mainfig,'Tag','PopupMenu2'),'String');
ftype=alltypes(get(findobj('Tag','PopupMenu2'),'Value'));

% get the selected region
latN=str2num(get(findobj(mainfig,'Tag','EditText15'),'String'));
latS=str2num(get(findobj(mainfig,'Tag','EditText17'),'String'));
lonW=str2num(get(findobj(mainfig,'Tag','EditText16'),'String'));
lonE=str2num(get(findobj(mainfig,'Tag','EditText18'),'String'));

% load the appropriate level 2 data
k=length(data);

for i=1:length(params),
   [L2data, slope, intercept, equation, L2name]=readL2MODIS(pathfile,str2num(params{i}));

    % calculate rows/columns of data to load
   L2size=size(L2data);
   if (latN==90 & latS==-90 & lonW==-180 & lonE==180), 
      rowN=L2size(2);
      rowS=1;
      colW=1;
      colE=L2size(1);
   else
      rowN=L2size(2)-latS+1;
      rowS=L2size(2)-latN+1;
      colW=lonW;
      colE=lonE;
   end    
   L2data=L2data(colW:colE,rowS:rowN);   % is inverted since data is inverted

   % subset by step size
   step=str2num(get(findobj(mainGUI,'Tag','EditText5'),'String'));
   newdata=L2data(1:step:end,1:step:end);    
   clear L2data;

   % change the matrix orientation 
   tempdata=newdata(:,end:-1:1)';
   clear newdata

   % change from single to double precision
   newdata=double(tempdata);
   clear tempdata 

    % remove missing values from old data files
   bad=find(newdata==-9100000 | newdata==-999);  
   if (~isempty(bad)),
       newdata(bad)=NaN;
   end
   clear bad;

   % change scale if appropriate 
   [scaleeqn]=GUIscaleeqn(equation);
   eval(scaleeqn);
   %newdata=newdata*double(slope)+double(intercept);
  
   data{k+i}=newdata;
   names{k+i}=L2name;
   files{k+i}=[newfile ': ' L2name];
   mins(k+i)=min(min(newdata));
   maxs(k+i)=max(max(newdata));
   matrices{k+i}=['data{' num2str(k+i) '}'];
   sizes(k+i,:)=size(newdata);
   if (latN==90 & latS==-90 & lonW==-180 & lonE==180),
      region{k+i}=[L2size(2) 1 1 L2size(1) 1];  % pixel numbers
   else
      region{k+i}=[latN latS lonW lonE 1];  % pixel numbers
   end
   filetype{k+i}=ftype;
end


% put the selected region in the edit map boxes
set(findobj('Tag','EditText8'),'String',num2str(region{end}(1)));
set(findobj('Tag','EditText9'),'String',num2str(region{end}(2)));
set(findobj('Tag','EditText10'),'String',num2str(region{end}(3)));
set(findobj('Tag','EditText11'),'String',num2str(region{end}(4)));

  for i=1:length(sizes(:,1)),
     sizescell{i}=['[' num2str(sizes(i,1)) ' x ' num2str(sizes(i,2)) ']'];
  end

  % put in data list box and x/y data boxes 
   set(findobj('Tag','Listbox1'),'String',files,'Value',length(files));
   set(findobj('Tag','Listbox3'),'String',sizescell,'Value',length(files));
   set(findobj('Tag','Listbox4'),'String',matrices,'Value',length(files));
   set(findobj('Tag','Listbox5'),'String',files,'Value',length(files));
   set(findobj('Tag','Listbox6'),'String',files,'Value',length(files));
    
   % display min and max of new data
   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','Pushbutton5'),'Enable','on');
   set(findobj('Tag','Pushbutton15'),'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('Pushbutton1','GO','off');
close(gcbf)
