       !************************************************************************
       !         A Subroutine of Main, reading in the PDB file                 *
       !************************************************************************

       Subroutine ReadPDB(PDBid, MinModel, MaxModel, MinChain, MaxChain, MinResidue, MaxResidue, MinAtom, MaxAtom, PDBerror, ATM, AANm, X, Y, Z, B, vd)
       
       Implicit None

! ---- Variables : -------------------------------------------------------------- !       
       Character*4 PDBid
       
       Integer    PDBerror  ! Error Code (process PDBerror at line 777; PDBerror=100 : no error)
       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
       
       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
       Real        B(  MinModel:MaxModel, MinChain:MaxChain, MinResidue:MaxResidue, MinAtom:MaxAtom )         ! B-factors
       
       Integer     vd(  MinModel:MaxModel, MinChain:MaxChain, MinResidue:MaxResidue, MinAtom:MaxAtom )        ! Record the existence of the ATOM :
                                                                                                              ! Amino Acids Atom   : vd=1
                                                                                                              ! Nucleic Acids Atom : vd=2
! ---- Variables : -------------------------------------------------------------- !       
       Integer ::   aaflag=0     ! indicate the Amino Acids(1) or Nucleic Acids(2), or Null (0)
       Integer      ICA,  D1INT, D2INT, Imodel, Ichain, Iresidue, Iatom
       Real         XXX, YYY, ZZZ, R1, BBB
       Integer      AAA
       Character*6  DUMMY6
       Character*4  ATNAME
       Character*3  DUMMY3 
       Character*80 DUMMY80 
       Character*1  A1
       Integer      i, j, k, s, ct
       
       Integer      ChainIndex             ! function, return a number for a given chain ID
                            
! ------------------------------------------------------------------------------- !
       
!===================================! Initializing vd(Mmodel,Mchain,Mresidue,Matom)
       Do k=MinModel, MaxModel
            Do j=MinChain, MaxChain
                Do i=MinResidue, MaxResidue
                    Do s=MinAtom, MaxAtom
                       vd(k,j,i,s)=0
                    Enddo
                Enddo
            Enddo
       Enddo
       ct=0


!++++++++++++++++++++++++++++++++++++! Begin Read in the PDB File

       Open(11, file=PDBid(1:4)//'.pdb')

       Imodel=1
       
310	   DUMMY6 = '      '
       READ(11,'(A)', ERR=330, END=330) DUMMY6

       If(DUMMY6(1:5)=='MODEL') Then !---------------Deal with Model 
            Backspace(11)
            Read(11, *) DUMMY6, D1INT
            Imodel=D1INT
       EndIf !-------------------------------------Deal with Model 
        
!       IF(DUMMY6=='END   ')  GOTO 330 !===============End of File, jump to 330
        
       IF(DUMMY6(1:4).NE.'ATOM')  GOTO 310
       
	   BACKSPACE(11)

       READ(11,55) DUMMY6,D1INT,ATNAME,DUMMY3,A1,D2INT,XXX,YYY,ZZZ,R1,BBB
              
       aaflag=0
       !===========[Amino Acids]============
       If( (DUMMY3=='ALA').OR.(DUMMY3=='ARG').OR.(DUMMY3=='ASN').OR.(DUMMY3=='ASP').OR.(DUMMY3=='ASX') ) aaflag=1
       If( (DUMMY3=='CYS').OR.(DUMMY3=='GLN').OR.(DUMMY3=='GLU').OR.(DUMMY3=='GLX').OR.(DUMMY3=='GLY') ) aaflag=1
       If( (DUMMY3=='HIS').OR.(DUMMY3=='ILE').OR.(DUMMY3=='LEU').OR.(DUMMY3=='LYS').OR.(DUMMY3=='MET') ) aaflag=1
       If( (DUMMY3=='PHE').OR.(DUMMY3=='PRO').OR.(DUMMY3=='SER').OR.(DUMMY3=='THR').OR.(DUMMY3=='TRP') ) aaflag=1
       If( (DUMMY3=='TYR').OR.(DUMMY3=='VAL')                                                          ) aaflag=1
      
      
       !===========[Nucleic Acids]============
       If( (DUMMY3=='  A').OR.(DUMMY3==' +A').OR.(DUMMY3=='  C').OR.(DUMMY3==' +C') ) aaflag=2
       If( (DUMMY3=='  G').OR.(DUMMY3==' +G').OR.(DUMMY3=='  I').OR.(DUMMY3==' +I') ) aaflag=2
       If( (DUMMY3=='  T').OR.(DUMMY3==' +T').OR.(DUMMY3=='  U').OR.(DUMMY3==' +U') ) aaflag=2
       If( (DUMMY3=='  T').OR.(DUMMY3==' +T').OR.(DUMMY3=='  U').OR.(DUMMY3==' +U') ) aaflag=2
                                           

	   If( (aaflag==1).OR.(aaflag==2) ) Then

	        Iresidue=D2INT
        	
   	        Ichain=ChainIndex(A1)

   	        Iatom=AAA(ATNAME)
   	        
!   	        If(Iatom==0) Then
!   	            PDBerror=104 ! 'Error: This atom name is not in the list: ',  ATNAME   	            
!   	            Goto 777
!   	        EndIf
            
!            write(*,*) IChain, Iresidue, Iatom
            
            ATM(Imodel,Ichain,Iresidue,Iatom)=ATNAME
            AANm(Imodel,Ichain,Iresidue,Iatom)=DUMMY3
            X(Imodel,Ichain,Iresidue,Iatom)=XXX
            Y(Imodel,Ichain,Iresidue,Iatom)=YYY
            Z(Imodel,Ichain,Iresidue,Iatom)=ZZZ
            B(Imodel,Ichain,Iresidue,Iatom)=BBB
            
            vd(Imodel,Ichain,Iresidue,Iatom)=aaflag
            
                    	
       EndIf ! If( (aaflag==1).OR.(aaflag==2) ) 
	   
    
	   GOTO 310	

330	   Continue
       Close(11)    !  file=PDBid(1:4)//'.pdb1' 
              
777    If(PDBerror==100) Then
          Write(*,*) '       '
          Write(9,*) '       '
          Write(*,*) PDBid, '  Successfully read in! '
          Write(9,*) PDBid, '  Successfully read in! '
          
       ElseIF(PDBerror==104) Then       
         Write(*,*) 'Error: This atom name is not in the list: [ATNAME] :',  ATNAME
         Write(9,*) 'Error: This atom name is not in the list: [ATNAME] :',  ATNAME
         Stop
         
       EndIF

55	   FORMAT(A6,I5,1X,A4,1X,A3,1X,A1,I4,4X,3F8.3,2F6.2)
       
       
! =====E==N===D================================================================== !
       Return
       End    