     	PROGRAM TORSIO
C
C  PROGRAMMER:  R.S. ZHOU
C		BOSTON COLLEGE, 9/1988
C
C	THIS ROUTINE PLOTS TORSION ANGLES IN ARROWHEADS.
C
     	CHARACTER FNAME*32, LNAME*32, TEXT*80, TITLE*80
     	INTEGER IRES(999), PHI(999), PSI(999)
     	CHARACTER RESL(999)*1, PLOTID*32
     	INTEGER   CRT
     	DIMENSION HPAR(20)
C
C  PARSE THE ACTIVATING DCL COMMAND LINE...
C
C     	CRT = IF_DCL_PUT( 'TORSION' )
C     	IF( CRT.LT.0 ) GOTO 888
     	CALL IFINIT_SESSION
     	CRT = IFINIT_ITM(0)
C
C  GET THE PHIPSI FILENAME...
C
     	CALL IFANSI_CLEAR( 0 )
     	WRITE(6,2)
 2	FORMAT(/' *** This routine plots Phi/Psi angles in arrows ***'/)
 1     	NRES = 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('__oops: 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('__oops: 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
3000    WRITE(6,3001)
3001    FORMAT(' Enter plot origin [2.25,2.0] ',$)
        READ(5,5001,ERR=3000,END=999) XORG, YORG
        IF( XORG.LE.0.0 ) XORG = 2.25
        IF( YORG.LE.0.0 ) YORG = 2.00
4000	WRITE(6,5000)
5000	FORMAT(' Enter plot size (x,y) [6.0,4.0] ',$)
     	READ(5,5001,ERR=4000,END=999) XLENG,YLENG
5001	FORMAT(2F12.0)
     	IF( XLENG.LT.2.0 .OR. XLENG.GT.15.0 ) XLENG = 6.0
     	IF( YLENG.LT.1.0 .OR. YLENG.GT.10.0 ) YLENG = 4.0
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
        CALL IFINIT_LINE(3)
        CALL IFINIT_PLOT( XORG, YORG, FNAME )
        IF( IFCTRL_C() ) GOTO 776
C
C  OPEN A GRAPHICS SEGMENT TO SAVE THE PLOT...
C
        IF( CRT.EQ.2 ) THEN
            CALL IFSEG_PIVOT( 0.0, 0.0 )
            CALL IFSEG_OPEN( 15 )
        ENDIF
     	CALL IFSPEN( 1 )
     	IF(TITLE.NE.' ') CALL IFTEXT_CENT(0,7.,-1.,-1.,TITLE,.15,.15)
        IF( IFCTRL_C() ) GOTO 776
     	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 )
        IF( IFCTRL_C() ) GOTO 776
     	NTKY = 4
     	MTKY = -8
     	DELY = 360.0/YLENG
     	CALL IFAXIS( 4, XLENG, YLENG, 'Degrees', -180.0, 90.0, 
     +  0.0, NTKY, MTKY, 0, 1 )
        IF( IFCTRL_C() ) GOTO 776

     	CALL IFSPEN( IPEN )
     	DO I=1, NRES
           IF( IFCTRL_C() ) GOTO 776
     	   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_AUX( 0, XORG, YORG, LNAME )
  776	CALL IFINIT_PLOT_END
C
C   BRANCH AT LOCAL COMMANDS...
C
        IF( CRT.EQ.1 ) THEN
            CALL IFANSI_CURSOR(1,1)
            CALL IFANSI_CLEAR(6)
        ENDIF
  777   CALL IFANSI_VIDEO(47)
        WRITE(6,778)
  778   FORMAT(' TORSIO> Please press a desired function key:')
  779   WRITE(6,780)
  780   FORMAT(' TORSIO> ?>Help 0>Exit 1>Hardcopy 2>Layout',
     +' 3>Newfile 4>Dialogue $>DCL ...')
        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
            CALL IFCOPY( HPAR(1), PLOTID )
            CALL IFINIT_LINE( -3 )
        ELSE IF( KEY.EQ.-80 ) THEN			!hardocpy
            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
            CALL LAYOUT_AUX( 2, XORG, YORG, LNAME )
        ELSE IF( KEY.EQ.51 .OR. KEY.EQ.-82 ) THEN	!newfile
            GOTO 1
     	ELSE IF( KEY.EQ.52 ) THEN			!dialogue
     	    GOTO 3000
     	ELSE IF( KEY.EQ.13 .OR. KEY.EQ.32
     +  .OR. KEY.EQ.63 .OR. KEY.EQ.-81 ) THEN !help
     	    CALL IFMISC_HLP( 'TORSIO', '$ MENU', -2, 0 )
     	ELSE IF( KEY.EQ.36 .OR. KEY.EQ.-83 ) THEN	!dcl prompt
     	    CALL IFMISC_SPAWN( '$', 0 )
     	    CALL IFINIT_LINE( -3 )
     	ELSE
     	    CALL IFMISC_OOPS( 5, 0, 0, ' ' )
    	ENDIF
        GOTO 779
 999    CALL IFINIT_EXIT
 888    CALL EXIT
     	END

