



      SUBROUTINE SC9001

















      IMPLICIT      NONE







      CALL TEXSECT ('?M0301')



      CALL SC9002



      CALL SC9003



      CALL SC9020





      RETURN
      END   
      SUBROUTINE SC9002

















      IMPLICIT      NONE
      LOGICAL       HASWATER







      IF (HASWATER()) THEN



         CALL WIFPUSHPPP (590,1)



         CALL SC9004



         CALL SC9005



         CALL SC9006



         CALL WIFPOPPPP (590)
      ELSE



         CALL PDBMES ('NOWAT')
      END IF





      RETURN
      END   
      SUBROUTINE SC9003

















      IMPLICIT      NONE
      LOGICAL       CHKTODO, DON







      CALL WIFPUSHPPP (590,1)

      CALL WIFPOPPPP (590)



      IF (CHKTODO('HNQ')) THEN
         CALL SC9008
      END IF





      RETURN
      END   
      SUBROUTINE SC9004

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'CNTINC.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       MAXPAI
      PARAMETER     (MAXPAI=2*MAXAT)
      INTEGER       ISOUP, IPAI, H2OPAI(2,MAXPAI), H2OES0, H2OESZ, 
     +              IAA, IAT, JAT, ICONT, NUMPAI
      LOGICAL       DONE, WIFISW, WIFIRW, HASWATER, FIRST
      PARAMETER     (H2OES0=20001)
      EQUIVALENCE   (H2OPAI(1,1),ESPACE(H2OES0))
      PARAMETER     (H2OESZ=H2OES0+2*MAXPAI)







      IF (.NOT.HASWATER()) THEN
         CALL PDBMES ('NOWAT')
         GOTO 998
      END IF



      CALL GVSBTF (USEAT,MAXAT)
      CALL ESPSET (H2OES0,H2OESZ-1,6)
      CALL GVSIT0 (H2OPAI,2*MAXPAI)
      CALL GVSRT0 (ATTVAL,MAXAT)
      NUMPAI=0
      WIFPAR(442)=0
      FIRST=.TRUE.



      CALL WIFPUSHPPP (590,1)



      CALL CDB013 ('H2O1NOK','H2O1NEU','H2O1OK')



      CALL CDB014 (1,'FLOATING','BAD')



      CALL ENVTOT



      DO 100 ISOUP=1,NUMSOU
         IF (.NOT.WIFISW(ISOUP)) GOTO 100



         IAA=SOUADM(1,ISOUP)
         DO 90 IAT=PNTAAS(SOUADM(1,ISOUP)),PNTAAE(SOUADM(2,ISOUP))





            CALL FCONAT (IAT,5.0)



            DO 80 ICONT=1,NUMCONIAT
               JAT=CONIAT(ICONT)
               IF (.NOT.WIFIRW(ATIAA(JAT))) THEN
                  USEAT(IAT)=.TRUE.
               ELSE
                  IF (NUMPAI.LT.MAXPAI) THEN
                     NUMPAI=NUMPAI+1
                     H2OPAI(1,NUMPAI)=IAT
                     H2OPAI(2,NUMPAI)=JAT
                  ELSE
                     CALL GVSTTE ('?M0438')
                     GOTO 998
                  END IF
               END IF
   80       CONTINUE
   90    CONTINUE
  100 CONTINUE



      CALL WIFPOPPPP (590)



  110 CONTINUE
      DONE=.FALSE.
      DO 120 IPAI=1,NUMPAI
         IF (USEAT(H2OPAI(1,IPAI))) THEN
            IF (.NOT.USEAT(H2OPAI(2,IPAI))) THEN
               USEAT(H2OPAI(2,IPAI))=.TRUE.
               DONE=.TRUE.
            END IF
         END IF
  120 CONTINUE
      IF (DONE) GOTO 110



      DO 210 ISOUP=1,NUMSOU
         IF (.NOT.WIFISW(ISOUP)) GOTO 210
         DO 200 IAT=PNTAAS(SOUADM(1,ISOUP)),PNTAAE(SOUADM(2,ISOUP))
            ATCOLR(IAT)=20
            IF (.NOT.USEAT(IAT)) THEN
               ATTVAL(IAT)=1.0
               ATCOLR(IAT)=120
               CALL PDBC92 (0,IAT)
               WIFPAR(442)=WIFPAR(442)+1



               IF (WIFPAR(450).NE.0.AND.WIFPAR(451).NE.0) THEN
                  IF (FIRST) THEN
                     FIRST=.FALSE.
                     CALL GVSTT6 ('?M4861')
                     CALL GVSTT6 ('?M4862')
                     CALL GVSTTU (WIFPAR(451),'?M4861')
                     CALL GVSTTU (WIFPAR(451),'?M4862')
                  END IF
                  CALL GVSFMT ('?FMT1414')
                  CALL GVSCTB (LINE,80)
                  WRITE (LINE,FMT,ERR=999) IAA,ORGNAM(IAT),NUMNAM(IAA),
     +             CHANAM(IAA)
                  CALL GVSTT6 (LINE)
                  CALL GVSTTU (WIFPAR(451),LINE)
               END IF
            END IF
  200    CONTINUE
  210 CONTINUE



      CALL CDB012



      CALL ESPKIL (H2OES0,H2OESZ-1,6,'SC9004')
      GOTO 998



 999  CONTINUE
      CALL GVSEIW ('SC9004')



  998 CONTINUE


      RETURN
      END
      SUBROUTINE SC9005



















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'SYMINC.INC'
      INCLUDE       'PDBINC.INC'
      INTEGER       IAT, ISOUP, ISYM, SYMNEG, JAT, JSYM, IUNIT, IERR,
     +              IAA, ISERIAL
      REAL          TMP(3), ANYDST, OWNDST
      LOGICAL       HASWATER, WIFISW, GVFISF, GOTFILE
      CHARACTER*4   LTC4
      CHARACTER*6   HETATM







      IUNIT=0
      CALL GVSCLF (3)
      CALL GVSCLF (4)
      CALL GVSSTX (HETATM,'HETATM')
      CALL GVSCTB (LTC4,4)



      IF (.NOT.SYMHAS) THEN
         CALL PDBMES ('WATNOSYM')
         GOTO 998
      END IF



      IF (.NOT.HASWATER()) THEN
         CALL PDBMES ('NOWAT')
         GOTO 998
      END IF



      CALL CDB013 ('WATFARN','WATFARNEU','WATFAROK')



      CALL CDB014 (1,'FAR','POOR')



      IF (GVFISF('?M4731')) CALL GVSDEL ('?M4731')
      IF (.NOT.GOTFILE(IUNIT,'?M4731')) GOTO 998



      CALL ENVNOWAT
      CALL WIFPUSHPPP (590,1)



      ISERIAL=0
      DO 100 ISOUP=1,NUMSOU
         IF (.NOT.WIFISW(ISOUP)) GOTO 100



         DO 90 IAT=PNTAAS(SOUADM(1,ISOUP)),PNTAAE(SOUADM(2,ISOUP))
            IAA=ATIAA(IAT)
            CALL SC9011 (IAT,OWNDST,ANYDST,JAT,JSYM)



            IF (JSYM.GT.1.AND.OWNDST.GT.ANYDST+1.0) THEN
               ISYM=SYMNEG(JSYM)
               TMP(1)=XCOORD(IAT)
               TMP(2)=YCOORD(IAT)
               TMP(3)=ZCOORD(IAT)
               CALL GVSMPV (TMP,SYMMAT(1,1,ISYM),SYMVEC(1,ISYM))
               CALL PDBC92A (0,IAT)
               WRITE (LINE2,PDBFMT,ERR=999) LINE92(1:28),TMP(1),
     +          TMP(2),TMP(3)
               CALL PDBC93 (LINE2)



               CALL GVSWAP (XCOORD(IAT),TMP(1))
               CALL GVSWAP (YCOORD(IAT),TMP(2))
               CALL GVSWAP (ZCOORD(IAT),TMP(3))
               USEAT(IAT)=.FALSE.
               CALL WIF509 (IUNIT,IAA,IAT,ISERIAL,HETATM,ONESA3(IAA),
     +          NUMNAM(IAA),LTC4,IERR)
               CALL GVSWAP (XCOORD(IAT),TMP(1))
               CALL GVSWAP (YCOORD(IAT),TMP(2))
               CALL GVSWAP (ZCOORD(IAT),TMP(3))
            END IF
 90      CONTINUE
 100  CONTINUE



      CALL CDB012
      CALL WIFPOPPPP (590)
      GOTO 998



  999 CONTINUE
      CALL GVSEIW ('SC9005')



  998 CONTINUE
      IF (IUNIT.GT.0) CALL GVSCLF (IUNIT)


      RETURN
      END
      SUBROUTINE SC9006



















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'PDBINC.INC'
      INTEGER       MAXNUM
      PARAMETER     (MAXNUM=250)
      INTEGER       IAA, NUM, JATARR(MAXNUM), JATSYM(MAXNUM), ICON,
     +              SYMNEG, IAT, JAT, JSYM, ISTEP, KAT
      REAL          JATDST(MAXNUM), HBOQVAL, V
      LOGICAL       WIFIRW, HASWATER







      IF (.NOT.HASWATER()) THEN
         CALL PDBMES ('NOWAT')
         GOTO 998
      END IF



      CALL WIFPUSHPPP (590,1)



      CALL WIFPUSHPPP (1509,0)



      CALL HBO010



      CALL CDB013 ('H2ONOHBO','H2OHBO','H2OHBOOK')
      CALL CDB014 (1,'LOOSE','POOR')
      CALL GVSRT0 (ATTVAL,NUMATF)



      CALL ENVTOT



      FIXLST(12)=0.0
      DO 100 IAA=NUMAAF+1,NUMAAT
         IF (.NOT.WIFIRW(IAA)) GOTO 100
         ISTEP=1
         IF (WIFPAR(84).EQ.1) ISTEP=3
         DO 90 IAT=PNTAAS(IAA),PNTAAE(IAA),ISTEP
            CALL FCONATI (IAT,4.0,JATARR,JATSYM,JATDST,NUM,MAXNUM)
            V=0.0
            DO 60 ICON=1,NUM
               JAT=JATARR(ICON)
               JSYM=JATSYM(ICON)
               V=V+HBOQVAL(IAT,JAT,JSYM)+HBOQVAL(JAT,IAT,SYMNEG(JSYM))
               IF (V.GT.0.0) GOTO 90
   60       CONTINUE
            IF (ISTEP.GT.1) THEN
               KAT=IAT+1
               CALL FCONATI (KAT,4.0,JATARR,JATSYM,JATDST,NUM,MAXNUM)
               V=0.0
               DO 70 ICON=1,NUM
                 JAT=JATARR(ICON)
                 JSYM=JATSYM(ICON)
                 V=V+HBOQVAL(KAT,JAT,JSYM)+HBOQVAL(JAT,KAT,SYMNEG(JSYM))
                 IF (V.GT.0.0) GOTO 90
   70          CONTINUE
               KAT=IAT+2
               CALL FCONATI (KAT,4.0,JATARR,JATSYM,JATDST,NUM,MAXNUM)
               V=0.0
               DO 80 ICON=1,NUM
                 JAT=JATARR(ICON)
                 JSYM=JATSYM(ICON)
                 V=V+HBOQVAL(KAT,JAT,JSYM)+HBOQVAL(JAT,KAT,SYMNEG(JSYM))
                 IF (V.GT.0.0) GOTO 90
   80          CONTINUE
            END IF
            CALL PDBC92 (0,IAT)
            ATTVAL(IAT)=1.0
            FIXLST(12)=FIXLST(12)+1.0
   90    CONTINUE
  100 CONTINUE



      CALL CDB012
      CALL WIFPOPPPP (1509)
      CALL WIFPOPPPP (590)



  998 CONTINUE


      RETURN
      END
      SUBROUTINE SC9007

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'PDBINC.INC'
      INCLUDE       'RESULT.INC'
      INTEGER       IAA, IAT, MX2, NUM, ICON, JAT
      PARAMETER     (MX2=200)
      INTEGER       CONIAT(MX2), CONSYM(MX2)
      REAL          CONDST(MX2), ELMCHG
      LOGICAL       T54EQUAL
      CHARACTER*3   CHAR3
      CHARACTER*4   CHAR4







      CALL ENVINI
      GENIVL(11)=0
      GENIVL(14)=0



      CALL CDB013 ('BHBONOK','BHBONEU','BHBOOK')



      CALL CDB014 (1,'NON-HBO','POOR')
      CALL CDB014 (2,'MIS-HBO','BAD')



      CALL ENVNOWAT
      DO 20 IAA=1,NUMAAF
         CALL WIF041 (IAA)
   20 CONTINUE



      CALL GVSBTF (USEAT,MAXAT)
      CALL GVSRT0 (ATTVAL,MAXAT)



      CALL HBO008 (USEAT)



      CALL ENVTOT



      DO 40 IAA=1,NUMAAF
         CALL GVSSTX (CHAR4,ONESA3(IAA))
         CALL GVSSHL (CHAR4)
         CALL GVSSTX (CHAR3,CHAR4(1:3))
         DO 30 IAT=PNTAAS(IAA),PNTAAE(IAA)
            IF (USABLE(IAT).AND.ACCESS(IAT).LT.0.1.AND.ISATOK(IAT))
     +       GENIVL(14)=GENIVL(14)+1
            IF (.NOT.USEAT(IAT).AND.USABLE(IAT).AND.
     +           ISATOK(IAT).AND.ACCESS(IAT).LT.0.1.AND.
     +           .NOT.T54EQUAL(ONESAT(IAT),' SG ').AND.
     +           .NOT.T54EQUAL(ONESAT(IAT),' SD ')) THEN



               IF ((CHAR3.EQ.'ARG'.OR.CHAR3.EQ.'LYS'.OR.
     +              CHAR3.EQ.'GLU'.OR.CHAR3.EQ.'ASP'.OR.
     +              CHAR3.EQ.'GLN'.OR.CHAR3.EQ.'ASN'.OR.
     +              CHAR3.EQ.'HIS').AND.PINIAA(IAT).GT.4) THEN
                  ATTVAL(IAT)=2.0
               ELSE
                  ATTVAL(IAT)=1.0
               END IF



               IF (ATTVAL(IAT).GT.0.0) THEN
                  CALL FCONATI (IAT,0.3,CONIAT,CONSYM,CONDST,NUM,MX2)
                  DO 50 ICON=1,NUM
                     JAT=CONIAT(ICON)
                     IF (ELMCHG(JAT).GT.+0.5) THEN
                        CALL DIF015 (6,'?R00281',IAT)
                        ATTVAL(IAT)=0.0
                        GOTO 30
                     END IF
 50               CONTINUE
               END IF



               CALL PDBC92 (IAA,IAT)
               GENIVL(11)=GENIVL(11)+1
               CALL DIF013 (6,IAT,ATTVAL(IAT))
            END IF
 30      CONTINUE

 40   CONTINUE



      CALL CDB012





      RETURN
      END
      SUBROUTINE SC9007A


















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'RESULT.INC'
      INTEGER       IAA, IAT, MX2, NUM, ICON, JAT
      PARAMETER     (MX2=200)
      INTEGER       CONIAT(MX2), CONSYM(MX2)
      REAL          CONDST(MX2), ELMCHG
      LOGICAL       T54EQUAL
      CHARACTER*3   CHAR3
      CHARACTER*4   CHAR4







      CALL ENVINI
      GENIVL(11)=0
      GENIVL(14)=0



      CALL ENVNOWAT
      DO 20 IAA=1,NUMAAF
         CALL WIF041 (IAA)
   20 CONTINUE



      CALL GVSBTF (USEAT,MAXAT)
      CALL GVSRT0 (ATTVAL,MAXAT)



      CALL GVSBTF (USEAT,NUMATF)
      CALL HBO001F



      CALL ENVTOT



      DO 40 IAA=1,NUMAAF
         CALL GVSSTX (CHAR4,ONESA3(IAA))
         CALL GVSSHL (CHAR4)
         CALL GVSSTX (CHAR3,CHAR4(1:3))
         DO 30 IAT=PNTAAS(IAA),PNTAAE(IAA)
            IF (USABLE(IAT).AND.ACCESS(IAT).LT.0.1.AND.ISATOK(IAT))
     +       GENIVL(14)=GENIVL(14)+1
            IF (.NOT.USEAT(IAT).AND.USABLE(IAT).AND.
     +           ISATOK(IAT).AND.ACCESS(IAT).LT.0.1.AND.
     +           .NOT.T54EQUAL(ONESAT(IAT),' SG ').AND.
     +           .NOT.T54EQUAL(ONESAT(IAT),' SD ')) THEN



               IF ((CHAR3.EQ.'ARG'.OR.CHAR3.EQ.'LYS'.OR.
     +              CHAR3.EQ.'GLU'.OR.CHAR3.EQ.'ASP'.OR.
     +              CHAR3.EQ.'GLN'.OR.CHAR3.EQ.'ASN'.OR.
     +              CHAR3.EQ.'HIS').AND.PINIAA(IAT).GT.4) THEN
                  ATTVAL(IAT)=2.0
               ELSE
                  ATTVAL(IAT)=1.0
               END IF



               IF (ATTVAL(IAT).GT.0.0) THEN
                  CALL FCONATI (IAT,0.3,CONIAT,CONSYM,CONDST,NUM,MX2)
                  DO 50 ICON=1,NUM
                     JAT=CONIAT(ICON)
                     IF (ELMCHG(JAT).GT.+0.5) THEN
                        CALL DIF015 (6,'?R00281',IAT)
                        ATTVAL(IAT)=0.0
                        GOTO 30
                     END IF
 50               CONTINUE
               END IF



               CALL PDBC92 (IAA,IAT)
               GENIVL(11)=GENIVL(11)+1
               CALL DIF013 (6,IAT,ATTVAL(IAT))
            END IF
 30      CONTINUE
         CALL SHOIAA (IAA)
 40   CONTINUE



      CALL CDB012





      RETURN
      END
      SUBROUTINE SC9008




















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INTEGER       IERR







      CALL WIFPUSHPPP (590,1)



      CALL SYMIPG (IERR,' ')
      CALL SYMSD (2.8)
      CALL WIF041X
      CALL SYMSD (2.8)
      CALL WIFPUSHPPP (592,WIFPAR(592))
      CALL HB2INI



      CALL HB2RED
      CALL WIFPOPPPP (592)



      CALL SYMIPG (IERR,' ')
      CALL SYMSD (2.8)
      CALL SC9009



      CALL SYMIPG (IERR,' ')
      CALL SYMSD (2.8)
      CALL SC9010



      CALL SYMIPG (IERR,' ')
      CALL SYMSD (2.8)
      CALL HB2LUH



      CALL SYMIPG (IERR,' ')
      CALL SYMSD (2.8)
      CALL HB2LUA



      CALL SYMIPG (IERR,' ')
      CALL SYMSD (2.8)
      CALL HB2RES
      CALL WIFPOPPPP (590)




      
      RETURN
      END
      SUBROUTINE SC9009



















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'PDBINC.INC'
      INCLUDE       'HB2INC.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       IAMB, IAA
      LOGICAL       ESPBAD, ANY







      IF (.NOT.HB2IDN.OR.ESPBAD(HB2ES0,HB2ESZ,1)) THEN
         CALL GVSTT6 ('?NEWT607')
         GOTO 998
      END IF



      ANY=.FALSE.
      DO 50 IAA=1,NUMAAF
         IF (RESTYP(IAA).EQ.7.OR.RESTYP(IAA).EQ.12.OR.
     +        RESTYP(IAA).EQ.14) THEN
            ANY=.TRUE.
         END IF
   50 CONTINUE
      IF (.NOT.ANY) THEN
         CALL PDBMES ('R00011')
         GOTO 998
      END IF



      WIFPAR(443)=0
      CALL GVSRT0 (PRPVAL,MAXAA)



      CALL CDB013 ('HNQERR','HNQNEU','HNQOK')
      CALL CDB014 (1,'HBONDFLIP','POOR')
      CALL CDB014 (2,'HBONDFLIP','BAD')



      DO 100 IAMB=1,NAMB1
         IAA=HB2IAA(IAMB)
         IF (HB2POS(IAMB).NE.1) THEN
            IF (HB2FLX(IAA)) THEN
               PRPVAL(IAA)=1.0
               CALL PDBS51 (IAA,1.0,0.1,1.0)
               FIXLST(11)=1.0
            ELSE
               PRPVAL(IAA)=2.0
               CALL PDBS51 (IAA,2.0,0.1,1.0)
               FIXLST(11)=1.0
            END IF
            CALL PDBC92 (IAA,0)
            WIFPAR(443)=WIFPAR(443)+1
         END IF
 100  CONTINUE



      CALL CDB012



  998 CONTINUE


      RETURN
      END
      SUBROUTINE SC9010




















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'ESPACE.INC'
      INCLUDE       'HB2INC.INC'
      INTEGER       HISTYP, IHTYP, IAA, IAT, JAT, N, ICON, IERR
      REAL          HISDST(5,3), HISSIG(5,3), RMSVAL(3), D, SCORE,
     +              WIFDST
      CHARACTER*5   NAM(3)
      SAVE          HISDST, HISSIG, NAM
      DATA  NAM    /'HIS-D','HIS-E','HIS-H'/
      DATA  HISDST /1.378,1.356,1.345,1.382,1.319,
     +              1.371,1.356,1.319,1.374,1.345,
     +              1.378,1.354,1.321,1.374,1.321/
      DATA  HISSIG /0.011,0.011,0.020,0.030,0.013,
     +              0.017,0.011,0.013,0.021,0.020,
     +              0.011,0.011,0.010,0.011,0.010/

      IF (.NOT.HB2IDN) GOTO 998







      CALL PDBTAB ('?TABHISA')



      CALL PDBTCH ('?TCH012')



      CALL PDBSTL (WIFPAR(593)*2)



      DO 100 IAA=1,NUMAAF
         IF (RESTYP(IAA).EQ.7) THEN
            HISTYP=0
            IAT=PNTAAS(IAA)+6
            IF (.NOT.ISATOK(IAT)) GOTO 100
            IF (ATDON(IAT).LT.1) THEN
               CALL DIF015 (6,'?R00288',IAT)
               GOTO 100
            END IF
            IF (NUXYZH(ATDON(IAT)).EQ.1) HISTYP=HISTYP+1
            IAT=PNTAAS(IAA)+9
            IF (.NOT.ISATOK(IAT)) GOTO 100
            IF (ATDON(IAT).LT.1) THEN
               CALL DIF015 (6,'?R00288',IAT)
               GOTO 100
            END IF
            IF (NUXYZH(ATDON(IAT)).EQ.1) HISTYP=HISTYP+2
            IF (HISTYP.EQ.0) GOTO 100
            DO 90 IHTYP=1,3
               RMSVAL(IHTYP)=0.0
               N=0
               DO 80 ICON=6,10
                  IAT=PNTAAS(IAA)+MUTCON(1,ICON,7)-1
                  JAT=PNTAAS(IAA)+MUTCON(2,ICON,7)-1
                  IF (.NOT.ISATOK(IAT)) GOTO 80
                  IF (.NOT.ISATOK(JAT)) GOTO 80
                  D=WIFDST(IAT,JAT)
                  SCORE=(D-HISDST(ICON-5,IHTYP))/HISSIG(ICON-5,IHTYP)
                  RMSVAL(IHTYP)=RMSVAL(IHTYP)+SCORE**2
                  N=N+1
 80            CONTINUE
               IF (N.LT.5) GOTO 100
               RMSVAL(IHTYP)=SQRT(RMSVAL(IHTYP)/N)
 90         CONTINUE



            IHTYP=1
            IF (RMSVAL(2).LT.RMSVAL(IHTYP)) IHTYP=2
            IF (RMSVAL(3).LT.RMSVAL(IHTYP)) IHTYP=3



            CALL PDBC92A (IAA,0)
            CALL GVSFMT ('?FMTHISA')
            IF (IHTYP.NE.HISTYP) THEN
               WRITE (LINE2,FMT,ERR=999) LINE92(1:23),NAM(IHTYP),
     +          RMSVAL(IHTYP),NAM(HISTYP),RMSVAL(HISTYP)
            ELSE
               WRITE (LINE2,FMT,ERR=999) LINE92(1:23),
     +          NAM(IHTYP),RMSVAL(IHTYP)
            END IF
            CALL PDBC93 (LINE2)
         END IF
 100  CONTINUE
      CALL PDBTND (IERR)
      IF (IERR.EQ.0) THEN
         CALL PDBMES ('TABHISE')
      END IF
      GOTO 998



  999 CONTINUE
      CALL GVSEIW ('SC9010')



  998 CONTINUE


      RETURN
      END
      SUBROUTINE SC9011 (IAT,OWNDST,ANYDST,JAT,JSYM)



















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       MX2
      PARAMETER     (MX2=15000)
      INTEGER       CONIAT(MX2), CONSYM(MX2), NUM, IAT, JAT, JSYM, ICON
      REAL          CONDST(MX2), OWNDST, ANYDST, RAD, GVFMIN, XYZMIN(3),
     +              XYZMAX(3), XYZCEN(3), MAXRAD
      EQUIVALENCE   (CONIAT,ESPACE(10001))
      EQUIVALENCE   (CONSYM,ESPACE(10001+MX2))
      EQUIVALENCE   (CONDST,ESPACE(10001+2*MX2))







      CALL ESPSET (10001,10000+3*MX2,53)
      ANYDST=999.0
      OWNDST=999.0
      JSYM=0
      JAT=0



      CALL WIFMMX (1,NUMAAF,XYZMIN,XYZMAX,XYZCEN)
      MAXRAD=MAX(XYZMAX(1)-XYZMIN(1),XYZMAX(2)-XYZMIN(2))
      MAXRAD=MAX(MAXRAD,XYZMAX(3)-XYZMIN(3))
      MAXRAD=MIN(MAXRAD,50.0)



      WIFPAR(590)=0
      RAD=10.0
   10 CONTINUE
      CALL FCONATI (IAT,RAD,CONIAT,CONSYM,CONDST,NUM,MX2)
      IF (NUM.EQ.0) THEN
         IF (RAD.GT.MAXRAD) THEN
            CALL GVSTTE ('?R00294')
            GOTO 998
         ELSE
            RAD=RAD+10.0
            GOTO 10
         END IF
      ELSE IF (NUM.EQ.MX2) THEN
         RAD=RAD*0.9
         GOTO 10
      END IF
      OWNDST=GVFMIN(CONDST,NUM)



      WIFPAR(590)=1
      RAD=10.0
   20 CONTINUE
      CALL FCONATI (IAT,RAD,CONIAT,CONSYM,CONDST,NUM,MX2)
      IF (NUM.EQ.0) THEN
         IF (RAD.GT.MAXRAD) THEN
            CALL GVSTTE ('?R00294')
            GOTO 998
         ELSE
            RAD=RAD+10.0
            GOTO 20
         END IF
      ELSE IF (NUM.EQ.MX2) THEN
         RAD=RAD*0.9
         GOTO 20
      END IF



      DO 30 ICON=1,NUM
         IF (CONDST(ICON).LT.ANYDST) THEN
            JSYM=CONSYM(ICON)
            JAT=CONIAT(ICON)
            ANYDST=CONDST(ICON)
         END IF
   30 CONTINUE



      CALL ESPKIL (10001,10000+3*MX2,53,'SC9011')



  998 CONTINUE


      RETURN
      END
      SUBROUTINE SC9012

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'SYMINC.INC'
      INCLUDE       'PDBINC.INC'
      INTEGER       IAT, ISOUP, ISYM, SYMNEG, JAT, JSYM, IUNIT, IERR,
     +              IAA, ISERIAL
      REAL          TMP(3), ANYDST, OWNDST
      LOGICAL       GVFISF, GOTFILE, WIFIRM
      CHARACTER*4   LTC4
      CHARACTER*6   HETATM







      IUNIT=0
      CALL GVSSTX (HETATM,'HETATM')
      CALL GVSCTB (LTC4,4)



      IF (.NOT.SYMHAS) THEN
         CALL PDBMES ('IONNOSYM')
         GOTO 998
      END IF



      CALL CDB013 ('IONFARN','IONFARNEU','OKFAROK')



      CALL CDB014 (1,'FAR','POOR')



      IF (GVFISF('?M4731')) CALL GVSDEL ('?M4731')
      IF (.NOT.GOTFILE(IUNIT,'?M4731')) GOTO 998



      CALL ENVNOWAT
      CALL WIFPUSHPPP (590,1)



      ISERIAL=0
      DO 100 ISOUP=1,NUMSOU
         IF (.NOT.WIFIRM(SOUADM(1,ISOUP))) GOTO 100



         DO 90 IAT=PNTAAS(SOUADM(1,ISOUP)),PNTAAE(SOUADM(2,ISOUP))
            IAA=ATIAA(IAT)
            CALL SC9011 (IAT,OWNDST,ANYDST,JAT,JSYM)



            IF (JSYM.GT.1.AND.OWNDST.GT.ANYDST+1.0) THEN
               ISYM=SYMNEG(JSYM)
               TMP(1)=XCOORD(IAT)
               TMP(2)=YCOORD(IAT)
               TMP(3)=ZCOORD(IAT)
               CALL GVSMPV (TMP,SYMMAT(1,1,ISYM),SYMVEC(1,ISYM))
               CALL PDBC92A (0,IAT)
               WRITE (LINE2,PDBFMT,ERR=999) LINE92(1:28),TMP(1),
     +          TMP(2),TMP(3)
               CALL PDBC93 (LINE2)



               CALL GVSWAP (XCOORD(IAT),TMP(1))
               CALL GVSWAP (YCOORD(IAT),TMP(2))
               CALL GVSWAP (ZCOORD(IAT),TMP(3))
               USEAT(IAT)=.FALSE.
               CALL WIF509 (3,IAA,IAT,ISERIAL,HETATM,ONESA3(IAA),
     +          NUMNAM(IAA),LTC4,IERR)
               CALL GVSWAP (XCOORD(IAT),TMP(1))
               CALL GVSWAP (YCOORD(IAT),TMP(2))
               CALL GVSWAP (ZCOORD(IAT),TMP(3))
            END IF
 90      CONTINUE
 100  CONTINUE



      CALL CDB012
      CALL WIFPOPPPP (590)
      GOTO 998



  999 CONTINUE
      CALL GVSEIW ('SC9012')



  998 CONTINUE
      IF (IUNIT.GT.0) CALL GVSCLF (IUNIT)


      RETURN
      END
      SUBROUTINE SC9020

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'SUGARS.INC'
      INCLUDE       'DATBAS.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       ISOUP, IAA, ION, IERR, IUNIT
      REAL          AVER, SDEV
      LOGICAL       WIFIRM, HASION, FOUND, FNDONE, HASWATER, WIFISW,
     +              GOTFILE







      CALL WIFPUSHPPP (590,1)
      CALL WIFPUSHPPP (482,4)
      CALL WIFPUSHPPP (279,2)
      CALL GVSSTX (LINE3,FILNMS(62))
      CALL WALSTRIPFILNAM (LINE3,IERR)
      CALL GVSCPC (LINE3,'_ION.OUT')
      IF (.NOT.GOTFILE(IUNIT,LINE3)) GOTO 998






      CALL CDB013 ('SOUPBAD',' ','SOUPOK')
      DO 10 ISOUP=1,NUMSOU
         CALL SOUTEX (ISOUP,BIGLIN,IERR)
         CALL PDBC93 (BIGLIN)
 10   CONTINUE
      CALL CDB012



      CALL SC9022



      IF (.NOT.HASION()) THEN
         CALL PDBMES ('OEPSNOION')
         GOTO 100
      END IF



      CALL CDB013 ('SIO1ERR','SIO1NEU','SIO1GOO')



      DO 20 ISOUP=1,NUMSOU
         IAA=SOUADM(1,ISOUP)
         ION=PNTAAS(IAA)
         IF (.NOT.WIFIRM(IAA)) GOTO 20
         CALL SC9026 (ION)
 20   CONTINUE



      CALL CDB012



      CALL SC9027



      FOUND=.FALSE.



      CALL ENVTOT
      CALL SYMINI
      CALL SYMSPG (IERR)
      CALL SYMNEA



      CALL CDB013 ('ION1ERR','ION1NEU','ION1GOO')



      DO 30 ISOUP=1,NUMSOU
         IAA=SOUADM(1,ISOUP)
         ION=PNTAAS(IAA)



         IF (.NOT.WIFIRM(IAA)) GOTO 30



         CALL SC9021 (IUNIT,ION,FNDONE,IERR)
         FOUND=FOUND.OR.FNDONE
   30 CONTINUE



      CALL CDB012



 100  CONTINUE
      IF (.NOT.HASWATER()) THEN
         CALL PDBMES ('NOWAT')
         CALL GVSTT6 ('?M2086')
         IF (WIFPAR(279).EQ.0) CALL SHOSOU
         GOTO 998
      END IF



      CALL CDB013 ('WIO1ERR','WIO1NEU','WIO1GOO')




      CALL SC9040 (AVER,SDEV)
      DO 40 ISOUP=1,NUMSOU
         IF (WIFISW(ISOUP)) CALL SC9024 (IUNIT,AVER,SDEV,ISOUP)
 40   CONTINUE



      CALL CDB012



 998  CONTINUE
      CALL WIFPOPPPP (279)
      CALL WIFPOPPPP (482)
      CALL WIFPOPPPP (590)
      IF (IUNIT.NE.0) CALL GVSCLF (IUNIT)


      RETURN
      END   
      SUBROUTINE SC9021 (IUNIT,ION,FOUND,IERR)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'DATBAS.INC'
      INCLUDE       'CNTINC.INC'
      INCLUDE       'SUGARS.INC'
      INCLUDE       'PDBINC.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       ION, IERR, NUMTYP, IMIN, ITYPE, IAA, GVFLEN,
     +              NFND, IUNIT, ISLF
      REAL          ONE, SELF, BEST, VION(50), VSCO(50), WTOT, 
     +              SCODIF, ION_VALENCY
      LOGICAL       WIFIRH, WIFIRO, FOUND, HASVIO, SPOS, DOION
      CHARACTER*2   CHAR2, CROW(50)
      CHARACTER*3   BRACK(0:4)
      CHARACTER*60  QUAL
      CHARACTER*132 LOCLIN







      CALL WIFPUSHPPP (405,1)
      CALL WIFPUSHPPP (449,0)
      CALL WIFPUSHPPP (457,1)
      CALL WIFPUSHPPP (470,2)
      IF (WIFIRO(ATIAA(ION))) THEN
         IERR=1
         GOTO 998
      END IF
      IF (WIFIRH(ATIAA(ION))) THEN
         IERR=1
         GOTO 998
      END IF



      ONE=1.0
      IERR=0
      IMIN=1
      ISLF=1
      SELF=0.0
      BEST=999.9
      FOUND=.FALSE.
      CALL GVSCTB (QUAL,60)
      CALL ION113 (CROW,NUMTYP)
      NUMCONIAT=0
      IAA=ATIAA(ION)
      CALL GVSCTB (QUAL,40)
      CALL GVSSTX (BRACK(0),'(0)')
      CALL GVSSTX (BRACK(1),'(1)')
      CALL GVSSTX (BRACK(2),'(2)')
      CALL GVSSTX (BRACK(3),'(3)')
      CALL GVSSTX (BRACK(4),'(4)')



      CALL FCONAT (ION,IONHGH)
      CALL FCONRED (ION,IONHGH)
      CALL FCONREC



      CALL SC9031 (ION,CHAR2)
      CALL GVSSTX (IONTYP,CHAR2)
      CALL ION100 (ION,SPOS,WTOT,NFND)
      CALL SC9028 (IUNIT,ION,SPOS,WTOT,IERR)
      IF (IERR.NE.0) GOTO 998



      CALL FCONAT (ION,IONHGH)
      CALL FCONRED (ION,IONHGH)
      CALL FCONREC



      IF (NUMCONIAT.LE.3) THEN
         CALL GVSSTX (QUAL,'Too few ligands')
         CALL GVSCBC (QUAL,BRACK(NUMCONIAT))
         CALL PDBC92A (IAA,0)
         WRITE (LINE2,1010,ERR=999) LINE92(1:GVFLEN(LINE92)),QUAL
         CALL PDBC93 (LINE2)
         GOTO 998
      END IF 



      IF (.NOT.HASVIO(ION)) THEN
         CALL GVSSTX (QUAL,'Wrong ion-type for Vion calculation')
         CALL GVSCBC (QUAL,BRACK(NUMCONIAT))
         CALL PDBC92A (IAA,0)
         WRITE (LINE2,1010,ERR=999) LINE92(1:GVFLEN(LINE92)),QUAL
         CALL PDBC93 (LINE2)
      END IF



      PRINT*,' '
      PRINT*,'Working on:',ATIAA(ION),ONESA3(ATIAA(ION)),
     + '(',NUMNAM(ATIAA(ION)),') ',ONESAT(ION)
      DO 10 ITYPE=1,WIFPAR(480)
         CALL ION115 (CROW(ITYPE),DOION)
         IF (ITYPE.LE.NUMTYP.OR.DOION) THEN
            CALL GVSSTX (IONTYP,CROW(ITYPE))
            VION(ITYPE)=ION_VALENCY(ION)
            VSCO(ITYPE)=ABS(1.0-VION(ITYPE))
            IF (VION(ITYPE).GT.1.0) VSCO(ITYPE)=VSCO(ITYPE)/2.0
            IF (DOION) VSCO(ITYPE)=VSCO(ITYPE)/1.5
            CALL ION_SETTYP (CROW(ITYPE))
            PRINT*,'Test:',CROW(ITYPE),VION(ITYPE),VSCO(ITYPE)
            IF (VSCO(ITYPE).LT.BEST) THEN
               BEST=VSCO(ITYPE)
               IMIN=ITYPE
            END IF
            IF (CROW(ITYPE).EQ.CHAR2) THEN
               SELF=VSCO(ITYPE)
               ISLF=ITYPE
            END IF
         END IF
 10   CONTINUE
      
      IF (CROW(IMIN).EQ.CHAR2) PRINT*,'Self ion is best ion'
      SCODIF=SELF-BEST
      PRINT*,'SCODIF=',SCODIF
      PRINT*,' '



      DO 20 ITYPE=1,WIFPAR(480)
         CALL ION115 (CROW(ITYPE),DOION)
         IF (.NOT.DOION.AND.ITYPE.GT.NUMTYP) GOTO 20




         IF (ITYPE.EQ.IMIN) THEN
            IF (CROW(ITYPE).EQ.CHAR2) THEN
               IF (BEST.LT.0.25) THEN
                  CALL GVSSTX (QUAL,'Seems OK')
                  GOTO 20
               ELSE IF (BEST.LT.0.5) THEN
                  CALL GVSSTX (QUAL,'Poor packing')
               ELSE 
                  CALL GVSSTX (QUAL,'Bad packing')
               END IF
               IF (NUMCONIAT.LE.4) THEN
                  CALL GVSCBC (QUAL,'(Few ligands')
                  CALL GVSCBC (QUAL,BRACK(NUMCONIAT))
                  CALL GVSCBC (QUAL,')')
               END IF 
               CALL PDBC92A (IAA,0)
               WRITE (LINE2,1020,ERR=999) LINE92(1:GVFLEN(LINE92)),
     +          VION(IMIN),QUAL
               CALL PDBC93 (LINE2)
            ELSE
               FOUND=.TRUE.
               IF (SCODIF.GT.0.75) THEN
                  CALL GVSSTX (QUAL,'Should be')
                  CALL GVSCBC (QUAL,CROW(IMIN))
               ELSE IF (SCODIF.GT.0.5) THEN
                  CALL GVSSTX (QUAL,'Could be')
                  CALL GVSCBC (QUAL,CROW(IMIN))
               ELSE IF (SCODIF.GT.0.25) THEN
                  CALL GVSSTX (QUAL,'Is perhaps')
                  CALL GVSCBC (QUAL,CROW(IMIN))
               ELSE 
                  CALL GVSSTX (QUAL,'Scores about as good as')
                  CALL GVSCBC (QUAL,CROW(IMIN))
               END IF
               IF (NUMCONIAT.LE.4) THEN
                  CALL GVSCBC (QUAL,'(Few ligands')
                  CALL GVSCBC (QUAL,BRACK(NUMCONIAT))
                  CALL GVSCBC (QUAL,')')
               END IF 
               CALL SC9029 (QUAL,SPOS,CROW(IMIN))
               CALL PDBC92A (IAA,0)
               WRITE (LOCLIN,1000,ERR=999) LINE92(1:GVFLEN(LINE92)),
     +          VION(ISLF),VION(IMIN),QUAL
               CALL PDBC93 (LOCLIN)
            END IF
         END IF
 20   CONTINUE
      GOTO 998



  999 CONTINUE
      CALL GVSEIW ('SC9021')  



  998 CONTINUE
      CALL WIFPOPPPP (470)
      CALL WIFPOPPPP (457)
      CALL WIFPOPPPP (449)
      CALL WIFPOPPPP (405)
      CALL GVSSTX (ONESAT(ION)(1:2),CHAR2)


 1000 FORMAT (a,'&',2F7.2,'&',a)
 1010 FORMAT (a,'& -.-  -.- &',a)
 1020 FORMAT (a,F7.2,'& -.- ',' &',a)

      RETURN
      END   
      SUBROUTINE SC9022

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'SUGARS.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       I, J, P, GVFLEN







      IF (NR280.LE.0) THEN
         CALL PDBMES ('NOR280')
         GOTO 998
      END IF



      CALL CDB013 ('XIO1ERR','XIO1NEU','XIO1GOO')



      DO 20 I=1,NR280
         CALL GVSCTB (BIGLIN,132)
         IF (GVFLEN(CRYSTL(I)).LE.0) THEN
            GOTO 20
         ELSE
            P=0
            DO 10 J=1,80
               P=P+1
               IF (CRYSTL(I)(J:J).EQ.'%') THEN
                  BIGLIN(P:P)='\\'
                  P=P+1
               END IF
               IF (J.EQ.1.AND.CRYSTL(I)(J:J).EQ.' ') THEN
                  BIGLIN(P:P+14)='\\hspace{0.2cm}'
                  P=P+15
               END IF
               BIGLIN(P:P)=CRYSTL(I)(J:J)
 10         CONTINUE
         END IF
         IF (I.LT.NR280) THEN
            IF (GVFLEN(CRYSTL(I+1)).LE.0) THEN
               CALL GVSCBC (BIGLIN,'\\vspace{0.2cm}')
            END IF
         END IF
         CALL PDBC93 (BIGLIN)
 20   CONTINUE



      CALL CDB012



  998 CONTINUE


      RETURN
      END   
      SUBROUTINE SC9023

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'SYMINC.INC'
      INCLUDE       'PDBINC.INC'
      INTEGER       IAT, ISOUP, ISYM, SYMNEG, JAT, JSYM, IAA, IUNIT
      REAL          TMP(3), ANYDST, OWNDST
      LOGICAL       WIFIRM







      IF (.NOT.SYMHAS) THEN
         CALL GVSTTE ('IONESYM')
         GOTO 998
      END IF



      DO 100 ISOUP=1,NUMSOU
         IF (.NOT.WIFIRM(SOUADM(1,ISOUP))) GOTO 100



         DO 90 IAT=PNTAAS(SOUADM(1,ISOUP)),PNTAAE(SOUADM(2,ISOUP))
            IAA=ATIAA(IAT)
            CALL SC9011 (IAT,OWNDST,ANYDST,JAT,JSYM)



            IF (JSYM.GT.1.AND.OWNDST.GT.ANYDST+1.0) THEN
               ISYM=SYMNEG(JSYM)
               TMP(1)=XCOORD(IAT)
               TMP(2)=YCOORD(IAT)
               TMP(3)=ZCOORD(IAT)
               CALL GVSMPV (TMP,SYMMAT(1,1,ISYM),SYMVEC(1,ISYM))



               CALL GVSWAP (XCOORD(IAT),TMP(1))
               CALL GVSWAP (YCOORD(IAT),TMP(2))
               CALL GVSWAP (ZCOORD(IAT),TMP(3))



               CALL DIF015 (6,'?M4911',IAT)
               IF (IUNIT.NE.6) CALL DIF015 (IUNIT,'?M4911',IAT)
            END IF
 90      CONTINUE
 100  CONTINUE



  998 CONTINUE


      RETURN
      END
      SUBROUTINE SC9024 (IUNIT,AVER,SDEV,ISOUP)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'CNTINC.INC'
      INCLUDE       'SUGARS.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       ISOUP, IWAT, IERR, IUNIT
      REAL          AVER, SDEV
      LOGICAL       WIFISW







      IF (.NOT.WIFISW(ISOUP)) THEN
         CALL GVSTTB ('?M4839')
         GOTO 999
      END IF



      CALL GVSRT0 (ATTVAL,NUMATF)



      DO 10 IWAT=PNTAAS(SOUADM(1,ISOUP)),PNTAAE(SOUADM(2,ISOUP))
         CALL SC9025 (IUNIT,IWAT,AVER,SDEV,IERR)
 10   CONTINUE
      GOTO 998



  999 CONTINUE
      CALL GVSEIW ('SC9024')



  998 CONTINUE


      RETURN
      END
      SUBROUTINE SC9025P (ION,AVER,SDEV)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'DATBAS.INC'
      INCLUDE       'SUGARS.INC'
      INCLUDE       'CNTINC.INC'
      INCLUDE       'SYMINC.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       NUMTYP, ION, IERR, ITYPE, AROUND, IBAD
      REAL          VION, VSCO, ION_VALENCY, AVER, SDEV
      LOGICAL       DOION
      CHARACTER*2   CROW(50)







      CALL WIFPUSHPPP (590,1)



      ATTVAL(ION)=999.9



      IF (NUMSYM.LE.1) THEN
         CALL ENVTOT
         CALL SYMINI
         CALL SYMSPG (IERR)
         CALL SYMNEA
      END IF
      CALL WIFPUSHPPP (457,1)
      CALL FCONAT (ION,1.0)
      CALL FCONRED (ION,IONHGH)
      CALL FCONREC
      CALL WIFPOPPPP (457)
      IF (NUMCONIAT.LT.4) GOTO 998



      IF (XBFACT(ION).GT.AVER) GOTO 998
      IBAD=0
      DO 10 AROUND=1,NUMCONIAT
         IF (XBFACT(CONIAT(AROUND)).GT.AVER+2*SDEV) IBAD=IBAD+1
 10   CONTINUE
      IF (NUMCONIAT-IBAD.LT.4) GOTO 998



      CALL ION113 (CROW,NUMTYP)
      DO 20 ITYPE=1,WIFPAR(480)
         CALL ION115 (CROW(ITYPE),DOION)
         IF (ITYPE.LE.NUMTYP.OR.DOION) THEN
            CALL GVSSTX (IONTYP,CROW(ITYPE))
            VION=ION_VALENCY(ION)
            VSCO=ABS(1.0-VION)
            IF (VION.GT.1.0.AND.VION.LT.1.5) VSCO=VSCO/2.0
            IF (DOION) VSCO=VSCO/1.5
            ATTVAL(ION)=MIN(ATTVAL(ION),VSCO)
         END IF
 20   CONTINUE



  998 CONTINUE
      CALL WIFPOPPPP (590)


      RETURN
      END   
      SUBROUTINE SC9025 (IUNIT,ION,AVER,SDEV,IERR)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'DATBAS.INC'
      INCLUDE       'SUGARS.INC'
      INCLUDE       'CNTINC.INC'
      INCLUDE       'SYMINC.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       NUMTYP, ION, IERR, ITYPE, IMIN, GVFLEN, IUNIT, IAT,
     +              WATNCS(100), NNCS, I, NFND, IBAD, AROUND
      REAL          VION(50), VORG(50), SMIN, WTOT, ION_VALENCY, AVER, 
     +              SDEV, CUTOFF
      LOGICAL       FIRST, FNDSOU, FND280, HASNCS, SPOS, DOION
      CHARACTER*2   CROW(50), CHAR2
      CHARACTER*8   CHAR8







      CALL WIFPUSHPPP (590,1)



      IERR=0
      IMIN=1
      SMIN=999.9
      CUTOFF=0.15
      CALL SC9031 (ION,CHAR2)



      IF (NUMSYM.LE.1) THEN
         CALL ENVTOT
         CALL SYMINI
         CALL SYMSPG (IERR)
         CALL SYMNEA
      END IF
      CALL WIFPUSHPPP (457,1)
      CALL FCONAT (ION,1.0)
      CALL FCONRED (ION,IONHGH)
      CALL FCONREC
      CALL WIFPOPPPP (457)
      IF (NUMCONIAT.LT.4) THEN
         CALL DIF015 (IUNIT,'Analyzing:',ION)
         CALL GVSTUI (IUNIT,'Number of (oxygen) contacts:',NUMCONIAT)
         GOTO 998
      END IF

      CALL DIF015 (IUNIT,'Analyzing:',ION)
      CALL ION105A (IUNIT)







      CALL ION113 (CROW,NUMTYP)
      DO 10 ITYPE=1,WIFPAR(480)
         VORG(ITYPE)=0
         CALL ION115 (CROW(ITYPE),DOION)
         IF (ITYPE.LE.NUMTYP.OR.DOION) THEN
            CALL GVSSTX (IONTYP,CROW(ITYPE))
            VORG(ITYPE)=ION_VALENCY(ION)
            VION(ITYPE)=ABS(1.0-VORG(ITYPE))
            IF (VION(ITYPE).GT.1.0.AND.VORG(ITYPE).LT.1.5) 
     +       VION(ITYPE)=VION(ITYPE)/2.0
            IF (DOION) VION(ITYPE)=VION(ITYPE)/1.5
            IF (VION(ITYPE).LT.SMIN) THEN
               SMIN=VION(ITYPE)
               IMIN=ITYPE
            END IF
         END IF
 10   CONTINUE



      CALL SC9030 (IUNIT,NUMTYP,ION,CROW,VION,IMIN)



      IF (SMIN.GT.CUTOFF) GOTO 998
      CALL PDBC92A (0,ION)
      WRITE (LINE2,1010,ERR=999) LINE92(1:GVFLEN(LINE92)),VORG(IMIN),
     + CROW(IMIN)



      CALL ION100 (ION,SPOS,WTOT,NFND)
      CALL SC9029 (LINE2,SPOS,CROW(IMIN))








      FIRST=.TRUE.
      DO 20 ITYPE=1,NUMTYP
         IF (ITYPE.EQ.IMIN) GOTO 20
         IF (ABS(VION(ITYPE)-SMIN).GT.0.1) GOTO 20
         CALL ION114 (CROW(ITYPE),FNDSOU)
         CALL ION115 (CROW(ITYPE),FND280)



         IF (FIRST) THEN
            FIRST=.FALSE.
            CALL GVSCBC (LINE2,'(or')
            CALL GVSCBC (LINE2,CROW(ITYPE))
         ELSE
            CALL GVSCPC (LINE2,',')
            CALL GVSCBC (LINE2,CROW(ITYPE))
         END IF



         IF (FNDSOU) THEN
            IF (FND280) THEN
               CALL GVSCBC (LINE2,'*1 and *2')
            ELSE
               CALL GVSCBC (LINE2,'*1')
            END IF
         ELSE IF (FND280) THEN
            CALL GVSCBC (LINE2,'*2')
         END IF
 20   CONTINUE



      IF (.NOT.FIRST) CALL GVSCPC (LINE2,')')



      IF (XBFACT(ION).GT.AVER+SDEV) THEN
         CALL GVSCBC (LINE2,'ION-B')
      ELSE IF (XBFACT(ION).GT.AVER) THEN
         CALL GVSCBC (LINE2,'Ion-B')
      END IF
      IBAD=0
      DO 30 AROUND=1,NUMCONIAT
         IF (XBFACT(CONIAT(AROUND)).GT.AVER+2*SDEV) IBAD=IBAD+1
 30   CONTINUE
      IF (NUMCONIAT-IBAD.LT.4) CALL GVSCBC (LINE2,'H2O-B')



      IMIN=0
      IF (HASNCS(ION,WATNCS,NNCS,100)) THEN
         CALL GVSTUI (IUNIT,'Checking NC-symmetry-related waters:',NNCS)
         IF (NNCS.GE.1) THEN
            CALL DIF013 (IUNIT,ION,SMIN)
            DO 40 I=1,NNCS
               IAT=WATNCS(I)
               IF (ATTVAL(IAT).LT.SMIN+0.15) THEN
                  IMIN=IMIN+1
                  CALL DIF013 (IUNIT,IAT,ATTVAL(IAT))
               END IF
 40         CONTINUE
         END IF
      END IF
      IF (IMIN.GE.1) THEN
         CALL GVSCTB (CHAR8,8)
         IF (NNCS.LE.9) THEN
            WRITE (CHAR8,1020,ERR=999) IMIN,NNCS
         ELSE
            WRITE (CHAR8,1030,ERR=999) IMIN,NNCS
         END IF
         CALL GVSCBC (LINE2,CHAR8)
      END IF
      CALL PDBC93 (LINE2)
      GOTO 998



  999 CONTINUE
      CALL GVSEIW ('SC9025')



  998 CONTINUE
      CALL WIFPOPPPP (590)


 1010 FORMAT (a,'&',F5.2,'&',a)
 1020 FORMAT ('NCS ',I1,'/',I1)
 1030 FORMAT ('NCS',I2,'/',I2)


      RETURN
      END   
      SUBROUTINE SC9026 (ION)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'PDBINC.INC'
      INCLUDE       'SYMINC.INC'
      INTEGER       ION, IAA, NFND, GVFLEN
      REAL          WTOT
      LOGICAL       SPOS, GVFRIN, WIFIRM
      CHARACTER*40  QUAL







      IF (.NOT.WIFIRM(ATIAA(ION))) GOTO 998



      IAA=ATIAA(ION)
      CALL GVSCTB (QUAL,40)



      CALL ION100 (ION,SPOS,WTOT,NFND)



      IF (.NOT.SPOS) THEN
         IF (GVFRIN(WTOT,0.95,1.05)) THEN
            GOTO 998
         ELSE
            IF (WTOT.GT.1.01) THEN
               CALL GVSSTX (QUAL,'?M4956')
            ELSE IF (WTOT.LT.0.95) THEN
               CALL GVSSTX (QUAL,'Low occupancy')
            END IF
            WRITE (QUAL(31:40),'(F10.2)',ERR=999) WTOT
            CALL GVSRDB (QUAL)
         END IF
      ELSE
         IF (GVFRIN(WTOT,0.95,1.05)) THEN
            CALL GVSSTX (QUAL,'Special position.')
         ELSE
            IF (WTOT.GT.1.05) THEN
               CALL GVSSTX (QUAL,'Error: mult=   occ=')
               WRITE (QUAL(13:15),'(I3)',ERR=999) NFND
               WRITE (QUAL(31:40),'(F10.2)',ERR=999) WTOT
            ELSE IF (WTOT.LT.0.95) THEN
               CALL GVSSTX (QUAL,'Low occupancy. Mult=')
               WRITE (QUAL(37:40),'(I3)',ERR=999) NFND
            END IF
            CALL GVSRDB (QUAL)
         END IF
      END IF



      IF (XBFACT(ION).LT.5.0) THEN
         CALL GVSCBC (QUAL,'B-low')
      ELSE IF (XBFACT(ION).GT.80.0) THEN
         CALL GVSCBC (QUAL,'B-high!')
      ELSE IF (XBFACT(ION).GT.60.0) THEN
         CALL GVSCBC (QUAL,'B-high')
      END IF



      CALL PDBC92A (IAA,0)
      WRITE (LINE2,1000,ERR=999) LINE92(1:GVFLEN(LINE92)),QUAL
      CALL PDBC93 (LINE2)
      GOTO 998



  999 CONTINUE
      CALL GVSEIW ('SC9026')



  998 CONTINUE


 1000 FORMAT (a,'&',a)

      RETURN
      END   
      SUBROUTINE SC9027

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'SUGARS.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       ISOUP1, ISOUP2, IMAT, JMAT, KMAT, IERR
      REAL          RMS, GVFRTD, GVFGTA, GVFLNV
      LOGICAL       WIFISR







      NUMNCS=0



      DO 100 ISOUP1=1,NUMSOU-1
         IF (.NOT.WIFISR(ISOUP1)) GOTO 100
         DO 90 ISOUP2=ISOUP1+1,NUMSOU
            IF (SOUADM(5,ISOUP1).NE.SOUADM(5,ISOUP2)) GOTO 90



            NUMNCS=NUMNCS+1
            CALL SUP024 (ISOUP1,ISOUP2,NCSMAT(1,1,NUMNCS),
     +       NCSVEC(1,NUMNCS),RMS,IERR)
            IF (IERR.NE.0) THEN
               NUMNCS=NUMNCS-1
               GOTO 90
            END IF
            IF (NUMNCS.GE.MAXNCS) GOTO 101
 90      CONTINUE
 100  CONTINUE
 101  CONTINUE



      IF (NUMNCS.LE.1) GOTO 998
      DO 200 IMAT=1,NUMNCS-1
         DO 190 JMAT=IMAT+1,NUMNCS
            IF (ABS(GVFRTD(GVFGTA(NCSMAT(1,1,IMAT)))-
     +          GVFRTD(GVFGTA(NCSMAT(1,1,JMAT)))).GT.0.5) GOTO 190
            CALL GVSTV3 (CV,NCSVEC(1,IMAT),NCSVEC(1,JMAT))
            IF (GVFLNV(CV).GT.0.5) GOTO 190
            IF (JMAT.LT.NUMNCS) THEN
               DO 180 KMAT=JMAT,NUMNCS-1
                  CALL GVSCPR (NCSMAT(1,1,KMAT),NCSMAT(1,1,KMAT+1),9)
                  CALL GVSCPR (NCSVEC(1,KMAT),NCSVEC(1,KMAT+1),3)
 180           CONTINUE
            END IF
            NUMNCS=NUMNCS-1
            GOTO 101
 190     CONTINUE
 200  CONTINUE



  998 CONTINUE


      RETURN
      END   
      LOGICAL FUNCTION HASNCS(IAT,JNCS,NNCS,MNCS)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INCLUDE       'SUGARS.INC'
      INCLUDE       'ESPACE.INC'
      INTEGER       MNCS
      INTEGER       IAT, JNCS(MNCS), NNCS, IMAT, JWAT, KWAT, JSOU
      REAL          D
      LOGICAL       WIFISW







      HASNCS=.FALSE.
      NNCS=0
      CALL GVSIT0 (JNCS,MNCS)



      IF (NUMNCS.LE.0) GOTO 998



      DO 100 IMAT=1,NUMNCS
         CV(1)=NCSMAT(1,1,IMAT)*XCOORD(IAT)+
     +         NCSMAT(1,2,IMAT)*YCOORD(IAT)+
     +         NCSMAT(1,3,IMAT)*ZCOORD(IAT)+NCSVEC(1,IMAT)
         CV(2)=NCSMAT(2,1,IMAT)*XCOORD(IAT)+
     +         NCSMAT(2,2,IMAT)*YCOORD(IAT)+
     +         NCSMAT(2,3,IMAT)*ZCOORD(IAT)+NCSVEC(2,IMAT)
         CV(3)=NCSMAT(3,1,IMAT)*XCOORD(IAT)+
     +         NCSMAT(3,2,IMAT)*YCOORD(IAT)+
     +         NCSMAT(3,3,IMAT)*ZCOORD(IAT)+NCSVEC(3,IMAT)
         DO 90 JSOU=1,NUMSOU
            IF (WIFISW(JSOU)) THEN
               DO 80 JWAT=PNTAAS(SOUADM(1,JSOU)),PNTAAE(SOUADM(2,JSOU))
                  D=SQRT((CV(1)-XCOORD(JWAT))**2+
     +                   (CV(2)-YCOORD(JWAT))**2+
     +                   (CV(3)-ZCOORD(JWAT))**2)
                  IF (D.LT.0.5) THEN
                     IF (NNCS.GE.1) THEN
                        DO 70 KWAT=1,NNCS
                           IF (JWAT.EQ.JNCS(KWAT)) GOTO 80
 70                     CONTINUE
                     END IF
                     HASNCS=.TRUE.
                     NNCS=NNCS+1
                     JNCS(NNCS)=JWAT
                     IF (NNCS.GE.MNCS) GOTO 998
                  END IF
 80            CONTINUE
            END IF
 90      CONTINUE
 100  CONTINUE



  998 CONTINUE


      RETURN
      END   
      SUBROUTINE SC9028 (IUNIT,ION,SPOS,WTOT,IERR)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INTEGER       IERR, ION, GVFLEN, IUNIT
      REAL          WTOT
      LOGICAL       SPOS, ION103, HASVIO, WNTION
      CHARACTER*60  QUAL







      CALL ION115 (ONESAT(ION)(1:2),WNTION)
      IF ((.NOT.HASVIO(ION)).AND.(.NOT.WNTION)) THEN
         IERR=1
         GOTO 998
      END IF



      IERR=0
      CALL GVSCTB (QUAL,60)



      CALL DIF015 (IUNIT,'Analyzing:',ION)



      IF (WTOT.LT.0.8.OR.XBFACT(ION).GT.80.0) THEN



         IF (WTOT.LT.0.8) THEN
            IF (XBFACT(ION).GT.80.0) THEN
               WRITE (QUAL,1010,ERR=999) WTOT,XBFACT(ION)
            ELSE
               WRITE (QUAL,1020,ERR=999) WTOT
            END IF
         ELSE
            IF (XBFACT(ION).GT.80.0) THEN
               WRITE (QUAL,1030,ERR=999) XBFACT(ION)
            END IF
         END IF
         IF (SPOS) CALL GVSCBC (QUAL,'*S')



         CALL PDBC92A (ATIAA(ION),0)



         WRITE (LINE2,1000,ERR=999) LINE92(1:GVFLEN(LINE92)),QUAL
         CALL PDBC93 (LINE2)
         IERR=1



      ELSE IF (ION103(6,ION)) THEN
         CALL GVSSTX (QUAL,'Part of ionic cluster')
         IF (SPOS) CALL GVSCBC (QUAL,'*S')
         CALL PDBC92A (ATIAA(ION),0)
         CALL GVSCTB (BIGLIN,132)
         WRITE (BIGLIN,1000,ERR=999) LINE92(1:GVFLEN(LINE92)),QUAL
         CALL PDBC93 (BIGLIN)
      END IF

      GOTO 998



  999 CONTINUE
      CALL GVSEIW ('SC9028')



  998 CONTINUE


 1000 FORMAT (a,'& -.-  -.- &',a)
 1010 FORMAT ('Low probability ion. Occ=',F4.2,'; B=',F5.1)
 1020 FORMAT ('Low probability ion. Occ=',F4.2)
 1030 FORMAT ('Low probability ion. B=',F5.1)

      RETURN
      END   
      SUBROUTINE SC9029 (QUAL,SPOS,IONTYP)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      LOGICAL       FNDSOU, FND280, SPOS
      CHARACTER*2   IONTYP
      CHARACTER*(*) QUAL







      CALL ION114 (IONTYP,FNDSOU)
      CALL ION115 (IONTYP,FND280)





      IF (SPOS) CALL GVSCBC (QUAL,'*S')



      IF (FNDSOU) THEN
         IF (FND280) THEN
            IF (SPOS) THEN
               CALL GVSCBC (QUAL,'; *1 and *2')
            ELSE
               CALL GVSCBC (QUAL,'*1 and *2')
            END IF
         ELSE
            IF (SPOS) THEN
               CALL GVSCBC (QUAL,'; *1')
            ELSE
               CALL GVSCBC (QUAL,'*1')
            END IF
         END IF
      ELSE IF (FND280) THEN
         IF (SPOS) THEN
            CALL GVSCBC (QUAL,'; *2')
         ELSE
            CALL GVSCBC (QUAL,'*2')
         END IF
      END IF





      RETURN
      END   
      SUBROUTINE SC9030 (IUNIT,NUMTYP,ION,CROW,VION,IMIN)

















      IMPLICIT      NONE
      INTEGER       IUNIT, NUMTYP, IMIN, ITYPE, ION
      REAL          VION(NUMTYP)
      CHARACTER*2   IONTYP, CHAR2
      CHARACTER*(*) CROW(NUMTYP)







      CALL SC9031 (ION,CHAR2)



      DO 10 ITYPE=1,NUMTYP
         CALL GVSSTX (IONTYP,CROW(ITYPE))
         IF (CROW(ITYPE).EQ.CHAR2) THEN
            IF (ITYPE.EQ.IMIN) THEN
               WRITE (IUNIT,1100,ERR=999) CROW(ITYPE),VION(ITYPE),
     +          ABS(1.0-VION(ITYPE))
            ELSE
               WRITE (IUNIT,1110,ERR=999) CROW(ITYPE),VION(ITYPE),
     +          ABS(1.0-VION(ITYPE))
            END IF
         ELSE
            IF (ITYPE.EQ.IMIN) THEN
               WRITE (IUNIT,1120,ERR=999) CROW(ITYPE),VION(ITYPE),
     +          ABS(1.0-VION(ITYPE))
            ELSE
               WRITE (IUNIT,1000,ERR=999) CROW(ITYPE),VION(ITYPE),
     +          ABS(1.0-VION(ITYPE))
            END IF
         END IF
 10   CONTINUE
      
      GOTO 998



  999 CONTINUE
      CALL GVSEIW ('SC9030')



  998 CONTINUE


 1000 FORMAT (A2,2F6.2)
 1100 FORMAT (A2,2F6.2,' <- Self <- Best')
 1110 FORMAT (A2,2F6.2,' <- Self')
 1120 FORMAT (A2,2F6.2,' <- Best')

      RETURN
      END   
      SUBROUTINE SC9031 (ION,CHAR2)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INTEGER       ION
      LOGICAL       CH4EQCH4
      CHARACTER*2   CHAR2







      IF (CH4EQCH4(ONESA3(ATIAA(ION)),' NH4')) THEN
         CALL GVSSTX (CHAR2,'N4')
      ELSE
         CALL GVSSTX (CHAR2,ONESAT(ION)(1:2))
      END IF





      RETURN
      END   
      SUBROUTINE SC9040 (AVER,SDEV)

















      IMPLICIT      NONE
      INCLUDE       'BIGINC.INC'
      INTEGER       ISOUP, IWAT, IERR
      REAL          AVER, SDEV
      LOGICAL       WIFISW







      CALL GHS021 (0,0,IERR)
      IF (IERR.NE.0) THEN
         CALL GVSTTB ('Water B-factor statistics failed')
         AVER=20.0
         SDEV=40.0
         GOTO 998
      END IF



      DO 20 ISOUP=1,NUMSOU
         IF (WIFISW(ISOUP)) THEN
            DO 10 IWAT=PNTAAS(SOUADM(1,ISOUP)),PNTAAE(SOUADM(2,ISOUP))
               CALL GHS022 (0,XBFACT(IWAT),IERR)
               IF (IERR.NE.0) THEN
                  CALL GVSTTB ('Water B-factor statistics failed')
                  AVER=20.0
                  SDEV=40.0
                  GOTO 998
               END IF
 10         CONTINUE
         END IF
 20   CONTINUE



      CALL GHS023 (0,0,AVER,SDEV,IERR)



  998 CONTINUE
      CALL GHS034 (IERR)


      RETURN
      END   
