       !************************************************************************
       !            A Subroutine of Main, writing a PDB Movie file             *
       !************************************************************************
       
       Subroutine WriteMoviePDB ( MEIG, Nstep, dist0, itrt,    PDBid, SITE, lmtA, Step, PDBRecord, Line, ATM, AANm, X, Y, Z, vd, vx, vy, vz, Seq, SeqIndex, mt )
       
       Implicit None
       
! ---- Variables : --------------------------------------------------------------------------------- !
       
       Integer     MEIG
       Integer     Nstep
       
       Real        dist0  ! the magnitude of the initial displacement vector
       
       Integer     itrt   ! The iteration number
       
! ---- Variables : --------------------------------------------------------------------------------- !
       
       CHARACTER   PDBid*4
       
       Integer     SITE
       
       Integer     lmtA(4,2)   ! Range of Structure A
       
       Real        Step(1:Nstep, 1:MEIG)   ! The weight on each mode of each step

       Character   PDBRecord(100,5000)*80 ! Recording the content of each line : PDBRecord(RecordType, Line#) 
       Integer     Line(100)              ! The line number of each RecordType : Line(RecordType)

       
       Character   ATM(  lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )*4    ! The Name of atom
       Character   AANm(  lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )*3    ! The Name of residue
       
       Real        X(  lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )         ! Coordinates: X
       Real        Y(  lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )         ! Coordinates: Y
       Real        Z(  lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )         ! Coordinates: Z
       
       Integer     vd( lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) )         ! Record the existence of the ATOM :
       
       Real        vx(1:SITE, 1:MEIG), vy(1:SITE, 1:MEIG), vz(1:SITE, 1:MEIG)                                       ! Eigenvectors
       
       Integer     Seq( lmtA(1,1):lmtA(1,2), lmtA(2,1):lmtA(2,2), lmtA(3,1):lmtA(3,2), lmtA(4,1):lmtA(4,2) ) ! The sequential number of a certain node at (k,j,i,s)
       Integer     SeqIndex(1:SITE,1:4) ! Array SeqIndex(Site#,1/2/3/4) /Model#/Chain#/Residue#/Atom#/
       
       Integer     mt                   ! The minimum RMSD appears at the 'mt' step
       
! ---- Variables : --------------------------------------------------------------------------------- !

       Integer     MinModel, MinChain, MinResidue, MinAtom  
       Integer     MaxModel, MaxChain, MaxResidue, MaxAtom  
       
       Real        Deform(1:SITE)
       
       Real        DX(1:SITE, 1:Nstep)
       Real        DY(1:SITE, 1:Nstep)
       Real        DZ(1:SITE, 1:Nstep)
       
       Integer     RecordType ! each section in PDB file is assigned a RecordType

       Character   DUMMY6*6
       Character   ATNAME*4
       Character   DUMMY3*3
       Character   A1*1
       Integer     D1INT, D2INT
       Real        XXX, YYY, ZZZ, BBB
       Real        R1
       
       Character   ChainName*1
       Character   IDModel*1
       
       Character   DUMMY80*80
       
	   Character   ACE*2 ! the string will be stored here 
       
! ---- Variables : --------------------------------------------------------------------------------- !
       Integer     k, j, i, s, n, m, t, jj, ct
       
! ==== Body of Subroutine WriteMoviePDB ============================================================ !     
       
!       Open (90, File=PDBid//'_Trajectory_Itr'//ACE(itrt,2)//'.pdb') 
       
!       Call Deformation ( SITE, MEIG, Nstep, vx, vy, vz, Step, Deform )

       MinModel      =  lmtA(1,1)
       MaxModel      =  lmtA(1,2)
       MinChain      =  lmtA(2,1)
       MaxChain      =  lmtA(2,2)
       MinResidue    =  lmtA(3,1)
       MaxResidue    =  lmtA(3,2)
       MinAtom       =  lmtA(4,1)
       MaxAtom       =  lmtA(4,2)                                               


! |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.| |.|  Preparing the DX(), DY(), DZ()       
       Do t=1, Nstep
            Do n=1, SITE
            
                DX(n,t)=0.0
                DY(n,t)=0.0
                DZ(n,t)=0.0
                Do m=1, MEIG
                    DX(n,t)  =  DX(n,t)   +   dist0 * Step(t,m) * vx(n,m)
                    DY(n,t)  =  DY(n,t)   +   dist0 * Step(t,m) * vy(n,m)
                    DZ(n,t)  =  DZ(n,t)   +   dist0 * Step(t,m) * vz(n,m)
                Enddo ! m
                
            Enddo ! n   
       Enddo ! t
       
       Call Bcolor ( SITE, Nstep, DX, DY, DZ, Deform )

! <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> <*> Generating the displacement vectors for next step(Transistion Pathway Movie) 

       Open(95, File='['//PDBid//']_DfmVector_Iteration'//ACE(itrt,2)//'.xyz' )
       
       Do n=1, SITE
           t=mt
           Write(95,*) n, DX(n,t), DY(n,t), DZ(n,t)
       EndDo
       
       Close(95) ! '['//PDBid//']_DfmVector_Iteration'//ACE(itrt,2)//'.xyz'
! .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-.  Movie Generating Loop       
    
       Open(91, File=PDBid//'_Itr'//ACE(itrt,2)//'.pdb' )
       Open(92, File=PDBid//'.pdb' )

       RecordType=1     !+++++++++++++++++  Title Section
       Do i=1, Line(RecordType)
!           Write(90, '(A80)') PDBRecord(RecordType, i)
           If(t==mt) Write(91, '(A80)') PDBRecord(RecordType, i)
           If(t==mt) Write(92, '(A80)') PDBRecord(RecordType, i)
       Enddo

       Do t=1, Nstep
            
            Write(*,'(A,A4,A,I3)') ' -> Now writing the trajectory PDB files : ', PDBid, '_Trajectory.pdb  |   Step : ', t                       
    
!            Write(90,'(A6,5X,I3)') 'MODEL ', t
            
            RecordType=5     !+++++++++++++++++  Secondary Structure 
            Do i=1, Line(RecordType)
!                Write(90, '(A80)') PDBRecord(RecordType, i)
                If(t==mt)  Write(91, '(A80)') PDBRecord(RecordType, i)
                If(t==mt)  Write(92, '(A80)') PDBRecord(RecordType, i)
            Enddo
                                
            Do k=MinModel, MaxModel               
                                    
                ct=0
                Do j=MinChain, MaxChain
                    
                    Do i=MinResidue, MaxResidue
                        Do s=MinAtom, MaxAtom 
            
                            IF(vd(k,j,i,s)>0) Then
                            
                                DUMMY6='ATOM  '
                                ATNAME=ATM(k,j,i,s)
                                DUMMY3=AANm(k,j,i,s)
                                A1=ChainName(j)
                                D2INT=i
                                R1=1.00
                                
                                If( (vd(k,j,i,s)==1).AND.(1<=s).AND.(s<=4) ) Then ! Backbone Atoms
                                    
                                    ct=ct+1
                                    D1INT=ct
                                    
                                    n = seq(k,j,i,2)
                                    
                                    ! atom CA
                                    XXX = X(k,j,i,s) + DX(n,t)
                                    YYY = Y(k,j,i,s) + DY(n,t)
                                    ZZZ = Z(k,j,i,s) + DZ(n,t)
                                    BBB = Deform(n)
                                    
                                    If( (s==1).AND.(1<n).AND.(n<SITE) ) Then  ! atom N
                                        jj=SeqIndex(n-1,2)
                                        If(jj==j) Then
                                            XXX = X(k,j,i,s) + 0.25*DX(n-1,t) + 0.75*DX(n,t)
                                            YYY = Y(k,j,i,s) + 0.25*DY(n-1,t) + 0.75*DY(n,t)
                                            ZZZ = Z(k,j,i,s) + 0.25*DZ(n-1,t) + 0.75*DZ(n,t)
                                            BBB = 0.25*Deform(n-1) + 0.75*Deform(n)
                                        EndIF ! (jj==j)
                                    EndIF ! ( (s==1).AND.(1<n).AND.(n<SITE)   ! atom N
                                    
                                    If( (s==3).AND.(1<n).AND.(n<SITE) ) Then  ! atom C
                                        jj=SeqIndex(n+1,2)
                                        If(jj==j) Then
                                            XXX = X(k,j,i,s) + 0.25*DX(n+1,t) + 0.75*DX(n,t)
                                            YYY = Y(k,j,i,s) + 0.25*DY(n+1,t) + 0.75*DY(n,t)
                                            ZZZ = Z(k,j,i,s) + 0.25*DZ(n+1,t) + 0.75*DZ(n,t)
                                            BBB = 0.25*Deform(n+1) + 0.75*Deform(n)
                                        EndIF ! (jj==j)
                                    EndIF ! ( (s==3).AND.(1<n).AND.(n<SITE)   ! atom C
                                    
                                    If( (s==4).AND.(1<n).AND.(n<SITE) ) Then  ! atom O
                                        jj=SeqIndex(n+1,2)
                                        If(jj==j) Then
                                            XXX = X(k,j,i,s) + 0.50*DX(n+1,t) + 0.50*DX(n,t)
                                            YYY = Y(k,j,i,s) + 0.50*DY(n+1,t) + 0.50*DY(n,t)
                                            ZZZ = Z(k,j,i,s) + 0.50*DZ(n+1,t) + 0.50*DZ(n,t)
                                            BBB = 0.50*Deform(n-1) + 0.50*Deform(n)
                                        EndIF ! (jj==j)
                                    EndIF ! ( (s==4).AND.(1<n).AND.(n<SITE)   ! atom O
                                    
                                    
!                                    Write(90,55) DUMMY6,D1INT,ATNAME,DUMMY3,A1,D2INT,XXX,YYY,ZZZ,R1, BBB
                                    If(t==mt) Write(91,55) DUMMY6,D1INT,ATNAME,DUMMY3,A1,D2INT,XXX,YYY,ZZZ,R1, BBB
                                    If(t==mt) Write(92,55) DUMMY6,D1INT,ATNAME,DUMMY3,A1,D2INT,XXX,YYY,ZZZ,R1, BBB
                                    
                                EndIf ! ( (vd(k,j,i,s)==1).AND.(1<=s).AND.(s<=4) ) 
                                
                                If(vd(k,j,i,s)==2) Then
                                    ct=ct+1
                                    D1INT=ct
                                    n = seq(k,j,i,40)
                                    XXX = X(k,j,i,s) + DX(n,t)
                                    YYY = Y(k,j,i,s) + DY(n,t)
                                    ZZZ = Z(k,j,i,s) + DZ(n,t)
                                    BBB = Deform(n)
!                                    Write(90,55) DUMMY6,D1INT,ATNAME,DUMMY3,A1,D2INT,XXX,YYY,ZZZ,R1, BBB
                                    If(t==mt) Write(91,55) DUMMY6,D1INT,ATNAME,DUMMY3,A1,D2INT,XXX,YYY,ZZZ,R1, BBB
                                    If(t==mt) Write(92,55) DUMMY6,D1INT,ATNAME,DUMMY3,A1,D2INT,XXX,YYY,ZZZ,R1, BBB
                                EndIf                                                    
                                
                            EndIF  ! IF(vd(k,j,i,s)>0)
                            
                        Enddo  ! s
                    Enddo  ! i
                    ct=ct+1
!                    Write(90, '(A6,I5,6x,A3,1x,A1,I4)') 'TER   ', ct, DUMMY3, A1, D2INT
                    If(t==mt) Write(91, '(A6,I5,6x,A3,1x,A1,I4)') 'TER   ', ct, DUMMY3, A1, D2INT
                    If(t==mt) Write(92, '(A6,I5,6x,A3,1x,A1,I4)') 'TER   ', ct, DUMMY3, A1, D2INT
                    
                Enddo  ! j
            
            Enddo ! k    
            
!            Write(90,'(A6)') 'ENDMDL'        
            If(t==mt) Write(91,'(A6)') 'END                            '        
            If(t==mt) Write(92,'(A6)') 'END                            '        
       
            
       Enddo ! t=1, Nstep
       
!       Write(90,'(A)') 'END                                                     '        
       
       
55	   FORMAT(A6,I5,1X,A4,1X,A3,1X,A1,I4,4X,3F8.3,2F6.2)
       
! .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-.  Movie Generating Loop       
       
       Write(*,'(A)') '   '
       Write(*,'(A)') '.-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. '
              
!       Close(90) ! File=PDBid//'_Trajectory.pdb'
       Close(91) ! File=PDBid//'_Itr'//ACE(itrt,2)//'.pdb'
       Close(92) ! File=PDBid//'_Itr'//'.pdb'

! =====E==N===D====================================================================================== !
       Return
       End                           