C this program modified 12/31/97 (!) to print out any high significance
C stars that we missed that had mag differences  smaller than -0.25...
C ---plm
C this program modified 6/15/95 so that it works correctly on unsorted
C input files.  Need id,xc,yc,mag,merr for wc frame, wn frame, and ct
C frame.
C
C added in ".coo" file on 16/jun/95
C+++++++++++++++
C
C
C
C
C
C latest modification 5/30/84
C                     6/5/84 Fixed bug with end of frame hits
C Modified for the SUN on 6/13/95, believe it or not! M33 project
C with Olivia
C---------------
	parameter (imax=80,lotsos=5000)
	DIMENSION IBIN(imax),MODWCT(500),MODWNT(500),MODWCN(500)
	DIMENSION WCNCOM(lotsos),WCTCOM(lotsos),WNTCOM(lotsos)
	DIMENSION WCNCOMN(lotsos),WCTCOMN(lotsos),WNTCOMN(lotsos)
	CHARACTER*40 WNFILE,WCFILE,CTFILE,field
	character*80 fileout1,fileout2,fileout3
	character*10 offsets
        INTEGER*4 WC(lotsos,2),WN(lotsos,2),CT(lotsos,2)
        DIMENSION WCM(lotsos),WCE(lotsos),WNM(lotsos),WNE(lotsos),
     .CTM(lotsos),CTE(lotsos),WCX(lotsos),WCY(lotsos),WNX(lotsos),WNY
     .(lotsos),CTX(lotsos),CTY(lotsos)

	data dxwcn,dywcn,dxwct,dywct/-0.0,-0.5,-0.0,-1.5/
C
C
C Read in sorted .NST files (listed in FILES.DAT) and put
C into direct access files for easier handling.
C
701	write(6,601)
601	format('Input WC file name:',$)
	read(5,1) wcfile
	OPEN(UNIT=1,NAME=WCFILE,STATUS='OLD',err=701)
	close(unit=1)
702	write(6,602)
602	format('Input WN file name:',$)
	read(5,1) wnfile
	OPEN(UNIT=1,NAME=WNFILE,STATUS='OLD',err=702)
	close(unit=1)
703	write(6,603)
603	format('Input CT file name:',$)
	read(5,1) ctfile
	OPEN(UNIT=1,NAME=CTFILE,STATUS='OLD',err=703)
	write(6,604)
604	format('Input field name for output files:',$)
	read(5,1) field
	write(6,1) field
	fileout1=''
	fileout2=''
	fileout3=''
	do 605 i=1,40
	if(field(i:i).eq.'') then
	fileout1=field(1:i-1)//'ansOfe.ans'
	fileout2=field(1:i-1)//'ans.all'
	fileout3=field(1:i-1)//'ansOfe.coo'
	OPEN(UNIT=7,NAME=fileout1,STATUS='NEW')
COPEN(UNIT=8,NAME=fileout2,STATUS='NEW')
	OPEN(UNIT=9,NAME=fileout3,STATUS='NEW')
	go to 606
	end if
605	continue
	stop 'field name too long'
1	FORMAT(A40)
606	write(6,610) dxwcn
610	format('Difference in X for WC-WN:(',f6.2,')',$)
	read(5,608) offsets
608	format(a10)
	if(offsets.ne.'') read(offsets,*) dxwcn
	write(6,611) dywcn
611	format('Difference in Y for WC-WN:(',f6.2,')',$)
	read(5,608) offsets
	if(offsets.ne.'') read(offsets,*) dywcn
	write(6,612) dxwct
612	format('Difference in X for WC-CT:(',f6.2,')',$)
	read(5,608) offsets
	if(offsets.ne.'') read(offsets,*) dxwct
	write(6,613) dywct
613	format('Difference in Y for WC-CT:(',f6.2,')',$)
	read(5,608) offsets
	if(offsets.ne.'') read(offsets,*) dywct
	DXWNT=DXWCT-DXWCN
	DYWNT=DYWCT-DYWCN
	write(6,615) dxwcn,dywcn,dxwct,dywct,dxwnt,dywnt
615	format('Adopting differences dx, dy:'/,10x,'WC-WN:',2f10.2,
     &  /10x,'WC-CT:',2f10.2,
     &  /10x,'WN-CT:',2f10.2)
	OPEN(UNIT=1,NAME=WCFILE,STATUS='OLD')
	OPEN(UNIT=2,STATUS='scratch',ACCESS='DIRECT',
     .FORM='UNFORMATTED',RECL=36)
	IFLAG=0
5	IU=IFLAG+2
3	FORMAT(A20)
	IK=0
2       FORMAT(1X,I5,4F9.3)
810	READ(1,*,END=850) IWCC,WCXX,WCYY,WCMM,WCEE
Cwrite(6,*) iwcc,wcxx,wcyy,wcmm,wcee
	IF(WCXX.LE.0) GO TO 810
        IK=IK+1
        WRITE(IU'IK) IWCC,WCXX,WCYY,WCMM,WCEE
	GO TO 810
850	IFLAG=IFLAG+1
	CLOSE(UNIT=1)
	GO TO (855,860,8100), IFLAG
855	OPEN(UNIT=1,NAME=WNFILE,STATUS='OLD')
	OPEN(UNIT=3,STATUS='scratch',ACCESS='DIRECT',
     .FORM='UNFORMATTED',RECL=36)
 	NWC=IK
C NUMBER OF IMAGES WITH PHOTOMETRY IN WC FILE
	WRITE(7,1101)WCFILE,NWC
	WRITE(6,1101)WCFILE,NWC
	WRITE(8,1101)WCFILE,NWC
1101	FORMAT(1X,'WC FILE',A40,I5,' STARS')
	GO TO 5
860	OPEN(UNIT=1,NAME=CTFILE,STATUS='OLD')
	OPEN(UNIT=4,STATUS='scratch',ACCESS='DIRECT',
     .FORM='UNFORMATTED',RECL=36)
	NWN=IK
C NUMBER IN WN FILE
	WRITE(7,102)WNFILE,NWN
	WRITE(8,102)WNFILE,NWN
	WRITE(6,102)WNFILE,NWN
102	FORMAT(1X,'WN FILE',A40,I5,' STARS')
	GO TO 5
8100	NCT=IK
C NUMBER OF IMAGES IN CT FILE
	WRITE(7,103)CTFILE,NCT
	WRITE(8,103)CTFILE,NCT
	WRITE(6,103)CTFILE,NCT
103	FORMAT(1X,'CT FILE',A40,I5,' STARS')
C
C
C
        N1=0
	N2=0
	IK=0
	IJ=0
	N3=0
C AVERAGE DIFFERENCES EXPECTED BASED ON EXPERIENCES WITH THE FILTERS
C SO FAR, IN THE SENCE COMPUTED LATER ON
	TOL=2.0
	SWCNX=0.
	SWCNY=0.
	SWNTX=0.
	SWNTY=0.
	SWCTX=0.
	SWCTY=0.
10      IK=IK+1
	IF(IK.GT.NWC) GO TO 50
	READ(2'IK) IWC,WCXX,WCYY,WCMM,WCEE
	IF(WCXX.EQ.0.) GO TO 10
	WCM(IK)=WCMM
	WCE(IK)=WCEE
	WCX(IK)=WCXX
	WCY(IK)=WCYY
	IHIT=0
	BIG=3*TOL
	IFLG=0
	IJ=0
C start from the beginning of the WN list each time: BIG CHANGE!
20      IJ=IJ+1
	IF(IJ.GT.NWN) GO TO 655
        READ(3'IJ) IWN,WNXX,WNYY,WNMM,WNEE
	IF(WNXX.EQ.0) GO TO 20
	WNM(IJ)=WNMM
	WNE(IJ)=WNEE
	WNX(IJ)=WNXX
	WNY(IJ)=WNYY
	DELTX=WCXX-WNXX-DXWCN
	DELTY=WCYY-WNYY-DYWCN
	IF((DELTY.LT.TOL).AND.(IFLG.EQ.0)) THEN
	IFLG=1
	IJ0=IJ
	END IF 
C RECORD THE IJTH NUMBER OF WHEN IT FIRST GETS TO WITHIN TOL IN Y
	DIST=SQRT(DELTX**2+DELTY**2)
	IF((DIST.LT.TOL).AND.(DIST.LT.BIG)) THEN
C	   WE HAVE A HIT 
C	   SAVE PARAMETERS SO WE CAN CHOICE THE CLOSEST
		BIG=DIST
		IHIT=1
C	WRITE(6,222)
D222	FORMAT(1X,'IDENTIFIED AS A HIT')
		IWCC=IK
		IWNN=IJ
		DX=DELTX
		DY=DELTY
		GO TO 20
C	   GO SEE IF THERE IS AN EVEN CLOSER ONE
	END IF
C
C
C
C
C
	GO TO 20
C KEEP LOOKING UNTIL WE ARE WELL BELOW IT IN Y
C	
655	CONTINUE
 	IF(IHIT.EQ.1) THEN
		IF(WN(IWNN,1).GT.0) THEN
C THIS IS THE SECOND HIT WHICH IDENTIFIES THIS PARTICULAR WN STAR
C SEE IF ITS CLOSER OR FURTHER AWAY
		OLDX=WCX(WN(IWNN,1))-WNX(IWNN)
		OLDY=WCY(WN(IWNN,1))-WNY(IWNN)
		OLDD=SQRT(OLDX**2+OLDY**2)
D	WRITE(6,771) IWCC,IWNN,BIG,OLDD
D771	FORMAT(1X,'SECOND HIT FOR A WN STAR',2I5,2F6.2)
		IF(OLDD.LT.BIG) GO TO 25
D	WRITE(6,772) BIG
D772	FORMAT(1X,'KEPT NEW ONE; NEW DISTANCE=',F6.2)
C IF ITS CLOSER NOW, CONTINUE.  IF NOT, FORGET IT AND LOOK FOR A NEW WC
		END IF
	 	WC(IWCC,1)=IWNN
		WN(IWNN,1)=IWCC
		N1=N1+1
		SWCNX=SWCNX+DX
		SWCNY=SWCNY+DY
	END IF
25	IJ=IJ0-1
	IF(IJ.LT.1)IJ=0
	IF(IJ.GE.NWN) GO TO 50
	GO TO 10
C
C
C
C  NOW DO THE SAME FOR WC VS CT
C
C
50	IK=0
	IL=0

	WRITE(6,53)N1
53	FORMAT(1X,'TOTAL HITS THIS GROUP=',I5)
51      IK=IK+1
	IF(IK.GT.NWC) GO TO 100
	READ(2'IK) IWC,WCXX,WCYY,WCMM,WCEE
	IF(WCXX.EQ.0) GO TO 51
	IHIT=0
	BIG=3.*TOL
	IFLG=0
	IL=0
C Start from the beginning of the CT list each time
C BIG CHANGE!
60	IL=IL+1
	IF(IL.GT.NCT) GO TO 101
        READ(4'IL) ICT,CTXX,CTYY,CTMM,CTEE
	IF(CTXX.EQ.0) GO TO 60
	CTM(IL)=CTMM
	CTE(IL)=CTEE
	CTX(IL)=CTXX
	CTY(IL)=CTYY
	DELTX=WCXX-CTXX-DXWCT
	DELTY=WCYY-CTYY-DYWCT
	IF((DELTY.LT.TOL).AND.(IFLG.EQ.0)) THEN
	IFLG=1
	IL0=IL
	END IF
	DIST=SQRT(DELTX**2+DELTY**2)
C	IF( ( (IK.GT.260). AND. (IK.LT.265) ) .OR.
C    .( (IK.GT.310) .AND.(IK.LT.315) ) ) 
C    .WRITE(6,211)IK,IL,WCXX,WNXX,WCYY,WNYY,DELTX,DELTY,DIST
	IF((DIST.LT.TOL).AND.(DIST.LT.BIG)) THEN
		BIG=DIST
		IHIT=1
C	WRITE(6,222)
		IWCC=IK
		ICTT=IL
		DX=DELTX
		DY=DELTY
		GO TO 60
	END IF
	GO TO 60
C OK NOW WEVE FINISHED THIS WC STAR
101	CONTINUE
	IF(IHIT.EQ.1) THEN
		IF(CT(ICTT,1).GT.0) THEN
C THIS IS THE SECOND HIT WHICH IDENTIFIES THIS PARTICULAR CT STAR
C SEE IF ITS CLOSER OR FURTHER AWAY
		OLDX=WCX(CT(ICTT,1))-CTX(ICTT)
		OLDY=WCY(CT(ICTT,1))-CTY(ICTT)
		OLDD=SQRT(OLDX**2+OLDY**2)
D	WRITE(6,722) IWCC,ICTT,BIG,OLDD
D722	FORMAT(1X,'SECOND HIT FOR A CT STAR',2I5,2F6.2)
		IF(OLDD.LT.BIG) GO TO 55
D	WRITE(6,723) BIG
D723	FORMAT(1X,'KEPT NEW ONE; DIST=',F6.2)
C IF ITS CLOSER NOW, CONTINUE.  IF NOT, FORGET IT AND LOOK FOR A NEW WC
		END IF
D	IF(IWCC.EQ.262) WRITE(6,721) IWCC,ICTT
D721	FORMAT(1X,'IDENTIFIED CT STAR FOR 262:',2I6)
		WC(IWCC,2)=ICTT
		CT(ICTT,1)=IWCC
C PROVIDE LINKAGE
	WN(WC(IWCC,1),2)=ICTT
	CT(ICTT,2)=WC(IWCC,1)
C
		N2=N2+1
		SWCTX=SWCTX+DX
		SWCTY=SWCTY+DY
	END IF
55	IL=IL0-1
	IF(IL.LT.1)IL=0
	IF(IL.GE.NCT) GO TO 100
	GO TO 51
100     IL=0
	WRITE(6,53) N2
C Now do this for WN and CT:
	IJ=0
110     IJ=IJ+1
	IF(IJ.GT.NWN) GO TO 150
	READ(3'IJ) IWN,WNXX,WNYY,WNMM,WNEE
	IF(WNXX.EQ.0) GO TO 110
	IHIT=0
	BIG=3.*TOL
	IFLG=0
	IL=0
C Start from the beginning of the CT each time
120	IL=IL+1
	IF(IL.GT.NCT) GO TO 151
	READ(4'IL) ICT,CTXX,CTYY,CTMM,CTEE
	IF(CTXX.EQ.0) GO TO 120
D	IF(CTX(IL).NE.CTXX) STOP
D	IF(CTY(IL).NE.CTYY) STOP
D	IF(CTM(IL).NE.CTMM) STOP
D	IF(CTE(IL).NE.CTEE) STOP
	DELTX=WNXX-CTXX-DXWNT
	DELTY=WNYY-CTYY-DYWNT
D	IF( (IJ.GT.215). AND. (IJ.LT.230) ) 
D    .WRITE(6,211)IJ,IL,WNXX,CTXX,WNYY,CTYY,DELTX,DELTY,DIST
	IF((DELTY.LT.TOL).AND.(IFLG.EQ.0)) THEN
	IFLG=1
	IL0=IL
	END IF
	DIST=SQRT(DELTX**2+DELTY**2)
	IF((DIST.LT.TOL).AND.(DIST.LT.BIG)) THEN
		IWNN=IJ
		ICTT=IL
		BIG=DIST
		IHIT=1
D	WRITE(6,222)
		DX=DELTX
		DY=DELTY
		GO TO 120
	END IF
	GO TO 120
C NOW WE ARE DONE WITH THIS WN IMAGE
151	CONTINUE
	IF(IHIT.EQ.1) THEN
	IF (CT(ICTT,2).GT.0) THEN
C THIS IS THE SECOND HIT WHICH IDENTIFIES THIS PARTICULAR CT STAR
C SEE IF ITS CLOSER OR FURTHER AWAY
		OLDX=WNX(CT(ICTT,2))-CTX(ICTT)
		OLDY=WNY(CT(ICTT,2))-CTY(ICTT)
		OLDD=SQRT(OLDX**2+OLDY**2)
D	WRITE(6,527)IWNN,ICTT,BIG,OLDD
D527	FORMAT(1X,'SECOND HIT FOR A WN-CT STAR',2I5,2F6.2)
		IF(OLDD.LT.BIG) GO TO 525
CC IF ITS CLOSER NOW, CONTINUE.  IF NOT, FORGET IT AND LOOK FOR A NEW WN
D	WRITE(6,528) BIG
D528	FORMAT(1X,'KEPT SECOND ONE; DIST=',F6.1)
		END IF
		WN(IWNN,2)=ICTT
D	IF(IWNN.EQ.222) WRITE(6,526) WN(IWNN,1),IWNN,ICTT
D526	FORMAT(1X,'IWNN=222,WN(IWNN,1),IWNN,ICTT',3I8)
		CT(ICTT,2)=IWNN
C PROVIDE LINKAGE
	WC(WN(IWNN,1),2)=ICTT
	CT(ICTT,1)=WN(IWNN,1)
C
		N3=N3+1
		SWNTX=SWNTX+DX
		SWNTY=SWNTY+DY
	END IF
525	IL=IL0-1
	IF(IL.LT.0)IL=0
	IF(IL.GE.NCT) GO TO 150
	GO TO 110
150	SWCNX=SWCNX/N1
	SWCNY=SWCNY/N1
	SWCTX=SWCTX/N2
	SWCTY=SWCTY/N2
	SWNTX=SWNTX/N3
	SWNTY=SWNTY/N3
	WRITE(6,53) N3
	DO 9881 I=1,NCT
	IB=(CTM(I)*10-100)/5+1
C FIND BIN NUMBER
	IF(IB.LT.1)IB=1
	IF(IB.GT.imax)IB=imax
	IBIN(IB)=IBIN(IB)+1
9881	CONTINUE
	DO 9882 I=1,imax
	IF (IBIN(I).GT.IBIG) THEN
	IBIG=IBIN(I)
	INDX=I
	END IF
9882	CONTINUE
	AMAG=INDX-1
	AMAG=AMAG*5+100
	AMAG=AMAG/10.
	CTLIM=AMAG+0.5
	SUMWCN=0.
	SUMWCT=0.
	SUMWNT=0.
	ICOM=0
	DO 200 I=1,NWC
Cwrite(6,*) wc(i,1),wc(i,2),ctm(wc(i,2)),ctlim
	IF(WC(I,1)*WC(I,2).LE.0) GO TO 200
	IF(CTM(WC (I,2) ).GE.CTLIM) GO TO 200
	ICOM=ICOM+1
	WCNCOM(ICOM)=WCM(I)-WNM(WC(I,1))
	WCTCOM(ICOM)=WCM(I)-CTM(WC(I,2))
	WNTCOM(ICOM)=WNM(WC(I,1))-CTM(WN(WC (I,1) ,2))
	SUMWCN=SUMWCN+WCNCOM(ICOM)
	SUMWCT=SUMWCT+WCTCOM(ICOM)
	SUMWNT=SUMWNT+WNTCOM(ICOM)
Cwrite(6,*) i,sumwct,sumwnt,icom
	IWCN=250+WCNCOM(ICOM)*50.+.5
	IWCT=250+WCTCOM(ICOM)*50.+.5
	IWNT=250+WNTCOM(ICOM)*50.+.5
C	WRITE(6,203) ICOM,WCNCOM(ICOM),IWCN,
C    .                    WCTCOM(ICOM),IWCT,
C    .                    WNTCOM(ICOM),IWNT
203	FORMAT(1X,I5,3(F8.2,I8))
	if((iwcn.lt.1).or.(iwcn.gt.500)) go to 200
	if((iwct.lt.1).or.(iwct.gt.500)) go to 200
	if((iwnt.lt.1).or.(iwnt.gt.500)) go to 200
	MODWCN(IWCN)=MODWCN(IWCN)+1
	MODWCT(IWCT)=MODWCT(IWCT)+1
	MODWNT(IWNT)=MODWNT(IWNT)+1
200	CONTINUE
C NOW FIND THE MODES
	IBIGWCN=0
	IBIGWCT=0
	IBIGWNT=0
	DO 1501 I=1,500
	IF(MODWNT(I).GT.IBIGWNT) THEN
		IBIGWNT=MODWNT(I)
		AMODWNT=(I-250)/50.
	END IF
	IF(MODWCT(I).GT.IBIGWCT) THEN
		IBIGWCT=MODWCT(I)
		AMODWCT=(I-250)/50.
	END IF
	IF(MODWCN(I).GT.IBIGWCN) THEN
		IBIGWCN=MODWCN(I)
		AMODWCN=(I-250)/50.
	END IF
1501	CONTINUE
C FIND 0TH ORDER AVERAGE
Cwrite(6,*) sumwcn,icom
	CORWCN=SUMWCN/ICOM
	CORWCT=SUMWCT/ICOM
	CORWNT=SUMWNT/ICOM
C FIND NEXT ORDER
	SUMWCN=0.
	SUMWCT=0.
	SUMWNT=0.
	INEW=0
	DO 201 I=1,ICOM
	IF (ABS(WCNCOM(I)-CORWCN).GT..5)GO TO 201 
	IF (ABS(WCTCOM(I)-CORWCT).GT..5)GO TO 201
	IF (ABS(WNTCOM(I)-CORWNT).GT..5)GO TO 201
	SUMWCN=SUMWCN+WCNCOM(I)
	SUMWCT=SUMWCT+WCTCOM(I)
	SUMWNT=SUMWNT+WNTCOM(I)
	INEW=INEW+1	
	WCNCOMN(INEW)=WCNCOM(I)
	WCTCOMN(INEW)=WCTCOM(I)
	WNTCOMN(INEW)=WNTCOM(I)
201	CONTINUE
	CORWCN=SUMWCN/INEW
	CORWCT=SUMWCT/INEW
	CORWNT=SUMWNT/INEW
C NOW FIND SIGMA
	SUMWCN2=0.
	SUMWCT2=0.
	SUMWNT2=0.
	DO 1234 IK=1,iNEW
	SUMWCN2=SUMWCN2+(WCNCOMN(IK)-CORWCN)**2
	SUMWCT2=SUMWCT2+(WCTCOMN(IK)-CORWCT)**2
	SUMWNT2=SUMWNT2+(WNTCOMN(IK)-CORWNT)**2
1234	CONTINUE
	SUMWCN2=SUMWCN2/INEW
	SG1WCN=SQRT(SUMWCN2)
	SGMWCN=SG1WCN/SQRT(INEW-1.)
	SUMWCT2=SUMWCT2/INEW
	SG1WCT=SQRT(SUMWCT2)
	SGMWCT=SG1WCT/SQRT(INEW-1.)
	SUMWNT2=SUMWNT2/INEW
	SG1WNT=SQRT(SUMWNT2)
	SGMWNT=SG1WNT/SQRT(INEW-1.)
	WRITE(7,980)
980	FORMAT(1X,'OFFSETS',/8x,'ResX  ResY   N   Using:')
	WRITE(6,980)
	WRITE(8,980)
	WRITE(7,981) SWCNX,SWCNY,N1,DXWCN,DYWCN,
     .SWCTX,SWCTY,N2,DXWCT,DYWCT,SWNTX,SWNTY,N3,DXWNT,DYWNT
	WRITE(8,981) SWCNX,SWCNY,N1,DXWCN,DYWCN,
     .SWCTX,SWCTY,N2,DXWCT,DYWCT,SWNTX,SWNTY,N3,DXWNT,DYWNT
	WRITE(6,981) SWCNX,SWCNY,N1,DXWCN,DYWCN,
     .SWCTX,SWCTY,N2,DXWCT,DYWCT,SWNTX,SWNTY,N3,DXWNT,DYWNT
981	FORMAT(1X,'WC-WN',2F6.2,I5,2F6.2,/1X,'WC-CT',2F6.2,I5,2F6.2,
     //1X,'WN-CT',2F6.2,I5,2F6.2)
990	FORMAT(/1X,'The folowing corrections have been applied to',
     .' the colors:'/8X,'Corr.',' SE1   SEM    MD   N')
	WRITE(7,990)
	WRITE(6,990)
	WRITE(8,990)
	WRITE(6,991)CORWCN,SG1WCN,SGMWCN,AMODWCN,INEW,
     .              CORWCT,SG1WCT,SGMWCT,AMODWCT,INEW,
     .              CORWNT,SG1WNT,SGMWNT,AMODWNT,INEW
	WRITE(7,991)CORWCN,SG1WCN,SGMWCN,AMODWCN,INEW,
     .              CORWCT,SG1WCT,SGMWCT,AMODWCT,INEW,
     .              CORWNT,SG1WNT,SGMWNT,AMODWNT,INEW
	WRITE(8,991)CORWCN,SG1WCN,SGMWCN,AMODWCN,INEW,
     .              CORWCT,SG1WCT,SGMWCT,AMODWCT,INEW,
     .              CORWNT,SG1WNT,SGMWNT,AMODWNT,INEW
991	FORMAT(1X,'WC-WN',4F6.2,I4/1X,'WC-CT',4F6.2,I4
     ./1X,'WN-CT',4F6.2,I4)
	WRITE(7,995)
	WRITE(6,995)
	WRITE(8,995)
995	FORMAT(//1X,'The following are in all 3 data sets:',
     &  /'   WCX   WCY    WCM  WCE   WNM  WNE  CTM   CTE',
     &   '     WC-WN      WC-CT     WN-CT')
	IWCNT=0
	DO 1000 I=1,NWC
	IF((WC(I,1).GT.0).AND.(WC(I,2).GT.0)) THEN
D	WRITE(6,1009)I,WC(I,1),WC(I,2),WN(WC(I,1),1),
D    .WN(WC(I,1),2),CT(WC(I,2),1),CT(WC(I,2),2)
D1009   FORMAT(1X,7I9)
C SAME STAR IDENTIFIED IN ALL 3 DATA SETS
	 IWCNT=IWCNT+1
	 WCN=WCM(I)-WNM(WC(I,1))-CORWCN
C CORRECTED COLOR WC-WN
   	WRC=WCM(I)-CTM(WC(I,2))-CORWCT
C CORRECTED COLOR WC-CT
	WRN=WNM(WC(I,1))-CTM(WC(I,2))-CORWNT
C CORRECTED COLOR WN-CT
	ITEMP1=WC(I,2)
C CT STAR CORRESPONDING TO THIS WC STAR
	ITEMP2=WN(WC(I,1),2)
C CT STAR CORRESPONDING TO THIS WN STAR
C MAKE SURE THEY ARE THE SAME CT
	IF(ITEMP1.NE.ITEMP2) THEN
D		WRITE(6,1001) I,ITEMP1,ITEMP2
D1001		FORMAT(1X,'WOW, ARE THINGS FUCKED UP',
D    ./1X,'I,WC(I,2),WN((WC(I,1),2)=',3I8)
	WC(I,2)=0
	WN(I,2)=0
C SIMPLY CLAIM NEITHER OF THEM HAVE A CT COUNTERPART
	GO TO 1000
	END IF
	SIGWCN=SQRT(WCE(I)**2+WNE(WC(I,1))**2)
	SIGWRC=SQRT(WCE(I)**2+CTE(WC(I,2))**2)
	SIGWRN=SQRT(WNE(WC(I,1))**2+CTE(WC(I,2))**2)
	SIGC=0.-WRC/SIGWRC
	SIGN=0.-WRN/SIGWRN
	SIGT=WCN/SIGWCN
	if((wrc.ge.0.0).and.(wrn.ge.0.0)) go to 159
	IF(((SIGC.GT.5.0).OR.(SIGN.GT.5.0))
     /.AND.((WRC.ge.-.25).OR.(WRN.ge.-.25)) ) THEN
	write(9,*) wcx(i),wcy(i)
	WRITE(7,1005)WCX(I),WCY(I),WCM(I),WCE(I),
     .WNM(WC(I,1)),WNE(WC(I,1)),CTM(WC(I,2)),CTE(WC(I,2)),
     .WCN,SIGT,WRC,SIGC,WRN,SIGN
	WRITE(6,1005)WCX(I),WCY(I),WCM(I),WCE(I),
     .WNM(WC(I,1)),WNE(WC(I,1)),CTM(WC(I,2)),CTE(WC(I,2)),
     .WCN,SIGT,WRC,SIGC,WRN,SIGN
1005	FORMAT(1X,2F6.1,1X,3(F6.2,F5.2),3(F6.2,F5.1))
	END IF
159	continue
C ON UNIT 8, WRITE THEM ALL OUT
	WRITE(8,1005)WCX(I),WCY(I),WCM(I),WCE(I),
     .WNM(WC(I,1)),WNE(WC(I,1)),CTM(WC(I,2)),CTE(WC(I,2)),
     .WCN,SIGT,WRC,SIGC,WRN,SIGN
	END IF
1000	CONTINUE
	WRITE(6,1006)IWCNT
	WRITE(7,1006)IWCNT
	WRITE(8,1006)IWCNT
1006	FORMAT(1X,'There were ',i5,' stars in all 3 files.')
	IWCN=0
	WRITE(6,1007)CTLIM
	WRITE(7,1007)CTLIM
	WRITE(8,1007)
1007   FORMAT(/1X,'The following were in the WC and WN',
     .' frames only',/1X,' and were brighter than 2.5sig',F6.2)
	DO 2000 I=1,NWC
	IF((WC(I,1).GT.0).AND.(WC(I,2).EQ.0)) THEN
	IWCN=IWCN+1
	WCN=WCM(I)-WNM(WC(I,1))-CORWCN
	SIGWCN=SQRT(WCE(I)**2+WNE(WC(I,1))**2)
	SIGT=WCN/SIGWCN
	FAINTC=WCM(I)+2.5*WCE(I)
	FAINTN=WNM(WC(I,1))+2.5*WNE(WC(I,1))
	TCL=FAINTC-CTLIM-CORWCT
	TNL=FAINTN-CTLIM-CORWNT
	IF((TCL.LT.0.).OR.(TNL.LT.0)) THEN
	WRITE(6,2005)WCX(I),WCY(I),WCM(I),WCE(I),
     .WNM(WC(I,1)),WNE(WC(I,1)),WCN,SIGT
2005	FORMAT(1X,2F6.1,1X,2(F6.2,F5.2),11X,2(F6.2,F5.1))
	write(9,*) wcx(i),wcy(i)
	WRITE(7,2005)WCX(I),WCY(I),WCM(I),WCE(I),
     .WNM(WC(I,1)),WNE(WC(I,1)),WCN,SIGT
	END IF
	WRITE(8,2005)WCX(I),WCY(I),WCM(I),WCE(I),
     .WNM(WC(I,1)),WNE(WC(I,1)),WCN,SIGT
	END IF
2000	CONTINUE
	WRITE(7,2006)IWCN
	WRITE(6,2006)IWCN
	WRITE(8,2006)IWCN
2006	FORMAT(1X,'There were ',i5,' stars in WC and WN only')
	WRITE(6,2007)CTLIM
	WRITE(7,2007)CTLIM
	WRITE(8,2007)
2007	FORMAT(/1X,'The following are in WC only',
     ./1X,' and are 2.5 sig brighter than',F6.2)
	IWCK=0
	DO 3000 I=1,NWC
	IF((WC(I,1).EQ.0).AND.(WC(I,2).EQ.0))THEN
	IWCK=IWCK+1
	IF(WCX(I).EQ.0.) GO TO 3000
	FAINTC=WCM(I)+2.5*WCE(I)
	TCL=FAINTC-CTLIM-CORWCT
	IF(TCL.LT.0.) THEN
	write(9,*) wcx(i),wcy(i)
	WRITE(7,3005) WCX(I),WCY(I),WCM(I),WCE(I)
3005	FORMAT(1X,2F6.1,1X,F6.2,F5.2)
	WRITE(6,3005) WCX(I),WCY(I),WCM(I),WCE(I)
	END IF
	WRITE(8,3005) WCX(I),WCY(I),WCM(I),WCE(I)
	END IF
3000	CONTINUE
	WRITE(6,3007)IWCK
	WRITE(7,3007)IWCK
	WRITE(8,3007)IWCK
3007	FORMAT(1X,'There were ',i5,' stars in WC only')
	WRITE(6,3009)CTLIM
	WRITE(7,3009)CTLIM
	WRITE(8,3009)
3009	FORMAT(/1X,'The following are in WN only',
     ./1X,' and are brighter than',F6.2)
	IWNK=0
	DO 4000 I=1,NWN
	IF((WN(I,1).EQ.0).AND.(WN(I,2).EQ.0)) THEN
	IWNK=IWNK+1
	X=WNX(I)+SWCNX
	Y=WNY(I)+SWCNY
4003	FORMAT(1X,2F6.1,12X,F6.2,F5.2)
	FAINTN=WNM(I)+2.5*WNE(I)
	TNL=FAINTN-CTLIM-CORWNT
	IF(TNL.LT.0.) THEN
	WRITE(6,4003) X,Y,WNM(I),WNE(I)
	WRITE(7,4003) X,Y,WNM(I),WNE(I)
	write(9,*) x,y
	END IF
	WRITE(8,4003) X,Y,WNM(I),WNE(I)
	END IF
4000	CONTINUE
	WRITE(6,4001) IWNK
4001	FORMAT(1X,'There were ',i5,' stars in WN only')		
	WRITE(7,4001) IWNK
	WRITE(8,4001) IWNK
	WRITE(6,4007)
	WRITE(7,4007)
	WRITE(8,4007)
4007	FORMAT(/1X,'The following were in WC and CT only',
     ./1x,'and hence are not likely WRs unless something was',
     ./1X,'wrong in the WN filter exposure')
	IWCT=0
	DO 5000 I=1,NWC
	IF((WC(I,1).EQ.0).AND.(WC(I,2).GT.0)) THEN
	IWCT=IWCT+1
	WRC=WCM(I)-CTM(WC(I,2))-CORWCT
	SIGWRC=SQRT(WCE(I)**2+CTE(WC(I,2))**2)
	SIGC=0.-WRC/SIGWRC
	IF((SIGC.GT.2.).OR.(WRC.LT.-.25)) THEN
	write(9,*) wcx(i),wcy(i)
	WRITE(7,5005) WCX(I),WCY(I),WCM(I),WCE(I),
     .CTM(WC(I,2)),CTE(WC(I,2)),WRC,SIGC
5005	FORMAT(1X,2F6.1,1X,F6.2,F5.2,11X,F6.2,F5.2,1X,
     .10x,F6.2,F5.1)
	WRITE(6,5005) WCX(I),WCY(I),WCM(I),WCE(I),
     .CTM(WC(I,2)),CTE(WC(I,2)),WRC,SIGC
	END IF
	WRITE(8,5005) WCX(I),WCY(I),WCM(I),WCE(I),
     .CTM(WC(I,2)),CTE(WC(I,2)),WRC,SIGC
	END IF
5000	CONTINUE
	WRITE(6,5009)IWCT
	WRITE(7,5009)IWCT
	WRITE(8,5009)IWCT
5009	FORMAT(1X,'There were', i5, 'stars in WC and CT only')
	WRITE(6,5008)
	WRITE(7,5008)
	WRITE(8,5008)
5008	FORMAT(/1X,'The following were in WN and CT only')
	IWNT=0
	DO 6000 I=1,NWN
	IF((WN(I,1).EQ.0).AND.(WN(I,2).GT.0)) THEN
	IWNT=IWNT+1
	WRN=WNM(I)-CTM(WN(I,2))-CORWNT
	SIGWRN=SQRT(WNE(I)**2+CTE(WN(I,2))**2)
	SIGN=0.-WRN/SIGWRN
	X=WNX(I)+SWCNX
	Y=WNY(I)+SWCNY
	IF((SIGN.GT.2.).OR.(WRN.LT.-.25))THEN
	write(9,*) x,y
	WRITE(7,6005)X,Y,WNM(I),WNE(I),
     .CTM(WN(I,2)),CTE(WN(I,2)),WRN,SIGN
	WRITE(6,6005)X,Y,WNM(I),WNE(I),
     .CTM(WN(I,2)),CTE(WN(I,2)),WRN,SIGN
6005	FORMAT(1X,2F6.1,12X,2(F6.2,F5.2),22X,2(F6.2,F5.1))
	END IF
	WRITE(8,6005)X,Y,WNM(I),WNE(I),
     .CTM(WN(I,2)),CTE(WN(I,2)),WRN,SIGN
	END IF
6000	CONTINUE	
	WRITE(7,6007)IWNT
	WRITE(8,6007)IWNT
	WRITE(6,6007)IWNT
6007	FORMAT(1X,'There were ',i5,' stars in WN and CT only')
	CLOSE(UNIT=7)
	CLOSE(UNIT=8)
	STOP
	END
