function [data, config] = restruct(instclass, dd, varargin)
% [dd, config] = restruct(instclass, data, varargin)
% index is indices: step, range, or list
% rotate velocities into beam coords, 
% reshape things to output one structure with vel, amp, cor, pg, temperature,etc
% varargin can be  'irange' (index start and end)
%                  'ilist' (indices)
%                  'step' (subsample whole thing by this)
%                  'ddayrange' (ddrange start and end)

cfg.irange =  [];
cfg.ilist =     [];
cfg.ddayrange = [];
cfg.step =   1;
cfg = fillstruct(cfg, varargin);

if isa(instclass, 'bb')
   inst = 'BB';
elseif isa(instclass, 'nb')
   inst = 'NB';
elseif isa(instclass, 'os')
   inst = 'OS';
else
   help restruct
   error('choices for instclass are bb, nb, and os')
end


index = [1:length(dd.dday)]';

if (~isempty(cfg.irange)),index = [cfg.irange(1):cfg.step:cfg.irange(2)]; end
if (~isempty(cfg.ilist)),       index = cfg.ilist; end
if (~isempty(cfg.ddayrange))
   [junk, first_index] = min(abs(dd.dday - cfg.ddayrange(1)));
   [junk, last_index]  = min(abs(dd.dday - cfg.ddayrange(end)));
   index = [first_index:cfg.step:last_index]';
end


if (isfield(dd, 'vel'))
   data.vel1=squeeze(dd.vel(:,1,index));
   data.vel2=squeeze(dd.vel(:,2,index));
   data.vel3=squeeze(dd.vel(:,3,index));
   data.vel4=squeeze(dd.vel(:,4,index));
   data.bins = [1:length(dd.vel(:,1,1))]';
end

if (isfield(dd, 'amp'))
   data.amp1=squeeze(dd.amp(:,1,index));
   data.amp2=squeeze(dd.amp(:,2,index));
   data.amp3=squeeze(dd.amp(:,3,index));
   data.amp4=squeeze(dd.amp(:,4,index));
   data.bins = [1:length(dd.amp(:,1,1))]';
end

if (isfield(dd, 'cor'))
   data.cor1=squeeze(dd.cor(:,1,index));
   data.cor2=squeeze(dd.cor(:,2,index));
   data.cor3=squeeze(dd.cor(:,3,index));
   data.cor4=squeeze(dd.cor(:,4,index));
   data.bins = [1:length(dd.cor(:,1,1))]';
end
if (isfield(dd, 'rssi'))
   data.rssi1=squeeze(dd.rssi(:,1,index));
   data.rssi2=squeeze(dd.rssi(:,2,index));
   data.rssi3=squeeze(dd.rssi(:,3,index));
   data.rssi4=squeeze(dd.rssi(:,4,index));
   data.bins = [1:length(dd.rssi(:,1,1))]';
end

if (isfield(dd, 'sw'))
   data.sw1=squeeze(dd.sw(:,1,index));
   data.sw2=squeeze(dd.sw(:,2,index));
   data.sw3=squeeze(dd.sw(:,3,index));
   data.sw4=squeeze(dd.sw(:,4,index));
   data.bins = [1:length(dd.sw(:,1,1))]';
end

if (isfield(dd, 'pg'))
   data.pg1=squeeze(dd.pg(:,1,index));
   data.pg2=squeeze(dd.pg(:,2,index));
   data.pg3=squeeze(dd.pg(:,3,index));
   data.pg4=squeeze(dd.pg(:,4,index));
   data.bins = [1:length(dd.pg(:,1,1))]';
end


data.dday = dd.dday(index);
data.ens_num = dd.ens_num(index);

if (isfield(dd, 'heading')),  data.heading = dd.heading(index); end
if (isfield(dd, 'pitch')),    data.pitch   = dd.pitch(index);   end
if (isfield(dd, 'roll')),     data.roll = dd.roll(index);       end
if (isfield(dd, 'temperature')), data.temperature = dd.temperature(index); end
if (isfield(dd, 'soundspeed')),
                        data.soundspeed = dd.soundspeed(index); end
if (isfield(dd, 'meas_soundspeed')),   
                        data.meas_soundspeed = dd.meas_soundspeed(index); end
if (isfield(dd, 'aduheading')),  data.aduheading = dd.aduheading(index);  end

if (isfield(dd, 'enu'))
   data.umeas=squeeze(dd.enu(:,1,index));
   data.vmeas=squeeze(dd.enu(:,2,index));
   data.wmeas=squeeze(dd.enu(:,3,index));
end

if (isfield(dd, 'xyze'))
   data.emeas=squeeze(dd.xyze(:,4,index));
end


if (isfield(dd, 'nav'))
   if (isfield(dd.nav, 'txy1'))
      data.mps = ll2mps(dd.nav.txy1(1,index), ...
                        dd.nav.txy1(2,index), dd.nav.txy1(3,index))';
      data.lon = dd.nav.txy1(2,:)';
      data.lat = dd.nav.txy1(3,:)';
   end
end


data.config =  dd.config;
if (isfield(dd,'bt'))
   data.bt.vel   = dd.bt.vel(:,index);
   data.bt.range  = dd.bt.range(:,index);
   if isfield(dd.bt, 'cor'),    data.bt.cor    = dd.bt.cor(:,index);     end
   if isfield(dd.bt, 'amp'),    data.bt.amp    = dd.bt.amp(:,index);     end
   if isfield(dd.bt, 'rssi'),   data.bt.rssi   = dd.bt.rssi(:,index);    end
   if isfield(dd.bt, 'pg'),     data.bt.pg     = dd.bt.pg(:,index);      end   
end


if (isfield(dd, 'depth'))
   data.depth = dd.depth;
end


%initialize some default titles and limits 

config.title.vel1            = sprintf('%s BEAM 1 VELOCITY (m/s)', inst);
config.title.vel2            = sprintf('%s BEAM 2 VELOCITY (m/s)', inst);
config.title.vel3            = sprintf('%s BEAM 3 VELOCITY (m/s)', inst);
config.title.vel4            = sprintf('%s BEAM 4 VELOCITY (m/s)', inst);

config.title.cor1            = sprintf('%s BEAM 1 CORRELATION', inst);
config.title.cor2            = sprintf('%s BEAM 2 CORRELATION', inst);
config.title.cor3            = sprintf('%s BEAM 3 CORRELATION', inst);
config.title.cor4            = sprintf('%s BEAM 4 CORRELATION', inst);

config.title.sw1            = sprintf('%s BEAM 1 SPECTRAL WIDTH', inst);
config.title.sw2            = sprintf('%s BEAM 2 SPECTRAL WIDTH', inst);
config.title.sw3            = sprintf('%s BEAM 3 SPECTRAL WIDTH', inst);
config.title.sw4            = sprintf('%s BEAM 4 SPECTRAL WIDTH', inst);

config.title.amp1            = sprintf('%s BEAM 1 AMPLITUDE', inst);
config.title.amp2            = sprintf('%s BEAM 2 AMPLITUDE', inst);
config.title.amp3            = sprintf('%s BEAM 3 AMPLITUDE', inst);
config.title.amp4            = sprintf('%s BEAM 4 AMPLITUDE', inst);

config.title.rssi1            = sprintf('%s BEAM 1 RSSI', inst);
config.title.rssi2            = sprintf('%s BEAM 2 RSSI', inst);
config.title.rssi3            = sprintf('%s BEAM 3 RSSI', inst);
config.title.rssi4            = sprintf('%s BEAM 4 RSSI', inst);

config.title.umeas            = sprintf('%s MEAS U (m/s)', inst);
config.title.vmeas            = sprintf('%s MEAS V (m/s)', inst);
config.title.wmeas            = sprintf('%s MEAS W (m/s)', inst);
config.title.emeas            = sprintf('%s ERROR VEL (m/s)', inst);


config.minclipval.vel1 = -6;    config.maxclipval.vel1 =  6;
config.minclipval.vel2 = -6;    config.maxclipval.vel2 =  6;
config.minclipval.vel3 = -6;    config.maxclipval.vel3 =  6;
config.minclipval.vel4 = -6;    config.maxclipval.vel4 =  6;


config.minclipval.amp1 = 10;    config.maxclipval.amp1 =  200;
config.minclipval.amp2 = 10;    config.maxclipval.amp2 =  200;
config.minclipval.amp3 = 10;    config.maxclipval.amp3 =  200;
config.minclipval.amp4 = 10;    config.maxclipval.amp4 =  200;


config.minclipval.rssi1 = 10;    config.maxclipval.rssi1 =  200;
config.minclipval.rssi2 = 10;    config.maxclipval.rssi2 =  200;
config.minclipval.rssi3 = 10;    config.maxclipval.rssi3 =  200;
config.minclipval.rssi4 = 10;    config.maxclipval.rssi4 =  200;


config.minclipval.cor1 = 20;    config.maxclipval.cor1 =  260;
config.minclipval.cor2 = 20;    config.maxclipval.cor2 =  260;
config.minclipval.cor3 = 20;    config.maxclipval.cor3 =  260;
config.minclipval.cor4 = 20;    config.maxclipval.cor4 =  260;

config.minclipval.sw1 = 20;    config.maxclipval.sw1 =  260;
config.minclipval.sw2 = 20;    config.maxclipval.sw2 =  260;
config.minclipval.sw3 = 20;    config.maxclipval.sw3 =  260;
config.minclipval.sw4 = 20;    config.maxclipval.sw4 =  260;

config.minclipval.umeas = -6;    config.maxclipval.umeas =  6;
config.minclipval.vmeas = -6;    config.maxclipval.vmeas =  6;
config.minclipval.wmeas = -6;    config.maxclipval.wmeas =  6;
config.minclipval.emeas = -6;    config.maxclipval.emeas =  6;


config.comment = '';