function data = get_rawinrange(instclass, filelist, ddrange, varargin)
% data =  get_rawinrange(instclass, filelist, ddrange, varargin)
% REQUIRED:
% instclass is nb, bb, or os   
% filelist is as from 'dir' (or 'dirs'), path is './' unless in varargin
% ddrange is [startdd enddd] 
% OPTIONAL:
% assume varargin is a collection of name, values pairs
% variables and values are as follows:
% 'rawdir',   [string]             % default is '.'
% 'yearbase', [yearbase]           % default is first year in the file
% 'second_set', [0 or 1]           % if interleaved BB/NB which set?
%                                  % 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
% 


cfg.yearbase  = 0;  %default: use first year in the file
cfg.rawdir = '.';  %here
cfg.second_set  = 0; %instclass == os ONLY: (NB and BB collected: get BB)
cfg.step        = 1;
cfg.vars =      'all';% default variables to 'all'

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

% update cfg.
if isa(instclass, 'nb')
   if (isfield(varargin, 'second_set'))
      varargin  = rmfield(varargin, 'second_set');
   end
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

for filei = 1:length(filelist)
   file = fullfile(cfg.rawdir, filelist(filei).name);
   dd = read(instclass, file, '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 ,file , 'yearbase', cfg.yearbase);
      irange = [min(find((tempstruct.dday >= startdd))) ...
                max(find((tempstruct.dday <= enddd)))];

      
      if isa(instclass, 'nb')
         newdata = read(instclass ,file , ...
                        'yearbase', cfg.yearbase, ...
                        'step', cfg.step,   ...
                        'irange', irange,...
                        'step', cfg.step, ...
                        'vars', cfg.vars);
         else
         newdata = read(instclass ,file , ...
                        'yearbase', cfg.yearbase, ...
                        'second_set', cfg.second_set, ...
                        'step', cfg.step,   ...
                        'irange', irange,...
                        'step', cfg.step, ...
                        'vars', cfg.vars);
         end
      
      if (enddd <= dd.dday(end)) %stop reading after this one
         reading = 0;
      end
      data = catadcp(data, newdata);
      %shouldn't need the cutadcp here
   end

   if (reading == 0)
      break;
   end
end      
      
   