function [dd, config] = get_rndata(time_range, varargin)
% get raw and navigation data (merged)
% [dd, config] = get_rndata(time_range, varargin)  %uses time_range
%
% time_range should be like [94 94.0174]   %for 15 minutes
% varargin is a collection of name, value pairs
% 
%    name                                              default   value
% hoffset        mounting angle   +   phase            [required] 
%                (from start.cnf) (from watertrk)
%
% geometry       'convex' (eg. Ocean Surveyor)          'concave'
%                or 'concave' (NB shipboard)
% use_external_gyro    [0 1] (use external serial           0
%                gyro measurement)
%
% use_adu        use ashtech heading (no editing)           0
%                (if chosen, overrides use_external_gyro
%
% rawpath        relative path to adcp raw files       '../raw/adcp_pings';
%
%  time_path             : path to [logger_dday best_dday] gbin file
%                             default '../gbinfiles/time'
%  time_ext              : extension for .???.gbin time file
%                             default 'tim'
% 
%  position_path         : path to [ lon lat] gbin file
%                             default '../gbinfiles/pcode'
%  position_ext          : extension for .???.gbin position file
%                             default 'pos'
% 
%  ashtech_path          : path to [heading pitch roll] gbin file
%                             default '../gbinfiles/adu2'
%  ashtech_ext           : extension for .???.gbin ashtech file
%                             default 'adu'
% 
%  gyro_path             : path to [meas_heading] gbin file
%                             default '../gbinfiles/gyro'
%  gyro_ext              : extension for .???.gbin gyro heading file
%                             default 'hdg'
% 
%  sound_path            : path to [meas_soundspeed] gbin file
%                             default '../gbinfiles/sndspd'
%  sound_ext             : extension for .???.gbin soundspeed file
%                                default 'snd'
% verbose        [0 or 1]  say some useful things            0
% 
% example: NB Palmer 
% hoffset = 45.0 + .55;  %Mounting Angle +  phase 
% 
% %%% do this from the raw/ directory
% [dd, config] = get_rndata([95 96], 'hoffset', 45.55, 'gui',1, ...
%                           'use_external_gyro', 1);
% %then run 
% rlookit(dd, config, 1, [15])  %beam 1, bin 15
% rlookit2(dd, config, [15])  % bin 15
%

cfg.hoffset = [];
cfg.geometry = 'concave';
cfg.time_path =      '../gbinfiles/time';
cfg.position_path =  '../gbinfiles/pcode';
cfg.ashtech_path =   '../gbinfiles/adu2';
cfg.gyro_path =      '../gbinfiles/gyro';
cfg.sound_path =     '../gbinfiles/sndspd';
cfg.rawpath =        '../raw/adcp_pings';
cfg.time_ext =     'tim';
cfg.position_ext = 'pos';
cfg.ashtech_ext =  'adu';
cfg.gyro_ext =     'hdg';
cfg.sound_ext =    'snd';
cfg.gui = 0;
cfg.step = 30;
cfg.use_adu = 0;
cfg.use_external_gyro = 0;


cfg = fillstruct(cfg, varargin, 0);

if (isempty(cfg.hoffset))
   help(mfilename)
   error('must set transducer angle')
end

step = cfg.step;

%now get the data
data = [];

finfo = tr2ilist(time_range,  'gbinpath', cfg.time_path);

if (isempty(finfo{1,1}))
   error('no data found in that time range')
else
   for ii = 1:length(finfo(:,1))
      ilist = finfo{ii,2};
      fullrawfile = fullfile(cfg.rawpath,finfo{ii,1});
      fprintf('reading %d pings [%d-%d] from file %d of %d (%s)\n', ...
              length(ilist), ilist(1), ilist(end), ...
              ii, length(finfo(:,1)),  finfo{ii,1});
      newdata = read(nb, fullrawfile, 'yearbase', 2001, 'ilist', ilist,...
                     'vars', 'all');
      aux_struct = get_serfields(...
         newdata,'verbose',1, 'use_external_gyro',cfg.use_external_gyro,...
         'timpath', cfg.time_path,      'timgbin', 'tim.gbin', ...
         'pospath', cfg.position_path,   'posgbin', 'pos.gbin',...
         'adupath', cfg.ashtech_path,   'adugbin', 'adu.gbin',...
         'hdgpath', cfg.gyro_path,      'hdggbin', 'hdg.gbin',...
         'spdpath', cfg.sound_path,     'spdgbin', 'spd.gbin')    ;
      newdata = update_raw(newdata, aux_struct);
      data = catadcp(data, newdata);
   end
end

data.depth = mk_depth(data, 5);

if (cfg.use_adu)
   hdg = data.aduheading;
else
   hdg = data.heading;
end

[data.enu data.xyze] = beam_enu(data.vel,...
                                cfg.hoffset + hdg, 0*hdg, 0*hdg', ...
                                'geometry', cfg.geometry);

[dd, config] = restruct(nb, data);

