      SUBROUTINE PLOTPW(XNU,FF,GG,KD,IPTEST,TITLE,COMMAND)     
C
C     ROUTINE TO PLOT THE AMPLITUDE SPECTRUM (PLOTP COMMAND)
C     THE SPECTRAL WINDOW (PLOTW COMMAND) OR THE POWER SPECTRUM,
C     PERHAPS ON A LOG SCALE (COMMANDS PLOG, PSQLOG, PSQAMP)
C
      COMMON /PGPL/PGPLDEV,IPGPL
      CHARACTER PGPLDEV*80
      COMMON /PLOTFL/PLNAME
      COMMON /ARROWS/NARROW,FARROW(20),HARROW(20)
      CHARACTER PLNAME*12
      DIMENSION XNU(1),FF(1),GG(1),DUM(4)   
      CHARACTER TITLE*80,SFILE*25,COMMAND*(*)   
      INTEGER TRMLEN
      EXTERNAL TRMLEN
C
C     IPTEST=0: PLOT AMPLITUDE SPECTRUM
C           =1:      SPECTRAL WINDOW
C           =2:      AMPLITUDE OR POWER SPECTRUM ON A LOG SCALE
C

      LENTIT=TRMLEN(TITLE)
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     AND TO OCCUPY:
C        IF /VPS DEVICE, 50 % OF WIDTH IN X, 30 % OF WIDTH IN Y.
C        IF /PS DEVICE , 50 % OF WIDTH IN X, 60 % OF WIDTH IN Y.
C
      IF(INDEX(PGPLDEV,'/VPS') .NE.0.OR.
     *   INDEX(PGPLDEV,'/XWIN').NE.0)THEN
        CALL PGSVP(0.20,0.85,0.2,0.6)
      END IF
      IF(INDEX(PGPLDEV,'/PS').NE.0)THEN
        CALL PGSVP(0.3,0.8,0.25,0.85)
      END IF

      READ *,YMIN,YMAX  

C
C     X SCALING - CONVERT ALL THE FREQUENCIES TO MICROHZ (TEMPORARILY)
C
      XHINU=0.0   
      XLONU=1.0E9 
      DO J=1,KD
        XNU(J)=XNU(J)*1.0E6  
        IF(XHINU.LT.XNU(J))XHINU=XNU(J)   
        IF(XLONU.GT.XNU(J))XLONU=XNU(J)   
      END DO      
      XLONU=INT(XLONU)
      IF(IPTEST.EQ.1)XLONU=-XHINU

      PRINT 15,TITLE
   15 FORMAT(12X,'TITLE : ',A)   

C
C     Y SCALING
C
      IF(YMIN.EQ.0.0.AND.YMAX.EQ.0.0)THEN   
        IF(IPTEST.EQ.0)THEN
          YMIN=0.0
          YMAX=-1.0E30
          DO J=1,KD    
            IF(FF(J).GT.YMAX)YMAX=FF(J) 
          END DO  
          YMAX=YMAX*1.02
        END IF  
        IF(IPTEST.EQ.1)THEN
          YMIN=0.0
          YMAX=1.02
        END IF  
        IF(IPTEST.EQ.2)THEN
          YMAX=-1.0E30    
          SUM=0.0     
          DO J=1,KD
            IF(FF(J).GT.YMAX)YMAX=FF(J)   
            SUM=SUM+FF(J)   
          END DO        
          FFMEAN=SUM/KD     
          YMAX=NINT(YMAX+0.99999)   
          YMIN=YMIN-6.0     
          IF(YMAX-FFMEAN.GT.4.0)YMIN=YMAX-9.0     
        END IF  
      END IF
C
C     INCREASE THE Y SCALING SO THE SPECTRUM IF LIFTED OFF THE AXIS
C
      IF(YMIN.EQ.0.0)YMIN=-0.035*YMAX  

      IF(IPTEST.EQ.0)PRINT 24,'AMPLITUDE SPECTRUM'    
      IF(IPTEST.EQ.1)PRINT 24,'SPECTRAL WINDOW'
      IF(IPTEST.EQ.2)PRINT 24,'SPECTRUM ON A LOG SCALE'    
   24 FORMAT(12X,'PLOTTING ',A)                      
      PRINT 25,YMIN,YMAX,XLONU,XHINU   
   25 FORMAT(12X,' Y MINIMUM OF ',F13.5/
     *       12X,'   MAXIMUM OF ',F13.5/
     *       12X,'LO FREQUENCY',F15.5,' microHZ'/  
     *       12X,'HI FREQUENCY',F15.5,' microHZ')  
C
C     ESTABLISH MAPPING BETWEEN VIEWPORT AND WORLD COORDINATES
C
      CALL PGSWIN(XLONU,XHINU,YMIN,YMAX)
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 PGTEXT( XLONU-0.3*(XHINU-XLONU), YMAX+0.30*(YMAX-YMIN),
     *            '.')                    
      CALL PGTEXT( XHINU+0.12*(XHINU-XLONU), YMIN-0.25*(YMAX-YMIN),
     *            '.')                    
      CALL PGSCH(1.2)
C
C     ANNOTATE VIEWPORT WITH FRAME, TICK MARKS AND NUMERIC LABELS
C
      IF(IPTEST.EQ.1)THEN
        CALL PGBOX('BCNTS1',0.0,0,'BCNTSV',0.0,0)
      ELSE
        CALL PGBOX('BNTS1',0.0,0,' ',0.0,0)
        CALL PAXIS( XLONU, XHINU )
        CALL PGMTXT('T', 2.2, 0.5, 0.5, 'Period (s)')
        CALL MYBOX(' ',XTICK,0,'BCNTSV',YTICK,0)
      END IF
C
C     PLOT SPECTRUM
C
      IF(IPTEST.EQ.0)CALL PGLINE(KD,XNU,FF)
      IF(IPTEST.EQ.1)THEN
        CALL PGLINE(KD,XNU,GG)
C
C       NOW CHANGE SIGN OF FREQUENCIES TO PLOT NEGATIVE HALF
C       OF SPECTRAL WINDOW
C
        DO J=1,KD
          XNU(J)=-XNU(J)
        END DO      
        CALL PGLINE(KD,XNU,GG)
        DO J=1,KD
          XNU(J)=-XNU(J)
        END DO      
      END IF
      IF(IPTEST.EQ.2)CALL PGLINE(KD,XNU,FF)
C
C     WRITE PLOT TITLE, X AND Y AXIS LABELS
C
      IF(TITLE.NE.' ')THEN
        IF(IPTEST.EQ.1)THEN
          CALL PGTEXT( XLONU, YMAX + 0.1*(YMAX-YMIN), TITLE)
        ELSE
          CALL PGTEXT( XLONU, YMAX + 0.22*(YMAX-YMIN), TITLE)
        END IF
      END IF

      IF(IPTEST.EQ.0)THEN
        IF(COMMAND.EQ.'PLOTP')
     *  CALL PGTEXT( XLONU - 0.25*(XHINU-XLONU), 
     *               YMAX + 0.12*(YMAX-YMIN), 'Amplitude')            
        IF(COMMAND.EQ.'PSQAMP')
     *  CALL PGTEXT( XLONU - 0.25*(XHINU-XLONU), 
     *               YMAX + 0.12*(YMAX-YMIN), '  Power  ')            
      END IF
      IF(IPTEST.EQ.1)THEN
        CALL PGTEXT( XLONU-0.25*(XHINU-XLONU), 
     *               0.7*YMAX+0.08*(YMAX-YMIN), ' Spectral')            
        CALL PGTEXT( XLONU - 0.25*(XHINU-XLONU), 
     *               0.7*YMAX,                  '  Window ')            
      END IF
      IF(IPTEST.EQ.2)THEN
        IF(COMMAND.EQ.'PLOG') 
     *  CALL PGTEXT( XLONU - 0.25*(XHINU-XLONU),
     *               YMAX + 0.12*(YMAX-YMIN), 'Amplitude')            
        IF(COMMAND.EQ.'PSQLOG')
     *  CALL PGTEXT( XLONU - 0.25*(XHINU-XLONU),
     *               YMAX + 0.12*(YMAX-YMIN), '  Power  ')            
      END IF

C     USE PGLAB TO DRAW X-AXIS LABEL
      CALL PGLAB('Frequency (microHz)',' ',' ')

      IF(IPTEST.EQ.1)GO TO 50
C
C     DRAWS ARROWS AT SPECIFIED FREQUENCIES OVER THE SPECTRUM
C
      DO J=1,NARROW
        CALL PGSCH(0.8)
        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)*1.0E6,HARROW(J)+0.1*(YMAX-YMIN),
     *                FARROW(J)*1.0E6,HARROW(J))
        ELSE
          CALL PGARRO(FARROW(J)*1.0E6,YMAX,
     *                FARROW(J)*1.0E6,YMAX-0.1*(YMAX-YMIN))
        END IF
        CALL PGSCH(1.2)
      END DO  
C
C     FINISH PLOTTING
C
   50 CALL PGPAGE
C
C     RESTORE THE FREQUENCY ARRAY
C
      DO J=1,KD
        XNU(J)=XNU(J)/1.0E6  
      END DO      

      RETURN
      END   
 
