function DAT = st_env_plot(LT,sims,s)

%ST_ENV computes space-time cdf for a given space-time pattern,
%and in addition, computes the outer envelopes of the cdf's for 
%a set of random time relabelings of that pattern. The residuals
%of the space-time cdf above the cdf envelope are then plotted.

%Written by: TONY E. SMITH, 2/9/98

%Functions called: space_time.m
%
% INPUTS:
%     (i)   LT  = location-time file [loc(i)=(Xi,Yi,Ti),i=1..n]
%     (ii)  sims = number of relabelings
%     (iii) s = number of point values to use in space-time range.

% OUTPUTS: (1x4)-cell array, DAT
%     (i)   DAT{1} = (s x s) CDF for original pattern.
%     (ii)  DAT{2} = (s x s) Upper Envelopes of simulated CDF's.
%     (iii) DAT{3} = (s x s) Lower Envelopes of simulated CDF's.
%     (iv)  DAT{4} = (s x 1) array of distance values.
%     (v)   DAT{5} = (s x 1) array of time values.
%           [Here the max pairwise distance, Dmax, and time interval,
%            Tmax,are computed and (d,t) = (Dmax/2,Tmax/2)then 
%            defines the relevant ranges.]

[m,n] = size(LT) ;

%Compute pairwise distances and Dmax

DIST = zeros(m*(m-1)/2, 3) ; %Matrix of loc-dist pairs

i = 1 ;

k = 1 ;  %counter for DIST.

while i < m
   
   j = i + 1 ;
   
   while j <= m
      
      DIST(k,1) = norm(LT(i,1:2) - LT(j,1:2));

      DIST(k,2) = i ;
      
      DIST(k,3) = j ;  
                
      j = j + 1 ;
      
      k = k + 1 ;
      
   end
   
   i = i + 1 ;
   
end

Dmax = max(DIST(:,1)) ;

Tmax = max(LT(:,3)) - min(LT(:,3)) ;

%Compute CDF for LT

CDF = space_time(LT,s,DIST) ;

%Compute Upper and Lower Envelopes

U = zeros(s) ; %Upper envelope

L = ones(s) ;  %Lower envelope

i = 1 ;

index = 10;

while i <= sims
   
   %Form randomly relabeled sample
   
   sample = randperm(m) ;   
        
   LT(:,3) = LT(sample,3) ;
   
   %Construct Envelopes
   
   X = space_time(LT,s,DIST) ;
        
   U = max(U,X) ;
   
   L = min(L,X) ;
   
      
   percent = 100*(i/sims);
     
     if percent >= index
        
        disp(['percent_completed = ',num2str(index)]);
        
        index = index + 10;
        
     end
     
     i = i + 1 ;
   
end

%Compute space-time mesh

d = Dmax/2 ;

inc = d/(s - 1) ;

D = 0:inc:d ;

t = Tmax/2 ;

inc = t/(s - 1) ;

T = 0:inc:t ;

%Assign output

DAT = cell(1,5) ;

DAT{1} = CDF ;

DAT{2} = U ;

DAT{3} = L ;

DAT{4} = D ;

DAT{5} = T ;


Z = zeros(size(CDF));

pos = max(Z, CDF - U)  ; 

surf(D,T,pos) ; 

%view([-80 30])

view([110 70])

%colormap cool 


%Type 'help graph3d' and look under 'Color maps" for further options

%use colormap([1 1 1]) to remove all colors

colormap white

