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


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

pingtype = cfg.pingtype;

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


data.depth = dd.depth;

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

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

data.ens_num = dd.ens_num(index);


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(2,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);
   data.bt.cor   = dd.bt.cor(:,index);
   data.bt.amp   = dd.bt.amp(:,index);
   data.bt.rssi  = dd.bt.rssi(:,index);
end

%initialize some default titles and limits 

config.title.vel1     = ['OS ',pingtype, ' BEAM 1 VELOCITY (m/s)'];
config.title.vel2     = ['OS ',pingtype, ' BEAM 2 VELOCITY (m/s)'];
config.title.vel3     = ['OS ',pingtype, ' BEAM 3 VELOCITY (m/s)'];
config.title.vel4     = ['OS ',pingtype, ' BEAM 4 VELOCITY (m/s)'];
                   
config.title.cor1     = ['OS ',pingtype, ' BEAM 1 CORRELATION'];
config.title.cor2     = ['OS ',pingtype, ' BEAM 2 CORRELATION'];
config.title.cor3     = ['OS ',pingtype, ' BEAM 3 CORRELATION'];
config.title.cor4     = ['OS ',pingtype, ' BEAM 4 CORRELATION'];
                   
config.title.amp1     = ['OS ',pingtype, ' BEAM 1 AMPLITUDE'];
config.title.amp2     = ['OS ',pingtype, ' BEAM 2 AMPLITUDE'];
config.title.amp3     = ['OS ',pingtype, ' BEAM 3 AMPLITUDE'];
config.title.amp4     = ['OS ',pingtype, ' BEAM 4 AMPLITUDE'];


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


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;

if (pingtype == 'osn')
   config.minclipval.cor1 = 0;    config.maxclipval.cor1 =  260;
   config.minclipval.cor2 = 0;    config.maxclipval.cor2 =  260;
   config.minclipval.cor3 = 0;    config.maxclipval.cor3 =  260;
   config.minclipval.cor4 = 0;    config.maxclipval.cor4 =  260;
else
   config.minclipval.cor1 = 60;    config.maxclipval.cor1 =  200;
   config.minclipval.cor2 = 60;    config.maxclipval.cor2 =  200;
   config.minclipval.cor3 = 60;    config.maxclipval.cor3 =  200;
   config.minclipval.cor4 = 60;    config.maxclipval.cor4 =  200;
end


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 = '';