       !************************************************************************
       !         A Subroutine, generating a the network frame for PyMOL        *
       !************************************************************************
       
       Subroutine Network_illustration ( PDBid, rcut, SITE, xx, yy, zz ) 
       
       Implicit None

! ---- Variables : ---------------------------------------------------------------------------- !       
       Character   PDBid*4
       Real        rcut      ! cutoff distance (in A)
       
       Integer     SITE
       Real(8)     xx(1:SITE), yy(1:SITE), zz(1:SITE)    ! Coordinates in 1-D

! ---- Variables : ---------------------------------------------------------------------------- !       
       Character   Chartemp*120
       
       Real        radius_node
       Real        radius_spring 
       
       Character   Color_node*15          ! The color name
       Character   Color_spring*15
       Character   Color_edge*15

       Real         Color1_R, Color1_G, Color1_B   ! The color of the nodes
       Real         Color2_R, Color2_G, Color2_B   ! The color of the springs
       Real         Color3_R, Color3_G, Color3_B   ! The color of the springs' edges
       Integer      Status            ! The status of this subroutine, 1:Successful; 0:Fail 

       Real         springl ! The spring period length 
       Real         dbratio ! The dash/blank ratio
       
! ---- Variables : ---------------------------------------------------------------------------- !       

       Integer      FileUnit
      
       Real(8)      bx, by, bz
       Real(8)      rij
       
       Integer      nrd  ! total number of rings
       Real(8)      Lrd  ! the length of each ring
       
       Real(8)      rax, ray, raz
       Real(8)      rbx, rby, rbz
       
       Real(8)      tax, tay, taz
       Real(8)      tbx, tby, tbz
              
       Real(8)      h1, h2
       
! ---- Variables : ---------------------------------------------------------------------------- !       
       Integer     i, j, k, n
        
! ===== Body of Subroutine Network_illustration ================================================!
       
       
        Open(21, File='Network_illustration.inp')
       
        Read (21,'(A)') chartemp
        Read (21,'(A)') chartemp
        Read (21,*)     radius_node

        Read (21,'(A)') chartemp
        Read (21,'(A)') chartemp
        Read (21,*)     radius_spring
        
        Read (21,'(A)') chartemp
        Read (21,'(A)') chartemp
        Read (21,'(A15)')     Color_node
        Call Color_Palette ( Color_node, Color1_R, Color1_G, Color1_B, Status )
        If(status==0) Stop 'Error! Unkonwn color input [Color_node]'
       
        Read (21,'(A)') chartemp
        Read (21,'(A)') chartemp
        Read (21,'(A15)')     Color_spring
        Call Color_Palette ( Color_spring, Color2_R, Color2_G, Color2_B, Status )
        If(status==0) Stop 'Error! Unkonwn color input [Color_spring]'           
        
        Read (21,'(A)') chartemp
        Read (21,'(A)') chartemp
        Read (21,'(A15)')     Color_edge
        Call Color_Palette ( Color_edge, Color3_R, Color3_G, Color3_B, Status )
        If(status==0) Stop 'Error! Unkonwn color input [Color_edge]'  
        
        Read (21,'(A)') chartemp
        Read (21,'(A)') chartemp     
        Read (21,*) springl ! The spring period length 
        
        Read (21,'(A)') chartemp
        Read (21,'(A)') chartemp     
        Read (21,*) dbratio ! The spring period length         
       
        close(21) ! 'Network_illustration.inp'
! +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=       
        
        FileUnit = 6875
        
        Open(FileUnit, File=PDBid//'_network.py' ) 

! - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - nodes' spheres        
    
        Write(FileUnit, '(A)') 'from pymol.cgo import *'
        Write(FileUnit, '(A)') 'from pymol import cmd  '
        Write(FileUnit, '(A)') 'import math            '
        
        Write(FileUnit, '(A)') '                       '        
        Write(FileUnit, '(A)') 'obj = [                '
        
        Write(FileUnit,'(A,3(F5.3,A2))') '   COLOR, ', Color1_R, ', ',Color1_G, ', ',Color1_B, ', '
        
        Do i=1, SITE
            Write(FileUnit,'(A,4(F8.3,A2))') '   SPHERE  , ', xx(i), ', ', yy(i), ', ', zz(i), ', ', radius_node, ', '                  
        Enddo ! i
 
        Write(FileUnit, '(A)') '       ]               '
        Write(FileUnit, '(A)') '                       '
        Write(FileUnit, '(5A)') 'cmd.load_cgo( obj, ',  "'", 'nodes', "'", ') '     
        Write(FileUnit, '(A)') '                       '
        Write(FileUnit, '(A)') '                       '
        

! - -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - springs' cylinders     

        Write(FileUnit, '(A)') '                       '        
        Write(FileUnit, '(A)') 'obj = [                '    
       
        Do j=1,Site !==================================================================== Do j=1,Site

            Do k=1,Site ! +++++++++++++++++++++++++++++++++ Do k=1,Site

                bx = xx(j) - xx(k)
                by = yy(j) - yy(k)
                bz = zz(j) - zz(k)
                
                rij = bx*bx + by*by + bz*bz 
                
                If( (j.ne.k) .and. (rij<rcut*rcut) ) then      
                
                    nrd = Sqrt(rij) / springl     
                    Lrd = Sqrt(rij) / (nrd*1.0) 
                    
                    h1 = 0.5 * Lrd / (1+dbratio)  ! half length of the 'black' part
                    h2 = Lrd - h1*2                 ! length of the 'ring' part
                    
                    Do n = 1, nrd
                        
                        rax = xx(k) + (n-1) * ( xx(j) - xx(k) ) / (nrd*1.0) 
                        ray = yy(k) + (n-1) * ( yy(j) - yy(k) ) / (nrd*1.0) 
                        raz = zz(k) + (n-1) * ( zz(j) - zz(k) ) / (nrd*1.0)
                        
                        rbx = xx(k) + n * ( xx(j) - xx(k) ) / (nrd*1.0) 
                        rby = yy(k) + n * ( yy(j) - yy(k) ) / (nrd*1.0) 
                        rbz = zz(k) + n * ( zz(j) - zz(k) ) / (nrd*1.0) 
                        
                        tax = rax + ( rbx - rax ) * 0.5 / (1.0+dbratio)
                        tay = ray + ( rby - ray ) * 0.5 / (1.0+dbratio)
                        taz = raz + ( rbz - raz ) * 0.5 / (1.0+dbratio)
                        
                        tbx = ( rbx + rax ) * 0.5
                        tby = ( rby + ray ) * 0.5
                        tbz = ( rbz + raz ) * 0.5                        
                        
                        Write(FileUnit, '(A,7(F10.5,A1) )' ) '     CYLINDER, ', tax, ',', tay, ',', taz, ',', tbx, ',', tby, ',', tbz, ',', radius_spring, ','
                        Write(FileUnit, '(A,6(F7.3 ,A1) )' ) '               ', Color3_R, ',', Color3_G, ',', Color3_B, ',', Color2_R, ',', Color2_G, ',', Color2_B, ','
                        
                        tax = tbx
                        tay = tby
                        taz = tbz
                        
                        tbx = rax + ( rbx - rax ) * (0.5+dbratio) / (1.0+dbratio)
                        tby = ray + ( rby - ray ) * (0.5+dbratio) / (1.0+dbratio)
                        tbz = raz + ( rbz - raz ) * (0.5+dbratio) / (1.0+dbratio)

                        Write(FileUnit, '(A,7(F10.5,A1) )' ) '     CYLINDER, ', tax, ',', tay, ',', taz, ',', tbx, ',', tby, ',', tbz, ',', radius_spring, ','
                        Write(FileUnit, '(A,6(F7.3 ,A1) )' ) '               ', Color2_R, ',', Color2_G, ',', Color2_B, ',', Color3_R, ',', Color3_G, ',', Color3_B, ','
                        
                                                
                    Enddo ! n

                EndIf ! If( (j.ne.k) .and. (rij<rcut) ) 
             
            Enddo ! ++++++++++++++++++++++++++++++++++++++++++ Do k=1,Site

        Enddo !============================================================================== Do j=1,Site
       
       
        Write(FileUnit, '(A)') '       ]               '
        Write(FileUnit, '(A)') '                       '
        Write(FileUnit, '(5A)') 'cmd.load_cgo( obj, ',  "'", 'springs', "'", ') '                    

        
        Close(FileUnit) ! Filename(1:15)//'_Arrows.py', DEFAULTFILE=OutputPath) 
    
       
! =====E==N===D================================================================== !
       Return
       End    