function [orbits,orbitsa,orbitsd] = nsc50orb(aE, aW, dE, dW, mintime, ...
                    maxtime)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Determine which orbits crossed region (using inventory of NSCAT files  with 
% [orbit time 1st equatorial crossing] )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%initialize orbits:
orbits=[];
orbitsa=[];
orbitsd=[];
idxa=[];
idxb=[];
testinv=[];

% load NSCAT_inv_new
load INV50
T1 = INV50(:,2);				% 1st data Time 

% First, subset the inventory based on time limits.

time_idx = find(INV50(:,2) >= mintime & INV50(:,2) <= maxtime);
INV50 = INV50(time_idx,:);


%%%%
% check to see if any orbits were found to cross the (lon,lat,time) boundaries.
%
%   pdh 6 July 1999
%%%%

if isempty(INV50)
    return;
end

% Second, subset the inventory based on 1st equatorial crossing longitude.
% This requires different logic for two cases where eastern boundary
% determined by eqlonlim may be greater or less than western boundary, i.e.
% whether or not zero longitude is crossed by the equatorial limits
% determined for ascending and descending passes.

eqX1 = INV50(:,3);	% Longitude of 1st rev equatorial crossing

%set variables for longitude range across zero longitude.
dW1=0; aW1=0;


if (dE-dW>=360)
   orbitsa=INV50;
end
if (aE-aW>=360)
   orbitsd=INV50;
end

if (dE-dW>=360) | (aE-aW)>=360
   orbits=INV50;
else

   if aE>360
      %process an ascending longitude range across zero longitude:
      aW1 = aW;  aE1 = 360;
      aW2 = 0;   aE2 = aE - 360;
%     fprintf('aW1: %f;  aE1: %f;\naW2: %f;  aE2: %f\n',aW1,aE1,aW2,aE2)
      idxa=find((eqX1 <= aE1 & eqX1>=aW1)|(eqX1 <= aE2 & eqX1>aW2));
   else
      idxa = find(eqX1 <= aE & eqX1>=aW);
   end

   if dE>360
      %process a descending longitude range across zero longitude:
      dW1 = dW;  dE1 = 360;
      dW2 = 0;   dE2 = dE - 360;
%     fprintf('dW1: %f;  dE1: %f;\ndW2: %f;  dE2: %f\n',dW1,dE1,dW2,dE2)
      idxb=find((eqX1 <= dE1 & eqX1>=dW1)|(eqX1 <= dE2 & eqX1>dW2));
   else
      idxb = find(eqX1 <= dE & eqX1>=dW);
   end

%test for no orbits, either ascending or descending:
% pdh 6 July 99

   if isempty(idxa)&isempty(idxb)
      orbits=[];
      return
   end

% get rid of duplicate (both ascending and descending) orbits
   [idx1,indx]=sort([idxa;idxb]);
   if (length(idx1)>1)

%   make sure idx1 is a column vector:
    if size(idx1,2)~=1
       idx1=idx1';
    end

    idx2=[idx1(2:length(idx1)); 0];
    whr=find(idx1~=idx2);
    idx=idx1(whr);
   else
     idx=idx1(1);
   end

%all orbits:
   orbits=INV50(idx,:); 
%ascending orbits:
%  if idxa~=[]
   if ~isempty(idxa)
      orbitsa=INV50(idxa,:);
   end
%descending orbits:
%  if idxb~=[]
   if ~isempty(idxb)
      orbitsd=INV50(idxb,:);
   end

end

return;
