function  scan(instclass, varargin);
% function  scan(instclass, varargin);
%
% Produce a listing of the binary ADCP data files 
% Listing produces start/end times and ensemble numbers, etc.
% 
% The instrument class (nb or os, (NO QUOTES)) is required and 
% determines how the files are to be read. Only one kind of raw data
% can be specified per call to "scan".  Other options are 
% specified with keyword, value pairs.  Files to be scanned are 
% determined by the keyword 'suffix' or by a list specified with 
% 'filelist'.    One scan file is produced for all files read.
%
% keyword, value pairs are:
% 
%  ---------- required ----------
% 'yearbase', [4-digit yearbase]   %
%
% 'filelist', [one file or cellarray] % must specify at least 
% 'suffix',   [string]                %     one of these
%
% ---------- optional ----------
% 'source_dir', [path to files]    % default is here (i.e. './')
% 'dest_dir', [output directory]   % default is here (i.e. './')
% 'outfile',  [string]             % default 'rawscan.scn'
% 'verbose', [0 or 1]              % verbose read, default is 0 (not verbose)
%
% example usage (from the directory where the raw files exist)
% scan(nb, 'yearbase', 2000, 'suffix', '.raw')
%
% Note: (1) it is easiest to go to the directory where the files are 
%   and point the output elsewhere (2) nb dates are RDI dates (uncorrected)
%

% 2000/09/25 EF
% 2001/02/15 JH adding instclass method
%


% defaults: (must initialize all defaults or fillstruct will fail)
cfg.yearbase = [];
cfg.filelist = [];
cfg.suffix = [];
cfg.verbose = 0;

cfg.source_dir = './';
cfg.dest_dir = './';
cfg.outfile = 'rawscan.scn';

if (nargin <= 1)
   help(mfilename)
   error('must specify files')
end

cfg = fillstruct(cfg,varargin,0);

if (isempty(cfg.filelist) & (isempty(cfg.suffix)))
   help(mfilename)
   error('must specify suffix or file list')
end

if (isempty(cfg.yearbase) | (length(num2str(cfg.yearbase))~=4))
   help(mfilename)
   error('must specify 4-digit yearbase')
end

if (isempty(cfg.outfile))
   help(mfilename)
   error('may not specify an empty outfile name')
end

%get rid of the dot.
suffix = cfg.suffix;
suffix(strmatch('.', cfg.suffix)) = [];
   
% --- assemble the list of files  ---


filelist1 = []; filelist2 = []; filelist3 = [];
if (~isempty(suffix))
   glob1 = fullfile(cfg.source_dir, ['*.', upper(suffix)]);
   glob2 = fullfile(cfg.source_dir, ['*.', lower(suffix)]);
   filelist1 = dir(glob1);
   filelist2 = dir(glob2);
end

if (isstr(cfg.filelist))
   filelist3.name  = cfg.filelist;
else
   filelist3 = cfg.filelist;
end

filelist = [filelist1; filelist2; filelist3];
if size(filelist, 1) == 0,
   error(sprintf('could not find files in directory %s\n',cfg.source_dir))
else
   %sort it
   fnames = fieldnames(filelist);
   fcell = struct2cell(filelist);   %(4 X nfiles)
   [sorted_names, sorti] = sort(fcell(1,:));
   sorted_fcell = fcell(:,sorti);
   filelist = cell2struct(sorted_fcell', fnames(:)',2);
end

% --- read and scan the files ---

f = [];
ii = 1;

for ifile = 1:length(filelist)
   file = fullfile(cfg.source_dir, filelist(ifile).name);
   disp(file)
   d = read(instclass, file, 'ends', 2, 'yearbase', cfg.yearbase, ...
            'verbose', cfg.verbose);
   f(ii).name = filelist(ifile).name;
   f(ii).dd = d.dday([1 end]);
   f(ii).ens = d.ens_num([1 end]);
   f(ii).yearbase = d.config.yearbase;
   f(ii).error = d.error;
   ii = ii + 1;
end


fid = fopen(cfg.outfile, 'w');
if fid == -1, 
   error(['Can''t open ', cfg.outfile]); 
else
   fprintf('writing scan info to %s\n',cfg.outfile)
end


for ii = 1:length(f)
   if (diff(f(ii).ens) == 0)
      sprintf('filename %s has only one ensemble\n', f(ii).name)
   
   end   
      
   n = 1 + diff(f(ii).ens);
   dt = 86400 * diff(f(ii).dd)/(n - 1);
   DT = dt * n;
   if ii == 1,
      ens_gap = 0;
      t_gap = 0;
   else
      ens_gap = f(ii).ens(1) - f(ii-1).ens(2);
      t_gap = 86400 * (f(ii).dd(1) - f(ii-1).dd(2));
   end

   fprintf(fid, '%s  %s  %s', f(ii).name, ...
                  to_date(f(ii).yearbase, f(ii).dd(1), 'string'), ...
                  to_date(f(ii).yearbase, f(ii).dd(2), 'string'));

   fprintf(fid, '  %12.6f  %12.6f', f(ii).dd);
   fprintf(fid, '  %6d %6d', f(ii).ens);
   fprintf(fid, '  %5d %5d', n, ens_gap);
   fprintf(fid, '  %4.1f %5.0f %6.0f\n', dt, t_gap, DT);

end

fclose(fid);






