     	SUBROUTINE PIEPLT_MENU( ICODE )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY, FEB-1985
C
C  	INTERACTIVE DIALOGUE MENU FOR PIE-CHART PLOT.
C	RETURN WITH "ICODE=1" IF QUITING.
C
    	INCLUDE		'PIEPLT.CBS'
     	PARAMETER	NKS = 18
     	CHARACTER	KWD(NKS)*2
    	CHARACTER	TEXT*80, KEY*2, SYM*1
     	EQUIVALENCE	(SYM,TEXT(3:3)), (KEY,TEXT(1:2))
     	COMMON/PIEPLT_MENU/  JSET, IKEY, NCHS, TEXT
C
C  ACCESS 2 LETTER CODES TO GRAPH PARAMETERS...
C
     	DATA KWD / 'F1', 'F4', 'GT', 'TP', 'TC', 'GO', 'SF', 'SE',
     + 'PA', 'PD', 'NO', 'LP', 'PI', 'PN', 'PT', 'LI', 'CO', 'ID' /
C
C  PRESENT THE DIALOGUE MENU...
C
    	JSET = 1			!ACTIVE PIE POINTER
     	CALL PIEPLT_FORM( JSET )
     	IF( NPIE.EQ.0 ) CALL PIEPLT_OOPS(' Type HELP if you need any.')
C
C  START THE DIALOGUE LOOP...
C
111	CALL IFANSI_VIDEO(47)
     	CALL IFANSI_TEXT('PIEPLT> ', 8, 14, 1 )
     	CALL IFANSI_CLEAR(4)
     	IKEY = IFQIOW( NCHS, TEXT )
     	IF( IKEY.EQ.26) GOTO 888
     	IF( NCHS.EQ.0 ) GOTO 999
     	NCHS = IFMISC_STRIP(TEXT)
     	CALL   IFMISC_UPPER(KEY)
     	IF( KEY.EQ.'QU' ) GOTO 888
     	IF( KEY.EQ.'EX' ) GOTO 999
     	CALL PIEPLT_CLEAR_OOPS
C
C  COMMAND INPUT...
C
     	IF( SYM.NE.'='  ) THEN
     	    CALL PIEPLT_COM
     	    IF( IKEY.EQ.-1 ) GOTO 999
     	    GOTO 111
     	ENDIF
C
C  PARAMETER INPUT...
C
     	DO IKEY = 1, NKS
     	   IF( KEY.EQ.KWD(IKEY) ) GOTO 222
     	ENDDO
     	CALL PIEPLT_ERROR(1)	!INVALID KEYWORD
     	GOTO 111
C
C  YES, WE HAVE A VALID KEYWORD...
C
222	TEXT = TEXT(4:)
     	NCHS = NCHS-3
     	IF( IKEY.LT.3 ) THEN
     	    CALL PIEPLT_FILES
     	    GOTO 111
     	ELSE IF( IKEY.EQ.3 ) THEN
     	    TITL = TEXT
     	    CALL PIEPLT_PARS_ALL(1)
     	ELSE IF( IKEY.LT.11 ) THEN
     	    IF( NCHS.EQ.0 ) GOTO 666
     	    IKEY = IKEY-2
     	    CALL PIEPLT_OVERALL
     	ELSE IF( IKEY.LT.18 ) THEN
     	    IF( NCHS.EQ.0 ) GOTO 666
     	    IKEY = IKEY-10
     	    CALL PIEPLT_SET
     	ELSE
     	    LABL(JSET) = TEXT
     	    CALL PIEPLT_PARS_SET(JSET,8)
     	ENDIF
     	GOTO 111
666	CALL IFANSI_VIDEO(70)
     	CALL PIEPLT_OOPS(' Missing input parameter.')
     	GOTO 111
888	ICODE = 1		!QUIT FROM THE DIALOGUE
     	RETURN
999	IF( NPIE.EQ.0 ) GOTO 888
     	CALL PIEPLT_DATA(0,DATNAM)
     	ICODE = 0
     	RETURN
    	END

     	SUBROUTINE PIEPLT_FORM(JSET)
C
C	THIS ROUTINE LAYOUTS THE DIALOGUE MENU FOR PIE-CHART PLOT.
C
    	INCLUDE 'PIEPLT.CBS'
C
C  LAYOUT THE MENU...
C
     	CALL IFINIT_LINE(24)
     	CALL IFANSI_TITLE('GRAFIT: Pie-Chart Plot (max    pies)')
     	CALL IFANSI_VIDEO(60)
     	CALL IFANSI_IFL( MNP, 1, 2, 1, 29 )

     	IF( CRT.EQ.2 ) THEN
     	    CALL IFANSI_VIDEO(50)
     	ELSE
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_VIDEO(90)
     	ENDIF
     	WRITE(6,1000)
1000	FORMAT('     * * * * *   O V E R A L L   G R A P H'
     +,'   P A R A M E T E R S   * * * * *     ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,1001)
1001	FORMAT(' GT='/' TP=',8X,'TC=',4X,'GO=',14X,'SF='
     +,15X,'SE=',6X,'PA=',6X,'PD=')

     	CALL IFANSI_VIDEO(90)
     	WRITE(6,1002)
1002	FORMAT(X,80(' '))
     	IF( CRT.EQ.2 ) THEN
     	    CALL IFANSI_VIDEO(50)
     	ELSE
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_VIDEO(90)
     	ENDIF
     	WRITE(6,2000)
2000	FORMAT('    * * * *   P I E   S E T   D R A W I N G'
     +,'   P A R A M E T E R S   * * * *      ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,2001)
2001	FORMAT(' NO=',5X,'NP:',7X,'ID=',44X,'LP='/
     +' PI=',19X,'%',16X,'PN=',6X,'PT=',6X,'LI=',5X,'CO=')

     	CALL IFANSI_VIDEO(90)
     	WRITE(6,1002)
     	IF( CRT.EQ.2 ) THEN
     	    CALL IFANSI_VIDEO(50)
     	ELSE
     	    CALL IFANSI_VIDEO(90)
     	ENDIF
     	WRITE(6,3000)
3000	FORMAT('     * * * * * * * *   I N P U T  &  O U T P U T'
     +,'   F I L E S   * * * * * * *     ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,3001)
3001	FORMAT(' F1=',38X,'F4=')

     	CALL IFANSI_VIDEO(90)
     	WRITE(6,1002)
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,4000)
4000	FORMAT(' oops:'//' Auxiliary: HELP/PF2, DCL/PF4,',
     +' DOWN_PIE/PF1, UP_PIE/PF3, LIST, REFRESH,'/' _commands: EXIT,',
     +' QUIT, READ, PLOT, LAYOUT, SAVE, RESET, DELETE, ...')
C
C  INSERT THE PARAMETERS...
C
     	CALL PIEPLT_PARS(JSET)
     	RETURN
     	END

     	SUBROUTINE PIEPLT_PARS(JSET)
C
C	THIS ROUTINE INSERTS ALL GRAPH PARAMETERS.
C
     	CALL PIEPLT_PARS_ALL(0)		!OVERALL PLOT PARAMETER GROUP
     	CALL PIEPLT_PARS_SET(JSET,0)	!PIE DRAWING PARAMETER GROUP
     	CALL PIEPLT_NPIE		!TOTAL NUMBER OF PIES ENTERED
     	CALL PIEPLT_PARS_FILE(0)	!I/O FILE GROUP
     	RETURN
    	END

     	SUBROUTINE PIEPLT_PARS_ALL( ICODE )
C
C	THIS ROUTINE DISPLAYS OVERALL PLOT PARAMETERS.
C
     	INCLUDE 'PIEPLT.CBS'
     	CHARACTER YES*3

     	CALL IFANSI_VIDEO(30)
     	I = ICODE
    	IF(I.EQ.0.OR.I.EQ.1) CALL IFANSI_TEXT( TITL, 77, 3, 4 )
    	IF(I.EQ.0.OR.I.EQ.2) THEN
     	   IF( PTIT.EQ.1 ) CALL IFANSI_TEXT( 'BOTTOM', 6, 4, 4 )
     	   IF( PTIT.EQ.2 ) CALL IFANSI_TEXT( 'TOP   ', 6, 4, 4 )
     	ENDIF
    	IF(I.EQ.0.OR.I.EQ.3) CALL IFANSI_IFL( CTIT, 1, 2, 4, 15 )
    	IF(I.EQ.0.OR.I.EQ.4) CALL IFANSI_FFL( XORG, 2, 6.2, 4, 22 )
     	IF(I.EQ.0.OR.I.EQ.5) CALL IFANSI_FFL( XSCL, 2, 6.3, 4, 39 )
     	IF(I.EQ.0.OR.I.EQ.6) THEN
     	   YES = 'YES'
     	   IF( .NOT.INIT ) YES = 'NO'
     	   CALL IFANSI_TEXT( YES, 3, 4, 57 )
     	ENDIF
    	IF(I.EQ.0.OR.I.EQ.7) CALL IFANSI_IFL( PANG, 1, 4, 4, 66 )
    	IF(I.EQ.0.OR.I.EQ.8) CALL IFANSI_FFL( PIED, 1, 6.2, 4, 75 )

     	RETURN
     	END

	SUBROUTINE PIEPLT_PARS_SET( ICODE, IPAR )
C
C	THIS ROUTINE DISPLAYS PIE DRAWING PARAMETERS.
C
    	INCLUDE 'PIEPLT.CBS'

     	CALL IFANSI_VIDEO(30)
     	JSET = ICODE
     	I = IPAR-1
     	IF( I.LT.0 ) I = 0
     	IF(I.EQ.0) CALL IFANSI_IFL( JSET, 1, 2, 7, 4 )
    	IF(I.EQ.0.OR.I.EQ.7) CALL IFANSI_TEXT(LABL(JSET), 42, 7, 22 )
     	IF(I.EQ.0.OR.I.EQ.1) CALL IFANSI_FFL(XLPS(1,JSET),2,6.2,7,69)
    	IF(I.EQ.0.OR.I.EQ.2) THEN
     	   CALL IFANSI_GFL( PIES(JSET),1,12.5,8,4 )
    	   CALL IFANSI_FFL( PERC(JSET),1, 6.2,8,17)
     	ENDIF
    	IF(I.EQ.0.OR.I.EQ.3) CALL IFANSI_IFL( IPEN(JSET),1, 3, 8, 43)
    	IF(I.EQ.0.OR.I.EQ.4) CALL IFANSI_IFL( IPAT(JSET),1, 3, 8, 52)
    	IF(I.EQ.0.OR.I.EQ.5) CALL IFANSI_IFL( ILIN(JSET),1, 3, 8, 61)
    	IF(I.EQ.0.OR.I.EQ.6) CALL IFANSI_FFL(XOFF(1,JSET),2,6.2,8,69)

     	RETURN
	END

     	SUBROUTINE PIEPLT_NPIE
     	INCLUDE 'PIEPLT.CBS'
     	CALL IFANSI_VIDEO(24)
     	CALL IFANSI_VIDEO(80)
     	CALL IFANSI_IFL( NPIE, 1, 3, 7, 12 )
        CALL IFANSI_VIDEO(30)
     	RETURN
     	END

     	SUBROUTINE PIEPLT_PARS_FILE( ICODE )
C
C	THIS ROUTINE DISPLAYS FILENAMES.
C
     	INCLUDE 'PIEPLT.CBS'

     	I = ICODE
     	CALL IFANSI_VIDEO(20)
     	IF( I.LT.2 ) CALL IFANSI_TEXT( DATNAM, 36, 11, 4 )
     	IF( I.NE.1 ) CALL IFANSI_TEXT( DESNAM, 36, 11, 45)
     	RETURN
     	END

     	SUBROUTINE PIEPLT_COM
C
C	THIS ROUTINE IMPLEMENTS SOME DIALOGUE COMMANDS.
C
C	LIST OF COMMANDS:
C
C	HELP   : display help information.
C	REFRESH: re-layout the dialogue menu.
C	SAVE   : save graph parameters.
C	RESET  : reset plot setup.
C	READ   : read a data file to start a new plot.
C	PLOT   : read and plot a data file to start a new plot.
C	LAYOUT : use another layout file.
C	DELETE : delete a pie from input list.
C	LIST   : list all pie parameters.
C	DCL    : spawn a system subprocess.
C	DOWN   : step pie pointer down by one.
C	UP     : step pie pointer back by one.
C
     	INCLUDE		'PIEPLT.CBS'
    	CHARACTER	TEXT*80
     	COMMON/PIEPLT_MENU/  JSET, IKEY, NCHS, TEXT

     	CALL IFMISC_UPPER(TEXT)		!CONVERT TO UPPERCASES
C
C  IF "TEXT" IS A SINGLE WORD AND CONTAINS A DOT, TAKE IT
C  AS A DATA FILENAME TO BE PLOTTED...
C
     	JKEY = INDEX( TEXT(1:NCHS), ' ' )
     	IF( JKEY.EQ.0 ) THEN
     	    JKEY = INDEX( TEXT(1:NCHS), '.' )
     	    IF( JKEY.GT.0 ) THEN
     		IKEY = 1
     		CALL PIEPLT_FILES
     		RETURN
     	    ENDIF
     	ENDIF
     	IF( TEXT(1:4).EQ.'DOWN'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OP') ) THEN
C
C  DOWN...
     	    JSET = JSET + 1
     	    IF( JSET.GT.MNP ) JSET = 1
     	    CALL PIEPLT_PARS_SET(JSET,0)
     	ELSE IF( KEY.EQ.'UP'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OR') ) THEN
C
C  UP...
     	    JSET = JSET - 1
     	    IF( JSET.LT.1 ) JSET = MNP
     	    CALL PIEPLT_PARS_SET(JSET,0)
     	ELSE IF( TEXT(1:4).EQ.'LIST' ) THEN
C
C  LIST...
     	    IF( NPIE.EQ.0 ) GOTO 888
     	    CALL PIEPLT_LIST( JSET )
     	ELSE IF( TEXT(1:4).EQ.'HELP'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OQ') ) THEN
C
C  HELP...
     	    CALL PIEPLT_MENU_HELP
     	ELSE IF( TEXT(1:3).EQ.'REF' ) THEN
C
C  REFRESH...
     	    CALL PIEPLT_FORM(JSET)
     	ELSE IF( TEXT(1:4).EQ.'SAVE' ) THEN
C
C  SAVE...
     	    IF( NPIE.EQ.0 ) GOTO 888
     	    CALL PIEPLT_SAVE
     	ELSE IF( TEXT(1:4).EQ.'READ' .OR. TEXT(1:4).EQ.'PLOT' ) THEN
C
C  READ...
     	    IKEY = 1
     	    IF( TEXT(1:4).EQ.'PLOT' ) IKEY = -1
     	    NCHS = IFMISC_NEXT(TEXT)
     	    CALL PIEPLT_FILES
     	ELSE IF( TEXT(1:6).EQ.'LAYOUT' ) THEN
C
C  LAYOUT...
     	    IKEY = 2
     	    NCHS = IFMISC_NEXT(TEXT)
     	    CALL PIEPLT_FILES
     	ELSE IF( TEXT(1:3).EQ.'DEL' ) THEN
C
C  DELETE...
     	    IF( NPIE.EQ.0 ) GOTO 888
     	    NCHS = IFMISC_NEXT(TEXT)
     	    CALL PIEPLT_DELPIE
     	ELSE IF( TEXT(1:5).EQ.'RESET' ) THEN
C
C  RESET...
     	    CALL PIEPLT_RESET( JSET )
     	ELSE IF( TEXT(1:3).EQ.'DCL'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OS') ) THEN
C
C  SPAWN...
     	    CALL IFANSI_CLEAR(2)
     	    CALL IFMISC_SPAWN( TEXT, 0 )
     	    CALL PIEPLT_FORM(JSET)
     	ELSE
     	    IF( IKEY.NE.27 ) THEN
     		CALL PIEPLT_ERROR(2)	!UNDEFINED COMMAND
     	    ELSE
     		CALL IFANSI_VIDEO(70)
     		CALL PIEPLT_OOPS(' Undefined function key.')
     	    ENDIF
     	ENDIF
     	RETURN
888	CALL PIEPLT_ERROR(7)	!NO PIE YET TO WORK ON
     	RETURN
     	END

     	SUBROUTINE PIEPLT_RESET( JSET )
C
C	THIS ROUTINE RESETS THE PLOT.
C
     	CALL PIEPLT_CLEAR_OOPS
     	CALL PIEPLT_OOPS(' Resetting the plot...')
     	CALL PIEPLT_INIT(1)
     	CALL PIEPLT_PARS(JSET)
     	CALL PIEPLT_CLEAR_OOPS

     	RETURN
     	END

     	SUBROUTINE PIEPLT_FILES
C
C	THIS ROUTINE GETS INPUT DATA/LAYOUT FILES.
C
     	INCLUDE		'PIEPLT.CBS'
     	CHARACTER	TEXT*80
     	COMMON/PIEPLT_MENU/  JSET, IKEY, NCHS, TEXT

     	JKEY = IKEY
     	IKEY = IABS(IKEY)
     	IF( NCHS.EQ.0  ) THEN
     	    NCHS = PIEPLT_NEXT(TEXT)
     	    IF( NCHS.EQ.0 ) GOTO 999
     	ENDIF
     	IF( NCHS.GT.32 ) THEN
     	    CALL PIEPLT_ERROR(6)	!FILENAME TOO LONG TO HANDLE
     	    GOTO 999
     	ENDIF
     	CALL IFMISC_UPPER(TEXT)
     	IF( IKEY.NE.1 ) GOTO 222
C
C  START A NEW PLOT WITH A NEW DATA FILE...
C
     	IKEY = PIEPLT_DATA(1,TEXT)
     	IF( IKEY.GT.0 ) THEN
     	    CALL IFANSI_VIDEO(70)
     	    IF( IKEY.EQ.1 ) THEN
     		CALL PIEPLT_OOPS(' Can not find/open
     + the file and new output filename assumed.')
     	    ELSE
     		CALL PIEPLT_OOPS(' No pie found in the file.')
     		GOTO 999
     	    ENDIF
     	ENDIF
C
C  YES, WE HAVE A NEW FILE...
C
     	DATNAM = TEXT
     	DESNAM = DATNAM
     	CALL IFMISC_NAMEXT( DESNAM, '.DES' )
     	IF( IKEY.EQ.0 ) THEN
     	    JSET = 1
     	    TEXT = ' '
     	    IKEY = IFMISC_DIGITS(NPIE)+1
     	    ENCODE( IKEY, 1000, TEXT ) NPIE
1000	    FORMAT(I<IKEY>)
     	    TEXT(IKEY+2:) = 'pies have been read from'
     	    TEXT(IKEY+27:) = DATNAM
     	    CALL IFANSI_VIDEO(20)
     	    CALL PIEPLT_OOPS( TEXT )
     	    IF( JKEY.GT.0 ) THEN
     		CALL IFANSI_VIDEO(30)
     		CALL PIEPLT_PARS(JSET)
     	    ELSE
     		IKEY = JKEY
     	    ENDIF
     	ELSE
     	    CALL PIEPLT_PARS_FILE(0)
     	ENDIF
     	GOTO 999

222	IF( IKEY.NE.2 ) GOTO 999
C
C  NEW LAYOUT FILE (ONLY *.DES FILENAME IS ALLOWED SO THAT THE USER
C  MAY NOT RISK LOOSING HIS/HER FILE BY MIS-ENTERING A FILENAME...
C
     	DESNAM = TEXT
     	CALL IFMISC_NAMEXT( DESNAM, '.DES' )
     	CALL PIEPLT_PARS_FILE(2)

999	RETURN
     	END

     	FUNCTION PIEPLT_DATA( ICODE, FNAME )
C
C	THIS ROUTINE READS/WRITES A PIE-CHART DATA FILE.
C
     	INCLUDE        'PIEPLT.CBS'
    	PARAMETER	LUN= 13
    	CHARACTER	KEY*4, REST*127, FNAME*(*)
    	DIMENSION	KP(20), RP(20)
     	LOGICAL         YES

     	IF( FNAME.EQ.' ' ) RETURN
C
C  SET DEFAULT FILE EXT TO .PIE...
C
     	CALL IFMISC_DEFEXT( FNAME, '.PIE' )
     	IF( ICODE.NE.0 ) GOTO 1234
C
C  WRITE THE DATA FILE...
C
    	OPEN( UNIT=LUN, FILE=FNAME, STATUS='UNKNOWN',
     +        CARRIAGECONTROL='LIST', ERR=666 )

    	WRITE(LUN,1000,ERR=666) TITL, PTIT, CTIT, PANG, PIED
     +, XORG, XSCL, (HPAR(I), I=1, 20), PLOTID
1000	FORMAT('%This is a GRAFIT pie-chart data file:'/
     +,'TITL=',A80,/,'OVER=',3I5,5F8.3,/,'PAR1=',10F7.2,/
     +,'PAR2=',10F7.2,/'IDEN=',A32)

     	DO I=1, MNP
     	   IF( PIES(I).GT.0.0 ) WRITE(LUN,1001,ERR=666) PIES(I),
     +     IPEN(I), IPAT(I), ILIN(I), XOFF(1,I),XOFF(2,I),
     +     XLPS(1,I),XLPS(2,I), LABL(I)
     	ENDDO
1001	FORMAT('PIES=',G13.5,3I4,4F8.3,',',A32)
     	PIEPLT_DATA = 0.0
    	GOTO 999
C
C  READ A PIE-CHART DATA FILE...
C
1234	INQUIRE( FILE=FNAME, EXIST=YES )
     	IF( .NOT.YES ) GOTO 666
     	OPEN( UNIT=LUN, FILE=FNAME, STATUS='OLD',
     +        CARRIAGECONTROL='LIST', READONLY, ERR=666 )
C
C  READ KEYWORD LINES ONE BY ONE...
C
     	MPIE = 0
 111	READ(LUN,112,END=555) KEY,REST
 112	FORMAT(A4,X,A127)
     	IF( KEY.EQ.' ' .OR. REST.EQ.' ' ) GOTO 111

     	NCH = IFMISC_STRIP(REST)
     	CALL  IFMISC_UPPER(KEY)

     	IF( KEY.EQ.'TITL' ) THEN	!GRAPH TITLE
     	    TITL = REST
     	    GOTO 111
     	ENDIF

     	IF( KEY.EQ.'OVER' ) THEN	!OVERALL GRAPH PARAMETERS
     	    NCH = IFMISC_DECODE( REST, 8, RP, KP )
    	    IF( NCH.EQ.0 ) GOTO 111	!ERROR IN THE INPUT
     	    IF( KP(1).EQ.1 ) PTIT = RP(1)
     	    IF( KP(2).EQ.1 ) CTIT = RP(2)
     	    IF( KP(3).EQ.1 ) PANG = RP(3)
     	    IF( KP(4).EQ.1 ) PIED = RP(4)
     	    IF( KP(5).EQ.1 ) XORG(1) = RP(5)
     	    IF( KP(6).EQ.1 ) XORG(2) = RP(6)
     	    IF( KP(7).EQ.1 ) XSCL(1) = RP(7)
     	    IF( KP(8).EQ.1 ) XSCL(2) = RP(8)
     	    GOTO 111
     	ENDIF

     	IF( KEY.EQ.'PAR1' ) THEN	!HARDCOPY SETUP PARAMETERS
     	    NCH = IFMISC_DECODE( REST, 10, RP, KP )
    	    IF( NCH.EQ.0 ) GOTO 111	!ERROR IN THE INPUT
     	    DO  I=1, 10
     		IF( KP(I).EQ.1 ) HPAR(I) = RP(I)
     	    ENDDO
     	    GOTO 111
     	ENDIF

     	IF( KEY.EQ.'PAR2' ) THEN	!HARDCOPY SETUP PARAMETERS
     	    NCH = IFMISC_DECODE( REST, 10, RP, KP )
    	    IF( NCH.EQ.0 ) GOTO 111	!ERROR IN THE INPUT
     	    DO  I=1, 10
     		IF( KP(I).EQ.1 ) HPAR(I+10) = RP(I)
     	    ENDDO
     	    GOTO 111
     	ENDIF

     	IF( KEY.EQ.'PIES' ) THEN	!PIE DRAWING PARAMETERS
     	    IF( MPIE.EQ.MNP ) GOTO 111
     	    NCH = IFMISC_DECODE( REST, 8, RP, KP )
    	    IF( NCH.EQ.0 ) GOTO 111	!ERROR IN THE INPUT
     	    IF( RP(1).LE.0.0 ) GOTO 111
     	    MPIE = MPIE + 1
     	    PIES(MPIE) = RP(1)
     	    IF( KP(2).EQ.1 ) IPEN(MPIE) = RP(2)
     	    IF( KP(3).EQ.1 ) IPAT(MPIE) = RP(3)
     	    IF( KP(4).EQ.1 ) ILIN(MPIE) = RP(4)
     	    IF( KP(5).EQ.1 ) XOFF(1,MPIE) = RP(5)
     	    IF( KP(6).EQ.1 ) XOFF(2,MPIE) = RP(6)
     	    IF( KP(7).EQ.1 ) XLPS(1,MPIE) = RP(7)
     	    IF( KP(8).EQ.1 ) XLPS(2,MPIE) = RP(8)
     	    LABL(MPIE) = REST(NCH:)
     	    GOTO 111
     	ENDIF

     	IF( KEY.EQ.'IDEN' ) THEN	!PLOT ID LABEL
     	    PLOTID = REST
     	    GOTO 111
     	ENDIF
     	GOTO 111
555	IF( MPIE.GT.0 ) GOTO 888
     	PIEPLT_DATA = 2.0		!NO PIE FOUND
     	GOTO 999
666	IERR = 1
     	PIEPLT_DATA = 1.0		!FILE NOT FOUND
     	GOTO 999
888	NPIE = MPIE
     	CALL PIEPLT_SCALE
     	PIEPLT_DATA = 0.0
999	CLOSE(UNIT=LUN)
     	RETURN
     	END

    	SUBROUTINE PIEPLT_INIT( ICODE )
C
C	THIS ROUTINE INITIALIZES THE COMMON BLOCKS FOR PIE-CHART PLOT.
C
    	INCLUDE 'PIEPLT.CBS'

     	IF( ICODE.EQ.0 ) THEN
     	    CRT = IFINIT_ITM(0)		!GET THE TYPE OF TERMINAL
     	    DATNAM = 'JUNK.PIE'
     	    DESNAM = 'JUNK.DES'
     	    TITL = ' '
     	    DO I=1, MNP
     	       PIES(I) = 0.0
     	       PERC(I) = 0.0
    	       LABL(I) = ' '
     	    ENDDO
     	    NPIE = 0
     	    INIT = .TRUE.
	    PLOTID = ' '
     	ENDIF

     	CTIT = 3
     	PANG = 0.0
     	PIED = 4.0
     	XORG(1) = 5.0
     	XORG(2) = 4.0
     	XSCL(1) = 1.0
     	XSCL(2) = 1.0
     	PTIT = 1
     	DO I=1, MNP
     	   IPEN(I) = PTIT
     	   PTIT = PTIT + 1
     	   IF( PTIT.GT.7 ) PTIT = 1
     	   IPAT(I) = 1
     	   ILIN(I) = 1
    	   XOFF(1,I) = 0.0
    	   XOFF(2,I) = 0.0
    	   XLPS(1,I) = 0.0
    	   XLPS(2,I) = 0.0
    	ENDDO
     	PTIT = 1

    	RETURN
    	END

	SUBROUTINE PIEPLT_OVERALL
C
C	THIS ROUTINE CHANGES OVREALL PLOT PARAMETERS.
C
    	INCLUDE		'PIEPLT.CBS'
     	CHARACTER	TEXT*80
     	COMMON/PIEPLT_MENU/ JSET, IKEY, NCHS, TEXT
     	DIMENSION	RP(2), KP(2)
C
C  DECODE THE PARAMETERS...
C
     	IF( IKEY.NE.2 .AND. IKEY.NE.6 ) THEN
     	    NCHS = IFMISC_DECODE( TEXT, 2, RP, KP )
    	    IF( NCHS.EQ.0 ) THEN
     		CALL PIEPLT_ERROR(3)
     		RETURN
     	    ENDIF
     	ENDIF

     	IF( IKEY.EQ.2 ) THEN
     	    IF( NCHS.LT.3 ) GOTO 777
     	    CALL IFMISC_UPPER(TEXT)
     	    IF( TEXT(1:3).NE.'TOP'.AND.TEXT(1:6).NE.'BOTTOM' ) GOTO 777
     	    PTIT = 1
     	    IF( TEXT(1:3).EQ.'TOP' ) PTIT = 2
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.3 ) THEN
     	    IF( RP(1).LT.1.0 .OR. RP(1).GT.8.0 ) GOTO 777
     	    CTIT = RP(1)
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.4 ) THEN		!GRAPH ORIGIN
     	    DO I=1,2
    	    IF( KP(I).EQ.1 ) THEN
     		IF(RP(I).LT.0.0.OR.RP(I).GT.99.0) GOTO 777
     		XORG(I) = RP(I)
    	    ENDIF
     	    ENDDO
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.5 ) THEN		!OVERALL SCALE FACTORS
     	    DO I=1,2
    	    IF( KP(I).EQ.1 ) THEN
     		IF(RP(I).LT.0.1.OR.RP(I).GT.3.0) GOTO 777
    	       	XSCL(I) = RP(I)
    	    ENDIF
     	    ENDDO
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.6 ) THEN		!SCREEN ERASE FLAG
     	    INIT = .TRUE.
     	    IF( TEXT(1:1).EQ.'N' .OR. TEXT(1:1).EQ.'n'
     +     .OR. TEXT(1:1).EQ.'F' .OR. TEXT(1:1).EQ.'f' ) INIT=.FALSE.
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.7 ) THEN		!1ST PIE ANGULER OFFSET
     	    IF( ABS(RP(1)).GE.360.0 ) GOTO 777
     	    PANG = RP(1)
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.8 ) THEN		!PIE DIAMETER IN INCHES
     	    IF(RP(1).LT.1.0.OR.RP(1).GT.99.0) GOTO 777
     	    PIED = RP(1)
     	    GOTO 888
     	ENDIF

777	CALL PIEPLT_ERROR(4)
     	RETURN
888	CALL PIEPLT_PARS_ALL(IKEY)
999	RETURN
	END

	SUBROUTINE PIEPLT_SET
C
C	THIS ROUTINE CHANGES PIE DRAWING PARAMETERS
C	IN COLUMN-RUN MODE.
C
    	INCLUDE		'PIEPLT.CBS'
     	CHARACTER	TEXT*80
     	COMMON/PIEPLT_MENU/ JSET, IKEY, NCHS, TEXT
     	DIMENSION	KP(2), RP(2)
C
C  DECODE THE INPUT...
C
     	IF( IKEY.NE.2 ) THEN
     	    J = IFMISC_DECODE( TEXT, 2, RP, KP )
    	    IF( J.EQ.0 ) GOTO 666
     	ENDIF
C
C  STORE THE VALID PARAMETERS...
C
     	IF( IKEY.EQ.1 ) THEN
     	    J = RP(1)
     	    IF( J.LT.1 .OR. J.GT.MNP ) GOTO 777
     	    JSET = J
     	    IKEY = 0
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.2 ) THEN
     	    DO J=1, 2
     		IF( KP(J).EQ.1 ) THEN
     		    IF( ABS(RP(J)).GT.99.0 ) GOTO 777
     		    XLPS(J,JSET) = RP(J)
     		ENDIF
     	    ENDDO
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.3 ) THEN
     	    IF( RP(1).LT.0.0 ) GOTO 777
     	    PIES(JSET) = RP(1)
     	    CALL PIEPLT_SCALE
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.4 ) THEN
     	    J = RP(1)
     	    IF( J.LT.0 .OR. J.GT.16 ) GOTO 777
     	    IPEN(JSET) = J
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.5 ) THEN
     	    J = RP(1)
     	    IF( IABS(J).GT.99 ) GOTO 777
     	    IPAT(JSET) = J
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.6 ) THEN
     	    J = RP(1)
     	    IF( J.LT.1 .OR. J.GT.9 ) GOTO 777
     	    ILIN(JSET) = J
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.7 ) THEN
     	    DO J=1, 2
     		IF( KP(J).EQ.1 ) THEN
     		    IF( ABS(RP(J)).GT.9.0 ) GOTO 777
     		    XOFF(J,JSET) = RP(J)
     		ENDIF
     	    ENDDO
     	    GOTO 888
     	ENDIF

666	CALL PIEPLT_ERROR(3)	!INVALID PARAMETER
     	RETURN
777	CALL PIEPLT_ERROR(4)	!PARAMETER VALUE OUT OF RANGE
     	RETURN
888	CALL PIEPLT_PARS_SET(JSET,IKEY)
     	RETURN
	END

	SUBROUTINE PIEPLT_LIST( JSET )
C
C	LIST OF ALL PIE PARAMETERS.
C
	INCLUDE 'PIEPLT.CBS'

	CALL IFANSI_CURSOR(7,1)
	CALL IFANSI_CLEAR(2)
     	CALL IFANSI_VIDEO(47)
     	CALL IFANSI_VIDEO(90)
     	WRITE(6,1)
  1	FORMAT(' NO  PIE-SIZE       %   PEN PAT LIN OFFSET(X,Y)',
     +' LABELS(X,Y)       ID             ')
     	CALL IFANSI_VIDEO(47)
     	DO I=1, MNP
     	   IF( PIES(I).GT.0.0 ) WRITE(6,1234) I, PIES(I), PERC(I),
     +     IPEN(I), IPAT(I), ILIN(I), XOFF(1,I), XOFF(2,I),
     +     XLPS(1,I), XLPS(2,I), LABL(I)
     	ENDDO
1234	FORMAT(X,I2,G13.5,F7.2,3I4,4F6.2,X,A20)

	WRITE(6,1000)
1000	FORMAT('$Press SPACEBAR to continue...')
     	CALL IFQIOW_KEY(I)
     	CALL PIEPLT_FORM(JSET)
	RETURN
	END

     	SUBROUTINE PIEPLT_SAVE
C
C	THIS ROUTINE SAVES CURRENT PLOT IN A FILE.
C
     	INCLUDE		'PIEPLT.CBS'
    	CHARACTER	TEXT*80, STRN*75
     	COMMON/PIEPLT_MENU/  JSET, IKEY, NCHS, TEXT

     	NCHS = IFMISC_NEXT(TEXT)
     	IF( NCHS.EQ.0 ) THEN
     	    NCHS = PIEPLT_NEXT(TEXT)
     	    IF( NCHS.EQ.0 ) GOTO 999
     	ENDIF
     	IF( NCHS.GT.32 ) THEN
     	    CALL PIEPLT_ERROR(6)
     	    GOTO 999
     	ENDIF
     	IKEY = PIEPLT_DATA( 0, TEXT )
     	IF( IKEY.EQ.0 ) THEN
     	    CALL IFANSI_VIDEO(70)
     	    CALL PIEPLT_OOPS(' Error during writing the file.')
     	ELSE
     	    STRN = ' Current plot has been saved in'
     	    STRN(39:) = TEXT
     	    CALL PIEPLT_OOPS( STRN )
     	ENDIF

999	RETURN
     	END

     	SUBROUTINE PIEPLT_ERROR( IERR )
C
C	THIS ROUTINE DISPLAYS DIALOGUE ERROR MESSAGE.
C
     	CHARACTER TEXT*75

     	I = IERR
     	IF(I.EQ.1) TEXT=' Undefined access-code.'
     	IF(I.EQ.2) TEXT=' Undefined command.'
     	IF(I.EQ.3) TEXT=' Invalid parameters.'
     	IF(I.EQ.4) TEXT=' Parameter value out of range.'
     	IF(I.EQ.5) TEXT=' Can not find/open the file.'
     	IF(I.EQ.6) TEXT=' Filename too long (max. 32 characters).'
     	IF(I.EQ.7) TEXT=' No input pie yet to work on.'
     	CALL IFANSI_VIDEO(70)
     	CALL PIEPLT_OOPS( TEXT )

999	RETURN
     	END

     	SUBROUTINE PIEPLT_OOPS( TEXT )
     	CHARACTER TEXT*(*)
     	NCHS = IFMISC_LENG(TEXT)
     	CALL PIEPLT_CLEAR_OOPS
     	CALL IFANSI_TEXT( TEXT, NCHS, 13, 6 )
     	RETURN
     	END

     	SUBROUTINE PIEPLT_CLEAR_OOPS
     	CALL IFANSI_CURSOR(13,6)
     	CALL IFANSI_CLEAR(4)
     	RETURN
     	END

     	SUBROUTINE PIEPLT_MENU_HELP
C
C	THIS ROUTINE DISPLAYS ON-LINE HELP FOR X,Y PLOT.
C
     	CHARACTER TEXT*80, KEY*4
     	COMMON/PIEPLT_MENU/ JSET, IKEY, NCHS, TEXT

     	NCHS = IFMISC_NEXT(TEXT)
     	IF( NCHS.EQ.2 ) THEN
     	    IKEY=IFMISC_HELP('GRAFIT$HLP:PIEPLT_LINE.HLP',TEXT,13,6)
     	ELSE
     	    IF( NCHS.EQ.0 ) THEN
     		KEY = 'HELP'
     	    ELSE 
     		KEY  = TEXT
     		IKEY = INDEX( TEXT(1:NCHS), '/ALL' )
     		IF( IKEY.EQ.0 )	IKEY =INDEX( TEXT(1:NCHS), '/MORE' )
     		IF( IKEY.EQ.0 )	IKEY =INDEX( TEXT(1:NCHS), '/FULL' )
     		IF( IKEY.GT.1 ) KEY  =TEXT(1:IKEY-1)
     	    ENDIF
     	    IKEY = IFMISC_TYPE('GRAFIT$HLP:PIEPLT_HELP.HLP',KEY,21)
     	    IF( IKEY.GT.0 ) GOTO 888
     	    CALL PIEPLT_FORM(JSET)
     	ENDIF
     	RETURN
C
C  ERROR TRANSFER...
C
888	CALL IFANSI_VIDEO(70)
     	IF(IKEY.EQ.1) THEN
     	   CALL PIEPLT_OOPS(' HELP file is not available.')
     	ELSE
     	   CALL PIEPLT_OOPS(' no HELP found for the keyword.')
     	ENDIF
     	RETURN
     	END

     	SUBROUTINE PIEPLT_SCALE
C
C	THIS ROUTINE SCALES AND REPACKS PIE LIST.
C
     	INCLUDE 'PIEPLT.CBS'

     	NPIE = 0
     	SUMP = 0.0
     	DO  I=1, MNP
     	    P = PIES(I)
     	    IF( P.GT.0.0 ) THEN
     		NPIE = NPIE + 1
     		SUMP = SUMP + P
     	    ENDIF
     	ENDDO
     	CALL PIEPLT_NPIE
     	IF( NPIE.GT.0 ) THEN
     	    DO  I=1, MNP
     		P = PIES(I)
     		IF( P.GT.0.0 ) PERC(I) = 100.0*P/SUMP
     	    ENDDO
     	ENDIF
     	RETURN
     	END

     	SUBROUTINE PIEPLT_DELPIE
C
C	THIS ROUTINE DELETES A VALID PIE FROM INPUT LIST.
C
     	INCLUDE		'PIEPLT.CBS'
    	CHARACTER	TEXT*80
     	COMMON/PIEPLT_MENU/  JSET, IKEY, NCHS, TEXT

     	IF( NCHS.EQ.0 ) THEN
     	    CALL IFANSI_TEXT('_Pie number ', 12, 14, 9 )
     	    READ(5,1001,END=999) TEXT
1001	    FORMAT(A)
     	    IF( TEXT.EQ.' ' ) GOTO 999
     	    NCHS = IFMISC_STRIP(TEXT)
     	ENDIF
     	DECODE( NCHS, 1000, TEXT, ERR=666 ) IKEY
1000	FORMAT(I)
     	IF( IKEY.LT.1 .OR. IKEY.GT.MNP ) THEN
     	    CALL PIEPLT_ERROR(4)
     	    GOTO 999
     	ENDIF
     	IF( PIES(IKEY).GT.0.0 ) THEN
     	    PIES(IKEY) = 0.0
     	    PERC(IKEY) = 0.0
     	    CALL PIEPLT_SCALE
     	    CALL PIEPLT_PARS_SET(JSET,0)
     	ENDIF
     	GOTO 999
666	CALL PIEPLT_ERROR(3)
999	RETURN
     	END

     	FUNCTION PIEPLT_NEXT(TEXT)
     	CHARACTER FNAME*64, TEXT*(*)
     	CALL IFANSI_TEXT('_Filename: ', 11, 14, 9 )
     	READ(5,1001,END=999) FNAME
1001	FORMAT(A)
     	NCHS = 0
     	IF( FNAME.NE.' ' ) THEN
     	    CALL   IFMISC_STRIP(FNAME)
     	    NCHS = IFMISC_UPPER(FNAME)
     	    TEXT = FNAME
     	ENDIF
     	PIEPLT_NEXT = NCHS
     	RETURN
999	PIEPLT_NEXT = 0
     	RETURN
     	END

