function [url, loaddodsbool, infotext, times, depths, ...
      lats, lons, num_url] = gsodocku (getarchive, ...
    getranges, getmode, getvariablelist, serverlist, ...
    passtimes, passdepths, whichurl)
%
%
%    This function will build GSO Dock file name and return infotext and url
%    to the browser
%

% The preceding empty line is important
% $Log: gsodocku.m,v $
% Revision 1.5  2000/03/31 20:57:11  kwoklin
% Add to handle faulty loaddods call.  klee
%
% Revision 1.4  2000/03/17 08:49:48  dbyrne
%
%
% Fixed a lot of bugs & klugdes.  -- dbyrne 00/03/17
%
% Revision 1.3  2000/03/17 01:25:38  dbyrne
%
%
% Changed gsodocku.m so that CompTime is requested but not returned
% to user.  Cleaned up getgsod.m which was a mess. -- dbyrne 00/03/16
%
% Revision 1.1  2000/03/16 18:48:57  kwoklin
% Modify selection constraint to be based on selected vars instead of vertical
% ranges.   klee
%
% Revision 1.2  2000/01/12 18:51:14  kwoklin
% Second checkup.  klee
%
% Revision 1.1  1999/07/22 19:28:05  kwoklin
% Add R. Platner's local data files in.   klee
%

% $Id: gsodocku.m,v 1.5 2000/03/31 20:57:11 kwoklin Exp $
% Ruth Platner 07/06/99
% This is based on the following revision of nbnerr.m 
% Revision 1.1  1999/06/07 16:06:29  paul
% renamed nbneer.m to nbnerr.m     renamed nbneeru.m to nbnerru.m
% renamed nbneeru to nbnerru inside nbnerr.m
%

% initiate
  num_url = 1; callbool = 1; depthbool = 1; barpresbool = 1; Constraint = [];
  infotext = []; times = []; 
  depths = []; lats = []; lons = [];
  Depth = []; lat = []; lon = []; CompTime = [];
% To prevent named file conflict, force variable names to be appeared as variables.  
  CompTime=[]; lat=[]; lon=[]; Depth=[]; BarPres = [];

% reading selected ranges

  if exist(getarchive) == 2
    eval(getarchive)
  else
    dodsmsg('Metadata not found!')
    return
  end

% find dates in CompTime format for user selected time period

  dref = DataTime;
  decstartdate = getranges(4,1);
  decenddate = getranges(4,2);

% Find the time range  and build the constraint

  StartDate = num2str(year2day(decstartdate,dref),9);
  EndDate = num2str(year2day(decenddate,dref),9);
  URLdate = sprintf('%s','&',deblank(DodsName1(3,:)),'<=',EndDate, ...
                         '&',deblank(DodsName1(3,:)),'>=',StartDate);

% Find the geographic range selected by user and build the constraint

  Ulat = num2str(getranges(2,2),6);
  Llat = num2str(getranges(2,1),6);
  URLlat = sprintf('%s','&lat>=',Llat,'&lat<=',Ulat);
  Llon = num2str(getranges(1,1),6); 
  Rlon = num2str(getranges(1,2),6); 
  % for [-180, 180]  
  URLlon = sprintf('%s','&lon>=',Llon,'&lon<=',Rlon);

% Find the depth range selected by user and build the constraint. 

  ldepth = getranges(3,1);
  udepth = getranges(3,2);
  Ldepth = num2str(ldepth,7);
  Udepth = num2str(udepth,7);
  isbar = 0; isdepth = 0;
  for i = 1:size(getvariablelist,1)
    if (strcmp(deblank(getvariablelist(i,:)), 'Barometric_Pres') | ...
        strcmp(deblank(getvariablelist(i,:)), 'BarPres'))
      isbar = 1;
    elseif (strcmp(deblank(getvariablelist(i,:)), 'Tidal_Height') | ...
            strcmp(deblank(getvariablelist(i,:)), 'Depth'))
      isdepth = 1;
    end
  end
  if ldepth <= 0 & udepth >= 0
    if ldepth <= -735.08
      if (isbar & isdepth) | (~isbar & ~isdepth)
        URLdepth = sprintf('%s', '&Depth>=',Ldepth,'&Depth<=',Udepth,...
                                 '&BarPres<=',num2str(-ldepth,7),'&BarPres>=',Udepth);
      elseif (isbar & ~isdepth)
        URLdepth = sprintf('%s','&BarPres<=',num2str(-ldepth,7),'&BarPres>=',Udepth);
      elseif (~isbar & isdepth)
        URLdepth = sprintf('%s', '&Depth>=',Ldepth,'&Depth<=',Udepth);
      end
    else
      if isbar & ~isdepth
        dodsmsg('Selected BarPres is out of range: BarPres = [735.08 785.11]');
        callbool = 0;
      else
        URLdepth = sprintf('%s', '&Depth>=',Ldepth,'&Depth<=',Udepth);
        barpresbool = 0;
      end
    end
  elseif ldepth >= 0 & udepth >= 0
    if isbar & ~isdepth
      dodsmsg('Selected BarPres is out of range: BarPres = [735.08 785.11]');
      callbool = 0;
    else
      URLdepth = sprintf('%s', '&Depth>=',Ldepth,'&Depth<=',Udepth);
      barpresbool = 0;
    end
  elseif ldepth <= 0 & udepth <= 0
    if ldepth <= -735.08 
      if isdepth & ~isbar
        dodsmsg('Selected Tidal_Height is out of range: Tidal_Height = [0.0 3.5]');
        callbool = 0;
      else
        URLdepth = sprintf('%s','&BarPres<=',num2str(-ldepth,7),'&BarPres>=',...
                           num2str(-udepth,7));
        depthbool = 0;
      end
    else
      callbool = 0; depthbool = 0; barpresbool = 0;
    end
  end

  % builds constraints 
  returned_var = [];
  infotext = [];
  nvars = size(SelectableVariables,1);
  nvard = size(DodsName,1);
  if strcmp(getmode, 'cat') | strcmp(getmode, 'datasize')
      if callbool == 1
        if (isbar & isdepth) | (~isbar & ~isdepth)
           returned_var = sprintf('%s',deblank(DodsName1(3,:)), ',', ...
	       LatitudeName, ',', LongitudeName, ',', 'Depth,BarPres');
        elseif isbar & ~isdepth
           returned_var = sprintf('%s',deblank(DodsName1(3,:)), ',', ...
               LatitudeName, ',', LongitudeName, ',', 'BarPres');
        elseif isdepth & ~isbar
           returned_var = sprintf('%s',deblank(DodsName1(3,:)), ',', ...
	      LatitudeName, ',', LongitudeName, ',', 'Depth');
        end
          
        Constraint = sprintf('%s', returned_var, URLdate, URLlat, URLlon, URLdepth);
        URL = sprintf( '%s', Server, '?', Constraint);
        if findstr(URL, 'nph-jg')
          loaddods('-F',URL);
        else
          loaddods(URL);
        end
        if ~isempty(CompTime)
          if CompTime(1) > 1e-10 
            % convert times to decimal times
            decimaltimes = d2years(CompTime,DataTime);
            tmp = find(decimaltimes >= getranges(4,1) & decimaltimes <= getranges(4,2));
            times = [times; decimaltimes(tmp)]; 
            tmpdepth = find(Depth >= getranges(3,1) & Depth <= getranges(3,2));
            tmpbarpres = find(BarPres <= -getranges(3,1) & BarPres >= -getranges(3,2));
            depths = [depths; Depth(tmpdepth); -BarPres(tmpbarpres)];
            lats = [lats; lat];
            lons = [lons; lon];
          else
            num_url = num_url - 1;
          end
        else
          num_url = num_url - 1;
        end
      else
        num_url = 0;
      end % end of callbool == 1
      loaddodsbool = 0;
  elseif strcmp(getmode, 'get')
      vartmp = [];
      for i = 1:size(getvariablelist,1)
          vartmp = deblank(getvariablelist(i,:));
          if ~strcmp(vartmp, 'Decimal_Years')
            returned_var = [returned_var, vartmp];
            if i < size(getvariablelist,1)
              returned_var = [returned_var, ','];
	    end
	  else % if user requests decimal time, request
	    % comptime so this can be computed
            returned_var = [returned_var, 'CompTime'];
            if i < size(getvariablelist,1)
	      returned_var = [returned_var, ','];
	    end
          end % end of strcmp
      end % end of for loop
      Constraint = sprintf('%s', returned_var, URLdate, URLlat, URLlon, URLdepth);
      loaddodsbool = 1;
  end

  % builds URL w/ server (Server2 in archive.m) and Constraint

  URL = sprintf( '%s', Server, '?', Constraint);
  url = URL;

  return
