function uhdaslog5(filelist, yearbase, zipfilename)
% uhdaslog5(filelist, yearbase, zipfilename )
% 
% filelist is as: dirs('nb150/*log', 'fullfile',1)
% yearbase is the year of the cruise
% zipfilename is the name to store the original log files (include path)
%   (if not included, no zipping takes place)
%
% replace the log files in filelist with 5-column couterparts
% specifically, read the log file and associated data file, 
%     add ensemble number and adcp_dday as colomns, write it out again.
%
% eg.  cd raw/nb150
%  uhdaslog5(dirs('*.log'), 2004)
%  uhdaslog5(dirs('*.log'), 2004, 'log3.zip')
  


if nargin <2
   help(mfilename)
   return
end

if isemptys(filelist)
   fprintf('no files to convert')
   return
end


if ~isstruct(filelist)
   help(mfilename)
   return
end

logyearbase = 2001; %old files were all logged with 2001 yearbase

dirname = fileparts(filelist(1).name);
fstr = ' ';
for ii=1:length(filelist)
  fstr  = [fstr ' ' filelist(ii).name];
end

if nargin == 3
   if ~strcmp(zipfilename(end-3:end) , '.zip')
      zipfilename = sprintf('%s.zip', zipfilename);
   end
   cmd = sprintf('zip %s %s', zipfilename, fstr);

   %%% zip the old log files
   fprintf('attempting to zip old files...\n')
   [ss,ww]=unix(cmd);
   if ss ~= 0
      fprintf('zip failed. cannot continue\n')
      ww
      return
   else
      fprintf('zip was successful.  continuing with log file regeneration.\n')
   end
else
   fprintf('not zipping\n')
end

%%% now move them out of the way and then make the new ones
for ii=1:length(filelist)
   logfilename = filelist(ii).name;
   if ~strcmp(logfilename(end-2:end) , 'log')
      fprintf('wrong filelist: start with "log" files\n')
      return
   end
   savefilename = sprintf('%s.log3', logfilename);
   [ss,ww] = unix(sprintf('mv %s %s', logfilename, savefilename));
   if ss ~= 0
      fprintf('attempt to move log file for safekeeping failed\n')
      ww
      return
   else
      tic
      fprintf('%d out of %d\n', ii, length(filelist))
      rawfilename = logfilename(1:end-4)
      data = read(nb, rawfilename, 'yearbase', logyearbase);
      loginfo = read_asc(savefilename);

      instdday =  data.dday;
      d0 = datenum(logyearbase, 1,1);
      [Y, M, D, H, Mi, S] = datevec(d0 + loginfo(:,1));
      ymdhms = [Y(:), M(:), D(:), H(:), Mi(:), S(:)];
      pcdday =   to_day(yearbase, ymdhms)';
     
      if length(data.dday) > length(loginfo(:,1))
         efid = fopen(sprintf('%s.err',logfilename),'w');
         fprintf(efid, 'Log file is smaller than data\n');
         fclose(efid);
         fprintf('This should not happen.  Log file is smaller than data\n')
         [ss,ww] = unix(sprintf('mv %s %s', savefilename, logfilename));
      else
         loginfo = loginfo(1:length(data.dday),:);
         maxi = min(length(data.dday), length(loginfo(:,1)));
         logi = 1:maxi;
         loginfo = [pcdday(logi);
                    loginfo(logi,[2 3])';
                    data.ens_num(logi);
                    instdday(logi)]';
         fid = fopen(logfilename,'w');
         if fid == -1
            fprintf('error opening %s to write\n', logfilename)
            return
         else
            fprintf(fid,'%.7f %d %d %d %.7f\n', loginfo');
            fclose(fid);
         end
         toc
      end
   end
end

