     	PROGRAM TORSION
C
C  PROGRAMMER:  R.S. ZHOU
C		BOSTON COLLEGE, 9/1988
C
C	THIS ROUTINE PLOTS TORSION ANGLES IN ARROWHEADS.
C
     	CHARACTER KEY*2, FNAME*32, LNAME*32, TEXT*80, TITLE*80
     	INTEGER IRES(999), PHI(999), PSI(999)
     	CHARACTER RESL(999)*1
     	DIMENSION HPAR(20)
     	DATA HPAR/ 1.,1.,1.,1.,0,0,1.,1.,1.,0,5.,1.,1.,0,0,0,0,0,0,0 /
C
C  GET THE TYPE OF TERMINAL ON LINE...
C
     	ITERM = IFINIT(0)
     	IF( ITERM.EQ.0 ) GOTO 999
C
C  GET THE PHIPSI FILENAME...
C
1     	NRES = 0
     	CALL IFANSI_CLEAR( 0 )
     	WRITE(6,1000)
1000	FORMAT('$Enter Phi/Psi filename [.DIH] ')
     	READ(5,1001,END=999) FNAME
1001	FORMAT(A)
     	IF( FNAME.EQ.' ' ) GOTO 1

     	CALL IFMISC_DEFEXT( FNAME, '.DIH' )
     	OPEN( UNIT=1, FILE=FNAME, STATUS='OLD', FORM='FORMATTED',
     +        CARRIAGECONTROL='LIST', ERR=111 )
     	LNAME = FNAME
     	CALL IFMISC_NAMEXT( LNAME, '.DES' )
     	GOTO 100

111	WRITE(6,112)
112	FORMAT('__ERROR: can not find/open th file.')
     	CLOSE( UNIT=1 )
     	GOTO 1
C
C  GET THE DATA SET...
C
100	NCH = 0
555	READ(1,556,END=666) TEXT
556	FORMAT(A)
     	IF( TEXT.EQ.' ' ) GOTO 555
     	IF( NCH.EQ.1 ) GOTO 557
     	NPS = INDEX( TEXT, 'PHI' )
     	IF( NPS.EQ.0 ) GOTO 555
     	NPS = INDEX( TEXT, 'PSI' )
     	IF( NPS.EQ.0 ) GOTO 555
     	NCH = 1
     	GOTO 555

557	NRES = NRES + 1
     	READ(TEXT,558,ERR=222) RESL(NRES),IRES(NRES),PHI(NRES),PSI(NRES)
558	FORMAT(3X,A1,I3,3X,I4,8X,I4)
      	GOTO 555

222	NRES = NRES - 1
666	CLOSE( UNIT=1 )
     	IF( NRES.LT.2 ) THEN
     	    WRITE(6,667)
667	FORMAT('__ERROR: insufficient number of residues to plot.')
     	    GOTO 1
     	ENDIF
     	WRITE(6,1234) NRES
1234	FORMAT(' A total of ',I3,' residues have been read.')
C
C  SOLICIT THE GRAPH PARAMETERS...
C
4000	WRITE(6,5000)
5000	FORMAT('$Enter plot size [7.0x4.5] ')
     	READ(5,5001,ERR=4000,END=999) XLENG,YLENG
5001	FORMAT(2F12.0)
     	IF( XLENG.LT.2.0 .OR. XLENG.GT.15.0 ) XLENG = 7.0
     	IF( YLENG.LT.1.0 .OR. YLENG.GT.10.0 ) YLENG = 4.5
4001	WRITE(6,5002)
5002	FORMAT('$Enter arrowhead color number [3] ')
     	READ(5,5003,ERR=4001,END=999) IPEN
5003	FORMAT(I)
     	IF( IPEN.LT.1 .OR. IPEN.GT.8 ) IPEN = 3
4002	WRITE(6,5004)
5004	FORMAT(' Enter graph title below...'/'$>')
     	READ(5,5005,ERR=4002,END=999) TITLE
5005	FORMAT(A)
C
C  START THE PLOTTING...
C
     	XORG = 1.75
     	YORG = 1.75
     	CALL IFINIT_LINE(-3)
     	CALL IFINIT_PLOT( XORG, YORG, FNAME )
     	CALL IFSEG_PIVOT( XORG, YORG )
     	CALL IFSEG_OPEN( 15 )
     	CALL IFSPEN( 1 )
     	IF(TITLE.NE.' ') CALL IFTEXT_CENT(0,7.,-1.,-1.,TITLE,.15,.15)
     	NTKX = NRES/10
     	IF( 10*NTKX.LT.NRES ) NTKX = NTKX + 1
     	IF( NTKX.LT.1 ) NTKX = 1
     	MTKX = -9
     	IF( NTKX.GT.10) MTKX = -1
     	DELX = FLOAT(10*NTKX)/XLENG
     	CALL IFAXIS( 1, XLENG, YLENG, 'Residue Number', 0.0, 10.0,
     +  0.0, NTKX, MTKX, 0, 1 )
     	NTKY = 4
     	MTKY = -8
     	DELY = 360.0/YLENG
     	CALL IFAXIS( 4, XLENG, YLENG, 'Degrees', -180.0, 90.0, 
     +  0.0, NTKY, MTKY, 0, 1 )

     	CALL IFSPEN( IPEN )
     	DO I=1, NRES
     	   XPS = FLOAT(IRES(I))/DELX
     	   YPS = FLOAT(PSI(I)+180)/DELY
     	   YLN = FLOAT(PSI(I)-PHI(I))/DELY
     	   ANG = 90.0
     	   IF( PSI(I).LT.PHI(I) ) THEN
     	       YLN = -YLN
     	       ANG = -90.0
     	   ENDIF
     	   CALL IFARRH( 1, XPS, YPS, YLN, 0.03, 0.1, ANG )
     	ENDDO
     	CALL IFSEG_OPEN( 0 )
C
C  OVERLAY A LAYOUT IF AT ALL...
C
     	CALL LAYOUT_MAIN( 0, XORG, YORG, LNAME )
     	CALL IFINIT_PLOT_END
C
C   BRANCH AT LOCAL COMMANDS...
C
8000	IF( ITERM.EQ.2 ) THEN
     	    CALL IFMAC_MACROS( 1 )
     	    CALL IFINIT_FKEYS( 'EXIT', 'HARDCOPY', 'LAYOUT',
     + 'DIALOGUE', ' ', ' ', ' ', ' ' )
     	ENDIF
     	CALL IFANSI_VIDEO(47)
     	WRITE(6,8001)
8001	FORMAT(' COMMAND MENU: Exit/F1, Hardcopy/F2, Layout/F3,',
     +' Dialogue/F4'/'$INPUT> ')
     	READ(5,8002,END=999) KEY
8002	FORMAT(A)

     	IF( KEY.EQ.' ' ) GOTO 8000
     	CALL IFMISC_STRIP(KEY)
     	CALL IFMISC_UPPER(KEY)
     	IF( KEY.EQ.'EX' .OR. KEY.EQ.'F1' ) GOTO 999
     	IF( KEY.EQ.'HA' .OR. KEY.EQ.'F2' ) THEN
     	    CALL IFCOPY( HPAR(1) )
     	    GOTO 8000
     	ENDIF
     	IF( KEY.EQ.'LA' .OR. KEY.EQ.'F3' ) THEN
     	    CALL LAYOUT_MAIN( 2, XORG, YORG, LNAME )
     	    GOTO 8000
     	ENDIF
     	IF( KEY.EQ.'DI' .OR. KEY.EQ.'F4' ) GOTO 1
     	GOTO 8000

999	CALL IFINIT_EXIT
     	CALL EXIT
     	END
