*CMZ :          19/04/2000  15.49.57  by  Thomas Naumann
*-- Author :  Thomas Naumann   18/11/99
      SUBROUTINE BSTALI (X,Y,Z, XT,YT,ZT)

**********************************************************************
*     Transform space point (X,Y,Z) with
*     BST 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:7)
      LOGICAL FIRST
      DATA    FIRST /.TRUE./

      IF (BEGRUN.OR.FIRST) THEN
        FIRST=.FALSE.
* get BST geometry
*   MC UGTRUN
        IDTYPE = JRDATA('RUNTYPE',IRET)
        IF (IRET.EQ.0.AND.IDTYPE.EQ.1) THEN
          LCONFG = JRDATA('VERSION',IRET) - 1
          IF (IRET.EQ.0) THEN
*   BST1 1997
            IF     (LCONFG.LE.4) THEN
              IND = NDB('DBPAR =GET "BGAS" DP0 VLE 210000')
*   BST2 1998-2000
            ELSEIF (LCONFG.LE.9) THEN
              IND = NDB('DBPAR =GET "BGAS" DP0 VLE 300000')
*   BST3 2000
            ELSE
              IND = NDB('DBPAR =GET "BGAS" DP0 VMX 999999')
            ENDIF
          ENDIF
        ENDIF

        IND = IABS(NDB('BGAS'))
        IF (IND.EQ.0) THEN
          CALL ERRLOG (1,'F: BSTALI. No BGAS bank')
        ELSE
          NZ    = IW(IND+12)
*   BST1,3   4,5 planes
          IF (NZ.LT.8) THEN
            DO 1 I=0,3
1           ZN(I) = RW(IND+18) + RW(IND+13+I)
            DO 7 I=4,NZ-1
7           ZN(I) = RW(IND+18) + RW(IND+26+I)
*   BST2     8 planes
          ELSE
            DO 2 I=0,3
2           ZN(I) = RW(IND+18) + RW(IND+30+I)
            DO 3 I=4,NZ-1
3           ZN(I) = RW(IND+18) + RW(IND+ 9+I)
          ENDIF
          ZROT = 0.5*(ZN((NZ-1)/2)+ZN(NZ/2))
        ENDIF

* get BST alignment
        CALL UGTBNK ('BSTA',IND)
        IF (IND.EQ.0) THEN
          CALL ERRLOG (30,'W: BSTALI. No BSTA bank')
          RBST = 0.
          FBST = 0.
          XBST = 0.
          YBST = 0.
          ZBST = 0.
          SX = 0.
          SY = 0.
          CX = 1.
          CY = 1.
        ELSE
          RBST = RW(IND+1)
          FBST = RW(IND+2)
          ZBST = RW(IND+3)
          XBST = RBST*COS(FBST)
          YBST = RBST*SIN(FBST)
* 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 - XBST
      YY = Y - YBST
      ZT = Z - ZBST - 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
