function alistit(flagfiles, suffix)
% listflags(flagfiles, suffix)
%flagfiles is a cellarray of files with flags to list
%
%comes from list.m in adcpvm editing, (original comment:)
% list is used to save all the editing to text files bottom.asc,
% badprf.asc, and badbin.asc, which are then applied to the database
% by running the dbupdate and badbin programs;
% anything that is currently marked as bottom (*), glitchy (o),
% or having a high error velocity (o) on the plots (except for 'x',
% which means it's already been flagged in the database) will be
% recorded in those files.
%

% The fprintf statements could be vectorized.  The array
% form is probably a holdover from Matlab 4.


% Jules 2000/11/27

if (nargin == 1)
   suffix = '';
end


bottom_file = sprintf('abottom%s.asc', suffix);
badprf_file = sprintf('abadprf%s.asc', suffix);
badbin_file = sprintf('abadbin%s.asc', suffix);

%%nomenclature: variable name ending with "M" is a matrix

for filei = 1:length(flagfiles)
   % read the file, set up some useful matrices --------------------
   fprintf('loading %s\n', flagfiles(filei).name);
   load(flagfiles(filei).name);
   nbins =  length(pflags.flagged(:,1));
   bins = [1:nbins]';                       % Nx1 vector with bin number
   binsM = bins * ones(size(pflags.dday));  % NxM matrix with bin number

   time = to_date(pflags.yearbase, pflags.dday(:),'n');          % was TIME
   iblkprf = 0*time(:,[1 2]);                   %should be irrelevant

   %we don't want to flag bad pg: use setflags for that
   %take this out so it doesn't get seen as an error
   pflags.flag = rmfield(pflags.flag, 'pgcutoff'); 
   tempflags = pflags.flag;  %use this to store all flags, 
                             % Eliminate them as they are used: 
                             % error if any are left (that means alistit 
                             % needs to be checked)
   %set up cellarrays to loop through 
   bottom_names = {'maxampbin'};
   badprf_names = {'badpgrefl',...
                   'jitter',...
                   'underway',...
                   'orphan'};
   badbin_names = {'ecutoff',...
                  'trimtopbins',...
                  'wire'};
   %initialize flags
   bottom_flags = [];
   badprf_flags = [];
   badbin_flags = [];

   %now fill flags and do error checking
   tempflags = rmfield(tempflags, 'bottom');  %this was only used for 
   for flagi = 1:length(bottom_names)
         bottom_flags = ...
             [bottom_flags; getfield(pflags.flag, bottom_names{flagi})];
         tempflags = rmfield(tempflags, bottom_names{flagi});
   end
   for flagi = 1:length(badprf_names)
         badprf_flags = ...
             [badprf_flags; getfield(pflags.flag, badprf_names{flagi})];
         tempflags = rmfield(tempflags, badprf_names{flagi});
   end
   for flagi = 1:length(badbin_names)
         badbin_flags = ...
             [badbin_flags; getfield(pflags.flag, badbin_names{flagi})];
         tempflags = rmfield(tempflags, badbin_names{flagi});
   end

   if ~isempty(tempflags)
      tempnames = fieldnames(tempflags);
      for tempnamei = 1:length(tempnames)
         sprintf('WARNING: new field "%s" is not saved in alistit\n', ...
                 tempnames{tempnamei})
      end
      error('must fix aflagit to match aflagit_setup.m before continuing')
   end
   
   % get bottom from pflags.flagged; --------------------
   % apply  with "dbupdate $dbname bottom_file"
   % sets max_amp_bin in ANCILLARY_2
   maxampM = zeros(size(pflags.flagged));
   for ii=1:length(bottom_flags)
      maxampM = maxampM | ...
        (bitand( pflags.flagged, bottom_flags(ii)) == bottom_flags(ii));
   end
   
   [maxamp, maxampbin] = max(maxampM);  %bin of max amplitude location
   ibot = find(maxamp ~= 0);
   if ~isempty(ibot)
     fid = fopen(bottom_file, 'a');
     for ii = ibot
       fprintf(fid, ...
       '  0  %3.0f %3.0f %2.0f/%02.0f/%02.0f  %02.0f:%02.0f:%02.0f',  ...
	       [0 0], time(ii,1:6));
       fprintf(fid, ' A    32767 %5.0f %3.0f\n', ...
               maxampbin(ii), maxampbin(ii));
     end
     fclose(fid);
   end

   
   % get bad profiles from pflags.flagged --------------------
   % apply  with "dbupdate $dbname badprf_file"
   % sets max_amp_bin to (-1) in ANCILLARY_2
   badprfM = zeros(size(pflags.flagged));
   for ii=1:length(badprf_flags)
      badprfM = badprfM | ...
        (bitand( pflags.flagged, badprf_flags(ii)) == badprf_flags(ii));
   end
   if (~isempty(find(diff(badprfM) ~= 0)))
      error('bad profile flag not applied to all bins: check flagit.m')
   end

   ibad = find(badprfM(1,:) == 1);
   if ~isempty(ibad)
     fid = fopen(badprf_file, 'a');
     for ii = ibad
       fprintf(fid, ...
      ' -1 %3.0f %3.0f %4.0f/%02.0f/%02.0f  %02.0f:%02.0f:%02.0f\n', ...
	       iblkprf(ii,:), time(ii,1:6));
     end
     fclose(fid);
   end
   
   
   %get bad bins from pflags   
   % apply  with "dbupdate $dbname badprf_file"
   % sets max_amp_bin to (-1) in ANCILLARY_2
   badbinM = zeros(size(pflags.flagged));
   for ii=1:length(badbin_flags)
      badbinM = badbinM | ...
        (bitand( pflags.flagged, badbin_flags(ii)) == badbin_flags(ii));
   end

   
   ibad = find(badbinM == 1);
   if ~isempty(ibad)
     fid = fopen(badbin_file, 'a');
     ibinprf = ndx2subs(ibad, length(pflags.flagged(:,1)));
     iprf   = [0 find(diff([ibinprf(:,2)' -99]) ~= 0)];  % identifying unique 
     for ii = 2:length(iprf)                              %profile entries
       fprintf(fid, ...
	       '\n%4.0f/%02.0f/%02.0f  %02.0f:%02.0f:%02.0f %3.0f %3.0f', ...
	       time(ibinprf(iprf(ii),2),1:6), iblkprf(ibinprf(iprf(ii),2),:));
       fprintf(fid, ...
       ' %3.0f %3.0f', iprf(ii)-iprf(ii-1), ibinprf(1+iprf(ii-1):iprf(ii),1));
     end
     fclose(fid);
   end
end