% Function for the MODIS GUI
% GUIpreloadfile
% Copyright by Jasmine S Nahorniak, Oregon State University.
% May 14 2001 

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


if get(findobj('Tag','PopupMenu2'),'Value')<=3, % is level 3 file

  [data,names,files,mins,maxs,matrices,sizes,region,filetype]= ...
      GUIloadfile(data,names,files,mins,maxs,matrices,sizes,region,filetype);
  

else % is level 2 file so open parameter list GUI
   
  
   % get the path
   pathh=findobj('Tag','EditText4');
   path=get(pathh,'String');
   if path(end)~='/',
     path(end+1)='/';
   end

   % get the selected file 
   filelisth=findobj('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];
      
   % open HDF file and check number of params
   SD_id = hdfsd('start',pathfile,'read');   % open HDF file
    [numpars,nglobal_attr,status1] = hdfsd('fileinfo',SD_id);
   status5 = hdfsd('end',SD_id);   % close HDF file
   
   % open the level 2 GUI
   if numpars<=18,
      level2GUI
   else
      level2GUIB
   end

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

   % clear all checkboxes
   h=findobj('Style','checkbox');
   set(h,'Visible','off','String','','Value',0);


   % find number of parameters in each file
   
  % if newfile(1:7)=='MODOCL2',
  %    if newfile(8)=='A',   % MODOCL2A file
  %        numpars=17;
  %    elseif newfile(8)=='B',  % MODOCL2B file
  %        numpars=11;
  %    else  % MODOCL2 file
  %       numpars=12;
  %    end
  % elseif newfile(1:7)=='MOD28L2',  % MOD28L2 file
  %    numpars=4;
  % elseif newfile(1:5)=='MOD03',  % MOD03 geolocation file
  %    numpars=10;
  % else  % unknown number of parameters
  %    numpars=18;
  % end
   
   % get the parameter names from the file, and add to GUI list

   j=0;
   for i=1:numpars,
      name=''; 
      while isempty(name), 
        j=j+1; 
        [name]=readMODISparams(pathfile,j); 
      end 
      h=findobj('Tag',['Checkbox' num2str(i)]);
      set(h,'Visible','on','String',name,'UserData',num2str(j));
   end


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

end


