function gps = ndx2gps(ndx,fs,gps0)
% NDX2GPS - convert indices to gps times
%
% gps = ndx2gps(ndx,fs,gps0)
%
% ndx         index or vector of indices
% fs          sample rate
% gps0        gps time of first sample. Either a scalar or a struct
%             with elements sec, ns.
% 
% gps         struct (array) gps time relative to time of first sample
%   (:).sec   seconds
%   (:).ns    nanoseconds

% $Id: ndx2gps.m,v 1.1 2004/05/19 15:04:00 stuver Exp $

sec = (ndx-1)/fs;
if (isstruct(gps0))
  sec = sec + gps0.sec + gps0.ns/1e9;
else
  sec = sec + gps0;
end

ns  = (sec - floor(sec))*1e9;
sec = floor(sec);
ns = floor(ns);
gps = struct('sec',num2cell(sec),'ns',num2cell(ns));

return
