function as = get_serfields(data, varargin)
% aux_struct = get_serfields(data, varargin)
% ("data" can simply be data.info.rawfilebase and data.info.ilist)
%              (raw file name [without path or '.raw'])     (ilist into file)
%              (such as nbp2001_091_52596'            )
%
% read .gbin files where they exist and return a structure suitable for
% adding info to raw data structure (returned by read).  The filename
% root is in data.info.rawfilebase; indices are in data.info.ilist:
% varargin is only used to set paths and extensions.
%
% default: assume adcptree directory structure and paths are relative
% to a subdirectory such as load/.  Override as necessary with varargin
%
% example: (from load/) ------
%   ilist = 1:300;
%   data = read(nb, fullrawfile, 'yearbase', 2001, 'ilist', ilist,...
%         'vars', 'all')
%   data = update_raw(data, get_serfields(data));
% % -------------------------
%
% NOTE!!! varargin specifies what kind of gyro to use:
%
% use_external_gyro     [0 or 1]   %default is 0: (assume direct gyro input)
% 
% varargin can be override paths
% 
%   name             [what it is]                         % default
%
% timpath [path to *.tim.gbin]             '../gbinfiles/time'   
% pospath [path to *.pos.gbin]             '../gbinfiles/pcode'   
% adupath [path to *.adu.gbin]             '../gbinfiles/adu2'   
% hdgpath [path to *.hdg.gbin]             '../gbinfiles/gyro'   
% spdpath [path to *.spd.gbin]             '../gbinfiles/sndspd'   
%
% timgbin  [extension to read gridded binary file]        % '.tim.gbin'
% posgbin  [extension to read gridded binary file]        % '.pos.gbin'
% adugbin  [extension to read gridded binary file]        % '.adu.gbin'
% hdggbin  [extension to read gridded binary file]        % '.hdg.gbin'
% spdgbin  [extension to read gridded binary file]        % '.spd.gbin'
%
% verbose [0 1]    give error messages if file not found  % 0
%

%JH 2001/04/05

cfg.use_external_gyro = 0;
cfg.verbose = 0;

cfg.timpath =   '../gbinfiles/time';
cfg.pospath =   '../gbinfiles/pcode';
cfg.adupath =   '../gbinfiles/adu2';
cfg.hdgpath =   '../gbinfiles/gyro';
cfg.spdpath =   '../gbinfiles/sndspd';
%extension to rawfnamebase 
cfg.timgbin =   'tim.gbin';
cfg.posgbin =   'pos.gbin';
cfg.adugbin =   'adu.gbin';
cfg.hdggbin =   'hdg.gbin';
cfg.spdgbin =   'spd.gbin';
%
   
cfg = fillstruct(cfg, varargin, 0);

if (~isfield(data, 'info'))
   error('no "info" structure in data')
end
infostruct = data.info;

if (~isfield(infostruct, 'rawfilebase') )
   error('data.info  is missing "rawfilebase"')
end
rawfilebase = data.info.rawfilebase;

if (~isfield(infostruct, 'ilist') )
   error('data.info  is missing "ilist"')
end
ilist = data.info.ilist; %this gets used 


% set up the structure

%get time data
timfullfile = sprintf('%s.%s', fullfile( cfg.timpath, rawfilebase),...
                      cfg.timgbin);
if (exist(timfullfile, 'file'))
   timdata = read_bin(timfullfile);

   as.dday = timdata(2,ilist)';   %best time, not necessarily logger time
   nav.sec_pc_minus_utc =  timdata(3,ilist)';
else
   %require the time file at any rate: and show whether we have filename right
   warning(sprintf('tim.gbin file %s does not exist\n',timfullfile))
end


%get position data
posfullfile = sprintf('%s.%s', fullfile( cfg.pospath, rawfilebase),...
                      cfg.posgbin);
if (exist(posfullfile, 'file'))
   posdata = read_bin(posfullfile);

   nav.txy1 = [as.dday(:)' ;posdata([1 2],ilist)];
   nav.txy2 = 0*nav.txy1;
   as.nav = nav;
else
   if (cfg.verbose)
      fprintf('pos.gbin file %s does not exist\n',posfullfile)
   end
end


% get external gyro if necessary
hdgfullfile = sprintf('%s.%s', fullfile( cfg.hdgpath, rawfilebase),...
                      cfg.hdggbin);
if (cfg.use_external_gyro)
   if (exist(hdgfullfile, 'file'))
      hdgdata = read_bin(hdgfullfile);
      as.heading = hdgdata(ilist)';
   else
      error('no external gyro gbin file could be found')
   end
end


% get adu 
adufullfile = sprintf('%s.%s', fullfile( cfg.adupath, rawfilebase),...
                      cfg.adugbin);
if (exist(adufullfile, 'file'))
   adudata = read_bin(adufullfile);      

   as.aduheading= adudata(1,ilist)';
   as.pitch     = adudata(2,ilist)';
   as.roll      = adudata(3,ilist)';
   as.attdday   = adudata(4,ilist)';
   as.mrms      = adudata(5,ilist)';
   as.brms      = adudata(6,ilist)';
   
else
   if (cfg.verbose)
      fprintf('adu.gbin file %s does not exist\n',adufullfile)
   end
end


% get soundspeed
spdfullfile = sprintf('%s.%s', fullfile( cfg.spdpath, rawfilebase),...
                      cfg.spdgbin);
if (exist(spdfullfile, 'file'))
   spddata = read_bin(spdfullfile);      
   as.meas_soundspeed = spddata(ilist)';
else
   if (cfg.verbose)
      fprintf('spd.gbin file %s does not exist\n',spdfullfile)
   end
end


if (~isstruct(as))
   as = [];
else
   fnames = fieldnames(as);
   if (length(fnames) == 0)
      as = [];
   end
end

