function [start, stop] = nscatrows(minlat, maxlat, lat_matrix)


% Function to find the row indices which contain the man and max lat values.
% Sort of a horible bounding box problem...
%
% $Id: nscatrows.m,v 1.7 1998/11/09 20:26:57 dbyrne Exp $

% $Log: nscatrows.m,v $
% Revision 1.7  1998/11/09 20:26:57  dbyrne
% *** empty log message ***
%
% Revision 1.1  1998/05/17 14:18:10  dbyrne
% *** empty log message ***
%
% Revision 1.1  1997/12/09 03:43:52  jimg
% Added.
%
% Note that the satellite swath will fall on the Earth at an angle, so you
% have to exhaustively search the matrix.

% Search from the top left down to the bottom right. Note that the particular
% corner does not matter but we must search from the top down to find the
% start row.

% I believe that the direction of orbits, either ascending or descending, is 
% crucial to decide the both start and stop rows.
% Liang
% Dec.23, 97


% Also declared a global variabl in nscatcols.m.

global ascend;
global million;
global zero;


rows = size(lat_matrix, 1);
cols = size(lat_matrix, 2);


one = ones(rows,cols);
million = one*1000000.;
zero = one*0.;


% Find zero elements.
index = find(lat_matrix(:,:)==0);


if ( ascend == 1)

  % Ascending. Start searching from k=1.

  k = 1;

  while ( k < rows & max(0.01*lat_matrix(k, :)) < minlat)
	k = k + 1;
  end
    if( k > 1 )
	start = k - 1;
    else
    	start = k;
    end


  % Remove zeros
  lat_matrix(index) = million(index);

  while (k < rows & min(0.01*lat_matrix(k,:)) < maxlat)
  	k = k + 1;
  end
  stop = k;


  % Retrieve zeros
  lat_matrix(index) = zero(index);

else

  % Descending. Start searching from the end.

  k = rows;

  while (k > 1 & max(0.01*lat_matrix(k,:)) < minlat)
	k = k - 1;
  end
   if( k < rows)
	stop = k + 1;
   else
  	stop = k;
   end


  % Remove zeros
  lat_matrix(index) = million(index);

  while (k > 1 & min(0.01*lat_matrix(k,:)) < maxlat)
  	k = k - 1;
  end
  start = k;

  % Retrieve zeros
  lat_matrix(index) = zero(index);

end

return;
