function finfo = tr2ilist(time_range, varargin)
% finfo = tr2ilist(time_range, varargin)
%
% get an Nx2 cellarray of rawfilebase and indices from binfpath/*gps.gbin
% appropriate to using with read(instclass,...) to get data from those
% time ranges without reading the whole raw file
%
% if called with empty time range or no argumetns, return an Nx4 cellarray
%     with rawfilebase, start_dday, end_dday, number_of_indices
% 
% varargin can override paths.  The default would be to run from the 'load'
% subdirectory, for example
%
%    [name]                                                  [default]
%   gbinpath   =  [location of .gps.gbin]              '../raw/gbinfiles';  
%   file_exten  = [for time file: "*.tim.gbin"]          'tim'
%   ascii_file =  [ascii printout of all files and time ranges [off]
%
% example:
%  tr2ilist([], 'ascii_file', 'alltimes.asc') %writes all files and times
%  finfo = tr2ilist([94 94.002]);
%  rawfilenames = finfo(:,1)  %cellarray of filenames
%  ilists       = finfo(:,2)  %cellarray of ilists


cfg.gbinpath  = '../raw/gbinfiles';  
cfg.ascii_file = '';
cfg.file_exten = 'tim';

cfg = fillstruct(cfg, varargin, 0);

filelist = dirs(fullfile(cfg.gbinpath, ['*.', cfg.file_exten, '.gbin']));

if (~exist(cfg.gbinpath, 'dir'))
   error(sprintf('directory %s does not exist\n', cfg.gbinpath))
end


if (isempty(filelist))
   error(sprintf('no gbin files exist in directory %s\n', cfg.gbinpath))
end


%there should be at least one
count = 0;


if (~isempty(cfg.ascii_file))
   fid = fopen(cfg.ascii_file, 'w');
   if (fid ~= -1)
      fprintf('writing ascii output to %s\n',cfg.ascii_file)
   else
      fprintf('cannot open %s to write ascii file\n', cfg.ascii_file)
      cfg.ascii_file = '';
   end
end

if (nargin == 0) | (isempty(time_range))
   finfo = cell(1,4);
   for filei = 1:length(filelist)
      fname = filelist(filei).name;
      rawfname = [fname(1:end-9), '.raw'];
      gpsdata = read_bin(fullfile(cfg.gbinpath, fname));
      goodi = find(~isnan(gpsdata(1,:)));
      count = count + 1;
      finfo{count,1} =  rawfname;
      finfo{count,2} = gpsdata(1,goodi(1));  
      finfo{count,3} = gpsdata(1,goodi(end));
      finfo{count,4} = length(gpsdata(1,:));
      if (~isempty(cfg.ascii_file))
         fprintf(fid, '%s  [%10.7f to %10.7f], ilist = %d:%d\n',...
                 rawfname, gpsdata(1,goodi(1)), gpsdata(1,goodi(end)), ...
                 1, length(gpsdata(1,:)));
      end
   end
else
   finfo = cell(1,2);
   reading = 0;
   for filei = 1:length(filelist)
      fname = filelist(filei).name;
      rawfname = [fname(1:end-9), '.raw'];
      gpsdata = read_bin(fullfile(cfg.gbinpath, fname));
      goodi = find(  (gpsdata(1,:) >= time_range(1)) & ...
                     (gpsdata(1,:) <= time_range(2)));
      if (~isempty(goodi))
         reading = 1;
         count = count + 1;
         finfo{count,1} = rawfname;
         finfo{count,2} = goodi;
         if (~isempty(cfg.ascii_file))
            fprintf(fid, '%s  [%10.7f to %10.7f], ilist = %d:%d\n',...
                    rawfname, gpsdata(1,goodi(1)), ...
                    gpsdata(1,goodi(end)), ...
                    goodi(1), goodi(end));
         end
      
      end

      %can we get out early?
      tt = gpsdata(1,find(~isnan(gpsdata(1,:))));
      if (max(tt) >= time_range(2))
         break;
      end
   end
end

if (~isempty(cfg.ascii_file))
   fclose(fid);
end
