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

% The preceding empty line is important
% $Log: gbdriftc.m,v $
% Revision 1.4  2000/03/31 20:53:48  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: gbdriftc.m,v 1.4 2000/03/31 20:53:48 kwoklin Exp $
% klee 04/01/99

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

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

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

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

  StartTime = BeginTime;
  SetDay = 311.5596;
  Yearstring = str2mat('1995','1996');
 
  StartYear = floor(StartTime);  
  DaysInYear = 365;
  if isleap(StartYear)
    DaysInYear = 366;                % This is a leapyear.
  end
  TmpStartYear = 1995;
  StartYrday = (StartTime - StartYear) * DaysInYear;
  if StartYear == 1995, TmpStartYrday = StartYrday; else, TmpStartYrday = StartYrday + 365; end

  LastYear = floor(LastTime);
  LastYrday = (LastTime - LastYear) * DaysInYear;
  if LastYear == 1995 & LastYrday >= SetDay, EndYrday = LastYrday; bool = 1; 
  elseif LastYear == 1996, EndYrday = LastYrday + 365; bool = 1; 
  else EndYrday = LastYrday; bool = 0;
  end

  %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); 

  for i = 1:bool+1
    Constraint = ['&',dayname,'>=',num2str(TmpStartYrday,9), '&',dayname,'<=',num2str(EndYrday,9), URLlat, URLlon];

    returned_var = [];
    returned_var = sprintf('%s', dayname,',',LatitudeName,',',LongitudeName);
    server = [Server, deblank(Yearstring(i,:))];
    url = [server, '?', returned_var, Constraint];
      
    %Now, get the inventory for this year
    loaddods('-F',url);

    %record time, 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; TmpStartYrday,EndYrday,str2num(Llat),...
                    str2num(Ulat),str2num(Llon),str2num(Rlon),str2num(deblank(Yearstring(i,:)))];
      Ndays = size(dayp,1);
      if any(dayp > 365)
        if all(dayp > 365)
          timetemp = (TmpStartYear+1) + (dayp(1:Ndays)-365) / 366;
        else
          DaysIn5 = find(dayp <= 365);
          DaysIn6 = find(dayp > 365);
          timetemp = TmpStartYear + dayp(DaysIn5) / 365;
          timetemp = [timetemp; (TmpStartYear+1) + (dayp(DaysIn6)-365) / 366];
        end
      else
        timetemp = TmpStartYear + dayp(1:Ndays) / 365;
      end
      tmp = find(timetemp >= getranges(4,1) & timetemp <= getranges(4,2));
      times = [times; timetemp(tmp)];
      lats = [lats; eval(LatitudeName)];
      lons = [lons; eval(LongitudeName)];
    end
  end  %end of for loop
  loaddodsbool = 0;
return









