     	PROGRAM MAPLOT_MAIN
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY   JULY-1985
C
C	MAIN ROUTINE OF CONTOUR PLOT.
C
    	INCLUDE		'MAPLOT.CBS'
     	DIMENSION	RP(2)
C
C  INITIALIZE THE SESSION...
C
     	CALL IFINIT_SESSION
C
C  INITIALIZE PLOT PARAMETERS AND MEMORY ALLOCATION...
C
     	CALL MAPLOT_INIT( 0 )
C
C  CHECK DCL COMMAND LINE INPUT...
C
     	CALL MAPLOT_DCL( KEY )
     	IF( KEY.NE.0 ) GOTO 333
C
C  CONDUCT THE DIALOGUE...
C
  222	CALL MAPLOT_MENU( KEY )
     	IF( KEY.NE.0 ) THEN
     	    IF( .NOT.IFINIT_DIFF() ) GOTO 999
     	    GOTO 444
     	ENDIF
C
C  CARRY OUT THE PLOTTING...
C
  333	CALL MAPLOT_PLOT
     	IF( CRT.EQ.1 ) THEN
     	    CALL IFANSI_CURSOR(1,1)
     	    CALL IFANSI_CLEAR(6)
     	ENDIF
     	WRITE(6,334)
  334	FORMAT(' MAPLOT> Please press a desired function key:')
C
C  PRESENT LOCAL COMMAND MENU...
C
  444	CALL IFANSI_VIDEO(47)
     	WRITE(6,445)
  445	FORMAT(' MAPLOT> ?>Help 0>Exit 1>Hardcopy 2>Layout 3>Dialog',
     +' 4>Zoom 5>Cursor 6>Redraw ...')
     	CALL IFANSI_CURSOR(0,8)
     	CALL IFQIOW_KEY( KEY )

     	IF( KEY.EQ.26 .OR. KEY.EQ.48 .OR. KEY.EQ.69 ) GOTO 999
     	IF( KEY.EQ.49 .OR. KEY.EQ.72 ) THEN	!HARDCOPY WITH PAGESETUP
     	    CALL IFCOPY( HPAR(1), PLOTID )
     	    CALL MAPLOT_SETUP( 0, PARNAM )
     	    CALL IFINIT_LINE( -3 )
     	ELSE IF( KEY.EQ.-80 ) THEN	!HARDCOPY WITHOUT PAGE SETUP
     	    CALL IFANSI_CURSOR(0,14)
     	    CALL IFANSI_LEDS(1)
     	    CALL IFFEED( HPAR(1), PLOTID, ' ' )
     	    CALL IFANSI_LEDS(0)
     	ELSE IF( KEY.EQ.50 .OR. KEY.EQ.76 ) THEN	!LAYOUT
     	    IF( DESNAM.EQ.' ' ) THEN
     		DESNAM = DATNAM
     		CALL IFMISC_NAMEXT( DESNAM, '.DES' )
     	    ENDIF
     	    CALL LAYOUT_AUX( 2, XORG, YORG, DESNAM )
     	ELSE IF( KEY.EQ.51 .OR. KEY.EQ.-82 .OR. KEY.EQ.68 ) THEN
     	    MENU = .TRUE.
     	    GOTO 222				!DIALOGUE MENU
     	ELSE IF( KEY.EQ.13 ) THEN	!DIALOGUE WITHOUT THE MENU
     	    MENU = .FALSE.
     	    GOTO 222
     	ELSE IF( KEY.EQ.52 .OR. KEY.EQ.90 ) THEN	!ZOOM
     	    KEY = MAPLOT_ZOOM()
     	    IF( KEY.NE.0 ) GOTO 333
     	ELSE IF( KEY.EQ.53 .OR. KEY.EQ.67 .OR. KEY.EQ.71 ) THEN	!GIN
     	    CALL MAPLOT_GIN( RP, RP )
     	ELSE IF( KEY.EQ.54 .OR. KEY.EQ.82 ) THEN
     	    GOTO 333				!REDRAW THE PLOT
     	ELSE IF( KEY.EQ.32 .OR. KEY.EQ.63 .OR. KEY.EQ.-81 ) THEN
     	    CALL IFMISC_HLP( 'MAPLOT', '$ MENU', -2, 0 )
     	ELSE IF( KEY.EQ.36 .OR. KEY.EQ.-83 ) THEN	!SPAWN
     	    CALL IFMISC_SPAWN( '$', 0 )
     	    CALL IFINIT_LINE( -3 )
     	ELSE
     	    CALL IFMISC_OOPS( 5, 0, 0, ' ' )
     	ENDIF
    	GOTO 444
C
C  DEALLOCATE THE VIRTUAL MEMORY BEFORE EXIT...
C
  999	KEY = 4*MNGS*MNGS
     	CALL LIB$FREE_VM( KEY, MAPL(1) )
     	CALL LIB$FREE_VM( KEY, MAPL(2) )
     	CALL IFINIT_EXIT
     	CALL EXIT
	END

     	SUBROUTINE MAPLOT_DCL( ICODE )
C
C	THIS ROUTINE CHECKS THE INPUT COMMAND LINE.
C
     	INCLUDE 'MAPLOT.CBS'
C
C  IS THERE A FILE IN THE LINE...
C
     	ICODE = 0
     	CALL IF_DCL_NFILE( NFLS )
     	IF( NFLS.EQ.0 ) RETURN
C
C  GET MAP DATA FILE...
C
     	SLIN = -1
     	SCOL = 0
     	CALL IF_DCL_FILE( 1, ADDNAM )
     	IERR = MAPLOT_DATA( 1, ADDNAM, MNGS, %VAL(MAPL(1)) )
     	IF( IERR.GT.0 ) GOTO 999
     	DATNAM = ADDNAM
     	IF( NFLS.GT.1 ) THEN
     	    CALL IF_DCL_FILE( 2, ADDNAM )
     	    IERR = MAPLOT_DATA( 2, ADDNAM, MNGS, %VAL(MAPL(2)) )
     	    IF( IERR.GT.0 ) GOTO 999
     	ENDIF
     	CALL IF_DCL_SETUP( PARNAM )
     	IF( PARNAM.EQ.' ' ) THEN
     	    PARNAM = DATNAM
     	    CALL IFMISC_NAMEXT( PARNAM, '.PAR' )
     	ENDIF
     	CALL IF_DCL_LAYOUT( DESNAM )
     	IF( DESNAM.EQ.' ' ) THEN
     	    DESNAM = DATNAM
     	    CALL IFMISC_NAMEXT( DESNAM, '.DES' )
     	ENDIF
C
C  SCALE THE DATA...
C
     	CALL MAPLOT_INIT(1)
     	IERR = MAPLOT_SETUP( 1, PARNAM )
     	IF( IERR.EQ.0 ) THEN
     	    DO  I=1, 4
     		CALL MAPLOT_SCALE(I)
     	    ENDDO
     	ENDIF
     	ICODE = 1
C
C  MAKE THE HARDCOPY...
C
     	IF( IF_DCL_HARDCOPY() ) THEN
     	    CALL MAPLOT_PLOT
     	    CALL IFCOPY( HPAR(1), PLOTID )
     	ENDIF
     	GOTO 888
C
C  ERROR FROM READING A DATA_FILE...
C
999	IF( IERR.EQ.1 ) WRITE(6,1001) ADDNAM
     	IF( IERR.EQ.2 ) WRITE(6,1002) ADDNAM
     	IF( IERR.EQ.3 ) WRITE(6,1003) ADDNAM
     	IF( IERR.EQ.4 ) WRITE(6,1004) ADDNAM
     	IF( IERR.EQ.5 ) WRITE(6,1005) ADDNAM
1001	FORMAT(' %MAPLOT: can not open file ',A32)
1002	FORMAT(' %MAPLOT: no data found in file ',A32)
1003	FORMAT(' %MAPLOT: invalid data format in file ',A32)
1004	FORMAT(' %MAPLOT: map grid overflows in file ',A32)
1005	FORMAT(' %MAPLOT: map grid is different in file ',A32)
888	IF( .NOT.IF_DCL_HARDCOPY() ) RETURN
     	CALL EXIT
     	END

     	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
     	IF( MENU ) THEN
     	    SLIN = 21
     	    SCOL = 6
     	    CALL MAPLOT_FORM
     	    CALL IFANSI_VIDEO(47)
          CALL IFANSI_TEXT(' Type HELP (or press PF2) if you need any',
     +      42, SLIN, SCOL+10 )
     	ELSE
     	    CALL IFANSI_LEDS(3)
     	    SLIN = -1
     	    SCOL = 0
     	    WRITE(6,1)
  1	    FORMAT('_MAPLOT> You are in dialogue mode without',
     +' the dialogue menu. Proceed as usual.')
     	ENDIF
C
C  START THE DIALOGUE LOOP...
C
111	CALL IFANSI_VIDEO(47)
     	IF( MENU ) THEN
     	    CALL IFANSI_TEXT('MAPLOT> ', 8, 22, 1 )
     	    CALL IFANSI_CLEAR(4)
     	ELSE
     	    WRITE(6,112)
112	    FORMAT('$MAPLOT> ')
     	ENDIF
     	IKEY = IFQIOW( NCHS, TEXT )
     	IF( IKEY.EQ.26) GOTO 888
     	IF( NCHS.EQ.0 ) GOTO 111
     	NCHS = IFMISC_STRIP(TEXT)
     	CALL   IFMISC_UPPER(2,KEY)
     	IF( KEY.EQ.'QU' .OR. KEY.EQ.'EX' ) GOTO 888
     	IF( NCHS.EQ.2  .AND. KEY.EQ.'GO' ) GOTO 999
     	IF( MENU ) CALL MAPLOT_CLEAR_OOPS
C
C  COMMAND INPUT...
C
     	IF( SYM.NE.'='  ) THEN
     	    CALL MAPLOT_COM
     	    IF( IKEY.EQ.-999 ) GOTO 999
     	ELSE
C
C  PARAMETER INPUT...
C
     	    DO IKEY = 1, NKS
     		IF( KEY.EQ.KWD(IKEY) ) GOTO 222
     	    ENDDO
     	    CALL IFMISC_OOPS( 2, SLIN, SCOL, KEY )
     	ENDIF
     	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 777
     		IKEY = IKEY-28
     		CALL MAPLOT_MAP(2)
     	    ENDIF
     	ENDIF
     	GOTO 111
C
C  ERROR TRANSFER...
C
666	CALL IFMISC_OOPS( 4, SLIN, SCOL, ' ' )
     	GOTO 111
777	CALL IFMISC_OOPS( 11, SLIN, SCOL, ' ' )
     	GOTO 111
888	ICODE = 1		!QUIT FROM THE DIALOGUE
     	CALL IFANSI_CLEAR(2)
     	IF( .NOT.MENU ) CALL IFANSI_LEDS(0)
     	RETURN
999	IF( NCOL.EQ.0 ) GOTO 888
     	CALL MAPLOT_SETUP( 0, PARNAM )
     	ICODE = 0
     	IF( .NOT.MENU ) CALL IFANSI_LEDS(0)
     	RETURN
    	END

     	SUBROUTINE MAPLOT_FORM
C
C	THIS ROUTINE LAYOUTS THE DIALOGUE MENU FOR CONTOUR PLOT.
C
    	INCLUDE 'MAPLOT.CBS'
        CHARACTER TEXT*80
C
C  LAYOUT THE MENU...
C
     	IF( .NOT.MENU ) RETURN
     	CALL IFINIT_LINE(24)
        TEXT(1:34) = 'GRAFIT: Contour Plot (data storage'
        TEXT(35: ) = ' allocated:    x    grid/map)'
     	CALL IFANSI_TITLE( TEXT )
     	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=',14X,'SE=',4X,'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(20)
     	WRITE(6,6000)
6000	FORMAT(' oops:'//' Auxiliary: HELP/PF2, DCL/PF4, REFRESH/PF1,',
     +' RESET/PF3, SAVE, READ, PLOT,',8X,' '/' _commands: GO, ALLOCATE,'
     +,' OVERLAY/INCLUDE, SETUP, LAYOUT, TIME, EXIT ...',8X,' ',$)
C
C  INSERT THE PARAMETERS...
C
     	CALL MAPLOT_PARS_VM( MNGS )
     	CALL MAPLOT_PARS
     	RETURN
     	END

     	SUBROUTINE MAPLOT_PARS_VM( MS )
     	INCLUDE 'MAPLOT.CBS'
     	IF( MENU ) THEN
     	    CALL IFANSI_VIDEO(60)
     	    CALL IFANSI_IFL( MS, 1, 'I3', 1, 47 )
     	    CALL IFANSI_IFL( MS, 1, 'I3', 1, 51 )
     	ENDIF
     	RETURN
     	END

     	SUBROUTINE MAPLOT_PARS
C
C	THIS ROUTINE INSERTS ALL GRAPH PARAMETERS.
C
     	INCLUDE 'MAPLOT.CBS'
     	IF( MENU ) THEN
     	    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
     	ENDIF
     	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

     	IF( .NOT.MENU ) RETURN
     	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, '2F6.2', 5, 4 )
     	ENDIF
     	IF(I.EQ.0.OR.I.EQ.5) THEN
     	   RP(1) = XSCL
     	   RP(2) = YSCL
    	   CALL IFANSI_FFL( RP, 2, '2F6.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, 38 )
     	ENDIF
    	IF(I.EQ.0.OR.I.EQ.7) CALL IFANSI_IFL( IAXS, 1, 'I2', 5, 45 )
    	IF(I.EQ.0.OR.I.EQ.8) CALL IFANSI_IFL( MARK, 1, 'I2', 5, 51 )
    	IF(I.EQ.0.OR.I.EQ.9) CALL IFANSI_IFL( DRAW, 1, 'I2', 5, 57 )
    	IF(I.EQ.0.OR.I.EQ.10) CALL IFANSI_IFL( LDIG, 1, 'I2', 5, 63 )
    	IF(I.EQ.0.OR.I.EQ.11) THEN
     	   RP(1) = XLPS
     	   RP(2) = YLPS
     	   CALL IFANSI_FFL( RP, 2, '2F6.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)

     	IF( .NOT.MENU ) RETURN
     	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, '4I3', 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, '4F6.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_FFL( RP, 3, '3G11.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'

     	IF( .NOT.MENU ) RETURN
     	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,'7I2',K,4 )
    	IF(I.EQ.0.OR.I.EQ.2) CALL IFANSI_IFL(LINE(1,J),7,'7I2',K,23)
    	IF(I.EQ.0.OR.I.EQ.3) CALL IFANSI_IFL(BOLD(1,J),7,'7I2',K,42)
    	IF(I.EQ.0.OR.I.EQ.4) CALL IFANSI_IFL(FILL(1,J),7,'7I3',K,60)
     	IF(I.EQ.0.OR.I.EQ.5) CALL IFANSI_FFL(CLEV(1,J),7,'7G11.4',K+1,4)
     	IF(I.EQ.0.OR.I.EQ.6) CALL IFANSI_FFL(CMAP(J),1,'G11.4',K+2,4 )
     	IF(I.EQ.0.OR.I.EQ.7) CALL IFANSI_IFL(NEXP(J),1,'I2',K+2,23)
     	IF(I.EQ.0) THEN
     	   CALL IFANSI_VIDEO(60)
     	   CALL IFANSI_IFL( NCOL, 1, 'I4', K+2, 36 )
     	   CALL IFANSI_IFL( NROW, 1, 'I4', K+2, 45 )
     	   CALL IFANSI_FFL( ZBND(1,J), 1, 'G11.4', K+2, 54 )
     	   CALL IFANSI_FFL( ZBND(2,J), 1, 'G11.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

     	IF( .NOT.MENU ) RETURN
     	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	TIME   : display system clock.
C	ALLOCATE: allocate virtual memory for map storage.
C
     	INCLUDE    'MAPLOT.CBS'
    	CHARACTER  TEXT*80
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT

     	CALL IFMISC_UPPER(NCHS,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...
     	    IF( MENU ) THEN
     		IKEY = IFMISC_HLP( 'MAPLOT', TEXT, SLIN, SCOL )
     		IF( IKEY.EQ.0 ) CALL MAPLOT_FORM
     	    ELSE
     		IKEY = IFMISC_HLP( 'MAPLOT', TEXT, 22, 0 )
     		CALL IFINIT_LINE(-3)
     	    ENDIF
     	ELSE IF( TEXT(1:3).EQ.'REF' .OR. TEXT(1:4).EQ.'MENU'
     + .OR. (IKEY.EQ.27 .AND. TEXT(1:2).EQ.'OP') ) THEN
C
C  REFRESH...
     	    IF( .NOT.MENU ) THEN
     		MENU = .TRUE.
     		SLIN = 21
     		SCOL = 6
     		CALL IFANSI_LEDS(0)
     	    ENDIF
     	    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( TEXT(1:3).EQ.'ALL' ) THEN
C
C  ALLOCATE...
     	    CALL MAPLOT_VM
     	ELSE IF( TEXT(1:4).EQ.'TIME' ) THEN
C
C  TIME...
     	    IF( MENU ) CALL IFMISC_TIME
     	ELSE
     	    IF( IKEY.NE.27 ) THEN
     		CALL IFMISC_OOPS( 2, SLIN, SCOL, TEXT )
     	    ELSE
     		CALL IFMISC_OOPS( 5, SLIN, SCOL, ' ' )
     	    ENDIF
     	ENDIF
     	RETURN
888	CALL IFMISC_OOPS( 11, SLIN, SCOL, ' ' )	!NO DATA YET TO WORK ON
     	RETURN
     	END

     	SUBROUTINE MAPLOT_RESET
C
C	THIS ROUTINE RESETS THE PLOT.
C
     	INCLUDE 'MAPLOT.CBS'
     	IF( MENU ) CALL MAPLOT_CLEAR_OOPS
     	CALL MAPLOT_OOPS(' Resetting the plot...')
     	CALL MAPLOT_INIT(1)
     	DO I=1, 4
     	   CALL MAPLOT_SCALE(I)
     	ENDDO
     	IF( MENU ) THEN
     	    CALL MAPLOT_PARS
     	    CALL MAPLOT_CLEAR_OOPS
     	ENDIF
     	RETURN
     	END

     	SUBROUTINE MAPLOT_FILES
C
C	THIS ROUTINE GETS INPUT DATA/PARAMETER/LAYOUT FILES.
C
     	INCLUDE		'MAPLOT.CBS'
     	CHARACTER	TEXT*80
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT

     	JKEY = IKEY
     	IKEY = IABS(IKEY)
     	IF( NCHS.EQ.0  ) THEN
     	    NCHS = IFMISC_PROMPT('_Filename:',TEXT,SLIN+1,9)
     	    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 IFMISC_OOPS( 7, SLIN, SCOL, TEXT )
     	    GOTO 999
     	ENDIF
     	CALL IFMISC_UPPER(NCHS,TEXT)
     	IF( IKEY.NE.1 ) GOTO 222
C
C  START A NEW PLOT WITH A NEW MAP FILE...
C
     	IKEY = MAPLOT_DATA( 1, TEXT, MNGS, %VAL(MAPL(1)) )
     	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 = -999
     	ENDIF
     	GOTO 999

222	IF( IKEY.NE.2 ) GOTO 333
C
C  OVERLAY A MAP FILE...
C
     	IKEY = MAPLOT_DATA( 2, TEXT, MNGS, %VAL(MAPL(2)) )
     	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 IFMISC_OOPS( 1, SLIN, SCOL, TEXT )
     	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	IF(IKEY.EQ.1) CALL IFMISC_OOPS( 1, SLIN, SCOL, TEXT )
     	IF(IKEY.EQ.2) CALL IFMISC_OOPS( 13, SLIN, SCOL, TEXT )
     	IF(IKEY.EQ.3) CALL IFMISC_OOPS( 14, SLIN, SCOL, TEXT )
     	IF(IKEY.EQ.4) CALL MAPLOT_OOPS(' Map grid size overflows.')
     	IF(IKEY.EQ.5) THEN
     	   CALL MAPLOT_OOPS(' 2nd map has different grid size.')
     	ENDIF

999	RETURN
     	END

     	FUNCTION MAPLOT_DATA( MAP, FNAME, MNG, ZMAP )
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'
     	DIMENSION ZMAP(MNG,MNG)
     	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),I=1,NC),J=1,NR)
3000	    FORMAT(<NC>G)
     	ELSE
     	    READ(13,ERR=888,END=777) ((ZMAP(I,J),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)
     	YH = YL
     	DO  J=1, NR
     	    DO  I=1, NC
     		XL = ZMAP(I,J)
     		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.
     	    MENU = .TRUE.
	    PLOTID = ' '
     	    MNGS = 51
     	    CALL IF_DCL_VM( IAXS, I )
     	    IF( IAXS.GT.1 .AND. IAXS.LT.1000 ) MNGS = IAXS
     	    I = 4*MNGS*MNGS
     	    IAXS = LIB$GET_VM(I,MAPL(1))
     	    IF( .NOT.IAXS ) GOTO 888
     	    IAXS = LIB$GET_VM(I,MAPL(2))
     	    IF( .NOT.IAXS ) GOTO 888
     	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
888	CALL IFMISC_OOPS( 6, 0, 0, ' ' )
     	CALL EXIT
    	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 .AND. IKEY.NE.7 ) THEN
     	    NCHS = IFMISC_DECODE( TEXT, NCHS, 2, RP, KP )
    	    IF( NCHS.EQ.0 ) GOTO 777
     	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
     	ELSE 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
     	ELSE 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.
     	ELSE 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)
     	ELSE 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)
     	ELSE 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)
     	ELSE 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)
     	ELSE 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
     	ELSE
     	    GOTO 777
     	ENDIF
     	CALL MAPLOT_PARS_ALL(IKEY)
     	RETURN
777	CALL IFMISC_OOPS( 3, SLIN, SCOL, TEXT )
     	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, NCHS, 4, RP, KP )
    	IF( J.EQ.0 ) GOTO 777
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 IFMISC_OOPS( 3, SLIN, SCOL, TEXT )	!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, NCHS, 4, RP, KP )
    	IF( J.EQ.0 ) GOTO 777
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 IFMISC_OOPS( 3, SLIN, SCOL, TEXT )
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, NCHS, 3, RP, KP )
    	IF( J.EQ.0 ) GOTO 777
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 777
     	    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 IFMISC_OOPS( 3, SLIN, SCOL, TEXT )
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, NCHS, 7, RP, KP )
    	IF( J.EQ.0 ) GOTO 777
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
     	ELSE 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
     	ELSE 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
     	ELSE 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
     	ELSE 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
     	ELSE IF( IKEY.EQ.6 ) THEN
     	    IF( RP(1).NE.0.0 ) CMAP(JMAP) = RP(1)
     	ELSE IF( IKEY.EQ.7 ) THEN
     	    J = RP(1)
     	    IF( J.LT.0 .OR. J.GT.9 ) GOTO 777
     	    IF( J*NROW.GT.MNGS .OR. J*NCOL.GT.MNGS ) GOTO 777
     	    NEXP(JMAP) = J
     	ELSE
     	    GOTO 777
     	ENDIF
     	CALL MAPLOT_PARS_MAP(JMAP,IKEY)
     	GOTO 999
777	CALL IFMISC_OOPS( 3, SLIN, SCOL, TEXT )
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 = IFMISC_PROMPT('_Filename:',TEXT,SLIN+1,9)
     	    IF( NCHS.EQ.0 ) GOTO 999
     	ENDIF
     	IF( NCHS.GT.32 ) THEN
     	    CALL IFMISC_OOPS( 7, SLIN, SCOL, TEXT )
     	    GOTO 999
     	ENDIF
     	IKEY = MAPLOT_SETUP( 0, TEXT )
     	IF( IKEY.EQ.0 ) THEN
     	    CALL IFMISC_OOPS( 21, SLIN, SCOL, TEXT )
     	ELSE
     	    STRN = ' Current plot setup has been saved in'
     	    STRN(39:) = TEXT
     	    CALL MAPLOT_OOPS( STRN )
     	ENDIF

999	RETURN
     	END

     	SUBROUTINE MAPLOT_OOPS( TEXT )
     	INCLUDE 'MAPLOT.CBS'
     	CHARACTER TEXT*(*)
     	NCHS = IFMISC_LENG(TEXT)
     	IF( SLIN.GT.0 .AND. SCOL.GT.0 ) THEN
     	    CALL MAPLOT_CLEAR_OOPS
     	    CALL IFANSI_TEXT( TEXT, NCHS, SLIN, SCOL )
     	ELSE
     	    WRITE(6,1000) TEXT(1:NCHS)
1000	    FORMAT('_oops: ',A)
     	ENDIF
     	RETURN
     	END

     	SUBROUTINE MAPLOT_CLEAR_OOPS
     	INCLUDE 'MAPLOT.CBS'
     	IF( MENU ) THEN
     	    CALL IFANSI_CURSOR(SLIN,SCOL)
     	    CALL IFANSI_CLEAR(4)
     	ENDIF
     	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 IFMISC_OOPS( 17, 0, 0, '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 IFMISC_OOPS( 19, 0, 0, ' ' )
     	    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
     	CALL IFMISC_UPPER(4,KEY)
     	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, NCH, 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
     	ELSE 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)
     	ELSE 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)
     	ELSE 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
     	ELSE 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
     	ELSE IF( I.EQ.11 ) THEN	!HARDCOPY SETUP PARAMETERS
     	    DO  I=1, 10
     		IF( KP(I).EQ.1 ) HPAR(I) = RP(I)
     	    ENDDO
     	ELSE IF( I.EQ.12 ) THEN	!HARDCOPY SETUP PARAMETERS
     	    DO  I=1, 10
     		IF( KP(I).EQ.1 ) HPAR(I+10) = RP(I)
     	    ENDDO
     	ELSE IF( I.EQ.13 ) THEN	!PLOT ID LABEL
     	    PLOTID = REST
     	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

     	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

     	SUBROUTINE MAPLOT_VM
C
C	THIS ROUTINE ALLOCATES VIRTUAL MEMORY FOR DATA STORAGE.
C
     	INCLUDE 'MAPLOT.CBS'
     	CHARACTER TEXT*80
     	COMMON/MAPLOT_MENU/ IKEY, NCHS, TEXT

     	NCHS = IFMISC_NEXT(TEXT)
     	IF( NCHS.EQ.0 ) THEN
     	    NCHS = IFMISC_PROMPT('_Grid size (I):',TEXT,SLIN+1,9)
     	    IF( NCHS.EQ.0 ) RETURN
     	ENDIF
     	NCHS = IFMISC_DECODE( TEXT, NCHS, 1, RP, KP )
     	IF( NCHS.EQ.0 ) THEN
     	    CALL IFMISC_OOPS( 3, SLIN, SCOL, TEXT )
     	ELSE
     	    IF( KP.NE.0 ) THEN
     		KP = RP
     		IF( KP.LT.2 .OR. KP.GT.999 ) GOTO 888
     	    ELSE
     		KP = MNGS
     	    ENDIF
     	    NCHS = 4*MNGS*MNGS
     	    CALL LIB$FREE_VM(NCHS,MAPL(1))
     	    CALL LIB$FREE_VM(NCHS,MAPL(2))
     	    MNGS = KP
     	    NCHS = 4*MNGS*MNGS
     	    IKEY = LIB$GET_VM( NCHS, MAPL(1) )
     	    IF( IKEY.EQ.0 ) GOTO 777
     	    IKEY = LIB$GET_VM( NCHS, MAPL(2) )
     	    IF( IKEY.EQ.0 ) GOTO 777
     	    NCOL = 0
     	    NROL = 0
     	    CALL MAPLOT_INIT(1)
     	    CALL MAPLOT_PARS_VM(MNGS)
     	    CALL MAPLOT_PARS
     	ENDIF
     	RETURN
777	CALL IFMISC_OOPS( 6, SLIN, SCOL, ' ' )
     	RETURN
888	CALL IFMISC_OOPS( 3, SLIN, SCOL, TEXT )
     	RETURN
     	END

     	SUBROUTINE MAPLOT_PLOT
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY
C
C	THIS ROUTINE CARRIES OUT CONTOURING FOR CONTOUR PLOT.
C
     	INCLUDE 'MAPLOT.CBS'
C
C  MAKE SURE THE AXIS SCALES ARE OK...
C
     	IF( XINC(1).EQ.0.0 .OR. XINC(2).EQ.0.0 ) THEN
     	    IF( IF_DCL_HARDCOPY() ) THEN
     		STOP '%MAPLOT: data scaling failure.'
     	    ELSE
     		CALL IFMISC_OOPS( 10, 0, 0, ' ' )
     		RETURN
     	    ENDIF
     	ENDIF
C
C  INITIALIZE THE CRT...
C
     	CALL IFANSI_VIDEO(47)
     	CALL IFINIT_LINE( 3 )
     	IF( INIT ) THEN
     	    CALL IFINIT_PLOT( XORG, YORG, DATNAM )
     	ELSE
     	    CALL IFINIT_PLOT( XORG, YORG, '$OVERLAY' )
     	ENDIF
     	IF( IFCTRL_C() ) GOTO 999
C
C  OPEN A GRAPHICS SEGMENT TO SAVE THE PLOT...
C
     	IF( .NOT.IFINIT_SAVE() ) RETURN
     	CALL IFSCAL( XSCL, YSCL )
     	CALL IFROTA( XANG(1), XANG(2) )
C
C  DRAW GRAPH TITLE IF ANY...
C
     	IF( TITL(1).NE.' ' ) THEN
     	    CALL IFSPEN(1)
     	    CALL IFTEXT_CENT(0.,ABS(XLNS(1)),-1.,-1.,TITL(1),0.15,0.15)
     	ENDIF
C
C  DRAW THE X,Y AXES...
C
     	CALL IFSPEN( IAXS )
     	IF( XLNS(1).GT.0 ) THEN
     	IF( IFCTRL_C() ) GOTO 999
     	    IF( SOFF(1).NE.0.0 ) CALL IFPLOT( 0.0, SOFF(1), -3 )
     	    CALL IFAXIS( 1, XLNS(1), ABS(XLNS(2)), TITL(2), XLOW(1),
     +      XINC(1), OFFS(1), NTKS(1), MTKS(1), NDGS(1), IGDS(1) )
     	    IF( SOFF(1).NE.0.0 ) CALL IFPLOT( 0.0, -SOFF(1), -3 )
     	ENDIF
     	IF( XLNS(2).GT.0 ) THEN
     	IF( IFCTRL_C() ) GOTO 999
     	    IF( SOFF(2).NE.0.0 ) CALL IFPLOT( SOFF(2), 0.0, -3 )
     	    CALL IFAXIS( 4, ABS(XLNS(1)), XLNS(2), TITL(3), XLOW(2),
     +      XINC(2), OFFS(2), NTKS(2), MTKS(2), NDGS(2), IGDS(2) )
     	    IF( SOFF(2).NE.0.0 ) CALL IFPLOT( -SOFF(2), 0.0, -3 )
     	ENDIF
C
C  DRAW THE CONTOURS...
C
     	IF( IFCTRL_C() ) GOTO 999
     	CALL MAPLOT_DRAW(1)			!1ST MAP
     	IF( IFCTRL_C() ) GOTO 999
     	IF( ADDNAM.NE.' ' ) CALL MAPLOT_DRAW(2)	!2ND MAP
C
C  DRAW THE LABEL BOX IF DESIRED...
C
     	CALL IFSTYL( 1, 1 )
     	CALL IFSPEN( 1 )
     	IF( DRAW.EQ.2 ) CALL MAPLOT_DRAW_BOX( XLPS, YLPS )
C
C  CLOSE THE PLOT SESSION...
C
     	IF( CRT.EQ.2 ) CALL IFSEG_OPEN( 0 ) 	!CLOSE THE SEGMENT
     	IF( IFCTRL_C() ) GOTO 999
    	IF( DESNAM.NE.' ' ) CALL LAYOUT_AUX(0,XORG,YORG,DESNAM)
  999	CALL IFINIT_PLOT_END
     	RETURN
    	END

     	SUBROUTINE MAPLOT_DRAW( MAP )
C
C 	THIS ROUTINE CONTOURS THE MAPS.
C
     	INCLUDE   'MAPLOT.CBS'
     	COMMON/MAPLOT_DRAW/ XPOS, YPOS, LMAX
C
C  ALLOCATE SCRATCH ARRAY...
C
     	NBYTES = 4*MNGS*MNGS
     	I = LIB$GET_VM( NBYTES, MAPS )
     	IF( .NOT.I ) GOTO 888
     	MBYTES = 40*MNGS
     	I = LIB$GET_VM( MBYTES, MAPR )
     	IF( .NOT.I ) GOTO 888
     	KM = MAP
     	IF( KM.EQ.1 ) THEN
     	    XPOS = XLPS + 0.15
     	    YPOS = YLPS + 0.15
     	    LMAX = 3
     	ENDIF
C
C  LOAD THE MAP DATA...
C
     	IF( IFCTRL_C() ) GOTO 999
     	CALL MAPLOT_LOAD( KM,NC,NR,MNGS, %VAL(MAPL(KM)), %VAL(MAPS) )
     	IF( IFCTRL_C() ) GOTO 999
C
C  CONTOUR THE MAP LEVEL BY LEVEL...
C
     	DO  I=1, 7
     	IF( IFCTRL_C() ) GOTO 999
     	    IF( CPEN(I,KM).GT.0 ) THEN
     		K = LINE(I,KM)
     		CALL IFSPEN( CPEN(I,KM) )
     		CALL IFSTYL( IABS(K), BOLD(I,KM) )
     		CALL MAPLOT_CONTOR( MNGS, NC, NR, K, CLEV(I,KM),
     +  %VAL(MAPS),%VAL(MAPR), FILL(I,KM),ABS(XLNS(1)),ABS(XLNS(2)) )
     	IF( IFCTRL_C() ) GOTO 999
     		IF(DRAW.GT.0) CALL MAPLOT_LABEL( CLEV(I,KM) )
     	    ENDIF
     	ENDDO
C
C  MARK MIN AND MAX CONTOUR POINTS IF DESIRED...
C
	IF( MARK.GT.0 ) CALL MAPLOT_MARK( KM,NC,NR,MNGS,%VAL(MAPS) )
999	CALL LIB$FREE_VM( NBYTES, MAPS )
     	CALL LIB$FREE_VM( MBYTES, MAPR )
     	RETURN
888	IF( IF_DCL_HARDCOPY() ) THEN
	STOP '%MAPLOT: insufficient virtual memory to be allocated'
     	ELSE
     	CALL IFMISC_OOPS( 6, 0, 0, ' ' )
     	ENDIF
     	RETURN
     	END

     	SUBROUTINE MAPLOT_LOAD( MAP, NC, NR, MG, ZMAP, Z )
C
C	THIS ROUTINE LOADS THE MAP DATA FOR CONTOURING.
C
     	INCLUDE 'MAPLOT.CBS'
     	DIMENSION ZMAP(MG,MG), Z(MG*MG)
C
C  GET THE INITIAL AND THE ENDING COLUMNS AND ROWS TO CONTOUR...
C
     	KM  = MAP
     	IC1 = 1
     	IC2 = NCOL
     	IR1 = 1
     	IR2 = NROW
	IF(XLOW(1).GT.XBND(1,1)) IC1=(XLOW(1)-XBND(1,1))/XBND(3,1)+1.1
     	IF(XHIG(1).LT.XBND(2,1)) IC2=(XHIG(1)-XBND(1,1))/XBND(3,1)+1.1
	IF(XLOW(2).GT.XBND(1,2)) IR1=(XLOW(2)-XBND(1,2))/XBND(3,2)+1.1
     	IF(XHIG(2).LT.XBND(2,2)) IR2=(XHIG(2)-XBND(1,2))/XBND(3,2)+1.1
C
C  LOAD THE MAP...
C
     	CM = CMAP(KM)
     	K = 0
     	DO  J= IR1, IR2
     	    DO  I= IC1, IC2
     		K = K + 1
     		Z(K) = ZMAP(I,J)*CM
     	    ENDDO
     	ENDDO
     	NC = IC2-IC1+1
     	NR = IR2-IR1+1

     	RETURN
     	END

     	SUBROUTINE MAPLOT_LABEL( CLV )
C
C	THIS ROUTINE DRAWS THE CONTOUR LEVEL LABELS.
C
     	INCLUDE 'MAPLOT.CBS'
     	COMMON/MAPLOT_DRAW/ XPOS, YPOS, LMAX
     	CHARACTER LABEL*12

     	IF( CLV.NE.0.0 ) THEN
     	    IF( LDIG.GT.5 ) LDIG = 5
     	    WRITE( LABEL, 111 ) CLV
111	    FORMAT(G<LDIG+7>.<LDIG>)
     	    NCH = MAPLOT_STRIP( 12, LABEL )
     	    IF( NCH.GT.LMAX ) LMAX = NCH
     	ELSE
     	    LABEL = '0.0'
     	ENDIF
     	CALL IFTEXT( XPOS+0.05, YPOS, LABEL, 0.1, 0.1, 0 )
     	CALL IFPLOT( XPOS, YPOS-0.03, 3 )
     	CALL IFPLOT( XPOS+0.5, YPOS-0.03, 2 )
     	YPOS = YPOS + 0.25

     	RETURN
     	END

     	SUBROUTINE MAPLOT_DRAW_BOX( XPS, YPS )
C
C	THIS ROUTINE DRAWS A BOX AROUND THE CONTOUR LEVEL LABELS.
C
     	COMMON/MAPLOT_DRAW/ XPOS, YPOS, LMAX

     	XPOS = 0.8
     	IF( LMAX.GT.5 ) XPOS = 0.3 + 0.1*LMAX
     	XLPS = XPS
     	YLPS = YPS
     	XPOS = XPOS + XLPS
     	CALL IFPLOT( XLPS, YLPS, 3 )
     	CALL IFPLOT( XLPS, YPOS, 2 )
     	CALL IFPLOT( XPOS, YPOS, 2 )
     	CALL IFPLOT( XPOS, YLPS, 2 )
     	CALL IFPLOT( XLPS, YLPS, 2 )

     	RETURN
     	END

     	SUBROUTINE MAPLOT_MARK( MAP, NC, NR, MC, Z )
C
C	THIS ROUTINE MARKS THE MIN AND MAX POINTS ON THE MAP.
C
     	INCLUDE   'MAPLOT.CBS'
     	CHARACTER LABEL*12
     	DIMENSION Z(MC*MC)
C
C  SEARCH FOR MIN AND MAX POINTS IN THE MAP...
C
     	KM = MAP
     	KC = NC
     	KR = NR
     	CX = -1.7E38
     	CM =  1.7E38
     	DO  J=1, KR
     	    DO  I=1, KC
     		CMX = Z(I+(J-1)*KC)
     		IF( CMX.GT.CX ) THEN
     		    CX = CMX
     		    IC = I
     		    IR = J
     		ENDIF
     		IF( CMX.LT.CM ) THEN
     		    CM = CMX
     		    JC = I
     		    JR = J
     		ENDIF
     	    ENDDO
     	ENDDO
C
C  MARK THE MAXIMUM POINT WITH A SOLID TRIANGLE...
C
     	XPS = ABS(XLNS(1))*FLOAT(IC-1)/FLOAT(KC-1)
     	YPS = ABS(XLNS(2))*FLOAT(IR-1)/FLOAT(KR-1)
     	IPN = 3
     	CMX = -1.7E38
     	DO  I=1, 7
     	    IF( CPEN(I,KM).GT.0 .AND. CLEV(I,KM).GT.CMX ) THEN
     		CMX = CLEV(I,KM)
     		IPN = CPEN(I,KM)
     	    ENDIF
     	ENDDO
     	CALL IFSPEN( IPN )
     	CALL IFMARK( XPS, YPS, 0.1, 0, 6 )
     	IF( CX.NE.0.0 ) THEN
     	    WRITE( LABEL, 111 ) CX
111	    FORMAT(G<LDIG+7>.<LDIG>)
     	    IPN = MAPLOT_STRIP( 12, LABEL )
     	ELSE
     	    LABEL = '0.0'
     	    IPN = 3
     	ENDIF
     	CALL IFTEXT( XPS-0.05*IPN, YPS+0.1, LABEL, 0.1, 0.1, 0 )
C
C  MARK THE MINIMUM POINT WITH A TRIANGLE...
C
     	XPS = ABS(XLNS(1))*FLOAT(JC-1)/FLOAT(KC-1)
     	YPS = ABS(XLNS(2))*FLOAT(JR-1)/FLOAT(KR-1)
     	IPN = 1
     	CMX = 1.7E38
     	DO  I=1, 7
     	    IF( CPEN(I,KM).GT.0 .AND. CLEV(I,KM).LT.CMX ) THEN
     		CMX = CLEV(I,KM)
     		IPN = CPEN(I,KM)
     	    ENDIF
     	ENDDO
     	CALL IFSPEN( IPN )
     	CALL IFMARK( XPS, YPS, 0.1, 0, 5 )
     	IF( CM.NE.0.0 ) THEN
     	    WRITE( LABEL(1:LDIG+7), 111 ) CM
C     	    ENCODE( LDIG+7, 111, LABEL ) CM
     	    IPN = MAPLOT_STRIP( LDIG+7, LABEL )
     	ELSE
     	    LABEL = '0.0'
     	    IPN = 3
     	ENDIF
     	CALL IFTEXT( XPS-0.05*IPN, YPS+0.1, LABEL, 0.1, 0.1, 0 )

     	RETURN
     	END

     	FUNCTION MAPLOT_STRIP( NK, FNUM )
     	CHARACTER FNUM*(*)

     	K = NK
     	DO  I=K,1,-1
     	    IF( FNUM(I:I).NE.' ' ) GOTO 2
     	ENDDO
  2	K = I
     	I = INDEX( FNUM(1:K), 'E' )
     	IF( I.GT.0 ) K = I-1
     	DO  J=K,1,-1
     	    IF( FNUM(J:J).NE.'0' ) GOTO 3
     	ENDDO
  3	IF( FNUM(J:J).EQ.'.' ) J = J + 1
     	IF( I.GT.0 ) THEN
     	    FNUM(J+1:) = FNUM(I:)
     	    J = J + 4
     	ENDIF
     	FNUM(J+1:) = ' '
     	MAPLOT_STRIP = J
     	RETURN
     	END

     	SUBROUTINE MAPLOT_CONTOR( MC,NC,NR,JS,CL,AM,IR,FF,XS,YS )
C
C	THIS ROUTINE CONTOURS A MAP.
C
C	NC --------- NUMBER OF COLUMNS (X) TO BE PLOTTED.
C	NR --------- NUMBER OF    ROWS (Y) TO BE PLOTTED.
C	JS --------- SPLINE SMOOTH FLAG.
C	CL --------- CONTOUR LEVEL.
C	AM(I,J) ---- ORDINATE AT POINT X(I), Y(J)
C	             X(I),Y(J) ARE ASSUMED TO BE EQUALLY SPACED.
C	IR --------- SCRATCH ARRAY OF SIZE MC.
C	FF --------- PATTERN OF AREA-FILL IF FILL IS DESIRED.
C	XS,YS ------ WIDTH AND HEIGHT OF THE GRAPH IN INCHES.
C
     	DIMENSION AM(MC*MC), IR(10*MC)
     	INTEGER   DIM, FF, SPS
     	COMMON/MAPLOT_XYBND/ MT, NT, XINC, YINC, XSIZ, YSIZ
     	COMMON/MAPLOT_CAVIN/ DIM, IX, IY, IDX, IDY, ISS, SPS,
     +  NP, N, CV, IS, IS0, IX0, IY0, DCP, IFF, INX(8), INY(8), MMM
     	DATA INX/ -1, -1,  0,  1,  1,  1,  0, -1 /
     	DATA INY/  0,  1,  1,  1,  0, -1, -1, -1 /
C
C  INITIALIZE...
C
     	DIM  = NC
     	MT   = NC
     	NT   = NR
     	SPS  = 0
     	IF( JS.LT.0 ) SPS = 1		!2-D SPLINE-SMOOTH ON
     	XSIZ = XS
     	YSIZ = YS
     	XINC = XSIZ/(MT-1)
     	YINC = YSIZ/(NT-1)
     	IFF  = FF
     	CV   = CL
     	NP   = 0
     	ISS  = 0
     	MT1  = MT-1
     	NT1  = NT-1
     	MMM  = 10*MC-1
C
C  ALLOCATE THE LINE ARRAYS...
C
     	NBYTES = 40*MC
     	I = LIB$GET_VM( NBYTES, JXX )
     	IF( .NOT.I ) GOTO 888
     	I = LIB$GET_VM( NBYTES, JYY )
     	IF( .NOT.I ) GOTO 888
C
C  CONTOUR THE MAP...
C
     	DO 110 I=1,MT1
     	IF( AM(I).GE.CV .OR. AM(I+1).LT.CV ) GO TO 110
     	IX0=I+1
     	IX=I+1
     	IY0=1
     	IY=1
     	IS0=1
     	IS=1
     	IDX=-1
     	IDY=0
     	CALL MAPLOT_TRACE( MC, AM, IR, %VAL(JXX), %VAL(JYY) )
  110	CONTINUE

     	J=MT-DIM
     	DO 120 I=1,NT1
     	J=J+DIM
     	IF( AM(J).GE.CV .OR. AM(J+DIM).LT.CV ) GO TO 120
     	IX0=MT
     	IX=MT
     	IY0=I+1
     	IY=I+1
     	IDX=0
     	IDY=-1
     	IS0=7
     	IS=7
     	CALL MAPLOT_TRACE( MC, AM, IR, %VAL(JXX), %VAL(JYY) )
  120	CONTINUE

     	J=MT+NT1*DIM+1
     	DO 130 I=1,MT1
     	J=J-1
     	IF( AM(J).GE.CV .OR. AM(J-1).LT.CV ) GO TO 130
     	IX0=MT-I
     	IX=MT-I
     	IY0=NT
     	IY=NT
     	IDX=1
     	IDY=0
     	IS0=5
     	IS=5
     	CALL MAPLOT_TRACE( MC, AM, IR, %VAL(JXX), %VAL(JYY) )
  130	CONTINUE

     	J=NT*DIM+1
     	DO 140 I=1,NT1
     	J=J-DIM
     	IF( AM(J).GE.CV .OR. AM(J-DIM).LT.CV ) GO TO 140
     	IX0=1
     	IX=1
     	IY0=NT-I
     	IY=NT-I
     	IDX=0
     	IDY=1
     	IS0=3
     	IS=3
     	CALL MAPLOT_TRACE( MC, AM, IR, %VAL(JXX), %VAL(JYY) )
  140	CONTINUE

     	ISS=1
     	L=0
     	DO 170 J=2,NT1
     	L=L+DIM
     	DO 160 I=1,MT1
     	L=L+1
     	IF( AM(L).GE.CV .OR. AM(L+1).LT.CV ) GO TO 160
     	K=L+1
     	DO ID=1,NP
     	   IF (IR(ID) .EQ. K) GO TO 160
  	ENDDO
     	IX0=I+1
     	IX=I+1
     	IY0=J
     	IY=J
     	IDX=-1
     	IDY=0
     	IS0=1
     	IS=1
     	CALL MAPLOT_TRACE( MC, AM, IR, %VAL(JXX), %VAL(JYY) )
  160	CONTINUE
  170	L=L-MT1
     	CALL LIB$FREE_VM( NBYTES, JXX )
     	CALL LIB$FREE_VM( NBYTES, JYY )
     	RETURN
  888	IF( IF_DCL_HARDCOPY() ) THEN
     	STOP '%MAPLOT: Insufficient virtual memory to be allocated'
     	ELSE
     	CALL MAPLOT_OOPS( 6, 0, 0 )
     	ENDIF
     	RETURN
     	END

     	SUBROUTINE MAPLOT_TRACE( MC, AM, IREC, X, Y )
     	DIMENSION AM(MC*MC), IREC(10*MC), X(10*MC), Y(10*MC)
     	INTEGER   DIM, SPS
     	COMMON/MAPLOT_XYBND/ MT, NT, XINC, YINC, XSIZ, YSIZ
     	COMMON/MAPLOT_CAVIN/ DIM, IX, IY, IDX, IDY, ISS, SPS,
     +  NP, N, CV, IS, IS0, IX0, IY0, DCP, IFF, INX(8), INY(8), MMM
     	LOGICAL YES

     	N  = 0
     	JY = DIM*(IY-1)+IX
     	MY = DIM*IDY+IDX+JY
     	YES = .FALSE.
  100   IF (N.EQ.MMM) THEN
     	    YES = .TRUE.
     	    GO TO 310
        ENDIF
     	N = N+1
     	IF (IDX .GT. 0) GO TO 120
     	IF (IDX .LT. 0) GO TO 110
     	Y(N)=FLOAT(IY-1)+FLOAT(IDY)*(AM(JY)-CV)/(AM(JY)-AM(DIM*IDY+JY))
     	X(N)=FLOAT(IX-1)
     	GO TO 130
  110	NP=NP+1
     	IREC(NP)=JY
  120	X(N)=FLOAT(IX-1)+FLOAT(IDX)*(AM(JY)-CV)/(AM(JY)-AM(JY+IDX))
     	Y(N)=FLOAT(IY-1)
  130	IS=IS+1
  140	IF (IS .LE. 8) GO TO 150
     	IS=IS-8
  150	IDX=INX(IS)
     	IDY=INY(IS)
     	IX2=IX+IDX
     	IY2=IY+IDY
     	IR=IDX*IDY
     	IF (ISS .EQ. 0) GO TO 160
     	IF ((IS.NE.IS0) .OR. (IY.NE.IY0) .OR. (IX.NE.IX0)) GO TO 170
     	N=N+1
     	X(N)=X(1)
     	Y(N)=Y(1)
     	GO TO 310
  160	IF( (IX2.EQ.0).OR.(IX2.GT.MT).OR.(IY2.EQ.0).OR.(IY2.GT.NT) )
     +	GO TO 310
  170	MY=DIM*IDY+IDX+JY
     	IF (IR .GT. 0) GO TO 200
     	IF (IR .LT. 0) GO TO 190
     	IF (CV .GT. AM(MY)) GO TO 100
     	IX=IX2
     	IY=IY2
  180	IS=IS+5
     	JY=MY
     	GO TO 140
  190	KY=JY+IDX
     	LY=MY-IDX
     	GO TO 210
  200	KY=MY-IDY
     	LY=JY+IDY
  210	DCP=(AM(JY)+AM(KY)+AM(LY)+AM(MY))*.25
     	IF (CV .LE. DCP) GO TO 220
     	CALL MAPLOT_GETPT(JY,AM,X,Y)
     	GO TO 130
  220	IF (IR .GE. 0) GO TO 230
     	IX=IX2
     	IDX=-IDX
     	CALL MAPLOT_GETPT(KY,AM,X,Y)
     	IY=IY2
     	IDY=-IDY
     	GO TO 240
  230	IY=IY2
     	IDY=-IDY
     	CALL MAPLOT_GETPT(KY,AM,X,Y)
     	IX=IX2
     	IDX=-IDX
  240	IF (CV .LE. AM(MY)) GO TO 180
     	CALL MAPLOT_GETPT(MY,AM,X,Y)
     	IF (IR .GE. 0) GO TO 250
     	IX=IX+IDX
     	IDX=-IDX
     	GO TO 260
  250	IY=IY+IDY
     	IDY=-IDY
  260	IF (CV .GT. AM(LY)) GO TO 290
     	IS=IS-1
     	JY=LY
     	GO TO 150
  290	CALL MAPLOT_GETPT(LY,AM,X,Y)
     	IF (IR .GE. 0) GO TO 300
     	IY=IY+IDY
     	GO TO 130
  300	IX=IX+IDX
     	GO TO 130
  310	DO 320 I=1, N
     	X(I) = X(I)*XINC
  320	Y(I) = Y(I)*YINC
C
C  PLOT THE CONTOUR LINE...
C  IF AREA-FILL IS ON, CLOSE AN OPEN LOOP BY ADDING A CORNER POINT...
C
     	IF( IFF.NE.0 .AND. ABS(X(1)-X(N)).GT.0.01
     +.AND. ABS(Y(1)-Y(N)).GT.0.01 ) THEN
     	    IF( X(1).EQ.0.0 .OR. X(N).EQ.0.0 ) THEN
     		X(N+1) = 0.0
     	    ELSE
     		X(N+1) = XSIZ
     	    ENDIF
     	    IF( Y(1).EQ.0.0 .OR. Y(N).EQ.0.0 ) THEN
     		Y(N+1) = 0.0
     	    ELSE
     		Y(N+1) = YSIZ
     	    ENDIF
     	    N = N + 1
     	ENDIF
     	IF( IFF.NE.0 ) CALL IFFILL( IFF )
     	CALL IFCURV( N, SPS, X, Y )
     	IF( IFF.NE.0 ) CALL IFFILL( 0 )
     	IF( YES ) THEN
     	    N = 0
     	    YES = .FALSE.
     	    GOTO 100
      	ENDIF
     	RETURN
     	END

     	SUBROUTINE MAPLOT_GETPT(J,AM,X,Y)
     	DIMENSION AM(1), X(1), Y(1)
     	INTEGER   DIM, SPS
     	COMMON/MAPLOT_CAVIN/ DIM, IX, IY, IDX, IDY, ISS, SPS,
     +  NP, N, CV, IS, IS0, IX0, IY0, DCP, IFF, INX(8), INY(8), MMM
     	N=N+1
     	B=AM(J)-DCP
     	IF (B.NE.0) THEN
     	   V=.5*(AM(J)-CV)/B
     	ELSE
     	   V=.5
     	ENDIF
     	X(N)=FLOAT(IX-1)+FLOAT(IDX)*V
     	Y(N)=FLOAT(IY-1)+FLOAT(IDY)*V
     	RETURN
     	END

