function data = get_rawauxinrange(filelist, ddrange, varargin)
% data =  get_rawauxinrange(filelist, ddrange, varargin)
% REQUIRED:
% filelist is as from 'dir' (or 'dirs'), path is './' unless in varargin
% ddrange is [startdd enddd] 
% adds aux info: must have already run split_aux.prl and nmea_gps.prl
% OPTIONAL:
% assume varargin is a collection of name, values pairs
% variables and values are as follows:
% 'rawdir',   [string]             % default is '.'
% 'ddrange',  [startdd enddd]      % decimal day range to look for data
% 'yearbase', [yearbase]           % default is first year in the file
%                                  % 0 (default) get BB
% 'step', [step]                   % use only every "step" index
% 
% 'vars', (os or bb): {'vel', 'cor', 'amp', 'ts', 'bt', 'pg', 'nav', 'all'}
% 'vars', (nb)      : {'vel', 'sw',  'amp', 'ts', 'bt', 'pg',        'all'}
% i.e. aquisition of single data types  in a cellarray called 'vars'
%     include any or all in the cellarray to turn them on 
% -- or -- if there is only one variable, it can be a string 
% 'vars', 'ts'
% -- or --  use the string 'all' to get all variables possible
%       (values returned will be NaN if variable is not available)
% 'vars', 'all'                    % default is 'all' possible
% 


instclass = nb;

cfg.yearbase  = 0;  %default: use first year in the file
cfg.rawdir = '';  %empty
cfg.ddrange = [];  % must specify
cfg.step        = 1;
cfg.vars =      'all';% default variables to 'all'

if (nargin < 3)
   help mfilename
   error('must specify instclassm yearbase, and ddrange')
end


if (nargin > 3)%   we'll assume the rest is a set of name,value pairs
               %override input structure with name,value pairs
   cfg = fillstruct(cfg, varargin, 0 );
end


startdd = ddrange(1);
enddd = ddrange(2);


data = [];
reading = 1;

%                        s............e
% 1s--------1e,2s------------2e,3s---------3e,4s-------4e


if (isempty(cfg.rawdir))  %it's either local or [path] is part of filename
   [ppath, nname, eext] = fileparts(filelist(1).name);
   if (isempty(ppath))     %it's local
      cfg.rawdir = '.';
   else                   %it's in the filename
      cfg.rawdir = ppath;
   end
end

for filei = 1:length(filelist)

   % override cfg.rawdir if not already set

   [ppath, nname, eext] = fileparts(filelist(filei).name);   
   fullfilename  = fullfile(cfg.rawdir, [nname, eext]);
   file  = [nname, eext];
   

   dd = read(instclass, fullfilename, 'yearbase', cfg.yearbase, 'ends',2);

   if (startdd >= dd.dday(end))
      fprintf('... skipping %s  [%f-%f]\n', ...
              filelist(filei).name, dd.dday(1), dd.dday(end))
   else %read this one
      fprintf('reading %s : [%f-%f]\n', ...
              filelist(filei).name, dd.dday(1), dd.dday(end))
      %read all the times
      tempstruct = read(instclass ,fullfilename , 'yearbase', cfg.yearbase);
      irange = [min(find((tempstruct.dday >= startdd))) ...
                max(find((tempstruct.dday <= enddd)))];

      if (length(irange) > 1)
         newdata = read(instclass ,fullfilename , ...
                        'yearbase', cfg.yearbase, ...
                        'step', cfg.step,   ...
                        'irange', irange,...
                        'step', cfg.step, ...
                        'vars', cfg.vars);
         
         
         nb2gga_coef = get_nbddaycoef(nb, cfg.rawdir, file(1:end-4), cfg.yearbase);
         newdata = fixtime(nb, newdata, nb2gga_coef);
         newdata = fillnav(nb, fullfile(cfg.rawdir, file(1:end-4)) ,newdata);
         
         data = catadcp(data, newdata);
      end
      
      if (enddd <= dd.dday(end)) %stop reading after this one
         reading = 0;
      end
            %shouldn't need the cutadcp here
   end

   if (reading == 0)
      break;
   end
end      
      
   