C
C LAST UPDATE:
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C*******************************
C**     LFN_SPOTS_ANALYSE     **
C*******************************
C
CD-Routine: Analyse the Spots List - LFN_SPOTS_ANALYSE
C This routine is used to analyse the list of spots determined by the
C routine LFN_FINDSPOTS. The analysis is performed for a series of
C angular bins around the image centre.
CD-end
CD-Fortran:
      SUBROUTINE LFN_SPOTS_ANALYSE (MINDX_SPOT, IOPT, CUT, N_BIN,
     +                              BIN_START, BIN_END, SUM, NUMBIN,  
     +                              VARIANCE, STDDEVN, AVBIN,
     +                              N_REJECT, SUM_GOOD, N_GOOD, 
     +                              SEL_SIZE, IERR, ERRSTR) 
CD-end
C
C PURPOSE: Analyse spot shapes from spots list preoared by LFN_FINDSPOTS
C
C AUTHOR: D. Nguti
C
C ARGUMENTS:
C
      INTEGER MINDX_SPOT
      REAL CUT
      INTEGER N_BIN
      REAL BIN_START(*)
      REAL BIN_END(*)
      REAL SUM(*) 
      INTEGER NUMBIN(*)
      REAL VARIANCE(*) 
      REAL STDDEVN(*) 
      REAL AVBIN(*)
      INTEGER N_REJECT(*)
      REAL SUM_GOOD(*)
      INTEGER N_GOOD(*)
      REAL SEL_SIZE
      INTEGER IERR
      CHARACTER*(*) ERRSTR
CD-Parameters:
C   MINDX_SPOT    i (R)  Index to in memory spots list from LFN_FINDSPOTS
C   IOPT          i (R)  Flag = 1 analyse spot length, = 2 analyse spot 
C                        width.
C   CUT           r (R)  No. of standard deviations from mean spot 
C                        dimension for which spots are eliminated as too 
C                        small or too big. Suggested value 3.5.
C   N_BIN         i (R)  Divide into N_BIN angular bins for the analysis.
C   BIN_START(*)  r (W)  Start angle for each bin (must be dimensioned to
C                        at least N_BIN).
C   BIN_END(*)    r (W)  End angle for each bin (must be dimensioned to
C                        at least N_BIN).
C   SUM(*)        r (W)  Sum for each bin of spot length/width (must be
C                        dimensioned to at least N_BIN).
C   NUMBIN(*)     i (W)  Number of spots in each bin (must be dimensioned 
C                        to at least N_BIN).
C   VARIANCE(*)   r (W)  Variance of spot length/width for each bin (must 
C                        be dimensioned to at least N_BIN).
C   STDDEVN(*)    r (W)  Standard deviation of spot length/width for each
C                        bin (must be dimensioned to at least N_BIN).
C   AVBIN (*)     r (W)  Average of spot length/width for each
C                        bin (must be dimensioned to at least N_BIN).
C   N_REJECT(*)   i (W)  No. of spots rejected for each bin (must be
C                        dimensioned to at least N_BIN).
C   SUM_GOOD(*)   r (W)  Sum of spot lengths/widths for each bin after
C                        rejection on standard deviation cutoff (must be
C                        dimensioned to at least N_BIN).
C   N_GOOD(*)     i (W)  No. of spots for each bin after rejection on
C                        standard deviation cutoff (must be dimensioned to
C                        at least N_BIN).
C   SEL_SIZE      r (W)  Selected spot length/width.
C   IERR          i (W)  Error return flag = 0 OK
C                                          = 1 spots list not found
C   ERRSTR        c (W)  Error message returned if IERR is non-zero (should
C                        be dimensioned to 80 characters).
C
C
CD-end
C
C====== Parameter statements
C
 
C
C====== GLOBALS:
C
 
C
C====== FUNCTION TYPES:
C
 
C
C====== LOCALS:
C
 
	INTEGER IBINI
	INTEGER IBINP1
	INTEGER I
	INTEGER J
	INTEGER N
        INTEGER NPIX
        INTEGER IBUF(7)
        REAL SX
        REAL SY
        REAL SPOT_LENGTH
        REAL SPOT_WIDTH
        REAL SPOT_ANGLE
        REAL SUMINT
	REAL AZIMUTH
	REAL THRESH
	REAL SLOW
        REAL SIZE
 
      EQUIVALENCE (SX,IBUF(1)), (SY,IBUF(2)), (SUMINT,IBUF(3))
      EQUIVALENCE (NPIX,IBUF(4)), (SPOT_LENGTH,IBUF(5))
      EQUIVALENCE (SPOT_WIDTH,IBUF(6)), (SPOT_ANGLE,IBUF(7))
C
C-------------------------------------------------------------------------------
C
      IERR = 0
      ERRSTR = ' '
      SEL_SIZE = 0.0
      DO 20 I = 1 , N_BIN
         BIN_START(I) = 0.0
         BIN_END(I) = 0.0
         SUM_GOOD(I) = 0.0
         N_GOOD(I) = 0
         VARIANCE(I) = 0.0
         STDDEVN(I) = 0.00
         N_REJECT(I) = 0
         SUM(I) = 0.00
         NUMBIN(I) = 0
 20   CONTINUE
      CALL DMLF_NUMRECS(MINDX_SPOT,N)
      IF (N.LT.0) THEN
         IERR = 1
         ERRSTR = 'Invalid list index given'
      END IF
      IF (N.EQ.0) THEN
         RETURN
      END IF
C
C====== Sum spot sizes
C
      DO 40 I = 1 , N
         CALL DMLF_GETREC(MINDX_SPOT,I,IBUF,IERR)
         IF(IOPT.EQ.1) THEN
            SIZE = SPOT_LENGTH
         ELSE
            SIZE = SPOT_WIDTH
         ENDIF
         AZIMUTH = SPOT_ANGLE
         IF (AZIMUTH.LT.0.0) AZIMUTH = AZIMUTH + 360.0
         DO 30 J = 1 , N_BIN
            IBINI = (J-1)*360.0/REAL(N_BIN)
            IBINP1 = IBINI + 360.0/REAL(N_BIN)
            IF (AZIMUTH.LT.IBINP1 .AND. AZIMUTH.GE.IBINI) THEN
               SUM(J) = SUM(J) + SIZE
               NUMBIN(J) = NUMBIN(J) + 1
               VARIANCE(J) = 0.0
            END IF
 30      CONTINUE
 40   CONTINUE
C
C====== Calculate variances
C
      DO 60 I = 1 , N
         CALL DMLF_GETREC(MINDX_SPOT,I,IBUF,IERR)
         IF(IOPT.EQ.1) THEN
            SIZE = SPOT_LENGTH
         ELSE
            SIZE = SPOT_WIDTH
         ENDIF
         AZIMUTH = SPOT_ANGLE
         IF (AZIMUTH.LT.0.0) AZIMUTH = AZIMUTH + 360.0
         DO 50 J = 1 , N_BIN
            AVBIN(J) = SUM(J)/NUMBIN(J)
            IBINI = (J-1)*360.0/REAL(N_BIN)
            IBINP1 = IBINI + 360.0/REAL(N_BIN)
            IF (AZIMUTH.LT.IBINP1 .AND. AZIMUTH.GE.IBINI) THEN
               VARIANCE(J) = (AVBIN(J) - SIZE)**2.0 + VARIANCE(J)
            END IF
 50      CONTINUE
 60   CONTINUE
 
 
C
C====== Calculate standard deviations and rejections
C
      DO 80 I = 1 , N
         CALL DMLF_GETREC(MINDX_SPOT,I,IBUF,IERR)
         IF(IOPT.EQ.1) THEN
            SIZE = SPOT_LENGTH
         ELSE
            SIZE = SPOT_WIDTH
         ENDIF
         AZIMUTH = SPOT_ANGLE
         IF (AZIMUTH.LT.0.0) AZIMUTH = AZIMUTH + 360.0
         DO 70 J = 1 , N_BIN
            STDDEVN(J) = SQRT(VARIANCE(J)/(NUMBIN(J)-1.0))
            IBINI = (J-1)*360.0/REAL(N_BIN)
            IBINP1 = IBINI + 360.0/REAL(N_BIN)
            IF (AZIMUTH.LT.IBINP1 .AND. AZIMUTH.GE.IBINI) THEN
               THRESH = NINT(CUT*STDDEVN(J)+0.5)
               SLOW = NINT(STDDEVN(J)/CUT+0.5)
               IF (SIZE.GT.THRESH .OR. SIZE.LE.SLOW) THEN
                  N_REJECT(J) = N_REJECT(J) + 1
               ELSE
                  N_GOOD(J) = N_GOOD(J) + 1
                  SUM_GOOD(J) = SUM_GOOD(J) + SIZE
               END IF
               GO TO 80
            END IF
 70      CONTINUE
 80   CONTINUE
C
C data bins and average spot size
C
      DO 90 I = 1, N_BIN
         BIN_START(I) = (I-1) * 360.0 / N_BIN
         BIN_END(I) = I * 360.0 / N_BIN
	 SEL_SIZE = SUM_GOOD(I)/N_GOOD(I) + SEL_SIZE
 90   CONTINUE
      SEL_SIZE = SEL_SIZE / N_BIN
      RETURN
      END
