function [dectimes, urlinfolist, urlinfotext, url, n, pcheckbool] =  ...
       usgstsc(CatalogServer, timerange, getarchive, getranges, getvars)

%
% This function passes lat/lon/depth/time information back to getfunction
%

% The preceding empty line is important.
% $Log$

% $Id$
% klee   08/12/99

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

%Pass_Times = [];  times = [];  timetemp = [];
%Pass_Depths = []; depths = []; depthtemp = [];
%time = []; timein = []; 
url = []; Constraint = []; urlinfolist = []; dectimes = []; 
n = 1; pcheckbool = 0;
stime = []; etime = []; varbool = []; fname = [];
lon = []; lat = []; wdepth = []; instdepth = [];

% We issue loaddods call based on lat/lon/depth/time ranges
StartTime = getranges(4,1);
EndTime = getranges(4,2);
URLlat = sprintf('%s','&lat>=',num2str(getranges(2,1),5),'&lat<=',num2str(getranges(2,2),5));
URLlon = sprintf('%s','&lon>=',num2str(getranges(1,1),5),'&lon<=',num2str(getranges(1,2),5));
URLdate = sprintf('%s','&stime<=',num2str(EndTime,9),'&etime>=',num2str(StartTime,9));
URLdepth = sprintf('%s','&instdepth>=',num2str(getranges(3,1),7),'&instdepth<=',num2str(getranges(3,2),7));
%Set constraint based on date and lat and lon, depth is the water depth, not the real inst depth.
%We could with some extra work
Constraint = ['?stime,etime,syear,smon,sday,shr,smin,ssec,eyear,emon,eday,ehr,emin,esec,cycle,varbool,fname,wdepth,instdepth,lat,lon', URLlat, URLlon, URLdate, URLdepth];  
URL = [CatalogServer,Constraint];
url = URL;

loaddods(URL);

if stime(1) > 0 & stime(1) > 1e-10
% Find out the index number of selected var(s), and put them into getnum.
%varm = ['u_1205','v_1204','w_1204','CD_310','CS_300','T_20','T_25','S_40','C_50','O_60','OST_62','AT_21','ST_70','STH_71','ATTN_55','NEP_56','SV_80','FAC_81','SL_7','TRN_107','WS_400','WD_410','WU_422','WV_423','Tx_446','Ty_447','SDP_850','BP_915','van_1403','comp_1404','bear_1411','upr_4001','lowr_4002','lowr_4003','rdif_4004','rot_4005','rspd_4006','stead_4009','ptrn_4011','rtrn_4012','tiltx_4017','tilty_4018','P_4020','P_4022','P_4023','wp_4060','wh_4061','wd_4062'];
varm1 = str2mat(str2mat('U_Current','V_Current','W_Current','Current_Dir','Current_Speed','Sea_Temp','SST','Salinity','Conductivity'),str2mat('Dissolved_Oxygen','Oxygen_Saturation','Air_Temp','Sigma_T','Sigma_Theta','Beam_Attenuation','Backscatter_Intensity','Sound_Velocity','Sound_Speed_Corr_Factor'),str2mat('Sea_Level_Anomaly','Transmissometer','Wind_Speed','Wind_Dir','U_Wind','V_Wind','Zonal_Wind_Stress','Merid_Wind_Stress','STD_Burst_Pressure'),str2mat('Barometric_Press','Van_Angle','Compass','Bearing','Upper_Rotor_Spd','Lower_Roter_Spd(cm/s)','Lower_Rotor_Spd(counts)','Rotor_Spd_Diff','Rotor_Counts'),str2mat('Rotor_Speed','Steadiness','Percent_Transmission','Raw_Transmission','Tiltx','Tilty','Avg_Relative_Press','Interval_Pressure','Burst_Pressure'),str2mat('Avg_Wave_Period','Significant_Wave_Height',''));
varm = varm1;

getnum = [];
for i = 1:size(getvars,1)
  for j = 1:size(varm,1)
    if strcmp(deblank(varm(j,:)),deblank(getvars(i,:)))
      getnum = [getnum, j];
    end
  end
end

% Now we get the filename based on the selected var(s).
fn = derefurl(fname);                 %filenames with the right lat/lon/time/depth ranges
vb = deref2(varbool,setstr(10));      %the varbool vector
tmp = []; tmpvb = []; numvb = []; tmpfn = []; tmpindex = []; varindex = []; tmpvarindex = [];
varsize = 0;
for i = 1:size(vb,1)
  tmp = find(vb(i,:) ~= setstr(32));  %get rid of the spaces
  tmpvb = vb(i,tmp);                  %vb w/o spaces
  numvb = find(tmpvb == setstr(49));  %find which var(s) is existed in the file
  [bool, vindex] = acompar(numvb,getnum,1);     %compare the file var(s) with the selected var(s)
  if bool == 1
    % if true, pass the file name on
    % (Note, even if only one var in numvb and it is existed in getnum also, it's true.)
    % (Only that none of the var in numvb is existed in getnum would return false.)
    %tmpfn = str2mat(tmpfn, fn(i,:));  
    tmpfn = [tmpfn; fn(i,:)];         %it's the final filename list, w/ the right vars and ranges
    tmpindex = [tmpindex; i];         %and record the index number for later on
    tmpvarindex = nan*ones(1,26);     %and record which vars are selected and belong to the filename
    tmpvarindex(1,1:length(vindex)) = rot90(vindex);
    %for j = 1:size(vindex,1)
    %  tmpvarindex(i,j) = vindex(j);
    %end
    varindex = [varindex; tmpvarindex];
    varsize = varsize + cycle(i)*(size(vindex,2)+6)*8/1e6;
  end
end 
varsize = floor(varsize);
if varsize < 3 & size(tmpfn,1) <= 50
  % do nothing
elseif varsize < 9 & size(tmpfn,1) <= 50
  %if size(tmpfn,1) <= 50 
    infotext = sprintf('%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n', '   ',...
                 ['You are requesting over ',num2str(varsize),' MB of data, the browse will issue ',num2str(size(tmpfn,1))], ...
                 ['loaddods client calls. Please be aware of the amonut of transfer'], ...
                 ['through network. You will be able to monitor the progress of calls'], ...
                 ['issued on MATLAB command line window. However, if you encounter'], ...
                 ['any error during the transfer, you WILL NOT be able to save any'], ...
                 ['data for the current request, you will have to RE-request it on'], ...
                 ['the browser. The optimal request is about 2 to 6 month period at'], ...
                 ['one time, though the pratical case depends also on the network'], ...
                 ['condition and your machine setup.']);
    dodsmsg(infotext);     
  %else
  %  infotext = sprintf('%s\n%s\n%s\n%s\n%s\n%s\n%s\n', '   ',...
  %               ['You are requesting over ',num2str(varsize),' MB of data, the browse will issue ',num2str(size(tmpfn,1))], ...
  %               ['loaddods client calls. THIS REQUEST CANNOT BE PROCESSED because'], ...
  %               ['of the amount of memory used would most likely kill matlab, or'], ...
  %               ['you would experience a long delay when the data are transferred'], ...
  %               ['from the network. We suggest a shorter time period or reduce the'], ...
  %               ['number of variables of your request. Afterall, all the data of'], ...
  %               ['every successful request will be stored in matlab as Rxx_.']); 
  %  dodsmsg(infotext); 
  %end
elseif varsize >= 9 | size(tmpfn,1) > 50
  infotext = sprintf('%s\n%s\n%s\n%s\n%s\n%s\n%s\n', '   ',...
                 ['You are requesting over ',num2str(varsize),' MB of data, the browse will issue ',num2str(size(tmpfn,1))], ...
                 ['loaddods client calls. THIS REQUEST CANNOT BE PROCESSED because'], ...
                 ['of the amount of memory used would most likely kill matlab, or'], ...
                 ['you would experience a long delay when the data are transferred'], ...
                 ['from the network. We suggest a shorter time period or reduce the'], ...
                 ['number of variables of your request. Afterall, all the data of'], ...
                 ['every successful request will be stored in matlab as Rxx_.']);
  dodsmsg(infotext);
  urlinfolist = [];
  dectimes = [];
  url = [];
  n = 0;
  return;
end 

% Then we set the time index for the files (It is clumsy!)
% first pick up the time with the right vars in the right files
tmpstime = stime(tmpindex);          
tmpetime = etime(tmpindex);          
% second, convert start/end ranges into julian day (time) and seconds (time2)
Basesyear = floor(StartTime);
if StartTime < min(tmpstime)
  Basesyear = floor(min(tmpstime));
  StartTime = min(tmpstime);
end
Baseeyear = floor(EndTime);
if EndTime > max(tmpetime)
  Baseeyear = floor(max(tmpetime));
  EndTime = max(tmpetime);
end
if Baseeyear > Basesyear
  Baseyeararray = [Basesyear:Baseeyear];
else
  Baseyeararray = [Basesyear];
end
if isleap (Basesyear)
  syeardays = 366;
else
  syeardays = 365;
end
if isleap(Baseeyear)
  eyeardays = 366;
else
  eyeardays = 365;
end
Basesdate = (StartTime - Basesyear) * syeardays;
Baseedate = (EndTime - Baseeyear) * eyeardays;
Basesjulday = jul(Basesyear, floor(Basesdate)+1) + 1 + 2440000;
Baseejulday = jul(Baseeyear, floor(Baseedate)+1) + 1 + 2440000;
Basessec = floor((Basesdate - floor(Basesdate)) * 24 * 3600) * 1000;
Baseesec = floor((Baseedate - floor(Baseedate)) * 24 * 3600) * 1000;
% third,  for year/month/day/hr/min/sec
tmpsyear = syear(tmpindex); tmpsmon = smon(tmpindex); tmpsday = sday(tmpindex);
tmpshr = shr(tmpindex); tmpsmin = smin(tmpindex); tmpssec = ssec(tmpindex);
tmpeyear = eyear(tmpindex); tmpemon = emon(tmpindex); tmpeday = eday(tmpindex);
tmpehr = ehr(tmpindex); tmpemin = emin(tmpindex); tmpesec = esec(tmpindex);
sindex = []; eindex = [];
% last, compare start/end ranges to the base start/end time and calculate the index 
% for constraint expression
for i = 1:size(tmpstime,1)
  % compute days in year and apply jul.m to get time, compute sec * 1000 to get time2
  juliansday = jul(tmpsyear(i),cyday(tmpsyear(i),tmpsmon(i),tmpsday(i))+1) + 1 + 2440000;
  sseconds = (tmpshr(i)*3600 + tmpsmin(i)*60 + tmpssec(i)) * 1000;
  julianeday = jul(tmpeyear(i),cyday(tmpeyear(i),tmpemon(i),tmpeday(i))+1) + 1 + 2440000;
  %eseconds = (tmpehr(i)*3600 + tmpemin(i)*60 + tmpesec(i)) * 1000;
  %if strcmp(deblank(tmpfn(i,:)), 'h4601101.cdf') keyboard; end
  if Basesjulday > juliansday
    % Where the Basesjulday starts in this file
    % index is the index number of the last time of the (juliansday-1) day
    index = ceil((86400000-sseconds)/3600000) + 24*(Basesjulday-juliansday-1);
    if floor((86400000-sseconds)/3600000) == ceil((86400000-sseconds)/3600000)
      index = index + 1;
    end
    % plus the seconds pass this (juliansday) day,
    index = index + ceil(Basessec/3600000);
    % since dods-cdf server index starts from 0
    if index > cycle(tmpindex(i))   
      % Ugly work around for some files contain both every hour and every couple of hours measurements.
      % It's hard to track down for all the files. In the case, let it starts from 0 for the moment.
      sindex = [sindex; 0, tmpstime(i)];
    else
      Basetime = Basesdate/syeardays + Basesyear;
      sindex = [sindex; index - 1, Basetime];
    end
  else
    sindex = [sindex; 0, tmpstime(i)]; 
  end
  if Baseejulday < julianeday
    % Where the Baseejulday ends in this file
    index = ceil((86400000-sseconds)/3600000) + 24*(Baseejulday-juliansday-1);
    if floor((86400000-sseconds)/3600000) == ceil((86400000-sseconds)/3600000)
      index = index + 1;
    end
    index = index + floor(Baseesec/3600000);
    if index > cycle(tmpindex(i))
      eindex = [eindex; cycle(tmpindex(i))-1, tmpetime(i)];
    else
      Basetime = Baseedate/eyeardays + Baseeyear;
      eindex = [eindex; index - 1, Basetime];
    end
  else
    eindex = [eindex; cycle(tmpindex(i))-1, tmpetime(i)];
  end
end

% Now we take care the returned lat/lon/depth and filename and start/end index 
rlon = lon(tmpindex);
rlat = lat(tmpindex);
rdepth = instdepth(tmpindex);
rdepth2 = wdepth(tmpindex);
depth_index = nan*ones(size(rdepth));
% Put numbers to urlinfolist, that could be passed to the get mode.
urlinfolist = [rlon, rlat, depth_index, rdepth, rdepth2, sindex(:,1), eindex(:,1), varindex];
urlinfotext = [tmpfn];
% and the returned time

st = sindex(:,2); et = eindex(:,2); rtime = [];
for i = 1:size(st,1)
  seyears = [floor(sindex(i,2)); floor(eindex(i,2))];
  leaps = isleapm(seyears);
  %if size(st,1) <= 40   %if requests about a month worth of data, displays hourly
    if seyears(1) == seyears(2)
      rtime = [rtime, sindex(i,2):1/24/(365+leaps(1)):eindex(i,2)];
    else
      rtime = [rtime, sindex(i,2):1/24/(365+leaps(1)):seyears(2), seyears(2):1/24/(365+leaps(2)):eindex(i,2)];
    end
  %else                 %else, display daily, because it's too slow for hourly display
  %  if seyears(1) == seyears(2)
  %    rtime = [rtime, sindex(i,2):1/(365+leaps(1)):eindex(i,2)];
  %  else
  %    rtime = [rtime, sindex(i,2):1/(365+leaps(1)):seyears(2), seyears(2):1/(365+leaps(2)):eindex(i,2)];
  %  end
  %end
end

%leaps = isleapm(Baseyeararray);
%leaplen = length(leaps);
%tmpBasesyear = Basesyear;  tmpBasesdate = Basesdate;
%if leaplen == 1
%  daylaps = [(Basesdate/(365+leaps)+Basesyear):1/24/(365+leaps):(Baseedate/(365+leaps)+Baseeyear)];
%  rtime = [rtime; daylaps.'];
%else
%  for i = 1:leaplen
%    %daylaps = [(Basesdate+Basesyear):1/24/(365+leaps(i)):(Basesyear+1-1/24/(365+leaps(i)))];
%    if i < leaplen
%      daylaps = [(tmpBasesdate/(365+leaps(i))+tmpBasesyear):1/24/(365+leaps(i)):(tmpBasesyear+1-1/24/(365+leaps(i)))];
%    else
%      daylaps = [(tmpBasesdate/(365+leaps(i))+tmpBasesyear):1/24/(365+leaps(i)):(Baseeyear+Baseedate/(365+leaps(i)))];
%    end
%    if (i + 1) < (leaplen - 1)
%      tmpBasesdate = 0;
%      tmpBasesyear = Basesyear + 1;
%    end
%    rtime = [rtime; daylaps.'];
%  end
%end
%%times = rtime;
%%Pass_Times = rtime;

%dectimes = rot90(rtime);
dectimes = rtime;

else
  urlinfolist = [];
  dectimes = [];
  url = [];
  n = 0;
end

return;
