function [url,loaddodsbool,infotext,times,Pass_Times,depths,Pass_Depths,lats,lons,num_url] = nbnerru ...
         (getarchive, getranges, getmode, ...
          getvariablelist, getserverlist,passtimes,passdepths,whichurl)
%
%    This function will build NB NERR file name and return infotext and url
%    to the browser
%

% The preceding empty line is important
% $Log: nbnerru.m,v $
% Revision 1.6  2000/03/31 20:57:11  kwoklin
% Add to handle faulty loaddods call.  klee
%
% Revision 1.5  1999/07/19 20:24:19  kwoklin
% *** empty log message ***
%
% 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
%
% Revision 1.4  1999/06/07 15:41:36  paul
% change NEER to NERR
%
% Revision 1.3  1999/06/04 19:48:52  kwoklin
% Fix file and variable name conflict.       klee
%
% Revision 1.2  1999/06/02 00:01:18  dbyrne
%
%
% Fixed infotext message in nbneeru.m.  Fixed something else in getjgsta.m
% -- dbyrne 99/06/01
%
% Revision 1.1  1999/06/01 07:10:34  dbyrne
%
%
% Moved up from kleedata/
%
% Revision 1.3  1999/05/28 22:53:06  kwoklin
% Point to new server for all jgofs datasets. Update getjgsta on loaddods.
% Fix infotext for all gb geturl files. Fix transmissivity for some gb
% files. Modify some variable names on gb files.       klee
%

% $Id: nbnerru.m,v 1.6 2000/03/31 20:57:11 kwoklin Exp $
% fix depth representation to nominal (deployment) depth    klee 05/27/99
% fix depth representation to min and max only     klee 05/05/99
% klee 04/01/99

% initiate
  num_url = 1;
  infotext = []; times = []; Pass_Times = []; 
  depths = []; Pass_Depths = []; lats = []; lons = [];
  depth = []; lat = []; lon = []; time = []; Constraint = []; URL = [];
% To prevent named file conflict, force variable names to be appeared as variables.  
  time=[]; mintime=[]; maxtime=[]; lat=[]; lon=[]; depth=[];

% reading selected ranges

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

  StartDate = num2str(getranges(4,1),9);
  EndDate = num2str(getranges(4,2),9);

  URLDate = sprintf('%s','&',dods_dbk(DodsName1(2,:)),'<=',EndDate, ...
                         '&',dods_dbk(DodsName1(3,:)),'>=',StartDate);
  URLdate = sprintf('%s','&',dods_dbk(DodsName1(6,:)),'<=',EndDate, ...
                         '&',dods_dbk(DodsName1(6,:)),'>=',StartDate);
  %[times, Pass_Times] = daily2(TimeRange, IntervalTime, getranges);

  Ulat = num2str(getranges(2,2),4);
  Llat = num2str(getranges(2,1),4);
  URLlat = sprintf('%s','&lat>=',Llat,'&lat<=',Ulat);

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

  Ldepth = getranges(3,1);
  Udepth = getranges(3,2);
  %mindepth = num2str(DepthRange(1)); maxdepth = num2str(DepthRange(2));
  %URLdepth = sprintf('%s','&depth>=',mindepth,'&depth<=',maxdepth);
  %URLdepth = sprintf('%s', '&depth>=',Ldepth,'&depth<=',Udepth);
 
  % builds constraints 
  returned_var = [];
  infotext = [];
  nvars = size(SelectableVariables,1);
  nvard = size(DodsName,1);
  if strcmp(getmode, 'cat') | strcmp(getmode, 'datasize')
      %if exist('TextField') == 1 
      %  infotext = sprintf('%s\n','   ',...
      %                            'Every station/site/cruise/inst is assigned a number ranges from 1 to total; ',...
      %                            'The numbers will be stored in RXX_stationno',...
      %                            'Station/site/cruise/inst names and numbers are organized as following:');
      %  for i = 1:size(TextField,1)
      %    text = str2mat(text, sprintf('%s%2d%s%s',DodsName(nvars+1,:), i, ' -- ', TextField(i,:)));
      %  end
      %  infotext = str2mat(infotext, text);
      %end
      returned_var = sprintf('%s',dods_dbk(DodsName1(6,:)), ',', ...
	  LatitudeName, ',', LongitudeName);
      Constraint = sprintf('%s', returned_var, URLDate, URLdate, URLlat, URLlon);
      URL = sprintf( '%s', Server, '?', Constraint);
      depth = []; time = []; lat = []; lon = [];
      loaddods('-F',URL);
      if ~isempty(time)
        if time(1) > 1e-10 
          tmp = find(time >= getranges(4,1) & time <= getranges(4,2));
          times = [times; time(tmp)];
          pc = find(lat >= 41.64 & lon <= -71.34);
          tw = find(lat <= 41.6 & lon >= -71.33);
          %If time/lat/lon/depth combination don't match, return blank.
          if ~isempty(pc) & ~isempty(tw)
            depths = [depths; 3; 4]; 
          elseif isempty(pc) & ~isempty(tw)
            if Ldepth < 4 & Udepth > 4
              depths = [depths; 4];
            else
              times = []; depths = []; lat = []; lon = []; 
              num_url = num_url - 1;
            end
          elseif ~isempty(pc) & isempty(tw)
            %if Udepth < 4
            if Ldepth < 3 & Udepth > 3
              depths = [depths; 3];
            else
              times = []; depths = []; lat = []; lon = [];
              num_url = num_url - 1;
            end
          end
          %mindepth = min(depth); maxdepth = max(depth);
          %depths = [depths; mindepth; maxdepth];
          lats = [lats; lat];
          lons = [lons; lon];
        else
          num_url = num_url - 1;
        end
      else
        num_url = num_url - 1;
      end
      
      loaddodsbool = 0;
  elseif strcmp(getmode, 'get')
      %for i = nvars+1:nvard
      %  returned_var = [returned_var; sprintf('%s',dods_dbk(DodsName(i,:)),',')];
      %end
      if exist('TextField') == 1 
        infotext = sprintf('%s\n', ...
	    'Every station/site/cruise/inst is assigned a number ',...
	    'which ranges from 1 to total; ',...
	    'The numbers will be stored in Rxx_stationno',...
	    'Station/site/cruise/inst names and numbers are ',...
	    'organized as following:');
	 text = '';
        for i = 1:size(TextField,1)
          texti = sprintf('%s%2d%s%s\n',DodsName(nvars+1,:), i, ' -- ', ...
	      TextField(i,:));
	  text = [text texti];
        end
        infotext = [infotext, text];
      end
      for i = 1:size(getvariablelist,1)
          returned_var = [returned_var, dods_dbk(getvariablelist(i,:))]; 
          if strcmp(deblank(getvariablelist(i,:)), 'sal')
            returned_var = [returned_var, sprintf('%s',',',dods_dbk(OptionalVariables(1,:)))];
          end 
          %if strcmp(deblank(getvariablelist(i,:)), 'do')
          %  returned_var = [returned_var, sprintf('%s',',',dods_dbk(OptionalVariables(2,:)))];
          %end
          if i < size(getvariablelist,1)
              returned_var = [returned_var, ','];
          end
      end
      Constraint = sprintf('%s', returned_var, URLDate, URLdate, URLlat, URLlon);
      loaddodsbool = 1;
  end

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

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

  return

  

  

  
