       !*************************************************************************
       !   A Subroutine of Main, Put the structure's mass center at the origin  *
       !                                                                        *
       !*************************************************************************
       
       Subroutine Positioning ( PDBid, MinModel, MaxModel, MinChain, MaxChain, MinResidue, MaxResidue, MinAtom, MaxAtom, ATM, AANm, vd, x, y, z, Site, SeqIndex, Seq )

       Implicit None
       
! ---- Variables : -------------------------------------------------------------- !       
       Character*4 PDBid
       
       Integer    SITE
       
       Integer    MinModel, MinChain, MinResidue, MinAtom  ! The initial Min value is set to an extreme big value
       Integer    MaxModel, MaxChain, MaxResidue, MaxAtom  ! The initial Max value is set to an extreme small value
       
! ---- Variables : -------------------------------------------------------------- !       
       Character   ATM(   MinModel:MaxModel, MinChain:MaxChain, MinResidue:MaxResidue, MinAtom:MaxAtom )*4    ! The Name of atom
       Character   AANm(  MinModel:MaxModel, MinChain:MaxChain, MinResidue:MaxResidue, MinAtom:MaxAtom )*3    ! The Name of residue
       
       Integer     vd(  MinModel:MaxModel, MinChain:MaxChain, MinResidue:MaxResidue, MinAtom:MaxAtom )        ! Record the existence of the ATOM :

       Real        X(  MinModel:MaxModel, MinChain:MaxChain, MinResidue:MaxResidue, MinAtom:MaxAtom )         ! Coordinates: X
       Real        Y(  MinModel:MaxModel, MinChain:MaxChain, MinResidue:MaxResidue, MinAtom:MaxAtom )         ! Coordinates: Y
       Real        Z(  MinModel:MaxModel, MinChain:MaxChain, MinResidue:MaxResidue, MinAtom:MaxAtom )         ! Coordinates: Z
 
       Integer      SeqIndex(1:Site, 1:4)     ! Array SeqIndex(Site#,1/2/3/4) /Model#/Chain#/Residue#/Atom#/  
       Integer      Seq( MinModel:MaxModel, MinChain:MaxChain, MinResidue:MaxResidue, MinAtom:MaxAtom ) ! The sequential number of a certain node at (k,j,i,s)


! ---- Variables : ={Amino Acids}= ={Amino Acids}= ={Amino Acids}= Part 1 : Read in 'Amino Acids List.inp'

       Character  AA_3L(40)*3             ! AA 3 Letter abbreviation
       Character  AA_Ful(40)*20           ! AA full name
       Character  AA_FORMULA(40)*22       ! AA Formula
       Real       AA_MW(40)               ! AA molecular weight
       
       Character  Chartemp*120
       
       Integer    AAtype                  ! the total AA type
       
       Integer    ic, ios
       
! ---- Variables :  _^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_  Calculate the Mass Center
       Real       mcx, mcy, mcz           ! Mass center of the entire structure
       Real       Mass                    ! Total mass
       
       Integer    ka, ja, ia, sa
       
       
       
       
       Integer    i, j, k, s, m, n


! ==== Body of Subroutine Positioning =========================================================================================================== !       
       
! ={Amino Acids}= ={Amino Acids}= ={Amino Acids}= ={Amino Acids}= Part 1 : Read in 'Amino Acids List.inp'
       Open(40, file='Amino Acids List.inp')
       
       Rewind(40)
        
       Read(40,*) Chartemp
       Read(40,*) Chartemp
       Read(40,*) Chartemp
       
       i=0
       ios=0
       Do While(ios==0)
          BackSpace(40)
          i=i+1
          Read(40, '(A20,4x,A3,12x,A22,2x,F6.2)', iostat=ios, END=400) AA_Ful(i), AA_3L(i), AA_FORMULA(i), AA_MW(i)
          Read(40,'(A50)',iostat=ios, END=400) Chartemp
       Enddo
        
400    AAtype=i

       Close(40) ! 'Amino Acids List.inp'
    
       
! _^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_^=^_ Calculate the Mass Center

       mcx=0.0
       mcy=0.0
       mcz=0.0
       mass=0.0
       
       Do  n=1, SITE

          ka=SeqIndex(n,1)
          ja=SeqIndex(n,2)
          ia=SeqIndex(n,3)
          sa=SeqIndex(n,4)
          
          m=0
          Do j=1, AAtype
            If( AANm(ka,ja,ia,sa)==AA_3L(j) ) m=j
          Enddo
          
          If(m==0) Stop 'Error! Can not find the reisude name from [Amino Acids List.inp]'       
          
          mcx = mcx + AA_MW(m)*x(ka,ja,ia,sa)
          mcy = mcy + AA_MW(m)*y(ka,ja,ia,sa)
          mcz = mcz + AA_MW(m)*z(ka,ja,ia,sa)
          
          mass = mass + AA_MW(m)
       
       Enddo
       
       mcx=mcx/mass
       mcy=mcy/mass
       mcz=mcz/mass
       
       Write(*,*) ' '
       Write(*,*) 'Mass center: ', mcx, mcy, mcz
       Write(*,*) ' '
       
! *-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-* Transltate the structure on the origin
       
       Do k=MinModel, MaxModel
       Do j=MinChain, MaxChain
       Do i=MinResidue, MaxResidue
       Do s=MinAtom, MaxAtom
       
           If( vd(k,j,i,s)>0 ) Then
           
               x(k,j,i,s) = x(k,j,i,s) - mcx
               y(k,j,i,s) = y(k,j,i,s) - mcy
               z(k,j,i,s) = z(k,j,i,s) - mcz
           
           EndIf
       
       Enddo
       Enddo
       Enddo
       Enddo

       
! =====E==N===D==================================================================================================================================== !

       Return
       End           
