function OUT = var_spher_plot_test(M,varargin)

%VAR_SPHER estimates a spherical variogram for a given data set
%using nonlinear least squares.

%Written by: TONY E. SMITH, 2/9/01

%Functions called: variogram.m, var_fit.m, spher.m, var_plot.m
%
%INPUTS:  M = (nx3)-matrix of locations and data values,
%              M(i) = (Xi,Yi,Zi), i = 1:n
%         varargin = optional bandwidth specification (if not 
%                    specified, the default value is Dmax/2).
%
%SCREEN OUTPUT: Range, Sill, Nugget, plus Number of Iterations in fitting.

%PLOT OUTPUT: Plot of Variogram plus Spherical Fit

%OUTPUTS: OUT = cell(6,1) with
%         (i)   OUT{1}= [r,s,a] (= estimated [range,sill,nugget])
%         (ii)  OUT{2}= distance values, (Di, i=1:b) 
%         (iii) OUT{3}= empirical variogram values Gi at Di
%         (iv)  OUT{4}= spherical values Vi at Di
%         (v)   OUT{5}= number of iterations in FMINS
%         (vi)  OUT{6}= Bandwidth used in variogram estimates


if nargin > 1
   
   d = varargin{:} ;
      
	[DAT,d] = variogram(M,d) ;

else
   
   [DAT,d] = variogram(M) ;

end

D = DAT(:,1) ;

G = DAT(:,2) ;

y = zeros(3,1) ;

y(1) = max(D) ;      %initial estimate of range

y(2) = max(G) ;        %initial estimate of sill

y(3) = max([0,min(G)]) ; %initial estimate of nugget

DAT = var_fit(y,D,G) ;

p = DAT{1};

iter = DAT{2} ;


OUT = cell(6,1) ;

OUT{1} = p ;

OUT{2} = D ;

OUT{3} = G ;

OUT{4} = spher(p,D) ;

OUT{5} = iter ;

OUT{6} = d;

var_plot(OUT);


%Make Screen Output


%SCREEN OUTPUT

info.fmt = '%12.3f'; %print format

   
   info.rnames = strvcat(' ','RANGE','SILL','NUGGET','ITERATIONS');
  
   mat(:,1) = [p;iter];
   
disp(' ');
disp('SPHERICAL VARIOGRAM:');
mprint_new(mat,info) %Display Regression Results
disp('MAX ITERATIONS = 600'); 

VARIOGRAM_BANDWIDTH = OUT{6}


