function [url,loaddodsbool,infotext,times,Pass_Times,depths,Pass_Depths,lats,lons,num_url] = gbemetc ...
         (getarchive, getranges, getmode)
%
%    This function will build gbemetw_x file names and return infotext and 
%    time/depth/lat/lon/url to the browser
%

% The preceding empty line is important
% $Log: gbemetc.m,v $
% Revision 1.4  2000/03/31 20:53:49  kwoklin
% Add to handle falty loaddods call.    klee
%
% Revision 1.3  1999/06/24 19:05:00  kwoklin
% Change file name of nbnerr.m. Fix some display bugs, define precision level
% of lat/lon/depth/time, and fix some more bugs in globec files.        klee
%
% Revision 1.1  1999/06/01 07:10:29  dbyrne
%
%
% Moved up from kleedata/
%
% Revision 1.3  1999/05/28 22:53:04  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: gbemetc.m,v 1.4 2000/03/31 20:53:49 kwoklin Exp $
% fix pressure representation to lat/lon basis     klee 05/28/99
% fix depth representation to min and max only     klee 05/05/99
% klee 04/99

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

  % Initialize variables.
  More = 1;
  num_url = 0;
  Pass_Times = []; times = [];
  Pass_Depths = []; depths = [];
  lats = []; lons = [];
  depthtemp = [];
  url = []; infotext = []; text = [];
  % To prevent named file conflict, force variable names to be appeared as variables.
  year_gmt = []; yrday_utc = []; lat = []; lon = []; press_bar = [];

  yearname = deblank(DodsName1(1,:));
  dayname = deblank(DodsName1(2,:));

  % Get beginning and ending of time interval.
  BeginTime = max(TimeRange(1), getranges(4,1));
  LastTime = min(TimeRange(2), getranges(4,2));

  StartTime = BeginTime;

  LastYear = floor(LastTime);
  DaysInYear = 365;
  if rem(LastYear,4) == 0 & LastYear ~= 1900
    DaysInYear = 366;                % This is a leapyear.
  end
  LastYrday = (LastTime - LastYear) * DaysInYear;

  % Iterate, making one call to the catalog server for every year requested in
  % the time range. 

  while More == 1
  
    % Begin by defining starting constraint.
  
    StartYear = floor(StartTime);  
    DaysInYear = 365;
    if isleap(StartYear)
      DaysInYear = 366;                % This is a leapyear.
    end
    StartYrday = (StartTime - StartYear) * DaysInYear;

    Constraint = ['&',yearname,'=',num2str(StartYear),'&',dayname,'>=',num2str(StartYrday,9)];
  
    EndTime = StartYear + 1;
    EndYear = floor(EndTime);
  
    % Now for ending constraint.  
    if EndTime >= LastTime % All in same year
      More = 0;
      EndTime = LastTime;
      EndYrday = LastYrday;
      % work around for year-end bug  K.L. 12/98
      %if (EndTime == LastTime) & (EndTime == TimeRange(2))
      %  EndYrday = DaysInYear;      
      %end
    else
      EndYrday = DaysInYear;
      StartTime = EndTime;
    end  %end of if EndTime
  
    Constraint = [Constraint, '&',dayname,'<=',num2str(EndYrday,9)];

    %Now, for depth constraint
    Ldepth = num2str(-getranges(3,2),9);
    Udepth = num2str(-getranges(3,1),9);
    URLdepth = sprintf('%s', '&', DepthName, '>=', Ldepth, '&', DepthName, '<=', Udepth);

    %Now, for lat and lon constraint
    Ulat = num2str(getranges(2,2));
    Llat = num2str(getranges(2,1));
    URLlat = sprintf('%s','&', LatitudeName, '>=', Llat, '&', LatitudeName, '<=', Ulat);

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

    Constraint = [Constraint, URLdepth, URLlat, URLlon];   
  
    returned_var = [];
    returned_var = sprintf('%s', yearname,',',dayname,',',LatitudeName,',',LongitudeName,',',DepthName);
    server = [Server, num2str(StartYear)];
    url = [server, '?', returned_var, Constraint];
      
    %Now, get the inventory for this year
    loaddods('-F',url);

    %record time, depth, lat and lon info back to get function
    dayp = [];
    dayp = eval(dayname);
    if ~isempty(dayp) & (dayp(1) > 1e-10 | dayp(1) == 0)
      num_url = num_url + 1;
      Pass_Times = [Pass_Times; StartYear,StartYrday,EndYrday,str2num(Ldepth),str2num(Udepth),str2num(Llat),...
                    str2num(Ulat),str2num(Llon),str2num(Rlon)];
      Ndays = size(dayp,1);
      timetemp = StartYear + dayp(1:Ndays) / DaysInYear;
      tmp = find(timetemp >= getranges(4,1) & timetemp <= getranges(4,2));
      times = [times; timetemp(tmp)];
      depths = [depths; -(eval(DepthName))];
      %depthtemp = [depthtemp; eval(DepthName)];
      lats = [lats; eval(LatitudeName)];
      lons = [lons; eval(LongitudeName)];
    end
  end  %end of while
  %maxdepth = max(depthtemp);   mindepth = min(depthtemp);
  %depths = [depths; -maxdepth; -mindepth];
  loaddodsbool = 0;
return









