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

% The preceding empty line is important
% $Log: gbnfwsc.m,v $
% Revision 1.6  2000/03/31 21:02:13  kwoklin
% Add to handle falty loaddods call. Fix string mooring bug.
% Fix tmpwhichvariables bug.
%
% Revision 1.5  1999/07/13 14:46:04  kwoklin
% Use getnfws for gbnfws. Fix empty lat/lon return in gbnfwsc. Coded lat/lon
% display in getnfws.   klee
%
% Revision 1.1  1999/06/01 07:10:32  dbyrne
%
%
% Moved up from kleedata/
%
% Revision 1.3  1999/05/28 22:53:05  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: gbnfwsc.m,v 1.6 2000/03/31 21:02:13 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;
  Pass_Times = []; times = [];
  Pass_Depths = []; depths = [];
  lats = []; lons = [];
  url = []; infotext = []; text = []; Constraint = [];
  % To prevent named file conflict, force variable names to be appeared as variables.  
  year = []; yrday0_gmt = [];
 
  yearname = deblank(DodsName1(1,:));
  dayname = deblank(DodsName1(2,:));
  % station [Ntls, 44003, 44005, 44008, 440011, st1]
  blat = [40.5, 40.8, 42.9, 40.5, 41.1, 40.87];
  blon = [-69.4, -68.5, -68.9, -69.4, -66.6, -67.56];

  % 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;
      num_url = num_url + 1;
      EndTime = LastTime;
      EndYrday = LastYrday;
    else
      num_url = num_url + 1;
      EndYrday = DaysInYear;
      StartTime = EndTime;
    end  %end of if EndTime
  
    Constraint = [Constraint, '&',dayname,'<=', num2str(EndYrday,9)];
  
    returned_var = [];
    returned_var = sprintf('%s',yearname,',',dayname, ',', 'mooring');
    url = [Server, '?', returned_var, Constraint];
      
    %Now, get the inventory for this year
    year = []; mooring = [];
    s16 = []; s2 = []; s3 = []; s4 = []; s5 = []; tmps1 = []; tmps6 = [];
    loaddods('-F',url);

    %record time, depth, lat and lon info back to get function
    lon = []; lat = []; days = []; timetemp = []; tmp = []; count = [];
    days = eval(dayname);
    if ~isempty(days) & (days(1) > 1e-10 | days(1) == 0)
      Ndays = size(days,1);
      timetemp = StartYear + days(1:Ndays) / DaysInYear;
      tmp = find(timetemp >= getranges(4,1) & timetemp <= getranges(4,2));
      times = [times; timetemp(tmp)];

      %s16 = find(mooring < 1e-10);
      s16 = find(isnan(mooring));
      if ~isempty(s16)
         tmps1 = find(year(s16)<1995); 
         if ~isempty(tmps1)
           lat(tmps1,1) = blat(1)*ones(length(tmps1),1);  lon(tmps1,1) = blon(1)*ones(length(tmps1),1); 
           %lat = [lat; blat(1)]; lon = [lon; blon(1)];
         end
         tmps6 = find(year(s16)==1995); 
         if ~isempty(tmps6)
           lat(tmps6,1) = blat(6)*ones(length(tmps6),1);  lon(tmps6,1) = blon(6)*ones(length(tmps6),1); 
           %lat = [lat; blat(6)]; lon = [lon; blon(6)]; 
         end
      end
      s2 = find(mooring == 44003);
      if ~isempty(s2), lat(s2,1) = blat(2)*ones(length(s2),1);  lon(s2,1) = blon(2)*ones(length(s2),1); end
        %lat = [lat; blat(2)]; lon = [lon; blon(2)]; end
      s3 = find(mooring == 44005);
      if ~isempty(s3), lat(s3,1) = blat(3)*ones(length(s3),1);  lon(s3,1) = blon(3)*ones(length(s3),1); end
        %lat = [lat; blat(3)]; lon = [lon; blon(3)]; end
      s4 = find(mooring == 44008);
      if ~isempty(s4), lat(s4,1) = blat(4)*ones(length(s4),1);  lon(s4,1) = blon(4)*ones(length(s4),1); end
        %lat = [lat; blat(4)]; lon = [lon; blon(4)]; end
      s5 = find(mooring == 440011);
      if ~isempty(s5), lat(s5,1) = blat(5)*ones(length(s5),1);  lon(s5,1) = blon(5)*ones(length(s5),1); end
        %lat = [lat; blat(5)]; lon = [lon; blon(5)]; end

      tmp = find(lat >= getranges(2,1) & lat <= getranges(2,2) &...
                 lon >= getranges(1,1) & lon <= getranges(1,2));
      lats = [lats; lat(tmp)];
      lons = [lons; lon(tmp)];

      count = StartYear*ones(length(lat),1);
      Pass_Times = [Pass_Times; StartYear,StartYrday,EndYrday];
      Pass_Depths = [Pass_Depths; lat,lon,count];
    else
      num_url = num_url - 1;
    end
  end  %end of while
  if isempty(lons) & isempty(lats)
    num_url = 0;
    times = []; Pass_Times = []; Pass_Depths = [];
  end
  loaddodsbool = 0;

return









