function [values, url] = qss35cat(server, vars, yr, s_month, e_month)
%
% Query the ssmi level 3.5 catalog server. This function is designed to be
% called from a loop so that multi-year queries can be broken up into
% multiple calls.
%
% 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.
%

% Build the constraint
constraint = [ vars '&year=' int2str(yr) ];
values = []; url = [];
if (s_month == e_month)
    constraint = [ constraint '&month=' int2str(s_month) ];
else
    constraint = [ constraint '&month>=' int2str(s_month) '&month<=' ...
	      int2str(e_month) ];
end

% Build the url
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 ~strcmp(eval(var),setstr(10))
  % a quick fix for null return (empty string contains newline still)     klee
    if isstr(eval(var))
      if strcmp(deblank(var),'year') | strcmp(deblank(var),'month')
	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(:)];
      end
    else % whatever is in var is not a string
      values = [values, eval(var)];
    end
  end
  end
  [var, vars] = strtok(vars, ',');
end    
return;


