% Load MODIS HDF file
% function for the MODIS GUI
% Copyright by Jasmine S Nahorniak, Oregon State University. 
% Dec 19 2000

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

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

% 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

% get the file type
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('Tag','EditText15'),'String'));
latS=str2num(get(findobj('Tag','EditText17'),'String'));
lonW=str2num(get(findobj('Tag','EditText16'),'String'));
lonE=str2num(get(findobj('Tag','EditText18'),'String'));

% put the selected region in the edit map boxes
set(findobj('Tag','EditText8'),'String',num2str(latN));
set(findobj('Tag','EditText9'),'String',num2str(latS));
set(findobj('Tag','EditText10'),'String',num2str(lonW));
set(findobj('Tag','EditText11'),'String',num2str(lonE));



   % load file   
   [newdata, slope, intercept, equation, newname]=readMODISHDF([path newfile]);

   % calculate rows/columns of data to load
   datasize=size(newdata);
   deg2pix=datasize(1)/360;   % global data was loaded
   rowN=floor((90-latN)*deg2pix)+1;
   rowS=ceil((90-latS)*deg2pix);
   colW=floor((180+lonW)*deg2pix)+1;
   colE=ceil((180+lonE)*deg2pix);

   tempdata=newdata(colW:colE,rowN:rowS);   % is inverted since data is inverted
   clear newdata; 
  
   % subset by step size
   step=str2num(get(findobj('Tag','EditText5'),'String'));
   newdata=tempdata(1:step:end,1:step:end);
   deg2pix=length(newdata(:,1))/(lonE-lonW);  
   clear tempdata;

   % 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
   bad=find(newdata==-9100000); 
   if (~isempty(bad)),
      newdata(bad)=NaN;
   end
   clear bad

   % change scale if appropriate
   [scaleeqn]=GUIscaleeqn(equation);
   eval(scaleeqn); 
   %newdata=newdata*double(slope)+double(intercept);

   % size of new data
   nsize=size(newdata);
   newsize=[nsize(1) nsize(2)];

   % size of all data matrix
   numfiles=length(data);
 

if get(findobj('Tag','PopupMenu2'),'Value')==1, % is data (M,N,S)


   % matrix name
   newmatrix=['data{' num2str(numfiles+1) '}'];

    
   % add to existing arrays
     data{end+1}=newdata;
     files{end+1}=newfile;       
     names{end+1}=newname; 
     matrices{end+1}=newmatrix;     
     sizes(end+1,:)=newsize;
     region{end+1}=[latN latS lonW lonE deg2pix];
     filetype{end+1}=ftype;
    
  % find min and max of new data
    mindata=min(min(newdata));
    maxdata=max(max(newdata));
    mins(end+1)=mindata;
    maxs(end+1)=maxdata;

else    % is flag or quality data so convert to binary

 bindata=dec2bin(newdata,8);

 if get(findobj('Tag','PopupMenu2'),'Value')==2,  % is flag data

   for j=1:8,
       data{numfiles+j}=reshape(str2num(bindata(:,9-j)),nsize(1),nsize(2));
       files{numfiles+j}=[newfile ': flag ' num2str(j)];
       names{numfiles+j}=[newname ': flag ' num2str(j)];
       matrices{numfiles+j}=['data{' num2str(numfiles+j) '}'];
       sizes(numfiles+j,:)=newsize;
       mins(numfiles+j)=min(min(data{end}));
       maxs(numfiles+j)=max(max(data{end}));
       region{numfiles+j}=[latN latS lonW lonE deg2pix];
       filetype{numfiles+j}=ftype;
   end

 else   % is quality data

    for j=1:4,
       data{numfiles+j}=reshape(bin2dec(bindata(:,9-(j*2):10-(j*2))),nsize(1),nsize(2));
       files{numfiles+j}=[newfile ': quality ' num2str(j)];
       names{numfiles+j}=[newname ': quality ' num2str(j)];
       matrices{numfiles+j}=['data{' num2str(numfiles+j) '}'];
       sizes(numfiles+j,:)=newsize;
       mins(numfiles+j)=min(min(data{end}));
       maxs(numfiles+j)=max(max(data{end}));
       region{numfiles+j}=[latN latS lonW lonE deg2pix];
       filetype{numfiles+j}=ftype;
   end

  end
   
end
    
  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','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('Pushbutton1','GO','off');
