C
C LAST UPDATE:
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C*****************************
C**     LFN_LIST_INTSPOT    **
C*****************************
C
CD-Routine: List Integrated Spot Details - LFN_LIST_INTSPOT
C This routine is used to write spot data from an integration pass of
C the routine LFN_SAINT for the last individually integrated spot (for 
C diagnostic purposes). The pixel values plot and the spot profile plot
C are each scaled to a maximum of 99.0. The background subtracted pixel
C values plot is scaled by the same factor as for a pixel values plot.
CD-end
CD-Fortran:
      SUBROUTINE LFN_LIST_INTSPOT (IUN, ITERM, OP_BGPLAN, OP_PIXV,
     +                             OP_PIXSB, OP_PROF, IERR, ERRSTR)
CD-end
      INCLUDE 'implicit_none.finc'
C
C PURPOSE: Write out individual spot data calculated by LFN_SAINT
C 
C AUTHOR: J.W. Campbell 
C
C ARGUMENTS:
C
      INTEGER IUN
      INTEGER ITERM
      LOGICAL OP_BGPLAN
      LOGICAL OP_PIXV
      LOGICAL OP_PIXSB
      LOGICAL OP_PROF
      INTEGER IERR
      CHARACTER*(*) ERRSTR
CD-Parameters:
C   IUN       i (R)  Unit number for listing (0=no printing)
C   ITERM     i (R)  =1 output to a terminal, =0 output to a file
C   OP_BGPLAN l (R)  Output background plane parameters if .TRUE.
C   OP_PIXV   l (R)  Output scaled pixel values plot if .TRUE.
C   OP_PIXSB  l (R)  Output scaled background subtracted pixels plot 
C                    if .TRUE.
C   OP_PROF   l (R)  Output scaled spot profile plot if .TRUE.
C   IERR      i (W)  Error flag =0 OK
C                               =1 No such spot data currently stored
C   ERRSTR    c (W)  Error message string (max 80 chars) set if IERR is 
C                    non-zero
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
      INTEGER I
      INTEGER J
      INTEGER IX
      INTEGER IY
      INTEGER IXMIN
      INTEGER IXMAX
      INTEGER IYMIN
      INTEGER IYMAX
      INTEGER IS
      INTEGER IPIX
      INTEGER NCH
      REAL PIXMAX
      REAL PRFMAX
      REAL SCPIX
      REAL SCPRF
      REAL SBVAL
      REAL SBMIN
      CHARACTER*(4*MAX_PRLIN) LIN
      CHARACTER*10 STR
      CHARACTER*4 FMT
      CHARACTER*5 NUL
      CHARACTER*5 STAR
      CHARACTER*5 OVSP
      CHARACTER*5 OVLD
      CHARACTER*5 REJ
      
C     INTEGER I                     !Temp/loop variable
C     INTEGER J                     !Temp/loop variable
C     INTEGER IX                    !'x' pixel offset
C     INTEGER IY                    !'y' pixel offset
C     INTEGER IXMIN                 !Minimum 'x' pixel offset
C     INTEGER IXMAX                 !Maximum 'x' pixel offset
C     INTEGER IYMIN                 !Minimum 'y' pixel offset
C     INTEGER IYMAX                 !Maximum 'y' pixel offset
C     INTEGER IS                    !O/p line character position
C     INTEGER IPIX                  !Scaled pixel value
C     INTEGER NCH                   !No of characters / pixel field
C     REAL PIXMAX                   !Maximum spot pixel value
C     REAL PRFMAX                   !Maximum spot profile value
C     REAL SCPIX                    !Scale factor for pixels & bg. sub. pixels
C     REAL SCPRF                    !Scale factor for spot profile
C     REAL SBVAL                    !Scaled background subtracted value
C     REAL SBMIN                    !Minimum background subtracted pixel value
C     CHARACTER*(4*MAX_PRLIN) LIN   !Output line buffer
C     CHARACTER*10 STR              !String for current field
C     CHARACTER*4 FMT               !Format statement for integer field
C     CHARACTER*5 NUL               !String for point not included
C     CHARACTER*5 STAR              !String for omitted pixel border
C     CHARACTER*5 OVSP              !String for omitted pixel from overlapped
C                                   !spot
C     CHARACTER*5 OVLD              !String for omitted overload pixel
C     CHARACTER*5 REJ               !String for rejected pixel 
C
C------------------------------------------------------------------------------- 
C
      IERR = 0
      ERRSTR = ' '
      IF (IUN.LE.0) RETURN
      IF (KSPOT_LAST.EQ.0) THEN
         IERR = 1
         ERRSTR = 'No integrated spot currently stored'
         RETURN
      END IF
C
C====== Find maximum value in spots pixel values array & in spot profle
C
      PIXMAX = 0.0
      PRFMAX = 0.0
      DO 10 I = 1, NUM_IN_SPOT
         IF (PIXV(I).GT.PIXMAX) PIXMAX = PIXV(I)
         IF (PROF_IND(I).GT.PRFMAX) PRFMAX = PROF_IND(I)
 10   CONTINUE
      SCPIX = 1.0
      IF (PIXMAX.GT.0.0) SCPIX = 99.0/PIXMAX
      SCPRF = 1.0
      IF (PRFMAX.GT.0.0) SCPRF = 99.0/PRFMAX
C
C====== Output basic spot details
C
      LIN = 'Integrated spot'
      CALL STR_ADDI (LIN, I_H, 1)
      CALL STR_ADDI (LIN, I_K, 1)
      CALL STR_ADDI (LIN, I_L, 1)
      CALL STR_ADDS (LIN, 'at x =', 1)
      CALL STR_ADDF (LIN, XPOS, 1, 1)
      CALL STR_ADDS (LIN, ', y =', 0)
      CALL STR_ADDF (LIN, YPOS, 1, 1)
      CALL STR_ADDS (LIN, 'rasters',1)
      IF (ITERM.EQ.1) THEN
         CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
      ELSE
         WRITE(IUN,1000) LIN(1:LENSTR(LIN))
      END IF
      LIN = 'Intensity ='
      CALL STR_ADDF (LIN, INTEN, 1, 1)
      CALL STR_ADDS (LIN, ', sig(I) =', 0)
      CALL STR_ADDF (LIN, SIGI, 1, 1)
      IF (BAD) THEN
         CALL STR_ADDS (LIN, '- BAD SPOT, icode =',1)
         CALL STR_ADDI (LIN, ICODE, 1)
      END IF
      IF (ITERM.EQ.1) THEN
         CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
      ELSE
         WRITE(IUN,1000) LIN(1:LENSTR(LIN))
      END IF
C
C====== Ouput background plane parameters
C
      IF (OP_BGPLAN) THEN
         IF (ITERM.EQ.1) THEN
            CALL TTSEND (IUN,' ',1)
         ELSE
            WRITE(IUN,1001)
         END IF
         LIN = 'Background plane parameters: a ='
         CALL STR_ADDF (LIN, BACK(1), -3, 1)
         CALL STR_ADDS (LIN, ', b =', 0)
         CALL STR_ADDF (LIN, BACK(2), -3, 1)
         CALL STR_ADDS (LIN, ', c =', 0)
         CALL STR_ADDF (LIN, BACK(3), -3, 1)
         IF (ITERM.EQ.1) THEN
            CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
         ELSE
            WRITE(IUN,1000) LIN(1:LENSTR(LIN))
         END IF
         LIN = 'Number of rejected pixels ='
         CALL STR_ADDI (LIN, NREJ, 1)
         IF (ITERM.EQ.1) THEN
            CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
         ELSE
            WRITE(IUN,1000) LIN(1:LENSTR(LIN))
         END IF
      END IF        
C
C====== Get no. of characters required per o/p integer field (normally 3)
C
      NCH = 3
      FMT = '(I3)'
      NUL = '  .'
      STAR = '  *'
      OVSP = '  X'
      OVLD = '  +'
      REJ = '  R'
      SBMIN = 0.0
      DO 20 I = 1, NUM_IN_SPOT
         SBVAL = SUBPR(I)*SCPIX
         IF (SBVAL.LT.SBMIN) SBMIN = SBVAL
 20   CONTINUE
      I = NINT(-SBMIN)
      IF (I.GT.9) THEN
         NCH = 4
         FMT = '(I4)'
         NUL = '   .'
         STAR = '   *'
         OVSP = '   X'
         OVLD = '   +'
         REJ = '   R'
      END IF
      IF (I.GT.99) THEN
         NCH = 5
         FMT = '(I5)'
         NUL = '    .'
         STAR = '    *'
         OVSP = '    X'
         OVLD = '    +'
         REJ = '    R'
      END IF
C
C====== Get pixel offset limits
C
      IXMIN = 0
      IXMAX = 0
      IYMIN = 0
      IYMAX = 0
      DO 30 I = 1, NUM_IN_SPOT
         IF (IXP(I).LT.IXMIN) IXMIN = IXP(I)
         IF (IXP(I).GT.IXMAX) IXMAX = IXP(I)
         IF (IYP(I).LT.IYMIN) IYMIN = IYP(I)
         IF (IYP(I).GT.IYMAX) IYMAX = IYP(I)
 30   CONTINUE
C
C====== Output scaled pixel values
C
      IF (OP_PIXV) THEN
         IF (ITERM.EQ.1) THEN
            CALL TTSEND (IUN,' ',1)
         ELSE
            WRITE(IUN,1001)
         END IF
         LIN = 'Pixel values plot scaled by'
         CALL STR_ADDF (LIN, SCPIX, -3, 1)
         CALL STR_ADDS (LIN, 'to give a maximum value of 99', 1)
         IF (ITERM.EQ.1) THEN
            CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
         ELSE
            WRITE(IUN,1000) LIN(1:LENSTR(LIN))
         END IF
         DO 150 IY = IYMAX, IYMIN, -1
            LIN = ' '
            DO 140 IX = IXMIN, IXMAX
               IS = (IX-IXMIN)*NCH + 1
               STR = NUL
               DO 130 I = 1, NUM_IN_SPOT
                  IF (IXP(I).EQ.IX.AND.IYP(I).EQ.IY) THEN
                     IPIX = NINT (PIXV(I)*SCPIX)
                     WRITE (STR,FMT) IPIX
                     GO TO 135
                  END IF
 130           CONTINUE
 135           LIN(IS:IS+NCH-1) = STR
 140        CONTINUE
            IF (ITERM.EQ.1) THEN
               CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
            ELSE
               WRITE(IUN,1000) LIN(1:LENSTR(LIN))
            END IF
 150     CONTINUE
      END IF
C
C====== Output scaled background subtracted pixel values
C
      IF (OP_PIXSB) THEN
         IF (ITERM.EQ.1) THEN
            CALL TTSEND (IUN,' ',1)
         ELSE
            WRITE(IUN,1001)
         END IF
         LIN = 'Background subtracted pixel values plot scaled by'
         CALL STR_ADDF (LIN, SCPIX, -3, 1)
         IF (ITERM.EQ.1) THEN
            CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
         ELSE
            WRITE(IUN,1000) LIN(1:LENSTR(LIN))
         END IF
         DO 250 IY = IYMAX, IYMIN, -1
            LIN = ' '
            DO 240 IX = IXMIN, IXMAX
               IS = (IX-IXMIN)*NCH + 1
               STR = NUL
               DO 230 I = 1, NUM_IN_SPOT
                  IF (IXP(I).EQ.IX.AND.IYP(I).EQ.IY) THEN
                     IF (SHAPE(I).LT.0) THEN
                        STR = STAR
                        IF (SHAPE(I).EQ.-2) STR = OVSP
                        IF (SHAPE(I).EQ.-3) STR = OVLD
                        GO TO 235
                     END IF
                     DO 220 J = 1, NREJ
                        IF (IREJ(J).EQ.I) THEN
                           STR = REJ
                           GO TO 235
                        END IF
 220                 CONTINUE
                     IPIX = NINT (SUBPR(I)*SCPIX)
                     WRITE (STR,FMT) IPIX
                     GO TO 235
                  END IF
 230           CONTINUE
 235           LIN(IS:IS+NCH-1) = STR
 240        CONTINUE
            IF (ITERM.EQ.1) THEN
               CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
            ELSE
               WRITE(IUN,1000) LIN(1:LENSTR(LIN))
            END IF
 250     CONTINUE
      END IF
C
C====== Output scaled spot profile values
C
      IF (OP_PROF) THEN
         IF (ITERM.EQ.1) THEN
            CALL TTSEND (IUN,' ',1)
         ELSE
            WRITE(IUN,1001)
         END IF
         LIN = 'Spot profile plot scaled to a maximum of 99'
         IF (ITERM.EQ.1) THEN
            CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
         ELSE
            WRITE(IUN,1000) LIN(1:LENSTR(LIN))
         END IF
         DO 350 IY = IYMAX, IYMIN, -1
            LIN = ' '
            DO 340 IX = IXMIN, IXMAX
               IS = (IX-IXMIN)*NCH + 1
               STR = NUL
               DO 330 I = 1, NUM_IN_SPOT
                  IF (IXP(I).EQ.IX.AND.IYP(I).EQ.IY) THEN
                     IF (SHAPE(I).LT.0) THEN
                        STR = STAR
                        IF (SHAPE(I).EQ.-2) STR = OVSP
                        IF (SHAPE(I).EQ.-3) STR = OVLD
                        GO TO 335
                     END IF
                     IPIX = NINT (PROF_IND(I)*SCPRF)
                     WRITE (STR,FMT) IPIX
                     GO TO 335
                  END IF
 330           CONTINUE
 335           LIN(IS:IS+NCH-1) = STR
 340        CONTINUE
            IF (ITERM.EQ.1) THEN
               CALL TTSEND (IUN,LIN(1:LENSTR(LIN)),1)
            ELSE
               WRITE(IUN,1000) LIN(1:LENSTR(LIN))
            END IF
 350     CONTINUE
      END IF
      RETURN
C
C====== Format statements
C
1000  FORMAT(A)
1001  FORMAT(' ')
      END
