*--------1---------2---------3---------4---------5---------6---------7--

      subroutine TELDATA

* initialize BCS
*----------------------------------
      include './Include/bcs.inc'

* initialize DST branches
*----------------------------------
      include './Include/dstdata.inc'

* initialize NTUPLE variables
*----------------------------------
      include './Include/ntdata.inc'

* initialize TEL variables
*----------------------------------
      include './Include/teldata.inc'

* initialize user variables
*----------------------------------
      external UNTEL1, UNTLV1

      integer L1NR(18), L1WD(18), L1DATA(18)
      integer TYPE, SLICE, DSLICE, ITEL1(0:255), IEXIST
      integer TLV1A(0:127), TLV1R(0:127)

      parameter (TYPE = 0, SLICE = 0, DSLICE = 1)

* event processing for L1 data
*----------------------------------
      STTBST = 0
      STTPAD = 0
      STTXXX = 0

      STTR20 = 0
      STTR30 = 0
      STTR40 = 0

      SPELM1 = 0
      SPELM2 = 0
      SPHAD1 = 0
      SPHAD2 = 0

      FITIA = 0
      FITBG = 0
      BTOFBG = 0
      STOFBG = 0
      VETOBG = 0

      PADTRG = 0
      PADHAL = 0
      PADVTO = 0

      S0RAW = 0
      S2RAW = 0
      S3RAW = 0
      S4RAW = 0

      S7RAW = 0
      S8RAW = 0
      S9RAW = 0

      S0ACT = 0
      S2ACT = 0
      S3ACT = 0
      S4ACT = 0

      S7ACT = 0
      S8ACT = 0
      S9ACT = 0

      L2TR20 = 0
      L2TR30 = 0
      L2TR40 = 0

      TELFOUND = .FALSE.

      if ((TELFIL.eq.0).or.NONSENSE) return

      TELFOUND = .TRUE.

* standard CT subroutine UNTEL1
*----------------------------------

      call UNTEL1(TYPE, SLICE, DSLICE, ITEL1, IEXIST)

      if (IEXIST.eq.0) TELFOUND = .FALSE.

* extract TE numbers for unique names
*----------------------------------
      do I = 1, 18
        call TELNR(L1NAME(I), L1NR(I), L1WD(I))
        if (L1NR(I).eq.-1) TELFOUND = .FALSE.
      end do

* assign signals
*----------------------------------
      if (TELFOUND) then

* don't change this masterpiece !
*----------------------------------
          J = 1
	  L = 0
        do I = 1, 18
          if (I.eq.J) then
            do K = 1, L1WD(J)
	      L = L + 1
              L1DATA(L) = ITEL1(L1NR(J) + (K-1))
	    end do
	    J = J + L1WD(J)	    
          end if
        end do
*----------------------------------
        STTBST = L1DATA(1)
        STTPAD = L1DATA(2)
        STTXXX = L1DATA(3)

        STTR20 = L1DATA(4)
        STTR30 = L1DATA(5)
        STTR40 = L1DATA(6)

        SPELM1 = L1DATA(7)
        SPELM2 = L1DATA(8)
        SPHAD1 = L1DATA(9)
        SPHAD2 = L1DATA(10)

        FITIA  = L1DATA(11)
        FITBG  = L1DATA(12)
        BTOFBG = L1DATA(13)
        STOFBG = L1DATA(14)

        VETOBG = L1DATA(15)

        PADTRG = L1DATA(16)
        PADHAL = L1DATA(17)
        PADVTO = L1DATA(18)
      end if

      call UNTLV1(TYPE, TLV1A, TLV1R, IEXIST)

      if (IEXIST.eq.0) TELFOUND = .FALSE.

      if (TELFOUND) then
        S0RAW = TLV1R(0)
        S2RAW = TLV1R(2)
        S3RAW = TLV1R(3)
        S4RAW = TLV1R(4)

        S7RAW = TLV1R(7)
        S8RAW = TLV1R(8)
	S9RAW = TLV1R(9)

        S0ACT = TLV1A(0)
        S2ACT = TLV1A(2)
        S3ACT = TLV1A(3)
        S4ACT = TLV1A(4)

        S7ACT = TLV1A(7)
        S8ACT = TLV1A(8)
	S9ACT = TLV1A(9)
      end if

* L2_subtriggers:
*----------------------------------
      L2TR20 = iand(ishft(l2tel(2, L2ERR),-1),1) ! L2_T17      
      L2TR30 = iand(ishft(l2tel(2, L2ERR), 0),1) ! L2_T16
      L2TR40 = iand(ishft(l2tel(2, L2ERR),-3),1) ! L2_T19

      if (L2ERR.ne.0) TELFOUND = .FALSE.

* flag for further calculations
*----------------------------------
      if ((TELFIL.eq.1).and.(.not.TELFOUND)) NONSENSE = .TRUE.

      return
      end
