function scan(softclass, instclass, sourcedir, varargin)
% function scan(softclass, instclass, sourcedir, varargin)
%
% Produce a listing of any or all of the VMDAS data
% files in a given directory (sourcedir), with start/end
% times and ensemble numbers, etc.
%
% File types to be scanned are specified with the
% strings 'enr', 'ens', 'enx', 'sta', 'lta', or 'all'.
% Yearbase for decimal days can be specified with, for
% example, the argument pair 'yearbase', 2000.
% If the second argument is not one of the strings above,
% it will be interpreted as the directory for the output
% file(s).  One output file is produced per VMDAS file type.
%
% This version works only for Ocean Surveyor data, but can
% easily be extended to take an additional argument that
% will allow it to call the read_bb function instead of
% read_os, and therefore to work with BB data collected by
% VMDAS.
%
% 2000/09/25 EF
%
%

out_ext = '.scn';
out.enr = 0; out.ens = 0; out.enx = 0; out.sta = 0; out.lta = 0;
yearbase = 0;
dest = './';

get_numeric = 0;
for iarg = 1:(nargin-3)
   arg = varargin{iarg};
   if ischar(arg),
      get_numeric = 0;
      switch lower(arg)
         case 'enr', out.enr = 1;
         case 'ens', out.ens = 1;
         case 'enx', out.enx = 1;
         case 'sta', out.sta = 1;
         case 'lta', out.lta = 1;
         case 'yearbase', get_numeric = 1;
         case 'all', out.enr = 1; out.ens = 1; out.sta = 1; out.lta = 1;
         otherwise, dest = arg;
      end
   else %numeric
      if get_numeric == 1,
         yearbase = arg;
         get_numeric = 0;
      else
         error('No numeric argument was expected here')
      end
   end
end

[destpath, destname] = fileparts(dest);


icl = instclass;
sdir = sourcedir;

if out.enr,
   list(icl, sdir, 'ENR', fullfile(destpath, [destname, 'enr.scn']), yearbase);
end
if out.ens,
   list(icl, sdir, 'ENS', fullfile(destpath, [destname, 'ens.scn']), yearbase);
end
if out.enx,
   list(icl, sdir, 'ENX', fullfile(destpath, [destname, 'enx.scn']), yearbase);
end
if out.sta,
   list(icl, sdir, 'STA', fullfile(destpath, [destname, 'sta.scn']), yearbase);
end
if out.lta,
   list(icl, sdir, 'LTA', fullfile(destpath, [destname, 'lta.scn']), yearbase);
end



%-----------------------------------------------------

function list(instclass, sourcedir, ext, outfile, yearbase)
glob1 = fullfile(sourcedir, ['*.', ext])
glob2 = fullfile(sourcedir, ['*.', lower(ext)]);

filelist1 = dir(glob1);
filelist2 = dir(glob2);


filelist = [filelist1; filelist2];
if size(filelist, 1) == 0, return, end

f = [];
ii = 1;

for ifile = 1:length(filelist)
   file = fullfile(sourcedir, filelist(ifile).name);
   disp(file);
   d = read(instclass, file, 'ends', 2, 'yearbase', yearbase);
   if isempty(d.error),
      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;
      ii = ii + 1;
   end
end

fid = fopen(outfile, 'w');
if fid == -1, error(['Can''t open ', outfile]); end


for ii = 1:length(f)
   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)






