      SUBROUTINE DIV2CH (A,NOA,LIM,SAMTIM,HELJD,NORUN,TITLE)
C     
C     THIS SUBROUTINE IS AN ADAPTATION OF DISKRQ AND READS IN THE
C     SECOND CHANNEL DATA FOR A GIVEN RUN AND DIVIDES THE ARRAY Q
C     BY THE SECOND CHANNEL.
C     ***** INPUT DATA *****  
C     1 CARD IN FREE FORMAT WITH THE FILE NAME CONTAINING THE CH 2
C       DATA.
C
      DIMENSION A(1), IDATA(80), XDATA(80)
      DIMENSION SAMTIM(1),LIM(2,1)  
      CHARACTER*1 IFORMA*78,HEAD*36,FNAME*25,TITLE*80       
      DOUBLE PRECISION HELJD(1),HJD1     
      IFORMA='(10I7)'    
      NCARD=10  
      LOG=11
      PRINT 1     
    1 FORMAT(12X,'NORMALIZING BY SECOND CHANNEL DATA:'//
     *      12X,'NO OF  START    END   SMPL        H.J.D.  NO IN'
     *      ,'  TITLE'/ 
     *      12X,'  PTS     PT     PT   TIME                 FILE') 
      PRINT 3     
    3 FORMAT(12X,88('-'))   
    2 IRUN=IRUN+1
      IF(IRUN.GT.NORUN)RETURN
      READ(*,'(A)')FNAME
      IF(FNAME.EQ.' ')THEN
        PRINT *,' '
        GO TO 2
      END IF
      OPEN(LOG,FILE=FNAME,STATUS='OLD')     
C     
C     READ THE DATA FILE HEADER RECORD    
C     
      READ(LOG,15)HEAD,IHJD,HJD,DELTUA,NINFIL,IFACT   
   15 FORMAT(A36,I10,F8.7,6X,F8.1,2X,2I5) 
      HJD1=IHJD   
      HJD1=HJD1+HJD     
C     
C     PRINT THE INFORMATION GLEANED SO FAR
C     
      PRINT 25,NOA,LIM(1,IRUN),LIM(2,IRUN),DELTUA,HJD1,NINFIL,    
     *         HEAD
   25 FORMAT(12X,I5,2I7,F7.1,F14.7,I7,2X,A36)   
      PRINT 3   
      PRINT *,' '     

      K=LIM(1,IRUN)-1
      DATMAX=0.0
   30 J=K+1 
      K=K+NCARD   
      IF(K.GT.LIM(2,IRUN))K=LIM(2,IRUN) 
      READ(LOG,IFORMA)(IDATA(I),I=1,NCARD)    
      L=0   
      DO 60 I=J,K 
        L=L+1     
        IF(IDATA(L).EQ.0)PRINT   50,I,L,A(I),NOA,J,K
   50   FORMAT(' I= ',I5,'  L= ',I5,'  A(I) = ',F6.1,'  NOA= ',I4,
     *         '  J= ',I4,' K= ',I4)
        A(I)=A(I)/IDATA(L)  
        IF(DATMAX.LT.IDATA(L))DATMAX=IDATA(L)
   60 CONTINUE    
      IF(K.LT.LIM(2,IRUN))GO TO 30

      CLOSE(LOG)
C
C     NOW FIGURE OUT HOW MANY PHOTONS WERE DETECTED CORRESPONDING
C     TO THE MAXIMUM OF THE SECOND CHANNEL DATA. THIS NUMBER IS
C     DATMAX (THE MAXIMUM COUNT RATE IN CNTS/SEC, MULTIPLIED BY
C     THE SAMPLING TIME AND DIVIDED BY 10.0**(0.4x0.3xSecZ)
C     WHICH IS THE CORRECTION FOR ATMOSPHERIC EXTINCTION WITH AN
C     EXTINCTION COEFFICIENT OF 0.3. CHOOSE SECZ=1.0.
C
      DETPHT=DATMAX*DELTUA/(10.0**(0.12))
C
C     CALCULATE THE POISSON ERROR
C
      SIG=SQRT(DETPHT)
C
C     NOW ASSUME THAT THE MAXIMUM OF THE SECOND CHANNEL DATA CORRESPONDS
C     TO A POSITIVE EXCURSION ABOUT THE MEAN COUNT RATE OF 
C         MEAN+3Sigma
C     SO CALCULATE THE MEAN CORRESPONDING MEAN COUNT RATE BY SCALING
C     DATMAX DOWN BY MEAN/(MEAN+3Sigma)
C
      AVEPHT=DETPHT-3.0*SIG
      SCALE=AVEPHT/DETPHT
      DO I=LIM(1,IRUN),LIM(2,IRUN)
        A(I)=A(I)*DATMAX*SCALE
      END DO
      PRINT 70,DATMAX,DETPHT,SIG,AVEPHT,SCALE
   70 FORMAT(12X,'Q DIVIDED BY SECOND CHANNEL DATA BUT SCALED UP'/
     *       12X,'BY THE MAXIMUM SECOND CHANNEL VALUE OF ',F10.1/
     *       12X,'CORRESPONDING TO A DETECTED PHOT NO OF ',F10.1/
     *       12X,'AND POISSON STANDARD DEVIATION OF      ',F10.1/
     *       12X,'GIVING A MEAN DETECTED PHOTON RATE OF  ',F10.1/
     *       12X,'SO THAT Q IS SCALED DOWN BY A FACTOR   ',F10.5/)
      GO TO 2
      END   
