      
C LAST UPDATE:
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C***************************
C**   LSC_RFACTS_LSP      **
C***************************
C
CD-Routine: Get R-factor tables data - LSC_RFACTS_LSP
C Calls LSC_RFACTS with parameters derived from LSP and then calls
C LSC_RFACTS_OUT with flags derived from LSP.
CD-end
CD-Fortran:
      SUBROUTINE LSC_RFACTS_LSP (LINDX, IUN, PLATEMERGE, IERR)
CD-end
C
      INCLUDE 'implicit_none.finc'
C
C Purpose: Get options for R-factor output
C
C Author:  Steffi Arzt, July 1997
C
C Arguments:
C
      INTEGER LINDX
      INTEGER IUN
      INTEGER IERR
      LOGICAL PLATEMERGE
CD-Parameters:
C  LINDX      i  (R)  Index for LIRL
C  IUN        i  (R)  Unit no. for tables output
C  IERR       i  (W)  Error flag as returned from LSC_RFACTS
C  PLATEMERGE l  (R)  .true.  = Merge plate data prior to statistics
C                     .false. = keep all data seperate
CD-end
C
C
C====== PARAMETERS:
C
      INCLUDE 'limits.finc'
C
C====== GLOBALS:
C
      INCLUDE 'rout_arrays.finc'
      INCLUDE 'lsm_flags.finc'
      INCLUDE 'other_scale.finc'

C
C====== FUNCTION TYPES:
C

      INTEGER KDMSTR
      INTEGER LENSTR

C
C====== LOCALS:
C
C 
      LOGICAL SPOV    
      LOGICAL OVLD
      INTEGER KERR 
      INTEGER IR1             
      INTEGER IR2             
      INTEGER IR3             
      INTEGER MAXE
      INTEGER IVAL(10)
      INTEGER NUM_INP
      INTEGER ISTAT 
      INTEGER ND(MAXCOEFFS)
      INTEGER NBIN(MAXRANGE)
      INTEGER ITYP   
      INTEGER OVLD_OUT,SPAT_OUT,NEGINT           
      REAL FVAL(MAXVARR)
      REAL SIGMA_CUT
      CHARACTER*80 STR   
      CHARACTER*20 VALSTR
      CHARACTER*80 ERRSTR  

C     LOGICAL SPOV                 ! Include spatial overlaps flag
C     INTEGER KERR                 ! Error return flag
C     INTEGER IR1                  ! R-factor table 1 flag
C     INTEGER IR2                  ! R-factor table 2 flag             
C     INTEGER IR3                  ! R-factor table 2 flag             
C     INTEGER MAXE                 ! Max length for error string
C     INTEGER IVAL(10)             ! Integer values array
C     INTEGER NUM_INP              ! No. of input values for a variable 
C                                  ! array param.
C     INTEGER ISTAT                ! Parameter status flag
C     INTEGER ND(MAXCOEFFS)        ! No. of decimal places flags array
C     INTEGER NBIN(MAXRANGE)       ! No. of bins per range
C     INTEGER ITYP                 ! Parameter type
C     REAL FVAL(MAXVARR)           ! Real values array
C     REAL SIGMA_CUT               ! Sigma cutoff for R-factor analyses
C     CHARACTER*80 STR             ! Temporary string
C     CHARACTER*20 VALSTR          ! Dummy value string
C     CHARACTER*80 ERRSTR          ! Error string

      DATA NBIN/MAXRANGE*10/

C
C-------------------------------------------------------------------------------
C
      MAXE = 80

      STR = 'RFAC_OUTPUT'
      CALL KDMF_GETVALUE (KINDX, KDMSTR(STR),LENSTR(STR), 1, 1, 
     +                    2, KDMSTR(VALSTR), FVAL, ND, IVAL, 
     +                    NUM_INP, ISTAT, ITYP, KDMSTR(ERRSTR), 
     +                    MAXE, KERR) 
      IF (KERR.NE.0) GOTO 810  
      IR1 = 1
      IR2 = 0
      IR3 = 0  
      IF (IVAL(1).EQ.0) IR1 = 0
      IF (IVAL(1).EQ.2) IR2 = 1
      IF (IVAL(1).EQ.3) IR3 = 1
      IF (IVAL(1).GT.3) THEN
         IR2 = 1
         IR3 = 1
      END IF 
 
      CALL LSC_GET_OUTPUTFLAGS (LINDX, OVLD_OUT, SPAT_OUT,  
     +                          NEGINT, SIGMA_CUT)      

      IF (SPAT_OUT.EQ.1) SPOV = .TRUE.
      IF (SPAT_OUT.EQ.0) SPOV = .FALSE.
      IF(OVLD_OUT.NE.0) THEN
         OVLD = .TRUE.
      ELSE
         OVLD = .FALSE.
      END IF      

      CALL LSC_RFACTS (LINDX,NBIN,SIGMA_CUT,SPOV,.FALSE.,
     +     OVLD,IERR)
      CALL LSC_RFACTS_OUT (IUN, IR1, IR2, IR3, .FALSE.)

      IF(PLFLAG.AND.PLATEMERGE) THEN
         CALL LSC_RFACTS (LINDX,NBIN,SIGMA_CUT,SPOV,PLATEMERGE,
     +        IERR)
         CALL LSC_RFACTS_OUT (IUN, IR1, IR2, IR3, .TRUE.)
      END IF

      RETURN
C
C====== Fatal error condition
C

810   WRITE (IUN,2010) ERRSTR, STR(1:LENSTR(STR))
      WRITE (IUN,2000) 
      STOP
C
C====== Format statements
C
 2010 FORMAT (/,' ***  ERROR  ***  ERROR IN LSC_RFACTS_LSP',
     +       /' ERROR RETURN FROM KDMF_GETVALUE:',/,
     +       A,/,' KEYWORD: ',A)
 2000 FORMAT (//,' ******** PROCESSING STOPPED *******')

      END
