function [start,stop] = swathrows(minlat,maxlat,latmatrix,latscale,latnull)

% swathrows find two vectors (startrows and stoprows) of the same
%    length that give the start and stop row indices of contiguous
%    rows which are within the latitude limits minlat and maxlat.
%
%    These start and stop rows are used to formulate URL Constraints.
%
%    begun 1/7/99 by Paul Hemenway
%
%    The basic ideas come from "nscatrows.m" by D. Byrne and J. Gallagher.
%

% The preceeding blank line is important for cvs.
% $Id: swathrow.m,v 1.3 2000/03/15 00:26:33 dbyrne Exp $
%

% $Log: swathrow.m,v $
% Revision 1.3  2000/03/15 00:26:33  dbyrne
% *** empty log message ***
%
% Revision 1.2  1999/06/01 15:10:09  paul
% change ~= []  to ~isempty()
%

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

% determine the rows with valid data:

rowndex = zeros(rows,1);

for k=1:rows
   latdx=latmatrix(k,:)~=latnull;
   latvec=latmatrix(k,latdx);
   if  ~isempty(latvec)
      if min(latscale*latvec)<maxlat & ...
         max(latscale*latvec)>minlat
         rowndex(k) = 1.;
      end
   end
end

% determine the start and stop indices with contiguous data:

% the maximum number of starts and stops (swath segments) is expected
%    to be three (the ascending or descending segment may "wrap around")
%
start=zeros(3,1);
stop=start;
swathcnt=0;
urlcnt = 1;
onoff='off';
for k=1:rows
   if strcmp(onoff,'off')&rowndex(k)==1
      start(urlcnt)=k;
      onoff = 'on';
      swathcnt=swathcnt + 1;
   end
   if strcmp(onoff,'on')&rowndex(k)==0 
      stop(urlcnt)=k-1;
      onoff = 'off';
      urlcnt=urlcnt + 1;
   end
   if strcmp(onoff,'on')&k==rows
      stop(urlcnt)=k;
   end
end
   
if swathcnt<1
   fprintf('too few swaths: %d\n',swathcnt)
   return;
elseif swathcnt>3
   fprintf('too many swath segments: %d\n',swathcnt)
   return;
else
   start=start(1:swathcnt);
   stop=stop(1:swathcnt);
end

return;





