!-----------------------------------------------------------------------
! Local alignment: correction of cerenkov angle due to mirror misalignments
! M. Staric, apr-2002 (from calc_thcer.F of jan-2000, jan-2002)
!-----------------------------------------------------------------------

      SUBROUTINE CORRECT_THC(ID_SPH,ID_PLM,TH_CER,FI_CER)

      IMPLICIT NONE
      INTEGER ID_SPH, ID_PLM
      REAL TH_CER, FI_CER

#include "riter/RIC_MIRAL.fi"
      DATA MIRAL_UPD /0/
#include "riter/local.fi"
      REAL X0, Y0

      IF(ID_SPH.LT.0) RETURN
      IF(ID_PLM.LT.0) RETURN
      X0=DX_SPH(ID_SPH)+DX_PLM(ID_PLM)
      Y0=DY_SPH(ID_SPH)+DY_PLM(ID_PLM)
      TH_CER=TH_CER-(X0*COS(FI_CER)+Y0*SIN(FI_CER))

      RETURN
      END



      SUBROUTINE RITER_PUT_MIRAL(N,TIP,ID,SX,SY,EX,EY,EXR,EYR)

c--   called by acReadMiral

      IMPLICIT NONE
      INTEGER N
      INTEGER TIP(*), ID(*)
      REAL SX(*), SY(*), EX(*), EY(*), EXR(*), EYR(*)

#include "riter/RIC_MIRAL.fi"
      INTEGER I

      IF(N.LE.0) RETURN
      DO I=0,79
         DX_SPH(I)=0.
         DY_SPH(I)=0.
      ENDDO
      DO I=0,35
         DX_PLM(I)=0.
         DY_PLM(I)=0.
      ENDDO
      DO I=1,N
         IF(TIP(I).EQ.0) THEN
            IF(ID(I).GE.0.AND.ID(I).LT.80) THEN
               DX_SPH(ID(I))=SX(I)/1000.
               DY_SPH(ID(I))=SY(I)/1000.
            ENDIF
         ELSE
            IF(ID(I).GE.0.AND.ID(I).LT.36) THEN
               DX_PLM(ID(I))=SX(I)/1000.
               DY_PLM(ID(I))=SY(I)/1000.
            ENDIF
         ENDIF
      ENDDO
      MIRAL_UPD=MIRAL_UPD+1

      RETURN
      END



      SUBROUTINE READ_MIRR_CORR(LUN,FILE_NAME,IO)

c--   read misalignments from file

      IMPLICIT NONE
      INTEGER LUN
      CHARACTER*(*) FILE_NAME
      INTEGER IO

#include "riter/RIC_MIRAL.fi"
      INTEGER I, NMI, L, ID
      CHARACTER*80 TT, SS
      REAL X,Y,EX,EY

      DO I=0,79
         DX_SPH(I)=0.
         DY_SPH(I)=0.
      ENDDO
      DO I=0,35
         DX_PLM(I)=0.
         DY_PLM(I)=0.
      ENDDO
      IF(FILE_NAME.EQ.' ') RETURN
      OPEN(LUN,FILE=FILE_NAME,STATUS='OLD',IOSTAT=IO)
      IF(IO.NE.0) THEN
         PRINT*,'can''t open file: ',FILE_NAME
         RETURN
      ENDIF
      NMI=0
 1    CONTINUE
      READ(LUN,'(A)',ERR=1,END=99) TT
      CALL STRIP_OFF(TT,L)
      IF(L.EQ.0.OR.TT(1:1).EQ.'#') GOTO 1
      CALL SUB_STRING(TT,SS,L)
      IF(SS(1:L).EQ.'S') THEN
         READ(TT,*,IOSTAT=IO) ID,X,Y,EX,EY
         IF(IO.NE.0.OR.ID.LT.0.OR.ID.GT.79) THEN
            PRINT*,'file corrupted: ',SS(1:L),'|',TT
            GOTO 99
         ENDIF
         DX_SPH(ID)=X/1000.
         DY_SPH(ID)=Y/1000.
         NMI=NMI+1
      ELSE IF(SS(1:L).EQ.'P') THEN
         READ(TT,*,IOSTAT=IO) ID,X,Y,EX,EY
         IF(IO.NE.0.OR.ID.LT.0.OR.ID.GT.35) THEN
            PRINT*,'file corrupted: ',SS(1:L),'|',TT
            GOTO 99
         ENDIF
         DX_PLM(ID)=X/1000.
         DY_PLM(ID)=Y/1000.
         NMI=NMI+1
      ENDIF
      GOTO 1

 99   CONTINUE
      CLOSE(LUN)
      PRINT*,NMI,' corrections for mirrors read'

      RETURN
      END



      SUBROUTINE RITER_DUMP_MIRAL

c--   dump alignment correction on screen (called by kuip command)

      IMPLICIT NONE
#include "riter/RIC_MIRAL.fi"
      INTEGER I

      PRINT*,'Alignment constants for mirrors (in mrad): '
      DO I=0,79
         IF(DX_SPH(I).NE.0.OR.DY_SPH(I).NE.0) THEN
            PRINT '(A3,I3,2F8.2)',' S ',I,DX_SPH(I)*1000,DY_SPH(I)*1000
         ENDIF
      ENDDO
      DO I=0,35
         IF(DX_PLM(I).NE.0.OR.DY_PLM(I).NE.0) THEN
            PRINT '(A3,I3,2F8.2)',' P ',I,DX_PLM(I)*1000,DY_PLM(I)*1000
         ENDIF
      ENDDO
      PRINT*,'---- Updated = ',MIRAL_UPD
      PRINT*

      RETURN
      END



