c use main body of mean_Val
	parameter (k4=4096)
	parameter (maxbyte=2*2880+k4*k4*4)
	parameter (nbyte=k4*k4*4)
	character*80 head(72)
	byte buf(maxbyte),a(nbyte)
	character*30 f1,c*1
	logical logi
	equivalence (buf,head),(buf(5761),a)

	ibatch=iargc()
	if(ibatch.eq.0)then
 	  write(*,*)' Usage:  balance batch_file [!] 60224'
	  stop
	endif

	call getarg(1,f1)	! get batch filename
	open(1,file=f1,status='old')
	c=' '
	if(ibatch.eq.2)call getarg(2,c)
10	read(1,'(a)',end=999)f1
	k=index(f1,'.')
        if(k.eq.0)f1=f1(1:lnblnk(f1))//'.fit'
        inquire(file=f1,exist=logi)
        if(.not.logi)stop 'data not found!'
c check if already done ?
	open(3,file=f1,status='old',access='direct',recl=1440*4)
	read(3,rec=1)head
	read(head(4)(21:),*)n1
	read(head(5)(21:),*)n2
        ipos=indexpos(head,'CRVAL1  ')
	read(head(ipos)(21:),*)nc1
	close(3)
	read(head(2)(23:),*)k
	if(k.ne.-32)then
	  write(*,*)f1,' not p*.fit'
	  goto  10
	endif
        ipos=indexpos(head,'END     ')
	if(ipos.eq.73)then
          write(*,*)f1,' must PURE p*.fit, before IRAF'
          goto 10
        endif 				
	f1=f1(1:lnblnk(f1))//char(0)
	call filesize(f1,mbyte)
	if(c.eq.'!')goto 11
        ipos=indexpos(head,'VOLT7   ')
	if(ipos.eq.73)ipos=indexpos(head,'MEANVAL ')
	if(head(ipos)(1:7).eq.'MEANVAL')then
	  read(head(ipos)(23:),*)x
	  write(*,*)f1,' already done mean value',x
	  goto 10
	endif
11	call dumpread(f1,buf,mbyte)
	mbyte=n1*n2*4+5760	!2880*2+image	
	call swap4(a,n1*n2*4)			!dec
        ipos=indexpos(head,'PZERO   ')
	if(ipos.eq.73)ipos=indexpos(head,'BZERO   ')
	read(head(ipos)(20:),*)f64
        head(ipos)(1:1)='P'
        head(ipos+1)(1:1)='P'
	if(f64.ne.0)call f64_0(a,n1,n2,head,f64)
	call mean(a,n1,n2,x,nc1)
        ipos=indexpos(head,'VOLT7   ')
	if(ipos.eq.73)ipos=indexpos(head,'MEANVAL ')
	head(ipos)='MEANVAL =                     '
     *  //' / MEAN VALUE OF FRAME'
	write(head(ipos)(23:30),'(f8.2)')x
	write(*,*)f1(1:25),'   Mean value: ',x
	call swap4(a,n1*n2*4)			!dec
	call dumpwrite('1.fm',buf,mbyte)
	call unlink(f1)
	call system('mv 1.fm '//f1)
	goto 10
999	close(1)
	end

	subroutine mean(a,n1,n2,x,nc1)
c take 2:2,2:3,3:2,3:3  1/4 of whole ccd frame do statistic, mode method
	real a(n1,n2)
	integer*4 ihist(30000)
        do 10 i=1,30000
10      ihist(i)=0
        n=0
        n11=n1/2-512
	n22=n2/2+512
	if(n11.lt.1)n11=1
	if(n22.gt.n2)n22=n2
        n3=2048-nc1
	do 70 j=n11,n22
	do 70 i=n3-512,n3
        k=a(i,j)+101
        if(k.lt.1.or.k.gt.30000)goto 70
        ihist(k)=ihist(k)+1
        n=max(k,n)
70      continue
        call histat(ihist,mode,maxh,xmean,xpeak,sigma,gsigma,n)
        xleft=xpeak-100.0

        do 110 i=1,30000
110     ihist(i)=0
        n=0
	do 170 j=n11,n22
	do 170 i=n3+1,n3+512+1
        k=a(i,j)+101
        if(k.lt.1.or.k.gt.30000)goto 170
        ihist(k)=ihist(k)+1
        n=max(k,n)
170     continue
        call histat(ihist,mode,maxh,xmean,xpeak,sigma,gsigma,n)
        x=xpeak-100.0

	y=x/xleft
	do 270 j=1,n2
	do 270 i=1,n3
270     a(i,j)=a(i,j)*y
	end

	subroutine f64_0(a,n1,n2,head,f64)
	real a(n1*n2)
	character*80 head(72)
        ipos=indexpos(head,'PZERO   ')
	if(ipos.eq.73)ipos=indexpos(head,'BZERO   ')
	head(ipos)(21:24)='  0.'
	do 10 i=1,n1*n2
10	a(i)=a(i)+f64
	end 

	subroutine dumpread(f1,buf,mbyte)
	character*(*) f1
	byte buf(mbyte)
	open(41,file=f1,status='old',access='direct',recl=mbyte)
	read(41,rec=1)buf
	close (41)
	end

	subroutine dumpwrite(f1,buf,mbyte)
	character*(*) f1
	byte buf(mbyte)
	call unlink(f1)
	open(41,file=f1,status='new',access='direct',recl=mbyte) 
	write(41,rec=1)buf
	close (41)
	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
