     	SUBROUTINE IFINIT( LOUT, IR, ARRAY )
C
C  PROGRAMMER: R.S. ZHOU
C	       ALFRED UNIVERSITY, SEP-1986
C
C	THIS ROUTINE INITIALIZES A PLOT PAGE FOR HP7221.
C
C		LOUT --- OUTPUT FILE LOGICAL UNIT
C		IR ----- LENGTH OF "ARRAY"
C		ARRAY -- DEVICE INITIALIZATION PARAMETERS
C
    	INCLUDE 'HP7221.CBS'
     	DIMENSION ARRAY(1)
C
C  INITIALIZE COMMON GRAPHIC PARAMETERS...
C
    	IPL = 0
     	DO I=1, NBS+5
     	   IPB(I) = 0
     	ENDDO
     	LUN = LOUT
     	JXO = 0
     	JYO = 0
    	FUNI = 1016.0
     	IRR  = IR
    	IF( IRR.GT.0 ) JXO = ARRAY(1)*FUNI
    	IF( IRR.GT.1 ) JYO = ARRAY(2)*FUNI
     	XSCL = 1.0
     	YSCL = 1.0
     	IF( IRR.GT.2 .AND. ARRAY(3).GT.0.0 ) XSCL= ARRAY(3)
     	IF( IRR.GT.3 .AND. ARRAY(4).GT.0.0 ) YSCL= ARRAY(4)
     	LTY = 1
     	LWD = 1
     	IF( IRR.GT.4 .AND. ARRAY(5).GT.0.0 ) LWD = ARRAY(5)
     	CALL IFSTYL( LTY, LWD )		!SET PLOTTING STYLUS
C
C  INITIALIZE THE PLOTTER...
C
     	IPB(IPL+1) = 27		!TURN ON PLOTTER IN CASE OF BYPASS MODE
     	IPB(IPL+2) = '.'
     	IPB(IPL+3) = '('
     	IPB(IPL+4) = '~'		!INITIALIZE
     	IPB(IPL+5) = '_'
     	IPB(IPL+6) = '}'
     	IPB(IPL+7) = '~'		!SET GRAPHIC LIMITS
     	IPB(IPL+8) = 'W'
     	IPL = IPL + 8
     	CALL IFINIT_MBP(0,0)
     	CALL IFINIT_MBP(16000,11400)
     	IPB(IPL+1) = '~'		!SET GRID SIZE
     	IPB(IPL+2) = 'S'
     	IPL = IPL + 2
     	CALL IFINIT_MBP(16000,11400)
     	IPB(IPL+1) = '~'		!LABEL FONTS (STANDARD,ALTERNATE)
     	IPB(IPL+2) = 'P'
     	IPL = IPL + 2
     	CALL IFINIT_MBP(0,5)
     	IPB(IPL+1) = '~'		!SET STRING TERMINATOR TO (ETX)=3
     	IPB(IPL+2) = '\'
     	IPL = IPL + 2
     	CALL IFINIT_MBN(3)
     	IF( IRR.GT.6 .AND. ARRAY(7).GT.0.0 ) THEN
     	    IRR = 4.0*ARRAY(7)
     	    IF( IRR.LT.1 .OR. IRR.GT.36 ) IRR = 36
     	    IPB(IPL+1) = '~'
     	    IPB(IPL+2) = 'V'
     	    IPL = IPL + 2
     	    CALL IFINIT_SBN(IRR)
    	ENDIF
     	IPL = IPL + 1
     	IPB(IPL) = '}'
999	RETURN
    	END

    	SUBROUTINE IFINIT_DUMP
C
C  PROGRAMMER: R.S. ZHOU,	SEP-1986
C
C	THIS ROUTINE DUMPS THE GRAPHIC BUFFER "IPB".
C
    	INCLUDE 'HP7221.CBS'

     	IF( IPL.EQ.0 ) GOTO 999
C
C  DUMP THE BUFFER...
C
     	WRITE(LUN,1) (IPB(I),I=1,IPL)
1	FORMAT(<IPL>A1$)
C
C  NULL THE BUFFER...
C
    	DO I=1, IPL
    	   IPB(I) = 0
    	ENDDO
     	IPL = 0

999	RETURN
    	END

     	SUBROUTINE IFINIT_MBP(NX,NY)
C
C	THIS ROUTINE PACKS A MULTIPLE BYTE PAIR (MAX=5 BYTES)
C
     	INCLUDE 'HP7221.CBS'
C
C  DETERMINE MAGNITUDE...
C
     	N=NX
     	IF(NX.LT.NY) N=NY
     	IF(N.LT.256) GO TO 10
     	IF(N.LT.2048) GO TO 4
     	IF(N.LT.16384) GO TO 5
     	N=16384
     	GO TO 5
 10	IF(N.GT.31) GO TO 3
     	IF(N.GT.3) GO TO 2
C
C  PACK SINGLE BYTE NUMBER...
C
  1	NP1=NY+96+4*NX
     	IPL=IPL+1
     	IPB(IPL)=NP1
     	GO TO 20
C
C  PACK TWO BYTE NUMBER...
C
  2	NX1=NX/2
     	NX2=NX-2*NX1
     	NP1=NX1+96
     	NP2=NY+32*NX2
     	IF(NP2.GT.31) GO TO 15
     	NP2=NP2+64
 15	IPB(IPL+1)=NP1
     	IPB(IPL+2)=NP2
     	IPL=IPL+2
     	GO TO 20
C
C  PACK THREE BYTE NUMBER...
C
  3	NX1=NX/16
     	NX2=NX-16*NX1
     	NY2=NY/64
     	NY3=NY-64*NY2
     	NP1=NX1+96
     	NP2=NY2+4*NX2
     	NP3=NY3
     	IF(NP2.LE.31) NP2=NP2+64
     	IF(NP3.LE.31) NP3=NP3+64
     	IPB(IPL+1)=NP1
     	IPB(IPL+2)=NP2
     	IPB(IPL+3)=NP3
     	IPL=IPL+3
     	GO TO 20
C
C  PACK FOUR BYTE NUMBER...
C
  4	NX1=NX/128
     	NXR=NX-128*NX1
     	NX2=NXR/2
     	NX3=NXR-2*NX2
     	NY3=NY/64
     	NY4=NY-64*NY3
     	NP1=96+NX1
     	NP2=NX2
     	IF(NP2.LE.31) NP2=NP2+64
     	NP3=NY3+32*NX3
     	IF(NP3.LE.31) NP3=NP3+64
     	NP4=NY4
     	IF(NP4.LE.31) NP4=NP4+64
     	IPB(IPL+1)=NP1
     	IPB(IPL+2)=NP2
     	IPB(IPL+3)=NP3
     	IPB(IPL+4)=NP4
     	IPL=IPL+4
     	GO TO 20
C
C  PACK FIVE BYTE NUMBER..
C
  5	NX1=NX/1024
     	NXR=NX-1024*NX1
     	NX2=NXR/16
     	NX3=NXR-16*NX2
     	NY3=NY/4096
     	NYR=NY-4096*NY3
     	NY4=NYR/64
     	NY5=NYR-64*NY4
     	NP1=96+NX1
     	NP2=NX2
     	IF(NP2.LE.31) NP2=NP2+64
     	NP3=NY3+4*NX3
     	IF(NP3.LE.31) NP3=NP3+64
     	NP4=NY4
     	IF(NP4.LE.31) NP4=NP4+64
     	NP5=NY5
     	IF(NP5.LE.31) NP5=NP5+64
     	IPB(IPL+1)=NP1
     	IPB(IPL+2)=NP2
     	IPB(IPL+3)=NP3
     	IPB(IPL+4)=NP4
     	IPB(IPL+5)=NP5
     	IPL=IPL+5

 20	RETURN
     	END

     	SUBROUTINE IFINIT_MBN(N)
C
C	THIS ROUTINE PACKS A MULTIPLE BYTE NUMBER (MAX=3 BYTES)
C
     	INCLUDE 'HP7221.CBS'

     	NN=N
     	IF(NN.LT.0) NN=0
     	IF(NN.LT.16) GO TO 1
     	IF(NN.LT.1024) GO TO 2
     	IF(NN.LT.32767) GO TO 3
     	NN=32767
     	GO TO 3
C
C  ONE BYTE FORMAT...
C
  1	NP1=NN+96
     	IPL=IPL+1
     	IPB(IPL)=NP1
     	GO TO 10
C
C  TWO BYTE FORMAT...
C
  2	J=2
     	NN1=NN/64
     	NN2=NN-64*NN1
     	GO TO 5
C
C  THREE BYTE FORMAT...
C
  3	J=3
     	NN1=NN/4096
     	NR=NN-4096*NN1
     	NN2=NR/64
     	NN3=NR-64*NN2
     	NP3=NN3
     	IF(NP3.LE.31) NP3=NP3+64
     	IPB(IPL+3)=NP3
  5	NP2=NN2
     	IF(NP2.LE.31) NP2=NP2+64
     	NP1=NN1+96
     	IPB(IPL+1)=NP1
     	IPB(IPL+2)=NP2
     	IPL=IPL+J

 10	RETURN
     	END

     	SUBROUTINE IFINIT_SBN(NN)
C
C	THIS ROUTINE PACKS A SINGLE BYTE NUMBER
C
     	INCLUDE 'HP7221.CBS'

     	NP=NN
     	IF(NP.LE.31) NP=NP+64
     	IPL=IPL+1
     	IPB(IPL)=NP
     	RETURN
     	END

     	SUBROUTINE IFINIT_MBA(ANGLE)
C
C	THIS ROUTINE PACKS A MULTIPLE BYTE ANGLE (MAX=3 BYTES)
C
     	INCLUDE 'HP7221.CBS'
C
C  ADJUST DECIMAL PART OF ANGLE FOR .00X RESOLUTION...
C
     	A=(ANGLE/360.-AINT(ANGLE/360.))*360.
     	A=AINT(A*1016.)
     	A=A/1016.
     	NBYTE=0
     	NP1=0
     	IF(A.LT.180.0) GO TO 1
     	NP1=8
     	A=A-180.0
C
C  ASSEMBLE BYTE 1...
C
  1	NA=(A/90.0)*16384
     	NA1=NA/4096
     	NP1=NP1+NA1+96
     	IPB(IPL+1)=NP1
     	NBYTE=1
     	NR=NA-4096*NA1
     	IF(NR.EQ.0) GO TO 4
C
C  ASSEMBLE BYTE 3 IF NECESSARY...
C
     	NA2=NR/64
     	NA3=NR-64*NA2
     	IF(NA3.EQ.0) GO TO 3
     	NP3=NA3
     	IF(NP3.LT.32) NP3=NP3+64
     	IPB(IPL+3)=NP3
     	NBYTE=3
C
C  ASSEMBLE BYTE 2...
C
  3	NP2=NA2
     	IF(NP2.LT.32) NP2=NP2+64
     	IPB(IPL+2)=NP2
     	IF(NBYTE.NE.3) NBYTE=2
  4	IPL=IPL+NBYTE

     	RETURN
     	END

     	SUBROUTINE IFINIT_PLOT( XORG, YORG )
C
C	THIS ROUTINE INITIALIZES A PLOT SESSION.
C
     	INCLUDE 'HP7221.CBS'

     	IXO = JXO + INT(XORG*FUNI)
     	IYO = JYO + INT(YORG*FUNI)
	IXL = IXO
	IYL = IYO
     	ILP = 0
     	IFN = 1
     	IFP = 0
     	JSK = 0
     	XANG = 0.0
     	YANG = 0.0
     	XCTH = 1.0
     	XSTH = 0.0
     	YCTH = 1.0
     	YSTH = 0.0
     	XORS = 0.0
     	YORS = 0.0
     	CALL IFDRAW(0.0,0.0,3)		!MOVE TO THE ORIGIN
     	RETURN
     	END

