% Function for the MODISGUI.
% Reads the scaling equation from an HDF file
% (output from readMODISHDF or readL2MODIS)
% and converts it into a format useable in matlab.
% jun 2001
% Copyright by Jasmine S Nahorniak, Oregon State University

function [matlabeqn]=GUIscaleeqn(equation);

% no equation, so set to linear
if equation==-1,    
   matlabeqn='newdata=newdata*double(slope)+double(intercept);';

% equation exists
else

matlabeqn=char(zeros(1,100));
matlabeqn(1:8)='newdata=';  % assumes that equation is of the form "y = ..."

j=9;
k=find(equation=='=');
while k<length(equation),
  k=k+1; 
  c=equation(k);   % reads a character from the equation string
  if c=='x',
     matlabeqn(j:j+6)='newdata';
     j=j+7;
  elseif (c=='+' | c=='-' | c=='/' | c=='(' | c==')' | ...
	 (floor(c)>=48 & floor(c)<=57)),  % numbers between 0 and 9
     matlabeqn(j)=c;
     j=j+1;
  elseif k+1<=length(equation),
    if (c=='*' & equation(k+1)~='*'),
     matlabeqn(j)=c;
     j=j+1;
    elseif equation(k:k+1)=='**',
        matlabeqn(j:j+1)='.^';
        j=j+2;
        k=k+1;
    elseif equation(k:k+1)=='ln',
        matlabeqn(j:j+2)='log'; 
        j=j+3;
        k=k+1;
    elseif k+2<=length(equation),
      if equation(k:k+2)=='exp',
        matlabeqn(j:j+2)='exp'; 
        j=j+3;
        k=k+2;
      elseif equation(k:k+2)=='log',
        matlabeqn(j:j+4)='log10'; 
        j=j+5;
        k=k+2;
      elseif k+3<=length(equation),
        if equation(k:k+3)=='data',
           matlabeqn(j:j+6)='newdata';
           j=j+7;
           k=k+3;
        elseif k+4<=length(equation),
           if (equation(k:k+4)=='Slope' | equation(k:k+4)=='slope'),
               matlabeqn(j:j+12)='double(slope)';
               j=j+13;
               k=k+4;
           elseif equation(k:k+4)=='log10',
              matlabeqn(j:j+4)='log10'; 
              j=j+5;
              k=k+4;
           elseif k+8<=length(equation),
              if (equation(k:k+8)=='Intercept' | equation(k:k+8)=='intercept'),
                 matlabeqn(j:j+16)='double(intercept)';
                 j=j+17;
                 k=k+8;
     end
     end
     end
     end
     end
     end
end % while
matlabeqn(j)=';';
matlabeqn=matlabeqn(1:j);

end %  main if
