function [times, Pass_Times, depths, Pass_Depths, cs] =  ...
       usgsmcat(CatalogServer, get_archive, Time, Depth, ranges)

%
% This function passes time and depth information to getfunction
%

% The preceding empty line is important.
% $Log: usgsmcat.m,v $
% Revision 1.4  1999/09/14 18:52:05  kwoklin
% Fix empty StartDate and initialization bug.   kwoklin
%
% Revision 1.1  1999/06/01 07:10:34  dbyrne
%
%
% Moved up from kleedata/
%
% Revision 1.2  1999/05/28 17:32:51  kwoklin
% Add all globec datasets. Fix depth representation for all globec datasets
% and nbneer dataset. Fix frontal display for htn and glkfront. Make use of
% getjgsta for all jgofs datasets. Point usgsmbay to new server. Point htn,
% glk, fth and prevu to new FF server.                                 klee
%

% $Id: usgsmcat.m,v 1.4 1999/09/14 18:52:05 kwoklin Exp $
% klee   05/05/99

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

Pass_Times = [];  times = [];  timetemp = [];
Pass_Depths = []; depths = []; depthtemp = [];
time = []; timein = []; 
cs = []; Constraint = [];

BaseTime = Time(1);   % == das basetime
BaseYear = 1989;
BaseDay = 273;        % == das basedate  (10/01/1989)
DivideDay = 1208.36;  % == timedivide based on BaseDay
BaseYear = floor(BaseTime);
StartTime = max(Time(1), ranges(4,1));   
LastTime = min(Time(2), ranges(4,2));    

if LastTime <= TimeDivide, nload = 1; else, nload = 2; end

Constraint = ['time'];
for i = 1:nload
  eval(['loaddods(''', deblank(CatalogServer(i,:)), '?', Constraint,''')'])
  if i == 2
    timetemptemp = find(time > DivideDay);
    timetemp = [timetemp; time(timetemptemp)];
  else
    timetemp = [timetemp; time];
  end
end

%eval(['loaddods(''', deblank(CatalogServer(2,:)), '?', Constraint,''')'])
%timetemp = [timetemp; time];

LastYear = floor(LastTime);
if isleap(LastYear), DaysInYear = 366; else DaysInYear = 365; end
LastYrday = (LastTime - LastYear) * DaysInYear;

More = 1;
Yeardays = 0; StartDate = [];

BeginTime = BaseTime;
while More == 1
  StartYear = floor(StartTime);
  BeginYear = floor(BeginTime);
  TmpYeardays = Yeardays;
  if isleap(BeginYear), DaysInYear = 366; else, DaysInYear = 365; end
  BeginYrday = (BeginTime - BeginYear) * DaysInYear;
  if StartYear == BeginYear, StartYrday = (StartTime - StartYear) * DaysInYear; end
  BeginDate = BeginYrday + Yeardays - BaseDay;
  StartDate = StartYrday + Yeardays - BaseDay;
  
  EndTime = BeginYear + 1;
  if EndTime >= LastTime
    More = 0;
    EndTime = LastTime; 
    EndYrday = LastYrday;
    EndDate = LastYrday + Yeardays - BaseDay;
  else
    BeginTime = EndTime;
    if (StartTime <= EndTime), StartTime = EndTime; end
    EndYrday = DaysInYear;
    EndDate = 92 + Yeardays;
    Yeardays = Yeardays + DaysInYear;
  end
  
  if ~isempty(StartDate)
    timein = find(StartDate <= timetemp & EndDate >= timetemp);
    times = [times; (timetemp(timein)-TmpYeardays+BaseDay)/DaysInYear+BeginYear];
    Pass_Times = [Pass_Times; (reshape(timein,length(timein),1))-1];
  end
end   %end of while

%Depth in sigma. Depending on bathymetry and elevation, each location has it's own depth value. 
sigma = [0 -0.01 -0.04 -0.1 -0.2 -0.3 -0.4 -0.5 -0.6 -0.7 -0.8 -0.9 -1];
BaseDepth = Depth(1);    % on the sea surface
StartDepth = BaseDepth;  
BeginDepth = max(Depth(1), ranges(3,1));
EndDepth = min(Depth(2), ranges(3,2));

depthtemp = find(-(BeginDepth) >= sigma & -(EndDepth) <= sigma);
depths = [depths; -sigma(depthtemp)];
Pass_Depths = [Pass_Depths; (reshape(depthtemp,length(depthtemp),1))-1];



  

