function dd=scan_rawcfg(instclass, dirpath, filelist, varargin)
% dd = scan_rawcfg(instclass, dirpath, filelist, varargin)
% filelist is structure like comes from dir, or is a cellarray
% varargin has two options (name, value pairs)
% 'add_to_ens' num      %add to start of cumulative ensemble (default = 0)
% 'logfile' filename    % print out information (default is "scanrawcfg.log")


% need to add RDIdday 

cfg.add_to_ens = 0;    %just start the count for this block at buf.ens_num(1)


cfg = fillstruct(cfg,varargin); %update with varargin

add_to_ens  = cfg.add_to_ens;

if (isfield(cfg, 'logfile'))
   fid = fopen(cfg.logfile,'w');
else
   fid = fopen('scanrawcfg.log','w');
end


if (isempty(filelist))
   error('empty file list: better check the paths and filenames')
end

if (isstruct(filelist)) %
   %assume it came from a dir('*.raw') type of listing
   for ii = 1:length(filelist)
      flist(ii) = {fullfile(dirpath,filelist(ii).name)};
   end
else
   flist = filelist;
end

flist = flist(:);

fprintf(fid, ...
'     file         ens_num(1 end),             RDIensnum(1 end)  (add_to)   \n');

for ii = 1:length(flist)
   file = flist{ii};
   fprintf('(%d of %d) file: %s\n',ii, length(flist), file)
   tic
   dd(ii) = read_rawcfg(instclass, file, add_to_ens);
   fprintf(fid,'%d %s    %7d %7d          %7d %7d       %d \n',...
           ii, flist{ii},  ...  
           dd(ii).ens_num(1),  dd(ii).ens_num(end), ...
           dd(ii).RDIens_num(1),  dd(ii).RDIens_num(end), dd(ii).add_to_ens);
   add_to_ens = dd(ii).add_to_ens;
   toc
end


for ii = 1:length(flist)
   file = flist{ii};
   dd(ii).filename = file;
end

fclose(fid);