function [aE, aW, dE, dW] = nscat_25_EQ(CO, minlat, maxlat, minlon, maxlon)
%---------------------------------------------------------------------------
%
% Copyright 1997 University of Rhode Island.
%
% Compute the ascending and descending eastern and western boundries of the
% equatorial crossing for the given minimum and maximum latitude values. Use
% a `cannonical orbit' to perform these calculations. 
%
% Based on an algorithm by Peter Cornillon. This code is from an
% implementation by John Ryan. jhrg 11/19/97.
%  
% $Id: nscat_25_EQ.m,v 1.8 1998/11/05 22:08:45 dbyrne Exp $
% 
%---------------------------------------------------------------------------

%  
% $Log: nscat_25_EQ.m,v $
% Revision 1.8  1998/11/05 22:08:45  dbyrne
%
%
% Stuff.
%
% Revision 1.1  1998/05/17 14:18:09  dbyrne
% *** empty log message ***
%
% Revision 1.3  1997/11/21 22:43:18  jimg
% Removed Log from help text.
%
% Revision 1.2  1997/11/21 22:42:42  jimg
% Changed return argument format.
%
% Revision 1.1  1997/11/20 23:07:46  jimg
% Added.
%

% Golbal variable LON_CO is used in getnscat20.m to calculate the longitude
% that corresponds to the highest latitude.

global LON_DIS;


% Normalize the longitude ranges

if minlon < 0; minlon = minlon+360; end
if maxlon < 0; maxlon = maxlon+360; end


% Load canonical orbit. This matrix contains the following variables which
% are used below: 
% CO_eqX1, CO_eqX2: The longitude of the ascending and descending equatorial
% crossings.
% LD_AD: The longitude difference between the ascending and descending
% equatorial crossings of the canonical orbit. 
% LON_*, LAT_*: Longitude and Latitude of the edges and nadir of a `canonical
% orbit' of the NSCAT satellite. Offset these values to find the equatorial
% crossing of an actual orbit.

load(CO);

% Find the difference between the CO_eqX1 and LON_1(406) which corresponds to
% the zero-crossing and the northest longitude.
% The northest value is at 406.
% Dec.31,97	Liang

%LON_DIS = LON_1(406) - LON_1(206);
LON_DIS = - CO_eqX1 + LON_1(406);



% Find [ascending and descending] first-equatorial-crossing bounds for 
% data to exist within user specified bounds 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Ascending Pass %%%%%%%%%%%%%%%%%%%%%%%%%%%

ascnd = [1:400];

% Western

% Where is minlat is in canonical orbit (ascending)
[SL,idx] = sort(abs(LAT_24(ascnd) - minlat));

% Calculate longitude difference between [lon_nadir at equator] and
% [lon_24 at maxlat]

LD = CO_eqX1 - LON_24(idx(1));
 
%  Add this to minlon to get western boundary for ascending and normalize.

aW = minlon + LD;
if aW > 360; aW = aW -360; end
if aW < 0; aW = aW +360; end

% Eastern Boundary
% Where is maxlat is in the canonical orbit

[SL,idx] = sort(abs(LAT_1(ascnd) - maxlat));

% Calculate longitude difference between [lon_nadir at equator] and
% [lon_1 at maxlat]

LD = CO_eqX1 - LON_1(idx(1));

% Add this to maxlon to get eastern boundary for ascending and normalize

aE = maxlon + LD;
if aE > 360; aE = aE -360; end
if aE < 0; aE = aE +360; end

%%%%%%%%%%%%%%%%%%%%%%%%%%%% Descending Pass %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
descnd = [401:800];

% Western Boundary
% Where is maxlat is in the canonical orbit

[SL,idx] = sort(abs(LAT_1(descnd) - maxlat));

% Calculate longitude difference between [lon_nadir at equator] and
% [lon_1 at maxlat]

LD = CO_eqX2 - LON_1(idx(1) + descnd(1) - 1);

%  Add this to minlon to get western boundary for descending

dW = minlon + LD + LD_AD;
if dW > 360; dW = dW -360; end
if dW < 0; dW = dW +360; end

% Eastern Boundary
% Where is minlat is in the canonical orbit?

[SL,idx] = sort(abs(LAT_24(descnd) - minlat));

% Calculate longitude difference between [lon_nadir at equator] and
% [lon_1 at maxlat]

LD = CO_eqX2 - LON_24(idx(1) + descnd(1) - 1);

%  Add this to maxlon to get eastern boundary for descending

dE = maxlon + LD + LD_AD;
if dE > 360; dE = dE -360; end
if dE < 0; dE = dE +360; end

return;
