pro correlation, data1, data2, tw, probtw, d, probd, w1,    $
                    pnt=pnt, ztw=ztw, zd=zd


   n = n_elements(data1)

   if (n_params() lt 7) then w1 = fltarr(n)*0. + 1.

   wksp1 = data1
   wksp2 = data2

; Replace the values in the arrays by their ranks (and sort so that
; the second array is ordered from lowest to highest).

   sort2, wksp1, wksp2
   crank, wksp1, sf
   sort2, wksp2, wksp1
   crank, wksp2, sg

; Compute the "D-statistic" of the sum squared differences of the ranks
; and determine its significance (probability of coming from coming from
; uncorrelated data).

   d = total((wksp1 - wksp2)^2)

   en = n
   en3n = en^3 - en
   aved = en3n/6. - (sf + sg)/12.
   fac = (1. - sf/en3n)*(1. - sg/en3n)
   vard = ((en - 1.)*en^2*(en + 1.)^2/36.)*fac
   zd = (d - aved)/sqrt(vard)
   probd = 2.*gaussint(-abs(zd))

; Compute the "t-statistic" and determine its significance.

   t = fltarr(n)
   sum = 0.
   wksub1 = wksp1
  
   for i = 0, n-2 do begin

      ri = wksub1(0)
      ni = n_elements(wksub1)
      ei = (ni + 1)/2.
      vi = (ni*ni - 1)/12.

      t(i) = (ri - ei)/sqrt(vi)
      sum = sum + w1(i)*t(i)

      wksub1 = wksub1(1:ni-1)
      ss = where(wksub1 gt ri, count)
      if (count gt 0) then wksub1(ss) = wksub1(ss) - 1
   end

   tw = sum / sqrt(total(w1^2))
   ztw = tw
   probtw = 2.*gaussint(-abs(tw))

; print results to screen

  if keyword_set(pnt) then begin
     print, 'Probability of tw = ', probtw
     print, 'Probability of D = ', probd
  endif
  
   return
   end