Viewing contents of file '../idllib/uit/pro/color2phot.pro'
;
;
;+
; NAME:
;	COLOR2PHOT
; PURPOSE:
;	Extracts objects from a UITPHOT table, selecting those with
;	colors and magnitudes between an interactively specified ranges.
;
;	This is a SHOWPHOT procedure.
; CALLING SEQUENCE:
;	flg = color2phot([sel, WIDTH=wdth, SILENT=slnt, TXTWDGT=outtext,
;		BASWDGT=baswdgt,] INSWDGT=instext)
;	sel       - A structure array containing position/magnitude information
;	            for the selected stars.  Each element contains:
;			XI     - The x coordinate of the star in the image.
;			YI     - The y coordinate of the star in the image.
;			XT     - The x coordinate of the star in the table.
;			YT     - The y coordinate of the star in the table.
;			RA     - The right ascension of the star, in degrees.
;			DEC    - The declination of the star, in degrees.
;			EQNX   - The equinix of the celestial coordinates.
;			MAG1   - The first aperture magnitude of the star.
;			ERR1   - The first aperture magnitude error.
;			MAG2   - The second aperture magnitude of the star.
;			ERR2   - The second aperture magnitude error.
;			MAG3   - The third aperture magnitude of the star.
;			ERR3   - The third aperture magnitude error.
;			MAGPSF - The PSF magnitude of the star.
;			ERRPSF - The PSF magnitude error.
;			SKY    - The sky flux.
;			ERRSKY - The sky error.
;			IND    - The index in the table of the star.
;			FILTER - The table's filter.
;			COLOR  - A structure containing the UV color of the
;			         star.  This structure contains the color, and
;			         two strings indicating the filters used.
;	returned  - The number of positions that have been selected and
;	            extracted.
; KEYWORDS:
;	BASWDGT   - The widget id for the most basic widget.  This is the
;	            widget maintaining the SHOWPHOT command menu and is
;	            used here to desensitize that menu while objects are
;	            being extracted from the table.
;	INSWDGT   - The instruction widget.  This is where the instructions
;	            for the use of this function are written.  This keyword
;	            is mandatory.
;	SILENT    - If present and non-zero AND if sel has been specified on
;	            the command line, information concerning each selected star
;	            is not written to the screen.
;	TXTWDGT -   If present and non-negative, and if FILE has not been 
;	            specified, the output is written to this widget instead of 
;	            the standard output.
;	WIDTH     - Defines the size of the full-width half-max used in the
;	            centroid, the size of the boxes displayed about the stars,
;	            and the maximum separation between the table and image
;	            star positions defining a match.  1/3 of this number is
;	            used to determine the maximum separation.
; COMMON BLOCKS:
;	Image_Blk    The image common block.  See IMG_INIT for details.
;	Table_Bk1    The first FITS table block.  See TAB_INIT for details.
;	Table_Bk2    The second FITS table block.  See TAB_INIT for details.
;	Color_Share  Provides the event handler with the variables it needs
;	             from COLOR2PHOT.
; PROCEDURE:
;	A set of widgets are created to allow the user to interactively
;	specify the ranges (through a set of sliding bars).  The
;	user finalizes the range by pressing either the Abort button (which
;	cancels further processing), or one of the Done buttons (one will 
;	cause the objects' info to be written to the Output widget while the
;	other suppresses this output).  All objects with colors and magnitudes
;	between the range are then extracted from the table.
; MODIFICATION HISTORY:
;	Written by Michael R. Greason, Hughes STX, 1 May 1992.
;	Position indexing problem corrected.  MRG, HSTX, 16 July 1992.
;-
; -----------------------------------------------------------------------------
;		The local event handler.
;
PRO c2phot_event, ev
;
;			Define common blocks.
;
COMMON Color_Share, cmin, cmax, mmin, mmax, wdth, slnt, winc, winp, $
	stttx1, stttx2, stttx3, stttx4, stttx5, butstat, mag, col
;
;			Decode the event.  Start with button events.
;
type = tag_names(ev, /STRUCTURE)
IF (type EQ 'WIDGET_BUTTON') THEN BEGIN
	widget_control, ev.id, GET_UVALUE=value
	CASE value OF
		  90 :	BEGIN
			butstat = 1
			widget_control, ev.top, /DESTROY
			END
		  91 :	BEGIN
			butstat = 2
			widget_control, ev.top, /DESTROY
			END
		  92 : 	BEGIN
			butstat = 0
			widget_control, ev.top, /DESTROY
			END
		ELSE : value = 9
	ENDCASE
ENDIF
;
;			Now deal with slider events.
;
IF (type EQ 'WIDGET_SLIDER') THEN BEGIN
	widget_control, ev.id, GET_VALUE=value, GET_UVALUE=uvalue
	CASE uvalue OF
		   1 :	BEGIN		; Upper magnitude limit.
			mmax = float(value) / 10.
			IF (mmax GE mmin) THEN BEGIN
				mmax = mmin - 0.1
				widget_control, ev.id, SET_VALUE=fix(mmax*10.)
			ENDIF
			END
		   2 :	BEGIN		; Lower magnitude limit.
			mmin = float(value) / 10.
			IF (mmax GE mmin) THEN BEGIN
				mmin = mmax + 0.1
				widget_control, ev.id, SET_VALUE=fix(mmin*10.)
			ENDIF
			END
		  11 :	BEGIN		; Upper color limit.
			cmax = float(value) / 10.
			IF (cmax LE cmin) THEN BEGIN
				cmax = cmin + 0.1
				widget_control, ev.id, SET_VALUE=fix(cmax*10.)
			ENDIF
			END
		  12 :	BEGIN		; Lower color limit.
			cmin = float(value) / 10.
			IF (cmax LE cmin) THEN BEGIN
				cmin = cmax - 0.1
				widget_control, ev.id, SET_VALUE=fix(cmin*10.)
			ENDIF
			END
		ELSE :	value = 9
	ENDCASE
;
;			Determine the number of objects within the limits.
;
	n = n_elements(where(((mag GE mmax) AND (mag LE mmin)) AND $
	                     ((col LE cmax) AND (col GE cmin))))
	widget_control, stttx1, SET_VALUE=string(mmin, FORMAT="(F6.2)")
	widget_control, stttx2, SET_VALUE=string(mmax, FORMAT="(F6.2)")
	widget_control, stttx3, SET_VALUE=string(cmin, FORMAT="(F6.2)")
	widget_control, stttx4, SET_VALUE=string(cmax, FORMAT="(F6.2)")
	widget_control, stttx5, SET_VALUE=string(n, FORMAT='(I5)')
;
;			Plot the new limits.
;
	wset, winc
	device, COPY=[0, 0, 512, 512, 0, 0, winp]
	xrng = [cmin, cmax]
	yrng = [mmin, mmax]
	oplot, xrng, [mmin, mmin], LINESTYLE=5
	oplot, xrng, [mmax, mmax], LINESTYLE=5
	oplot, [cmin, cmin], yrng, LINESTYLE=5
	oplot, [cmax, cmax], yrng, LINESTYLE=5
ENDIF
;
RETURN
END
; -----------------------------------------------------------------------------
;		COLOR2PHOT proper.
;
FUNCTION color2phot, sel, WIDTH=wdth, SILENT=slnt, TXTWDGT=outtext, $
	INSWDGT=instext, BASWDGT=baswdgt
;
;			Define common blocks.
;
COMMON Image_Blk, sclx, scly, cn, im, hd
COMMON Table_Bk1, t1hdr, t1hdrt, t1tab, xt1, yt1
COMMON Table_Bk2, avail2, t2hdr, t2hdrt, t2tab, xt2, yt2, xind1, xind2, xsep
COMMON Color_Share, cmin, cmax, mmin, mmax, wth, snt, winc, winp, $
	stttx1, stttx2, stttx3, stttx4, stttx5, butstat, mag, col
;
;			Return if only one table is available or if there are
;			no common objects.
;
i1 = where(xind1 NE (-1))
IF ((avail2 EQ 0) OR (i1(0) LT 0)) THEN RETURN, 0
;
;			Check parameters.
;
np = n_params(0)
IF ((NOT keyword_set(slnt)) OR (np LT 1)) THEN slnt = 0 ELSE slnt = 1
IF (NOT keyword_set(wdth)) THEN wdth = 5
IF (n_elements(outtext) EQ 0) THEN outtext = -1
IF (n_elements(instext) EQ 0) THEN BEGIN
	print, 'COLOR2PHOT -- The INSWDGT keyword must be specified.'
	RETALL
ENDIF
IF (n_elements(baswdgt) LE 0) THEN baswdgt = -1
;
;			Initialize.
;
wth = wdth
snt = slnt
origwin = !d.window
;
;			Define the sel_star structure.
;
c = {uv_color, color:0., colerr:0., fil1:"  ", fil2:"  "}
sel = {sel_star, xi:0., yi:0., xt:0., yt:0., ra:0., dec:0., eqnx:2000., $
       mag1:0., err1:0., mag2:0., err2:0., mag3:0., err3:0., $
       magpsf:0., errpsf:0., sky:0., errsky:0., ind:0, filter:'  ', color:c}
tmp = sel
;
;			Write instructions to the Info widget.
;
widget_control, instext, SET_VALUE="This portion of SHOWPHOT is used to " + $
	"select stars by defining ranges in"
widget_control, instext, /APPEND, SET_VALUE="both color and magnitude.  " + $
	"Use the two pairs of sliding scales to define"
widget_control, instext, /APPEND, SET_VALUE="the upper and lower limits " + $
	"to the magnitude and the color; these scales"
widget_control, instext, /APPEND, SET_VALUE="return integers in units of " + $
	"tenths of a magnitude.  When done, press the"
widget_control, instext, /APPEND, SET_VALUE="appropriate button.  Abort " + $
	"cancels this operation.  Done - Print extracts"
widget_control, instext, /APPEND, SET_VALUE="the stars and writes the " + $
	"results to the Output Window.  Done - Noprint"
widget_control, instext, /APPEND, SET_VALUE="extracts the stars but does " + $
	"not write the results to the window."
;
;			Extract and sort the magnitudes from the table.
;
mag = ftget(t1hdrt, t1tab, 'AP1 MAG')
mag2 = ftget(t2hdrt, t2tab, 'AP1 MAG')
i2 = xind1(i1)
mag = mag(i1)
mag2 = mag2(i2)
fil1 = strupcase(strtrim(sxpar(t1hdr,'FILTER'),2))
fil2 = strupcase(strtrim(sxpar(t2hdr,'FILTER'),2))
IF (strmid(fil1,0,1) EQ 'B') THEN BEGIN
	col = mag - mag2
	xtitl = 'Color -- ' + fil1 + ' - ' + fil2
ENDIF ELSE BEGIN
	col = mag2 - mag
	xtitl = 'Color -- ' + fil2 + ' - ' + fil1
ENDELSE
mmin = float(fix(max(mag) + 0.9) < 26)
mmax = float(fix(min(mag) - 0.9) > (-5))
cmin = (min(col) - 0.5) > (-10.)
cmax = (max(col) + 0.5) < 10.
;
;			Create the necessary widgets.
;
magbas = widget_base(/COLUMN, TITLE='SHOWPHOT - Select by Color')
;
magdrw = widget_draw(magbas, /FRAME, RETAIN=2, XSIZE=512, YSIZE=512)
;
limbas = widget_base(magbas, /ROW, XPAD=20)
limbasm = widget_base(limbas, /COLUMN)
limdum = widget_label(limbasm, VALUE='Magnitude Limits')
limdrmu = widget_slider(limbasm, MAXIMUM=fix(mmin * 10.), UVALUE=1, $
	MINIMUM=fix(mmax * 10.), VALUE=(mmax * 10.), TITLE='Upper Limit')
limdrml = widget_slider(limbasm, MAXIMUM=fix(mmin * 10.), UVALUE=2, $
	MINIMUM=fix(mmax * 10.), VALUE=(mmin * 10.), TITLE='Lower Limit')
limdum = widget_label(limbas, VALUE='       ', XSIZE=40)
limbasc = widget_base(limbas, /COLUMN)
limdum = widget_label(limbasc, VALUE='Color Limits')
limdrcu = widget_slider(limbasc, MAXIMUM=fix(cmax * 10.), UVALUE=11, $
	MINIMUM=fix(cmin * 10.), VALUE=(cmax * 10.), TITLE='Upper Limit')
limdrcl = widget_slider(limbasc, MAXIMUM=fix(cmax * 10.), UVALUE=12, $
	MINIMUM=fix(cmin * 10.), VALUE=(cmin * 10.), TITLE='Lower Limit')
;
sttbas = widget_base(magbas, /COLUMN, /FRAME)
sttbs1 = widget_base(sttbas, /ROW)
sttlb = widget_label(sttbs1, VALUE='Magnitude limits: ')
stttx1 = widget_text(sttbs1, YSIZE=1, XSIZE=6, $
	VALUE=string(mmin, FORMAT="(F6.2)"))
sttlb = widget_label(sttbs1, VALUE=' -- ')
stttx2 = widget_text(sttbs1, YSIZE=1, XSIZE=6, $
	VALUE=string(mmax, FORMAT="(F6.2)"))
sttbs2 = widget_base(sttbas, /ROW)
sttlb = widget_label(sttbs2, VALUE='Color limits: ')
stttx3 = widget_text(sttbs2, YSIZE=1, XSIZE=6, $
	VALUE=string(cmin, FORMAT="(F6.2)"))
sttlb = widget_label(sttbs2, VALUE=' -- ')
stttx4 = widget_text(sttbs2, YSIZE=1, XSIZE=6, $
	VALUE=string(cmax, FORMAT="(F6.2)"))
sttbs3 = widget_base(sttbas, /ROW)
stttx5 = widget_text(sttbs3, YSIZE=1, XSIZE=5, $
	VALUE=string(n_elements(mag), FORMAT='(I5)'))
sttlb = widget_label(sttbs3, VALUE=' objects between the limits')
;
butbas = widget_base(magbas, /ROW)
butd = widget_button(butbas, VALUE='Done - Print', UVALUE=90)
butd = widget_button(butbas, VALUE='Done - No Print', UVALUE=91)
buta = widget_button(butbas, VALUE='Abort', UVALUE=92)
widget_control, magbas, /REALIZE
;
;			Create a pixmap and plot the magnitudes to it.
;			Then copy the plot to the draw widget and overplot
;			the limits.
;
widget_control, magdrw, GET_VALUE=winc
window, /FREE, /PIXMAP, XSIZE=512, YSIZE=512
winp = !d.window
xrng = [cmin, cmax]
yrng = [mmin, mmax]
plot, col, mag, yrange=yrng, xrange=xrng, psym=1, xtitle=xtitl, $
	ytitle=("Magnitude -- " + fil1), title="Color Magnitude Diagram"
wset, winc
device, COPY=[0, 0, 512, 512, 0, 0, winp]
oplot, xrng, [mmin, mmin], LINESTYLE=5
oplot, xrng, [mmax, mmax], LINESTYLE=5
oplot, [cmin, cmin], yrng, LINESTYLE=5
oplot, [cmax, cmax], yrng, LINESTYLE=5
;
;			Execute.
;
butstat = 0
xmanager, 'color2phot', magbas, EVENT_HANDLER='c2phot_event', /MODAL
;
;			Extract the info from the mag. limits.
;
IF (butstat NE 0) THEN BEGIN
;
;				Make sure the limits are in the right order.
;
	IF (mmin LT mmax) THEN BEGIN
		t = mmin
		mmin = mmax
		mmax = t
	ENDIF
	IF (cmin GT cmax) THEN BEGIN
		t = cmin
		cmin = cmax
		cmax = t
	ENDIF
;
;				Find the magnitudes within the limits.
;
	ind = where(((mag GE mmax) AND (mag LE mmin)) AND $
	            ((col LE cmax) AND (col GE cmin)))
	IF (ind(0) GE 0) THEN BEGIN
		IF ((butstat GT 1) OR (slnt NE 0)) THEN slntn = 1 ELSE slntn = 0
		ind = i1(ind)
		chngxy, t1hdr, xt1(ind), yt1(ind), hd, x, y
		n = n_elements(x) - 1
		FOR j = 0, n DO BEGIN
			inds = ind(j)
			flg = ext_phot(inds, x(j), y(j), wdth, slntn, tmp)
			IF ((flg NE 0) AND (slntn EQ 0)) THEN BEGIN
				show_star, wdth, 255, INDEX=inds
				disp_entry, tmp, TXTWDGT=outtext
			ENDIF
			IF (flg NE 0) THEN sel = [sel, tmp]
		ENDFOR
	ENDIF
ENDIF
;
;			Prepare to return.  Get rid of the extraneous
;			element in sel (if no stars were extracted, 
;			set sel to zero).
;
ns = n_elements(sel) - 1
IF (ns GE 1) THEN sel = sel(1:ns) ELSE sel = 0
wdelete, winp
wset, origwin
widget_control, instext, SET_VALUE='  '
;
RETURN, ns
END