function bindata = bin_extract(binfpath, fext, logger_timerange, vv)
% bindata = bin_extract(binfpath, fext, logger_timerange)
% bindata is nrows X N array read from the list of files specified as
%    binfpath/*fext.bin (bin files were written by serasc2bin.py) 
%    with logger_times matching the boundaries specified by logger_timerange
%
% see 'serasc2bin.py' for more details of columns

% undocumented option: vv == 1 is verbose
%

% JH 2001/04/05 
if (~exist('vv'))
   vv = 0;
end
     

filelist = dirs(fullfile(binfpath, sprintf('*%s.bin', fext)));

bindata = [];


for filei = 1:length(filelist)
   fname = filelist(filei).name;
   bdata = read_bin(fullfile(binfpath, fname));
   goodi = find(  (bdata(1,:) >= logger_timerange(1)) & ...
                  (bdata(1,:) <= logger_timerange(2)));
   bindata = [bindata bdata(:,goodi)];
   %fix GGA and ATT decimal days to have the integer part match logger dday
   if ( (fext == 'adu') | (fext == 'gps'))
      bindata(2,:) = bindata(2,:) + round(bindata(1,:) - bindata(2,:));
   end
   
   if (vv)
      fprintf('%s - %s',to_date(2001,bdata(1,1)),to_date(2001,bdata(1,end)))
      if (~isempty(goodi))
         fprintf('(goodi: %s - %s)',...
                 to_date(2001,bdata(1,goodi(1))),...
                 to_date(2001,bdata(1,goodi(end))))
      end
      fprintf('\n')
   end
   
   if (logger_timerange(1) > max(bdata(1,:)))
      filei = length(filelist) +1;
   end

end



if (length(bdata(1,:)) == 0)
   warning('returning no data!!!')
end
   
