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

% The preceding empty line is important
% $Log: gbenssc.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:30  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: gbenssc.m,v 1.4 2000/03/31 20:53:48 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/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;  
  Pass_Times = []; times = [];
  Pass_Depths = []; depths = [];
  lats = []; lons = [];
  depthtemp = [];
  url = []; infotext = []; text = []; Constraint = [];
  % To prevent named file conflict, force variable names to be appeared as variables.
  gmtyear=[]; year_gmt=[]; gmtmonth=[]; month_gmt=[]; gmtday=[]; day_gmt=[]; gmttime=[]; time_gmt=[];
  lat=[]; lon=[]; metpress=[]; press_bar=[]; 

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

  %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(1,:),'>=',Llat,'&',LatitudeName(1,:),'<=',Ulat);

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

  Constraint = [URLdepth, URLlat, URLlon];

  % 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;
  MonthFirstDay = [1 32 60 91 121 152 182 213 244 274 305 335 400];
  if isleap(LastYear)
    DaysInYear = 366;                % This is a leapyear.
    MonthFirstDay = [1 32 61 92 122 153 183 214 245 275 306 336 400];
  end
  LastDay = (LastTime - floor(LastTime)) * DaysInYear + 1;  
  LastDay = floor(LastDay);
  LastMonth = min(find(LastDay < MonthFirstDay)) - 1;
  LastMonthDay = LastDay - MonthFirstDay(LastMonth) + 1;

  tmp = []; depthtemp = []; lattemp = []; lontemp = [];
  while More == 1
    StartYear = floor(StartTime);
    DaysInYear = 365;
    MonthFirstDay = [1 32 60 91 121 152 182 213 244 274 305 335 400];
    MonthDay = [31 28 31 30 31 30 31 31 30 31 30 31];
    if isleap(StartYear)
      DaysInYear = 366;                % This is a leapyear.
      MonthFirstDay = [1 32 61 92 122 153 183 214 245 275 306 336 400];
      MonthDay = [31 29 31 30 31 30 31 31 30 31 30 31];
    end
    StartDay = (StartTime - floor(StartTime)) * DaysInYear + 1;
    StartDay = floor(StartDay);
    StartMonth = max(find(StartDay >= MonthFirstDay));
    StartMonthDay = StartDay - MonthFirstDay(StartMonth) + 1;

    EndTime = StartYear + 1;
    EndYear = floor(EndTime);
    if EndTime >= LastTime   %all in the same year
      More = 0;
      EndTime = LastTime; EndMonth = LastMonth; EndMonthDay = LastMonthDay;
    else
      EndMonth = 12; EndMonthDay = 31; EndHour = 24; EndMin = 60;
      StartTime = EndTime;
    end

    manymonths = EndMonth - StartMonth + 1;
    if manymonths > 1
       months = StartMonth:EndMonth; 
       startmondays = [StartMonthDay; ones(size(months,2)-1,1)];
       lastmondays = [reshape(MonthDay(StartMonth:EndMonth-1),EndMonth-StartMonth,1); EndMonthDay];
    else  
       months = StartMonth; startmondays = StartMonthDay; lastmondays = EndMonthDay;
    end
    for i = 1:manymonths
      ce = ['&',yearname,'=',num2str(StartYear),'&',monthname,'=',num2str(months(i))];
      ce = [ce, '&',dayname,'>=',num2str(startmondays(i,1))];
      ce = [ce, '&',dayname,'<=',num2str(lastmondays(i,1))];
      constraint = [Constraint, ce];

      returned_var = [];
      returned_var = sprintf('%s', yearname, ',', monthname, ',', dayname, ',', ...
                             LatitudeName(1,:), ',', LongitudeName(1,:), ',', DepthName);
      url = [Server, '?', returned_var, constraint];

      loaddods('-F',url);

      depthtemp = depths; lattemp = lats; lontemp = lons;
      dayp = [];
      dayp = eval(dayname);
      if ~isempty(dayp) & dayp(1) > 1e-10
        %num_url = num_url + 1;
        Ndays = size(dayp,1);
        monthp = MonthFirstDay(months(i));
        daytemp = ((monthp-1) + dayp(1:Ndays) - 1) / DaysInYear;
        timetemp = StartYear + daytemp;
        tmp = find(timetemp >= getranges(4,1) & timetemp <= getranges(4,2));
        if isempty(tmp)
          depths = depthtemp; lats = lattemp; lons = lontemp; 
        else
          num_url = num_url + 1;
          tmpstartmondays = round(min(daytemp(tmp)) * DaysInYear) - (monthp - 1) + 1;
          tmplastmondays = round(max(daytemp(tmp)) * DaysInYear) - (monthp - 1) + 1;
          times = [times; timetemp(tmp)];
          Pass_Times = [Pass_Times; StartYear,months(i),tmpstartmondays,tmplastmondays,str2num(Ldepth),...
                        str2num(Udepth),str2num(Llat),str2num(Ulat),str2num(Llon),str2num(Rlon)];

          depths = [depths; -(eval(DepthName))];
          %depthtemp = [depthtemp; eval(DepthName)];
          lats = [lats; eval(LatitudeName(1,:))];
          lons = [lons; eval(LongitudeName(1,:))];
        end
      end  %end of if dayp(1) 
    end  %end of for loop  
  end  %end of while loop
  %depths = [depths; -max(depthtemp); -min(depthtemp)];
  loaddodsbool = 0;

return
  

  
