function ac = cutadcp(a, ii)
% function ac = cutadcp(a, ii)
% Returns subset of "a" with time indices ii.
% 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.


ac = a;

if isfield(a, 'vel'), 
   if (length(size(ac.vel)) == 3)
      ac.vel = a.vel(:, :, ii); 
   else
      ac.vel = a.vel(:, ii);       
   end
end
if isfield(a, 'enu'), 
   if (length(size(ac.enu)) == 3)
      ac.enu = a.enu(:, :, ii); 
   else
      ac.enu = a.enu(:, ii);       
   end
end
if isfield(a, 'xyze'), 
   if (length(size(ac.xyze)) == 3)
      ac.xyze = a.xyze(:, :, ii); 
   else
      ac.xyze = a.xyze(:, ii);       
   end
end
if isfield(a, 'amp'), 
   if (length(size(ac.amp)) == 3)
      ac.amp = a.amp(:, :, ii); 
   else
      ac.amp = a.amp(:, ii);       
   end
end
if isfield(a, 'sdb'), 
   if (length(size(ac.sdb)) == 3)
      ac.sdb = a.sdb(:, :, ii); 
   else
      ac.sdb = a.sdb(:, ii);       
   end
end
if isfield(a, 'pg'), 
   if (length(size(ac.pg)) == 3)
      ac.pg = a.pg(:, :, ii); 
   else
      ac.pg = a.pg(:, ii);       
   end
end
if isfield(a, 'cor'), 
   if (length(size(ac.cor)) == 3)
      ac.cor = a.cor(:, :, ii); 
   else
      ac.cor = a.cor(:, ii);       
   end
end
if isfield(a, 'sw'), 
   if (length(size(ac.sw)) == 3)
      ac.sw = a.sw(:, :, ii); 
   else
      ac.sw = a.sw(:, ii);       
   end
end
if isfield(a, 'rssi'), 
   if (length(size(ac.rssi)) == 3)
      ac.rssi = a.rssi(:, :, ii); 
   else
      ac.rssi = a.rssi(:, ii);       
   end
end
if isfield(a, 'status'), 
   if (length(size(ac.sw)) == 3)
      ac.sw =      a.sw(:, :, ii); 
   else
      ac.sw =      a.sw(:, ii);       
   end
end
if isfield(a, 'HiV'),    
   if (length(size(ac.HiV)) == 3)
      ac.HiV =     a.HiV(:, :, ii); 
   else
      ac.HiV =     a.HiV(:, ii);       
   end
end
if isfield(a, 'XmtAmps'),
   if (length(size(ac.XmtAmps)) == 3)
      ac.XmtAmps = a.XmtAmps(:, :, ii); 
   else
      ac.XmtAmps = a.XmtAmps(:, ii);       
   end
end

if isfield(a, 'heading'),         ac.heading     = a.heading(ii);     end
if isfield(a, 'pitch'),           ac.pitch       = a.pitch(ii);       end
if isfield(a, 'roll'),            ac.roll        = a.roll(ii);        end
if isfield(a, 'heave'),            ac.heave        = a.heave(ii);     end
if isfield(a, 'pressure'),       ac.pressure        = a.pressure(ii); end
if isfield(a, 'soundspeed'),      ac.soundspeed  = a.soundspeed(ii);  end
if isfield(a, 'temperature'),     ac.temperature = a.temperature(ii); end
if isfield(a, 'ctemperature'),  ac.ctemperature = a.ctemperature(ii); end
if isfield(a, 'dday'),          ac.dday      = a.dday(ii);            end
if isfield(a, 'corr_dday'),     ac.corr_dday      = a.corr_dday(ii);  end
if isfield(a, 'lon'),           ac.lon      = a.lon(ii);              end
if isfield(a, 'lat'),           ac.lat      = a.lat(ii);              end
if isfield(a, 'noise'),         ac.noise     = a.noise(:,ii);         end
if isfield(a, 'voltage'),       ac.voltage   = a.voltage(ii);         end
if isfield(a, 'xflux'),         ac.xflux     = a.xflux(ii);           end
if isfield(a, 'yflux'),         ac.yflux     = a.yflux(ii);           end
if isfield(a, 'zflux'),         ac.zflux     = a.zflux(ii);           end


if isfield(a, 'ens_num'),       ac.ens_num   = a.ens_num(ii);         end
if isfield(a, 'num_pings'),     ac.num_pings = a.num_pings(ii);       end
if isfield(a, 'RDIens_num'),    ac.RDIens_num    = a.RDIens_num(ii);  end
if isfield(a, 'RDIymdhms'),     ac.RDIymdhms    = a.RDIymdhms(ii);    end
if isfield(a, 'config_nbytes'), ac.config_nbytes= a.config_nbytes(ii);end
if isfield(a, 'config_nleader'),ac.config_nleader= a.config_nleader(ii); end


if isfield(a, 'bt'),
   ac.bt.vel = a.bt.vel(:,ii);
   ac.bt.range = a.bt.range(:,ii);
   if isfield(ac.bt, 'cor'),    ac.bt.cor    = a.bt.cor(:,ii);     end
   if isfield(ac.bt, 'amp'),    ac.bt.amp    = a.bt.amp(:,ii);     end
   if isfield(ac.bt, 'sdb'),    ac.bt.sdb    = a.bt.sdb(:,ii);     end
   if isfield(ac.bt, 'rssi'),   ac.bt.rssi   = a.bt.rssi(:,ii);    end
   if isfield(ac.bt, 'pg'),     ac.bt.pg     = a.bt.pg(:,ii);      end   
end

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


if isfield(a, 'vel1'), ac.vel1 = a.vel1(:,ii); end
if isfield(a, 'vel2'), ac.vel2 = a.vel2(:,ii); end
if isfield(a, 'vel3'), ac.vel3 = a.vel3(:,ii); end
if isfield(a, 'vel4'), ac.vel4 = a.vel4(:,ii); end
if isfield(a, 'amp1'), ac.amp1 = a.amp1(:,ii); end
if isfield(a, 'amp2'), ac.amp2 = a.amp2(:,ii); end
if isfield(a, 'amp3'), ac.amp3 = a.amp3(:,ii); end
if isfield(a, 'amp4'), ac.amp4 = a.amp4(:,ii); end
if isfield(a, 'sdb1'), ac.sdb1 = a.sdb1(:,ii); end
if isfield(a, 'sdb2'), ac.sdb2 = a.sdb2(:,ii); end
if isfield(a, 'sdb3'), ac.sdb3 = a.sdb3(:,ii); end
if isfield(a, 'sdb4'), ac.sdb4 = a.sdb4(:,ii); end
if isfield(a, 'cor1'), ac.cor1 = a.cor1(:,ii); end
if isfield(a, 'cor2'), ac.cor2 = a.cor2(:,ii); end
if isfield(a, 'cor3'), ac.cor3 = a.cor3(:,ii); end
if isfield(a, 'cor4'), ac.cor4 = a.cor4(:,ii); end
if isfield(a, 'pg1'), ac.pg1 = a.pg1(:,ii); end
if isfield(a, 'pg2'), ac.pg2 = a.pg2(:,ii); end
if isfield(a, 'pg3'), ac.pg3 = a.pg3(:,ii); end
if isfield(a, 'pg4'), ac.pg4 = a.pg4(:,ii); end
if isfield(a,  'umeas'),ac.umeas = a.umeas(:,ii); end
if isfield(a,  'vmeas'),ac.vmeas = a.vmeas(:,ii); end
if isfield(a,  'uship'),ac.uship = a.uship(:,ii); end
if isfield(a,  'vship'),ac.vship = a.vship(:,ii); end
if isfield(a,  'uabs'),ac.uabs = a.uabs(:,ii); end
if isfield(a,  'vabs'),ac.vabs = a.vabs(:,ii); end
if isfield(a, 'fmeas'), ac.fmeas = a.fmeas(:,ii); end
if isfield(a, 'pmeas'), ac.pmeas = a.pmeas(:,ii); end
if isfield(a, 'wmeas'), ac.wmeas = a.wmeas(:,ii); end
if isfield(a, 'emeas'), ac.emeas = a.emeas(:,ii); end
if isfield(a,   'sw1'), ac.sw1 = a.sw1(:,ii); end
if isfield(a,   'sw2'), ac.sw2 = a.sw2(:,ii); end
if isfield(a,   'sw3'), ac.sw3 = a.sw3(:,ii); end
if isfield(a,   'sw4'), ac.sw4 = a.sw4(:,ii); end



if (isfield(a,'pflag')),         ac.pflag       = a.pflag(:,ii); end
if (isfield(a,'w')),             ac.w           = a.w(:,ii); end
if (isfield(a,'e')),             ac.e           = a.e(:,ii); end
if (isfield(a,'uabs_sm')),       ac.uabs_sm     = a.uabs_sm(:,ii); end
if (isfield(a,'vabs_sm')),       ac.vabs_sm     = a.vabs_sm(:,ii); end
if (isfield(a,'uabs_rawnav')),   ac.uabs_rawnav = a.uabs_rawnav(:,ii); end
if (isfield(a,'vabs_rawnav')),   ac.vabs_rawnav = a.vabs_rawnav(:,ii); end
if (isfield(a,'fabs_sm')),       ac.fabs_sm     = a.fabs_sm(:,ii); end
if (isfield(a,'pabs_sm')),       ac.pabs_sm     = a.pabs_sm(:,ii); end

if isfield(a,'uship_sm'),        ac.uship_sm    = a.uship_sm(ii); end
if isfield(a,'vship_sm'),        ac.vship_sm    = a.vship_sm(ii); end
if isfield(a,'ref_std'),         ac.ref_std     = a.ref_std(ii); end
if isfield(a,'smmps'),           ac.smmps       = a.smmps(ii); end
if isfield(a,'x'),               ac.x           = a.x(ii); end
if isfield(a,'y'),               ac.y           = a.y(ii); end
if isfield(a,'smcog'),           ac.smcog       = a.smcog(ii); end
if isfield(a,'rawmps'),          ac.rawmps      = a.rawmps(ii); end
if isfield(a,'wmean'),           ac.wmean       = a.wmean(ii); end

%now do the fields within structures

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


get_instfields;
for ofieldi = 1:length(instfields)
   if (isfield(a, instfields{ofieldi}))
      ostruct = getfield(a, instfields{ofieldi});
      if  ~isemptys(ostruct)
         fnames = fieldnames(ostruct);
         for fieldi=1:length(fnames);
            field = getfield(ostruct, fnames{fieldi});
            ostruct = setfield(ostruct, fnames{fieldi}, field(ii));
         end
         ac = setfield(ac, instfields{ofieldi}, ostruct);
      end
   end
end




   