C   27/02/80 403191842  MEMBER NAME  SUBOLD   (S)           FORTRAN
      SUBROUTINE SUBOLD(INIT)
C---  SUBTRACT ADC-PEDESTALS (MICROPROCESSOR VALUES)
C---  & CREATE NEW VALUES FOR LNG, HPOINT, AND HGGADC
C---  (DEALS WITH 240 ADC-CHANNELS)
C---  READ PEDESTAL-CONSTANTS FROM DATASET
C
C     H.WRIEDT     12.06.79       14:00
C     LAST MODIFICATION     27.02.80      23:30
C
      IMPLICIT INTEGER*2 (H)
C
      COMMON /CPHASE/ LIMEVT,KCONST(192),LIMIT(192),LCHANN(192),
     &                LTHRES(192),LDEAD(192),LSUB(240)
      LOGICAL LCHANN,LTHRES,LDEAD,LSUB
C
      COMMON /CADCPL/ HADCPL(102,240),HPBHIS(192),IPBPHD(192),
     &                IERR(3)
C
      COMMON /CWORK/ IWORK(42),LNG,HPOINT(4),HGGADC(2,240)
C
      COMMON /CSUBCP/ HSUBPD(240)
C
      COMMON /CRUNCO/ HRUN
C
      INTEGER*2 HGGPED(2,240)
C

      DATA ICOUNT/0/

      DATA IDONT/0/
      IER = 0

C
      IF (INIT.GT.1) GOTO 2
    1 READ(19,ERR=999,END=1000) IRUN,NDAT
      IF (NDAT.GT.0) READ(19,ERR=999,END=1000)
     1((HGGPED(K,J),K=1,2),J=1,NDAT)
      IF (IRUN.GT.HRUN) GOTO 300
      IF (IRUN.LT.HRUN) GOTO 1
      IF (NDAT.EQ.0) GOTO 301
        DO 50 I = 1,NDAT
        HADR = HGGPED(1,I)
C---    STORE PEDESTAL-CONSTANT FOR EACH CHANNEL
   11   HSUBPD(HADR) = HGGPED(2,I)
C       WRITE(6,603) HGGPED(1,I),HSUBPD(HADR),INIT
  603   FORMAT(' SUBTRACTION-CONSTANT FOR BLOCK NO. ',I3,' DETERMINED',
     &         ' TO ',I5,' IN EVENT NO. ',I3)
   50   CONTINUE
C
    2 NDAT = LNG - 2
        DO 100 I = 1,NDAT
        HADR = HGGADC(1,I)
C---
        IF (HADR.EQ.0.OR.HADR.EQ.47.OR.HADR.EQ.48.OR.HADR.EQ.95.OR.HADR
     &      .EQ.96.OR.HADR.EQ.143.OR.HADR.EQ.144.OR.HADR.EQ.191) GOTO 9900003160
        HGGADC(2,I) = HGGADC(2,I) + HSUBPD(HADR)
        IF (HGGADC(2,I).GE.50) GOTO 101
C---    CUTOFF OF ADCS WITH LOW CONTENT
C       (THRESHOLD: 49 CHANNELS)
   99   HGGADC(2,I) = 0
        GOTO 100
C---    SCRATCH UNPHYSICAL ADC-ADDRESSES (I.E. ADDRESSES 208 - 215,
C---    >229) AND CONTENTS
  101   IF ((HADR.GE.208.AND.HADR.LE.215).OR.(HADR.GE.230))
     &   HGGADC(2,I) = 0
  100   CONTINUE
C
      CALL GGSRTH(0,NDAT)
      LNG = 2
        DO 200 I = 1,NDAT
        IF (HGGADC(2,I).LE.0) GOTO 201
  200   LNG = I+2
  201 NDAT = LNG-2
      CALL GGSORT
C
C---  SET UP NEW POINTERS
      HPOINT(1) = 1
      HPOINT(2) = 1
      HPOINT(3) = 1
      IF (NDAT.LE.0) GOTO 211
        DO 210 I = 1,NDAT
        HADDR = HGGADC(1,I)
        IF (HADDR.LE.95) HPOINT(2) = 2*I+1
        IF (HADDR.LE.191) HPOINT(3) = 2*I+1
  210   CONTINUE
  211 HPOINT(4) = 2*NDAT+1
      RETURN
C
  300 IF(ICOUNT.LT.10)WRITE(6,600) IRUN,HRUN
  600 FORMAT(/' *** ERROR IN SUBOLD DURING READING OF PEDESTAL',
     &        '-CONSTANTS'/'    CORRECT RUN NOT FOUND'/2(2X,I6))
      IF (IER.EQ.1) GOTO 599
      REWIND 19
      IER = 1
      GOTO 1
  599 RETURN
C
  301 IF(ICOUNT.LT.10)WRITE(6,601) IRUN,NDAT
       ICOUNT = ICOUNT + 1
  601 FORMAT(/' *** ERROR IN SUBOLD DURING READING OF PEDESTAL',
     &        '-CONSTANTS'/'    NO PEDESTAL-CONSTANTS FOUND'/2(2X,I6))
      RETURN
C AJF ADDED THESE ERROR MESSAGES
999   WRITE(6,699)
699   FORMAT(/,/,'  READ ERROR IN SUBOLD ON UNIT 19 (TAG ''79 CALIBRATIO
     1N FILE) REST OF THIS CALIBRATION SKIPPED ')
      IDONT = 1
      RETURN
1000  WRITE(6,700)
700   FORMAT(/,/,' END OF DATA ENCOUNTERED WHILE  READING TAG 79
     1CALIBRATION FILE ')
      IDONT = 1
      RETURN
      END
