C
C LAST UPDATE:
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C*****************************
C**     LFN_INT_STATS1      **
C*****************************
C
CD-Routine:  Basic Integration Statistics - LFN_INT_STATS1
C This routine is used to collect some basic statistics about the integrated
C intensties from either the current Laue Reflection List (LRL) or for a
C requested plate stored in a Laue integrated spots list. 
CD-end
CD-Fortran:
      SUBROUTINE LFN_INT_STATS1 (LINDX, IPACK, IPLATE, NS, NG, NO, NN,  
     +                           NB, AIM, AIS, N3, N10, IERR)
CD-end
      INCLUDE 'implicit_none.finc'
C
C PURPOSE: Write out individual spot data calculated by LFN_SAINT
C 
C AUTHOR: J.W. Campbell, January 1996 
C
C ARGUMENTS:
C
      INTEGER LINDX
      INTEGER IPACK
      INTEGER IPLATE
      INTEGER NS(5)
      INTEGER NG(5)
      INTEGER NO(5)
      INTEGER NN(5)
      INTEGER NB(5)
      REAL AIM(5)
      REAL AIS(5)
      INTEGER N3(5)
      INTEGER N10(5)
      INTEGER IERR
      
CD-Parameters:
C   LINDX     i (R)  Index for LIRL (>=0) or -1 for collect data from LRL
C   IPACK     i (R)  Pack number (ignored if LRL used)
C   IPLATE    i (R)  Plate number (ignored if LRL used)
C   NS(5)     i (W)  No. of spots present for the 5 spot categories
C   NG(5)     i (W)  No. of spots integrated & good  for the 5 spot categories
C   NO(5)     i (W)  No. of overload spots flagged for the 5 spot categories
C   NN(5)     i (W)  No. of OK negative intensities for the 5 spot categories
C   NB(5)     i (W)  No. of bad spots spots flagged for the 5 spot categories
C   AIM(5)    r (W)  Mean I value for the 5 spot categories (for OK positive
C                    intensities only)
C   AIS(5)    r (W)  Mean I/sig(I) values for the 5 spot categories (for
C                    OK positive intensities only)
C   N3(5)     i (W)  No. OK spots with I>3*sig(I) for the 5 spot categories
C   N10(5)    i (W)  No. OK spots wit I>10*sig(I) for the 5 spot categories
C   IERR      i (W)  Error flag =0 OK
C                               =1 No LRL or plate not found or matched
C
C    Notes: 
C
C        The five spot categories: 1) Total numbers of spots
C                                  2) Spatially separated singles
C                                  3) Spatially separated multiples
C                                  4) Spatially overlapped singles
C                                  5) Spatially overlapped multiples
C
C        AIM, AIS, N3 and N10 values include overloads but not bad spots
C
C        Good = measured (integrated) and not bad/overload
C        OK   = measured (integrated) and not bad (i.e. may include overloads)
CD-end
C
C====== Parameter statements
C 
      INCLUDE 'saint_limits.finc'
C
C====== GLOBALS:
C
      INCLUDE 'lfn_saint.finc'
C
C====== FUNCTION TYPES:
C
      INTEGER LENSTR
C
C====== LOCALS:
C
      LOGICAL USE_LRL
      LOGICAL GOOD
      LOGICAL OKNOD
      LOGICAL SPOV
      LOGICAL MEAS
      LOGICAL OVLD
      LOGICAL BADSP
      INTEGER I
      INTEGER NMEAN(5)
      INTEGER ISPOT
      INTEGER NUMSPOTS
      INTEGER JERR
      INTEGER IH
      INTEGER IK
      INTEGER IL
      INTEGER MULT
      INTEGER KSYM
      INTEGER IPK
      INTEGER IPL
      INTEGER MINH
      INTEGER MAXH
      INTEGER INCH
      INTEGER KCODE
      INTEGER ICOD_TYP
      INTEGER ISPAT
      INTEGER ICLOS
      INTEGER IMEAS
      INTEGER IBAD
      INTEGER IOVLD
      REAL XFD
      REAL YFD
      REAL ALAM
      REAL DTHR2
      REAL AINTENS
      REAL SIGAI
      
C     LOGICAL USE_LRL               !Use LRL (rather than LIRL) flag
C     LOGICAL GOOD                  !Good spot flag
C     LOGICAL OKNOD                 !OK nodal flag
C     LOGCAL SPOV                   !Spatial overlapped flag
C     LOGICAL MEAS                  !Spot measured (integrated) flag
C     LOGICAL OVLD                  !Overload flag
C     LOGICAL BADSP                 !Bad spot flag
C     INTEGER I                     !Temp/loop variable
C     INTEGER NMEAN(5)              !Internal counts for means calculations
C     INTEGER ISPOT                 !Spot no. in list
C     INTEGER NUMSPOTS              !No. of spots in list
C     INTEGER JERR                  !Error flag
C     INTEGER IH                    !'h' index
C     INTEGER IK                    !'k' index
C     INTEGER IL                    !'k' index
C     INTEGER MULT                  !Multiplicity
C     INTEGER KSYM                  !Symetry flag
C     INTEGER IPK                   !Pack no.
C     INTEGER IPL                   !Plate no.
C     INTEGER MINH                  !Minimum harmonic
C     INTEGER MAXH                  !Maximum harmonic
C     INTEGER INCH                  !Harmonic increment
C     INTEGER KCODE                 !Integration code
C     INTEGER ICOD_TYP              !Integration code type flag
C     INTEGER ISPAT                 !Spatial overlap flag
C     INTEGER ICLOS                 !Spatially close flag
C     INTEGER IMEAS                 !Spot measured (integrated) flag
C     INTEGER IBAD                  !Bad spot flag
C     INTEGER IOVLD                 !Overload flag
C     REAL XFD                      !Spot 'x' coordinate
C     REAL YFD                      !Spot 'y' coordinate
C     REAL ALAM                     !Wavelength
C     REAL DTHR2                    !dmin**2 threshold
C     REAL AINTENS                  !Integrated intensity I
C     REAL SIGAI                    !sig(I)
C
C------------------------------------------------------------------------------- 
C
C====== Initialisations and checks
C
      IERR = 0
      DO 10 I = 1, 5
         NS(I) = 0
         NG(I) = 0
         NO(I) = 0
         NN(I) = 0
         NB(I) = 0
         AIM(I) = 0.0
         AIS(I) = 0.0
         N3(I) = 0
         N10(I) = 0
         NMEAN(I) = 0
 10   CONTINUE
      USE_LRL = .FALSE.
      IF (LINDX.LT.0) USE_LRL = .TRUE.
      IF (USE_LRL) THEN
         CALL LRL_NUMSPOTS (NUMSPOTS)
         IF (NUMSPOTS.LE.0) THEN
            IERR = 1
            RETURN
         END IF
      ELSE
         CALL LIRLF_NUMREFS (LINDX, NUMSPOTS)
         IF (NUMSPOTS.LE.0) THEN
            IERR = 1
            RETURN
         END IF
      END IF
C
C====== Gather statistics
C
      DO 100 ISPOT = 1, NUMSPOTS
         IF (USE_LRL) THEN
            CALL LRL_GET (ISPOT, IH, IK, IL, XFD, YFD, ALAM, DTHR2,
     +                    MULT, OKNOD, SPOV, JERR)
            CALL LRL_GET_INT (ISPOT, AINTENS, SIGAI, MEAS, BADSP, OVLD,
     +                        KCODE, ICOD_TYP, JERR)
         ELSE
            CALL LIRLF_GETREF (LINDX, ISPOT, IH, IK, IL, IPK, IPL, 
     +                        KSYM, XFD, YFD, ALAM,
     +                        MULT, MINH, MAXH, INCH, AINTENS, SIGAI,
     +                        ISPAT, ICLOS, IMEAS, IBAD, IOVLD, 
     +                        KCODE, JERR)
            IF (IPACK.NE.IPK.OR.IPLATE.NE.IPL) GO TO 100
            MEAS = .FALSE.
            IF (IMEAS.EQ.1) MEAS = .TRUE.
            SPOV = .FALSE.
            IF (ISPAT.EQ.1) SPOV = .TRUE.
            OVLD = .FALSE.
            IF (IOVLD.EQ.1) OVLD = .TRUE.
            BADSP = .FALSE.
            IF (IBAD.EQ.1) BADSP = .TRUE.
         END IF
         GOOD = MEAS
         IF (OVLD.OR.BADSP) GOOD = .FALSE.
         NS(1) = NS(1) + 1
         IF (GOOD) NG(1) = NG(1) + 1
         IF (MEAS.AND.OVLD) NO(1) = NO(1) + 1
         IF (MEAS.AND.BADSP) NB(1) = NB(1) + 1
         IF (MEAS.AND..NOT.BADSP) THEN
            IF (AINTENS.GT.0.0.AND.SIGAI.GT.0.0) THEN
               AIM(1) = AIM(1) + AINTENS
               AIS(1) = AIS(1) + AINTENS/SIGAI
               NMEAN(1) = NMEAN(1) + 1
            END IF
         END IF
         IF (MEAS.AND..NOT.BADSP) THEN
            IF (AINTENS.GT.3.0*SIGAI) N3(1) = N3(1) + 1
            IF (AINTENS.GT.10.0*SIGAI) N10(1) = N10(1) + 1
            IF (AINTENS.LT.0.0) NN(1) = NN(1) + 1
         END IF
         IF (MULT.EQ.1) THEN
            IF (SPOV) THEN
               NS(4) = NS(4) + 1
               IF (GOOD) NG(4) = NG(4) + 1
               IF (MEAS.AND.OVLD) NO(4) = NO(4) + 1
               IF (MEAS.AND.BADSP) NB(4) = NB(4) + 1
               IF (MEAS.AND..NOT.BADSP) THEN
                  IF (AINTENS.GT.0.0.AND.SIGAI.GT.0.0) THEN
                     AIM(4) = AIM(4) + AINTENS
                     AIS(4) = AIS(4) + AINTENS/SIGAI
                     NMEAN(4) = NMEAN(4) + 1
                  END IF
               END IF
               IF (MEAS.AND..NOT.BADSP) THEN
                  IF (AINTENS.GT.3.0*SIGAI) N3(4) = N3(4) + 1
                  IF (AINTENS.GT.10.0*SIGAI) N10(4) = N10(4) + 1
                  IF (AINTENS.LT.0.0) NN(4) = NN(4) + 1
               END IF
            ELSE
               NS(2) = NS(2) + 1
               IF (GOOD) NG(2) = NG(2) + 1
               IF (MEAS.AND.OVLD) NO(2) = NO(2) + 1
               IF (MEAS.AND.BADSP) NB(2) = NB(2) + 1
               IF (MEAS.AND..NOT.BADSP) THEN
                  IF (AINTENS.GT.0.0.AND.SIGAI.GT.0.0) THEN
                     AIM(2) = AIM(2) + AINTENS
                     AIS(2) = AIS(2) + AINTENS/SIGAI
                     NMEAN(2) = NMEAN(2) + 1
                  END IF
               END IF
               IF (MEAS.AND..NOT.BADSP) THEN
                  IF (AINTENS.GT.3.0*SIGAI) N3(2) = N3(2) + 1
                  IF (AINTENS.GT.10.0*SIGAI) N10(2) = N10(2) + 1
                  IF (AINTENS.LT.0.0) NN(2) = NN(2) + 1
               END IF
            END IF
         ELSE
            IF (SPOV) THEN
               NS(5) = NS(5) + 1
               IF (GOOD) NG(5) = NG(5) + 1
               IF (MEAS.AND.OVLD) NO(5) = NO(5) + 1
               IF (MEAS.AND.BADSP) NB(5) = NB(5) + 1
               IF (MEAS.AND..NOT.BADSP) THEN
                  IF (AINTENS.GT.0.0.AND.SIGAI.GT.0.0) THEN
                     AIM(5) = AIM(5) + AINTENS
                     AIS(5) = AIS(5) + AINTENS/SIGAI
                     NMEAN(5) = NMEAN(5) + 1
                  END IF
               END IF
               IF (MEAS.AND..NOT.BADSP) THEN
                  IF (AINTENS.GT.3.0*SIGAI) N3(5) = N3(5) + 1
                  IF (AINTENS.GT.10.0*SIGAI) N10(5) = N10(5) + 1
                  IF (AINTENS.LT.0.0) NN(5) = NN(5) + 1
               END IF
            ELSE
               NS(3) = NS(3) + 1
               IF (GOOD) NG(3) = NG(3) + 1
               IF (MEAS.AND.OVLD) NO(3) = NO(3) + 1
               IF (MEAS.AND.BADSP) NB(3) = NB(3) + 1
               IF (MEAS.AND..NOT.BADSP) THEN
                  IF (AINTENS.GT.0.0.AND.SIGAI.GT.0.0) THEN
                     AIM(3) = AIM(3) + AINTENS
                     AIS(3) = AIS(3) + AINTENS/SIGAI
                     NMEAN(3) = NMEAN(3) + 1
                  END IF
               END IF
               IF (MEAS.AND..NOT.BADSP) THEN
                  IF (AINTENS.GT.3.0*SIGAI) N3(3) = N3(3) + 1
                  IF (AINTENS.GT.10.0*SIGAI) N10(3) = N10(3) + 1
                  IF (AINTENS.LT.0.0) NN(3) = NN(3) + 1
               END IF
            END IF
         END IF
 100  CONTINUE

      DO 120 I = 1, 5
         IF (NMEAN(I).EQ.0) GO TO 120
         AIM(I) = AIM(I)/NMEAN(I)
         AIS(I) = AIS(I)/NMEAN(I)
 120  CONTINUE
      RETURN
      END
