function ac = catadcp(a, c, ii)
% function ac = catadcp(a, c, ii)
% Returns concatenation of a and c.
% It would be nice to have a more automated way of doing this.
% 

% This initialization is not necessary, but it ensures that
% the order of the fields remains the same, even if they are
% changed in read_os.



if (isemptys(a) & isemptys(c))
   ac = [];
elseif (isemptys(a))
   ac = c;
elseif (isemptys(c))
   ac = a;
else


   if chk_rconfig(a.config, c.config)
      fprintf('NOTE: CONFIG CHANGE (returning without catting)\n')
      ac = a;      
   end
   
   
   ac = a;
   if nargin == 2, ii = 1:length(a.dday); end
   
   if isfield(c, 'vel'),      ac.vel =      cat(3, a.vel(:, :, ii), c.vel); end
   if isfield(c, 'enu'),      ac.enu =      cat(3, a.enu(:, :, ii), c.enu); end
   if isfield(c, 'xyze'),     ac.xyze =     cat(3, a.xyze(:, :, ii), c.xyze); end
   if isfield(c, 'amp'),      ac.amp =      cat(3, a.amp(:, :, ii), c.amp); end
   if isfield(c, 'sdb'),      ac.sdb =      cat(3, a.sdb(:, :, ii), c.sdb); end
   if isfield(c, 'pg'),       ac.pg =       cat(3, a.pg(:, :, ii), c.pg); end
   if isfield(c, 'cor'),      ac.cor =      cat(3, a.cor(:, :, ii), c.cor); end
   if isfield(c, 'sw'),       ac.sw =       cat(3, a.sw(:, :, ii), c.sw); end
   if isfield(c, 'rssi'),     ac.rssi =     cat(3, a.rssi(:, :, ii), c.rssi); end
   if isfield(c, 'status'),   ac.status =   cat(3, a.status(:, :, ii), c.status); end

   if isfield(c, 'heading'),  ac.heading =  cat(2, a.heading(ii), c.heading); end
   if isfield(c, 'pitch'),    ac.pitch =    cat(2, a.pitch(ii), c.pitch); end
   if isfield(c, 'roll'),     ac.roll =     cat(2, a.roll(ii), c.roll); end
   if isfield(c, 'heave'),     ac.heave =   cat(2, a.heave(ii), c.heave); end
   if isfield(c, 'pressure'), ac.pressure = cat(2, a.pressure(ii), c.pressure);end

   if isfield(c, 'soundspeed'),    
      ac.soundspeed = cat(2, a.soundspeed(ii), c.soundspeed); 
   end
   if isfield(c, 'temperature'),    
      ac.temperature = cat(2, a.temperature(ii), c.temperature); 
   end
   if isfield(c, 'ctemperature'),    
      ac.ctemperature = cat(2, a.temperature(ii), c.temperature); 
   end
  if isfield(c, 'voltage'),    
      ac.voltage = cat(2, a.voltage(ii), c.voltage); 
   end

   
   if isfield(c, 'noise'), ac.noise = cat(2, a.noise(:,ii), c.noise); end
   if isfield(c, 'xflux'), ac.xflux = cat(2, a.xflux(ii), c.xflux); end
   if isfield(c, 'yflux'), ac.yflux = cat(2, a.yflux(ii), c.yflux); end
   if isfield(c, 'zflux'), ac.zflux = cat(2, a.zflux(ii), c.zflux); end
   
   
   if isfield(c, 'bt'),
      ac.bt.vel = cat(2, a.bt.vel(:,ii), c.bt.vel);
      ac.bt.range = cat(2, a.bt.range(:,ii), c.bt.range);
      if (isfield(c.bt, 'status')),
         ac.bt.status = cat(2, a.bt.status(ii), c.bt.status);
      end
      if (isfield(c.bt, 'cor')),
         ac.bt.cor = cat(2, a.bt.cor(:,ii), c.bt.cor);
      end
      if (isfield(c.bt, 'amp'))
         ac.bt.amp = cat(2, a.bt.amp(:,ii), c.bt.amp);
      end
      if (isfield(c.bt, 'sdb'))
         ac.bt.sdb = cat(2, a.bt.sdb(:,ii), c.bt.sdb);
      end
      if (isfield(c.bt, 'rssi'))
         ac.bt.rssi = cat(2, a.bt.rssi(:,ii), c.bt.rssi);
      end
      if (isfield(c.bt, 'pg'))
         ac.bt.pg = cat(2, a.bt.pg(:,ii), c.bt.pg);
      end
   end

   ac.dday = cat(2, a.dday(ii), c.dday);
   if (isfield(c,'corr_dday'))
      ac.corr_dday = cat(2, a.corr_dday(ii), c.corr_dday);
   end
   
   
   if (isfield(c, 'ens_num'))
      ac.ens_num = cat(2, a.ens_num(ii), c.ens_num);
   end

   if (isfield(c, 'num_pings'))   
      ac.num_pings = cat(2, a.num_pings(ii), c.num_pings);
   end
   
   if (isfield(c, 'depth')),    ac.depth = a.depth; end

   if (isfield(c, 'ymdhms')), ac.ymdhms = cat(2, a.ymdhms(:, ii), c.ymdhms);end

   
   if (isfield(c, 'umeas'));ac.umeas = [a.umeas(:,ii), c.umeas];   end
   if (isfield(c, 'vmeas'));ac.vmeas = [a.vmeas(:,ii), c.vmeas];   end
   if (isfield(c, 'wmeas'));ac.wmeas = [a.wmeas(:,ii), c.wmeas];   end
   if (isfield(c, 'emeas'));ac.emeas = [a.emeas(:,ii), c.emeas];   end
   if (isfield(c, 'fmeas'));ac.fmeas = [a.fmeas(:,ii), c.fmeas];   end
   if (isfield(c, 'pmeas'));ac.pmeas = [a.pmeas(:,ii), c.pmeas];   end

   
   if isfield(c, 'nav'),
      ac.nav.sec_pc_minus_utc = cat(2, a.nav.sec_pc_minus_utc(ii), ...
                                    c.nav.sec_pc_minus_utc);
      ac.nav.txy1 = cat(2, a.nav.txy1(:,ii), c.nav.txy1);
      ac.nav.txy2 = cat(2, a.nav.txy2(:,ii), c.nav.txy2);
   end

   %% cat serial fields
   if isfield(c, 'aux_struct') %assumes structure from get_serfields
      caux = c.aux_struct;
      aaux = a.aux_struct;  
      
      %% this is a bit complicated
      
      %%% check structures here
      
      
      allfieldnames = fieldnames(caux);
      get_instfields;
      for fieldi = 1:length(allfieldnames)
         if ismember(allfieldnames{fieldi}, instfields)
            cat_inst = allfieldnames{fieldi};
            %adu2 = caux.adu2;            
            inststruct = getfield(caux, cat_inst);  
            %suffixes from caux.adu2.gps, caux.adu2.adu
            if isstruct(inststruct)
               suffixes = fieldnames(inststruct);  
               for suffixi = 1:length(suffixes)
                  suffix = suffixes{suffixi};
                  eval(sprintf('ca_aux.%s.%s.gdata = [aaux.%s.%s.gdata(:,ii) caux.%s.%s.gdata];',...
                               cat_inst, suffix,cat_inst, suffix , cat_inst, suffix))
                  eval(sprintf('ca_aux.%s.%s.gstruct = [aaux.%s.%s.gstruct];',...
                               cat_inst, suffix,cat_inst, suffix))
               end
            end
         end
      end
      if exist('ca_aux','var')
         ac.aux_struct = ca_aux;
      end
      
   end
   

   
   
   
end
