function bssbndsok(bnds)
% BSSBNDSOK - Check the bounds of a B-spline series data structure for
%             validity.
%
% INPUTS:
%
%    bnds:
%       The lower and upper bounds for the estimation interval over which
%       the bspline series is to be created for evaluation. This is an NDIM
%       by 2 matrix, specifying the minimum and maximum values used to
%       create the B-spline series partition for each dimension.
%
%       NDIM must be >= 1. (not currently checked)
%
%       The BNDS variable must:
%
%         be nonempty
%         be numeric
%         be an NDIM by 2 matrix
%         be finite, not inf or -inf
%         and have bnds(i,1) < bnds(i,2),  i = 1:NDIM
%
% OUTPUTS:
%
%   None.
%

%
% Copyright 2014-2023 Biometrics Northwest LLC
%

%
% Modified: 2023-02-13
% Author : Kevin R. Gehringer
%
%    Added a test for finite bounds and no NaNs.
%
% Modified: 2023-01-23
% Author : Kevin R. Gehringer
%
%    Fixed a problem with formatted arguments to error() being scalar.
%
% Modified: 2022-12-18
% Author : Kevin R. Gehringer
%
%    Removed the output flag. It was not necessary since the function
%    signals errors. If it returns without an error all is OK.
%
% Created: 2014-11-25
% Author : Kevin R. Gehringer
%

%
% Begin the testing with nonempty and numeric.
%
if ( isempty(bnds) )
   error('The B-spline series estimation bounds were empty.');
end

if ( ~isnumeric(bnds) )
   error('The B-spline series estimation bounds were not numeric.');
end

if ( any(isnan(bnds),'all') )
   error('The B-spline series estimation bounds may not contain NaN values.');
end

if ( any(isinf(bnds),'all') )
   error('The B-spline series estimation bounds may not contain inf or -inf values.');
end

%
% At this point, we know that BNDS is nonempty, and numeric, and has no NaNs or inf values.
%
sz_bnds = size(bnds);
if ( sz_bnds(2) ~= 2 )
   error('The number of columns in the B-spline series estimation bounds was not equal to 2.');
end
   
ndim_b = sz_bnds(1);
%if ( ndim_b < 1 || ndim_b > 3 )
%   error('The number of dimensions used for the B-spline series must be 1, 2, or 3, and a value of %d was found for the estimation bounds.',ndim_b);
%end

%
% Check bounds for validity: bnds(i,1) < bnds(i,2), i=1:NDIM.
%
width     = bnds(:,2) - bnds(:,1);
row_ids   = (1:ndim_b)';
width_nok = width <= 0;

if ( any(width_nok) )
   fprintf('The estimation bounds for the B-Spline series were not valid for dimension %d: (%g, %g).\n', ...
   [row_ids(width_nok) bnds(width_nok,1) bnds(width_nok,2)]');
   error('The estimation bounds were invalid.');
end


