      PROGRAM TUNE_FEND_SCALING
C----------------------------------------------------------------------
C-
C-   Purpose and Methods : help tuning lookup analog_input_scaling
C-
C-   Inputs  : LSM file name
C-   Outputs : TUNE_FEND_SCALING.LOG
C-
C-   Created    6-DEC-1990  MICHIGAN STATE UNIVERSITY, TRIGGER CONTROL SOFTWARE
C-
C----------------------------------------------------------------------
      IMPLICIT NONE
C----------------------------------------------------------------------
C
C       Global declarations
C
      INCLUDE 'D0$LEVEL1:LEVEL1_LOOKUP.PARAMS'
      INCLUDE 'D0$LEVEL1:LEVEL1_LOOKUP.INC'
C
C       Local variables
C
      CHARACTER*80 FILE_NAME
      INTEGER      STATUS
      INTEGER      SIGN_ETA, MAGN_ETA, PHI, CHANNEL
      REAL         SIN_THETA_0(EM_TOWER:hd_TOWER,1:2)
      REAL         DIFF

      WRITE ( 6, 1000 )
 1000 FORMAT ( ' LSM FILE NAME: ', $ )
      READ ( 5, 1010 ) FILE_NAME
 1010 FORMAT ( A )

      CALL LOAD_COMMON_BLOCK ( 10, FILE_NAME, STATUS )

      WRITE ( 6, 1050 ) STATUS
 1050 FORMAT ( ' STATUS : ', I5 )

      IF ( STATUS .NE. 0 ) GOTO 999

      OPEN ( UNIT = 20, FILE = 'TUNE_FEND_SCALING.LOG', STATUS = 'NEW' )

      DO SIGN_ETA = POS_ETA, NEG_ETA
        DO MAGN_ETA = ETA_MIN, ETA_MAX
          DO PHI = 1, 2

            DO CHANNEL =  EM_TOWER , HD_TOWER

              IF ( TOWER_RZ_COORD(SIGN_ETA,MAGN_ETA,PHI,CHANNEL,R_COORD)
     &        .LT. 1.E-10 ) THEN
                SIN_THETA_0(CHANNEL,PHI) = 0.0
                GOTO 9
              END IF

              SIN_THETA_0(CHANNEL,PHI) =
     &      TOWER_RZ_COORD(SIGN_ETA,MAGN_ETA,PHI,CHANNEL,R_COORD)
     &      / SQRT(
     &        TOWER_RZ_COORD(SIGN_ETA,MAGN_ETA,PHI,CHANNEL,R_COORD)**2
     &      + TOWER_RZ_COORD(SIGN_ETA,MAGN_ETA,PHI,CHANNEL,Z_COORD)**2 )
C

    9         CONTINUE
            ENDDO

            WRITE ( 20, 2000 ) SIGN_ETA, MAGN_ETA, PHI,
     &                      (SIN_THETA_0(CHANNEL,PHI),
     &                                   CHANNEL=EM_TOWER,HD_TOWER)

          END DO

          IF ( SIN_THETA_0(HD_TOWER,1) .LT. 1.E-10 ) THEN
            DIFF = 0.0
            GOTO 99
          END IF

          DIFF = 100.0 * ( SIN_THETA_0(HD_TOWER,1)
     &                   - SIN_THETA_0(HD_TOWER,2) )
     &                 / SIN_THETA_0(HD_TOWER,1)

   99     CONTINUE

          WRITE ( 20, 3000 ) DIFF

        END DO
      END DO

 2000 FORMAT ( ' SIGN', I5, ' ETA', I5, ' PHI', I5,
     &         ' EM', F9.5, ' HD', F9.5 )

 3000 FORMAT ( ' % odd-even HD: ', F10.5 )

      CLOSE ( UNIT = 20 )

C----------------------------------------------------------------------
  999 CONTINUE
      END
