function [icyc,ncyc,jd]=mctime(cdf,start_time,stop_time)
% MCTIME   returns the first cycle and the number of cycles to use in mcvgt,
%          based on start and stop times, and the time vector in 
%          decimal Julian Days
%
%       [icyc,ncyc,jd]=mctime(cdf,start_time,stop_time)
%       where start_time and stop_time are 6 element time vectors
%       containing [y mo d h mn s]
%
%  example: 
% [icyc,ncyc,jd]=mctime(cdf,[1990 3 28 13 0 0],[1991 5 22 6 20 56])
% 
%  returns the cycle info from 3/28/1990 13:00:00 to 5/22/1991 06:20:56 GMT
%

% get base date from CDF file
%
base_date=mcagt(cdf,'NC_GLOBAL','base_date');
if isempty(base_date),
  disp(['file ' cdf 'not found'])
  return
end
base_jd=julian(base_date(1),base_date(2),base_date(3));
%
% get time variable from CDF file 
%
[foo1,foo2,foo3,timeid]=mcvinq(cdf,'time');
[name,size]=mcdinq(cdf,timeid);
time=mcvgt(cdf,'time',0,size);
% 
% get time variable (and convert to seconds if neccessary)
%
units=mcagt(cdf,'time','units');
%
if(units(1:7)=='seconds'),
  time=time;
elseif(units(1:12)=='milliseconds'),
  time=time/1000;
else
  disp('invalid time units')
  return
end
%
% desired start time
%
y1=start_time(1);
mo1=start_time(2);
d1=start_time(3);
h1=start_time(4);
mn1=start_time(5);
s1=start_time(6);
%
start_jd=julian(y1,mo1,d1);      % 
start_jd=start_jd - (5.e-9);
start_seconds=hms2h(h1,mn1,s1)*3600+(start_jd - base_jd)*24*3600;
%
% desired stop time
%
y2=stop_time(1);
mo2=stop_time(2);
d2=stop_time(3);
h2=stop_time(4);
mn2=stop_time(5);
s2=stop_time(6);

stop_jd=julian(y2,mo2,d2);
stop_jd=stop_jd+(5.e-9);
stop_seconds=hms2h(h2,mn2,s2)*3600+(stop_jd-base_jd)*24*3600;
%
%find time in interval
ind=find( start_seconds <= time & time <= stop_seconds);
jd=base_jd+time(ind)/3600/24;
icyc=min(ind)-1;
ncyc=(max(ind)-min(ind))+1;

