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

% The preceding empty line is important
% $Log: gbmcmcat.m,v $
% Revision 1.5  2000/03/31 20:53:49  kwoklin
% Add to handle falty loaddods call.    klee
%
% Revision 1.4  1999/06/25 16:31:45  kwoklin
% Expand lon range for clearer display.     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: gbmcmcat.m,v 1.5 2000/03/31 20:53:49 kwoklin Exp $
% fix depth representation to station basis            klee 05/27/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 = []; depth = [];
  lats = []; lons = []; lat = []; lon = []; 
  depthtemp = [];
  url = []; infotext = []; text = [];

  % To prevent named file conflict, force variable names to be appeared as variables.  
  yrday_utc = []; longitude = []; latitude = []; depth = [];
  
  %station = [c2, c2a, necw, necwa, nece, necea, nep];
  LatArray = [43.0442, 43.0430, 42.1272, 42.1254, 42.2978, 42.2963, 41.7335];
  LonArray = [-65.7817, -65.7793, -66.0118, -66.0114, -65.8450, -65.8404, -66.5383];

  % 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 = ['&yrday_utc>=',num2str(StartYrday,9)];
  
    EndTime = StartYear + 1;
    EndYear = floor(EndTime);
    EndArray = [1994.00, 1994.48000, 1995.00, 1995.43000, 1995.9000, 1996.00, 1996.8];
    TmpEndYrday = [365, 175.5, 365, 157, 328.6, 365, 292.8];

    % Now for ending constraint.  
    if EndTime >= LastTime % All in same year
      More = 0;
      Tmplast = find(LastTime <= EndArray);   
      Tmpstart = find(StartTime < EndArray);
    else
      Tmplast = find(EndTime <= EndArray);   
      Tmpstart = find(StartTime < EndArray);
      StartTime = EndTime;
    end  %end of if EndTime

    TmpEnd = [min(Tmpstart):min(Tmplast)];
    TmpLen = length(TmpEnd);
    num_url = num_url + TmpLen;

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

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

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

    Constraint = [Constraint, URLdepth, URLlat, URLlon];   
  
    returned_var = [];
    returned_var = sprintf('%s', TimeName, ',', LatitudeName, ',', LongitudeName, ',', DepthName);
    TmpServer = str2mat('93_fall','94_spring','94_fall','95_spring','95_sumfal','95_falwin','96_winsum');
    for i = 1:TmpLen
      Tmpendyrday = TmpEndYrday(TmpEnd(i));
      if LastTime <= (Tmpendyrday/DaysInYear + StartYear), Tmpendyrday = LastYrday; end
      constraint = [Constraint, '&yrday_utc<=', num2str(Tmpendyrday)];
      whichserver = TmpEnd(i);
      server = [Server, deblank(TmpServer(TmpEnd(i),:))];
      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(deblank(DodsName1(1,:)));
      if dayp(1) > 1e-10 & if ~isempty(dayp)
        %Pass_Times = [Pass_Times; StartYear,StartYrday,Tmpendyrday,str2num(Ldepth),str2num(Udepth),str2num(Llat),...
        %              str2num(Ulat),str2num(Llon),str2num(Rlon),whichserver];
        tmptimes = times;
        Ndays = size(dayp,1);
        timetemp = StartYear + dayp(1:Ndays) / DaysInYear;
        tmp = find(timetemp >= getranges(4,1) & timetemp <= getranges(4,2));
        times = [times; timetemp(tmp)];
        
        % following are the tangling of getting the right time/lat/lon/depth combination
        lat = []; lon = [];
        c21 = find(latitude == 43.0425 | (TmpEnd(i) <= 4 & latitude == 43.0430));
        c22 = find(latitude == 43.0455 | latitude == 43.0457);
        c2a = find(TmpEnd(i) > 4 & latitude == 43.0430);
        necw1 = find(latitude == 42.1255);
        necw2 = find(latitude == 42.1287 | latitude == 42.1272);
        necwa = find(latitude == 42.1267 | latitude == 42.1247);
        nece1 = find(latitude == 42.2940 | latitude == 42.2982);
        nece2 = find(latitude == 42.2987);
        necea = find(latitude == 42.2963);
        nep = find(latitude == 41.7335);
        udepth = str2num(Udepth); ldepth = str2num(Ldepth);
        tmpdepths = depths;
        
        if ~isempty(c21) & (ldepth <= 27 & udepth > 27)
           lat = [lat; 43.0442]; lon = [lon; -65.7817]; depths = [depths; 27;47;98]; end
        if ~isempty(c22) & (ldepth <= 47 & udepth > 47)
           lat = [lat; 43.0442]; lon = [lon; -65.7817]; depths = [depths; 47; 98]; end
        if ~isempty(c2a) & (udepth <= 26.5 & ldepth >= 26.5)
           lat=[lat; 43.0430]; lon=[lon; -65.7793]; depths = [depths; 26.5]; end
        if ~isempty(necw1) & (ldepth <= 22 & udepth > 22)
           lat=[lat; 42.1272]; lon=[lon; -66.0118]; depths=[depths; 22;51.5;102.5;152.5;189;195]; end
        if ~isempty(necw2) & (ldepth <= 51 & udepth > 51)
           lat=[lat; 42.1272]; lon=[lon; -66.0118]; depths=[depths; 51.5;102.5;152.5;189;195]; end
        if ~isempty(necwa) & (ldepth <= 24.5 & udepth > 24.5)
           lat=[lat; 42.1254]; lon=[lon; -66.0114]; depths=[depths; 24.5]; end
        if ~isempty(nece1) & (ldepth <= 22.5 & udepth > 22.5)
           lat=[lat; 42.2978]; lon=[lon; -65.8450]; depths=[depths; 22.5;49;100;150;195]; end
        if ~isempty(nece2) & (ldepth <= 49 & udepth >49)
           lat=[lat; 42.2978]; lon=[lon; -65.8450]; depths=[depths; 49;100;150;195]; end
        if ~isempty(necea) & (ldepth <= 23.5 & udepth > 23.5)
           lat=[lat; 42.2963]; lon=[lon; -65.8404]; depths=[depths; 23.5]; end
        if ~isempty(nep) & (ldepth <= 23 & udepth > 23)
           lat=[lat; 41.7335]; lon=[lon; -66.5383]; depths=[depths; 23;37;59]; end
        %depths = [depths; depth];
        %depthtemp = [depthtemp; eval(DepthName)];
        lats = [lats; lat];
        lons = [lons; lon];
        if isempty(lat) | isempty(lon)
           num_url = num_url - 1; depths = tmpdepths; times = tmptimes;
        else
           Pass_Times = [Pass_Times; StartYear,StartYrday,Tmpendyrday,str2num(Ldepth),str2num(Udepth),...
                         str2num(Llat),str2num(Ulat),str2num(Llon),str2num(Rlon),whichserver];
        end
      else
        num_url = num_url - 1;
      end  
    end
  end  %end of while
  %depths = [depths; max(depthtemp); min(depthtemp)];
  loaddodsbool = 0;

return









