     	SUBROUTINE MAPLOT_MENU( ICODE )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY, FEB-1985
C
C  	INTERACTIVE DIALOGUE MENU FOR CONTOUR PLOT.
C	RETURN WITH "ICODE=1" IF QUITING.
C
    	INCLUDE		'MAPLOT.CBS'
     	PARAMETER	NKS = 35
     	CHARACTER	KWD(NKS)*2
    	CHARACTER	TEXT*80, KEY*2, SYM*1
     	EQUIVALENCE	(SYM,TEXT(3:3)), (KEY,TEXT(1:2))
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT
C
C  ACCESS 2 LETTER CODES TO GRAPH PARAMETERS...
C
     	DATA KWD / 'F1', 'F2', 'F3', 'F4', 'GT', 'L1', 'L2', 'GO',
     + 'SF', 'SE', 'C1', 'LM', 'LB', 'LD', 'LP', 'T1', 'T2', 'X1',
     + 'X2', 'S1', 'S2', 'PF', 'LF', 'BF', 'FF', 'CF', 'MF', 'GF',
     + 'PS', 'LS', 'BS', 'FS', 'CS', 'MS', 'GS' /
C
C  PRESENT THE DIALOGUE MENU...
C
     	CALL MAPLOT_FORM
     	IF( NCOL.EQ.0 ) CALL MAPLOT_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('MAPLOT> ', 8, 22, 1 )
     	CALL IFANSI_CLEAR(4)
     	IKEY = IFQIOW( NCHS, TEXT )
     	IF( IKEY.EQ.26) GOTO 888
     	IF( NCHS.EQ.0 ) GOTO 999
     	NCHS = IFMISC_STRIP(TEXT)
     	CALL   IFMISC_UPPER(KEY)
     	IF( KEY.EQ.'QU' ) GOTO 888
     	IF( KEY.EQ.'EX' ) GOTO 999
     	CALL MAPLOT_CLEAR_OOPS
C
C  COMMAND INPUT...
C
     	IF( SYM.NE.'='  ) THEN
     	    CALL MAPLOT_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 MAPLOT_ERROR(1)	!INVALID KEYWORD
     	GOTO 111
C
C  YES, WE HAVE A VALID KEYWORD...
C
222	IF( NCOL.EQ.0 .AND. IKEY.NE.1 ) GOTO 777
     	TEXT = TEXT(4:)
     	NCHS = NCHS-3
     	IF( IKEY.LT.5 ) THEN
     	    CALL MAPLOT_FILES
     	    GOTO 111
     	ELSE IF( IKEY.LT.8 ) THEN
     	    IKEY = IKEY-4
     	    TITL(IKEY) = TEXT
     	    CALL MAPLOT_PARS_ALL(IKEY)
     	ELSE
     	    IF( NCHS.EQ.0 ) GOTO 666
     	    IF( IKEY.LT.16 ) THEN
     		IKEY = IKEY-4
     		CALL MAPLOT_OVERALL
     	    ELSE IF( IKEY.LT.18 ) THEN
     		IKEY = IKEY-15
     		CALL MAPLOT_AXIS_A
     	    ELSE IF( IKEY.LT.20 ) THEN
     		IKEY = IKEY-17
     		CALL MAPLOT_AXIS_B
     	    ELSE IF( IKEY.LT.22 ) THEN
     		IKEY = IKEY-19
     		CALL MAPLOT_AXIS_C
     	    ELSE IF( IKEY.LT.29 ) THEN
     		IKEY = IKEY-21
     		CALL MAPLOT_MAP(1)
     	    ELSE
     		IF( ADDNAM.EQ.' ' ) GOTO 555
     		IKEY = IKEY-28
     		CALL MAPLOT_MAP(2)
     	    ENDIF
     	ENDIF
     	GOTO 111
C
C  ERROR TRANSFER...
C
555	CALL IFANSI_VIDEO(70)
     	CALL MAPLOT_OOPS(' No 2nd map file to work on.')
     	GOTO 111
666	CALL IFANSI_VIDEO(70)
     	CALL MAPLOT_OOPS(' Missing input parameter.')
     	GOTO 111
777	CALL MAPLOT_ERROR(6)	!NO DATA YET TO WORK ON
     	GOTO 111
888	ICODE = 1		!QUIT FROM THE DIALOGUE
     	RETURN
999	IF( NCOL.EQ.0 ) GOTO 888
     	CALL MAPLOT_SETUP( 0, PARNAM )
     	ICODE = 0
     	RETURN
    	END

     	SUBROUTINE MAPLOT_FORM
C
C	THIS ROUTINE LAYOUTS THE DIALOGUE MENU FOR CONTOUR PLOT.
C
    	INCLUDE 'MAPLOT.CBS'
C
C  LAYOUT THE MENU...
C
     	CALL IFINIT_LINE(24)
     	CALL IFANSI_TITLE('GRAFIT: Contour Plot (max grid size:   x   )')
     	CALL IFANSI_VIDEO(60)
     	CALL IFANSI_IFL( MNG, 1, 3, 1, 37 )
     	CALL IFANSI_IFL( MNG, 1, 3, 1, 41 )

     	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=',38X,'L2=')
     	IF( CRT.NE.2 ) CALL IFANSI_VIDEO(90)
     	WRITE(6,1002)
1002	FORMAT(' GO=',14X,'SF=',13X,'SE=',5X,'C1=',3X,'LM=',3X,
     + 'LB=',3X,'LD=',3X,'LP=')

     	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('     DV dv DG PT      LENG  TOFF  XOFF  ROTA',
     +'        LOW        HIGH       STEP   ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,2002)
2002	FORMAT(' T1=',13X,'X1=',25X,'S1=')
     	IF( CRT.NE.2 ) CALL IFANSI_VIDEO(90)
     	WRITE(6,2003)
2003	FORMAT(' T2=',13X,'X2=',25X,'S2=')

     	IF( CRT.EQ.2 ) THEN
     	    CALL IFANSI_VIDEO(50)
     	ELSE
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_VIDEO(90)
     	ENDIF
     	WRITE(6,3000)
3000	FORMAT('     * * *   F I R S T   M A P   D R A'
     +,' W I N G   P A R A M E T E R S   * * *     ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,3001)
3001	FORMAT(' PF=',16X,'LF=',16X,'BF=',15X,'FF='/' CF=')
     	IF( CRT.NE.2 ) CALL IFANSI_VIDEO(90)
     	WRITE(6,3002)
3002	FORMAT(' MF=',16X,'GF=',10X,'NC:',6X,'NR:',6X,'ZL:',13X,'ZH:')

     	IF( CRT.EQ.2 ) THEN
     	    CALL IFANSI_VIDEO(50)
     	ELSE
     	    CALL IFANSI_VIDEO(47)
     	    CALL IFANSI_VIDEO(90)
     	ENDIF
     	WRITE(6,4000)
4000	FORMAT('    * * *   S E C O N D   M A P   D R A'
     +,' W I N G   P A R A M E T E R S   * * *    ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,4001)
4001	FORMAT(' PS=',16X,'LS=',16X,'BS=',15X,'FS='/' CS=')
     	IF( CRT.NE.2 ) CALL IFANSI_VIDEO(90)
     	WRITE(6,4002)
4002	FORMAT(' MS=',16X,'GS=',10X,'NC:',6X,'NR:',6X,'ZL:',13X,'ZH:')

     	IF( CRT.EQ.2 ) THEN
     	    CALL IFANSI_VIDEO(50)
     	ELSE
     	    CALL IFANSI_VIDEO(90)
     	ENDIF
     	WRITE(6,5000)
5000	FORMAT('     * * * * * * * *   I N P U T  &  O U T P U T'
     +,'   F I L E S   * * * * * * *     ')
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,5001)
5001	FORMAT(' F1=',17X,'F2=',17X,'F3=',17X,'F4=')
     	CALL IFANSI_VIDEO(90)
     	WRITE(6,5002)
5002	FORMAT(X,80(' '))
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,6000)
6000	FORMAT(' oops:'//' Auxiliary: HELP/PF2, DCL/PF4, REFRESH/PF1,',
     +' RESET/PF3, SAVE, READ, PLOT,'/' _commands: EXIT, QUIT,',
     +' OVERLAY/INCLUDE, SETUP, LAYOUT, ...')
C
C  INSERT THE PARAMETERS...
C
     	CALL MAPLOT_PARS
     	RETURN
     	END

     	SUBROUTINE MAPLOT_PARS
C
C	THIS ROUTINE INSERTS ALL GRAPH PARAMETERS.
C
     	CALL MAPLOT_PARS_ALL(0)		!OVERALL PLOT PARAMETER GROUP
     	CALL MAPLOT_PARS_AXIS(0,0)	!AXIS DRAWING PARAMETER GROUP
     	CALL MAPLOT_PARS_MAP(1,0)	!1ST MAP DRAWING PARAMETERS
     	CALL MAPLOT_PARS_MAP(2,0)	!2ND MAP DRAWING PARAMETERS
     	CALL MAPLOT_PARS_FILE(0)	!I/O FILE GROUP

     	RETURN
    	END

     	SUBROUTINE MAPLOT_PARS_ALL( ICODE )
C
C	THIS ROUTINE DISPLAYS OVERALL PLOT PARAMETERS.
C
     	INCLUDE 'MAPLOT.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), 36, 4, 4 )
    	IF(I.EQ.0.OR.I.EQ.3) CALL IFANSI_TEXT( TITL(3), 36, 4, 45)
    	IF(I.EQ.0.OR.I.EQ.4) THEN
     	   RP(1) = XORG
     	   RP(2) = YORG
     	   CALL IFANSI_FFL( RP, 2, 6.2, 5, 4 )
     	ENDIF
     	IF(I.EQ.0.OR.I.EQ.5) THEN
     	   RP(1) = XSCL
     	   RP(2) = YSCL
    	   CALL IFANSI_FFL( RP, 2, 6.2, 5, 21)
     	ENDIF
    	IF(I.EQ.0.OR.I.EQ.6) THEN
     	   YES = 'YES'
     	   IF( .NOT.INIT ) YES = 'NO'
     	   CALL IFANSI_TEXT( YES, 3, 5, 37 )
     	ENDIF
    	IF(I.EQ.0.OR.I.EQ.7) CALL IFANSI_IFL( IAXS, 1, 2, 5, 45 )
    	IF(I.EQ.0.OR.I.EQ.8) CALL IFANSI_IFL( MARK, 1, 2, 5, 51 )
    	IF(I.EQ.0.OR.I.EQ.9) CALL IFANSI_IFL( DRAW, 1, 2, 5, 57 )
    	IF(I.EQ.0.OR.I.EQ.10) CALL IFANSI_IFL( LDIG, 1, 2, 5, 63 )
    	IF(I.EQ.0.OR.I.EQ.11) THEN
     	   RP(1) = XLPS
     	   RP(2) = YLPS
     	   CALL IFANSI_FFL( RP, 2, 6.2, 5, 69 )
     	ENDIF

     	RETURN
     	END

     	SUBROUTINE MAPLOT_PARS_AXIS( ICODE, JCODE )
C
C	THIS ROUTINE DISPLAYS AXIS DRAWING PARAMETERS.
C
     	INCLUDE  'MAPLOT.CBS'
     	DIMENSION KP(4), RP(4)

     	CALL IFANSI_VIDEO(30)
     	I1 = 1
     	I2 = 2
     	IF( ICODE.GT.0 ) THEN
     	    I1 = ICODE
     	    I2 = I1
     	ENDIF
     	J = JCODE
     	DO  I=I1, I2
     	    IF( J.EQ.0 .OR. J.EQ.1 ) THEN
     		KP(1) = NTKS(I)
     		KP(2) = MTKS(I)
     		KP(3) = NDGS(I)
     		KP(4) = IGDS(I)
     		CALL IFANSI_IFL( KP, 4, 3, 7+I, 4  )
     	    ENDIF
     	    IF( J.EQ.0 .OR. J.EQ.2 ) THEN
     		RP(1) = XLNS(I)
     		RP(2) = OFFS(I)
     		RP(3) = SOFF(I)
     		RP(4) = XANG(I)
     		CALL IFANSI_FFL( RP, 4, 6.2, 7+I, 20 )
     	    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, 7+I, 48 )
     	    ENDIF
     	ENDDO

     	RETURN
     	END

	SUBROUTINE MAPLOT_PARS_MAP( IMAP, ICODE )
C
C	THIS ROUTINE DISPLAYS THE MAP DRAWING PARAMETERS.
C
    	INCLUDE 'MAPLOT.CBS'

     	CALL IFANSI_VIDEO(30)
     	J = IMAP
     	I = ICODE
     	K = 11
     	IF( J.EQ.2 ) K = 15
    	IF(I.EQ.0.OR.I.EQ.1) CALL IFANSI_IFL( CPEN(1,J), 7,2,K,4 )
    	IF(I.EQ.0.OR.I.EQ.2) CALL IFANSI_IFL( LINE(1,J), 7,2,K,23)
    	IF(I.EQ.0.OR.I.EQ.3) CALL IFANSI_IFL( BOLD(1,J), 7,2,K,42)
    	IF(I.EQ.0.OR.I.EQ.4) CALL IFANSI_IFL( FILL(1,J), 7,3,K,60)
     	IF(I.EQ.0.OR.I.EQ.5) CALL IFANSI_GFL( CLEV(1,J), 7,11.4,K+1,4)
     	IF(I.EQ.0.OR.I.EQ.6) CALL IFANSI_GFL( CMAP(J), 1,11.4,K+2,4 )
     	IF(I.EQ.0.OR.I.EQ.7) CALL IFANSI_IFL( NEXP(J), 1,2,K+2,23)
     	IF(I.EQ.0) THEN
     	   CALL IFANSI_VIDEO(60)
     	   CALL IFANSI_IFL( NCOL, 1, 4, K+2, 36 )
     	   CALL IFANSI_IFL( NROW, 1, 4, K+2, 45 )
     	   CALL IFANSI_GFL( ZBND(1,J), 1, 11.4, K+2, 54 )
     	   CALL IFANSI_GFL( ZBND(2,J), 1, 11.4, K+2, 70 )
     	ENDIF

     	RETURN
     	END

     	SUBROUTINE MAPLOT_PARS_FILE( ICODE )
C
C	THIS ROUTINE DISPLAYS FILENAMES.
C
     	INCLUDE 'MAPLOT.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 MAPLOT_COM
C
C	THIS ROUTINE IMPLEMENTS SOME DIALOGUE COMMANDS.
C
C	LIST OF COMMANDS:
C
C	HELP   : display help information.
C	REFRESH: re-layout the dialogue menu.
C	RESET  : reset graph parameters.
C	SAVE   : save graph parameters.
C	READ   : read the 1st map file.
C	PLOT   : read and plot the 1st map file.
C	OVERLAY/INCLUDE: read the 2nd map file.
C	SETUP  : use another setup/parameter file.
C	LAYOUT : use another layout file.
C	DCL    : spawn a system subprocess.
C
     	INCLUDE    'MAPLOT.CBS'
    	CHARACTER  TEXT*80
     	COMMON/MAPLOT_MENU/ 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 MAPLOT_FILES
     		RETURN
     	    ENDIF
     	ENDIF
     	IF( TEXT(1:4).EQ.'HELP'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OQ') ) THEN
C
C  HELP...
     	    CALL MAPLOT_MENU_HELP
     	ELSE IF( TEXT(1:3).EQ.'REF'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OP') ) THEN
C
C  REFRESH...
     	    CALL MAPLOT_FORM
     	ELSE IF( TEXT(1:5).EQ.'RESET'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OR') ) THEN
C
C  RESET...
     	    IF( NCOL.EQ.0 ) GOTO 888
     	    CALL MAPLOT_RESET
     	ELSE IF( TEXT(1:4).EQ.'SAVE' ) THEN
C
C  SAVE...
     	    IF( NCOL.EQ.0 ) GOTO 888
     	    CALL MAPLOT_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 MAPLOT_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 MAPLOT_FILES
     	ELSE IF( TEXT(1:5).EQ.'SETUP' ) THEN
C
C  SETUP...
     	    IKEY = 3
     	    NCHS = IFMISC_NEXT(TEXT)
     	    CALL MAPLOT_FILES
     	ELSE IF( TEXT(1:6).EQ.'LAYOUT' ) THEN
C
C  LAYOUT...
     	    IKEY = 4
     	    NCHS = IFMISC_NEXT(TEXT)
     	    CALL MAPLOT_FILES
     	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 MAPLOT_FORM
     	ELSE
     	    IF( IKEY.NE.27 ) THEN
     		CALL MAPLOT_ERROR(2)	!UNDEFINED INPUT
     	    ELSE
     		CALL IFANSI_VIDEO(70)
     		CALL MAPLOT_OOPS(' Undefined function key.')
     	    ENDIF
     	ENDIF
     	RETURN
888	CALL MAPLOT_ERROR(6)	!NO DATA YET TO WORK ON
     	RETURN
     	END

     	SUBROUTINE MAPLOT_RESET
C
C	THIS ROUTINE RESETS THE PLOT.
C
     	CALL MAPLOT_CLEAR_OOPS
     	CALL MAPLOT_OOPS(' Resetting the plot...')
     	CALL MAPLOT_INIT(1)
     	DO I=1, 4
     	   CALL MAPLOT_SCALE(I)
     	ENDDO
     	CALL MAPLOT_PARS
     	CALL MAPLOT_CLEAR_OOPS

     	RETURN
     	END

     	SUBROUTINE MAPLOT_FILES
C
C	THIS ROUTINE GETS INPUT DATA/PARAMETER/LAYOUT FILES.
C
     	INCLUDE		'MAPLOT.CBS'
     	CHARACTER	TERR*80, TEXT*80
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT

     	JKEY = IKEY
     	IKEY = IABS(IKEY)
     	IF( NCHS.EQ.0  ) THEN
     	    NCHS = MAPLOT_NEXT(TEXT)
     	    IF( NCHS.EQ.0 ) GOTO 999
     	ENDIF
     	JUNK = INDEX( TEXT, '/' )
     	IFL1 = 0
     	IFL2 = 0
     	IFL3 = 0
     	IF( JUNK.GT.0 ) THEN
     	    IF( NCOL.GT.0 ) IFL1 = INDEX( TEXT, 'NOINI' )
     	    IFL2 = INDEX( TEXT, 'NOSET' )
     	    IFL3 = INDEX( TEXT, 'NOLAY' )
     	    TEXT = TEXT(1:JUNK-1)
     	ENDIF
     	IF( NCHS.GT.32 ) THEN
     	    CALL MAPLOT_ERROR(9)
     	    GOTO 999
     	ENDIF
     	CALL IFMISC_UPPER(TEXT)
     	IF( IKEY.NE.1 ) GOTO 222
C
C  START A NEW PLOT WITH A NEW MAP FILE...
C
     	IKEY = MAPLOT_DATA( 1, TEXT )
     	IF( IKEY.GT.0 ) GOTO 888
     	ADDNAM = ' '
     	DATNAM = TEXT
     	PARNAM = DATNAM
     	CALL IFMISC_NAMEXT( PARNAM, '.PAR' )
     	DESNAM = ' '
     	IF( IFL3.EQ.0 ) THEN
     	    DESNAM = DATNAM
     	    CALL IFMISC_NAMEXT( DESNAM, '.DES' )
     	ENDIF
     	IF( IFL1.EQ.0 ) CALL MAPLOT_INIT(1)
     	IKEY = 0
     	IF( IFL2.EQ.0 ) IKEY = MAPLOT_SETUP( 1, PARNAM )
     	IF( IKEY.EQ.0 .AND. IFL1.EQ.0 ) THEN
     	    DO  I=1, 3
     		CALL MAPLOT_SCALE(I)
     	    ENDDO
     	ENDIF
     	IF( JKEY.GT.0 .AND. (IFL1.EQ.0.OR.IFL2.EQ.0) ) THEN
     	    CALL IFANSI_VIDEO(30)
     	    CALL MAPLOT_PARS
     	ELSE
     	    IKEY = JKEY
     	ENDIF
     	GOTO 999

222	IF( IKEY.NE.2 ) GOTO 333
C
C  OVERLAY A MAP FILE...
C
     	IKEY = MAPLOT_DATA( 2, TEXT )
     	IF( IKEY.GT.0 ) GOTO 888
     	ADDNAM = TEXT
     	CALL MAPLOT_SCALE(4)
     	CALL MAPLOT_PARS_MAP(2,0)
     	CALL MAPLOT_PARS_FILE(2)
     	GOTO 999

333	IF( IKEY.NE.3 ) GOTO 444
C
C  USE ANOTHER SETUP FILE ...
C
     	CALL IFMISC_DEFEXT( TEXT, '.PAR' )
     	IKEY = MAPLOT_SETUP( 1, TEXT )
     	IF( IKEY.EQ.0 ) THEN
     	    CALL IFANSI_VIDEO(70)
     	    TERR = ' can not find setup file'
     	    TERR(26:) = TEXT
     	    CALL MAPLOT_OOPS( TERR )
     	ELSE
     	    CALL IFANSI_VIDEO(30)
     	    CALL MAPLOT_PARS
     	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 MAPLOT_PARS_FILE(4)
     	GOTO 999
C
C  ERROR FROM READING...
C
888	CALL IFANSI_VIDEO(70)
     	IF(IKEY.EQ.1) CALL MAPLOT_ERROR(5)
     	IF(IKEY.EQ.2) CALL MAPLOT_OOPS(' No data found in the file.')
     	IF(IKEY.EQ.3) CALL MAPLOT_OOPS(' Invalid data format
     + in the input file.')
     	IF(IKEY.EQ.4) CALL MAPLOT_OOPS(' Map grid size overflows.')
     	IF(IKEY.EQ.5) CALL MAPLOT_OOPS(' 2nd map has different grid
     + size.')

999	RETURN
     	END

     	FUNCTION MAPLOT_DATA( MAP, FNAME )
C
C	THIS ROUTINE READS A MAP FILE.
C
C	MAP=1: READ THE 1ST MAP FILE.
C	MAP=2: READ THE 2ND MAP FILE.
C	FNAME: NAME  OF THE MAP FILE.
C
     	INCLUDE 'MAPLOT.CBS'
     	CHARACTER FNAME*(*)
     	BYTE      TEST(32)
     	LOGICAL   YES

    	NCHS = IFMISC_LENG( FNAME )
    	NDOT = INDEX( FNAME(1:NCHS), ']' )
    	NDOT = INDEX( FNAME(NDOT+1:NCHS), '.' )
     	IF( NDOT.GT.0 ) GOTO 9
     	IERR = 1
     	GOTO 2
C
C  OPEN FILE IN THE PRIORITY FROM *.MAP, *.DAT, *.RAW...
C
  1	CLOSE(13)
     	IF( NDOT.GT.0 ) GOTO 666
  2	IF( IERR.EQ.1 ) THEN
     	    FNAME(NCHS+1: ) = '.MAP'
     	    IERR = 2
     	ELSE IF( IERR.EQ.2 ) THEN
    	    FNAME(NCHS+1: ) = '.DAT'
    	    IERR = 3
     	ELSE IF( IERR.EQ.3 ) THEN
     	    FNAME(NCHS+1: ) = '.RAW'
    	    NDOT = 1
     	ENDIF

  9	INQUIRE( FILE=FNAME, EXIST=YES )
     	IF( .NOT.YES ) GOTO 1
C
C  READ THE FILE...
C
     	OPEN(13,FILE=FNAME,STATUS='OLD',FORM='FORMATTED',ERR=1)
     	READ(13,1000,ERR=888,END=777) TEST
1000	FORMAT(32A1)
     	DO I=1, 32
     	   IF( TEST(I).LT.7 ) GOTO 111		!UNFORMATTED FILE
     	ENDDO
     	REWIND (UNIT=13)
     	GOTO 222
111	CLOSE(13)
     	OPEN(13,FILE=FNAME,STATUS='OLD',FORM='UNFORMATTED',ERR=1)
     	YES = .FALSE.

222	IMAP = MAP
     	IF( YES ) THEN
     	    READ(13,2000,ERR=888,END=777) XL, XH, NC, YL, YH, NR
2000	    FORMAT(2G,I,2G,I)
     	ELSE
     	    READ(13,ERR=888,END=777) XL, XH, NC, YL, YH, NR
     	ENDIF
     	IF( NC.GT.MNG .OR. NR.GT.MNG ) GOTO 555
     	IF( IMAP.EQ.2 .AND. (NC.NE.NCOL.OR.NR.NE.NROW) ) GOTO 444
     	IF( YES ) THEN
     	READ(13,3000,ERR=888,END=777) ((ZMAP(I,J,IMAP),I=1,NC),J=1,NR)
3000	    FORMAT(<NC>G)
     	ELSE
     	    READ(13,ERR=888,END=777) ((ZMAP(I,J,IMAP),I=1,NC),J=1,NR)
     	ENDIF
      	IF( IMAP.EQ.1 ) THEN
     	    NCOL = NC
     	    NROW = NR
     	    XBND(1,1) = XL
     	    XBND(2,1) = XH
     	    XBND(3,1) = (XH-XL)/(NCOL-1)
     	    XBND(1,2) = YL
     	    XBND(2,2) = YH
     	    XBND(3,2) = (YH-YL)/(NROW-1)
     	ENDIF
     	YL = ZMAP(1,1,IMAP)
     	YH = YL
     	DO  J=1, NR
     	    DO  I=1, NC
     		XL = ZMAP(I,J,IMAP)
     		IF( XL.GT.YH ) THEN
     		    YH = XL
     		ELSE IF( XL.LT.YL ) THEN
     		    YL = XL
     		ENDIF
     	    ENDDO
     	ENDDO
     	ZBND(1,IMAP) = YL
     	ZBND(2,IMAP) = YH
     	IERR = 0
     	GOTO 999
C
C  ERROR FROM READING...
C
444	IERR = 5
     	GOTO 999
555	IERR = 4
     	GOTO 999
666	IERR = 1
     	GOTO 999
777	IERR = 2
     	GOTO 999
888	IERR = 3
999	MAPLOT_DATA = IERR
     	END

    	SUBROUTINE MAPLOT_INIT( ICODE )
C
C	THIS ROUTINE INITIALIZES THE COMMON BLOCKS FOR CONTOUR PLOT.
C
    	INCLUDE 'MAPLOT.CBS'

     	IF( ICODE.EQ.0 ) THEN
     	    CRT = IFINIT_ITM(0)		!GET THE TYPE OF TERMINAL
     	    NCOL = 0
     	    NROW = 0
     	    DATNAM = ' 1st map file'
     	    ADDNAM = ' 2nd map file'
     	    PARNAM = ' Setup file'
     	    DESNAM = ' Layout file'
    	    DO I=1, 3
     	       TITL(I) = ' '
    	    ENDDO
     	    DO  I=1, 2
     		DO  J=1, 2
     		    XBND(J,I) = 0.0
     		    ZBND(J,I) = 0.0
     		ENDDO
     	    ENDDO
     	    INIT = .TRUE.
	    PLOTID = ' '
     	ENDIF

     	DO I=1, 2
     	   NEXP(I) = 0
     	   CMAP(I) = 1.0
     	   DO J=1, 7
     	      CLEV(J,I) = 0.0
     	      CPEN(J,I) = J
     	      LINE(J,I) = 1
     	      BOLD(J,I) = 1
     	      FILL(J,I) = 0
     	   ENDDO
    	   NTKS(I) = 2
    	   MTKS(I) =-1
    	   NDGS(I) = 0
    	   IGDS(I) = 11
     	   XLNS(I) = 4.5
    	   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

     	DRAW = 2
     	MARK = 1
     	LDIG = 3
    	IAXS = 1
     	XLPS = 5.0
     	YLPS = 0.5
     	XORG = 2.0
     	YORG = 2.0
     	XSCL = 1.00
     	YSCL = 1.00

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

     	IF( IKEY.EQ.4 ) 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.5 ) 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.6 ) THEN		!SCREEN ERASE FLAG
     	    INIT = .TRUE.
     	    IF( TEXT(1:1).EQ.'N' .OR. TEXT(1:1).EQ.'n'
     +     .OR. TEXT(1:1).EQ.'F' .OR. TEXT(1:1).EQ.'f' ) INIT=.FALSE.
     	    GOTO 888
     	ENDIF

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

     	IF( IKEY.EQ.8 ) THEN		!MARK MIN AND MAX POINTS.
     	    IF(RP(1).LT.0.0.OR.RP(1).GT.1.0) GOTO 777
     	    MARK = RP(1)
     	    GOTO 888
     	ENDIF

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

     	IF( IKEY.EQ.10 ) THEN		!CONTOUR LEVEL LABEL DIGITS
     	    IF(RP(1).LT.1.0.OR.RP(1).GT.5.0) GOTO 777
     	    LDIG = RP(1)
     	    GOTO 888
     	ENDIF

     	IF( IKEY.EQ.11 ) 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

777	CALL MAPLOT_ERROR(4)
     	RETURN
888	CALL MAPLOT_PARS_ALL(IKEY)
     	RETURN
	END

	SUBROUTINE MAPLOT_AXIS_A
C
C	THIS ROUTINE CHANGES AXIS DRAWING PARAMETERS -- GROUP A
C
    	INCLUDE		'MAPLOT.CBS'
     	CHARACTER	TEXT*80
     	DIMENSION	KP(4), RP(4)
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT
C
C  DECODE THE PARAMETERS...
C
    	J = IFMISC_DECODE( TEXT, 4, RP, KP )
    	IF( J.EQ.0 ) THEN
     	    CALL MAPLOT_ERROR(3)	!ERROR IN INPUT
     	    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.99 ) GOTO 777
     	    NTKS(IKEY) = J
     	ENDIF

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

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

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

     	CALL MAPLOT_PARS_AXIS(IKEY,1)
     	IF( KP(1).EQ.1 ) THEN
     	    XINC(IKEY) = (XHIG(IKEY)-XLOW(IKEY)) / IABS(NTKS(IKEY))
     	    CALL MAPLOT_PARS_AXIS(IKEY,3)
     	ENDIF
     	GOTO 999

777	CALL MAPLOT_ERROR(4)	!PARAMETER OUT OF RANGE

999	RETURN
     	END

	SUBROUTINE MAPLOT_AXIS_B
C
C	THIS ROUTINE CHANGES AXIS DRAWING PARAMETERS -- GROUP B
C
    	INCLUDE		'MAPLOT.CBS'
     	CHARACTER	TEXT*80
     	DIMENSION	KP(4), RP(4)
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT
C
C  DECODE THE PARAMETERS...
C
    	J = IFMISC_DECODE( TEXT, 4, RP, KP )
    	IF( J.EQ.0 ) THEN
     	    CALL MAPLOT_ERROR(3)	!ERROR IN INPUT
     	    GOTO 999
     	ENDIF
C
C  STORE THE NEW PARAMETERS...
C
     	IF( KP(1).EQ.1 ) THEN
     	    P = RP(1)
     	    IF( ABS(P).LT.2.0 .OR. ABS(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.ABS(XLNS(IKEY))/3.0 ) GOTO 777
     	    OFFS(IKEY) = P
     	ENDIF

     	IF( KP(3).EQ.1 ) THEN
     	    P = RP(3)
     	    IF( ABS(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 MAPLOT_PARS_AXIS(IKEY,2)
     	GOTO 999

777	CALL MAPLOT_ERROR(4)	!PARAMETER OUT OF RANGE

999	RETURN
     	END

	SUBROUTINE MAPLOT_AXIS_C
C
C	THIS ROUTINE CHANGES AXIS DRAWING PARAMETERS -- GROUP C
C
    	INCLUDE		'MAPLOT.CBS'
     	CHARACTER	TEXT*80
     	DIMENSION	KP(3), RP(3)
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT
C
C  DECODE THE PARAMETERS...
C
    	J = IFMISC_DECODE( TEXT, 3, RP, KP )
    	IF( J.EQ.0 ) THEN
     	    CALL MAPLOT_ERROR(3)	!ERROR IN INPUT
     	    GOTO 999
     	ENDIF
C
C  STORE THE NEW PARAMETERS...
C
     	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( RP(2).LT.RP(1) ) THEN
     	    P = RP(2)
     	    RP(2) = RP(1)
     	    RP(1) = P
     	ENDIF
     	P = RP(1)
     	X1= XBND(1,IKEY)
     	X2= XBND(2,IKEY)
     	X3= XBND(3,IKEY)
     	IF( P.GT.(X2-X3) ) THEN
     	    P = X2 - X3
     	ELSE IF( P.LT.X1 ) THEN
     	    P = X1
     	ELSE
     	    P = X1 + X3*INT((P-X1)/X3)
     	ENDIF
     	P = RP(2)
     	IF( P.LT.(X1+X3) ) THEN
     	    P = X1 + X3
     	ELSE IF( P.GT.X2 ) THEN
     	    P = X2
     	ELSE
     	    X2= X1 + X3*INT((P-X1)/X3)
     	    IF( X2.LT.P ) X2 = X2 + X3
     	    P = X2
     	ENDIF
     	IF( KP(3).EQ.0 ) THEN
     	    RP(3) = (RP(2)-RP(1))/IABS(NTKS(IKEY))
     	ELSE
     	    IF( RP(3).EQ.0.0 ) GOTO 777
     	    J = (RP(2)-RP(1))/RP(3) + 0.1
     	    IF( J.LT.1 .OR. J.GT.99 ) GOTO 888
     	    NTKS(IKEY) = J
     	    RP(3) = (RP(2)-RP(1))/J
     	    CALL MAPLOT_PARS_AXIS(IKEY,1)
     	ENDIF
     	XLOW(IKEY) = RP(1)
     	XHIG(IKEY) = RP(2)
     	XINC(IKEY) = RP(3)
     	CALL MAPLOT_PARS_AXIS(IKEY,3)
     	GOTO 999

777	CALL MAPLOT_ERROR(3)
     	GOTO 999
888	CALL MAPLOT_ERROR(4)

999	RETURN
	END

     	SUBROUTINE MAPLOT_MAP( MAP )
C
C	THIS ROUTINE CHANGES MAP DRAWING PARAMETERS.
C
    	INCLUDE		'MAPLOT.CBS'
     	CHARACTER	TEXT*80
     	DIMENSION	KP(7), RP(7)
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT
     	LOGICAL		REPEAT
C
C  DECODE THE PARAMETERS...
C
     	REPEAT = .FALSE.
     	IF( IKEY.LT.6 .AND. (TEXT(NCHS:NCHS).EQ.'R'
     + .OR. TEXT(NCHS:NCHS).EQ.'r') ) THEN
     	    REPEAT = .TRUE.
     	    NCHS = NCHS - 1
     	ENDIF
    	J = IFMISC_DECODE( TEXT(1:NCHS), 7, RP, KP )
    	IF( J.EQ.0 ) THEN
     	    CALL MAPLOT_ERROR(3)	!ERROR IN INPUT
     	    GOTO 999
     	ENDIF
C
C  REPEAT ENTRIES FOR THE REST OF PARAMETERS ...
C
	IF( REPEAT ) THEN
    	    DO I=1, 7
    	       IF( KP(I).EQ.0 ) GOTO 111
    	    ENDDO
111	    NCHS = I - 1
    	    DO J=I, 7
    	       KP(J) = 1
    	       RP(J) = RP(J-NCHS)
    	    ENDDO
    	ENDIF
C
C  STORE THE NEW PARAMETERS...
C
     	JMAP = MAP
     	IF( IKEY.EQ.1 ) THEN
     	    DO  I=1, 7
     		IF( KP(I).EQ.1 ) THEN
     		    J = RP(I)
     		    IF( J.LT.0 .OR. J.GT.7 ) GOTO 777
     		    CPEN(I,JMAP) = J
     		ENDIF
     	    ENDDO
     	    GOTO 555
     	ENDIF

     	IF( IKEY.EQ.2 ) THEN
     	    DO  I=1, 7
     		IF( KP(I).EQ.1 ) THEN
     		    J = RP(I)
     		    IF( IABS(J).GT.8 ) GOTO 777
     		    LINE(I,JMAP) = J
     		ENDIF
     	    ENDDO
     	    GOTO 555
     	ENDIF

     	IF( IKEY.EQ.3 ) THEN
     	    DO  I=1, 7
     		IF( KP(I).EQ.1 ) THEN
     		    J = RP(I)
     		    IF( J.LT.1 .OR. J.GT.9 ) GOTO 777
     		    BOLD(I,JMAP) = J
     		ENDIF
     	    ENDDO
     	    GOTO 555
     	ENDIF

     	IF( IKEY.EQ.4 ) THEN
     	    DO  I=1, 7
     		IF( KP(I).EQ.1 ) THEN
     		    J = RP(I)
     		    IF( IABS(J).GT.99 ) GOTO 777
     		    FILL(I,JMAP) = J
     		ENDIF
     	    ENDDO
     	    GOTO 555
     	ENDIF

     	IF( IKEY.EQ.5 ) THEN
     	    DO  I=1, 7
     		IF( KP(I).EQ.1 ) THEN
     		    P = RP(I)
     		    IF(P.LE.ZBND(1,JMAP).OR.P.GE.ZBND(2,JMAP)) GOTO 777
     		    CLEV(I,JMAP) = P
     		ENDIF
     	    ENDDO
     	    GOTO 555
     	ENDIF

     	IF( IKEY.EQ.6 ) THEN
     	    IF( RP(1).NE.0.0 ) CMAP(JMAP) = RP(1)
     	    GOTO 555
     	ENDIF

     	IF( IKEY.EQ.7 ) THEN
     	    J = RP(1)
     	    IF( J.LT.0 .OR. J.GT.9 ) GOTO 777
     	    IF( J*NROW.GT.MNG .OR. J*NCOL.GT.MNG ) GOTO 777
     	    NEXP(JMAP) = J
     	    GOTO 555
     	ENDIF
     	GOTO 777
555	CALL MAPLOT_PARS_MAP(JMAP,IKEY)
     	GOTO 999
777	CALL MAPLOT_ERROR(4)
999	RETURN
     	END
     	
     	SUBROUTINE MAPLOT_SAVE
C
C	THIS ROUTINE SAVES CURRENT PLOT SETUP IN A PARAMETER FILE.
C
     	INCLUDE		'MAPLOT.CBS'
    	CHARACTER	TEXT*80, STRN*75
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT

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

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

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

999	RETURN
     	END

     	SUBROUTINE MAPLOT_OOPS( TEXT )
     	CHARACTER TEXT*(*)
     	NCHS = IFMISC_LENG(TEXT)
     	CALL MAPLOT_CLEAR_OOPS
     	CALL IFANSI_TEXT( TEXT, NCHS, 21, 6 )
     	RETURN
     	END

     	SUBROUTINE MAPLOT_CLEAR_OOPS
     	CALL IFANSI_CURSOR(21,6)
     	CALL IFANSI_CLEAR(4)
     	RETURN
     	END

    	SUBROUTINE MAPLOT_GIN( Z1, Z2 )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY, JUNE-1986
C
C	THIS ROUTINE IMPLEMENTS GRAPHICS INPUT -- GIN FOR CONTOUR PLOT.
C
C	Z1,Z2: return the coordinates of last two cursor positions.
C
    	INCLUDE   'MAPLOT.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.')
     	    RETURN
     	ENDIF
C
C  GET THE SCALE UNITS...
C
     	CALL MAPLOT_LIMIT( XDL, YDL, XLO, XHI, YLO, YHI )
     	IF( XDL.LT.0.0 ) XLO = XHI
     	IF( YDL.LT.0.0 ) YLO = YHI
     	XLN = ABS(XLNS(1))
     	YLN = ABS(XLNS(2))
     	XXL = XLO
     	XXC = XXL
     	YYL = YLO
     	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(' MAPLOT_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
     	YHI = YLO + YPS*YDL
     	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
     	RETURN
    	END

    	SUBROUTINE MAPLOT_SCALE( IFLAG )
C
C  PROGRAMMER: R.S. ZHOU,  ALFRED UNIVERSITY
C
C  	THIS ROUTINE SCALES MAPS FOR CONTOUR PLOT.
C
C	IFLAG=1: SCALE THE X-AXIS.
C	IFLAG=2: SCALE THE Y-AXIS.
C	IFLAG=3: SCALE THE Z-AXIS OF THE 1ST MAP.
C	IFLAG=4: SCALE THE Z-AXIS OF THE 2ND MAP.
C
    	INCLUDE 'MAPLOT.CBS'

     	IS = IFLAG
     	IF( IS.LT.3 ) THEN
     	    NTKS(IS) = 2
     	    MTKS(IS) =-4
     	    XLOW(IS) = XBND(1,IS)
     	    XHIG(IS) = XBND(2,IS)
     	    XINC(IS) = (XBND(2,IS)-XBND(1,IS))/2.0
     	ELSE IF( IS.EQ.3 ) THEN
     	    STEP = (ZBND(2,1)-ZBND(1,1))/8.0
     	    DO  I=1, 7
     		CLEV(I,1) = CMAP(1)*(ZBND(1,1)+STEP*I)
     	    ENDDO
     	ELSE IF( ADDNAM.NE.' ' ) THEN
     	    STEP = (ZBND(2,2)-ZBND(1,2))/8.0
     	    DO  I=1, 7
     		CLEV(I,2) = CMAP(2)*(ZBND(1,2)+STEP*I)
     	    ENDDO
     	ENDIF
     	RETURN
     	END

    	SUBROUTINE MAPLOT_LIMIT( XDL,YDL, XLO,XHI, YLO,YHI )
C
C	THIS ROUTINE CALCULATES SCALE WINDOW OF A CONTOUR PLOT
C	BECAUSE OF POSSIBLE OFFSETS OF THE 1ST MAJOR TICKMARKS.
C
C	XDL,YDL: INCREMENTAL VALUES PER AXIS INCHES.
C	XLO,XHI: WINDOW LIMIT ON X-AXIS.
C	YLO,YHI: WINDOW LIMIT ON Y-AXIS.
C
     	INCLUDE 'MAPLOT.CBS'
C
C  CALCULATE THE X-WINDOW ...
C
     	XDL = (XHIG(1)-XLOW(1))/(ABS(XLNS(1))-2.0*OFFS(1))
     	XLO = XLOW(1) - XDL*OFFS(1)
     	XHI = XHIG(1) + XDL*OFFS(1)
    	IF( XLO.GT.XHI ) THEN
    	    XXX = XLO
    	    XLO = XHI
    	    XHI = XXX
    	ENDIF
C
C  CALCULATE THE Y-WINDOW ...
C
     	YDL = (XHIG(2)-XLOW(2))/(ABS(XLNS(2))-2.0*OFFS(2))
     	YLO = XLOW(2) - YDL*OFFS(2)
     	YHI = XHIG(2) + YDL*OFFS(2)
    	IF( YLO.GT.YHI ) THEN
    	    XXX = YLO
    	    YLO = YHI
    	    YHI = XXX
    	ENDIF

    	RETURN
    	END

    	FUNCTION MAPLOT_SETUP( IFLAG, FNAME )
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY
C
C	THIS ROUTINE WRITES/READS A PARAMETER/SETUP FILE FOR CONTOUR 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		'MAPLOT.CBS'
    	PARAMETER	NKS= 13, LUN= 13
    	CHARACTER	KWD(NKS)*4, KEY*4, REST*127, FNAME*(*)
    	DIMENSION	KP(30), RP(30), IRR(NKS)
C
C  PARAMETER GROUP KEYWORDS...
C
     	DATA KWD/ 'TITL', 'XLB1', 'YLB1', 'OVER', 'XAX1', 'YAX1',
     +    'MAP1', 'LEV1', 'MAP2', 'LEV2', 'PAR1', 'PAR2', 'IDEN' /
C
C  NUMBER OF NUMERIC PARAMETERS IN EACH SETUP GROUP...
C
     	DATA IRR/ 0, 0, 0, 9, 11, 11, 29, 8, 29, 8, 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,3)
     +, XORG, YORG, XSCL, YSCL, XLPS, YLPS, DRAW, MARK, IAXS
     +,(NTKS(I),MTKS(I),NDGS(I),IGDS(I), XLNS(I),OFFS(I),SOFF(I)
     +, XANG(I), XLOW(I),XHIG(I),XINC(I), I=1,2)
     +,(NEXP(J), (CPEN(I,J),I=1,7),(LINE(I,J),I=1,7),(BOLD(I,J),I=1,7)
     +,(FILL(I,J),I=1,7), CMAP(J), (CLEV(I,J),I=1,7), J=1,2)
     +,(HPAR(I), I=1, 20), PLOTID

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

    	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.4 ) THEN	!GRAPH TITLE AND AXIS LABELS
     	    TITL(I) = REST
     	    GOTO 111
     	ENDIF

     	IF( I.EQ.4 ) THEN	!OVERALL GRAPH PARAMETERS
     	    IF( KP(1).EQ.1 ) XORG = RP(1)
     	    IF( KP(2).EQ.1 ) YORG = RP(2)
     	    IF( KP(3).EQ.1 ) XSCL = RP(3)
     	    IF( KP(4).EQ.1 ) YSCL = RP(4)
     	    IF( KP(5).EQ.1 ) XLPS = RP(5)
     	    IF( KP(6).EQ.1 ) YLPS = RP(6)
     	    IF( KP(7).EQ.1 ) DRAW = RP(7)
     	    IF( KP(8).EQ.1 ) MARK = RP(8)
     	    IF( KP(9).EQ.1 ) IAXS = RP(9)
     	    GOTO 111
     	ENDIF

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

     	IF( I.EQ.7 .OR. I.EQ.9 ) THEN
     	    NCH = 1
     	    IF( I.EQ.9 ) NCH = 2
     	    NEXP(NCH) = RP(1)
     	    DO  I=1, 7
     		CPEN(I,NCH) = RP(I+1)
     		LINE(I,NCH) = RP(I+8)
     		BOLD(I,NCH) = RP(I+15)
     		FILL(I,NCH) = RP(I+22)
     	    ENDDO
     	    GOTO 111
     	ENDIF

     	IF( I.EQ.8 .OR. I.EQ.10 ) THEN
     	    NCH = 1
     	    IF( I.EQ.10 ) NCH = 2
     	    CMAP(NCH) = RP(1)
     	    DO  I=1, 7
     		CLEV(I,NCH) = RP(I+1)
     	    ENDDO
     	    GOTO 111
     	ENDIF

     	IF( I.EQ.11 ) THEN	!HARDCOPY SETUP PARAMETERS
     	    DO  I=1, 10
     		IF( KP(I).EQ.1 ) HPAR(I) = RP(I)
     	    ENDDO
     	    GOTO 111
     	ENDIF
     	IF( I.EQ.12 ) 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.13 ) THEN	!PLOT ID LABEL
     	    PLOTID = REST
     	    GOTO 111
     	ENDIF
     	GOTO 111
C
C  CHECK SCALE UNITS...
C
777	IF( XINC(1).EQ.0.0.OR.XLOW(1).EQ.XHIG(1) ) CALL MAPLOT_SCALE(1)
     	IF( XINC(2).EQ.0.0.OR.XLOW(2).EQ.XHIG(2) ) CALL MAPLOT_SCALE(2)
     	IF( CLEV(1,1).LT.ZBND(1,1) .OR. CLEV(7,1).GT.ZBND(2,1) )
     +  CALL MAPLOT_SCALE(3)
     	GOTO 999
888	MAPLOT_SETUP = 0
     	RETURN
999    	CLOSE(UNIT=LUN)
     	MAPLOT_SETUP = 1
    	RETURN
    	END

     	SUBROUTINE MAPLOT_MENU_HELP
C
C	THIS ROUTINE DISPLAYS ON-LINE HELP FOR CONTOUR PLOT.
C
     	CHARACTER TEXT*80, KEY*4
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT

     	NCHS = IFMISC_NEXT(TEXT)
     	IF( NCHS.EQ.2 ) THEN
     	    IKEY=IFMISC_HELP('GRAFIT$HLP:MAPLOT_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:MAPLOT_HELP.HLP',KEY,21)
     	    IF( IKEY.GT.0 ) GOTO 888
     	    CALL MAPLOT_FORM
     	ENDIF
     	RETURN
C
C  ERROR TRANSFER...
C
888	CALL IFANSI_VIDEO(70)
     	IF(IKEY.EQ.1) THEN
     	   CALL MAPLOT_OOPS(' HELP file is not available.')
     	ELSE
     	   CALL MAPLOT_OOPS(' no HELP found for the keyword.')
     	ENDIF
     	RETURN
     	END

     	FUNCTION MAPLOT_ZOOM()
C
C	THIS ROUTINE ZOOMS A CONTOUR PLOT.
C
     	INCLUDE 'MAPLOT.CBS'
     	DIMENSION Z1(2), Z2(2)
C
C  INVOKE MAPLOT_GIN...
C
     	CALL MAPLOT_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
     	    X1 = Z1(I)
     	    X2 = Z2(I)
     	    IF( X1.GT.X2 ) THEN
     		X3 = X1
     		X1 = X2
     		X2 = X3
     	    ENDIF
     	    X1 = XBND(1,I) + XBND(3,I)*INT((X1-XBND(1,I))/XBND(3,I))
     	    X3 = XBND(1,I) + XBND(3,I)*INT((X2-XBND(1,I))/XBND(3,I))
     	    IF( X3.LT.X2 ) X2 = X2 + XBND(3,I)
     	    X2 = X3
     	    NTKS(I) = 2
     	    XLOW(I) = X1
     	    XHIG(I) = X2
     	    XINC(I) = (X2-X1)/2
     	ENDDO
     	MAPLOT_ZOOM = 1
     	RETURN
999	MAPLOT_ZOOM = 0
     	RETURN
     	END

     	FUNCTION MAPLOT_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
     	MAPLOT_NEXT = NCHS
     	RETURN
999	MAPLOT_NEXT = 0
     	RETURN
     	END

