
fullfilelist = dirs('/home/data/km0413a/raw/os38/km*.raw','fullfile',1);

filelist = fullfilelist(1:10);


fid_genlog = fopen('write_ensblk.log','a');

%set defaults for varargin structure 
cfg.redo = 1;
cfg.ldcodas_path = './';
cfg.nav_path = './';     
cfg.att_path = './';
cfg.use_for_heading = 'heading';
cfg.pingtype = 'bb';
cfg.verbose = 0;
cfg.gbindir = '/home/data/km0413a/gbin/os38k';
cfg.path_ext = {'time',    'best';   
                'time',    'tim'};
p.use_3_beam = 0;    % 3-beam not yet implemented
p.min_pings = 10;
p.h_bias = 0; %hardwire here; deal with offset in h_align

cfg.max_BLKprofiles = 77;  % max number of profiles to use per block TESTING
ldcodas_path = cfg.ldcodas_path;

instclass = os;
params.yearbase = 2004;

params.instclass = os;
params.h_align =  43.8; 
params.velscale = 1.0;
params.ilayer = [1:40];
params.ducer_depth = 7;
params.pingtype = cfg.pingtype;
params.path_ext = cfg.path_ext;
params.gbindir = cfg.gbindir;
params.editcfg =  get_pedefaults(os, 'bb');
params.interval = 300;


if cfg.redo == 1
   system('/bin/rm ens_blk* write_blk.log')
   fprintf('deleting ens_blk*.* files\n')
   paused(1)
end

%%%%%%%%%%%%%%% deal with filelist %%%%%%%%%%%%%%%%
%% make sure filelist is a cellarray; 
%% extract rawdir and collapse to rawbases
if isstruct(filelist)
   rawfilelist = cell(length(filelist),1);
   for ifile=1:length(filelist)
      rawfilelist{ifile} = filelist(ifile).name;
   end
   filelist = rawfilelist;
end
% now parse into filebase and directory
% assume everything comes from the same directory
rawdir = fileparts(filelist{1});
rawbaselist = cell(size(filelist));
for ifile = 1:length(rawbaselist)
   [junk, rawbaselist{ifile}] = fileparts(filelist{ifile});
end


%%%%%%%%%%%%%%%%  get ensemble info %%%%%%%%%%%%%
% find last blk log file (assumes ens_blk???.log)
% NOTE ensemble numbers start with 1, profile and blk begin with 0 
% indicate new database with: last_ensnum = 0
%                             blknum = profnum = -1
%                             blkbase_str  =''
last_ensinfo = get_last_ensinfo(instclass, rawdir, rawbaselist)
if last_ensinfo.profnum >= 0
   last_ensinfo.pingtypes = ...
       get_pingtypes(instclass, rawdir, last_ensinfo.rawfilebase, params);
end
%% profnum is comes from last_ensinfo at the outset but is controlled by 
%%     write_ensblk.m, not passed around in structures
profnum = last_ensinfo.profnum;
blknum  = last_ensinfo.blknum;
blkbase_str = sprintf('ens_blk%03d', blknum);           
if blknum == -1
   newdb = 1;
else
   newdb = 0;
end
last_ensinfo = rmfield(last_ensinfo, 'profnum');
last_ensinfo = rmfield(last_ensinfo, 'blknum');
last_ensinfo = rmfield(last_ensinfo, 'blkbase_str');
   

%=============================================================================

%%% this block is repeated at the end of the "while(another_ens)" block  
%% part (a) get info to read next ensemble                               
[ensinfo, last_ensinfo, newblock] = ...
    get_next_ensinfo(rawdir, rawbaselist, last_ensinfo, params, newdb);
profnum = profnum+1;

newdb = 0;                                                                         
another_ens = length(ensinfo);                                           
if ~another_ens                                                          
   fprintf('no data remaining\n')                                        
   return                                                                
end                                                                      
%increment the blockfile counter and reset profile number if necessary

if  newblock == 1  | profnum > cfg.max_BLKprofiles               
   % reset these 
   blknum = blknum + 1;                                                  
   blkbase_str = sprintf('ens_blk%03d', blknum);           
   % make last_ensinfo match
   last_ensinfo.profnum = 0;  
   profnum = 0;
end

%%% deal with output files %%
% (part b) slightly different from below; only opening files here        
logfile = fullfile(ldcodas_path,    [blkbase_str  '.log'])
cmdfile = fullfile(ldcodas_path,    [blkbase_str  '.cmd']);            
binfile = fullfile(ldcodas_path,    [blkbase_str  '.bin']);             
nav1file = fullfile(ldcodas_path,   [blkbase_str  '.gps1']);            
nav2file = fullfile(ldcodas_path,   [blkbase_str  '.gps2']);            
                                                                         
fid_log  = fopen(logfile,  'a'); %% these may already exist
fid_nav1 = fopen(nav1file, 'a'); %%    so open to append
fid_nav2 = fopen(nav2file, 'a'); 
fid_cmd  = fopen(cmdfile,  'a'); 
fid_bin  = fopen(binfile,  'a'); 

if newblock == 1
   dbinfo = setup_dbinfo(rawdir, ensinfo, params);                                  
   fprintf(fid_cmd, 'New Block \n');
   fprintf(fid_log, '%% rawfilebase    last ensemble   secs(ens)   profile_num\n');
   fprintf(fid_log, '%% -----------   -------------    -------    ------------\n');
   fprintf( '------------   New Block\n');
   newblock = 0;
   paused(1)
end

if fid_log  == -1, error(['Can''t open ', logfile]);   return, end          
if fid_nav1 == -1, error(['Can''t open ', nav1file]);  return, end        
if fid_nav2 == -1, error(['Can''t open ', nav2file]);  return, end        
if fid_cmd  == -1, error(['Can''t open ', cmdfile]);   return, end        
if fid_bin  == -1, error(['Can''t open ', binfile]);   return, end        
%------------------------------------------------------------------------

%% do it:
while (another_ens)
   
   % write to block log file
   secs_in_ens = 0;
   for segi = 1:length(ensinfo)
      secs_in_ens = secs_in_ens + ensinfo(segi).secs_read;
   end
  
   if secs_in_ens > .3*params.interval    %%% set this up top

      fprintf(fid_log, '%20s   %10d  %10d %5d\n', ...
              ensinfo(end).rawfilebase, ...
              ensinfo(end).lastens, ...
              secs_in_ens,...
              profnum);
      fprintf(fid_cmd, '  new profile: %d \n', profnum);
      
      %% get the data here; call avg; return raw and averaged data
      fprintf('---------------   writing %s entry:\n ', cmdfile)
      fprintf('---------------   %20s   %10d %10d %5d\n', ensinfo(end).rawfilebase, ...
              ensinfo(end).lastens, secs_in_ens, profnum);
      
   end
   
   %-----------------------------------------------------------
   % set the stage for the next ensemble
   
   [ensinfo, last_ensinfo, newblock] = ...
       get_next_ensinfo(rawdir, rawbaselist, last_ensinfo, params, newdb);
   
   if length(ensinfo) == 0
      fprintf(' no more data. quitting here \n')
      break
   end
      
   %% evidently we still have data
   profnum = profnum+1;
   
   
   
   if  newblock == 1  | profnum >= cfg.max_BLKprofiles               
      fprintf('----------------starting new block.  closing files for blk%03d\n', blknum)
      % reset these 
      blknum = blknum + 1;                                               
      fprintf('----------------... opening files for blk%03d\n', blknum)
      blkbase_str = sprintf('ens_blk%03d', blknum);           
      % make last_ensinfo match
      profnum = 0;  
      newblock = 0;
      
      
      %% close the old files
      if exist('fid_bin'),  if fid_bin,  fclose(fid_bin);  fid_bin  = 0; end, end 
      if exist('fid_nav1'), if fid_nav1, fclose(fid_nav1); fid_nav1 = 0; end, end 
      if exist('fid_nav2'), if fid_nav2, fclose(fid_nav2); fid_nav2 = 0; end, end 
      if exist('fid_cmd') , if fid_cmd,  fclose(fid_cmd);  fid_cmd  = 0; end, end 
      if exist('fid_log'),  if fid_log,  fclose(fid_log);  fid_log  = 0; end, end 
                                                                                  
      % open the new files                                                    
      logfile = fullfile(ldcodas_path,    [blkbase_str  '.log'])
      cmdfile = fullfile(ldcodas_path,    [blkbase_str  '.cmd']);             
      binfile = fullfile(ldcodas_path,    [blkbase_str  '.bin']);             
      nav1file = fullfile(ldcodas_path,   [blkbase_str  '.gps1']);            
      nav2file = fullfile(ldcodas_path,   [blkbase_str  '.gps2']);            
                                                                         
                                                                         
      fid_log  = fopen(logfile,  'w'); % should be OK to open 'w' since 
      fid_nav1 = fopen(nav1file, 'w'); %    we're in a loop    
      fid_nav2 = fopen(nav2file, 'w'); 
      fid_cmd  = fopen(cmdfile,  'w'); 
      fid_bin  = fopen(binfile,  'w'); 

      %% write the new block info
      dbinfo = setup_dbinfo(rawdir, ensinfo, params);                                  
      fprintf(fid_cmd, 'New Block \n');
      fprintf(fid_log, '% rawfilebase    last ensemble   secs(ens)   profile_num\n');
      fprintf(fid_log, '% -----------   -------------    -------    ------------\n');
      fprintf( '------------   New Block\n');
      paused(1)
  
                                                                                    
      if fid_log  == -1, error(['Can''t open ', logfile]); return, end             
      if fid_nav1 == -1, error(['Can''t open ', nav1file]); return, end           
      if fid_nav2 == -1, error(['Can''t open ', nav2file]); return, end           
      if fid_cmd  == -1, error(['Can''t open ', cmdfile]);   return, end           
      if fid_bin  == -1, error(['Can''t open ', binfile]);   return, end           
      
    
   elseif  newblock == -1
      fprintf('ending write_blk session\n')
      break
   end                                                                            
   
   %------------------------------------------------------------------------------
   
end % of ensembles

% close thhe files
if exist('fid_bin'),  if fid_bin,  fclose(fid_bin);  fid_bin  = 0; end, end
if exist('fid_nav1'), if fid_nav1, fclose(fid_nav1); fid_nav1 = 0; end, end
if exist('fid_nav2'), if fid_nav2, fclose(fid_nav2); fid_nav2 = 0; end, end
if exist('fid_cmd') , if fid_cmd,  fclose(fid_cmd);  fid_cmd  = 0; end, end
if exist('fid_log'),  if fid_log,  fclose(fid_log);  fid_log  = 0; end, end

if exist('fid_genlog'),  if fid_genlog,  fclose(fid_genlog);  fid_genlog  = 0; end, end
