      SUBROUTINE PLTRNS(XNU,FF,KD,TITLE)     
      COMMON /PGPL/PGPLDEV,IPGPL
      CHARACTER PGPLDEV*80
      COMMON /ARROWS/NARROW,FARROW(20),HARROW(20)
      DIMENSION XNU(1),FF(1),YLFILE(100),AMIN(100),AMAX(100)
      DIMENSION TOPARR(100),BOTARR(100)
      CHARACTER TITLE*80,FNAME(100)*25,INTITL*80,YOPT*20    
      CHARACTER STRING*20
      DOUBLE PRECISION HJD,HJD1
      INTEGER TRMLEN
      EXTERNAL TRMLEN

   10 FORMAT(A)
      LENTIT=TRMLEN(TITLE)

      READ *,NFILE,XLEN,FLO,FHI,YLEN,AMINFF,AMAXFF,ILOG
      IF(XLEN.LE.0.0.OR.XLEN.GT.0.8)XLEN=0.8 
      IF(YLEN.EQ.0.0.OR.YLEN.GT.0.8)YLEN=0.8 
      LABEL=0
      IF(YLEN.LT.0.0)THEN
        YLEN=-YLEN
        LABEL=1
      END IF

      PRINT 15,NFILE,TITLE,
     *         XLEN,FLO,FHI,YLEN,AMINFF,AMAXFF
   15 FORMAT(12X,'FOURIER TRANSFORMS OF ',I5,' FILES'/
     *       12X,'TITLE : ',A/
     *       12X,'X-AXIS LENGTH ',F6.3,'  FLOW: ',1PE15.7,'  FHIGH: ',
     *       1PE15.7/
     *       12X,'Y-AXIS LENGTH ',0PF6.3,'  YLOW: ',1PE15.7,'  YHIGH: ',
     *       1PE15.7/)   
      ISEP=0
      IF(AMINFF.EQ.0.0.AND.AMAXFF.EQ.0.0)THEN
        ISEP=1
        PRINT 18
   18   FORMAT(12X,'*** SEPARATE VERTICAL SCALE FOR EACH TRANSFORM'/)
        DO I=1,NFILE
          READ 10,FNAME(I)
          READ *,YLFILE(I),AMIN(I),AMAX(I)
          IF(NARROW.GT.0)READ *,TOPARR(I),BOTARR(I)
        END DO
        FFMAX=AMAX(1)
        FFMIN=AMIN(1)
        YLYL=ABS(YLFILE(1))
        DO I=2,NFILE
          YLYL=YLYL+ABS(YLFILE(I))
          FFMIN=FFMIN-(AMAX(I)-AMIN(I))
        END DO
        ARROSZ=(FFMAX-FFMIN)/25.0
      ELSE IF(ABS(AMINFF).EQ.999.0.AND.ABS(AMAXFF).EQ.999.0)THEN
        ISEP=-1
        PRINT 19
   19   FORMAT(12X,'*** SAME VERTICAL SCALE FOR EACH TRANSFORM'/)
        DO I=1,NFILE
          READ 10,FNAME(I)
          IF(AMINFF.EQ.999.0.AND.AMAXFF.EQ.999.0)THEN
C
C           AUTOMATICALLY DETERMINE THE SCALING FOR EACH FILE
C           BY READING ALL THE DATA IN
C 
            AMIN(I)=0.0      
            AMAX(I)=-1.0E30
            OPEN(11,FILE=FNAME(I),STATUS='OLD')
            READ(11,40)INTITL
            READ(11,70)NQ,HJD,KDIN,XNULO,XNUHI,DF
            KD=0
C           IWINDO=1 IF IT'S A SPECTRAL WINDOW FILE, OTHERWISE IWINDO=0
            IWINDO=0
            DO J=1,KDIN
              READ(11,80)IDUM,XNUIN,FFIN
              IF(IDUM.LT.0.OR.XNUIN.LT.0)THEN
                IWINDO=1
                IF(J.EQ.1)XWIN1=XNUIN
              END IF
              IF(IWINDO.EQ.0.AND.XNUIN.LT.FLO)GO TO 20
              IF(IWINDO.EQ.0.AND.XNUIN.GT.FHI)GO TO 22
              KD=KD+1
              XNU(KD)=XNUIN
              IF(IWINDO.EQ.1)XNU(KD)=XNU(KD)-XWIN1+FLO
              IF(ILOG.EQ.0)FF(KD)=FFIN
              IF(ILOG.NE.0)FF(KD)=ALOG10(FFIN)
              IF(AMIN(I).GT.FF(KD))AMIN(I)=FF(KD)
              IF(AMAX(I).LT.FF(KD))AMAX(I)=FF(KD)
   20       END DO  
   22       CLOSE(11)
            AMAX(I)=AMAX(I)*1.05
          ELSE 
C
C           READ IN THE SCALING FOR EACH FILE
C 
            READ *,AMIN(I),AMAX(I)
          END IF
        END DO
        FFMAX=AMAX(1)
        FFMIN=AMIN(1)
        YLYL=YLEN
        DO I=2,NFILE
          FFMIN=FFMIN-(AMAX(I)-AMIN(I))
        END DO
        ARROSZ=(FFMAX-FFMIN)/25.0
        DO I=1,NFILE
          YLFILE(I)=YLYL*(AMAX(I)-AMIN(I))/(FFMAX-FFMIN)
        END DO
      ELSE
        DO I=1,NFILE
          READ 10,FNAME(I)
        END DO
        FFMAX=AMAXFF  
        FFMIN=AMINFF-NFILE*(AMAXFF-AMINFF)
        YLYL=YLEN          
        ARROSZ=(FFMAX-FFMIN)/25.0
      END IF
      IF(ILOG.NE.0)PRINT 25
   25 FORMAT(12X,'LOG POWER SPECTRA WILL BE PLOTTED')

C
C     INITIALISE THE PLOTTING IF NECESSARY
C
      IF(IPGPL.EQ.0)THEN
        IPGPL=1
        CALL PGBEG(0,PGPLDEV,1,1)
      END IF
C     SET THE CHARACTER HEIGHT
      CALL PGSCH(1.2)
C     SET THE LINE WIDTH (2=DARKER THAN DEFAULT)
      CALL PGSLW(2) 
C
C     ARRANGE FOR VIEWPORT TO BE CENTRED IN THE PLOTTING DEVICE AREA
C
      VPXLO=(1.0-XLEN)/2.0
      VPXHI=VPXLO+XLEN
      VPYLO=0.1 
      VPYHI=VPYLO+YLEN
      CALL PGSVP(VPXLO,VPXHI,0.95*VPYLO,VPYHI) ! Define viewport on view surface
C
C     WRITE A TINY DOT AT THE UPPER LEFT CORNER AND LOWER RIGHT CORNER
C     OF THE PLOT SO THE WINDOW SHOWS NICELY IN GHOSTVIEW OF THE PGPLOT.PS FILE
C
      CALL PGSCH(0.01)
      CALL PGMTXT( 'T', 600.0, -0.95*VPXLO/(VPXHI-VPXLO), 0.0, '.' )
      CALL PGMTXT( 'B', 400.0, 1.0+0.95*VPXLO/(VPXHI-VPXLO), 0.0, '.' )
      CALL PGSCH(1.2)
     
C     WRITE OUT THE TITLE.
      CALL PGMTXT( 'T', 3.0, 0.0, 0.0, TITLE ) 
      CALL PGSCH(1.0)
C
C     DRAW THE TOP AND BOTTOM X-AXES
C
      FLOLAB=INT(FLO*1.0E6)       ! Converting to microHz
      FLO=FLOLAB/1.0E6
      FHILAB=FHI*1.0E6
C     MAP THE VIEWPORT TO THE USER UNITS
      CALL PGSWIN(FLOLAB,FHILAB,0.0,1.0)   ! Dummy values are used for Y-axis
      CALL PGBOX('BNTS1', 0.0, 0, 'BC', 0.0, 0)
      CALL PGLAB('Frequency (microHz)',' ',' ')
      CALL PAXIS(FLOLAB,FHILAB)
      CALL PGMTXT('T', 2.2, 0.5, 0.5, 'Period (s)')

C
C     THE LOOP TO READ IN AND PLOT THE SEPARATE TRANSFORMS
C
      DO 150 I=1,NFILE
        PRINT 30,FNAME(I)
   30   FORMAT(/12X,'NEXT FOURIER TRANSFORM FROM FILE ',A)
        IF(ISEP.EQ.0)THEN
C         SAME VERTICAL AXIS LENGTH AND SCALING FOR EACH FILE
          YL=YLEN/NFILE*(1.0-0.05/NFILE)  ! To allow a little at the bottom
        END IF
        IF(ISEP.EQ.1.OR.ISEP.EQ.-1)THEN
C         SEPARATE VERTICAL AXIS LENGTH AND SCALING FOR EACH FILE
          NOARRO=0
          YL=YLFILE(I)
          AMINFF=AMIN(I)
          AMAXFF=AMAX(I)
          IF(YL.LT.0.0)THEN
            YL=-YL
            NOARRO=1
          END IF
        END IF
        OPEN(11,FILE=FNAME(I),STATUS='OLD')
        READ(11,40)INTITL
   40   FORMAT(A)
        PRINT 60,INTITL
   60   FORMAT(12X,'TITLE: ',A)
        READ(11,70)NQ,HJD,KDIN,XNULO,XNUHI,DF
   70   FORMAT('NPTS :  ',I5,4X,'HJD : ',F18.7,4X,'NFREQ :',I6/   
     *     'FLOW : ',1PE15.7,4X,'FHIGH : ',1PE15.7,4X,'DF : ',1PE15.7)    
        PRINT 75,NQ,HJD,KDIN,XNULO,XNUHI,DF
   75   FORMAT(12X,'NPTS : ',I5,4X,'HJD : ',F18.7,4X,'NFREQ :',I6/12X,
     *     'FLOW : ',1PE15.7,4X,'FHIGH : ',1PE15.7,4X,'DF : ',1PE15.7)    
        IF(ISEP.EQ.1)PRINT 76,YL,AMINFF,AMAXFF
   76   FORMAT(12X,'PLOTTED ON YAXIS ',F6.3,'   MIN Y ',1PE15.7,
     *         '   MAX Y ',E15.7)
        KD=0
C       IWINDO=1 IF IT'S A SPECTRAL WINDOW FILE, OTHERWISE IWINDO=0
        IWINDO=0
        DO 85 J=1,KDIN
          READ(11,80)IDUM,XNUIN,FFIN
   80     FORMAT(I10,2E20.8)
          IF(IDUM.LT.0.OR.XNUIN.LT.0)THEN
            IWINDO=1
            IF(J.EQ.1)XWIN1=XNUIN
          END IF
          IF(IWINDO.EQ.0.AND.XNUIN.LT.FLO)GO TO 85
          IF(IWINDO.EQ.0.AND.XNUIN.GT.FHI)GO TO 90
          KD=KD+1
          XNU(KD)=XNUIN
          IF(IWINDO.EQ.1)XNU(KD)=XNU(KD)-XWIN1+FLO
          IF(IWINDO.EQ.1)FFIN=FFIN*AMAX(I)*0.98
          IF(ILOG.EQ.0)FF(KD)=FFIN
          IF(ILOG.NE.0)FF(KD)=ALOG10(FFIN)
   85   CONTINUE
   90   CLOSE(11)
        PRINT 95,KD
   95   FORMAT(12X,'NO OF FREQUENCIES PLOTTED: ',I5)
C
C       REDEFINE THE VIEWPORT (REGION OCCUPIED BY THE FRAME OF THE GRAPH)
C       TO BE THE NEXT FRAME TO BE PLOTTED   
C

        IF(I.EQ.1)THEN
          YHI=VPYHI
        ELSE
          YHI=YLO 
        END IF
C       NORMAL VIEWPORT DEFINITION
        YLO=YHI-YL
        CALL PGSVP(VPXLO,VPXHI,YLO,YHI) 
C       MAP THE USER UNITS TO THE VIEWPORT (NORMAL CASE)
        CALL PGSWIN(FLO,FHI,AMINFF,AMAXFF) 
        YOPT='BCT'
        IF( I.EQ.1.OR.ISEP.EQ.1 )THEN
          YOPT(4:5)='NV'
          IF(ISEP.EQ.1.AND.MOD(I,2).EQ.0)YOPT(4:5)='MV'
          IF(AMAXFF.NE.0.0.AND.ABS(LOG10(AMAXFF)).LE.3.0)
     *      YOPT(6:6)='1'
        END IF
C       DRAW THE Y-AXES ONLY (X-AXES ALREADY DONE)
        XTICK=0.0
        YTICK=0.0
c       ytick=0.002        This statement and next used for large no.
c       call pgsch(0.7)    of stacked FT's.
        IF(I.EQ.1.OR.ISEP.NE.-1)CALL MYBOX(' ',XTICK,0,YOPT,YTICK,0)

        IF(I.EQ.1.AND.ISEP.EQ.-1)THEN
C
C         SPECIAL Y-AXIS FOR AUTO SCALING: THE VIEW PORT IS THE WHOLE
C         FRAME AND THE SCALING IS TO MAP FFMIN AND FFMAX TO THIS
C         VIEWPORT. AS SOON AS THIS Y AXIS IS DRAWN, THE NORMAL
C         (ISEP>-1) VIEWPORT AND SCALING IS REDEFINED TO BE THE
C         NORMAL LAYOUT           
C
          CALL PGSVP(VPXLO,VPXHI,VPYHI-YLYL,VPYHI) 
C         MAP THE SPECIAL CASE USER UNITS TO THE VIEWPORT
          CALL PGSWIN(FLO,FHI,FFMIN,FFMAX)   
          YOPT='BCT'
C         DRAW THE Y-AXES ONLY USING THE YTICK MARK FROM NORMAL
C         CALL TO MYBOX
          CALL MYBOX(' ', XTICK, 0, YOPT, YTICK, 0)
C
C         NOW RESTORE THE MAPPING OF THE VIEWPORT AND USER UNITS
          CALL PGSVP(VPXLO,VPXHI,YLO,YHI) 
          CALL PGSWIN(FLO,FHI,AMINFF,AMAXFF) 
        END IF

C       WRITE THE DAY NUMBER OF THIS SPECTRUM COMPARED TO THE FIRST
        IF(LABEL.EQ.1)THEN
          IF(I.EQ.1)HJD1=HJD
          DAY=HJD-HJD1
          WRITE(STRING,'(F6.1)')DAY
          CALL PGMTXT( 'T', -1.5, 0.95, 1.0, STRING )
c         call pgsch(0.6)                      Used for large no of FTs
c         call pgmtxt( 'T', -1.5, 0.03, 0.0, STRING )
        END IF
C       PLOT THE SPECTRUM
        DO 100 J=1,KD    
          IF(J.EQ.1)CALL PGMOVE(XNU(J),FF(J))
          CALL PGDRAW(XNU(J),FF(J))
  100   CONTINUE    
        IF(NOARRO.EQ.1)GO TO 150
C
C       DRAWS ARROWS AT SPECIFIED FREQUENCIES OVER THE SPECTRUM
C
        DO J=1,NARROW
          CALL PGSCH(0.5)
          CALL PGSAH(2,35.0,1.0)
          IF(HARROW(J).NE.0)THEN
C
C           USE THIS HEIGHT TO VERTICALLY POSITION THE ARROW
C
            CALL PGARRO(FARROW(J),HARROW(J)+ARROSZ,              
     *                  FARROW(J),HARROW(J))
          ELSE
            CALL PGARRO(FARROW(J),TOPARR(I),
     *                  FARROW(J),BOTARR(I))                 
          END IF
          CALL PGSCH(1.0)
        END DO  
  150 CONTINUE
      CALL PGEND
      RETURN
      END   

