*CMZU:          10/02/2000  17.53.47  by  Thomas Naumann
*-- Author :  Thomas Naumann   29/11/99

      SUBROUTINE FSTALI (X,Y,Z, XT,YT,ZT)

**********************************************************************
*     Transform space point (X,Y,Z) with
*     FST external alignment
**********************************************************************

*KEEP,BCS.
      INTEGER      NHROW,NHCOL,NHLEN
      PARAMETER   (NHROW = 2, NHCOL = 1, NHLEN=2)
      INTEGER      NBOSIW
      PARAMETER   (NBOSIW=1000)
      INTEGER      IW(NBOSIW)
      REAL         RW(NBOSIW)
      COMMON /BCS/ IW
      EQUIVALENCE (RW(1),IW(1))
      SAVE   /BCS/
*KEEP,BOSMDL.
C     ------BOSMDL
      LOGICAL       BEGJOB,ENDRUN,BEGRUN,REVENT,ENDJOB,OTHDAT
      COMMON/BOSMDL/BEGJOB,ENDRUN,BEGRUN,REVENT,ENDJOB,OTHDAT,
     +              LCCRUN,NCCRUN,NEVENT,
     +              IHA,IBS,IDB,IDATEL,LUP,ISN,JSN
      SAVE  /BOSMDL/
C     ------
*KEND.

      REAL    ZN(0:4)
      LOGICAL FIRST
      DATA    FIRST /.TRUE./
      SAVE

      IF (BEGRUN.OR.FIRST) THEN
        FIRST=.FALSE.
* get FST geometry
        IND = IABS(NDB('FGAS'))
        IF (IND.EQ.0) THEN
          CALL ERRLOG (99,'F: FSTALI. No FGAS bank')
        ELSE
          NZU    = IW(IND+13)
          DO 1 I=0,NZU-1
1         ZN(I) = RW(IND+14+I)
          ZROT = 0.5*(ZN((NZU-1)/2)+ZN(NZU/2))
        ENDIF

* get FST alignment
        CALL UGTBNK ('FSTA',IND)
        IF (IND.EQ.0) THEN
          CALL ERRLOG (1,'W: FSTALI. No FSTA bank')
          RFST = 0.
          FFST = 0.
          XFST = 0.
          YFST = 0.
          ZFST = 0.
          SX = 0.
          SY = 0.
          CX = 1.
          CY = 1.
        ELSE
          RFST = RW(IND+1)
          FFST = RW(IND+2)
          ZFST = RW(IND+3)
          XFST = RFST*COS(FFST)
          YFST = RFST*SIN(FFST)
* Euler angles THX,Y of rotation around axes X,Y
          THX  = RW(IND+4)
          THY  = RW(IND+5)
          SX  = SIN(THX)
          SY  = SIN(THY)
          CX  = COS(THX)
          CY  = COS(THY)
        ENDIF
      ENDIF

* ------------------------------------------------------------

* translate
      XX = X - XFST
      YY = Y - YFST
      ZT = Z - ZFST - ZROT

* rotate
      XT =  XX*CY            + ZT*SY
      YT = -XX*SX*SY + YY*CX + ZT*SX*CY
      ZT = -XX*CX*SY - YY*SX + ZT*CX*CY

      ZT = ZT + ZROT

      END
