      SUBROUTINE GENRED(Q,TQ,NQ,NORUN,SAMTIM,LIM,HELJD,ITPHAS)     
C     
C     ROUTINE TO READ IN DON KURTZ FORMATTED DATA     
C     
      DIMENSION Q(1),SAMTIM(1),LIM(2,1),VAL(20)   
      DOUBLE PRECISION TQ(1),HELJD(1),TIME,HJDZRO
      CHARACTER FNAME*25,FORMAT*80  

      HJDZRO=0.0D0
      IFREE=0
      ICOL=0
      FORMAT='(F15.6,F15.7)'
      IHEAD=0
      ISEC=0
    1 READ 4,FNAME
      IF(FNAME.EQ.'BEGIN'.OR.
     *   FNAME.EQ.'begin')THEN
        NORUN=0
        LIM(1,1)=0
        NQ=0
        ITPHAS=0
        GO TO 1
      END IF
      IF(FNAME.EQ.'FORMAT'.or.
     *   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.'#'.OR.FORMAT(J:J).EQ.'!')THEN
            ISEC=1
            IF(FORMAT(J:J).EQ.'!') READ *,HJDZRO
            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)
C
C       CHECK IF THE FORMAT HAS THE STRING 'FREE' IN IT. IF SO, IT  
C       MEANS THAT IT IS A FREE FORMAT READ IN WHICH CASE THE
C       TIME IS ASSUMED TO COME FIRST AND AN INTEGER IS NEEDED
C       TO SPECIFY WHAT COLUMN THE ORDINATE IS IN
C
        DO J=1,80
          IF(FORMAT(J:J+3).EQ.'FREE'.OR. 
     *       FORMAT(J:J+3).EQ.'free')THEN
            IFREE=1
            PRINT *,'           FREE FORMAT READING'
            READ *,ICOL
            PRINT *,'           DATA IN COLUMN ',ICOL
          END IF
        END DO
        GO TO 1
      END IF
    4 FORMAT(A)   
    5 IF(FNAME.EQ.'END'.OR.
     *   FNAME.EQ.'end')THEN  
        PRINT 6,NQ
    6   FORMAT(12X,'TOTAL NUMBER OF POINTS READ IS ',I7)
        RETURN    
      END IF
      OPEN(11,FILE=FNAME,STATUS='OLD')     
      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 IF(IFREE.EQ.0)THEN
        READ(11,FORMAT,END=50,ERR=10)TIME,Q(NQ)    
      ELSE
        READ(11,*,END=50,ERR=10)TIME,(VAL(IJ),IJ=1,ICOL-1),Q(NQ)
      END IF
      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.AND.HJDZRO.EQ.0.0)HELJD(NORUN)=0.0
        IF(IPT.EQ.1.AND.HJDZRO.GT.0.0)HELJD(NORUN)=HJDZRO
        TQ(NQ)=TIME
      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
c     IF(IPT.GT.2.AND.TQ(NQ).LT.TQ(NQ-1).AND.TQ(NQ-1).GT.85000.0)
c    *  TQ(NQ)=TQ(NQ)+86400.0
      IF(AMX.LT.Q(NQ))AMX=Q(NQ)
      IF(AMN.GT.Q(NQ))AMN=Q(NQ)
      NQ=NQ+1     
      GO TO 10    
C     
   50 NQ=NQ-1     
      LIM(2,NORUN)=NQ   
      PRINT 100,IPT,FNAME,HELJD(NORUN),SAMTIM(NORUN),AMN,AMX
  100 FORMAT(12X,I5,' POINTS READ FROM FILE ',A12,' FROM HJD ',  
     *       F11.5,'  SAMPLING TIME ',F7.2,'  MIN ',1PE12.5,'  MAX ',
     *       E12.5)
      REWIND 11   
      CLOSE(11)   
      GO TO 1     
      END   
