function [start_time,stop_time]=mcgtim(cdf)
% MCGTIM   gets the start and stop time from a cdf file that
%          has been created with BUOY2EPIC
%
%  Usage: [start_time,stop_time]=mcgtim(cdf);
%
%         where start_time and stop_time are 6 element vectors
%         containing [yr mo da hr mn sec]

% Open netCDF file
[cdfid,rcode] = mexcdf('ncopen',cdf,'NC_NOWRITE');
% supplress warnings from netCDF
[oldopts]=mexcdf('setopts',0);
% Check for time variable
[timevid,rcode] = mexcdf('ncvarid',cdfid,'time');
if (timevid==-1),
   disp(['file ' cdf 'does not variable named time'])
   return
end
%
% Check for time2 variable (for EPIC files)
[time2vid,rcode] = mexcdf('ncvarid',cdfid,'time2');
if (time2vid==-1),
   epic=0;
else
   epic=1;
end
%
% Find length of time variable  (time must be 1 dimensional)
[timevnam,type,nvdims,var_dim,natts,rcode]=mexcdf('ncvarinq',cdfid,timevid);
[timednam,n,rcode]=mexcdf('ncdiminq',cdfid,var_dim);
if (epic),
% time is julian day
  jd1=mexcdf('ncvarget1',cdfid,timevid,0);
  jd2=mexcdf('ncvarget1',cdfid,timevid,n-1);
% time2 is milliseconds from midnight
  m1=mexcdf('ncvarget1',cdfid,time2vid,0);
  m2=mexcdf('ncvarget1',cdfid,time2vid,n-1);
%
  start_time=gregorian(jd1);
  stop_time=gregorian(jd2);
  [start_time(4),start_time(5),start_time(6)]=ms2hms(m1);
  [stop_time(4),stop_time(5),stop_time(6)]=ms2hms(m2);
%
else,
  base_date=mexcdf('ncattget',cdfid,'NC_GLOBAL','base_date');
  jd0=julian(base_date(1),base_date(2),base_date(3));
% time is "units" from base_date
  t1=mexcdf('ncvarget1',cdfid,timevid,0);
  t2=mexcdf('ncvarget1',cdfid,timevid,n-1);
  [units]=mexcdf('ncattget',cdfid,timevid,'units');
  if(units(1:7)=='seconds'),
    [h1,m1,s1]=ms2hms(t1*1000);
    [h2,m2,s2]=ms2hms(t2*1000);
  elseif (units(1:12)=='milliseconds'),
    [h1,m1,s1]=ms2hms(t1);
    [h2,m2,s2]=ms2hms(t2);
  else
    disp('invalid time units')
    return
  end
% days from base_date
  days1=floor(h1/24);
  days2=floor(h2/24);
% julian start and stop day
  jd1=jd0+days1;
  jd2=jd0+days2;
  start_time=gregorian(jd1);
  stop_time=gregorian(jd2);
% hours from base_date+days
  h1=h1-days1*24;
  h2=h2-days2*24;
  start_time(4:6)=[h1 m1 s1];
  stop_time(4:6)=[h2 m2 s2];
end
if nargout==0,
  str=greg2str(start_time);
  str=['start time = ' str];
  disp(str);
  str=greg2str(stop_time);
  str=['stop time = ' str];
  disp(str);
end
