function  [binpt,binmean,binstd, binn]=boxbin(x,y, varargin)
%function [binpt,binmean,binstd, binn]=boxbin(x,y, varargin)
%  return    center, average, std, numpts (column vectors)
% varargin:
%
%  name          default                % description
% ---------      ----------             % ----------------------------------
% 'stepi'        divide into 20 segs    % bins centers are this far apart (index)
% 'segends'        []                   % array of segment dividers
%                                       % should be in the same units as x
%                                       % - first segment ENDS at segends(1)
%                                       % - last segment goes from segends(end)
%                                       %      to the last data point
%                                       % OVERRIDES stepi 
% 'avgtype'        'mean'               % how to average (or filter)
%                                       %  'mean'  is mstdgap mean
%                                       %  'med'  is mstdgap median
%                                       %  'min'  is nanmin
%                                       %  'max'  is nanmax
% 'noends'         [0, 1]               % if specifying segends, 
%                                       %    don't force endpoint values
%                                       %    to extend to the edges of 
%                                       %    the data
% eg:
%  [binpt,binmean,binstd, binn]=boxbin(x, y, 'avgtype', 'med')
% binpt is the median of the existing x values in the window 
%
%  [binpt,binmean,binstd, binn]=boxbin(x, y, 'avgtype', 'min')
% binpt is the minimum of the existing x values in the window 
%
% returns NaN, 0, 0 for binmean, binstd, binn if no values occur in window


cfg.stepi = [];              % index step
cfg.window = [];            %not using it yet
cfg.avgtype = 'mean';
cfg.segends = [];
cfg.noends = 0;                


cfg = fillstruct(cfg, varargin, 0);

nani = find(isnan(x));
x(nani)=[];
y(nani)=[];


%use step if it exists
if (isempty(cfg.stepi))
   cfg.stepi = round(length(x)/20);
end
binends=x(1:cfg.stepi:end);
if (binends(end) ~= max(x))
   binends = [binends max(x)];
end

%override if segends exists
if ~isempty(cfg.segends)
   cfg.stepi = [];
   if cfg.noends
      binends = [cfg.segends(:)'];
   else
      binends = [ min(x) cfg.segends(:)' x(end)];
   end      
end


n=length(binends);
binmean=NaN*zeros(n-1,1);            % mean of y
binpt = binmean;                     % x
binstd=binmean;                      % std of y
binn = binmean;                      % num pts


for ii = 1:n-1
   index = find( (x >= binends(ii)) & (x < binends(ii+1)));

   jj = find(~isnan(y(index)));
   if ~isempty(index(jj))
      binpt(ii) = mean(x(index(jj)));

      if strcmp(cfg.avgtype, 'mean');  binmean(ii) = mean(y(index(jj))); end
      if strcmp(cfg.avgtype, 'med');   binmean(ii) = median(y(index(jj))); end
      if strcmp(cfg.avgtype, 'min');   binmean(ii) = min(y(index(jj))); end
      if strcmp(cfg.avgtype, 'max');   binmean(ii) = max(y(index(jj))); end
      
      binn(ii) = length(jj);
      binstd(ii) = std(y(index(jj)));
      
   else
      binpt(ii) = (binends(ii) +  binends(ii+1))/2; %take the midpoint
      binstd(ii) = 0;
      binmean(ii) = NaN;
      binnn(ii) = 0;
   end
end


