function OUT = var_sim_test(L,Z,d)

%VAR_SIM estimates a spherical variogram for a collection of k data sets
%using VAR_SPHER.

%Written by: TONY E. SMITH, 2/21/98

%Functions called: variogram.m, var_fit.m, spher.m

%INPUTS:  L = (nx2)-matrix of locations (Xi,Yi), i = 1:n,
%         Z = (nxk)-matrix of k data sets (Zi) for locations in L.
%         d = bandwidth for estimation .
%
%OUT = (kx4)-matrix [ri,si,ai,vi] with (ri,si,ai) denoting the 
%      variogram parameter estimates, and vi the sample variance 
%      estimate for data set i.

[n,k] = size(Z);

OUT = zeros(k,3);

M(:,1:2) = L;

index = 10;

for i = 1:k
   
   M(:,3) = Z(:,i);
           
   DAT = var_spher_test(M,d);
   
   OUT(i,:) = DAT{1};
   
   if OUT(i,2) <= OUT(i,3)
      
      Bad_Iteration = i
      
      %error('Sill not bigger than nugget');
      
   end
   
   
   %screen counter
   
   if 100*(i/k) >= index
      
      percent_done = index
      
      index = index + 10;
      
   end %end simulation loop 
      
end

%compute sample variance (with normalizer = n-1)
   
v = (std(Z).^2)';
   
OUT = [OUT,v];

