      SUBROUTINE TIMBAS(TQ,NQ,HELJD,LIM,NORUN,EPOCH,EPER,ESEC,FREMAX,   
     *                  ITPHAS,HJDMAX)     
C     
C     ROUTINE TO REDEFINE THE TIMES IN TQ.  THE INPUT CONSISTS OF THE   
C     EPOCH WHICH, IF NOT 999, IS THE NEW ZERO POINT OF THE TIME BASE.  
C     IF EPOCH IS 999, THEN A SECOND READ OF EPOCH, EPER, ESEC IS NEEDED
C     WHICH INPUTS AN EPHEMERIS AND THE TIMES BECOME CYCLES AND PHASES  
C     RELATIVE TO THIS EPHEMERIS.  IN THIS CASE ITPHAS=1.   
C     IF EPOCH = -999, THEN FOR EACH RUN, INPUT A DT WHICH MUST BE ADDED
C     ON TO ALL THE TIMES. THIS SEPARATE TREATMENT OF THE RUNS IS TO
C     ACHIEVE PLOTS ON THE SAME FRAME FOR DIFFERENT RUNS WITH AN
C     ARBITRARY ZERO PT FOR EACH RUN.
C     
      DIMENSION LIM(2,NORUN)
      DOUBLE PRECISION TQ(NQ),TTT,HELJD(NORUN),EPOCH,EPER,ESEC,CYC,DT
      DOUBLE PRECISION HJDMAX,PLONG,EPLONG,DTLONG,ARG,TWOPI

      TWOPI=2.0*3.141592653d0
      PLONG=0.0
      EPLONG=0.0
      DTLONG=0.0

      READ *,EPOCH
      IF(EPOCH.EQ.999.0D0)GO TO 65  
      IF(EPOCH.EQ.-999.0D0)GO TO 100
      TTT=(HELJD(1)-EPOCH)*86400.0D0
      PRINT 10,EPOCH,TTT
   10 FORMAT(12X,'RE-SETTING THE TIME BASE RELATIVE TO HJD ',F15.5/     
     *       12X,'TIME DIFFERENCE FOR CORRECTING IS        ',F15.3)     
      DO 20 J=1,NQ
   20 TQ(J)=TQ(J)+TTT   
      PRINT 30    
   30 FORMAT(//12X,'RUN',6X,'NEW HJD',6X,'BEGIN',4X,'T(BEGIN)',5X,
     *       '  END',6X,'T(END)'/12X,61('-'))   
      DO 50 J=1,NORUN   
        HELJD(J)=HELJD(J)-TTT/86400.0D0   
        PRINT 40,J,HELJD(J),LIM(1,J),TQ(LIM(1,J)),LIM(2,J), 
     *             TQ(LIM(2,J))     
   40   FORMAT(12X,I3,F13.5,I11,F12.3,I10,F12.3)
   50 CONTINUE    
      PRINT 60    
   60 FORMAT(12X,61('-'))     
      RETURN
C     
   65 CONTINUE    
C     NOTE: IF IC=1, THEN ALL THE INTEGER CYCLES ARE SUBTRACTED
C     NOTE: IF IC=2, THEN ALL THE INTEGER CYCLES ARE SUBTRACTED
C                    AND PHASES 0-0.5 ARE MAPPED TO 1-1.5
C     NOTE: IF IC=3, THEN ALL THE INTEGER CYCLES ARE SUBTRACTED
C                    AND PHASES 0.5-1 ARE MAPPED TO -0.5 - 0
C
C     IF EPER < 0, EPER=-EPER AND A LONG PERIOD, AMPLITUDE AND
C     EPOCH IS READ IN AND USED IN THE EPHEMERIS.
C
      READ *,EPOCH,EPER,ESEC,IC
      IF(ABS(EPOCH-999.0D0).LT.0.1)THEN   
        EPER=1.0/FREMAX/86400.0D0   
        ESEC=0.0D0
        IF(HJDMAX.EQ.0.0D0)THEN
          EPOCH=HELJD(1)  
        ELSE
          EPOCH=HJDMAX
        END IF
      END IF
      IF(EPER.LT.0.0)THEN
        EPER=-EPER
        READ *,PLONG,EPLONG,DTLONG
      END IF
      PRINT 70,EPOCH,EPER,ESEC
   70 FORMAT(12X,'TIME BASE RELATIVE TO EPHEMERIS :'/ 
     *       12X,'EPOCH : ',T30,F15.5/12X,'PERIOD : ',T30,1PE15.7/
     *       12X,'QUADRATIC : ',0PF15.5/) 
      IF(IC.GE.1)PRINT 72
   72 FORMAT(12X,'ONLY FRACTIONAL CYCLES RETAINED')
      IF(PLONG.GT.0.0)PRINT 75,PLONG,EPLONG,DTLONG
   75 FORMAT(12X,'LONG PERIOD EPHEMERIS INCLUDED:'/
     *       14X,'PERIOD :    ',F15.1/
     *       14X,'EPOCH(MAX) :',F15.5/
     *       14X,'DT(DAYS) :  ',F15.5)
      TTT=HELJD(1)+TQ(1)/86400.0D0  
      IF(PLONG.GT.0.0)THEN
        ARG=DMOD(TWOPI*(TTT-EPLONG)/PLONG,TWOPI)
        TTT=TTT-DTLONG*DCOS(ARG)
      END IF
      IF(ESEC.EQ.0.0D0)THEN   
        CYC=(TTT-EPOCH)/EPER  
      ELSE  
        CYC=(-EPER+SQRT(EPER*EPER-4.0*ESEC*(EPOCH-TTT)))/   
     *    (2.0*ESEC)    
      END IF
      ICYC=CYC    
      DO 90 J=1,NQ
        TTT=HELJD(1)+TQ(J)/86400.0D0
        IF(PLONG.GT.0.0)THEN
          ARG=DMOD(TWOPI*(TTT-EPLONG)/PLONG,TWOPI)
          TTT=TTT-DTLONG*DCOS(ARG)
        END IF
        IF(ESEC.EQ.0.0D0)THEN 
          CYC=(TTT-EPOCH)/EPER
        ELSE
          CYC=(-EPER+SQRT(EPER*EPER-4.0*ESEC*(EPOCH-TTT)))/ 
     *      (2.0*ESEC)  
        END IF    
        CYC=CYC-ICYC    
        TQ(J)=CYC 
        IF(IC.GE.1)TQ(J)=CYC-INT(CYC)
   80   CONTINUE
        IF(IC.EQ.2.AND.TQ(J).LT.0.5)THEN
          TQ(J)=TQ(J)+1.0
          GO TO 80
        END IF
   85   CONTINUE
        IF(IC.EQ.3.AND.TQ(J).GT.0.5)THEN
          TQ(J)=TQ(J)-1.0
          GO TO 85
        END IF
   90 CONTINUE    
      PRINT 30    
      DO 95 J=1,NORUN   
        INTCYC=TQ(LIM(1,J))+ICYC    
        HELJD(J)=INTCYC 
        IF(IC.EQ.3.AND.J.GT.1)HELJD(J)=HELJD(1)
        PRINT 40,J,HELJD(J),LIM(1,J),TQ(LIM(1,J)),LIM(2,J), 
     *             TQ(LIM(2,J))     
   95 CONTINUE    
      PRINT 60    
      ITPHAS=1    
      RETURN

  100 CONTINUE

      PRINT 230    
      DO J=1,NORUN   
        PRINT 40,J,HELJD(J),LIM(1,J),TQ(LIM(1,J)),LIM(2,J), 
     *             TQ(LIM(2,J))     
      END DO      
      PRINT 60    

      DO 200 J=1,NORUN
        READ *,DT
        IF(ITPHAS.EQ.0)PRINT 120,J,DT
        IF(ITPHAS.EQ.1)PRINT 130,J,DT
  120   FORMAT(12X,'FOR RUN ',I3,' TIME OFFSET IS ',F13.6,' DAYS')
  130   FORMAT(12X,'FOR RUN ',I3,' TIME OFFSET IS ',F13.6,' CYCLES')
        DO 160 K=LIM(1,J),LIM(2,J)
          IF(ITPHAS.EQ.0)TQ(K)=TQ(K)+DT*86400.0D0
          IF(ITPHAS.EQ.1)TQ(K)=TQ(K)+DT
  160   CONTINUE
  200 CONTINUE

      PRINT 230    
  230 FORMAT(//12X,'RUN',6X,'  HJD  ',6X,'BEGIN',4X,'T(BEGIN)',5X,
     *       '  END',6X,'T(END)'/12X,61('-'))   
      DO J=1,NORUN   
        PRINT 40,J,HELJD(J),LIM(1,J),TQ(LIM(1,J)),LIM(2,J), 
     *             TQ(LIM(2,J))     
      END DO      
      PRINT 60    

      RETURN
      END   
