	parameter (n=9000)
	parameter (second=4)               ! err_bar =2 arcsec
	parameter (gapmag=0.8)
	character*50 f0,f1,f2,f3,f4
	character*80 head(72)
	real ax2(n),ay2(n),az2(n)
	real mag(100)
	real*8 a8(8)

        if(iargc().eq.0)then
	  write(*,*)
	  write(*,*)'                 Usage: all3 fit_file   97,7,3'
	  write(*,*)
	  write(*,*)' jobs will be done:'
	  write(*,*)'     1. j_find'
	  write(*,*)'     2. j_phot'
	  write(*,*)'     3. j_xy3ad  out star_table j_xy3ad.tmp from *.ap'
	  write(*,*)'                 & out gscno.par (frame center)'
	  write(*,*)'     4. gscno_z  out usno_table gscno_z.tmp'
	  write(*,*)' output star_table file: (threshold: 2 arcsec)'    
	  write(*,*)'     *.res   objects not marched usno_table'
	  write(*,*)'     *.bri   objects brighter 0.8mag than usno_table'
	  write(*,*)'     *.all   objects marched usno_table'
	  write(*,*)
	  write(*,*)' further command: all4 (deal with muti *.res & *.bri)'
	  write(*,*)
	  stop
	endif  

        pi=4.*datan(1d0)
        cx=pi/12.
        cy=pi/180.

        call getarg(1,f0)
	k=index(f0,'.')
	if(k.eq.0)f0=f0(1:lnblnk(f0))//'.fit'
	inquire(file=f0,exist=i)
	if(i.eq.0)stop '  file not existed!    '

	call gethead(f0,head)
	call geta8(head,a8)

	k=index(f0,'.')
	f1=f0(1:k-1)
	f2=f0(1:k)//'res'
	f3=f0(1:k)//'bri'
	f4=f0(1:k)//'all'
	
	call system('j_find '//f1)
	call system('j_phot '//f1)
	call system('j_xy3ad '//f1)               ! out j_xy3ad.tmp
	call system('gscno_z gscno.par 2000 58')  ! out gscno_z.tmp

	open(1,file='gscno.par',status='old')
	read(1,'(a)')f0
	close(1)
	call itohd3(f0,alpha,delta,xmag)
	cosdelta=cos(delta*cy)

	i=0
	open(1,file='gscno_z.tmp',status='old')         ! gscno_z
	read(1,'(a)',end=41)f0
	read(1,'(a)',end=41)f0

40	read(1,'(a)',end=41)f0
	i=i+1
	call itohd3(f0,alpha,delta,xmag)
	ax2(i)=alpha*54000.*cosdelta
	ay2(i)=delta*3600.
	az2(i)=xmag
	goto 40
41	close(1)
	n2=i

c to get mag_bias
	open(1,file='j_xy3ad.tmp',status='old')		! j_xy3ad
	read(1,'(a)',end=22)f0
	read(1,'(a)',end=22)f0
	i=0
20	read(1,'(a)',end=22)f0
	call itohd3(f0,alpha,delta,xmag)
	x=alpha*54000.*cosdelta
	y=delta*3600.
	r=30.
	do 21 j=1,n2
	z=(ax2(j)-x)**2+(ay2(j)-y)**2
	if(z.lt.r)k=j
21	if(z.lt.r)r=z
	if(r.lt.second)then
	  i=i+1
	  mag(i)=az2(k)-xmag
	  if(i+1.gt.100)goto 22 
	endif
	goto 20
22	close(1)
	call stat_mag(mag,i,zm,m)		

	write(*,*)'----------------------'
	open(2,file=f2,status='unkown')
	open(3,file=f3,status='unkown')
	open(4,file=f4,status='unkown')

	write(2,'(a,f6.2)')'  mag_bias: ',zm
	write(2,*)'  epoch 2000.0         batc'
	write(3,'(a,f6.2)')'  mag_bias: ',zm
	write(3,*)'  epoch 2000.0         batc usno'
	write(4,'(a,f6.2)')'  mag_bias: ',zm
	write(4,*)'  epoch 2000.0         batc usno'

	open(1,file='j_xy3ad.tmp',status='old')		! j_xy3ad
	read(1,'(a)',end=31)f0
	read(1,'(a)',end=31)f0

30	read(1,'(a)',end=31)f0
	call itohd3(f0,alpha,delta,xmag)
	x=alpha*54000.*cosdelta
	y=delta*3600.
	r=30.
	do 80 j=1,n2
	z=(ax2(j)-x)**2+(ay2(j)-y)**2
	if(z.lt.r)k=j
80	if(z.lt.r)r=z
	if(r.gt.second)then
	  write(2,'(a)')f0
	else
	  x=az2(k)-(xmag+zm)
	  if(x.gt.gapmag)write(3,'(a,f5.1)')f0(1:28),az2(k)
	  write(4,'(a,f5.1)')f0(1:28),az2(k)
	endif 

	goto 30
31	close(1)
	close(2)
	close(3)
	close(4)
	write(*,*)' last out_put file is: '//f2
	write(*,*)'                     : '//f3
	write(*,*)'                     : '//f4
	end

	subroutine stat_mag(a,n,z,m)
	real a(n)
100	x=0
	m=0
	do 10 i=1,n
	if(a(i).lt.99)then
	  x=x+a(i)
	  m=m+1
	endif
10	continue
	z=x/m

	xx=0
	do 20 i=1,n
20	if(a(i).lt.99) xx=xx+(a(i)-z)**2
	zz=sqrt(xx/m)
	zz=3*zz
	   write(*,*)m,zz,z
	mm=0
	do 30 i=1,n
	  if(abs(a(i)-z).gt.zz)then
	    a(i)=99.99
	  else
	    mm=mm+1
          endif
30	continue
	if(mm.ne.m)goto 100
	end	

        subroutine gethead(f1,head)
        character*50 f1
	character*80 head(72)
        open(52,file=f1,status='old',access='direct',recl=5760)
        read(52,rec=1)head
        close (52)
        end

        subroutine geta8(head,a8)
        real*8 a8(8)
        character*80 head(72)
        ipos=indexpos(head,'A81     ')-1
	if(ipos.gt.70)stop '  not batc_fits_file!    '
        do 10 i=1,8
10      read(head(i+ipos)(12:),*)a8(i)
        end

        function indexpos(head,f1)
        character*80 head(72),f1*8
        do 10 indexpos=1,72
10      if(head(indexpos)(1:8).eq.f1)return
        end


	subroutine itohd3(cc,alpha,delta,xmag)
	character*80 cc
	character*80 dd
	k1=index(cc(2:),' ')
	k2=index(cc,',')
	k=min(k1,k2)
	if(k1.eq.0)k=k2
	if(k2.eq.0)k=k1
	if(k.lt.2.or.k.gt.13)goto 100
	dd=cc(1:k)
	do 10 i=1,9
10	if(dd(i:i).eq.':')dd(i:i)=','
	x=0
	y=0
	z=0
	read(dd(1:),*,end=15,err=100)x,y,z
15	alpha=x+y/60.+z/3600.
	do 16 i=k+1,k+6
	if(cc(i:i).ne.' ')goto 17
16	continue
	goto 100
17	dd=cc(i:)
	k1=index(dd,' ')
	k2=index(dd,',')
	k=min(k1,k2)
	if(k1.eq.0)k=k2
	if(k2.eq.0)k=k1
	do 20 i=1,k
20	if(dd(i:i).eq.':')dd(i:i)=','
	x=0
	y=0
	z=0
	read(dd(1:k),*,end=30,err=100)x,y,z
30	if(x.lt.0.)x=-x
	delta=x+y/60.+z/3600.
	if(index(cc,'-').ne.0)delta=-delta
	read(dd(k+1:),*,end=100,err=100)xmag
100	return
	end
