function [values, urls, url] = qpathcat(server, vars, s_yr,e_yr, s_day, e_day, ...
    s_hour, e_hour)
%
% Query the Pathfinder catalog server.  This function no longer breaks
% multi-year queries up into multipe calls to the catalog server.  That
% was to inefficient given the new freeform server that stores all indexes
% for a dataset in a single file (which much be searched in its' entirety
% each time.
%
% Parameters:
% server: URL of the catalog server.
% vars: String containing names of variables to retrieve. Used verbatim in
% the projection part of the constraint expression. This should be a comma
% separated list of variable names, with no spaces. If there is only one
% variable, then the comma should be omitted.
% yr: Year for the constraint.
% s_day, e_day: Start and end day of the query.
%

% $Log: qpathcat.m,v $
% Revision 1.13  1999/07/22 14:29:46  kwoklin
% *** empty log message ***
%

% $Id: qpathcat.m,v 1.13 1999/07/22 14:29:46 kwoklin Exp $

% Build constraint expression
start_time = sprintf('%04d%03d%02d', s_yr, s_day, s_hour);
end_time   = sprintf('%04d%03d%02d', e_yr, e_day, e_hour);
constraint = [ vars '&ptime>=' start_time '&ptime<=' end_time ];

url = [server '?' constraint];
% Clear a variable for the loaddods success test.
% *** initialize each of the variables in vars with null value
[var,ovars] = strtok(vars,',');	
while ~isempty(var)
  eval([var '= [];']);
  [var,ovars] = strtok(ovars,',');	
end  

loaddods(url);
%load /home/matlab/DEV/DODS/tmp
% Look for return values; if none then exit. Note that I extract the strings
% held in `vars' (a comma separated list with no spaces) and load the return 
% parameter `values' with those variables. Even though loaddods loads
% directory into Matlab, it loads into the function workspace and thus, the
% variables (whatever they are called in the dataset) will disappear when
% this call returns. Only the values return parameter will hold the stuff
% retrieved by loaddods.

% if we have returned stuff, we stick it columnwise into values
% if we have no returned stuff, values is set to null.
% it is safe to assume that if one variable is empty the
% others are as well and vice versa. 

% process variables
[var,vars] = strtok(vars,',');
values = [];
urls = [];
while (~isempty(var))
  tmpvalues = values;
  if strcmp(var,'file_url')

    % find the double quote characters, the url strings are
     % between those
     i = findstr(file_url,setstr(34));

     % Pull out the url's and place them in a matrix
     if isempty(i)
       return;
     else
       % If there are url's, setup the matrix with the first one
       urls = setstr(file_url(i(1)+1:i(2)-1));

       % Now tack on the remaining url's
       for j = 3:2:length(i)
	 urls = [urls; setstr(file_url(i(j)+1:i(j+1)-1))];
       end
     end
   elseif strcmp(var,'ptime')
     % ptime is returned as a single number, but we must
     % split it up into hr, day, etc.
     % Column indices of `vals' matrix
     if eval(var) ~= 0  % added for void value return  klee 07/22/99
       year = 1;
       day  = 2;
       hour = 3;
       values = zeros(size(ptime,1),3);
       % get the year
       values(:,1) = floor(ptime/1e5);
       % now subtract off the year;
       ptime = (ptime - floor(ptime/1e5)*1e5);
       % get the day
       values(:,2) = floor(ptime/1e2);
       % now subtract off the day
       ptime = ptime - floor(ptime/1e2)*1e2;
       % the hour
       values(:,3) = ptime;
     else
       values = tmpvalues;
     end
  else
     % We're ignoring other variables right now.
     dodsmsg([ 'Warning from qpathcat: Ignoring variable ' var ]);
  end
  % Move onto the next variable if any
  [var, vars] = strtok(vars, ',');
end
return
