C   26/10/78 002200501  MEMBER NAME  LGADCN   (SOURCE)      FORTRAN
      SUBROUTINE LGADCN(IER)
C
C     S.YAMADA     17-10-78  19:30      COPY OF LGADCN IN LGSOURCE
C     LAST MODIFICATION  16-02-79  14:10
C
C---- ADC ADDDRESS OF THE LEAD GLASS DATA IS CHANGED TO
C     LEAD GLASS POSITION.
C---- CALLED FROM (LGANAL).
C
C---- INPUT FORMAT
C     ADDRESS
C     DATA
C     DATA
C      .
C      .
C     ADDRESS
C     DATA
C      .
C     HERE NO.OF DATA AFTER EACH ADDRESS IS NOT FIXED.
C---- ADDRESS= 2BITS ADDRESS FLAG(10)+4BITS CRATE#+10BITS SUBADDRESS
C
C---- POSITION = PHI NO.(0-83)*32 + Z NO.(0-31)
C---- FOR THE TOP ENDCAP POSITION IS (2688-2783)
C---- FOR THE BOTTOM ENDCAP POSITION IS (2784-2879)
C---- THE ENDCAP LEAD GLASS COUNTERS ARE NUMBERED ACCORDING TO
C     THE JADE NUMBERING SEQUENCE.
C
      IMPLICIT INTEGER *2 (H)
#include "cdata.for"
C
#include "clgwork1.for"
      DATA JMAX/4000/
C
      DIMENSION HH(2), JBT(4) ,HLGA(5760)
      EQUIVALENCE (HH(1),IFULL),(HH(2),HA)
      EQUIVALENCE (HLGA(1),HLGADC(1,1))
      DATA JBT/ 0,2688,2784,2880/
C
      COMMON/ CLGVRN/ NVRSN(20)
      DATA NVCODE/178112317/
      NVRSN(1) = NVCODE
C
      IER = 0
C
C---- LOOK FOR THE POINTER OF THE LG-DATA
      CALL CLOC( NP, 'ALGL', 0 )
      IF(NP.LE.0) RETURN
C
C---- FOR THE INPUT DATA LNG= # OF DATA IN HDATA INCLUDING SUB-POINTERS.
      LNG = IDATA(NP)
      NNP = NP+NP
C
C---- EXAMINE IF FORMAT CHANGE IS ALREADY DONE.
      IF(HDATA(NNP+1)-100) 1,200,200
C
C
C---- FORMAT IS CHANGED HERE.   / NOW 'ALGN' IS USED FOR THE INPUT.
C                                 I.E. CONVERSION MUST BE FINISHED.
C
    1 J = 0
C---- J IS THE COUNTER FOR THE CONVERTED DATA
      JP = 1
C---- JP IS THE INDEX FOR THE BOUNDARY TABLE.
      HA = 0
C---- HA IS THE CURRENT ADDRESS OF THE ADC DATAGROUP.
C
      LNGRAW = 2*LNG
C---- CHECK LENGTH (RAW DATA HAS NOT THE POINTER,IF NOT CONVERTED.)
      IF(LNGRAW-2) 20,20,2
    2   DO 10 I=3,LNGRAW
        IF(HDATA(I+NNP).LT.-16384) GO TO 3
        IF(HDATA(I+NNP)) 1,4,4
C----   JUNK DATA IS SKIPPED.
C
C----   NEW ADDRESS IS ENCOUNTERED. REMOVE THE FLAG PART.
    3   HA = HDATA(I+NNP)
        IFULL = IFULL+32768
C----   CONTINUE TO THE NEXT CRATE WITHOUT A GAP.
        IC = HA/1024
C       FOR THE CRATE NO. 0,1 AND 2 ARE EXPECTED.
        IW = HA-1024*IC
        IC = MOD(IC,4)
        IF(IC.GT.2) GO TO 92
        HA = IC*960+IW
        GO TO 10
C
C----   DATA
    4   J = J+1
C       J COUNTS NUMBER OF HITS.
        IF(J.GT.JMAX) GO TO 90
        HLGA(J) = HA
        J = J+1
        HLGA(J) = HDATA(I+NNP)
C----   CHECK THE BOUNDARY OF THE DETECTOR PARTS.
    5   IF(HA.LT.JBT(JP)) GO TO 6
C----   INCREASE POINTER AND REPEAT THE BOUNDARY CHECK.
C----   HPOINT POINTS INTEGER *2 WORD ARRAY NOT THE # OF ADD-DATA PAIR.
        HPOINT(JP) = J-1
        JP = JP+1
        IF(JP-4) 92,5,5
    6   HA = HA+1
   10   CONTINUE
      GO TO 20
C
C---- BUFFER OVERFLOW
   90 IER = 1
      CALL MESSAG( 1)
C
C---- STORE RESULTS
   20   DO 21 KJP=JP,4
   21   HPOINT(KJP) = J+1
      NLNG = J/2+3
C
C---- REPLACE THE 'ALGL'/0 BANK.
C
      NL = NLNG-LNG
      CALL BCHM( NP, NL, IERB)
      IF(IERB) 30,30,94
   30 CALL BSTR( NP, HNORD, NLNG )
      RETURN
C
C---- ADDRESS OVER THE UPPER BOUNDARY.
   92 IER = 2
      CALL MESSAG(2)
      RETURN
C
C---- NOT ENOUGH SPACE IN THE DATA BUFFER
   94 IER = 3
      RETURN
C
C
C
C---- FORMAT IS ALREADY CHANGED IN NORD.
C     CHECK THE POINTERS
  200 IF(HDATA(NNP+3).NE.1) GO TO 290
      IPO = 1
        DO 201 K=1,3
        IPK = HDATA(NNP+K+3)
        IF(IPK.LT.IPO) GO TO 290
        IF(MOD(IPK,2).NE.1) GO TO 290
        IPO = IPK
  201   CONTINUE
      IF((IPO-1)/2+3.NE.LNG) GO TO 290
      GO TO 100
C
C---- POINTER FORMAT ERROR
  290 KS = NNP+1
      KL = NNP+6
      WRITE(6,692) (HDATA(KK),KK=KS,KL)
  692 FORMAT('0***** ERROR IN LG-DATA ***** 1-ST 6 WORDS=',6I7)
      IER = 4
  100 RETURN
      END
