C
C**************************
C**     LFN_REFN_LOG     **
C**************************

C
CD-Routine: Log Automatic Refinement Results - LFN_REFN_LOG
C This routine is used to log the results from the automatic refinement
C procedure of the routine LFN_AUTO_REFN. If used, it is passed as the
C user supplied LOG_RESULTS subroutine which is one of the LFN_AUTO_REFN
C parameters and must be defined as external. Its sections of output 
C may also be used by calling it from a user supplied routine. (Note that 
C this version does not do output for all the possible stages)
CD-end
CD-Fortran:
      SUBROUTINE LFN_REFN_LOG (IUN_LOG, ISTAGE, IPACK, IPLATE, IGEN,
     +                         NSEARCH, NOBS, OLD_RMS, RMS, RFMSG,
     +                         IFLAG, JFLAG, KFLAG)
CD-end
      INCLUDE 'implicit_none.finc'
C
C Purpose: Log automatic refinement results
C
C Author:  John W. Campbell, October 1994
C
C Arguments:
C
      INTEGER IUN_LOG
      INTEGER ISTAGE
      INTEGER IPACK
      INTEGER IPLATE
      INTEGER IGEN
      INTEGER NSEARCH
      INTEGER NOBS
      REAL    OLD_RMS
      REAL    RMS
      CHARACTER*(*) RFMSG
      INTEGER IFLAG(*)
      INTEGER JFLAG(*)
      INTEGER KFLAG(*)
C
CD-Parameters:
C IUN_LOG     i  (R)  Unit number for log file as passed to LFN_AUTO_REFN
C ISTAGE      i  (R)  Refinement stage to be logged (ipack, iplate are
C                     always passed unless ISTAGE is 0 or 20; other 
C                     parameters which need to be set for a particular 
C                     stage are shown in brackets):
C                         
C                     = 0  Main heading for auto 
C                          refinement
C                     = 1  Current pack/plate 
C                     = 2  'INITIAL SEARCH' phase started
C                     = 3  Start image read
C                     = 4  End image read
C                     = 5  Start spots list generation
C                     = 6  End spots list generation
C                     = 7  Spot generation results
C                          (IGEN)
C                     = 8  Start spots search
C                     = 9  End spots search
C                     = 10 Initial spot search results 
C                          for the current plate
C                          (NSEARCH, NOBS, RMS)
C                     = 11 Other spot search results  
C                          (NSEARCH, NOBS, RMS)
C                     = 12 'INITIAL REFINEMENT' phase
C                           started 
C                     = 13 List of parameters to be
C                          refined (IFLAG, JFLAG,
C                          KFLAG)
C                     = 14 Start refinement cycle
C                     = 15 End refinement cycle
C                     = 16 Refinement results except
C                          for last cycle.
C                          (NOBS, OLD_RMS, RMS,
C                          RFMSG, IFLAG, JFLAG, 
C                          KFLAG)
C                     = 17 Refinement results for 
C                          last cycle.
C                          (NOBS, OLD_RMS, RMS,
C                          RFMSG, IFLAG, JFLAG, 
C                          KFLAG)
C                     = 18 'MAIN REFINEMENT' phase
C                     = 19 'FINAL REFINEMENT' phase
C                     = 20  Refinement completed
C
C IPACK       i  (R)  Current pack number
C IPLATE      i  (R)  Current plate number
C IGEN(6)     i  (R)  Counts from reflection generation (the 6 parameters
C                     returned from LRL_COUNTS)
C NSEARCH     i  (R)  No. of spots searched for.
C NOBS        i  (R)  No. spots found and used in refinement
C OLD_RMS     i  (R)  Rms from spot search prior to refinement
C RMS         i  (R)  Rms from spot search or refinement
C RFMSG       c  (R)  Message string from refinement routine
C IFLAG(7)    i  (R)  See LFN_RFN routine
C JFLAG(6)    i  (R)  See LFN_RFN routine
C KFLAG(6)    i  (R)  SEe LFN_RFN routine      
CD-end
C
C====== Parameter statements
C 

C
C====== GLOBALS:
C

C
C====== FUNCTION TYPES:
C
      INTEGER LENSTR
C
C====== LOCALS:
C
      INTEGER L
      INTEGER LL
      CHARACTER*80 STR

C     INTEGER L               !Loop counter
C     INTEGER LL              !Significant length of character sring
C     CHARACTER*80 STR        !Temp string
C
C-------------------------------------------------------------------------------
C
C====== Output information
C
      IF (ISTAGE.EQ.0) THEN
         WRITE (IUN_LOG, 1001)
         RETURN
      ELSE IF (ISTAGE.EQ.1) THEN
         STR = 'Refinement for pack'
         CALL STR_ADDI (STR, IPACK, 1)
         CALL STR_ADDS (STR, ', plate',0)
         CALL STR_ADDI (STR, IPLATE, 1)
         WRITE (IUN_LOG, 1002) STR(1:LENSTR(STR))
         LL = LENSTR(STR)
         WRITE (IUN_LOG, 1003) ('-',L=1,LL)
         RETURN
      ELSE IF (ISTAGE.EQ.10.OR.ISTAGE.EQ.11) THEN
         STR = 'Searched for'
         CALL STR_ADDI (STR, NSEARCH, 1)
         CALL STR_ADDS (STR, 'spots, found', 1)
         CALL STR_ADDI (STR, NOBS, 1)
         CALL STR_ADDS (STR, ', rms =', 0)
         CALL STR_ADDF (STR, RMS, 3, 1)
         CALL STR_ADDS (STR, 'mm.', 1)
         WRITE (IUN_LOG,1010) STR(1:LENSTR(STR))
         RETURN
      ELSE IF (ISTAGE.EQ.12) THEN
         WRITE (IUN_LOG, 1020)
         RETURN
      ELSE IF (ISTAGE.EQ.16.OR.ISTAGE.EQ.17) THEN
         CALL LFN_SHOWREF (IUN_LOG, 0, IPACK, IPLATE, RMS, OLD_RMS)
         RETURN
      ELSE IF (ISTAGE.EQ.18) THEN
         WRITE (IUN_LOG, 1030)
         RETURN
      ELSE IF (ISTAGE.EQ.19) THEN
         WRITE (IUN_LOG, 1040)
         RETURN
      ELSE IF (ISTAGE.EQ.20) THEN
         WRITE(IUN_LOG, 1050)
         RETURN
      END IF
      RETURN
C
C====== Format statements
C
 1001 FORMAT(/,'Automatic Parameter Refinement',/,
     +         '==============================')

 1002 FORMAT(/,A)
 1003 FORMAT(120A1)
 1010 FORMAT(A)
 1020 FORMAT('==Initial Refinement Phase==')
 1030 FORMAT('==Main Refinement Phase==')
 1040 FORMAT('==Final Refinement Phase==')
 1050 FORMAT('==End of Automatic Refinement==')
      END
