	program ave 
	parameter(maxstars=250000)
	parameter(maxfiles=20)
	character*80 inline,garbage
	character*50 filename
	character*80 outline
	character*80 outline2
	character*9 idie
	character*10 xie,yie
	character*12 magie
	character*14 magerrie
	character*6 niteie
	dimension x(maxstars,maxfiles),y(maxstars,maxfiles),
     &  am(maxstars,maxfiles),ame(maxstars,maxfiles), 
     &  istat(maxstars,maxfiles),nstars(maxfiles) 
	dimension diff(maxstars,maxfiles),ndif(maxfiles)
	dimension arr(maxstars)
	data hit/2.5/
C written 10 April 2002
	outline2="           0.030       0.668       0     No_error"
	open(unit=1,file="tempshift",status='old')
C Get the name of the next file
	nfiles=0
	iz=0
1	read(1,2,end=11) inline
2	format(a80)
Cwrite(6,2) inline
	do 3 i=2,50
	if(inline(i:i).eq.' ') then
	read(inline(i+1:80),*) ix,iy,zeropt
Cwrite(6,*) ix,iy,zeropt
	go to 4
	end if
3	continue
	stop "problem finding the end of the file name in ave"
4	nfiles=nfiles+1
Cwrite(6,144) nfiles
144	format('Reading in file',i5)
	if(nfiles.gt.maxfiles) stop "too many files"
	filename=inline(1:i-1)//'.als.2'
	close(unit=2)
	istar=0
	open(unit=2,file=filename,status='old')
5	read(2,2,end=10) inline
Cwrite(6,2) inline
	if((nfiles.eq.1).and.(inline(1:1).eq.'#')) write(9,2) inline
	if(inline(1:1).eq.'#') go to 5
C OK, read in the 2nd part of the line
	read(2,2,end=10) garbage
	istar=istar+1
	if(istar.gt.maxstars) stop "too many stars"
	read(inline(1:79),*) id,xc,yc,amag,amerr
Cwrite(6,145) id,xc,yc,amag,amerr
145	format('Read in',i5,2f8.2,2f8.3)
	xcp=xc+ix
	ycp=yc+iy
	amagp=amag-zeropt
	x(istar,nfiles)=xcp
	y(istar,nfiles)=ycp
	am(istar,nfiles)=amagp
	ame(istar,nfiles)=amerr
	istat(istar,nfiles)=0
	go to 5
10	nstars(nfiles)=istar
	write(6,146) nfiles,istar
146	format('In file',i5,' there were',i5,' stars')
	go to 1
C OK, we now have nfiles worth of nstars(i) stars each.  Let's go looking
C for matches!
11      continue
C Take a star in the first file and see if it is in any of the other files:
C for each star in each file
	do 150 iff=1,nfiles
	ndif(iff)=0
150	continue
	do 100 i=1,nfiles
	do 200 j=1,nstars(i)
C Has this star been vetted?
	if(istat(j,i).ne.0) go to 200
C So now look at the subsequent files
	num=1
	sumerr=ame(j,i)
	if(ame(j,i).lt.0.005) then
	wt=1./.005**2
	else
	wt=1./ame(j,i)**2
	end if
	sumwt=wt
	sum=am(j,i)*wt
	istat(j,i)=1
C this is just a cheap way of having to deal with the final file separately.
	if(i.eq.nfiles) go to 410
	do 300 k=i+1,nfiles
	do 400 l=1,nstars(k)
	if(istat(l,k).ne.0) go to 400
	dist2=(x(j,i)-x(l,k))**2+(y(j,i)-y(l,k))**2
	if(dist2.lt.hit) then
C maybe a hit!
C See if the mags are in relatively OK agreement...
C	dm2=(am(j,i)-am(l,k))**2
C	err2=ame(j,i)**2+ame(l,k)**2
	num=num+1
	sumerr=sumerr+ame(l,k)
	if(ame(l,k).lt.0.005) then
	wt=1./.005**2
	else
	wt=1./ame(l,k)**2	
	end if
	sum=sum+am(l,k)*wt
	sumwt=sumwt+wt
	istat(l,k)=1
	if(i.eq.1) then
	if((ame(j,i).lt.0.05).and.(ame(l,k).lt.0.05)) then
	ndif(k-1)=ndif(k-1)+1
	diff(ndif(k-1),k-1)=am(j,i)-am(l,k)
	write(10,123) am(j,i)-am(l,k)
123	format(f10.3)
	end if
	end if
	go to 300
	end if
400	continue
300	continue
410	ave=sum/sumwt
	aveerr=(sumerr/num)/sqrt(float(num))
	sigerr=sqrt(1./sumwt)
	iz=iz+1
	do 4111 ij=1,79
4111	outline(ij:ij)=' '
	outline(80:80)='\\'
	write(idie,411) iz
411	format(i9)
	do 412 ij=1,9
	if(idie(ij:ij).ne.'') go to 413
412	continue
	stop 'problem with idie'
413	n=9-ij
Cwrite(6,*) ij,n
	outline(1:1+n)=idie(ij:9)
	write(xie,414) x(j,i)
414	format(f10.3)
	do 415 ij=1,10
	if(xie(ij:ij).ne.'') go to 416
415	continue
	stop "problem with xie"
416	n=10-ij
	outline(10:10+n)=xie(ij:10)
	write(yie,414) y(j,i)
        do 4155 ij=1,10
        if(yie(ij:ij).ne.'') go to 4166
4155    continue
        stop "problem with yie"
4166    n=10-ij
        outline(20:20+n)=yie(ij:10)
	write(magie,417) ave
417	format(f12.3)
	do 418 ij=1,12
	if(magie(ij:ij).ne.'') go to 419
418	continue
	stop "problem with magie"
419	n=12-ij
	outline(30:30+n)=magie(ij:12)
	write(magerrie,420) sigerr
420	format(f14.3)
	do 421 ij=1,14
	if(magerrie(ij:ij).ne.'') go to 422
421	continue
	stop "problem with magerrie"
422	n=14-ij
	outline(42:42+n)=magerrie(ij:14)
	outline(56:70)='2000.000       '
	write(niteie,423) num
423	format(i6)
	do 424 ij=1,6
	if(niteie(ij:ij).ne.'') go to 425
424	continue
	stop "problem with niteie"
425	n=6-ij
	outline(71:71+n)=niteie(ij:6)
	write(9,2) outline
	write(9,2) outline2
200	continue
100	continue
	do 500 ji=1,nfiles-1
	do 490 ii=1,ndif(ji)
	arr(ii)=diff(ii,ji)
490	continue
	call median(arr,ndif(ji),amed)
	write(6,491) ji+1,amed
491	format('Median difference between files 1 and',i3,' =',f10.3)
500	continue
	stop
	end
	include 'mediansing.f'
