    	SUBROUTINE XYPLOT_MENU( ICODE )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY, FEB-1985
C
C  	INTERACTIVE DIALOGUE MENU FOR X,Y PLOT.
C	RETURN WITH "ICODE=1" IF QUITING.
C
    	INCLUDE		'XYPLOT.CBS'
     	PARAMETER	NKS = 47
     	CHARACTER	KWD(NKS)*2
    	CHARACTER	TEXT*80, KEY*2, SYM*1
     	EQUIVALENCE	(SYM,TEXT(3:3)), (KEY,TEXT(1:2))
     	COMMON/XYPLOT_MENU/  JSET, IKEY, NCHS, TEXT
C
C  ACCESS 2 LETTER CODES TO GRAPH PARAMETERS...
C
     	DATA KWD / 'F1', 'F2', 'F3', 'F4', 'GT', 'L1', 'L2', 'L3',
     + 'L4', 'C1', 'C2', 'GO', 'BO', 'SF', 'SE', 'MQ', 'NS', 'T1',
     + 'T2', 'T3', 'T4', 'X1', 'X2', 'X3', 'X4', 'S1', 'S2', 'S3',
     + 'S4', 'PN', 'SM', 'LI', 'DN', 'BC', 'BL', 'SS', 'XO', 'YO',
     + 'XS', 'YS', 'RL', 'PM', 'ID', 'LB', 'LM', 'LP', 'NO' /
C
C  PRESENT THE DIALOGUE MENU...
C
    	JSET = 1			!ACTIVE DATA SET POINTER
     	CALL XYPLOT_FORM( JSET )
     	IF( NSET.EQ.0 ) CALL XYPLOT_OOPS(' Enter a data filename
     + to start a plot. Type HELP if you need any.')
C
C  START THE DIALOGUE LOOP...
C
111	CALL IFANSI_VIDEO(47)
     	CALL IFANSI_TEXT('XYPLOT> ', 8, 22, 1 )
     	CALL IFANSI_CLEAR(4)
     	IKEY = IFQIOW( NCHS, TEXT )	!INPUT VIA QIOW
     	IF( IKEY.EQ.26) GOTO 888	!CTRL/Z PRESSED
     	IF( NCHS.EQ.0 ) GOTO 999	!BLANK INPUT
     	NCHS = IFMISC_STRIP(TEXT)
     	CALL   IFMISC_UPPER(KEY)
     	IF( KEY.EQ.'QU' ) GOTO 888
     	IF( KEY.EQ.'EX' ) GOTO 999
     	CALL XYPLOT_CLEAR_OOPS
C
C  COMMAND INPUT...
C
     	IF( SYM.NE.'='  ) THEN
     	    CALL XYPLOT_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 XYPLOT_ERROR(1)	!INVALID KEYWORD
     	GOTO 111
C
C  YES, WE HAVE A VALID KEYWORD...
C
222	IF( NSET.EQ.0 .AND. IKEY.NE.1 ) GOTO 777
     	TEXT = TEXT(4:)
     	NCHS = NCHS-3
     	IF( IKEY.LT.5 ) THEN
     	    CALL XYPLOT_FILES
     	    GOTO 111
     	ELSE IF( IKEY.LT.10 ) THEN
     	    IKEY = IKEY-4
     	    TITL(IKEY) = TEXT
     	    CALL XYPLOT_PARS_ALL(IKEY)
     	ELSE IF( IKEY.LT.18 ) THEN
     	    IF( NCHS.EQ.0 ) GOTO 666
     	    IKEY = IKEY-4
     	    CALL XYPLOT_OVERALL
     	ELSE IF( IKEY.LT.22 ) THEN
     	    IF( NCHS.EQ.0 ) GOTO 666
     	    IKEY = IKEY-17
     	    CALL XYPLOT_AXIS_A
     	ELSE IF( IKEY.LT.26 ) THEN
     	    IF( NCHS.EQ.0 ) GOTO 666
     	    IKEY = IKEY-21
     	    CALL XYPLOT_AXIS_B
     	ELSE IF( IKEY.LT.30 ) THEN
     	    IF( NCHS.EQ.0 ) GOTO 666
     	    IKEY = IKEY-25
     	    CALL XYPLOT_AXIS_C
     	ELSE IF( IKEY.LT.42 ) THEN
     	    IF( NCHS.EQ.0 ) GOTO 666
     	    IKEY = IKEY-29
     	    CALL XYPLOT_SET
     	ELSE IF( IKEY.EQ.42 ) THEN
     	    CALL XYPLOT_SET_PM
     	ELSE IF( IKEY.EQ.43 ) THEN
     	    LABL(LSET(1,JSET)) = TEXT
     	    CALL XYPLOT_PARS_SET(JSET,14)
     	ELSE
     	    IF( NCHS.EQ.0 ) GOTO 666
     	    IKEY = IKEY-43
     	    CALL XYPLOT_LID
     	ENDIF
     	GOTO 111
C
C  ERROR TRANSFER...
C
666	CALL IFANSI_VIDEO(70)
     	CALL XYPLOT_OOPS(' Missing input parameter.')
     	GOTO 111
777	CALL XYPLOT_ERROR(6)	!NO DATA YET TO WORK ON
     	GOTO 111
888	ICODE = 1		!QUIT FROM THE DIALOGUE
     	RETURN
999	IF( NSET.EQ.0 ) GOTO 888
     	CALL XYPLOT_SETUP( 0, PARNAM )
     	ICODE = 0
     	RETURN
    	END

     	SUBROUTINE XYPLOT_FORM( JSET )
C
C	THIS ROUTINE LAYOUTS THE DIALOGUE MENU FOR X,Y PLOT.
C
    	INCLUDE 'XYPLOT.CBS'
C
C  LAYOUT THE MENU...
C
     	CALL IFINIT_LINE(24)
     	CALL IFANSI_TITLE('GRAFIT: X,Y Plot (limits:     data sets,
     +       points/set)')
     	CALL IFANSI_VIDEO(60)
     	CALL IFANSI_IFL( MNS, 1, 3, 1, 27 )
     	CALL IFANSI_IFL( MNP, 1, 4, 1, 43 )

     	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='/' L1=',35X,'L2=',34X,'C1='/' L3=',35X,'L4=',34X,'C2=')
     	IF( CRT.NE.2 ) CALL IFANSI_VIDEO(90)
     	WRITE(6,1002)
1002	FORMAT(' GO=',19X,'BO=',13X,'SF=',20X,'SE=    MQ=    NS=')

     	IF( CRT.EQ.2 ) CALL IFANSI_VIDEO(50)
     	CALL IFANSI_VIDEO(90)
     	WRITE(6,2000)
2000	FORMAT('      * * * * *   A X I S   D R A W I N G'
     +,'   P A R A M E T E R S   * * * * *      ')
     	IF( CRT.EQ.2 ) CALL IFANSI_VIDEO(50)
     	WRITE(6,2001)
2001	FORMAT('     TY DV dv DG PT     LENG TOFF XOFF  ROTA',
     +'        LOW        HIGH       STEP   ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,2002)
2002	FORMAT(' T1=',16X,'X1=',22X,'S1='/
     +,' T2=',16X,'X2=',22X,'S2='/' T3=',16X,'X3=',22X,'S3=')
     	IF( CRT.NE.2 ) CALL IFANSI_VIDEO(90)
     	WRITE(6,2003)
2003	FORMAT(' T4=',16X,'X4=',22X,'S4=')

     	IF( CRT.EQ.2 ) CALL IFANSI_VIDEO(50)
     	CALL IFANSI_VIDEO(90)
     	WRITE(6,3000)
3000	FORMAT('      * * *   D A T A   S E T   D R A W I N G'
     +,'   P A R A M E T E R S   * * *      ')
     	IF( CRT.EQ.2 ) CALL IFANSI_VIDEO(50)
     	WRITE(6,3001)
3001	FORMAT('  NO: PN  SM  LI  DN  BC  BL     SS    XO    YO'
     +,'     XS         YS          RL    ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,3002)
3002	FORMAT(X,'   :'/'  PM=',9X,'ID=',37X,'LB=   LM=   LP=')
     	IF( CRT.NE.2 ) CALL IFANSI_VIDEO(90)
     	WRITE(6,3003)
3003	FORMAT(
     +' PTS:'6X'XL:'11X'XH:'11X'YL:'11X'YH:'11X'TOTAL SETS:')

     	IF( CRT.EQ.2 ) THEN
     	    CALL IFANSI_VIDEO(50)
     	ELSE
     	    CALL IFANSI_VIDEO(90)
     	ENDIF
     	WRITE(6,4000)
4000	FORMAT('     * * * * * * * *   I N P U T  &  O U T P U T'
     +,'   F I L E S   * * * * * * *     ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,4001)
4001	FORMAT(' F1=',17X,'F2=',17X,'F3=',17X,'F4=')
     	CALL IFANSI_VIDEO(90)
     	WRITE(6,4002)
4002	FORMAT(X,80(' '))
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,5000)
5000	FORMAT(' oops:'//' Auxiliary: HELP/PF2, DCL/PF4,',
     +' DOWN_SET/PF1, UP_SET/PF3, LIST, MISC, REFRESH,'/
     +' _commands: EXIT, QUIT, READ, PLOT, OVERLAY, SETUP,',
     +' LAYOUT, DELETE, RESET, ...')
C
C  INSERT THE PARAMETERS...
C
     	CALL XYPLOT_PARS(JSET)
     	RETURN
     	END

     	SUBROUTINE XYPLOT_PARS(JSET)
C
C	THIS ROUTINE INSERTS ALL GRAPH PARAMETERS.
C
     	CALL XYPLOT_PARS_ALL(0)		!OVERALL PLOT PARAMETER GROUP
     	CALL XYPLOT_PARS_AXIS(0,0)	!AXIS DRAWING PARAMETER GROUP
     	CALL XYPLOT_PARS_SET(JSET,0)	!DATA SET DRAWING PARAMETER GROUP
     	CALL XYPLOT_PARS_LID(0)		!DATA SET ID POSITION
     	CALL XYPLOT_PARS_NSET		!TOTAL NUMBER OF DATA SETS
     	CALL XYPLOT_PARS_FILE(0)	!I/O FILE GROUP
     	RETURN
    	END

     	SUBROUTINE XYPLOT_PARS_ALL( ICODE )
C
C	THIS ROUTINE DISPLAYS OVERALL PLOT PARAMETERS.
C
     	INCLUDE 'XYPLOT.CBS'
     	DIMENSION RP(2)
     	CHARACTER YES*3

     	CALL IFANSI_VIDEO(30)
     	I = ICODE
    	IF(I.EQ.0.OR.I.EQ.1) CALL IFANSI_TEXT( TITL(1), 77, 3, 4 )
    	IF(I.EQ.0.OR.I.EQ.2) CALL IFANSI_TEXT( TITL(2), 34, 4, 4 )
    	IF(I.EQ.0.OR.I.EQ.3) CALL IFANSI_TEXT( TITL(3), 33, 4, 42)
    	IF(I.EQ.0.OR.I.EQ.4) CALL IFANSI_TEXT( TITL(4), 34, 5, 4 )
    	IF(I.EQ.0.OR.I.EQ.5) CALL IFANSI_TEXT( TITL(5), 33, 5, 42)
    	IF(I.EQ.0.OR.I.EQ.6) CALL IFANSI_IFL( COL1, 1, 2, 4, 79 )
    	IF(I.EQ.0.OR.I.EQ.7) CALL IFANSI_IFL( COL2, 1, 2, 5, 79 )

    	IF(I.EQ.0.OR.I.EQ.8) THEN
     	   RP(1) = XORG
     	   RP(2) = YORG
     	   CALL IFANSI_FFL( RP, 2, 6.2, 6, 4 )
     	ENDIF
     	IF(I.EQ.0.OR.I.EQ.9) THEN
     	   RP(1) = XOFF
     	   RP(2) = YOFF
    	   CALL IFANSI_FFL( RP, 2, 6.2, 6, 26)
     	ENDIF
     	IF(I.EQ.0.OR.I.EQ.10) THEN
     	   RP(1) = XSCL
     	   RP(2) = YSCL
    	   CALL IFANSI_FFL( RP, 2, 6.3, 6, 42)
     	ENDIF
    	IF(I.EQ.0.OR.I.EQ.11) THEN
     	   YES = 'NO'
     	   IF( INIT ) YES = 'YES'
     	   CALL IFANSI_TEXT( YES, 3, 6, 65 )
     	ENDIF
    	IF(I.EQ.0.OR.I.EQ.12) THEN
     	   YES = 'NO'
     	   IF( QUAD.EQ.1 ) YES = 'YES'
     	   CALL IFANSI_TEXT( YES, 3, 6, 72 )
     	ENDIF
     	IF(I.EQ.0.OR.I.EQ.13) CALL IFANSI_IFL( NSDS, 1, 2, 6, 79)

     	RETURN
     	END

     	SUBROUTINE XYPLOT_PARS_AXIS( ICODE, JCODE )
C
C	THIS ROUTINE DISPLAYS AXIS DRAWING PARAMETERS.
C
     	INCLUDE 'XYPLOT.CBS'
     	DIMENSION KP(5), RP(5)

     	CALL IFANSI_VIDEO(30)
     	I1 = 1
     	I2 = 4
     	IF( ICODE.GT.0 ) THEN
     	    I1 = ICODE
     	    I2 = ICODE
     	ENDIF
     	J = JCODE
     	DO  I=I1, I2
     	    IF( J.EQ.0 .OR. J.EQ.1 ) THEN
     		KP(1) = LAXS(I)
     		KP(2) = NTKS(I)
     		KP(3) = MTKS(I)
     		KP(4) = NDGS(I)
     		KP(5) = IGDS(I)
     		CALL IFANSI_IFL( KP, 5, 3, I+8, 4 )
     	    ENDIF
     	    IF( J.EQ.0 .OR. J.EQ.2 ) THEN
     		RP(1) = XLNS(I)
     		RP(2) = OFFS(I)
     		RP(3) = SOFF(I)
     		CALL IFANSI_FFL( RP, 3, 5.2, I+8, 23 )
     		CALL IFANSI_FFL( XANG(I), 1, 6.2, I+8, 38 )
     	    ENDIF
     	    IF( J.EQ.0 .OR. J.EQ.3 ) THEN
     		RP(1) = XLOW(I)
     		RP(2) = XHIG(I)
     		RP(3) = XINC(I)
     		CALL IFANSI_GFL( RP, 3, 11.4, I+8, 48 )
     	    ENDIF
     	ENDDO
     	RETURN
     	END

	SUBROUTINE XYPLOT_PARS_SET( ICODE, IPAR )
C
C	THIS ROUTINE DISPLAYS DATA SET DRAWING PARAMETERS.
C
    	INCLUDE  'XYPLOT.CBS'
     	CHARACTER PLOT*7

     	JSET = ICODE
     	I = IPAR
     	IF( I.EQ.0 ) THEN
     	    CALL IFANSI_VIDEO(47)
    	    CALL IFANSI_IFL( JSET, 1, 3, 15, 1 )
     	ENDIF
     	CALL IFANSI_VIDEO(30)
    	IF(I.EQ.0.OR.I.EQ.1) CALL IFANSI_IFL(IPEN(JSET),1,3,15,5)
    	IF(I.EQ.0.OR.I.EQ.2) CALL IFANSI_IFL(ISYM(JSET),1,3,15,9)
    	IF(I.EQ.0.OR.I.EQ.3) CALL IFANSI_IFL(ILIN(JSET),1,3,15,13)
    	IF(I.EQ.0.OR.I.EQ.4) CALL IFANSI_IFL(IDEN(JSET),1,3,15,17)
    	IF(I.EQ.0.OR.I.EQ.5) CALL IFANSI_IFL(ISET(JSET),1,3,15,21)
    	IF(I.EQ.0.OR.I.EQ.6) CALL IFANSI_IFL(IBOL(JSET),1,3,15,25)
    	IF(I.EQ.0.OR.I.EQ.7) CALL IFANSI_FFL(SSIZ(JSET),1,5.2,15,31)
    	IF(I.EQ.0.OR.I.EQ.8) CALL IFANSI_FFL(XSH(JSET),1,5.2,15,37)
    	IF(I.EQ.0.OR.I.EQ.9) CALL IFANSI_FFL(YSH(JSET),1,5.2,15,43)
    	IF(I.EQ.0.OR.I.EQ.10) CALL IFANSI_GFL(XSF(JSET),1,10.3,15,49)
    	IF(I.EQ.0.OR.I.EQ.11) CALL IFANSI_GFL(YSF(JSET),1,10.3,15,60)
    	IF(I.EQ.0.OR.I.EQ.12) CALL IFANSI_GFL(SRL(JSET),1,10.3,15,71)
     	IF(I.EQ.0.OR.I.EQ.13) THEN
     	    K = ISHD(JSET)
     	    PLOT = 'DEFAULT'
     	    IF( K.EQ. 1 ) PLOT = 'FILL'
     	    IF( K.EQ.-1 ) PLOT = 'LOOP'
     	    IF( K.EQ. 2 ) PLOT = 'STICK'
     	    IF( K.EQ. 3 ) PLOT = 'BAR'
    	    CALL IFANSI_TEXT( PLOT, 7, 16, 5 )
     	ENDIF
     	K = LSET(1,JSET)
     	IF(I.EQ.0.OR.I.EQ.14) CALL IFANSI_TEXT( LABL(K),31,16,17 )
     	IF( I.EQ.0 ) THEN
     	    I = IFMISC_DIGITS( NUMS(K) )
     	    CALL IFANSI_VIDEO(60)
     	    CALL IFANSI_IFL( NUMS(K), 1, I, 17, 5 )
     	    IF( I.LT.5 ) CALL IFANSI_TEXT( '    ', 5-I, 17, 5+I )
     	    CALL IFANSI_GFL( SRNG(1,K), 1, 10.3, 17, 14 )
     	    CALL IFANSI_GFL( SRNG(2,K), 1, 10.3, 17, 28 )
     	    CALL IFANSI_GFL( SRNG(3,K), 1, 10.3, 17, 42 )
     	    CALL IFANSI_GFL( SRNG(4,K), 1, 10.3, 17, 56 )
     	ENDIF

     	RETURN
	END

     	SUBROUTINE XYPLOT_PARS_LID( ICODE )
     	INCLUDE 'XYPLOT.CBS'
     	DIMENSION RP(2)

     	CALL IFANSI_VIDEO(30)
     	I = ICODE
     	IF(I.EQ.0.OR.I.EQ.1) CALL IFANSI_IFL( DRAW, 1, 2, 16, 57 )
     	IF(I.EQ.0.OR.I.EQ.2) CALL IFANSI_IFL( MARK, 1, 2, 16, 63 )
    	IF(I.EQ.0.OR.I.EQ.3) THEN
     	   RP(1) = XLPS
     	   RP(2) = YLPS
     	   CALL IFANSI_FFL( RP, 2, 6.2, 16, 69 )
     	ENDIF
     	RETURN
     	END

     	SUBROUTINE XYPLOT_PARS_FILE( ICODE )
C
C	THIS ROUTINE DISPLAYS FILENAMES.
C
     	INCLUDE 'XYPLOT.CBS'
     	CHARACTER DNAME*32

     	I = ICODE
     	CALL IFANSI_VIDEO(20)
     	IF( I.EQ.0 .OR. I.EQ.1 ) THEN
     	    DNAME = DATNAM
     	    K = INDEX( DNAME, ']' )
     	    IF( K.GT.0 ) DNAME = DNAME(K+1:)
     	    CALL IFANSI_TEXT( DNAME, 16, 19, 4 )
     	ENDIF
     	IF( I.EQ.0 .OR. I.EQ.2 ) THEN
     	    DNAME = ADDNAM
     	    K = INDEX( DNAME, ']' )
     	    IF( K.GT.0 ) DNAME = DNAME(K+1:)
     	    CALL IFANSI_TEXT( DNAME, 16, 19, 24)
     	ENDIF
     	IF( I.EQ.0 .OR. I.EQ.3 ) THEN
     	    DNAME = PARNAM
     	    K = INDEX( DNAME, ']' )
     	    IF( K.GT.0 ) DNAME = DNAME(K+1:)
     	    CALL IFANSI_TEXT( DNAME, 16, 19, 44)
     	ENDIF
     	IF( I.EQ.0 .OR. I.EQ.4 ) THEN
     	    DNAME = DESNAM
     	    K = INDEX( DNAME, ']' )
     	    IF( K.GT.0 ) DNAME = DNAME(K+1:)
     	    CALL IFANSI_TEXT( DNAME, 17, 19, 64)
     	ENDIF

     	RETURN
     	END

     	SUBROUTINE XYPLOT_PARS_NSET

     	INCLUDE 'XYPLOT.CBS'

     	CALL IFANSI_VIDEO(24)
     	CALL IFANSI_VIDEO(80)
     	CALL IFANSI_IFL( NSET, 1, 3, 17, 78 )

     	RETURN
     	END

     	SUBROUTINE XYPLOT_COM
C
C	THIS ROUTINE IMPLEMENTS SOME DIALOGUE COMMANDS.
C
C	LIST OF COMMANDS:
C
C	HELP(PF2) : display help information.
C	DCL(PF4)  : spawn a system subprocess.
C	DOWN(PF1) : step data set pointer down by one.
C	UP(PF3)   : step data set pointer back by one.
C	REFRESH   : re-layout the dialogue menu.
C	RESET     : reset graph parameters.
C	SAVE      : save graph parameters.
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	WRITE     : write out input data.
C	SETUP     : use another setup/parameter file.
C	LAYOUT    : use another layout file.
C	LIST      : list all data set parameters.
C	MISC      : perform data manipulations.
C	OVERLAY/INCLUDE: overlay another data file.
C	DELETE/REMOVE  : remove or delete a set of input data.
C
     	INCLUDE   'XYPLOT.CBS'
    	CHARACTER TEXT*80
     	COMMON/XYPLOT_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 XYPLOT_FILES
     		RETURN
     	    ENDIF
     	ENDIF
     	IF( TEXT(1:4).EQ.'DOWN'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OP') ) THEN
C
C  DOWN ONE ACTIVE-DATA-SET POINTER...
C
     	    IF( NSET.EQ.0 ) GOTO 888
     	    JSET = JSET + 1
     	    IF( JSET.GT.NSET ) JSET = 1
     	    CALL XYPLOT_PARS_SET(JSET,0)
     	ELSE IF( TEXT(1:2).EQ.'UP'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OR') ) THEN
C
C  UP ONE ACTIVE-DATA-SET POINTER...
C
     	    IF( NSET.EQ.0 ) GOTO 888
     	    JSET = JSET - 1
     	    IF( JSET.LT.1 ) JSET = NSET
     	    CALL XYPLOT_PARS_SET(JSET,0)
     	ELSE IF( TEXT(1:4).EQ.'LIST' ) THEN
C
C  LIST...
     	    IF( NSET.EQ.0 ) GOTO 888
     	    CALL XYPLOT_LIST( JSET )
     	ELSE IF( TEXT(1:4).EQ.'HELP'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OQ') ) THEN
C
C  HELP...
     	    CALL XYPLOT_MENU_HELP
     	ELSE IF( TEXT(1:3).EQ.'REF' ) THEN
C
C  REFRESH...
     	    CALL XYPLOT_FORM(JSET)
     	ELSE IF( TEXT(1:5).EQ.'RESET' ) THEN
C
C  RESET...
     	    IF( NSET.EQ.0 ) GOTO 888
     	    CALL XYPLOT_RESET( JSET )
     	ELSE IF( TEXT(1:4).EQ.'SAVE' ) THEN
C
C  SAVE...
     	    IF( NSET.EQ.0 ) GOTO 888
     	    CALL XYPLOT_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 XYPLOT_FILES
     	ELSE IF( TEXT(1:4).EQ.'OVER' .OR. TEXT(1:3).EQ.'INC' ) THEN
C
C  OVERLAY...
     	    IKEY = 2
     	    NCHS = IFMISC_NEXT(TEXT)
     	    CALL XYPLOT_FILES
     	ELSE IF( TEXT(1:5).EQ.'SETUP' ) THEN
C
C  SETUP...
     	    IKEY = 3
     	    NCHS = IFMISC_NEXT(TEXT)
     	    CALL XYPLOT_FILES
     	ELSE IF( TEXT(1:6).EQ.'LAYOUT' ) THEN
C
C  LAYOUT...
     	    IKEY = 4
     	    NCHS = IFMISC_NEXT(TEXT)
     	    CALL XYPLOT_FILES
     	ELSE IF( TEXT(1:5).EQ.'WRITE' ) THEN
C
C  WRITE...
     	    IF( NSET.EQ.0 ) GOTO 888
     	    CALL XYPLOT_WRITE
     	ELSE IF( TEXT(1:4).EQ.'MISC' ) THEN
C
C  MISC...
     	    IF( NSET.EQ.0 ) GOTO 888
     	    CALL XYPLOT_MISC
     	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 XYPLOT_FORM(JSET)
     	ELSE IF( TEXT(1:3).EQ.'DEL' .OR. TEXT(1:3).EQ.'REM' ) THEN
C
C  REMOVE...
     	    IF( NSET.EQ.0 ) GOTO 888
     	    CALL XYPLOT_DELETE
     	ELSE
     	    IF( IKEY.NE.27 ) THEN
     		CALL XYPLOT_ERROR(2)	!UNDEFINED COMMAND
     	    ELSE
     		CALL IFANSI_VIDEO(70)
     		CALL XYPLOT_OOPS(' Undefined function key.')
     	    ENDIF
     	ENDIF
     	RETURN
888	CALL XYPLOT_ERROR(6)	!NO DATA YET TO WORK ON
     	RETURN
     	END

     	SUBROUTINE XYPLOT_RESET( JSET )
C
C	THIS ROUTINE RESETS THE PLOT.
C
     	CALL XYPLOT_CLEAR_OOPS
     	CALL XYPLOT_OOPS(' Resetting the plot...')
     	CALL XYPLOT_INIT(1)
     	CALL XYPLOT_SCALE(1)
     	CALL XYPLOT_SCALE(2)
     	CALL XYPLOT_PARS(JSET)
     	CALL XYPLOT_CLEAR_OOPS

     	RETURN
     	END

     	SUBROUTINE XYPLOT_FILES
C
C	THIS ROUTINE GETS INPUT DATA/PARAMETER/LAYOUT FILES.
C
     	INCLUDE		'XYPLOT.CBS'
     	CHARACTER	TERR*80, TEXT*80
     	COMMON/XYPLOT_MENU/  JSET, IKEY, NCHS, TEXT

     	JKEY = IKEY
     	IKEY = IABS(IKEY)
     	IF( NCHS.EQ.0  ) THEN
     	    NCHS = XYPLOT_NEXT(TEXT)
     	    IF( NCHS.EQ.0 ) RETURN
     	ENDIF
     	JUNK = INDEX( TEXT, '/' )
     	IFL1 = 0
     	IFL2 = 0
     	IFL3 = 0
     	IF( JUNK.GT.0 ) THEN
     	    IF( NSET.GT.0 ) IFL1 = INDEX( TEXT, '/NOINI' )
     	    IFL2 = INDEX( TEXT, '/NOSET' )
     	    IFL3 = INDEX( TEXT, '/NOLAY' )
     	    TEXT = TEXT(1:JUNK-1)
     	    NCHS = IFMISC_LENG(TEXT)
     	ENDIF
     	IF( NCHS.GT.32 ) THEN
     	    CALL XYPLOT_ERROR(9)	!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
     	KSET = 0
     	IKEY = XYPLOT_DATA( TEXT, MNS, MNP, KSET, NUMS, X, Y, LABL )
     	IF( IKEY.EQ.1 ) GOTO 666
     	IF( IKEY.EQ.2 ) GOTO 777
     	IF( KSET.EQ.0 ) GOTO 555
C
C  YES, WE HAVE A NEW PLOT...
C
     	JSET = 1
     	NSET = 0
     	MSET = KSET
     	CALL XYPLOT_RANGE( 1, KSET )
     	ADDNAM = ' '
     	DATNAM = TEXT
     	PARNAM = DATNAM
     	CALL IFMISC_NAMEXT( PARNAM, '.PAR' )
     	DESNAM = ' '
     	IF( IFL3.EQ.0 ) THEN
     	    DESNAM = DATNAM
     	    CALL IFMISC_NAMEXT( DESNAM, '.DES' )
     	ENDIF
     	TEXT = ' '
     	KSET = IFMISC_DIGITS(NSET)+1
     	ENCODE( KSET, 1000, TEXT ) NSET
1000	FORMAT(I<KSET>)
     	TEXT(KSET+2:) = 'set(s) of data have been read from'
     	TEXT(KSET+37:) = DATNAM
     	CALL IFANSI_VIDEO(20)
     	CALL XYPLOT_OOPS( TEXT )
     	IF( IFL1.EQ.0 ) CALL XYPLOT_INIT(1)
     	IKEY = 0
     	IF( IFL2.EQ.0 ) IKEY = XYPLOT_SETUP( 1, PARNAM )
     	IF( IKEY.EQ.0 .AND. IFL1.EQ.0 ) THEN
     	    CALL XYPLOT_SCALE(1)
     	    CALL XYPLOT_SCALE(2)
     	ENDIF
     	IF( JKEY.GT.0 .AND. (IFL1.EQ.0.OR.IFL2.EQ.0) ) THEN
     	    CALL IFANSI_VIDEO(30)
     	    CALL XYPLOT_PARS(JSET)
     	ELSE
     	    IKEY = JKEY
     	ENDIF
     	GOTO 999

222	IF( IKEY.NE.2 ) GOTO 333
     	IF( MSET.EQ.MNS ) THEN
     	    CALL IFANSI_VIDEO(70)
     	    CALL XYPLOT_OOPS(' Maximum number of data sets hit.')
     	    GOTO 999
     	ENDIF
C
C  OVERLAY A DATA FILE...
C
     	KSET = 0
     	IKEY = MSET + 1
     	IKEY = XYPLOT_DATA( TEXT, MNS-MSET, MNP, KSET,
     +         NUMS(IKEY), X(1,IKEY), Y(1,IKEY), LABL(IKEY) )
     	IF( IKEY.EQ.1 ) GOTO 666
     	IF( IKEY.EQ.2 ) GOTO 777
     	IF( KSET.EQ.0 ) GOTO 555
C
C  YES, OVERLAY THE FILE...
C
     	IKEY = NSET
     	CALL XYPLOT_RANGE( MSET+1, MSET+KSET )
     	MSET = MSET + KSET
     	CALL XYPLOT_PARS_NSET
     	KSET = NSET - IKEY
     	ADDNAM = TEXT
     	CALL XYPLOT_PARS_FILE(2)
     	TEXT = ' '
     	NCHS = IFMISC_DIGITS(KSET)+1
     	ENCODE( NCHS, 2000, TEXT ) KSET
2000	FORMAT(I<NCHS>)
     	TEXT(NCHS+2:) = 'set(s) of data have been read from file'
     	TEXT(NCHS+37:) = ADDNAM
     	CALL IFANSI_VIDEO(20)
     	CALL XYPLOT_OOPS( TEXT )
     	JSET = NSET
     	CALL XYPLOT_PARS_SET(JSET,0)
     	GOTO 999

333	IF( IKEY.NE.3 ) GOTO 444
C
C  USE ANOTHER SETUP FILE ...
C
     	CALL IFMISC_DEFEXT( TEXT, '.PAR' )
     	IKEY = XYPLOT_SETUP( 1, TEXT )
     	IF( IKEY.EQ.0 ) THEN
     	    CALL IFANSI_VIDEO(70)
     	    TERR = ' can not find setup file'
     	    TERR(26:) = TEXT
     	    CALL XYPLOT_OOPS( TERR )
     	ELSE
     	    CALL IFANSI_VIDEO(30)
     	    CALL XYPLOT_PARS(JSET)
     	ENDIF
     	GOTO 999

444	IF( IKEY.NE.4 ) 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 XYPLOT_PARS_FILE(4)
     	GOTO 999
C
C  ERROR TRANSFER...
C
555	CALL IFANSI_VIDEO(70)
     	CALL XYPLOT_OOPS(' No data found in the file.')
     	GOTO 999
666	CALL XYPLOT_ERROR(5)	!MISSING FILE
     	GOTO 999
777	TEXT = ' Error in reading data from record/line #'
     	NCHS = IFMISC_DIGITS(KSET)
     	ENCODE( NCHS, 778, TEXT(42:) ) KSET
778	FORMAT(I<NCHS>)
     	CALL IFANSI_VIDEO(70)
     	CALL XYPLOT_OOPS( TEXT )
999	RETURN
     	END

     	SUBROUTINE XYPLOT_RANGE( I1, I2 )
C
C	THIS ROUTINE GETS DATA RANGES FOR X,Y PLOT
C
     	INCLUDE 'XYPLOT.CBS'

     	DO  I = I1, I2
     	    NPTS = NUMS(I)
     	    IF( NPTS.NE.0 ) THEN	!NON-ERROR-BAR SET
     		NSET = NSET + 1
     		LSET(1,NSET) = I
     		IF( NPTS.GT.0 ) THEN
     		    LSET(2,NSET) = 0
     		ELSE
     		    NPTS = -NPTS
     		    NUMS(I) = NPTS
     		    LSET(2,NSET) = 1	!NEXT SET IS ERROR BAR
     		ENDIF
     		CALL IFMISC_RANGE( NPTS, X(1,I), Y(1,I), SRNG(1,I) )
     	    ENDIF
     	ENDDO

     	RETURN
     	END

    	SUBROUTINE XYPLOT_INIT( ICODE )
C
C	THIS ROUTINE INITIALIZES THE COMMON BLOCKS FOR X,Y PLOT.
C

    	INCLUDE 'XYPLOT.CBS'

     	IF( ICODE.EQ.0 ) THEN
     	    CRT = IFINIT_ITM(0)		!GET THE TYPE OF TERMINAL
     	    DATNAM = ' Data file here'
     	    ADDNAM = ' Additional file'
     	    PARNAM = ' Setup file'
     	    DESNAM = ' Layout file'
    	    DO I=1,5
     	       TITL(I) = ' '
    	    ENDDO
     	    DO I=1, MNS
     	       NUMS(I) = 0
     	       LSET(1,I) = I
     	       LSET(2,I) = 0
    	       LABL(I) = ' '
     	       DO J=1, 4
     		  SRNG(J,I) = 0.0
     	       ENDDO
     	    ENDDO
     	    NSET = 0
     	    MSET = 0
     	    INIT = .TRUE.
	    PLOTID = ' '
     	ENDIF

     	DO I=1, 4
     	   LAXS(I) = 1
    	   NTKS(I) = 1
    	   MTKS(I) = 1
    	   NDGS(I) = 0
    	   IGDS(I) = 11
    	   OFFS(I) = 0.0
     	   SOFF(I) = 0.0
     	   XANG(I) = 0.0
     	   XLOW(I) = 0.0
     	   XHIG(I) = 0.0
     	   XINC(I) = 0.0
    	ENDDO
     	XLNS(1) = 6.0
     	XLNS(2) = 4.0
     	XLNS(3) = 6.0
     	XLNS(4) = 4.0

     	DO I=1, MNS
     	   QUAD = MOD(I,7)
     	   IF( QUAD.EQ.0 ) QUAD = 7
     	   IPEN(I) = QUAD
     	   ISYM(I) = 0
     	   ILIN(I) = 1
    	   ISET(I) = 1
    	   IDEN(I) = 1
    	   IBOL(I) = 1
    	   ISHD(I) = 0
    	   SSIZ(I) = 0.1
    	    XSH(I) = 0.0
    	    YSH(I) = 0.0
    	    XSF(I) = 1.0
    	    YSF(I) = 1.0
    	    SRL(I) = 0.0
    	ENDDO

    	BAND = .FALSE.
    	QUAD = 0
    	NSDS = 1
    	COL1 = 1
    	COL2 = 1
     	DRAW = 2
     	MARK = 1
     	XLPS = 0.5
     	YLPS = 0.5
     	XORG = 2.25
     	YORG = 2.25
     	XOFF = 0.00
     	YOFF = 0.00
     	XSCL = 1.00
     	YSCL = 1.00

    	RETURN
    	END

	SUBROUTINE XYPLOT_OVERALL
C
C	THIS ROUTINE CHANGES OVREALL PLOT PARAMETERS.
C
    	INCLUDE		'XYPLOT.CBS'
     	CHARACTER	TEXT*80
     	DIMENSION	RP(2), KP(2)
     	COMMON/XYPLOT_MENU/ JSET, IKEY, NCHS, TEXT
C
C  DECODE THE PARAMETERS...
C
     	IF( IKEY.NE.11 .AND. IKEY.NE.12 ) THEN
     	    NCHS = IFMISC_DECODE( TEXT, 2, RP, KP )
    	    IF( NCHS.EQ.0 ) THEN
     		CALL XYPLOT_ERROR(3)
     		RETURN
     	    ENDIF
     	ENDIF

     	IF( IKEY.EQ.6 ) THEN		!1ST BAND AXIS COLOR
     	    IF(RP(1).LT.1.0.OR.RP(1).GT.8.0) GOTO 777
     	    COL1 = RP(1)
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.7 ) THEN		!2ND BAND AXIS COLOR
     	    IF(RP(1).LT.1.0.OR.RP(1).GT.8.0) GOTO 777
     	    COL2 = RP(1)
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.8 ) THEN		!GRAPH ORIGIN
    	    IF( KP(1).EQ.1 ) THEN
     		IF(RP(1).LT.0.0.OR.RP(1).GT.99.0) GOTO 777
     		XORG = RP(1)
    	    ENDIF
    	    IF( KP(2).EQ.1 ) THEN
     		IF(RP(2).LT.0.0.OR.RP(2).GT.99.0) GOTO 777
    	       	YORG = RP(2)
    	    ENDIF
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.9 ) THEN		!2ND BAND OFFSETS
    	    IF( KP(1).EQ.1 ) THEN
     		IF(RP(1).LT.-99.0.OR.RP(1).GT.99.0) GOTO 777
    	       	XOFF = RP(1)
    	    ENDIF
    	    IF( KP(2).EQ.1 ) THEN
     		IF(RP(2).LT.-99.0.OR.RP(2).GT.99.0) GOTO 777
    	       	YOFF = RP(2)
    	    ENDIF
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.10 ) THEN		!OVERALL SCALE FACTORS
    	    IF( KP(1).EQ.1 ) THEN
     		IF(RP(1).LT.0.1.OR.RP(1).GT.3.0) GOTO 777
    	       	XSCL = RP(1)
    	    ENDIF
    	    IF( KP(2).EQ.1 ) THEN
     		IF(RP(2).LT.0.1.OR.RP(2).GT.3.0) GOTO 777
    	       	YSCL = RP(2)
    	    ENDIF
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.11 ) 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.12 ) THEN		!QUADRANT CODE
     	    QUAD = 0
     	    IF( TEXT(1:1).EQ.'Y' .OR. TEXT(1:1).EQ.'y'
     +     .OR. TEXT(1:1).EQ.'T' .OR. TEXT(1:1).EQ.'t' ) QUAD = 1
     	    IF( QUAD.NE.0 ) THEN
     		IF( BAND ) QUAD = 0
     		IF( LAXS(1).LT.0.AND.LAXS(2).LT.0) QUAD= 0
     		IF( IABS(LAXS(1)).NE.1.AND.IABS(LAXS(2)).NE.1) QUAD= 0
     	    ENDIF
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.13 ) THEN		!SD BAR PLOTTING CODE
     	    IF(RP(1).LT.0.0.OR.RP(1).GT.3.0) GOTO 777
     	    NSDS = RP(1)
     	    GOTO 888
     	ENDIF

777	CALL XYPLOT_ERROR(4)
     	RETURN
888	CALL XYPLOT_PARS_ALL(IKEY)
     	RETURN
	END

	SUBROUTINE XYPLOT_SET
C
C	THIS ROUTINE CHANGES DATA SET DRAWING PARAMETERS
C	IN COLUMN-RUN MODE.
C
    	INCLUDE		'XYPLOT.CBS'
     	CHARACTER	TEXT*80
     	COMMON/XYPLOT_MENU/ JSET, IKEY, NCHS, TEXT
     	DIMENSION	KP(MNS), RP(MNS)
     	LOGICAL		REPEAT
C
C  CHECK TO SEE IF THERE IS ANY SPECIAL CHARACTER IN THE INPUT...
C
     	KSET = 1
     	REPEAT = .FALSE.
     	CALL IFMISC_UPPER(TEXT)
     	I = INDEX( TEXT(1:NCHS), 'I' )		!JUMP TO SET "I"
     	IF( I.EQ.0 ) THEN
     	    I = INDEX( TEXT(1:NCHS), 'R' )	!REPEAT PARAMETERS
     	    IF( I.GT.0 ) REPEAT = .TRUE.
     	ENDIF
     	IF( I.EQ.NCHS ) NCHS = NCHS - 1
     	IF( I.GT.0 .AND. I.LT.NCHS ) THEN	!GET STARTING SET POINTER
     	    J = NCHS-I
     	    DECODE( J, 1000, TEXT(I+1:), ERR=888 ) KSET
     	    IF( KSET.LT.1 .OR. KSET.GT.NSET ) GOTO 888
     	    NCHS = I-1
     	ENDIF
1000	FORMAT(I)
C
C  DECODE THE INPUT...
C
     	I = IFMISC_DECODE(TEXT(1:NCHS),NSET-KSET+1,RP(KSET),KP(KSET))
    	IF( I.EQ.0 ) GOTO 888	!ERROR IN INPUT
C
C  REPEAT ENTRIES FOR THE REST OF DATA SETS IF TRUE...
C
	IF( REPEAT ) THEN
    	    DO I=KSET, NSET
    	       IF( KP(I).EQ.0 ) GOTO 111
    	    ENDDO
111	    NCHS = I - 1
    	    IF( NCHS.LT.KSET ) GOTO 888
    	    DO J=I, NSET
    	       KP(J) = 1
    	       RP(J) = RP(J-(NCHS-KSET+1))
    	    ENDDO
    	ENDIF
C
C  STORE THE VALID PARAMETERS...
C
     	IF( IKEY.EQ.1 ) THEN
    	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    J = RP(I)
     		    IF( J.LT.0 .OR. J.GT.16 ) GOTO 222
     		    IPEN(I) = J
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.2 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    J = RP(I)
     		    IF( IABS(J).GT.99 ) GOTO 222
     		    ISYM(I) = J
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.3 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    J = RP(I)
     		    IF( J.EQ.0 .OR. IABS(J).GT.9 ) GOTO 222
     		    ILIN(I) = J
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.4 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    J = RP(I)
     		    IF( J.LT.1 .OR. J.GT.999 ) GOTO 222
     		    IF( J.GE.NUMS(LSET(1,I)) ) GOTO 222
     		    IDEN(I) = J
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.5 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    J = RP(I)
     		    IF( J.LT.1 .OR. J.GT.3 ) GOTO 222
     		    ISET(I) = J
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.6 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    J = RP(I)
     		    IF( J.LT.1 .OR. J.GT.9 ) GOTO 222
     		    IBOL(I) = J
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.7 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    P = RP(I)
     		    IF( P.LT.0.02 .OR. P.GT.4.0 ) GOTO 222
     		    SSIZ(I) = P
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.8 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    P = ABS(RP(I))
     		    IF( ISET(I).NE.2 .AND. P.GT.XLNS(1) ) GOTO 222
     		    IF( ISET(I).NE.1 .AND. P.GT.XLNS(3) ) GOTO 222
     		    XSH(I) = RP(I)
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.9 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    P = ABS(RP(I))
     		    IF( ISET(I).NE.2 .AND. P.GT.XLNS(2) ) GOTO 222
     		    IF( ISET(I).NE.1 .AND. P.GT.XLNS(4) ) GOTO 222
     		    YSH(I) = RP(I)
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.10 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) XSF(I) = RP(I)
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.11 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) YSF(I) = RP(I)
     	    ENDDO
     	    GOTO 333
     	ENDIF

     	IF( IKEY.EQ.12 ) THEN
     	    DO  I=KSET, NSET
     		IF( KP(I).NE.0 ) THEN
     		    J = XYPLOT_SRL( I, RP(I) )
     		    IF( J.EQ.0 ) GOTO 222
     		    SRL(I) = RP(I)
     		ENDIF
     	    ENDDO
     	    GOTO 333
     	ENDIF
C
C  PARAMETER VALUE OUT OF RANGE...
C
222	CALL XYPLOT_ERROR(4)
     	IF( I.EQ.KSET ) RETURN
C
C  DISPLAY NEW DATA SET LINE...
C
333	DO J= KSET, I-1
     	   IF( KP(J).NE.0 ) GOTO 444
     	ENDDO
     	GOTO 555
444	IF( J.NE.JSET ) THEN
     	    JSET = J
     	    IKEY = 0
     	ENDIF
C
C  CHECK THE BAND CODE...
C
555	J = 0
     	REPEAT = BAND
     	BAND = .FALSE.
     	DO I=1, NSET
    	   IF( ISET(I).NE.2 ) J = 1
    	   IF( ISET(I).NE.1 ) BAND = .TRUE.
    	ENDDO
     	IF( J.EQ.0 ) THEN
     	    BAND = .FALSE.
     	    DO  I=1, NSET
     		ISET(I) = 1
     	    ENDDO
     	ENDIF
	CALL XYPLOT_PARS_SET(JSET,IKEY)
     	IF( .NOT.REPEAT .AND. BAND ) THEN	!SCALE 2ND BAND
     	    CALL IFANSI_VIDEO(30)
     	    J = XYPLOT_SCALE(3)
     	    IF( J.EQ.0 ) CALL XYPLOT_ERROR(8)
     	    CALL XYPLOT_PARS_AXIS(3,0)
     	    J = XYPLOT_SCALE(4)
     	    IF( J.EQ.0 ) CALL XYPLOT_ERROR(8)
     	    CALL XYPLOT_PARS_AXIS(4,0)
     	ENDIF
     	RETURN

888	CALL XYPLOT_ERROR(3)	!INVALID PARAMETER
     	RETURN
	END

     	FUNCTION XYPLOT_SRL( I, P )
C
C	THIS ROUTINE MAKES SURE THAT SET PLOTTING REFERENCE
C	LINE IS WITHIN THE CURRENT Y-AXIS WINDOW.
C
     	INCLUDE 'XYPLOT.CBS'

     	PP = P
     	II = I
     	IF( ISET(II).NE.2 ) THEN
     	    CALL XYPLOT_LIMIT(1,YLO,YHI,YLO1,YHI1)
     	    YLO = YLO1
     	    YHI = YHI1
     	    LYY = IABS(LAXS(2))
     	ENDIF
     	IF( ISET(II).NE.1 ) THEN
     	    CALL XYPLOT_LIMIT(2,YLO,YHI,YLO2,YHI2)
     	    YLO = YLO2
     	    YHI = YHI2
     	    LYY = IABS(LAXS(4))
     	ENDIF
     	IF( ISET(II).EQ.3 ) THEN
     	    IF( YLO1.GT.YLO2 ) YLO = YLO1
     	    IF( YHI1.LT.YHI2 ) YHI = YHI1
     	    IF( YLO.GT.YHI ) THEN
     		YLO1 = YLO
     		YLO  = YHI
     		YHI  = YLO1
     	    ENDIF
     	ENDIF
     	IF( LYY.EQ.2 ) THEN
     	    IF( PP.LE.0.0 ) GOTO 777
     	    PP = LOG10(PP)
     	ENDIF
     	IF( LYY.EQ.3 ) THEN
     	    IF( PP.EQ.0.0 ) GOTO 777
     	    PP = 1.0/PP
     	ENDIF
     	IF( PP.LT.YLO .OR. PP.GT.YHI ) GOTO 777

     	XYPLOT_SRL = 1
     	RETURN
777	XYPLOT_SRL = 0
     	RETURN
     	END

     	SUBROUTINE XYPLOT_SET_PM
C
C	THIS ROUTINE GETS DATA SET PLOTTING MODE.
C
     	INCLUDE 'XYPLOT.CBS'
     	CHARACTER TEXT*80
     	COMMON/XYPLOT_MENU/ JSET, IKEY, NCHS, TEXT

     	IKEY = 0
     	IF( NCHS.GT.0 ) THEN
     	    IKEY = 9
     	    CALL IFMISC_UPPER(TEXT)
     	    IF( TEXT(1:7).EQ.'DEFAULT' ) IKEY = 0
     	    IF( TEXT(1:4).EQ.'FILL' ) IKEY = 1
     	    IF( TEXT(1:4).EQ.'LOOP' ) IKEY =-1
     	    IF( TEXT(1:5).EQ.'STICK') IKEY = 2
     	    IF( TEXT(1:3).EQ.'BAR'  ) IKEY = 3
     	    IF( IKEY.EQ.9 ) THEN
     		CALL XYPLOT_ERROR(3)
     		RETURN
     	    ENDIF
     	ENDIF
     	ISHD(JSET) = IKEY
     	CALL XYPLOT_PARS_SET(JSET,13)

     	RETURN
     	END

     	SUBROUTINE XYPLOT_LID
C
C	THIS ROUTINE CHANGES DATA SET LABEL POSITION.
C
    	INCLUDE 'XYPLOT.CBS'
     	CHARACTER TEXT*80
     	COMMON/XYPLOT_MENU/ JSET, IKEY, NCHS, TEXT
     	DIMENSION RP(2), KP(2)
C
C  DECODE THE PARAMETERS...
C
     	NCHS = IFMISC_DECODE( TEXT, 2, RP, KP )
    	IF( NCHS.EQ.0 ) THEN
     	    CALL XYPLOT_ERROR(3)
     	    RETURN
     	ENDIF

     	IF( IKEY.EQ.1 ) THEN		!DRAW DATA SET LABELS
     	    IF(RP(1).LT.0.0.OR.RP(1).GT.2.0) GOTO 777
     	    DRAW = RP(1)
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.2 ) THEN		!DRAW DATA SET MARKERS
     	    IF(RP(1).LT.0.0.OR.RP(1).GT.1.0) GOTO 777
     	    MARK = RP(1)
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.3 ) THEN		!SET LABELING POSITION
    	    IF( KP(1).EQ.1 ) THEN
     		IF( RP(1).LT.-9.0 .OR. RP(1).GT.99.0 ) GOTO 777
     		XLPS = RP(1)
    	    ENDIF
    	    IF( KP(2).EQ.1 ) THEN
     		IF( RP(2).LT.-9.0 .OR. RP(2).GT.99.0 ) GOTO 777
     		YLPS = RP(2)
    	    ENDIF
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.4 ) THEN		!ACTIVE DATA SET POINTER
     	    IKEY = RP(1)
     	    IF( IKEY.LT.1 .OR. IKEY.GT.NSET ) GOTO 777
     	    JSET = IKEY
     	    CALL IFANSI_VIDEO(30)
     	    CALL XYPLOT_PARS_SET(JSET,0)
     	    RETURN
     	ENDIF

777	CALL XYPLOT_ERROR(4)
     	RETURN
888	CALL XYPLOT_PARS_LID(IKEY)
     	RETURN
     	END

	SUBROUTINE XYPLOT_AXIS_A
C
C	THIS ROUTINE CHANGES AXIS DRAWING PARAMETERS -- GROUP A
C
    	INCLUDE		'XYPLOT.CBS'
     	CHARACTER	TEXT*80
     	COMMON/XYPLOT_MENU/	JSET, IKEY, NCHS, TEXT
     	DIMENSION	KP(5), RP(5)
C
C  DECODE THE PARAMETERS...
C
    	J = IFMISC_DECODE( TEXT, 5, RP, KP )
    	IF( J.EQ.0 ) THEN
     	    CALL XYPLOT_ERROR(3)	!ERROR IN INPUT
     	    GOTO 999
     	ENDIF
     	IF( IKEY.GT.2 .AND. .NOT.BAND ) THEN
     	    CALL XYPLOT_ERROR(7)
     	    GOTO 999
     	ENDIF
C
C  STORE THE NEW PARAMETERS...
C
     	IF( KP(1).EQ.1 ) THEN
     	    J = RP(1)
     	    IF( J.EQ.0 .OR. IABS(J).GT.3 ) GOTO 777
     	    LAXS(IKEY) = J
     	ENDIF

     	IF( KP(2).EQ.1 ) THEN
     	    J = RP(2)
     	    IF( J.EQ.0 .OR. IABS(J).GT.99 ) GOTO 777
     	    NTKS(IKEY) = J
     	ENDIF

     	IF( KP(3).EQ.1 ) THEN
     	    J = RP(3)
     	    IF( IABS(J).GT.99 ) GOTO 777
     	    MTKS(IKEY) = J
     	ENDIF

     	IF( KP(4).EQ.1 ) THEN
     	    J = RP(4)
     	    IF( IABS(J).GT.9 ) GOTO 777
     	    NDGS(IKEY) = J
     	ENDIF

     	IF( KP(5).EQ.1 ) THEN
     	    J = RP(5)
     	    IF( J.EQ.0 .OR. IABS(J).GT.44 ) GOTO 777
     	    IGDS(IKEY) = J
     	ENDIF

     	CALL XYPLOT_PARS_AXIS(IKEY,1)

     	IF( KP(1).EQ.1 ) THEN		!AUTO-SCALE ON
     	    J = XYPLOT_SCALE(IKEY)
     	    IF( J.EQ.0 ) CALL XYPLOT_ERROR(8)
     	    CALL XYPLOT_PARS_AXIS(IKEY,3)
     	    GOTO 999
     	ENDIF

     	IF( KP(2).EQ.1 .AND. IABS(LAXS(IKEY)).NE.2 ) THEN
     	    XINC(IKEY) = (XHIG(IKEY)-XLOW(IKEY)) / IABS(NTKS(IKEY))
     	    CALL XYPLOT_PARS_AXIS(IKEY,3)
     	ENDIF
     	GOTO 999

777	CALL XYPLOT_ERROR(4)	!PARAMETER OUT OF RANGE

999	RETURN
     	END

	SUBROUTINE XYPLOT_AXIS_B
C
C	THIS ROUTINE CHANGES AXIS DRAWING PARAMETERS -- GROUP B
C
    	INCLUDE		'XYPLOT.CBS'
     	CHARACTER	TEXT*80
     	COMMON/XYPLOT_MENU/	JSET, IKEY, NCHS, TEXT
     	DIMENSION	KP(4), RP(4)
C
C  DECODE THE PARAMETERS...
C
    	J = IFMISC_DECODE( TEXT, 4, RP, KP )
    	IF( J.EQ.0 ) THEN
     	    CALL XYPLOT_ERROR(3)	!ERROR IN INPUT
     	    GOTO 999
     	ENDIF
     	IF( IKEY.GT.2 .AND. .NOT.BAND ) THEN
     	    CALL XYPLOT_ERROR(7)
     	    GOTO 999
     	ENDIF
C
C  STORE THE NEW PARAMETERS...
C
     	IF( KP(1).EQ.1 ) THEN
     	    P = RP(1)
     	    IF( P.LT.2.0 .OR. P.GT.99.0 ) GOTO 777
     	    XLNS(IKEY) = P
     	ENDIF

     	IF( KP(2).EQ.1 ) THEN
     	    P = RP(2)
     	    IF( P.LT.0.0 .OR. P.GT.XLNS(IKEY)/3.0 ) GOTO 777
     	    OFFS(IKEY) = P
     	ENDIF

     	IF( KP(3).EQ.1 ) THEN
     	    P = RP(3)
     	    IF( P.LT.-9.99 .OR. P.GT.99.0 ) GOTO 777
     	    SOFF(IKEY) = P
     	ENDIF

     	IF( KP(4).EQ.1 ) THEN
     	    P = RP(4)
     	    IF( ABS(P).GT.80.0 ) GOTO 777
     	    XANG(IKEY) = P
     	ENDIF

     	CALL XYPLOT_PARS_AXIS(IKEY,2)
     	GOTO 999

777	CALL XYPLOT_ERROR(4)	!PARAMETER OUT OF RANGE

999	RETURN
     	END

	SUBROUTINE XYPLOT_AXIS_C
C
C	THIS ROUTINE CHANGES AXIS DRAWING PARAMETERS -- GROUP C
C
    	INCLUDE		'XYPLOT.CBS'
     	CHARACTER	TEXT*80
     	COMMON/XYPLOT_MENU/	JSET, IKEY, NCHS, TEXT
     	DIMENSION	KP(3), RP(3)
C
C  DECODE THE PARAMETERS...
C
    	J = IFMISC_DECODE( TEXT, 3, RP, KP )
    	IF( J.EQ.0 ) THEN
     	    CALL XYPLOT_ERROR(3)	!ERROR IN INPUT
     	    GOTO 999
     	ENDIF
     	IF( IKEY.GT.2 .AND. .NOT.BAND ) THEN
     	    CALL XYPLOT_ERROR(7)
     	    GOTO 999
     	ENDIF
C
C  STORE THE NEW PARAMETERS...
C
    	J = IABS(LAXS(IKEY))
     	IF( J.NE.2 ) THEN	!LINEAR TYPE OF AXIS
     	    IF( KP(1).EQ.0 ) RP(1) = XLOW(IKEY)
     	    IF( KP(2).EQ.0 ) RP(2) = XHIG(IKEY)
     	    IF( RP(1).EQ.RP(2) ) GOTO 777
     	    IF( KP(3).EQ.0 ) THEN
     		IF(J.EQ.3.AND.RP(1)*RP(2).LE.0.0) GOTO 777	!INVERSE AXIS
     		RP(3) = (RP(2)-RP(1))/IABS(NTKS(IKEY))
     	    ELSE
     		IF( RP(3).EQ.0.0   ) GOTO 777
     		J = ABS((RP(2)-RP(1))/RP(3)) + 0.1
     		IF( J.LT.1 .OR. J.GT.99 ) THEN
     		    CALL XYPLOT_ERROR(4)
     		    GOTO 999
     		ENDIF
     		NTKS(IKEY) = J
     		RP(3) = (RP(2)-RP(1))/J
     		CALL XYPLOT_PARS_AXIS(IKEY,1)
     	    ENDIF
     	ELSE			!LOG10 TYPE OF AXIS
     	    IF( KP(1).EQ.0 ) RP(1) = XLOW(IKEY)
     	    IF( KP(2).EQ.0 ) RP(2) = XHIG(IKEY)
     	    IF( RP(1).LE.0.0 .OR. RP(2).LE.0.0 ) GOTO 777
     	    RP(1) = 10**INT(LOG10(RP(1)))
     	    RP(2) = 10**INT(LOG10(RP(2)))
     	    IF( RP(1).EQ.RP(2) ) GOTO 777
     	    RP(3) = LOG10(RP(2)/RP(1))
     	ENDIF
     	XLOW(IKEY) = RP(1)
     	XHIG(IKEY) = RP(2)
     	XINC(IKEY) = RP(3)
     	CALL XYPLOT_PARS_AXIS(IKEY,3)
     	GOTO 999

777	CALL XYPLOT_ERROR(10)	!OUT OF RANGE

999	RETURN
	END

	SUBROUTINE XYPLOT_LIST( JSET )
C
C	LIST OF ALL DATA SET PARAMETERS.
C
	INCLUDE 'XYPLOT.CBS'

	CALL IFANSI_CURSOR(15,1)
	CALL IFANSI_CLEAR(2)
     	CALL IFANSI_VIDEO(47)

	WRITE(6,1234) (I,IPEN(I),ISYM(I),ILIN(I),IDEN(I),ISET(I),
     +  IBOL(I),SSIZ(I),XSH(I),YSH(I),XSF(I),YSF(I),SRL(I), I=1,NSET)
1234	FORMAT(7I4,2X,3F6.2,3G11.3)

	WRITE(6,1000)
1000	FORMAT('$Press SPACEBAR to continue...')
     	CALL IFQIOW_KEY(I)
     	CALL XYPLOT_FORM(JSET)
	RETURN
	END

     	SUBROUTINE XYPLOT_MISC
C
C	THIS ROUTINE IMPLEMENTS THE FOLLOWING DATA
C	MANIPULATIONS ON ACTIVE DATA SET --- JSET:
C
C	(1) SWAP X,Y, COLUMNS OF INPUT DATA.
C	(2) SORT INPUT DATA IN ASCENDING ORDER IN X-COORDIANTES.
C	(3) SORT INPUT DATA IN ASCENDING ORDER IN Y-COORDIANTES.
C	(4) SMOOTH INPUT DATA BY A SAVITZKY-GOLAY FILTER.
C	(5) DIFFERENTIATE INPUT DATA BY A SAVITZKY-GOLAY FILTER.
C	(6) LEAST-SQUARE POLYNOMIAL REGRESSION.
C
     	INCLUDE		'XYPLOT.CBS'
    	CHARACTER	TEXT*80
     	COMMON/XYPLOT_MENU/  JSET, IKEY, NCHS, TEXT
C
C  DISPLAY THE MENU...
C
     	IKEY = 0
     	CALL IFANSI_MARGIN(14,0)
     	CALL IFANSI_CURSOR(2,1)
     	CALL IFANSI_CLEAR(2)
     	CALL IFANSI_VIDEO(47)
     	CALL IFANSI_VIDEO(80)
     	CALL IFANSI_VIDEO(90)
     	CALL IFANSI_TEXT('     M E N U   O F   D A T A   M A N I P
     + U L A T I O N   U T I L I T I E S      ', 80, 3, 1 )
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,1000)
1000	FORMAT(/
     + 10X'1> SWAP THE X,Y COLUMNS               ACTIVE SET:'/
     +,10X'2> SORT ON X-COORDINATES             DATA POINTS:'/
     +,10X'3> SORT ON Y-COORDINATES               X-MINIMUM:'/
     +,10X'4> DATA SMOOTHING                      X-MAXIMUM:'/
     +,10X'5> DATA DIFFERENTIATION                Y-MINIMUM:'/
     +,10X'6> LEAST-SQUARE POLYNOMIAL             Y-MAXIMUM:'/
     +,10X'7> ADD/SUBTRACT/AVERAGE DATA SETS'//
     +,10X'PF1> DOWN-ACTIVE-SET   PF3> UP-ACTIVE-SET   0> EXIT')
     	CALL IFANSI_VIDEO(30)
     	CALL IFANSI_TEXT('               Please press a
     + desired function code', 68, 14, 6 )

  111	CALL IFANSI_VIDEO(20)
     	CALL IFANSI_IFL( JSET, 1, 5, 5, 59 )
     	CALL IFANSI_TEXT('      ', 6, 5, 64 )
     	NCHS = LSET(1,JSET)
     	CALL IFANSI_IFL( NUMS(NCHS), 1, 5, 6, 59 )
     	CALL IFANSI_TEXT('      ', 6, 6, 64 )
     	CALL IFANSI_GFL( SRNG(1,NCHS), 1, 11.4, 7, 59 )
     	CALL IFANSI_GFL( SRNG(2,NCHS), 1, 11.4, 8, 59 )
     	CALL IFANSI_GFL( SRNG(3,NCHS), 1, 11.4, 9, 59 )
     	CALL IFANSI_GFL( SRNG(4,NCHS), 1, 11.4,10, 59 )

  333	IF( IKEY.GT.0 ) THEN
     	    CALL IFANSI_VIDEO(30)
     	    CALL IFANSI_TEXT(' ITS DONE ', 10, 12, 53 )
     	ENDIF

  222	CALL IFANSI_VIDEO(47)
     	CALL IFANSI_CURSOR(13,54)
     	CALL IFQIOW_KEY( KEY )
     	IF( KEY.EQ.13 .OR. KEY.EQ.48 .OR. KEY.EQ.26 ) GOTO 999
C
C  GET THE OPTION...
C
     	CALL IFANSI_CURSOR(12,1)
     	CALL IFANSI_CLEAR(6)
     	CALL IFANSI_CURSOR(14,1)
     	CALL IFANSI_CLEAR(2)
     	IF( KEY.EQ.80 ) THEN
     	    JSET = JSET + 1
     	    IF( JSET.GT.NSET ) JSET = 1
     	    IKEY = 0
     	    GOTO 111
     	ELSE IF( KEY.EQ.82 ) THEN
     	    JSET = JSET - 1
     	    IF( JSET.LT.1 ) JSET = NSET
     	    IKEY = 0
     	    GOTO 111
     	ELSE IF( KEY.EQ.49 ) THEN
     	    CALL IFANSI_VIDEO(30)
     	    CALL IFANSI_TEXT('SWAP THE X,Y COLUMNS', 20,5,13)
     	    IKEY = XYPLOT_MISC_SWAP( JSET )
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_TEXT('SWAP THE X,Y COLUMNS', 20,5,13)
     	    GOTO 111
     	ELSE IF( KEY.EQ.50 ) THEN
     	    CALL IFANSI_VIDEO(30)
     	    CALL IFANSI_TEXT('SORT ON X-COORDIANTES', 21,6,13)
     	    IKEY = XYPLOT_MISC_SORT( 'X', JSET )
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_TEXT('SORT ON X-COORDIANTES', 21,6,13)
     	    GOTO 333
     	ELSE IF( KEY.EQ.51 ) THEN
     	    CALL IFANSI_VIDEO(30)
     	    CALL IFANSI_TEXT('SORT ON Y-COORDIANTES', 21,7,13)
     	    IKEY = XYPLOT_MISC_SORT( 'Y', JSET )
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_TEXT('SORT ON Y-COORDIANTES', 21,7,13)
     	    GOTO 333
     	ELSE IF( KEY.EQ.52 ) THEN
     	    CALL IFANSI_VIDEO(30)
     	    CALL IFANSI_TEXT('DATA SMOOTHING', 14,8,13)
     	    CALL IFANSI_CURSOR(14,1)
     	    CALL IFANSI_VIDEO(47)
     	    IKEY = XYPLOT_MISC_FILTER( JSET )
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_TEXT('DATA SMOOTHING', 14,8,13)
     	    GOTO 333
     	ELSE IF( KEY.EQ.53 ) THEN
     	    CALL IFANSI_VIDEO(30)
     	    CALL IFANSI_TEXT('DATA DIFFERENTIATION', 20,9,13)
     	    CALL IFANSI_CURSOR(14,1)
     	    CALL IFANSI_VIDEO(47)
     	    IKEY = XYPLOT_MISC_DERIV( JSET )
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_TEXT('DATA DIFFERENTIATION', 20,9,13)
     	    GOTO 333
     	ELSE IF( KEY.EQ.54 ) THEN
     	    CALL IFANSI_VIDEO(30)
     	    CALL IFANSI_TEXT('LEAST-SQUARE POLYNOMIAL', 23,10,13)
     	    CALL IFANSI_CURSOR(14,1)
     	    CALL IFANSI_VIDEO(47)
     	    IKEY = XYPLOT_MISC_LSQ( JSET )
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_TEXT('LEAST-SQUARE POLYNOMIAL', 23,10,13)
     	    GOTO 333
     	ELSE IF( KEY.EQ.55 ) THEN
     	    IF( MSET.EQ.MNS ) THEN
     		CALL IFANSI_VIDEO(70)
     	    CALL IFANSI_TEXT(' Max number of data sets hit ',29,12,10)
     		GOTO 222
     	    ENDIF
     	    IF( NSET.EQ.1 ) THEN
     		CALL IFANSI_VIDEO(70)
     	    CALL IFANSI_TEXT(' Only one data set present ',27,12,10)
     		GOTO 222
     	    ENDIF
     	    CALL IFANSI_VIDEO(30)
     	    CALL IFANSI_TEXT('ADD/SUBTRACT/AVERAGE DATA SETS', 30,11,13)
     	    CALL IFANSI_CURSOR(14,1)
     	    CALL IFANSI_VIDEO(47)
     	    IKEY = XYPLOT_MISC_ADD(JSET)
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_TEXT('ADD/SUBTRACT/AVERAGE DATA SETS', 30,11,13)
     	    GOTO 333
     	ENDIF
     	CALL IFANSI_VIDEO(70)
     	CALL IFANSI_TEXT(' Undefined input ', 17, 12, 10)
     	GOTO 222

999	CALL IFANSI_MARGIN(0,0)
     	CALL XYPLOT_FORM(JSET)
     	RETURN
     	END

     	FUNCTION XYPLOT_MISC_SWAP( JSET )
C
C	THIS ROUTINE SWAPPS THE X,Y COLUMNS OF DATA SET "JSET".
C
     	INCLUDE 'XYPLOT.CBS'

     	K = LSET(1,JSET)
     	DO  I=1, NUMS(K)
     	    TEMP = X(I,K)
     	    X(I,K) = Y(I,K)
     	    Y(I,K) = TEMP
     	ENDDO
     	TEMP = SRNG(1,K)
     	DUMY = SRNG(2,K)
     	SRNG(1,K) = SRNG(3,K)
     	SRNG(2,K) = SRNG(4,K)
     	SRNG(3,K) = TEMP
     	SRNG(4,K) = DUMY
     	XYPLOT_MISC_SWAP = 1

     	RETURN
     	END

     	FUNCTION XYPLOT_MISC_SORT( SYM, JSET )
C
C	THIS ROUTINE SORTS ACTIVE DATA SET "JSET".
C
     	INCLUDE 'XYPLOT.CBS'
     	CHARACTER SYM*1

     	K = LSET(1,JSET)
     	J = LSET(2,JSET)	!ERROR BAR FLAG
     	IF( SYM.EQ.'X' ) THEN	!SORT ON X-COORDINATES
     	    IF( J.EQ.0 ) THEN
     		CALL IFMISC_SORT_ONE(NUMS(K),X(1,K),Y(1,K))
     	    ELSE
     		CALL IFMISC_SORT_TWO(NUMS(K),X(1,K),Y(1,K),Y(1,K+1))
     	    ENDIF
     	ELSE			!SORT ON Y-COORDINATES
     	    IF( J.EQ.0 ) THEN
     		CALL IFMISC_SORT_ONE(NUMS(K),Y(1,K),X(1,K))
     	    ELSE
     		CALL IFMISC_SORT_TWO(NUMS(K),Y(1,K),Y(1,K+1),X(1,K))
     	    ENDIF
     	ENDIF
     	XYPLOT_MISC_SORT = 1

     	RETURN
     	END

     	FUNCTION XYPLOT_MISC_FILTER( JSET )
C
C	THIS ROUTINE SMOOTHS/FILTERS ACTIVE DATA SET "JSET".
C
     	INCLUDE		'XYPLOT.CBS'
     	CHARACTER	TEXT*16
     	LOGICAL		PLOT
     	DIMENSION	RP(2), KP(2)

     	K = LSET(1,JSET)
     	KSDS = LSET(2,JSET)
     	NPTS = NUMS(K)
     	WRITE(6,1000)
1000	FORMAT(' SMOOTHING/FILTERING implemented here employs the',
     +' Savitzky-Golay simplified'/' least square procedure which',
     +' essentially fits a moving polynomial of certain'/' degree',
     +' over a number of data points defined as "filter length".',
     +' You can select'/' a filter of your need by the following',
     +' parameters:'//5X'Polynomial degree(2,3,4,5); Filter length',
     +' (5,7,9 points).'/)

111	WRITE(6,1001)
1001	FORMAT('$Polynomial degree and filter length [2,7] ')
    	READ(5,1002,END=999) TEXT
1002	FORMAT(A)

     	IF( TEXT.EQ.' ' ) THEN
    	    NDEG = 2
    	    NFIL = 7
    	    NPAS = 1
     	ELSE
    	    CALL IFMISC_STRIP( TEXT )
     	    NCHS = IFMISC_DECODE( TEXT, 2, RP, KP )
     	    IF( NCHS.EQ.0 ) GOTO 111
     	    IF( KP(1).EQ.1 ) THEN
     		NDEG = RP(1)
     		IF( NDEG.LT.2 .OR. NDEG.GT.5 ) GOTO 111
    	    ENDIF
     	    IF( KP(2).EQ.1 ) THEN
     		NFIL = RP(2)
     		IF(NFIL.LT.5.OR.NFIL.GT.9.OR.NFIL.GT.NPTS) GOTO 111
     		IF(NFIL.EQ.2*(NFIL/2)) GOTO 111
    	    ENDIF
    	ENDIF
C
C  START THE FILTERING...
C
     	PLOT = .FALSE.
     	IF( MSET.LT.MNS ) THEN
     	    NSET = NSET + 1
     	    MSET = MSET + 1
     	    DO  I=1, NPTS
     		X(I,MSET) = X(I,K)
     		Y(I,MSET) = Y(I,K)
     	    ENDDO
     	    LSET(1,NSET) = MSET
     	    LSET(2,NSET) = 0
     	    K = MSET
     	    KSDS = 0
     	    PLOT = .TRUE.
     	ENDIF
     	CALL IFMISC_FILTER( NPTS, Y(1,K), NDEG, NFIL )
C
C  REMOVE UNFILTERABLE BOTH ENDS...
C
     	NPTS = NPTS - NFIL + 1
     	NCHS = NFIL/2
     	DO  I= 1, NPTS
     	    X(I,K) = X(NCHS+I,K)
     	    IF( KSDS.GT.0 ) Y(I,K+1) = Y(NCHS+I,K+1)
     	ENDDO
     	NUMS(K) = NPTS
     	CALL IFMISC_RANGE( NPTS, X(1,K), Y(1,K), SRNG(1,K) )
     	IF( PLOT ) THEN
     	    I = IFMISC_DIGITS( NSET )
     	    CALL IFANSI_VIDEO(30)
     	    WRITE(6,888) NSET
     	ENDIF
888	FORMAT(' Smoothed data have been saved in data set #',I<I>,'.')
     	XYPLOT_MISC_FILTER = 1
     	RETURN
999	XYPLOT_MISC_FILTER = 0
     	RETURN
     	END

     	FUNCTION XYPLOT_MISC_DERIV( JSET )
C
C	THIS ROUTINE DIFFERENTIATES ACTIVE DATA SET "JSET".
C
     	INCLUDE		'XYPLOT.CBS'
     	CHARACTER	TEXT*16
     	LOGICAL		PLOT
     	DIMENSION	RP(3), KP(3)

     	K = LSET(1,JSET)
     	KSDS = LSET(2,JSET)
     	NPTS = NUMS(K)
     	WRITE(6,1000)
1000	FORMAT(' DIFFERENTIATION implemented here employs the',
     +' Savitzky-Golay simplified least'/' square procedure which',
     +' essentially fits a moving polynomial of certain degree'/
     +' over a number of data points defined as "filter length".',
     +' You can select a'/' filter of your need by the following',
     +' parameters:'//5x'Polynomial degree(2,3,4); Filter length',
     +' (5,7,9 points); Derivative (1,2).'/)

111	WRITE(6,1001)
1001	FORMAT('$Pol. degree, fileter length and derivative [2,7,1] ')
    	READ(5,1002,END=999) TEXT
1002	FORMAT(A)

     	IF( TEXT.EQ.' ' ) THEN
    	    NDEG = 2
    	    NFIL = 7
    	    NDEV = 1
     	ELSE
    	    CALL IFMISC_STRIP( TEXT )
     	    NCHS = IFMISC_DECODE( TEXT, 3, RP, KP )
     	    IF( NCHS.EQ.0 ) GOTO 111
     	    IF( KP(1).EQ.1 ) THEN
     		NDEG = RP(1)
     		IF( NDEG.LT.2 .OR. NDEG.GT.4 ) GOTO 111
    	    ENDIF
     	    IF( KP(2).EQ.1 ) THEN
     		NFIL = RP(2)
     		IF(NFIL.LT.5.OR.NFIL.GT.9.OR.NFIL.GT.NPTS) GOTO 111
     		IF(NFIL.EQ.2*(NFIL/2)) GOTO 111
    	    ENDIF
     	    IF( KP(3).EQ.1 ) THEN
     		NDEV = RP(3)
     		IF( NDEV.LT.1 .OR. NDEV.GT.2 ) GOTO 111
     	    ENDIF
    	ENDIF
C
C  START THE DIFFERENTIATION...
C
     	PLOT = .FALSE.
     	IF( MSET.LT.MNS ) THEN
     	    NSET = NSET + 1
     	    MSET = MSET + 1
     	    DO  I=1, NPTS
     		X(I,MSET) = X(I,K)
     		Y(I,MSET) = Y(I,K)
     	    ENDDO
     	    LSET(1,NSET) = MSET
     	    LSET(2,NSET) = 0
     	    K = MSET
     	    KSDS = 0
     	    PLOT = .TRUE.
     	ENDIF
     	CALL IFMISC_DERIV( NPTS, Y(1,K), NDEG, NFIL, NDEV )
C
C  REMOVE UNDIFFENTIABLE BOTH ENDS...
C
     	NPTS = NPTS - NFIL + 1
     	NCHS = NFIL/2
     	DO  I= 1, NPTS
     	    X(I,K) = X(NCHS+I,K)
     	    IF( KSDS.GT.0 ) Y(I,K+1) = Y(NCHS+I,K+1)
     	ENDDO
     	NUMS(K) = NPTS
     	CALL IFMISC_RANGE( NPTS, X(1,K), Y(1,K), SRNG(1,K) )
     	IF( PLOT ) THEN
     	    I = IFMISC_DIGITS( NSET )
     	    CALL IFANSI_VIDEO(30)
     	    WRITE(6,888) NSET
     	ENDIF
888	FORMAT(' Derivatives have been saved in data set #',I<I>,'.')
     	XYPLOT_MISC_DERIV = 1
     	RETURN
999	XYPLOT_MISC_DERIV = 0
     	RETURN
     	END

     	FUNCTION XYPLOT_MISC_LSQ( JSET )
C
C	THIS ROUTINE IMPLEMENTS A LINEAR LEAST-SQUARE PROCEDURE
C	FOR POLYNOMIAL REGRESSION.
C
     	INCLUDE		'XYPLOT.CBS'
     	CHARACTER	XTYP*8, YTYP*8
    	REAL*8		XYPLOT_MISC_FIT, SDEV, SERR, RERR, COEF(10)
     	COMMON/GRAFIT_SCRATCH/ XDAT(MNP+3), YDAT(MNP+3), YBAR(MNP)
C
C  LOAD THE DATA SET...
C
     	IF( ISET(JSET).NE.2 ) THEN
     	    MDEG = 1
     	    LXXX = IABS(LAXS(1))
     	    LYYY = IABS(LAXS(2))
     	ELSE
     	    MDEG = 2
     	    LXXX = IABS(LAXS(3))
     	    LYYY = IABS(LAXS(4))
     	ENDIF
     	CALL XYPLOT_LOAD( MDEG, JSET, NPTS )
C
C  INFORM USER OF THE REGRESSION SPACE TO BE IN...
C
     	IF( LXXX.EQ.1 ) XTYP = 'LINEAR'
     	IF( LXXX.EQ.2 ) XTYP = 'LOG10'
     	IF( LXXX.EQ.3 ) XTYP = 'INVERSE'
     	IF( LYYY.EQ.1 ) YTYP = 'LINEAR'
     	IF( LYYY.EQ.2 ) YTYP = 'LOG10'
     	IF( LYYY.EQ.3 ) YTYP = 'INVERSE'
     	MDEG = IFMISC_DIGITS( NPTS )
     	WRITE(6,1002) XTYP, YTYP, NPTS
1002	FORMAT(' Note: Regression to be carried out in current X-',A7,
     +' and Y-',A7,' space'/7x'with ',I<MDEG>,' sampled data points.')
     	IF( NPTS.LT.3 ) THEN
     	    WRITE(6,100)
 100	    FORMAT(' oops: Insufficient data points',
     +' to perform regression on the data set.')
     	    RETURN
     	ENDIF
C
C  GET FITTING POLYNOMIAL DEGREE...
C
     	WRITE(6,1000)
1000	FORMAT(/'$Enter degree of fitting polynomial',
     +' (1-9,0>BEST-FIT) [0] ')
 111	CALL IFQIOW_KEY( NDEG )
     	IF( NDEG.EQ.26 ) GOTO 999
     	IF( NDEG.EQ.13 ) NDEG = 48
     	IF( NDEG.LT.48 .OR. NDEG.GT.57 ) GOTO 111
     	NDEG = NDEG - 48
C
C  FIND THE BEST FIT POLYNOMIAL...
C
     	IF( NDEG.EQ.0 ) THEN
     	    MDEG = 9
     	    IF( MDEG.GT.(NPTS-2) ) MDEG = NPTS - 2
     	    NDEG = 1
     	    RMIN = 1.7E+38
     	    DO  I = 1, MDEG
     		SDEV = XYPLOT_MISC_FIT( NPTS, XDAT, YDAT, I, COEF )
     		IF( SDEV.EQ.0.0 ) GOTO 222
     		WRITE(6,1001) I, SDEV
     		IF( SDEV.LT.RMIN ) THEN
     		    RMIN = SDEV
     		    NDEG = I
     		ENDIF
     	    ENDDO
     	ELSE
     	    IF( NDEG.GT.(NPTS-2) ) NDEG = NPTS - 2
     	ENDIF
1001	FORMAT(' Polynomial degree> ',I1,' .... SD of fit> ',G12.5)

  222	SDEV = XYPLOT_MISC_FIT( NPTS, XDAT, YDAT, NDEG, COEF )
     	IF( SDEV.EQ.0.0 ) GOTO 777	!SINGULAR MATRIX
C
C  CALCULATE THE RESIDUAL ERROR OF FIT...
C
	RERR = 0.0
     	SERR = 0.0
	DO  I=1, NPTS
     	    YCAL = COEF(1)
     	    RMIN = XDAT(I)
     	    DO  J=1, NDEG
     		YCAL = YCAL + COEF(J+1)*RMIN**J
     	    ENDDO
     	    RMIN = YDAT(I)
     	    RERR = RERR + (RMIN-YCAL)**2
     	    SERR = SERR + RMIN**2
     	ENDDO
     	RERR = 100.0*SQRT( RERR/SERR )
C
C  REPORT THE RESULTS...
C
     	WRITE(6,2000) NDEG
2000	FORMAT(/' Polynomial coefficients (degree=',I1,'):'/)
     	WRITE(6,2001) (COEF(I),I-1,I=1,NDEG+1)
2001	FORMAT(2(10X,G12.5,'*X**',I1))
     	WRITE(6,2002) SDEV, RERR
2002	FORMAT(/
     +' Standard deviation of fit:',G12.5,'; ',
     +' Residual error of fit:',F6.2,'%')
C
C  IF X,Y ARRAYS NOT FULL, STORE THE POLYNOMIAL FOR LATER PLOTTING...
C
     	IF( MSET.LT.MNS ) THEN
     	    NSET = NSET + 1
     	    MSET = MSET + 1
     	    LSET(1,NSET) = MSET
     	    LSET(2,NSET) = 0
     	    NPTS = 100
     	    NUMS(MSET) = NPTS
     	    PLOT = .TRUE.
     	    MDEG = LSET(1,JSET)
     	    STEP = (SRNG(2,MDEG)-SRNG(1,MDEG))/(NPTS-1)
     	    SDEV = SRNG(1,MDEG)-STEP
     	    DO  I=1, NPTS
     		YCAL = COEF(1)
     		RMIN = SDEV + STEP*I
     		IF( LXXX.EQ.2 ) RMIN = LOG10(RMIN)
     		IF( LXXX.EQ.3 ) RMIN = 1.0/RMIN
     		DO  J=1, NDEG
     		    YCAL = YCAL + COEF(J+1)*RMIN**J
     		ENDDO
     		IF( LXXX.EQ.2 ) RMIN = 10.0**RMIN
     		IF( LXXX.EQ.3 ) RMIN = 1.0/RMIN
     		X(I,MSET) = RMIN
     		RMIN = YCAL
     		IF( LYYY.EQ.2 ) RMIN = 10.0**RMIN
     		IF( LYYY.EQ.3 ) RMIN = 1.0/RMIN
     		Y(I,MSET) = RMIN
     	    ENDDO
     	    CALL IFMISC_RANGE(NPTS,X(1,MSET),Y(1,MSET),SRNG(1,MSET))
     	    NDEG = IFMISC_DIGITS( NSET )
     	    CALL IFANSI_VIDEO(30)
     	    WRITE(6,2003) NSET
     	ELSE
     	    CALL IFANSI_VIDEO(70)
     	    WRITE(6,2004)
     	ENDIF
2003	FORMAT(' Polynomial have been saved in data set #',I<NDEG>,'.')
2004	FORMAT(' Polynomial is not saved. Data sets full.')

     	XYPLOT_MISC_LSQ = 1
     	RETURN
777	CALL IFANSI_VIDEO(70)
     	WRITE(6,778)
778	FORMAT(' oops: Singular matrix calculated. Run aborted.')
999	XYPLOT_MISC_LSQ = 0
     	RETURN
     	END

     	REAL*8 FUNCTION XYPLOT_MISC_FIT(M,X,Y,N,B)
C
C	THIS FUNCTION CARRIES OUT AN NTH ORDER POLYNOMIAL
C	REGRESSION ON M DATA POINTS CONTAINED IN THE X AND Y ARRAYS.
C	THE FUNCTION NORMALLY RETURNS THE STANDARD DEVIATION SDEV OF
C	THE POINTS ABOUT THE REGRESSION CURVE.  THE REGRESSION
C	COEFFICIENTS ARE PLACED IN A AND B(1)...B(N).  HOWEVER, IF
C	THE SIMULTANEOUS EQUATION SOLVING ROUTINE ENCOUNTERS A
C	NEAR - SINGULAR MATRIX, THE FUNCTION RETURNS THE VALUE 0.0
C	THIS ROUTINE MUST HAVE MORE POINTS THAN DEGREE OF FIT.
C	IF REGLLS ENCOUNTERS LESS POINTS THAN DEGREE REQUESTED
C	NO PROCESSING OCCURS AND NO VALUES ARE RETURNED.
C
     	IMPLICIT REAL*8 (A-H, O-W)
     	REAL*8 XYPLOT_MISC_MATRIX
     	DIMENSION X(1),Y(1),B(1)
     	DIMENSION C(11,11),SX(20),SYX(10),CYX(10)
     	DATA EPS / 1.0E-20 /
C
C     ..... COMPUTE SUMS OF POWERS AND PRODUCTS .....
C
	NTWO = 2*N
	NP1 = N + 1
	SY = 0.0
	SYY = 0.0

	DO 1 I = 1,N
	NPI = N + I
	SX(I) = 0.0
	SX(NPI) = 0.0
  1	SYX(I) = 0.0

	DO 3 I = 1,M
     	YI = Y(I)
	SY = SY + YI
	SYY = SYY + YI**2
	DUM = 1.0
	DO 2 J = 1,N
     	XI = X(I)
	DUM = DUM*XI
	SX(J) = SX(J) + DUM
  2	SYX(J) = SYX(J) + YI*DUM
	DO 3 J = NP1,NTWO
	DUM = DUM*XI
  3	SX(J) = SX(J) + DUM
C
C     ..... COMPUTE COEFFICIENTS C(I,J) .....
C
	FM = M
	CYY = SYY - SY*SY/FM
	DO 4 I = 1,N
	CYX(I) = SYX(I) - SY*SX(I)/FM
	C(I,NP1) = CYX(I)
	DO 4 J = 1,N
	IPJ = I + J
  4	C(I,J) = SX(IPJ) - SX(I)*SX(J)/FM
C
C     ..... SOLVE SIMULTANEOUS EQUATIONS .....
C
	DET = XYPLOT_MISC_MATRIX(N,C,B,EPS,1,11)
	IF( DET.NE.0.0 )GO TO 6
	XYPLOT_MISC_FIT = 0.0
	RETURN
C
C     ..... COMPUTE INTERCEPT AND STANDARD DEVIATION SDEV .....
C
  6	DUM = SY
	DUMY = CYY
	DO 7 I = 1,N
	DUM = DUM - B(I)*SX(I)
  7	DUMY = DUMY - B(I)*CYX(I)
     	DO I=N,1,-1
     	   B(I+1) = B(I)
     	ENDDO
     	B(1) = DUM/FM
	DENOM = M - N - 1
	XYPLOT_MISC_FIT = DSQRT(DUMY/DENOM)
	RETURN
	END

     	REAL*8 FUNCTION XYPLOT_MISC_MATRIX(N,A,X,EPS,INDIC,NRC)
C
C	WHEN INDIC IS NEGATIVE, SIMULS COMPUTES THE INVERSE OF THE N
C	BY N MATRIX A IN PLACE.  WHEN INIC IS ZERO, SIMULS COMPUTES
C	THE N SOLUTIONS X(1)...X(N) CORRESPONDING TO THE SET OF LINEAR
C	EQUATIONS WITH AUGMENTED MATRIX OF COEFFICIENTS IN THE N BY
C	N + 1 ARRAY A AND IN ADDITION COMPUTES THE INVERSE OF THE
C	COEFFICIENT MATRIX IN PLACE AS ABOVE.  IF INDIC IS POSITIVE
C	THE SET OF LINEAR EQUATIONS IS SOLVED BUT THE INVERSE IS NOT
C	COMPUTED IN PLACE.  THE GAUSS - JORDEN COMPLETE ELIMINATION METHOD
C	IS EMPLOYED WITH THE MAXIMUM PIVOT STRATEGY.  ROW AND COLUMN
C	SUBSCRIPTS OF SUCCESSIVE PIVOT ELEMENTS ARE SAVED IN ORDER IN
C	THE IROW AND JCOL ARRAYS RESPECTIVELY.  K IS THE PIVOT COUNTER,
C	PIVOT THE ALGEBRAIC VALUE OF THE PIVOT ELEMENT, MAX
C	THE NUMBER OF COLUMNS IN A AND DETER THE DETERMINANT OF THE
C	COEFFICIENT MATRIX.  THE SOLUTIONS ARE COMPUTED IN THE (N + 1)TH
C	COLUMN OF A AND THEN UNSCRAMBLED AND PUT IN PROPER ORDER IN
C	X(1)...X(N) USING THE PIVOT SUBSCRIPT INFORMATION AVAILABLE
C	IN THE IROW AND JCOL ARRAYS,  THE SIGN OF THE DETERMINANT IS
C	ASJUSTED, IF NECESSARY, BY DETERIMINING IF AN ODD OR EVEN NUMBER
C	OF PAIRWISE INTERCHANGES IS REQUIRED TO PUT THE ELEMENTS OF THE
C	JORD ARRAY IN ASCENDING SEQUENCE WHERE JORD(IROW(I)) = JCOL(I).
C	IF THE INVERSE IS REQUIRED, IT IS UNSCRAMBLED IN PLACE USING
C	Y(1)...Y(N) AS TEMPORARY STORAGE.  THE VALUE OF THE DETERMINANT
C	IS RETURNED AS THE VALUE OF THE FUNCTION.  SHOULD THE POTENTIAL
C	PIVOT OF LARGEST MAGNITUDE BE SMALLER IN MAGNITUDE THAN EPS,
C	THE MATRIX IS CONSIDERED TO BE SINGULAR AND A TRUE ZERO IS
C	RETURNED AS THE VALUE OF THE FUNCTION.
C
     	IMPLICIT REAL*8 (A-H, O-Z)
     	DIMENSION IROW(11),JCOL(11),JORD(11),Y(11),A(NRC,NRC),X(N)

     	MAX = N
     	IF(INDIC.GE.0.0) MAX = N + 1
C
C     ..... BEGIN ELIMINATION PROCEDURE .....
C
	DETER = 1.0
	DO 18 K = 1,N
	KM1 = K - 1
C
C     ..... SEARCH FOR THE PIVOT ELEMENT .....
C
	PIVOT = 0.0
	DO 11 I = 1,N
	DO 11 J = 1,N
C
C     ..... SCAN IROW AND JCOL ARRAYS FOR INVALID PIVOT SUBSCRIPTS.....
C
	IF(K .EQ. 1)GO TO 9
	DO 8 ISCAN = 1,KM1
	DO 8 JSCAN = 1,KM1
	IF(I .EQ. IROW(ISCAN))GO TO 11
8	IF(J .EQ. JCOL(JSCAN))GO TO 11
9	IF(DABS(A(I,J)).LE.DABS(PIVOT))GO TO 11
	PIVOT = A(I,J)
	IROW(K) = I
	JCOL(K) = J
11	CONTINUE
C
C     ..... INSURE THAT SELECTED PIVOT IS LARGER THAN EPS .....
C
	IF(DABS(PIVOT).GT.EPS)GO TO 13
	XYPLOT_MISC_MATRIX = 0.0
	RETURN
C
C     ..... UPDATE THE DETERMINANT VALUE .....
C
13	IROWK = IROW(K)
	JCOLK = JCOL(K)
	DETER = DETER*PIVOT
C
C     ..... NORMALIZE PIVOT ROW ELEMENTS .....
C
	DO 14 J = 1,MAX
14	A(IROWK,J) = A(IROWK,J)/PIVOT
C
C     ..... CARRY OUT ELIMINATION AND CARRY OUT INVERSE .....
C
	A(IROWK,JCOLK) = 1.0/PIVOT
	DO 18 I = 1,N
	AIJCK = A(I,JCOLK)
	IF(I .EQ. IROWK)GO TO 18
	A(I,JCOLK) =  - AIJCK/PIVOT
	DO 17 J = 1,MAX
17	IF(J .NE. JCOLK) A(I,J) = A(I,J) - AIJCK*A(IROWK,J)
18	CONTINUE
C
C     ..... ORDER SOLUTION VALUES (IF ANY) AND CREATE JORD ARRAY .....
C
	DO 20 I = 1,N
	IROWI = IROW(I)
	JCOLI = JCOL(I)
	JORD(IROWI) = JCOLI
20	IF(INDIC.GE.0.0) X(JCOLI) = A(IROWI,MAX)
C
C     ..... ADJUST SIGN OF DETERMINANT .....
C
	INTCH = 0
	NM1 = N - 1
	DO 22 I = 1,NM1
	IP1 = I + 1
	DO 22 J = IP1,N
	IF(JORD(J).GE.JORD(I))GO TO 22
	JTEMP = JORD(J)
	JORD(J) = JORD(I)
	JORD(I) = JTEMP
	INTCH = INTCH + 1
22	CONTINUE
	IF(INTCH/2*2 .NE. INTCH) DETER =  - DETER
C
C     ..... IF INDIC IS POSITIVE RETURN WITH RESULTS .....
C
24	IF(INDIC.LE.0) GO TO 26
	XYPLOT_MISC_MATRIX = DETER
	RETURN
C
C     ..... IF INDIC IS ZERO OR NEGATIVE, UNSCRAMBLE THE INVERSE
C     FIRST BY ROWS .....
C
26	DO 28 J = 1,N
	DO 27 I = 1,N
	IROWI = IROW(I)
	JCOLI = JCOL(I)
27	Y(JCOLI) = A(IROWI,J)
	DO 28 I = 1,N
28	A(I,J) = Y(I)
C
C     ..... AND THEN BY COLUMNS .....
C
	DO 30 I = 1,N
	DO 29 J = 1,N
	IROWJ = IROW(J)
	JCOLJ = JCOL(J)
29	Y(IROWJ) = A(I,JCOLJ)
	DO 30 J = 1,N
30	A(I,J) = Y(J)
C
C     ..... RETURN FOR INDIC NEGATIVE OR ZERO .....
C
	XYPLOT_MISC_MATRIX = DETER
	RETURN
	END

     	SUBROUTINE XYPLOT_WRITE
C
C	THIS ROUTINE WRITES CURRENT DATA SETS INTO A FILE.
C
C	DEFAULT FILE FORMAT IS FORMAT NUMBER 1 --- ASCII.
C
     	INCLUDE		'XYPLOT.CBS'
    	CHARACTER	TEXT*80
     	COMMON/XYPLOT_MENU/  JSET, IKEY, NCHS, TEXT
C
C  PARSE DATA FORMAT QUALIFIER...
C
     	NCHS = IFMISC_NEXT(TEXT)
     	IF( NCHS.EQ.0 ) THEN
     	    NCHS = XYPLOT_NEXT(TEXT)
     	    IF( NCHS.EQ.0 ) GOTO 999
     	ENDIF
     	IFOM = 1
     	ISLA = INDEX( TEXT(1:NCHS), '/' )
     	IF( ISLA.GT.0 ) THEN
     	    ISLA = INDEX( TEXT(1:NCHS), '/FORMAT=' )
     	    IF( ISLA.LT.2 ) GOTO 777
     	    DECODE( 1, 111, TEXT(ISLA+8:ISLA+8), ERR=888 ) IFOM
  111	    FORMAT(I1)
     	    IF( IFOM.LT.1 .OR. IFOM.GT.6 ) GOTO 888
     	    TEXT = TEXT(1:ISLA-1)
     	    NCHS = ISLA - 1
     	ENDIF
     	IF( NCHS.GT.32 ) THEN
     	    CALL XYPLOT_ERROR(9)
     	    GOTO 999
     	ENDIF
C
C  WRITE THE FILE IN THE SPECIFIED FORMAT...
C
     	CALL XYPLOT_WRITE_DATA( TEXT, IFOM )
     	GOTO 999
777	CALL IFANSI_VIDEO(70)
     	CALL XYPLOT_OOPS(' Invalid data format qualifier.')
     	GOTO 999
888	CALL IFANSI_VIDEO(70)
     	CALL XYPLOT_OOPS(' Invalid data format number.')
999	RETURN
     	END

     	SUBROUTINE XYPLOT_WRITE_DATA( FNAME, IFOM )
C
C	THIS ROUTINE WRITES OUT CURRENT DATA SETS.
C
     	INCLUDE 'XYPLOT.CBS'
     	CHARACTER TEXT*75, DNAME*32, FNAME*(*)

     	IFORM = IFOM
     	DNAME = FNAME
C
C  OPEN THE OUTPUT FILE...
C
     	IF( IFORM.LT.5 ) THEN
     	    OPEN( UNIT=13, FILE=DNAME, STATUS='NEW', FORM='FORMATTED',
     +            CARRIAGECONTROL='LIST', ERR=888 )
     	ELSE
     	    OPEN( UNIT=13, FILE=DNAME, STATUS='NEW', FORM='UNFORMATTED',
     +            ERR=888)
     	ENDIF
C
C  WRITE THE DATA SETS OUT...
C
     	IF( IFORM.EQ.1 ) THEN
     	    DO  I=1, NSET
     		K = LSET(1,I)
     		J = LSET(2,I)
     		NPTS = NUMS(K)
     		WRITE(13,1000,ERR=888) NPTS, LABL(K)
     		IF( J.EQ.0 ) THEN
     		    WRITE(13,1001,ERR=888) (X(N,K),Y(N,K),N=1,NPTS)
     		ELSE
     	WRITE(13,1002,ERR=888) (X(N,K),Y(N,K),Y(N,K+1),N=1,NPTS)
     		ENDIF
     	    ENDDO
1000	    FORMAT(I5,X,'!',A80)
1001	    FORMAT(2G13.5)
1002	    FORMAT(3G13.5)
     	ELSE IF( IFORM.EQ.2 ) THEN
     	    DO  I=1, NSET
     		K = LSET(1,I)
     		J = LSET(2,I)
     		NPTS = NUMS(K)
     		IF( J.EQ.0 ) THEN
     		    WRITE(13,1001,ERR=888) (X(N,K),Y(N,K),N=1,NPTS-1)
     		    WRITE(13,2001,ERR=888) X(NPTS,K),Y(NPTS,K),I,LABL(K)
     		ELSE
     	WRITE(13,1002,ERR=888) (X(N,K),Y(N,K),Y(N,K+1),N=1,NPTS-1)
     	WRITE(13,2002,ERR=888) X(NPTS,K),Y(NPTS,K),Y(NPTS,K+1),I,LABL(K)
     		ENDIF
     	    ENDDO
2001	    FORMAT(2G13.5,',,',I3,X,'!',A80)
2002	    FORMAT(3G13.5,I4,X,'!',A80)
     	ELSE IF( IFORM.EQ.3 ) THEN
     	    J = NSET
     	    IF( J.GT.9 ) J = 9
     	    WRITE(13,3001,ERR=888) -J
     	    WRITE(13,3002,ERR=888) (X(I,1),(Y(I,LSET(1,K)),K=1,J),
     +                              I=1,NUMS(1))
3001	    FORMAT(I5)
3002	    FORMAT(<J+1>G13.5)
     	ELSE IF( IFORM.EQ.4 ) THEN
     	    DO  I=1, NSET
     		K = LSET(1,I)
     		NPTS = NUMS(K)
     		STEP = (X(NPTS,K)-X(1,K))/(NPTS-1)
     		WRITE(13,4001,ERR=888) X(1,K),X(NPTS,K),STEP,LABL(K)
     		WRITE(13,4002,ERR=888) (Y(J,K),J=1,NPTS)
     	    ENDDO
4001	    FORMAT(3G13.5,X,'!',A80)
4002	    FORMAT(6G13.5)
     	ELSE IF( IFORM.EQ.5 ) THEN
     	    J = 1
     	    DO  I=1, NSET
     		K = LSET(1,I)
     		NPTS = NUMS(K)
     		WRITE(13,ERR=888) NPTS, J
     		WRITE(13,ERR=888) (X(N,K),N=1,NPTS)
     		WRITE(13,ERR=888) (Y(N,K),N=1,NPTS)
     	    ENDDO
     	ELSE IF( IFORM.EQ.6 ) THEN
     	    DO  I=1, NSET
     		K = LSET(1,I)
     		NPTS = NUMS(K)
     		STEP = (X(NPTS,K)-X(1,K))/(NPTS-1)
     		WRITE(13,ERR=888) X(1,K),X(NPTS,K),STEP
     		WRITE(13,ERR=888) (Y(N,K),N=1,NPTS)
     	    ENDDO
     	ENDIF
     	TEXT = ' '
     	TEXT(2:) = DNAME
     	NPTS = IFMISC_LENG( DNAME )
     	TEXT(NPTS+3:) = 'written in data format # .'
     	ENCODE( 1, 777, TEXT(NPTS+27:) ) IFORM
777	FORMAT(I1)
     	CALL XYPLOT_OOPS( TEXT )
     	GOTO 999

888	CALL IFANSI_VIDEO(70)
     	CALL XYPLOT_OOPS(' Error from writing the file.')
999	CLOSE(UNIT=13)
     	RETURN
     	END

     	SUBROUTINE XYPLOT_SAVE
C
C	THIS ROUTINE SAVES CURRENT PLOT SETUP IN A PARAMETER FILE.
C
     	INCLUDE		'XYPLOT.CBS'
    	CHARACTER	TEXT*80, STRN*75
     	COMMON/XYPLOT_MENU/  JSET, IKEY, NCHS, TEXT

     	NCHS = IFMISC_NEXT(TEXT)
     	IF( NCHS.EQ.0 ) THEN
     	    NCHS  = XYPLOT_NEXT(TEXT)
     	    IF( NCHS.EQ.0 ) GOTO 999
     	ENDIF
     	IF( NCHS.GT.32 ) THEN
     	    CALL XYPLOT_ERROR(9)
     	    GOTO 999
     	ENDIF
     	IKEY = XYPLOT_SETUP( 0, TEXT )
     	IF( IKEY.EQ.0 ) THEN
     	    CALL IFANSI_VIDEO(70)
     	CALL XYPLOT_OOPS(' Error during writing the setup file.')
     	ELSE
     	    STRN = ' Current plot setup has been saved in'
     	    STRN(39:) = TEXT
     	    CALL XYPLOT_OOPS( STRN )
     	ENDIF

999	RETURN
     	END

     	SUBROUTINE XYPLOT_ERROR( IERR )
C
C	THIS ROUTINE DISPLAYS DIALOGUE ERROR MESSAGE.
C
    	CHARACTER	TEXT*80, STRN*75
     	COMMON/XYPLOT_MENU/  JSET, IKEY, NCHS, TEXT

     	I = IERR
     	IF( I.EQ.1 ) THEN
     	    STRN = ' Undefined access-code> '
     	    STRN(25:26) = TEXT(1:2)
     	ELSE IF( I.EQ.2 ) THEN
     	    STRN = ' Undefined command> '
     	    IKEY = INDEX( TEXT(1:NCHS), ' ' ) - 1
     	    IF( IKEY.LT.1 ) IKEY = NCHS
     	    STRN(21:) = TEXT(1:IKEY)
     	ELSE IF( I.EQ.3 ) THEN
     	    STRN = ' Invalid parameters> '
     	    STRN(22:) = TEXT
     	ELSE IF( I.EQ.4 ) THEN
     	    STRN = ' Parameter value out of range> '
     	    STRN(32:) = TEXT
     	ELSE IF( I.EQ.5 ) THEN
     	    STRN = ' Can not open the file> '
     	    STRN(25:) = TEXT
     	ELSE IF( I.EQ.6 ) THEN
     	    STRN = ' No data yet to work on.'
     	ELSE IF( I.EQ.7 ) THEN
     	    STRN = ' No data set found on 2nd band.'
     	ELSE IF( I.EQ.8 ) THEN
     	    STRN = ' Can not perform auto-scaling on data.'
     	ELSE IF( I.EQ.9 ) THEN
     	    STRN = ' Filename too long (max. 32 characters).'
     	ELSE IF( I.EQ.10 ) THEN
     	    STRN = ' Bad axis scale specification> '
     	    STRN(32:) = TEXT
     	ENDIF
     	CALL IFANSI_VIDEO(70)
     	CALL XYPLOT_OOPS( STRN )

999	RETURN
     	END

     	SUBROUTINE XYPLOT_OOPS( TEXT )
     	CHARACTER TEXT*(*)
     	NCHS = IFMISC_LENG( TEXT )
     	CALL XYPLOT_CLEAR_OOPS
     	CALL IFANSI_TEXT( TEXT, NCHS, 21, 6 )
     	RETURN
     	END

     	SUBROUTINE XYPLOT_CLEAR_OOPS
     	CALL IFANSI_CURSOR(21,6)
     	CALL IFANSI_CLEAR(4)
     	RETURN
     	END

    	SUBROUTINE XYPLOT_GIN( Z1, Z2 )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY, JUNE-1986
C
C	THIS ROUTINE IMPLEMENTS GRAPHICS INPUT -- GIN FOR X,Y PLOT.
C
C	Z1,Z2: return the coordinates of last two cursor positions.
C
    	INCLUDE   'XYPLOT.CBS'
    	BYTE       ESC
     	DIMENSION  Z1(1), Z2(1)

     	Z1(1) = 0.0
     	Z1(2) = 0.0
     	Z2(1) = 0.0
     	Z2(2) = 0.0
     	IF( CRT.NE.2 ) THEN
     	    CALL IFANSI_VIDEO(70)
     	    WRITE(6,1001)
1001	    FORMAT(' oops: Terminal incompatible for GIN.')
     	    GOTO 999
     	ENDIF
C
C  GET THE SCALE UNITS...
C
     	CALL XYPLOT_VINCH( 1, XDL, YDL )
     	CALL XYPLOT_LIMIT( 1, XLO, XHI, YLO, YHI )
     	IF( XDL.LT.0.0 ) XLO = XHI
     	IF( YDL.LT.0.0 ) YLO = YHI
     	LXX = IABS(LAXS(1))
     	LYY = IABS(LAXS(2))
     	XLN = XLNS(1)
     	YLN = XLNS(2)
     	XXL = XLO
     	IF( LXX.EQ.2 ) XXL = 10.0**XXL
     	IF( LXX.EQ.3 ) XXL = 1.00/XXL
     	XXC = XXL
     	YYL = YLO
     	IF( LYY.EQ.2 ) YYL = 10.0**YYL
     	IF( LYY.EQ.3 ) YYL = 1.00/YYL
     	YYC = YYL
     	XPL = 0.0
     	YPL = 0.0
C
C  ENABLE GIN ...
C
     	ESC = 27
     	CALL IFINIT_DIF(0)	!DISABLE DI FILE OUTPUT
     	CALL IFINIT_LINE(3)
     	CALL IFSPEN(3)
    	CALL IFGIN_ABLE(2)
     	CALL IFSEG_MOVE( 0, 0.0, 0.0 )
    	CALL IFINIT_DUMP
     	WRITE(6,1000) ESC,ESC,ESC,ESC
1000	FORMAT(' XYPLOT_GIN> Press ',A1,'[43;30m SPACEBAR '
     +, A1,'[44;37m to enter point; ',A1,'[43;30m RETURN '
     +, A1,'[44;37m to exit...',$)

111	IF( IFGIN_REPORT(JUNK,XPS,YPS).EQ.0 ) GOTO 888
     	IF( XPS.EQ.XPL .AND. YPS.EQ.YPL ) GOTO 111
     	IF( XPS.LT.0.0.OR.XPS.GT.XLN.OR.YPS.LT.0.0.OR.YPS.GT.YLN ) THEN
     	    CALL IFANSI_VIDEO(70)
     	    WRITE(6,444)
444	    FORMAT('_oops: Invalid cursor position.')
     	    CALL IFANSI_VIDEO(47)
     	    GOTO 111
     	ENDIF
C
C  MARK THE CURSOR POSITION ENTERED...
C
     	CALL IFPLOT( XPS-0.06, YPS, 3 )
     	CALL IFPLOT( XPS+0.06, YPS, 2 )
     	CALL IFPLOT( XPS, YPS-0.06, 3 )
     	CALL IFPLOT( XPS, YPS+0.06, 2 )
    	CALL IFINIT_DUMP
C
C  REPORT THE CURSOR POSITION ENTERED...
C
     	XHI = XLO + XPS*XDL
     	IF( LXX.EQ.2 ) XHI = 10.**XHI
     	IF( LXX.EQ.3 ) XHI = 1.0/XHI
     	YHI = YLO + YPS*YDL
     	IF( LYY.EQ.2 ) YHI = 10.**YHI
     	IF( LYY.EQ.3 ) YHI = 1.0/YHI
     	DIS1 = SQRT( (XPS-XPL)**2 + (YPS-YPL)**2 )
     	DIS2 = SQRT( (XHI-XXC)**2 + (YHI-YYC)**2 )
     	WRITE(6,2000) XPS,YPS, XHI,YHI, DIS1, DIS2
2000	FORMAT(' CURSOR AT ',2F7.3,'(INCHES), ',2G12.5
     +,'(AXIS UNITS).'/' DISTANCE FROM THE LAST: '
     +,F7.3,'(INCHES), ',G12.5,'(AXIS UNITS).')
     	XPL = XPS
     	YPL = YPS
     	XXL = XXC
     	YYL = YYC
     	XXC = XHI
     	YYC = YHI
     	GOTO 111
C
C  END OF THE SESSION...
C
888	CALL IFGIN_ABLE( 0 )
     	CALL IFINIT_DIF(1)		!ENABLE DI FILE OUTPUT
     	Z1(1) = XXL
     	Z2(1) = XXC
     	Z1(2) = YYL
     	Z2(2) = YYC
999	RETURN
    	END

    	FUNCTION XYPLOT_SCALE( IFLAG )
C
C  PROGRAMMER: R.S. ZHOU,  ALFRED UNIVERSITY
C
C  	THIS ROUTINE SCALES INPUT DATA FOR X,Y PLOT.
C
C	IFLAG=1: SCALE THE INPUT DATA FOR X-AXIS OF THE 1ST GRAPH BAND.
C	IFLAG=2: SCALE THE INPUT DATA FOR Y-AXIS OF THE 1ST GRAPH BAND.
C	IFLAG=3: SCALE THE INPUT DATA FOR X-AXIS OF THE 2ND GRAPH BAND.
C	IFLAG=4: SCALE THE INPUT DATA FOR Y-AXIS OF THE 2ND GRAPH BAND.
C
C	RETURN WITH XYPLOT_SCALE = 0.0 FOR SCALING FAILURE
C	RETURN WITH XYPLOT_SCALE = 1.0 FOR OK SCALING.
C
    	INCLUDE 'XYPLOT.CBS'
C
C  SEARCH FOR MINIMUM AND MAXIMUM X,Y VALUES IN THE DATA...
C
     	IS = IFLAG
     	IT = 2
     	IF( IS.GT.2 ) IT = 1
     	X1 = 1.7E38
     	X2 =-1.7E38
     	DO  I = 1, NSET
    	    IF( IPEN(I).NE.0 .AND. ISET(I).NE.IT ) THEN
     		K = LSET(1,I)
     		IF( IS.NE.2*(IS/2) ) THEN
     		    SCL = XSF(I)
     		    DO  J = 1, NUMS(K), IDEN(I)
     			X3 = SCL*X(J,K)
     			IF( X3.LT.X1 ) X1 = X3
     			IF( X3.GT.X2 ) X2 = X3
     		    ENDDO
     		ELSE
     		    SCL = YSF(I)
     		    DO  J = 1, NUMS(K), IDEN(I)
     			X3 = SCL*Y(J,K)
     			IF( X3.LT.X1 ) X1 = X3
     			IF( X3.GT.X2 ) X2 = X3
     		    ENDDO
     		ENDIF
     	    ENDIF
     	ENDDO
C
C  SCALE THE AXIS...
C
     	IT = IABS(LAXS(IS))
     	IF( IT.NE.2 ) THEN
     	    IF( IT.EQ.3 .AND. X1*X2.LE.0.0 ) GOTO 999
     	    I = IFAXIS_LINSCL( XLNS(IS), X1, X2 )
     	    IF( IT.EQ.3 .AND. X1*X2.LE.0.0 ) GOTO 999
     	    NTKS(IS) = I
     	    XLOW(IS) = X1
     	    XHIG(IS) = X2
     	    XINC(IS) = (X2-X1)/I
     	ELSE
     	    IF( X1.LE.0.0 .OR. X2.LE.0.0 ) GOTO 999
     	    I = IFAXIS_LOGSCL( X1, X2 )
     	    XLOW(IS) = X1
     	    XHIG(IS) = X2
     	    XINC(IS) = I
     	ENDIF
     	XYPLOT_SCALE = 1
     	RETURN
C
C  SCALING FAILURE...
C
999	XLOW(IS) = 0.0
     	XHIG(IS) = 0.0
     	XINC(IS) = 0.0
     	XYPLOT_SCALE = 0
     	RETURN
     	END

    	SUBROUTINE XYPLOT_LIMIT( IFLAG, XLO,XHI, YLO,YHI )
C
C	THIS ROUTINE CALCULATES SCALE WINDOW OF A X,Y PLOT
C	BECAUSE OF POSSIBLE OFFSETS OF THE 1ST MAJOR TICKMARKS.
C
C	IFLAG=1: RETURN WINDOW OF 1ST GRAPH BAND.
C	IFLAG=2: RETURN WINDOW OF 2ND GRAPH BAND.
C
C	XLO,XHI: WINDOW LIMIT ON X-AXIS.
C	YLO,YHI: WINDOW LIMIT ON Y-AXIS.
C
     	INCLUDE 'XYPLOT.CBS'
C
C  GET THE SCALE UNITS PER INCH FIRST...
C
     	CALL XYPLOT_VINCH( IFLAG, XDL, YDL )
     	IF( IFLAG.EQ.1 ) THEN
     	    KSET = 1
     	ELSE
     	    KSET = 3
     	ENDIF
C
C  CALCULATE THE X-WINDOW ...
C
     	LTYPE = IABS(LAXS(KSET))
     	IF( LTYPE.EQ.1 ) THEN
    	    XLO = XLOW(KSET)
    	    XHI = XHIG(KSET)
    	ELSE IF( LTYPE.EQ.2 ) THEN
    	    XLO = LOG10(XLOW(KSET))
    	    XHI = LOG10(XHIG(KSET))
    	ELSE
     	    XLO = 1.0/XLOW(KSET)
     	    XHI = 1.0/XHIG(KSET)
    	ENDIF
     	XLO = XLO - XDL*OFFS(KSET)
     	XHI = XHI + XDL*OFFS(KSET)
    	IF( XLO.GT.XHI ) THEN
    	    XDL = XLO
    	    XLO = XHI
    	    XHI = XDL
    	ENDIF
C
C  CALCULATE THE Y-WINDOW ...
C
     	LTYPE = IABS(LAXS(KSET+1))
     	IF( LTYPE.EQ.1 ) THEN
    	    YLO = XLOW(KSET+1)
    	    YHI = XHIG(KSET+1)
    	ELSE IF( LTYPE.EQ.2 ) THEN
    	    YLO = LOG10(XLOW(KSET+1))
    	    YHI = LOG10(XHIG(KSET+1))
    	ELSE
     	    YLO = 1.0/XLOW(KSET+1)
     	    YHI = 1.0/XHIG(KSET+1)
    	ENDIF
     	YLO = YLO - YDL*OFFS(KSET+1)
     	YHI = YHI + YDL*OFFS(KSET+1)
    	IF( YLO.GT.YHI ) THEN
    	    YDL = YLO
    	    YLO = YHI
    	    YHI = YDL
    	ENDIF

     	RETURN
    	END

    	SUBROUTINE XYPLOT_VINCH( IFLAG, DELTAX, DELTAY )
C
C	THIS ROUTINE CALCULATES SCALE VALUE PER INCH FOR X,Y PLOT.
C
C	IFLAG=1: RETURN DELTAX AND DELTAY FOR 1ST GRAPH BAND.
C	IFLAG=2: RETURN DELTAX AND DELTAY FOR 2ND GRAPH BAND.
C
C	DELTAX = SCALE VALUE PER INCH OF X-AXIS.
C	DELTAY = SCALE VALUE PER INCH OF Y-AXIS.
C
     	INCLUDE 'XYPLOT.CBS'

     	IF( IFLAG.EQ.1 ) THEN
     	    KSET = 1
     	ELSE
     	    KSET = 3
     	ENDIF
C
C  CALCULATE DELTAX...
C
     	XLENG = XLNS(KSET) - 2.0*OFFS(KSET)
     	LTYPE = IABS(LAXS(KSET))
     	IF( LTYPE.EQ.1 ) THEN
     	    DELTAX = (XHIG(KSET)-XLOW(KSET)) / XLENG
     	ELSE IF( LTYPE.EQ.2 ) THEN
    	    DELTAX = LOG10(XHIG(KSET)/XLOW(KSET)) / XLENG
     	ELSE
     	    DELTAX = (1.0/XHIG(KSET)-1.0/XLOW(KSET)) / XLENG
     	ENDIF
C
C  CALCULATE DELTAY...
C
     	XLENG = XLNS(KSET+1) - 2.0*OFFS(KSET+1)
     	LTYPE = IABS(LAXS(KSET+1))
     	IF( LTYPE.EQ.1 ) THEN
     	    DELTAY = (XHIG(KSET+1)-XLOW(KSET+1)) / XLENG
     	ELSE IF( LTYPE.EQ.2 ) THEN
    	    DELTAY = LOG10(XHIG(KSET+1)/XLOW(KSET+1)) / XLENG
     	ELSE
     	    DELTAY = (1.0/XHIG(KSET+1)-1.0/XLOW(KSET+1)) / XLENG
     	ENDIF

    	RETURN
    	END

    	FUNCTION XYPLOT_SETUP( IFLAG, FNAME )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY
C
C	THIS ROUTINE WRITES/READS A PARAMETER/SETUP FILE FOR X,Y PLOT.
C
C	IFLAG --- 1 : READ IN THE PARAMETERS FROM "FNAME"
C	      --- 0 : WRITE THE PARAMETERS TO "FNAME"
C
C		RETURN WITH "0" FOR ERROR STATUS, "1" FOR OK.
C
C	FNAME --- PARAMETER/SETUP FILENAME	
C
    	INCLUDE		'XYPLOT.CBS'
    	PARAMETER	NKS= 15, LUN= 13
    	CHARACTER	KWD(NKS)*4, KEY*4, REST*127, FNAME*(*)
    	DIMENSION	KP(20), RP(20), IRR(NKS)
C
C  PARAMETER GROUP KEYWORDS...
C
     	DATA KWD/ 'TITL', 'XLB1', 'YLB1', 'XLB2', 'YLB2',
     +            'OVER', 'XAX1', 'YAX1', 'XAX2', 'YAX2',
     +            'SETS', 'LABL', 'PAR1', 'PAR2', 'IDEN' /
C
C  NUMBER OF NUMERIC PARAMETERS IN EACH SETUP GROUP...
C
     	DATA IRR/ 0,0,0,0,0,14,12,12,12,12,13,0,10,10,0 /
C
C  SET DEFAULT FILE EXTENTION TO .PAR ...
C
     	IF( FNAME.EQ.' ' ) RETURN
     	CALL IFMISC_DEFEXT( FNAME, '.PAR' )
    	IF( IFLAG.EQ.1 ) GOTO 5
C
C  WRITE THE SETUP FILE...
C
    	OPEN( UNIT=LUN, FILE=FNAME, STATUS='UNKNOWN',
     +        CARRIAGECONTROL='LIST', ERR=888 )

    	WRITE(LUN,1000,ERR=888) (TITL(I),I=1,5)
     +, DRAW, MARK, QUAD, NSDS, COL1, COL2
     +, XLPS, YLPS, XORG, YORG, XOFF, YOFF, XSCL, YSCL
     +,(LAXS(I), NTKS(I), MTKS(I), NDGS(I), IGDS(I)
     +, XLNS(I), OFFS(I), SOFF(I), XANG(I)
     +, XLOW(I), XHIG(I), XINC(I), I=1,4)
     +,(HPAR(I), I=1, 20), PLOTID

1000	FORMAT('%This is a GRAFIT parameter/setup file:'/
     +,'TITL=',A80,/,'XLB1=',A80,/,'YLB1=',A80,/,'XLB2=',A80,/
     +,'YLB2=',A80,/,'OVER=',6I4,8F8.3,/
     +,'XAX1=',5I4,4F8.3,3G13.5,/,'YAX1=',5I4,4F8.3,3G13.5,/
     +,'XAX2=',5I4,4F8.3,3G13.5,/,'YAX2=',5I4,4F8.3,3G13.5,/
     +,'PAR1=',10F7.2,/,'PAR2=',10F7.2,/'IDEN=',A32)

     	WRITE(LUN,1001,ERR=888) (IPEN(I), ISYM(I), ILIN(I), IDEN(I)
     +, ISET(I), IBOL(I), ISHD(I), SSIZ(I), XSH(I), YSH(I), XSF(I)
     +, YSF(I), SRL(I), LABL(I), I=1,NSET)

1001	FORMAT('SETS=',7I4,3F8.3,3G13.5,/,'LABL=',A80)
    	GOTO 999
C
C  READ A SETUP FILE...
C
  5	OPEN( UNIT=LUN, FILE=FNAME, STATUS='OLD',
     +        CARRIAGECONTROL='LIST', READONLY, ERR=888 )
     	KSET = 0
C
C  READ KEYWORD LINES ONE BY ONE...
C
 111	READ(LUN,112,END=777) KEY,REST
 112	FORMAT(A4,X,A127)
     	IF( KEY.EQ.' ' .OR. REST.EQ.' ' ) GOTO 111
C
C  LOOK UP THE KEYWORD...
C
     	DO I=1, NKS
     	   IF( KEY.EQ.KWD(I) ) GOTO 222
     	ENDDO
     	GOTO 111
C
C  YES, WE HAVE A VALID KEYOWRD...
C
 222	NCH = IFMISC_STRIP( REST )
     	IF( NCH.GT.0 .AND. IRR(I).GT.0 ) THEN
     	    NCH = IFMISC_DECODE( REST, IRR(I), RP, KP )
    	    IF( NCH.EQ.0 ) GOTO 111	!ERROR IN THE INPUT
     	ENDIF
C
C  SAVE THE PARAMETERS...
C
     	IF( I.LT.6 ) THEN	!GRAPH TITLE AND AXIS LABELS
     	    TITL(I) = REST
     	    GOTO 111
     	ENDIF

     	IF( I.EQ.6 ) THEN	!OVERALL GRAPH PARAMETERS
     	    IF( KP( 1).EQ.1 ) DRAW = RP( 1)
     	    IF( KP( 2).EQ.1 ) MARK = RP( 2)
     	    IF( KP( 3).EQ.1 ) QUAD = RP( 3)
     	    IF( KP( 4).EQ.1 ) NSDS = RP( 4)
     	    IF( KP( 5).EQ.1 ) COL1 = RP( 5)
     	    IF( KP( 6).EQ.1 ) COL2 = RP( 6)
     	    IF( KP( 7).EQ.1 ) XLPS = RP( 7)
     	    IF( KP( 8).EQ.1 ) YLPS = RP( 8)
     	    IF( KP( 9).EQ.1 ) XORG = RP( 9)
     	    IF( KP(10).EQ.1 ) YORG = RP(10)
     	    IF( KP(11).EQ.1 ) XOFF = RP(11)
     	    IF( KP(12).EQ.1 ) YOFF = RP(12)
     	    IF( KP(13).EQ.1 ) XSCL = RP(13)
     	    IF( KP(14).EQ.1 ) YSCL = RP(14)
     	    GOTO 111
     	ENDIF

     	IF( I.LT.11 ) THEN		!AXIS DRAWING PARAMETERS
     	    I = I-6
     	    IF( KP( 1).EQ.1 ) LAXS(I) = RP( 1)
     	    IF( KP( 2).EQ.1 ) NTKS(I) = RP( 2)
     	    IF( KP( 3).EQ.1 ) MTKS(I) = RP( 3)
     	    IF( KP( 4).EQ.1 ) NDGS(I) = RP( 4)
     	    IF( KP( 5).EQ.1 ) IGDS(I) = RP( 5)
     	    IF( KP( 6).EQ.1 ) XLNS(I) = RP( 6)
     	    IF( KP( 7).EQ.1 ) OFFS(I) = RP( 7)
     	    IF( KP( 8).EQ.1 ) SOFF(I) = RP( 8)
     	    IF( KP( 9).EQ.1 ) XANG(I) = RP( 9)
     	    IF( KP(10).EQ.1 ) XLOW(I) = RP(10)
     	    IF( KP(11).EQ.1 ) XHIG(I) = RP(11)
     	    IF( KP(12).EQ.1 ) XINC(I) = RP(12)
     	    GOTO 111
     	ENDIF

     	IF( I.EQ.11 ) THEN	!DATA SET DRAWING PARAMETERS
     	    IF( KSET.EQ.MNS ) GOTO 111
     	    KSET = KSET + 1
     	    IF( KP( 1).EQ.1 ) IPEN(KSET) = RP( 1)
     	    IF( KP( 2).EQ.1 ) ISYM(KSET) = RP( 2)
     	    IF( KP( 3).EQ.1 ) ILIN(KSET) = RP( 3)
     	    IF( KP( 4).EQ.1 ) IDEN(KSET) = RP( 4)
     	    IF( KP( 5).EQ.1 ) ISET(KSET) = RP( 5)
     	    IF( KP( 6).EQ.1 ) IBOL(KSET) = RP( 6)
     	    IF( KP( 7).EQ.1 ) ISHD(KSET) = RP( 7)
     	    IF( KP( 8).EQ.1 ) SSIZ(KSET) = RP( 8)
     	    IF( KP( 9).EQ.1 )  XSH(KSET) = RP( 9)
     	    IF( KP(10).EQ.1 )  YSH(KSET) = RP(10)
     	    IF( KP(11).EQ.1 )  XSF(KSET) = RP(11)
     	    IF( KP(12).EQ.1 )  YSF(KSET) = RP(12)
     	    IF( KP(13).EQ.1 )  SRL(KSET) = RP(13)
     	    GOTO 111
     	ENDIF

     	IF( I.EQ.12 ) THEN	!DATA SET ID LABEL
     	    LABL(LSET(1,KSET)) = REST
     	    GOTO 111
     	ENDIF

     	IF( I.EQ.13 ) THEN	!HARDCOPY SETUP PARAMETERS
     	    DO  I=1, 10
     		IF( KP(I).EQ.1 ) HPAR(I) = RP(I)
     	    ENDDO
     	    GOTO 111
     	ENDIF
     	IF( I.EQ.14 ) THEN	!HARDCOPY SETUP PARAMETERS
     	    DO  I=1, 10
     		IF( KP(I).EQ.1 ) HPAR(I+10) = RP(I)
     	    ENDDO
     	    GOTO 111
     	ENDIF

     	IF( I.EQ.15 ) THEN	!PLOT ID LABEL
     	    PLOTID = REST
     	    GOTO 111
     	ENDIF
     	GOTO 111
C
C  CHECK BAND CODE AND SCALE UNITS...
C
777	KSET = 0
     	BAND = .FALSE.
     	DO I=1, NSET
    	   IF( ISET(I).NE.2 ) KSET = 1
    	   IF( ISET(I).NE.1 ) BAND = .TRUE.
    	ENDDO
     	IF( KSET.EQ.0 ) THEN
     	    BAND = .FALSE.
     	    DO  I=1, NSET
     		ISET(I) = 1
     	    ENDDO
     	ENDIF
     	IF(XINC(1).EQ.0.0.OR.XLOW(1).EQ.XHIG(1)) I=XYPLOT_SCALE(1)
     	IF(XINC(2).EQ.0.0.OR.XLOW(2).EQ.XHIG(2)) I=XYPLOT_SCALE(2)
     	IF( BAND ) THEN
     	IF(XINC(3).EQ.0.0.OR.XLOW(3).EQ.XHIG(3)) I=XYPLOT_SCALE(3)
     	IF(XINC(4).EQ.0.0.OR.XLOW(4).EQ.XHIG(4)) I=XYPLOT_SCALE(4)
     	ENDIF
     	GOTO 999

888	XYPLOT_SETUP = 0
     	RETURN

999    	CLOSE(UNIT=LUN)
     	XYPLOT_SETUP = 1
    	RETURN
    	END

     	SUBROUTINE XYPLOT_MENU_HELP
C
C	THIS ROUTINE DISPLAYS ON-LINE HELP FOR X,Y PLOT.
C
     	CHARACTER TEXT*80, KEY*4
     	COMMON/XYPLOT_MENU/ JSET, IKEY, NCHS, TEXT

     	NCHS = IFMISC_NEXT(TEXT)
     	IF( NCHS.EQ.2 ) THEN
     	    IKEY=IFMISC_HELP('GRAFIT$HLP:XYPLOT_LINE.HLP',TEXT,21,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:XYPLOT_HELP.HLP',KEY,21)
     	    IF( IKEY.GT.0 ) GOTO 888
     	    CALL XYPLOT_FORM(JSET)
     	ENDIF
     	RETURN
C
C  ERROR TRANSFER...
C
888	CALL IFANSI_VIDEO(70)
     	IF(IKEY.EQ.1) THEN
     	   CALL XYPLOT_OOPS(' Help file is not available.')
     	ELSE
     	   TEXT = ' No help found for keyword> '
     	   TEXT(29:) = KEY
     	   CALL XYPLOT_OOPS( TEXT )
     	ENDIF
     	RETURN
     	END

     	SUBROUTINE XYPLOT_DELETE
C
C	THIS ROUTINE DELETES OR REMOVES A SET OF DATA FOR X,Y PLOT.
C
     	INCLUDE 'XYPLOT.CBS'
     	CHARACTER TEXT*80
     	COMMON/XYPLOT_MENU/ JSET, IKEY, NCHS, TEXT
C
C  GET THE SET NUMBER TO REMOVE...
C
     	NCHS = IFMISC_NEXT(TEXT)
     	IF( NCHS.EQ.0 ) THEN
     	    CALL IFANSI_TEXT('_Data set #', 11, 22, 9 )
     	    READ(5,1001,END=888) TEXT
1001	    FORMAT(A)
     	    NCHS = IFMISC_STRIP(TEXT)
     	    IF( NCHS.EQ.0 ) GOTO 888
     	ENDIF
     	DECODE( NCHS, 111, TEXT, ERR=999 ) IKEY
111	FORMAT(I)
     	IF( IKEY.LT.1 .OR. IKEY.GT.NSET ) THEN
     	    CALL XYPLOT_ERROR(4)
     	    GOTO 888
     	ENDIF
C
C  REPACK THE LIST...
C
     	M = LSET(1,IKEY)-1
     	DO  I= IKEY, NSET-1
     	    M = M + 1
     	    J = I + 1
     	    K = LSET(1,J)
     	    N = LSET(2,J)
     	    LSET(1,I) = M
     	    LSET(2,I) = N
     	    NPTS = NUMS(K)
     	    NUMS(M) = NPTS
     	    LABL(M) = LABL(K)
     	    DO IJ = 1, 6
     		SRNG(IJ,M) = SRNG(IJ,K)
     	    ENDDO
     	    DO IJ = 1, NPTS
     		X(IJ,M) = X(IJ,K)
     		Y(IJ,M) = Y(IJ,K)
     		IF(N.GT.0) Y(IJ,M+1) = Y(IJ,K+1)
     	    ENDDO
     	    IF( N.GT.0 ) M = M + 1
     	    IPEN(I) = IPEN(J)
     	    ISYM(I) = ISYM(J)
     	    ILIN(I) = ILIN(J)
     	    IDEN(I) = IDEN(J)
     	    ISET(I) = ISET(J)
     	    IBOL(I) = IBOL(J)
     	    ISHD(I) = ISHD(J)
     	    SSIZ(I) = SSIZ(J)
     	     XSH(I) =  XSH(J)
     	     YSH(I) =  YSH(J)
     	     XSF(I) =  XSF(J)
     	     YSF(I) =  YSF(J)
     	     SRL(I) =  SRL(J)
     	ENDDO
     	NSET = NSET - 1
     	MSET = MSET - 1
     	CALL XYPLOT_PARS_NSET
     	IF( JSET.GT.NSET ) JSET = NSET
     	IF( JSET.GE.IKEY ) CALL XYPLOT_PARS_SET(JSET,0)
888	RETURN
999	CALL XYPLOT_ERROR(3)
     	RETURN
     	END

     	FUNCTION XYPLOT_MISC_ADD( JSET )
C
C	THIS ROUTINE ADDS/SUBTRACTS/AVERAGES DATA SETS.
C
     	INCLUDE 'XYPLOT.CBS'
     	CHARACTER TEXT*16
     	DIMENSION RP(2), KP(2), IX(MNS), CX(MNS)

     	WRITE(6,1000)
1000	FORMAT(' Following formula is used to accomplish addition',
     +' or subtraction or averaging'/' of data sets:  SET(NEW) =',
     +' (SET(I)*C(I)+SET(J)*C(J)+SET(K)*C(K)+...) / N'/
     +' where C(I),C(J),... and N are user-specified constants.',
     +' The action is on the'/' Y-coordinates of data sets which',
     +' must have identical numbers of data points.'/' The resulting',
     +' data set takes the X-coordinates of the 1st set data selected.'/
     +' Enter DEL to delete the last OK input; CTRL/Z to exit.')
C
C  SELECT DATA SETS TO WORK ON...
C
     	KSET = 0
111	WRITE(6,1001)
1001	FORMAT('$Enter I and C(I) [0,1.0] ')
     	READ(5,1002,END=444) TEXT
1002	FORMAT(A)
     	IF( TEXT.EQ.' ' ) GOTO 111
     	CALL IFMISC_STRIP( TEXT )
     	IF( TEXT(1:3).EQ.'DEL' ) THEN
     	    IF( KSET.GT.0 ) KSET = KSET - 1
     	    GOTO 111
     	ENDIF
     	NCHS = IFMISC_DECODE( TEXT, 2, RP, KP )
     	IF( NCHS.EQ.0 ) GOTO 222
     	NCHS = RP(1)
     	IF( NCHS.LT.1 .OR. NCHS.GT.NSET ) GOTO 222
     	DO  I= 1, KSET
     	    IF( NCHS.EQ.IX(I) ) GOTO 222
     	ENDDO
     	IF( KSET.EQ.1 ) NPTS = NUMS(LSET(1,IX(1)))
     	IF( KSET.GT.1 ) THEN
     	    IF( NUMS(LSET(1,NCHS)).NE.NPTS ) GOTO 222
     	ENDIF
     	KSET = KSET + 1
     	IX(KSET) = NCHS
     	CONS = RP(2)
     	IF( CONS.EQ.0.0 ) CONS = 1.0
     	CX(KSET) = CONS
     	IF( KSET.EQ.NSET ) GOTO 555
     	GOTO 111
222	CALL IFANSI_VIDEO(70)
     	WRITE(6,223)
223	FORMAT(' oops: Invalid input.')
     	CALL IFANSI_VIDEO(47)
     	GOTO 111
C
C  GET THE VALUE OF "N"...
C
444	IF( KSET.LT.2 ) GOTO 999
555	WRITE(6,2000)
2000	FORMAT('$Enter N [1] ')
     	READ(5,2001,ERR=555,END=999) NCHS
2001	FORMAT(I)
     	IF( NCHS.EQ.0 ) NCHS = 1
C
C  CARRY OUT THE ACTION...
C
     	NSET = NSET + 1
     	MSET = MSET + 1
     	LSET(1,NSET) = MSET
     	LSET(2,NSET) = 0
     	NUMS(MSET) = NPTS
     	K = LSET(1,IX(1))
     	DO  I=1, NPTS
     	    X(I,MSET) = X(I,K)
     	    CONS = 0.0
     	    DO  J=1, KSET
     		CONS = CONS + CX(J)*Y(I,LSET(1,IX(J)))
     	    ENDDO
     	    Y(I,MSET) = CONS/NCHS
     	ENDDO
     	CALL IFMISC_RANGE(NPTS,X(1,MSET),Y(1,MSET),SRNG(1,MSET))
     	CALL IFANSI_VIDEO(20)
     	NCHS = IFMISC_DIGITS( NSET )
     	WRITE(6,888) NSET
888	FORMAT(' Resulting data set is saved as data set #',I<NCHS>)
     	XYPLOT_MISC_ADD = 1
     	RETURN
999	XYPLOT_MISC_ADD = 0
     	RETURN
     	END

     	FUNCTION XYPLOT_ZOOM()
C
C	THIS ROUTINE ZOOMS A X,Y PLOT.
C
     	INCLUDE 'XYPLOT.CBS'
     	DIMENSION Z1(2), Z2(2)

     	IF( BAND ) THEN
     	    CALL IFANSI_VIDEO(70)
     	    WRITE(6,1000)
1000	    FORMAT(' oops: Zoom is not allowed for a two band plot.')
     	    GOTO 999
     	ENDIF
C
C  INVOKE XYPLOT_GIN...
C
     	CALL XYPLOT_GIN( Z1, Z2 )
     	IF( Z1(1).EQ.Z2(1) .OR. Z1(2).EQ.Z2(2) ) GOTO 999
C
C  SCALE THE NEW PLOT WINDOW...
C
     	DO  I=1, 2
     	    IT = IABS(LAXS(I))
     	    X1 = Z1(I)
     	    X2 = Z2(I)
     	    IF( X1.GT.X2 ) THEN
     		X3 = X1
     		X1 = X2
     		X2 = X3
     	    ENDIF
     	    IF( IT.NE.2 ) THEN
     		IT = IFAXIS_LINSCL( XLNS(I), X1, X2 )
     		NTKS(I) = IT
     		XLOW(I) = X1
     		XHIG(I) = X2
     		XINC(I) = (X2-X1)/IT
     	    ELSE
     		IT = IFAXIS_LOGSCL( X1, X2 )
     		XLOW(I) = X1
     		XHIG(I) = X2
     		XINC(I) = IT
     	    ENDIF
     	ENDDO
     	XYPLOT_ZOOM = 1
     	RETURN
999	XYPLOT_ZOOM = 0
     	RETURN
     	END

     	FUNCTION XYPLOT_DATA(FNS,MNS,MNP,NST,NUM,X,Y,LABL)
C
C  PROGRAMMER:  RONG-SHENG ZHOU
C		ALFRED UNIVERSITY,  MARCH-1986
C
C       THIS ROUTINE READS A DATA FILE FOR X,Y PLOT.
C
C	FNS ---- NAME OF DATA FILE TO READ.
C	MNS ---- MAXIMUM NUMBER OF DATA SETS ALLOWED FROM THIS FILE.
C	MNP ---- MAXIMUM NUMBER OF DATA POINTS ALLOWED IN EACH SET.
C
C	NST ---- NUMBER OF DATA SETS READ.
C	NUM ---- 1-D NUMBER OF DATA POINTS IN EACH SET READ.
C	X,Y ---- 2-D ARRAY THE STORING X,Y-COORDINATES.
C	LABL --- DATA SET CHARACTER ID.
C
C	XYPLOT_DATA=0: FILE READ SUCCESSFUL.
C	XYPLOT_DATA=1: FILE OPEN FAILURE.
C	XYPLOT_DATA=2: ERROR IN FILE FORMAT AND "NST" RETURNS WITH
C		       RECORD/LINE NUMBER WHERE THE ERROR OCCURS.
C
    	PARAMETER	LUN= 13
    	CHARACTER	LABL(MNS)*80, FNS*(*)
    	DIMENSION	NUM(MNS), X(MNP,MNS), Y(MNP,MNS)
     	INTEGER		FORMAT

    	NCHS = IFMISC_LENG( FNS )
    	NDOT = INDEX( FNS(1:NCHS), ']' )
    	NDOT = INDEX( FNS(NDOT+1:NCHS), '.' )
     	IF( NDOT.GT.0 ) GOTO 333
    	IERR = 1
     	GOTO 222
C
C  OPEN FILE IN THE PRIORITY FROM *.PLT, *.DAT, *.RAW...
C
  111	CLOSE(LUN)
     	IF( NDOT.GT.0 ) THEN
     	    XYPLOT_DATA = 1.0
     	    RETURN
     	ENDIF
  222	IF( IERR.EQ.1 ) THEN
     	    FNS(NCHS+1: ) = '.PLT'
     	    IERR = 2
     	ELSE IF( IERR.EQ.2 ) THEN
    	    FNS(NCHS+1: ) = '.DAT'
    	    IERR = 3
     	ELSE IF( IERR.EQ.3 ) THEN
     	    FNS(NCHS+1: ) = '.RAW'
    	    NDOT = 1
     	ENDIF
C
C  OPEN THE FILE...
C
  333	FORMAT = XYPLOT_DATA_OPEN(LUN,FNS)
     	IF( FORMAT.EQ.0 ) GOTO 111
     	IF(NST.GE.0) CALL XYPLOT_OOPS(' Data acquisition in process...')
     	IF( FORMAT.GT.0 ) THEN		!FORMATTED FILE
     	    IERR = XYPLOT_DATA_ASCII(LUN,MNS,MNP,NST,NUM,X,Y,LABL)
     	ELSE				!UNFORMATTED FILE
     	    IERR = XYPLOT_DATA_BINARY(LUN,MNS,MNP,NST,NUM,X,Y)
     	ENDIF
  	CLOSE(LUN)
     	XYPLOT_DATA = IERR
    	RETURN
    	END

     	FUNCTION XYPLOT_DATA_OPEN(LUN,FNAME)
C
C	THIS ROUTINE OPENS A FILE:
C
C	XYPLOT_DATA_OPEN=0: OPEN FAILURE
C	XYPLOT_DATA_OPEN>0: OPENED A FORMATTED FILE.
C	XYPLOT_DATA_OPEN<0: OPENED AN UNFORMATTED FILE.
C
     	CHARACTER FNAME*(*)
     	INTEGER   FORMAT
     	LOGICAL   YES
     	BYTE      TEST(32)

     	FORMAT = 0
     	INQUIRE( FILE=FNAME, EXIST=YES )
     	IF( .NOT.YES ) GOTO 999
     	OPEN(UNIT=LUN,FILE=FNAME,STATUS='OLD',FORM='FORMATTED',ERR=888)
     	READ(LUN,1000,ERR=222) TEST
1000	FORMAT(32A1)
     	DO I=1, 32
     	   IF( TEST(I).LT.7 ) GOTO 222
     	ENDDO
     	FORMAT = 1
     	REWIND(LUN)
     	GOTO 999
222	CLOSE(LUN)
     	OPEN(UNIT=LUN,FILE=FNAME,STATUS='OLD',FORM='UNFORMATTED',
     +       ERR=888)
     	FORMAT = -1
     	GOTO 999
888	CLOSE(LUN)
999	XYPLOT_DATA_OPEN = FORMAT
     	RETURN
     	END

     	FUNCTION XYPLOT_DATA_ASCII(LUN,MNS,MNP,NST,NUM,X,Y,LABL)
C
C	THIS ROUTINE READS AN ASCII DATA FILE.
C
    	CHARACTER	TEXT*132, ID*80, DATA*80, LABL(MNS)*80
    	DIMENSION	NUM(MNS), X(MNP,MNS), Y(MNP,MNS)
     	DIMENSION	RP(50), KP(50)
     	LOGICAL		BERR

     	IERR = 0
     	LINE = 0
     	NSET = 0
     	LUND = LUN
     	MNSS = MNS
     	MNPP = MNP
C
C  READ THE FIRST LINE TO PARSE FILE FORMAT...
C
 111	LINE = LINE + 1
     	READ(LUND,1000,ERR=888,END=999) DATA
    	IF( DATA.EQ.' ' ) GOTO 111
1000	FORMAT(A)

     	ID = ' '
    	N2 = IFMISC_STRIP( DATA )
     	N1 = INDEX( DATA(1:N2), '!' )
     	IF( N1.GT.0 ) THEN			!DATA SET ID
     	    ID = DATA(N1+1: )
     	    DATA = DATA(1:N1-1)
     	ENDIF
    	N3 = IFMISC_DECODE( DATA, 4, RP, KP )
    	IF( N3.EQ.0 ) GOTO 888

    	N2 = RP(1)
     	BERR = .FALSE.
    	IF( KP(4).EQ.1) GOTO 4444	!FILE FORMAT #4
    	IF( KP(2).EQ.1) GOTO 2222	!FILE FORMAT #2
    	IF( N2.LT.0   ) GOTO 3333	!FILE FORMAT #3
    	IF( N2.EQ.0   ) GOTO 2222	!FILE FORMAT #2
C
C  FILE FORMAT #1...
C
 110	N1 = N2
    	IF( N1.GT.MNPP ) N1 = MNPP
	NSET = NSET + 1
    	DO  I=1, N1
 120	    LINE = LINE + 1
    	    READ(LUND,1000,ERR=888,END=190) DATA
     	    IF( DATA.EQ.' ' ) GOTO 120
     	    N3 = IFMISC_STRIP( DATA )
     	    N4 = IFMISC_DECODE( DATA, 3, RP, KP )
     	    IF( N4.EQ.0 ) GOTO 888
     	    X(I,NSET) = RP(1)
     	    Y(I,NSET) = RP(2)
     	    IF( RP(3).GT.0.0 .AND. NSET.LT.MNSS ) THEN
     		Y(I,NSET+1)= RP(3)
     	        BERR = .TRUE.
     	    ENDIF
    	ENDDO
     	IF( ID.NE.' ' ) LABL(NSET) = ID
    	IF( .NOT.BERR ) THEN
     	    NUM(NSET) =  N1
     	ELSE
     	    NUM(NSET) = -N1
     	    NSET = NSET + 1
     	    NUM(NSET) = 0
     	ENDIF
	IF( NSET.EQ.MNSS ) GOTO 999
    	IF( N2.LE.MNPP ) GOTO 111
    	N2 = N2 - MNPP
     	BERR = .FALSE.
    	GOTO 110	!SPLITED SET
C
C  END OF FILE...
C
 190	IF( I.EQ.1 ) GOTO 888
     	I = I - 1
     	IF( ID.NE.' ' ) LABL(NSET) = ID
    	IF( .NOT.BERR ) THEN
     	    NUM(NSET) =  I
     	ELSE
     	    NUM(NSET) = -I
     	    NSET = NSET + 1
     	    NUM(NSET) = 0
     	ENDIF
    	GOTO 999
C
C  FILE FORMAT #2...
C
2222	I = 1
     	NSET = NSET + 1
     	GOTO 220
 210	LINE = LINE + 1
     	READ(LUND,1000,ERR=888,END=190) DATA
     	IF( DATA.EQ.' ' ) GOTO 210
     	N3 = IFMISC_STRIP( DATA )
     	N4 = IFMISC_DECODE( DATA, 4, RP, KP )
     	IF( N4.EQ.0 ) GOTO 888
 220	X(I,NSET) = RP(1)
     	Y(I,NSET) = RP(2)
     	IF( RP(3).GT.0.0 .AND. NSET.LT.MNSS ) THEN
     	    Y(I,NSET+1)= RP(3)
     	    BERR = .TRUE.
     	ENDIF
     	IF( RP(4).NE.0.0 .OR. I.EQ.MNPP ) GOTO 250
    	I = I + 1
     	GOTO 210

 250	N3 = INDEX( DATA, '!' )
     	IF( N3.GT.0 ) ID = DATA(N3+1:)
     	IF( ID.NE.' ' ) LABL(NSET) = ID
     	IF( .NOT.BERR ) THEN
     	    NUM(NSET) = I
     	ELSE
     	    NUM(NSET) = -I
     	    NSET = NSET + 1
     	    NUM(NSET) = 0
     	ENDIF
     	IF( NSET.EQ.MNSS ) GOTO 999
     	IF( RP(4).NE.0.0 ) GOTO 111
     	I = 1
     	NSET = NSET + 1
     	BERR = .FALSE.
    	GOTO 210
C
C  FILE FORMAT #3...
C
3333	N1 = IABS(N2)
    	IF( N1+NSET.GT.MNSS ) N1 = MNSS - NSET
     	IF( N1.GT.49 ) N1 = 49
     	I = 1
 310	LINE = LINE + 1
     	READ(LUND,1000,ERR=888,END=350) TEXT
     	IF( TEXT.EQ.' ' ) GOTO 310
     	IF( TEXT(1:1).EQ.'!' ) GOTO 390
     	N3 = IFMISC_STRIP( TEXT )
     	N4 = IFMISC_DECODE( TEXT, N1+1, RP, KP )
     	IF( N4.EQ.0 ) GOTO 888
     	DO J=1, N1
     	   X(I,NSET+J) = RP(1)
     	   Y(I,NSET+J) = RP(J+1)
     	ENDDO
     	I = I + 1
     	IF( I.GT.MNPP ) GOTO 350
     	GOTO 310

 350	IF( I.EQ.1 ) GOTO 888
     	I = I - 1
     	DO J=1, N1
    	   NSET = NSET + 1
    	   NUM(NSET) = I
    	ENDDO
    	GOTO 999
C
C  GET DATA SET ID IF AT ALL...
C
 360	IF( J.EQ.N1 ) GOTO 350
     	LINE = LINE + 1
     	READ(LUND,1000,ERR=888,END=350) DATA
     	IF( DATA(1:1).NE.'!' ) GOTO 360
     	J = J + 1
     	IF( DATA(2:).NE.' ' ) LABL(NSET+J) = DATA(2:)
     	GOTO 360
 390	J = 1
     	IF( TEXT(2:).NE.' ' ) LABL(NSET+J) = TEXT(2:)
     	GOTO 360
C
C  FILE FORMAT #4...
C
4444	N1 = RP(4)+0.1
    	IF( N1.LT.1 .OR. N1.GT.50 ) GOTO 888
     	X1 = RP(1)-RP(3)
     	X2 = RP(2)
     	X3 = RP(3)
    	IF( X3.EQ.0.0 ) GOTO 888
    	N2 = INT((X2-X1)/X3+0.5) + 1
    	IF( N2.LT.N1 ) GOTO 888
     	N3 = N2/N1
     	IF( N3*N1.LT.N2 ) N3 = N3 + 1
     	N5 = 0
     	N6 = 0
    	NSET = NSET + 1
    	DO  I=1, N3
 410	    LINE = LINE + 1
     	    READ(LUND,1000,ERR=888,END=888) TEXT
     	    IF( TEXT.EQ.' ' ) GOTO 410
     	    N4 = IFMISC_STRIP( TEXT )
     	    N4 = IFMISC_DECODE( TEXT, N1, RP, KP )
    	    IF( N4.EQ.0 ) GOTO 888
     	    DO  J=1, N1
     		N5 = N5 + 1
    		N6 = N6 + 1
     		X(N5,NSET) = X1 + X3*N6
    		Y(N5,NSET) = RP(J)
     		IF( N6.EQ.N2 ) GOTO 490
     		IF( N5.EQ.MNPP ) THEN
     		    NUM(NSET) = N5
     		    IF( ID.NE.' ' ) LABL(NSET) = ID
     		    IF( NSET.EQ.MNSS ) GOTO 999
     		    NSET = NSET + 1
     		    N5 = 0
     		ENDIF
     	    ENDDO
    	ENDDO
 490	NUM(NSET) = N5
     	IF( ID.NE.' ' ) LABL(NSET) = ID
    	IF( NSET.EQ.MNSS ) GOTO 999
     	GOTO 111
C
C  ERROR TRANSFERS...
C
888	NSET = LINE
     	IERR = 2

999	NST = NSET
     	XYPLOT_DATA_ASCII = IERR
     	RETURN
     	END

     	FUNCTION XYPLOT_DATA_BINARY(LUN,MNS,MNP,NST,NUM,X,Y)
C
C	THIS ROUTINE READS A BINARY DATA FILE.
C
    	DIMENSION	NUM(MNS), X(MNP,MNS), Y(MNP,MNS)
     	DIMENSION	RP(10)

     	IERR = 0
     	LINE = 0
     	NSET = 0
     	LUND = LUN
     	MNSS = MNS
     	MNPP = MNP

111	LINE = LINE + 1
     	READ(LUND,ERR=222,END=777) RP(1), RP(2), RP(3)
    	GOTO 6666
222	REWIND( LUND )
     	LINE = 0
223	LINE = LINE + 1
    	READ(LUND,ERR=888,END=777) N3
C
C  FILE FORMAT #5...
C
    	IF( N3.LT.2 ) GOTO 888
     	N2 = 0
    	IF( N3.GT.MNPP ) THEN
     	    N2 = N3/MNPP
     	    IF( N2*MNPP.LT.N3 ) N2 = N2 + 1
     	    IF( N2.GT.10 ) N2 = 10
     	    N3 = N3/N2
     	    N2 = N2-1
     	ENDIF
    	NSET = NSET + 1
    	LINE = LINE + 1
    	READ(LUND,ERR=888,END=888) (X(I,NSET),(RP(J),J=1,N2),I=1,N3)
     	LINE = LINE + 1
     	READ(LUND,ERR=888,END=888) (Y(I,NSET),(RP(J),J=1,N2),I=1,N3)
     	NUM(NSET) = N3
     	IF( NSET.EQ.MNSS ) GOTO 999
    	GOTO 223
C
C  FILE FORMAT #6 ...
C
6666	IF( RP(3).EQ.0.0 ) GOTO 888
     	X1 = RP(1)
     	X2 = RP(2)
     	X3 = RP(3)
    	N3 = INT((X2-X1)/X3+0.5) + 1
    	IF( N3.LT.2 ) GOTO 888
     	N2 = 0
    	IF( N3.GT.MNPP ) THEN
     	    N2 = N3/MNPP
     	    IF( N2*MNPP.LT.N3 ) N2 = N2 + 1
     	    IF( N2.GT.10 ) N2 = 10
     	    N3 = N3/N2
     	    N2 = N2-1
     	ENDIF
    	NSET = NSET + 1
    	LINE = LINE + 1
    	READ(LUND,ERR=888,END=888) (Y(I,NSET),(RP(J),J=1,N2),I=1,N3)
     	N2 = N2 + 1
     	X1 = X1 - X3
     	DO I=1, N3*N2, N2
     	   X(I,NSET) = X1+X3*I
     	ENDDO
    	NUM(NSET) = N3
    	IF( NSET.EQ.MNSS ) GOTO 999
     	GOTO 111
C
C  ERROR TRANSFER...
C
777	IF( NSET.GT.0 ) GOTO 999
888	IERR = 2
    	NSET = LINE

999	NST = NSET
     	XYPLOT_DATA_BINARY = IERR
     	RETURN
     	END

     	FUNCTION XYPLOT_NEXT(TEXT)
     	CHARACTER FNAME*64, TEXT*(*)
     	CALL IFANSI_TEXT('_Filename: ', 11, 22, 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
     	XYPLOT_NEXT = NCHS
     	RETURN
999	XYPLOT_NEXT = 0
     	RETURN
     	END

