	subroutine setrc(xn2,rad3,chrgv4,iatout,ipdbwrt,natom)
c
	include "qdiffpar4.h"
c
	character*80 line,filnam
	character*6 head
	character*3 tres,sres
	character*1 schn
	character*6 tatm,satm
	character*4 snum
	character*5 atnum 
	character*24 crdstr
	character*13 radstr
	logical iatout,ipdbwrt
	dimension xn2(3,natmax),rad3(natmax),chrgv4(natmax),xo(3)
c
204	format(a80)
205	format(3f8.3)
c
	write(6,*) "assigning charges and radii..."
	write(6,*) " "
	natom=0
	open(unit=13,status='unknown',err=903)
	if(ipdbwrt) open(20,form='unformatted')
	if(iatout) then
	  open(unit=19)
	  filnam = ' '
	  inquire(19,name = filnam)
	  write(6,*)'atomic coordinates, charges and radii written'
	  write(6,*)' to file'
	  write(6,*)filnam
	  write(6,*)'   '
	  write(19,*)'HEADER output from qdiff'
	  write(19,*)'HEADER atom radii in columns 55-60'
	  write(19,*)'HEADER atom charges in columns 61-67'
	end if
c
c begin read
c
103   continue
        read(13,204,end=303)line
        head = line(1:6)
	  call up(head,6)
c
c skip header lines
c
        if((head.ne.'ATOM  ').and.(head.ne.'HETATM')) then
	    if(iatout.and.(head.eq.'HEADER')) then
		write(19,204)line
	    endif
	    go to 103
	  end if
	  natom = natom + 1
	  crdstr = line(31:54)
        decode(24,205,crdstr)xo
	  atm = line(12:16)
	  res = line(18:20)
	  rnum = line(23:26)
	  chn = line(22:22)
	  call up(atm,6)
	  call elb(atm,6)
	  call up(res,3)
	  call elb(res,3)
	  call up(rnum,4)
	  call elb(rnum,4)
	  call up(chn,1)
	  call elb(chn,1)
c
c assign radius, searching for decreasingly specific specification
c ending with generic atom type
c note all atoms must have an assignment
c
	  call rfind(atm,res,ifind,n)
	  if(ifind.eq.0) then
	    tres = '   '
	    call rfind(atm,tres,ifind,n)
	    if(ifind.eq.0) then
		tatm = atm(1:1)//'     '
	      call rfind(tatm,tres,ifind,n)
	      if(ifind.eq.0) then
	        write(6,*)'no radius record found for'
	        write(6,*)line
	        stop
		end if
	    end if
	  end if
	  rad = radt(n)
c
c store rad,xn in rad3,xn2  for later use
c
	rad3(natom)=rad
	do 606 ix=1,3
	xn2(ix,natom)=xo(ix)
606	continue
c
c
c assign charge to atom, searching for decreasingly specific specification
c kas,29jn 89- add extra searches with wild card:
C search order:  atom, res, num, chain
c		      x     x    x   x
c			x     x    x   
c			x     x        x
c			x     x
c			x          x   x
c			x          x
c			x              x
c			x
c
c note if no charge record found, is assumed to be 0.0
c
	  call cfind(atm,res,rnum,chn,ifind,n)
	  if(ifind.eq.0) then
	    schn = chn
	    chn = ' '
	    call cfind(atm,res,rnum,chn,ifind,n)
	    if(ifind.eq.0) then
		chn = schn
		snum = rnum
		rnum = '    '
	      call cfind(atm,res,rnum,chn,ifind,n)
	      if(ifind.eq.0) then
	        schn = chn
	        chn = ' '
	        call cfind(atm,res,rnum,chn,ifind,n)
		  if(ifind.eq.0) then
		    chn = schn
		    rnum = snum
		    sres = res
		    res = '   '
	          call cfind(atm,res,rnum,chn,ifind,n)
	          if(ifind.eq.0) then
	            schn = chn
	            chn = ' '
	            call cfind(atm,res,rnum,chn,ifind,n)
	            if(ifind.eq.0) then
		        chn = schn
		        snum = rnum
		        rnum = '    '
	              call cfind(atm,res,rnum,chn,ifind,n)
	              if(ifind.eq.0) then
	                schn = chn
	                chn = ' '
	                call cfind(atm,res,rnum,chn,ifind,n)
			  endif
			endif
		    endif
		  end if
		end if
	    end if
	  end if
c
c scale and assign charge to grid
c
	  chrgv = 0.0
	  chrgv4(natom)=0.0
	  if(ifind.ne.0) then
	  chrgv = chrgvt(n)
	  chrgv4(natom)=chrgv
	  end if
c
c write record to new coordinate file if required, with
c occupancy and temperature factor fields replaced by radius and charge
c
	  if(ipdbwrt) then
	  write(20) xo,rad,chrgv
	  end if
c 
	  if(iatout) then
	    chrgv = chrgv
          encode(13,206,radstr)rad,chrgv
	    line(55:67) = radstr
206       format(F6.2,F7.3)
	    write(19,204)line
	  end if

      go to 103
303   continue		
c	end of file
	close(13)
	if(iatout) close(19)
	if(ipdbwrt) close(20)
	goto 999
903	write(6,*) "exiting due to non-existence of atom file"
	stop
999	continue
	return
	end
