C******************************************************************************
C                    F U N C T I O N     W I N D 
C******************************************************************************
C PURPOSE:
C   To compute the Winding number about zero of a complex valued function F
C   This works only for the scalar case
C
C INPUTS:
C   Integer      NSP   number of gridpoints
C   Integer      MXSP  maximum number of sample points allowed
C   Integer      MXDIM maximum numbe of dimension
C   Integer      NOUT  unit number for screen output
C   Dble Cmplx   AK    array holding gridpoint values of function F
C
C OUTPUTS:
C   Dble Prec    Wind  Winding number of F about zero
C
C ALGORITHM:
C   A cumulative total of argument change between successive gridpoints is kept
C   and divided by 2 PI to get the Winding number.
C******************************************************************************

      DOUBLE PRECISION FUNCTION  Wind (NSp, Ak, MxSp, MxDim, nOut)

C  Subroutine arguments:
      INTEGER           NSp, MxSp, nOut, MxDim
      DOUBLE COMPLEX    Ak(mxsp,mxdim)
C  Local variables
      DOUBLE COMPLEX    Z1, Z2, Z3
      DOUBLE PRECISION  TwoPi, Angle, Arg
      INTEGER           MinWind

C  Initializations:
      TwoPi   = 8.0d0 * DATAN(1.0d0)
      MinWind = 1.d-6



C     TOP OF LOOP
C     ===========
      Angle = 0.0d0
      DO 10 i = 1,NSp

C     ...Define Z1, Z2 
         IF (i .NE. NSp)  THEN
            k = i + 1
         ELSE
            k = 1
         ENDIF
         Z1 = Ak(i,1)
         Z2 = Ak(k,1)

C     ...If we go thru the origin, Winding number is undefined:
         IF ((CDABS(Z1) .LT. MinWind).or.
     &       (CDABS(Z2) .LT. MinWind) ) THEN
             Wind = 99.0d0
             WRITE(*,nout)  'Winding number undefined (thru origin)'
             RETURN
         ENDIF

C     ...We need unit vectors:
c         IF ((CDABS(Z1) .EQ. (0.0d0,0.0d0))   .OR. 
c     &       (CDABS(Z2) .EQ. (0.0d0,0.0d0)))
c     &      STOP 'Division by 0 in Subr. Wind.  Z1 or Z2 = 0'
         Z1 = Z1 /CDABS(Z1)
         Z2 = Z2 /CDABS(Z2)

C     ...Ratio, and update total:
         Z3 = Z1 /Z2
         Arg = DATAN2 (DIMAG(Z3),DREAL(Z3))
         Angle = Angle - Arg
10    CONTINUE
C     ========

C  ...Divide to get winding number
      Wind = Angle /TwoPi


      RETURN
      END




C@@@@@this was not tested after it was moved in the code
C*****************************************************************************
C         S U B R O U T I N E    W I N D V
C*****************************************************************************
C  PURPOSE:  
C  To compute the winding number in the vector case
C
C  INPUTS:
C    Integer     NDIM     Number of dimensions
C    Integer     NSP      Number of sample points
C    Integer     MXDIM    Maximum number of dimensions
C    Integer     MXSP     Maximum number of sample points
C    Integer     NOUT     Unit number for screen read
C    Dble Cmplx  AK       Holds grid values of partial der. of obj. function
C    Dble Cmplx  CSTORE   Storage variable only.  Value neither returned nor
C                         stored.   Dimension=mxsp
C  OUTPUTS:
C    Dble Prec   WnoV     Winding number
C****************************************************************************

	SUBROUTINE  WindV (NDim,NSp,MxDim,MxSp,Ak,WnoV,nOut,CStore)

C  Subroutine Arguments:
      INTEGER           MxDim, MxSp, NSp, NDim, nOut
      DOUBLE COMPLEX    Ak(mxsp,mxdim), CStore(mxsp)
      DOUBLE PRECISION  WnoV
C  Local Variables:
      INTEGER           Wno
      DOUBLE PRECISION  Sp
C  Functions:
      DOUBLE PRECISION  Wind

C  Initializations:
        Sp = DBLE(NSp)
 


      DO 100 j = 1,NDim
         Wno = NINT (Wind(NSp, Ak, MxSp, MxDim, nOut))
         IF (Wno .EQ. 1)  GO TO 110
100   CONTINUE

C@@@@@is this wnov sed to -1 as an arbitrary number?
c      WRITE(nout,*) 'No winding number is equal to 1'
      WnoV = -1.0d0
      RETURN


C@@@@@@i changed the loop parameters for 130 instead of having and
C@@@@@@if statements.  however, i think that it should be nsp/2
C@@@@@@also, it looks line wnov=0 should go inside 150 loop
110   WnoV = 0.0d0
      DO 150 i = 1,NDim
         DO 120 k = 1,NSp
            CStore(k) = Ak(k,i) /Ak(k,j)
120      CONTINUE        	
         CALL FFt(NSp,CStore)
         DO 130 k = 2,NSp/2-1
            WnoV = WnoV + CDABS(CStore(k) /Sp) 
130      CONTINUE
150   CONTINUE

      RETURN 

       
      END
