      SUBROUTINE FOLDAM(AT,AF,AA,SIGA,IPART,AJDH,IND,E,TITLE) 
C     
C     SUBROUTINE TO FOLD THE CALCULATED AMPLITUDE MAXIMA    
C     BACK ON TO EACH OTHER AT SPECIFIED PERIODS
C     
      COMMON /PLOTFL/PLNAME
      DIMENSION AT(1),AA(1),BIN(150,100),NBIN(100),NUMB(63),LINE(60)    
      DIMENSION AF(2,1),SIGA(1),E(1)
      DIMENSION IND(1),XXX(202),YYY(202),DUM(4) 
      CHARACTER*1 CH,NUMB,LINE,TITLE*80,PLNAME*12
      DOUBLE PRECISION AJDH,EPOCH,P,PHASE,PMIN,PMAX,DP
      NAMELIST /III/I,J,NOBIN,NB,NOP
      NAMELIST /PPP/IPHASE,PHASE,P,DPHASE 
      DATA NUMB/'A','B','C','D','E','F','G','H','I','J','K','L',
     *'M','N','O','P','Q','R','S','T','U','V','W','X',
     *'Y','Z','1','2','3','4','5','6','7','8','9','0',
     *'@','$','#','*','=','+',':',';','<','>',',','.',
     *'?','}','~','|','^','{',']','[','!','?','(',')',
     *'&','%','!'/  
      PAV=0.0     
      EPOCH=AJDH  
      MXINBN=50   
      DO 1 J=1,IPART    
        PAV=PAV+AF(2,J) 
    1 CONTINUE    
      PAV=PAV/IPART     
      AMAX=0.0    
      READ *,PMIN,PMAX,DP,AMAX,NB,XL,YL,IEB,IFIT
      IF(PMIN.EQ.999.0D0)THEN 
        READ *,EPOCH,P  
        PMIN=P    
        PMAX=P    
        DP=1.0    
      END IF
      IF(XL.EQ.0.0)XL=8.0     
      IF(YL.EQ.0.0)YL=6.0     
      NOP=(PMAX-PMIN)/DP+1    
      IF(NB.LE.0.OR.NB.GT.50)NB=50  
      IF(AMAX.LE.0.0)THEN     
        DO 10 J=1,IPART 
          IF(AA(J).GT.AMAX)AMAX=AA(J)     
   10   CONTINUE  
        DUM(1)=0.0
        DUM(2)=AMAX     
c       CALL SCALE(DUM,YL,2,1)
        DADY=DUM(4)     
      ELSE  
        DADY=AMAX/YL    
      END IF
   15 PRINT 20,PMIN,PMAX,DP,NOP,NB  
   20 FORMAT(12X,'PMIN ',F10.7,' DAYS  ','   PMAX ',F10.7,
     *       ' DAYS     DP',     
     *       F10.7,' DAYS  ','  NUMBER OF PERIODS ',I3,
     *       ' AND OF BINS'
     *       ,I3///)    
      IF(EPOCH.NE.AJDH)PRINT 25,EPOCH     
   25 FORMAT(12X,'PHASE ZERO IS HJD ',F15.5)    
      P=PMIN-DP   
      DO 1000 I=1,1000  
        P=P+DP    
        FP=P
        IF(P.GT.PMAX)RETURN   
        IF(IFIT.EQ.0)GO TO 36 
        YC=0.0    
        YS=0.0    
        SS=0.0    
        CC=0.0    
        SC=0.0    
        AMN=0.0   
        DO 29 J=1,IPART 
          AMN=AMN+AA(J)*AA(J)/IPART 
   29   CONTINUE  
        DO 30 J=1,IPART 
          ARG=(AT(J)/86400.0D0+AJDH-EPOCH)*6.283185306D0/P  
          S=SIN(ARG)    
          C=COS(ARG)    
          SS=SS+S*S     
          CC=CC+C*C     
          SC=SC+S*C     
          YC=YC+(AA(J)*AA(J)-AMN)*C 
          YS=YS+(AA(J)*AA(J)-AMN)*S 
   30   CONTINUE  
        PPPP=(YS*SC-YC*SS)/(SC*SC-CC*SS)  
        QQQQ=(YC*SC-YS*CC)/(SC*SC-CC*SS)  
        AMPL=SQRT(PPPP**2+QQQQ**2)  
        PHI=ATAN2(-QQQQ,PPPP) 
        PRINT 35,AMN,AMPL,PHI,P     
   35   FORMAT(///12X,5HMEAN=,1PE12.5,2X,4HAMP=,1PE12.5,6H  PHI=,E12.5, 
     *       9H  PERIOD=,E12.5)     
   36   CONTINUE
C
C       INITIALISE THE PLOTTING
C
c       CALL SETCC
c       CALL PLFILE(PLNAME)
c       CALL PLOTS(IERR,IDUMMY,28)
c       IF(IERR.NE.0) STOP 'ERROR ON OPENING SPOOLTEK CALL'
c       CALL FACTOR(2.54) 
c       CALL FRAME(XL+2.5,YL+3.0)
c       CALL PLOT(1.0,1.0,-3) 
        DPHDX=2.0/XL    
c       CALL AXIS(0.0,0.0,'PHASE',-5,XL,0.0,0.0,DPHDX)
c       CALL AXIS(0.0,0.0,'AMPLITUDE',9,YL,90.0,0.0,DADY)   
c       CALL SYMBOL(0.0,YL+1.5,0.2,TITLE,0.0,72)   
c       CALL SYMBOL(0.0,YL+1.0,0.14,     
c    *              'AMP VARIATION AT           D FROM HJD ',0.0,38)   
c       CALL SYMBOL(6.30,YL+1.0,0.14,' FOR THE      S PERIOD',0.0,22) 
c       CALL NUMBER(2.38,YL+1.0,0.14,FP,0.0,7) 
        AJD=AJDH  
c       CALL NUMBER(5.32,YL+1.0,0.14,AJD,0.0,2)
c       CALL NUMBER(7.56,YL+1.0,0.14,PAV,0.0,1)
c       CALL PLOT(XL,0.0,3)   
c       CALL PLOT(XL,YL,2)    
c       CALL PLOT(0.0,YL,2)   
   40   DO 100 J=1,NB   
          DO 50 K=1,MXINBN*3  
            BIN(K,J)=0.0
   50     CONTINUE
          NBIN(J)=0     
          IF(J.LE.60)LINE(J)=' '    
  100   CONTINUE  
        DPHASE=1.0/NB   
        DO 300 J=1,IPART
          IF(IEB.EQ.-1.AND.AA(J)/SIGA(J).LT.2.0)GO TO 300   
          IPHASE=(AT(J)/86400.0D0+AJDH-EPOCH)/P 
          PHASE=(AT(J)/86400.0D0+AJDH-EPOCH)/P-IPHASE 
          NOBIN=PHASE/DPHASE+1
          NOBIN=MIN0(MAX0(1,NOBIN),NB)    
          NBIN(NOBIN)=NBIN(NOBIN)+1 
          DO 200 K=1,2*MXINBN-1,3   
            IF(BIN(K,NOBIN).GT.0.0)GO TO 200    
            BIN(K,NOBIN)=AA(J)
            BIN(K+1,NOBIN)=IND(J)   
            BIN(K+2,NOBIN)=SIGA(J)  
            GO TO 300   
  200     CONTINUE
  300   CONTINUE  
        PRINT 350,P,AJDH,PAV  
  350   FORMAT('1',11X,92('-')/12X,'I',4X,23HAMPLITUDE VARIATION AT ,   
     *         F10.7,' D FROM HJD ',F14.5,9H FOR THE ,0PF6.1,9H S PERIOD
     *         ,3X,'I'/12X,92('-')/12X,'I',6H PHASE,67X,    
     *         'A',7X,6HNUMBER,3X,'I'/12X,92('-'))    
        STEP=60.0/AMAX  
        DO 500 JJ=1,NB*2
          J=JJ    
          IF(JJ.GT.NB)J=J-NB  
          PHASE=FLOAT((JJ-1))/FLOAT(NB)   
          JKJK=SQRT(AMN+AMPL*COS(6.283185306D0*PHASE+PHI))*STEP   
          JKJK=MIN0(MAX0(1,JKJK),60)
          IF(IFIT.EQ.1)LINE(JKJK)='+'     
          IF(NBIN(J).GT.0)GO TO 400 
          PRINT 375,PHASE,LINE
  375     FORMAT(12X,'I',F5.3,1X,'I',60A1,1X,'I',21X,'I')   
          GO TO 440     
  400     DO 415 K=1,2*MXINBN-1,3   
            IF(BIN(K,J).EQ.0.0)GO TO 420  
            INDEX=IFIX(BIN(K+1,J))  
            JJJ=STEP*BIN(K,J) 
            JJJ=MIN0(MAX0(1,JJJ),60)
            LINE(JJJ)=NUMB(MIN(INDEX,63)) 
            CH=LINE(JJJ)
            X=PHASE/DPHDX     
            Y=BIN(K,J)/DADY   
c           CALL SYMBOL(X-0.025,Y-0.025,.1,CH,0.0,1)  
            IF(IEB.EQ.0)GO TO 415   
            ERROR=BIN(K+2,J)/DADY   
            TOP=Y+ERROR 
            BOTTOM=Y-ERROR    
            IF(TOP.GT.YL)TOP=YL     
            IF(BOTTOM.LT.0.0)BOTTOM=0.0   
c           CALL PLOT(X+0.05,TOP,3) 
c           CALL PLOT(X-0.05,TOP,2) 
c           CALL PLOT(X,TOP,3)
c           CALL PLOT(X,BOTTOM,2)   
c           CALL PLOT(X+0.05,BOTTOM,3)    
c           CALL PLOT(X-0.05,BOTTOM,2)    
  415     CONTINUE
  420     PRINT 425,PHASE,LINE,BIN(1,J),NBIN(J) 
  425     FORMAT(12X,'I',F5.3,1X,'I',60A1,1X,'I',1X,1PE9.3,3X,I5,3X,'I')
  440     DO 450 K=1,60 
  450     LINE(K)=' '   
  500   CONTINUE  
        IF(IFIT.NE.1)GO TO 520 
        XXX(201)=0.0    
        YYY(201)=0.0    
        XXX(202)=DPHDX  
        YYY(202)=DADY   
        DO 505 J=1,200  
          YYY(J)=SQRT(AMN+AMPL*COS(FLOAT(J-1)/50.0*3.141593+PHI))/DADY  
          XXX(J)=FLOAT(J-1)/100.0/DPHDX   
c         IF(J.EQ.1)CALL PLOT(XXX(J),YYY(J),3)  
c         IF(J.GT.1)CALL PLOT(XXX(J),YYY(J),2)  
  505   CONTINUE  
  520   PRINT 525 
  525   FORMAT(12X,92('-'))   
        XX=XL+4.0 
c       CALL PLOT(XX,0.0,-3)  
c       CALL PLOT(0.0,0.0,999)
 1000 CONTINUE    
      PRINT 1500  
 1500 FORMAT(1X///)     
      RETURN
      END   
