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 (isempty(a) & isempty(c))
   ac = [];
elseif (isempty(a))
   ac = c;
elseif (isempty(c))
   ac = a;
else
   

   ac = a;
   if nargin == 2, ii = 1:length(a.dday); end
   
   if isfield(a, 'vel'), ac.vel = cat(3, a.vel(:, :, ii), c.vel); end
   if isfield(a, 'enu'), ac.enu = cat(3, a.enu(:, :, ii), c.enu); end
   if isfield(a, 'xyze'), ac.xyze = cat(3, a.xyze(:, :, ii), c.xyze); end
   if isfield(a, 'amp'), ac.amp = cat(3, a.amp(:, :, ii), c.amp); end
   if isfield(a, 'pg'), ac.pg = cat(3, a.pg(:, :, ii), c.pg); end
   if isfield(a, 'cor'), ac.cor = cat(3, a.cor(:, :, ii), c.cor); end
   if isfield(a, 'sw'), ac.sw = cat(3, a.sw(:, :, ii), c.sw); end
   if isfield(a, 'rssi'), ac.rssi = cat(3, a.rssi(:, :, ii), c.rssi); end
   if isfield(a, 'heading'), ac.heading = cat(1, a.heading(ii), c.heading); end
   if isfield(a, 'pitch'),    ac.pitch = cat(1, a.pitch(ii), c.pitch); end
   if isfield(a, 'roll'),    ac.roll = cat(1, a.roll(ii), c.roll); end
   if isfield(a, 'soundspeed'),    
      ac.soundspeed = cat(1, a.soundspeed(ii), c.soundspeed); 
   end
   if isfield(a, 'temperature'),    
      ac.temperature = cat(1, a.temperature(ii), c.temperature); 
   end
   
   %nb stuff
   if isfield(a, 'meas_soundspeed')
      ac.meas_soundspeed = cat(1, a.meas_soundspeed(ii), c.meas_soundspeed);
   end
   if isfield(a, 'aduheading')
      ac.aduheading = cat(1, a.aduheading(ii), c.aduheading);
   end
   if isfield(a, 'attdday')
      ac.attdday = cat(1, a.attdday(ii), c.attdday);
   end
   if isfield(a, 'mrms')
      ac.mrms = cat(1, a.mrms(ii), c.mrms);
   end
   if isfield(a, 'brms')
      ac.brms = cat(1, a.brms(ii), c.brms);
   end
   

   if isfield(a, '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(ac.bt, 'cor')),
         ac.bt.cor = cat(2, a.bt.cor(:,ii), c.bt.cor);
      end
      if (isfield(ac.bt, 'amp'))
         ac.bt.amp = cat(2, a.bt.amp(:,ii), c.bt.amp);
      end
      if (isfield(ac.bt, 'rssi'))
         ac.bt.rssi = cat(2, a.bt.rssi(:,ii), c.bt.rssi);
      end
      if (isfield(ac.bt, 'pg'))
         ac.bt.pg = cat(2, a.bt.pg(:,ii), c.bt.pg);
      end
   end

   if isfield(a, 'nav'),
      ac.nav.sec_pc_minus_utc = cat(1, 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

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

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

   if (isfield(a, 'config'))
      ac.config = a.config;
   end
   
   if (isfield(ac, 'depth')),    ac.depth = a.depth; end

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

end
