      SUBROUTINE CCDIN(Q,TQ,NQ,NORUN,SAMTIM,LIM,HELJD)     
C     
C     ROUTINE TO READ IN CCD DATA WITH COMPARISON STARS
C     AND FORM DIFFERENTIAL MAGNITUDES.
C     
      DIMENSION Q(1),SAMTIM(1),LIM(2,1)
      DIMENSION COMPST(5),CMPMAG(5),AVCOMP(5)
      DOUBLE PRECISION TQ(1),HELJD(1),TIME
      CHARACTER FNAME*25,FORMAT*80  
      FORMAT='(F15.6,F15.7)'
      IHEAD=0
      ISEC=0
      NCOMP=0
    1 READ 4,FNAME
      IF(FNAME.EQ.'BEGIN')THEN
        NORUN=0
        LIM(1,1)=0
        NQ=0
        GO TO 1
      END IF
      IF(FNAME.EQ.'COMPAR')THEN
C
C       READ IN THE NUMBER OF COMPARISON STARS AND THEIR 
C       MAGNITUDES
C
        READ *,NCOMP,(CMPMAG(J),J=1,NCOMP)
        GO TO 1
      END IF
      IF(FNAME.EQ.'FORMAT')THEN
        READ 4,FORMAT
C
C       CHECK IF THERE IS AN * IN THE FORMAT WHICH INDICATES THAT THERE
C       IS A HEADER RECORD CONTAINING THE NUMBER OF LINES IN THE FILE
C
        DO J=1,80
          IF(FORMAT(J:J).EQ.'*')THEN
            IHEAD=1
            PRINT *,'           FILE CONTAINS NUMBER OF RECORDS'
            DO K=J,79
              FORMAT(K:K)=FORMAT(K+1:K+1)
            END DO
          END IF
        END DO
C
C       CHECK IF THERE IS A # IN THE FORMAT WHICH INDICATES THAT THE
C       TIME IS NOT IN DAYS (DEFAULT) BUT IN SECONDS
C
        DO J=1,80
          IF(FORMAT(J:J).EQ.'#')THEN
            ISEC=1
            PRINT *,'           FILE CONTAINS TIME IN SECONDS'
            DO K=J,79
              FORMAT(K:K)=FORMAT(K+1:K+1)
            END DO  
          END IF
        END DO
        PRINT 3,FORMAT     
    3   FORMAT('            READ FORMAT IS ',A)
        GO TO 1
      END IF
    4 FORMAT(A)   
    5 IF(FNAME.EQ.'END')THEN  
        RETURN    
      END IF
      OPEN(11,FILE=FNAME,STATUS='OLD')     
      AMEAN=0.0
      DO J=1,5
        AVCOMP(J)=0.0
      END DO
      AMX=-1.0E35
      AMN=1.0E35
      NORUN=NORUN+1     
      NQ=NQ+1     
      LIM(1,NORUN)=NQ   
      IPT=0 
C
C     IF THERE IS A HEADER LINE TO BE SKIPPED, SKIP IT!
C
      IF(IHEAD.EQ.1)READ(11,*)JUNK
   10 READ(11,FORMAT,END=50)TIME,Q(NQ),(COMPST(J),J=1,NCOMP)
      IPT=IPT+1   
      IF(ISEC.EQ.0)THEN
        IF(IPT.EQ.1)HELJD(NORUN)=TIME 
        TQ(NQ)=(TIME-HELJD(1))*86400.0D0    
      ELSE
        IF(IPT.EQ.1)HELJD(NORUN)=0.0
        TQ(NQ)=(TIME-HELJD(1))    
      END IF
      IF(IPT.EQ.2)THEN  
        SAMTIM(NORUN)=TQ(NQ)-TQ(NQ-1)     
C       SET A NOMINAL SAMPLING TIME FOR SAMPLING  TIMES WHICH ARE TOO LARGE
        IF(SAMTIM(NORUN).GT.9999.0)SAMTIM(NORUN)=9999.0
      END IF
      DO J=1,NCOMP
        AVCOMP(J)=AVCOMP(J)+COMPST(J)
        Q(NQ)=Q(NQ)-(COMPST(J)-CMPMAG(J))/NCOMP
      END DO
      IF(AMX.LT.Q(NQ))AMX=Q(NQ)
      IF(AMN.GT.Q(NQ))AMN=Q(NQ)
      AMEAN=AMEAN+Q(NQ)
      NQ=NQ+1     
      GO TO 10    
C     
   50 NQ=NQ-1     
      LIM(2,NORUN)=NQ   
      AMEAN=AMEAN/IPT
      DO J=1,NCOMP
        AVCOMP(J)=AVCOMP(J)/IPT
      END DO
      PRINT 100,IPT,FNAME,HELJD(NORUN),SAMTIM(NORUN),AMN,AMX,AMEAN
  100 FORMAT(/12X,I4,' POINTS READ FROM ',A12,' FROM HJD ',  
     *        F10.5,'  DEL T ',F7.2/
     *        12X,4X,' MIN ',F9.3,'      MAX ',F9.3,'      MEAN ',F9.3)
      IF(NCOMP.GT.0)PRINT 110,NCOMP,(CMPMAG(J),AVCOMP(J),J=1,NCOMP)
  110 FORMAT(12X,I4,' COMPARISON STARS WITH KNOWN AND AVE. MAGNITUDES:'/
     *       12X,4X,5(F9.3,F9.3,2X)) 
      REWIND 11   
      CLOSE(11)   
      GO TO 1     
      END   
