function [values, url] = qpathcat(server, vars, yr, s_day, e_day)
%
% Query the Pathfinder catalog server. This function is designed to be called
% from a loop so that multi-year queries can be broken up into multiple calls.
% Note that the function does not use any hourly information so any excess
% hits based on hour constraints must be removed by the caller.
%
% 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.
%
% $Id: qpathcat.m,v 1.8 1998/11/10 21:22:30 dbyrne Exp $

% $Log: qpathcat.m,v $
% Revision 1.8  1998/11/10 21:22:30  dbyrne
%
%
% Updated to use new JPL/PODAAC catalogue server.
%
% Revision 1.2  1998/11/10 14:12:03  root
% fixed up to use PODAAC CS.  dbyrne 98/11/10
%
% Revision 1.1  1998/05/17 14:18:12  dbyrne
% *** empty log message ***
%
% Revision 1.2  1997/12/04 18:51:39  jimg
% Now uses the comma separated list of variable names to load the return
% parameter `values'. I think that this function could be used by other
% catalog servers access functions.
%
% Revision 1.1  1997/12/03 21:06:29  jimg
% Added.
%

constraint = [ vars '&year=' int2str(yr) ];
values = []; url = [];
if (s_day == e_day)
    constraint = [ constraint '&day=' int2str(s_day) ];
else
    constraint = [ constraint '&day>=' int2str(s_day) '&day<=' ...
	      int2str(e_day) ];
end

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  

% this returns all STRINGS because it is a jgofs server.  Some (but
% not all) of the string must be reinterpreted as numbers.
loaddods(url);

% 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. 
[var,vars] = strtok(vars,',');
values = [];
while (~isempty(var))
  if ~isempty(eval(var))
    if isstr(eval(var))
      if strcmp(deblank(var),'year') | strcmp(deblank(var),'day')
	var = eval(var);
	% find the newline characters
	i = findstr(var,setstr(10));
	i = [0 i];
	for j = 1:length(i)-1
	  value(j) = str2num(var(i(j)+1:i(j+1)-1));
	end
	values = [values, value(:)];
      elseif strcmp(deblank(var),'ascdec')
	var = eval(var);
	% find the newline characters
	i = findstr(var,setstr(10));
	i = [0 i];
	for j = 1:length(i)-1
	   if strcmp(var(i(j)+1:i(j+1)-1),'a');
	     value(j) = 0;
	   elseif strcmp(var(i(j)+1:i(j+1)-1),'d');
	     value(j) = 1;
	   end
	end
	values = [values, value(:)];
      end
    else % whatever is in var is not a string
      values = [values, eval(var)];
    end
  end
  [var, vars] = strtok(vars, ',');
end    
return;
