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

% The preceding empty line is important
% $Log: merldu.m,v $
% Revision 1.4  2000/03/31 20:57:11  kwoklin
% Add to handle faulty loaddods call.  klee
%
% Revision 1.3  2000/03/17 08:49:48  dbyrne
%
%
% Fixed a lot of bugs & klugdes.  -- dbyrne 00/03/17
%
% Revision 1.2  2000/01/12 18:51:14  kwoklin
% Second checkup.  klee
%
% Revision 1.1  1999/07/22 19:28:06  kwoklin
% Add R. Platner's local data files in.   klee
%

% $Id: merldu.m,v 1.4 2000/03/31 20:57:11 kwoklin Exp $
% Ruth Platner  07/06/99
% This is based on the following revision of nbnerr.m
% $Log: merldu.m,v $
% Revision 1.4  2000/03/31 20:57:11  kwoklin
% Add to handle faulty loaddods call.  klee
%
% Revision 1.3  2000/03/17 08:49:48  dbyrne
%
%
% Fixed a lot of bugs & klugdes.  -- dbyrne 00/03/17
%
% Revision 1.2  2000/01/12 18:51:14  kwoklin
% Second checkup.  klee
%
% Revision 1.1  1999/07/22 19:28:06  kwoklin
% Add R. Platner's local data files in.   klee
%
% 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; Constraint = [];
  infotext = []; times = []; tmptimes = [];
  depths = []; lats = []; lons = [];
  depth = []; lat = []; lon = []; time = [];
  url = '';
% To prevent named file conflict, force variable names to be appeared as variables.  
  time=[]; lat=[]; lon=[]; depth=[];

% reading selected ranges

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

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

  StartDate = num2str(getranges(4,1),6);
  EndDate = num2str(getranges(4,2),6);
  URLdate = sprintf('%s','&',dods_dbk(DodsName1(3,:)),'<=',EndDate, ...
                         '&',dods_dbk(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);
  %URLdepth = sprintf('%s', '&depth>=',Ldepth,'&depth<=',Udepth);
 
  % builds constraints 
  returned_var = [];
  infotext = [];  
  callbool = 0;
  nvars = size(SelectableVariables,1);
  nvard = size(DodsName,1);
  if strcmp(getmode, 'cat') | strcmp(getmode, 'datasize')
      enddate = str2num(EndDate); startdate = str2num(StartDate);
      if startdate < 1997.00 & enddate < 1997.00 & Ldepth <= 2 & Udepth >= 2
        callbool = 1; 
      elseif startdate >= 1997.00 & enddate >= 1997.00 & Ldepth <= 0.1 & Udepth >= 0.1
        callbool = 1;
      elseif startdate < 1997.00 & enddate >= 1997.00 & Ldepth <= 2 & Udepth >= 0.1
        callbool = 1;
        if Ldepth > 0.1 & Udepth < 2
          callbool = 0;
        end
      end
      if callbool == 1
        returned_var = sprintf('%s',dods_dbk(DodsName1(3,:)), ',', ...
	    LatitudeName, ',', LongitudeName, ',', DepthName);
        %Constraint = sprintf('%s', returned_var, URLdate, URLlat, URLlon, URLdepth);
        Constraint = sprintf('%s', returned_var, URLdate, URLlat, URLlon);
        URL = sprintf( '%s', Server, '?', Constraint);
        if findstr(URL, 'nph-jg')
          loaddods('-F',URL);
        else
          loaddods(URL);
        end
        if ~isempty(time)
          if time(1) > 1e-10 
            tmp = find(time >= getranges(4,1) & time <= getranges(4,2));
            tmptimes = [times; time(tmp)];
            %If time/lat/lon/depth combination don't match, return blank.
            if any(tmptimes < 1997.00) & Ldepth <= 2 & Udepth >= 2
              depths = [depths; 2]; times = [times; tmptimes(find(tmptimes < 1997.00))];
            end
            if any(tmptimes >= 1997.00) & Ldepth <= 0.1 & Udepth >= 0.1
              depths = [depths; 0.1]; times = [times; tmptimes(find(tmptimes >= 1997.00))];
            end
            %tmp = find(depth >= getranges(3,1) & depth <= getranges(3,2));
            %depths = [depths; depth(tmp)];
            if ~isempty(depths)
              lats = [lats; lat];
              lons = [lons; lon];
            else
              num_url = 0;
              lats = []; lons = []; times = []; depths = [];
            end
          else
            num_url = num_url - 1;
          end
        else
          num_url = num_url - 1;
        end
      end
      loaddodsbool = 0;
  elseif strcmp(getmode, 'get')
      for i = 1:size(getvariablelist,1)
          returned_var = [returned_var, dods_dbk(getvariablelist(i,:))]; 
          if i < size(getvariablelist,1)
              returned_var = [returned_var, ','];
          end
      end
      %Constraint = sprintf('%s', returned_var, URLdate, URLlat, URLlon, URLdepth);
      Constraint = sprintf('%s', returned_var, URLdate, URLlat, URLlon);
      loaddodsbool = 1;
  end

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

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

  return
