Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/contour_calib.pro'
;+
; PURPOSE:
;	Set fluxes at 2 points in mosaic image,
;	then setting factor and offset fields in mosaic_spec structure.
; CALLING:
;	contour_calib, mosaic_spec
; INPUT & OUTPUT:
;	mosaic_spec = structure with contour display specifications.
; HISTORY:
;	Written: Frank Varosi NASA/GSFC 1997.
;-

pro contour_calib, mosaic_spec, mosaic_image

	if N_struct( mosaic_spec ) NE 1 then return

	input = ""
	format = "(A30,G10.3)"
	point = "SOURCE"
	BELL = string( 7b )
	LF = string( 10b )
	init = 1
	menub = [ strtrim( 2*indgen(27)+3, 2 ), "ALL pixels" ]
	Magf = mosaic_spec.Magf
	sim = size( mosaic_image )
	maxw = Magf * sim
	max_xw = mosaic_spec.wx + maxw(1)
	max_yw = mosaic_spec.wy + maxw(2)
	window_set_show, mosaic_spec.windo
PICK:
	menu = [ "Select " + point + " Box Size", menub ]
	sel = wmenu( menu, INIT=init, TIT=0 )
	if (sel LE 0) then goto,PICK

	if (next_word( menu(sel) ) EQ "ALL") then begin
		boxsel = max( mosaic_spec.size_image(1:2) )
	  endif else boxsel = fix( menu(sel) )

	printw, ["select POINT to set "+point+" fluxes",$
		 "or RIGHT button to abort"], /ERASE
	tvcrs,0.5,0.5,/NORM

SELECT:	wait,0.3
	cursor, xc, yc, /DEV

	if (!mouse.button EQ 4) then begin
		printw,[" "," "],/ERASE
		return
	   endif

	if (!mouse.button EQ 2) then begin
		sel = wmenu( menu, INIT=sel, TITLE=0 )
		if (sel GT 0) then begin
			if (next_word( menu(sel) ) EQ "ALL") then begin
				boxsel = max( mosaic_spec.size_image(1:2) )
			  endif else boxsel = fix( menu(sel) )
		   endif
		tvcrs, xc, yc, /DEV 
		goto,SELECT
	   endif

	xw = (xc > mosaic_spec.wx) < max_xw
	yw = (yc > mosaic_spec.wy) < max_yw
	xi = round( float( xw - mosaic_spec.wx ) / Magf )
	yi = round( float( yw - mosaic_spec.wy ) / Magf )

	box_erase2
	boxhs = boxsel/2
	box_draw2, POS=[xw,yw], RADIUS=(boxsel*Magf/2)

	if (point EQ "SOURCE") then BEGIN
		xsrcmin = round_off( xi - boxhs ) > 0
		xsrcmax = ( xsrcmin + boxsel-1 ) < (sim(1)-1)
		ysrcmin = round_off( yi - boxhs ) > 0
		ysrcmax = ( ysrcmin + boxsel-1 ) < (sim(2)-1)
		msrc =  mosaic_image( xsrcmin:xsrcmax, ysrcmin:ysrcmax  )
		srcval = total( msrc )/N_elements( msrc )
		print," mean value in SOURCE box =",srcval,FORM=format
		point = "SKY"
		init=3
		print," select POINT for SKY values"
		wait,0.3
		goto,PICK
	  endif else if (point EQ "SKY") then BEGIN
		xskymin = round_off( xi - boxhs ) > 0
		xskymax = ( xskymin + boxsel-1 ) < (sim(1)-1)
		yskymin = round_off( yi - boxhs ) > 0
		yskymax = ( yskymin + boxsel-1 ) < (sim(2)-1)
		msky =  mosaic_image( xskymin:xskymax, yskymin:yskymax  )
		skyval = total( msky )/N_elements( msky )
		print," mean value in SKY box =",skyval,FORM=format
	   endif else begin
		message,"error: point="+point,/INFO
		return
	    endelse

	if !DEBUG gt 3 then stop
	read," enter new SOURCE value: ",input
	srcnew = scalar( float( get_words( input ) ) )

	if (input EQ "") then begin
		box_erase2
		print," retry ..."
		goto,PICK
	   endif

	read," enter new SKY value: ",input
	skynew = scalar( float( get_words( input ) ) )

	if (input EQ "") then begin
		box_erase2
		print," retry ..."
		goto,PICK
	   endif

	box_erase2

;save previous calibration:
	mosaic_spec.Fact_old = mosaic_spec.Factor
	mosaic_spec.Offs_old = mosaic_spec.Offset

;undo previous calibration:
	srcval = (srcval - mosaic_spec.Offs_old)/mosaic_spec.Fact_old
	skyval = (skyval - mosaic_spec.Offs_old)/mosaic_spec.Fact_old

;calc & set new calibration:
	mosaic_spec.Factor = (srcnew-skynew)/(srcval-skyval)
	mosaic_spec.Offset = skynew - mosaic_spec.Factor * skyval

;calc rescaling factor & offset:
	factor = mosaic_spec.Factor/mosaic_spec.Fact_old
	offset = mosaic_spec.Offset - Factor * mosaic_spec.Offs_old

;rescale relevant display specs:

	mosaic_spec.minT = mosaic_spec.minT * factor + offset
	mosaic_spec.maxT = mosaic_spec.maxT * factor + offset
	mosaic_spec.minLog = ( mosaic_spec.minLog * factor + offset )>1e-37

	mosaic_spec.contour.Levels = $
		mosaic_spec.contour.Levels * factor + offset

	mosaic_spec.contour_Log.Levels = $
	  aLog10( (10^mosaic_spec.contour_Log.Levels * factor + offset)>1e-37 )

	print," Factor :", mosaic_spec.Factor
	print," Offset :", mosaic_spec.Offset
	if !DEBUG gt 3 then stop
end