function [times, pass_times, url] = jplczcat(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 and month.
%
%----------------------------------------------------------------------------

% 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 temporary czcs dataset contains one image per month

% Find starting year and starting month, months numbered 1 thru 12
start_year  = floor(start_time);
start_month = start_time - start_year;
start_month = start_month * 12;
start_month = floor(start_month + 1);

% Find ending year and ending month
end_year  = floor(end_time);
end_month = end_time - end_year;
end_month = end_month * 12;
end_month = floor(end_month + 1);

vars = [ 'year,month'];
year = 1;				% Column indices of `vals' matrix
month = 2;

% Note: At some point, the url should get served by the catalog
%       server.  However that is inflexibly-coded in jplczurl.m for now.

% 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] = qjplczct(catalog_server, vars, start_year, start_month, ...
      end_month);
else
  % Query the CS about the first year.
  [vals, url] = qjplczct(catalog_server, vars, start_year, start_month, 12);
  % Query about the middle years.
  for k = start_year + 1 : end_year - 1
    vals = [vals; qjplczct(catalog_server, vars, k, 1, 12)];
  end
  % Query about the last year.
  vals = [vals; qjplczct(catalog_server, vars, end_year, 1, end_month)];
end

if isempty(vals)
  return;
end

% Move the results into the return variables
for k = 1:size(vals,1);
  pass_times = [ pass_times; vals(k,year), vals(k,month)];
  % year plus fraction of year given month and then add
  % to get to the middle of the month.
  pass_time = vals(k,year) + ((vals(k,month) - 1)/12) + 1/24;
  times = [times pass_time];
end
% subselect only the valid passes
i = find(times >= start_time & times <= end_time);
times = times(i);
pass_times = pass_times(i,:);

return;


