function [times, pass_times, url] = pathcat(catalog_server, time, ranges)
%----------------------------------------------------------------------------
%
% This function generates a time and satellite type for each satellite pass
% in the specified interval.
%
% Formal parameters:
% catalog_server: URL of the catalog server.
% time: The time range of the data set.
%
% The return values are:
% times: The time in years (and fractions, see note below) for all hits.
% pass_times: This is a matrix of time with the year, day and
% ascending/descending flag of each hit. Note that the day here is the
% calendar day.
%
% $Id: pathcat.m,v 1.9 1998/11/10 21:22:30 dbyrne Exp $
%
%----------------------------------------------------------------------------

% $Log: pathcat.m,v $
% Revision 1.9  1998/11/10 21:22:30  dbyrne
%
%
% Updated to use new JPL/PODAAC catalogue server.
%
% Revision 1.5  1998/11/10 14:14:55  root
% rearranged slightly
%
% Revision 1.4  1998/11/10 14:12:03  root
% fixed up to use PODAAC CS.  dbyrne 98/11/10
%
% Revision 1.3  1998/11/05 16:01:54  root
% Changed LonVector and LatVector to be used if no Lon returned and tried the update function.
% DataScale and DataNull should now be optional!
%
% Revision 1.2  1998/09/09 15:04:31  dbyrne
% Eliminating all global variables.
%
% Revision 1.1  1998/05/17 14:18:10  dbyrne
% *** empty log message ***
%
% Revision 1.3  1997/12/04 20:01:42  jimg
% Split the query of the catalog server up so that multiple years are handled
% properly - uses the qpathcat function.
% Use the return value from qpathcat.
%
% Revision 1.2  1997/11/29 02:44:53  jimg
% Fixed function name to match new, shorter, file name.
% Added test to see if loaddods really returned a value.
%

% Get beginning and ending of time interval.

start_time = max(time(1), ranges(4,1));
end_time = min(time(2), ranges(4,2));

% The JPL Pathfinder dataset contains two images per day, one for the
% ascending pass and one for the descending. Thus, to query the catalog
% server we need to know the year, day and time to within 1/2 a day. Because
% the year2day and day2year functions don't process HMS information, handle
% the HMS part of a decimal date specially. 

% this info was for the old GSO server, now supreceded by the PO.DAAC server:
%     Also, the catalog server contains a special kludge for the broken loaddods
%     (as of 2.14d); make sure to get the time_int value, not time. time_int is 0
%     for an ascending pass and 1 for descending.

start_year = floor(start_time);
start_day = year2day(start_time, start_year);

start_hms_frac = start_day;		% HMS is fraction of day.
start_day = floor(start_day);
start_hms_frac = start_hms_frac - start_day;

end_year = floor(end_time);
end_day = year2day(end_time, end_year);

end_hms_frac = end_day;
end_day = floor(end_day);
end_hms_frac = end_hms_frac - end_day;

vars = [ 'year,day,ascdec'];
% for GSO server
%vars = [ 'year,day,time_int' ];
year = 1;				% Column indices of `vals' matrix
day = 2;
time = 3;

% initialize returning variables
url = '';
pass_times = [];
times = [];

% If the request spans two years we must break it up into N requests, one for
% each calendar year within the request.

% Handle single-year queries separately
if (start_year == end_year)
  [vals, url] = qpathcat(catalog_server, vars, start_year, start_day, ...
      end_day);
else
  % Query the CS about the first year.
  [vals, url] = qpathcat(catalog_server, vars, start_year, start_day, ...
      dpyear(start_year));
  % Query about the middle years. Apologies to JR Tolkein.
  % well la-di-da!  Ain't we literary! -- D.B. 98/4/7
  for k = start_year + 1 : end_year - 1
    vals = [vals; qpathcat(catalog_server, vars, k, 1, dpyear(k))];
  end
  % Query about the last year.
  vals = [vals; qpathcat(catalog_server, vars, end_year, 1, end_day)];
end

% The inventory is returned in a single matrix, prune the result so that
% ascending and descend passes at each end are included only if the HMS value
% would have included them. NB: Assume values of 2am/2pm for the ascending
% and descending passes. 2am is 0.0833... and 2pm is 0.5833...

two_am = 2/24;				% Constants in Matlab; How?
two_pm = 14/24;

num_passes = size(vals, 1);
if isempty(vals)
  return;
end

k = 1;					% Start with the first pass.

% If the first pass is an ascend pass and the user's time constraint starts
% after 2:00am, skip the first pass.
if (vals(k, time) == 0 & start_hms_frac > two_am)
  k = k + 1;
  if (vals(k, time) == 1 & start_hms_frac > two_pm)
    k = k + 1;
  end
end

% If the last pass is a descend pass and the user's time constaint ends
% before 2:00pm, skip the last pass.
if (vals(num_passes, time) == 1 & end_hms_frac < two_pm)
  num_passes = num_passes - 1;
  if (vals(num_passes, time) == 0 & end_hms_frac < two_am)
    num_passes = num_passes - 1;
  end
end

while (k <= num_passes)
  pass_times = [pass_times; vals(k, year), vals(k, day), vals(k, time)];
  
  if (vals(k, time) == 0)
    pass_hms_frac = two_am;		% ascending pass; assume 2am
  else
    pass_hms_frac = two_pm;		% descending; assume 2pm
  end
  pass_time = day2year(vals(k, day) + pass_hms_frac, vals(k, year));
  times = [times pass_time];
  k = k + 1;
end    
return;
