Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/analyze_image.pro'
; NAME:
;	Analyze_Event
;
; PURPOSE:
;	Process button events which are user requests to display, extract,
;	probe, analyze, store/Load, etc..., the image data,
;	or unmap/destroy the widget if the user has pressed the DONE button.
;	This is the event handler routine that the XManager calls when
;	a widget event occurs for the Analyze_Image widget.
;
; CALLING:
;	Analyze_Event, event
;
; INPUT:
;	event = structure variable defining the widget event.
;
; EXTERNAL CALLS:
;	function Check_Subimage
;	function get_words
;	pro iconify_windows
;	pro XLoadct
;	pro color_tables
;	pro adjct
;	pro Analysis_Task
;	pro Analyze_Cursor
;	pro get_Effic_Data
;	pro get_Scat_Data
;
; COMMON BLOCKS:
;	common Analyze_Image, analyze_widget
;	common opticon0, imaged, imhead, imhist
;	common opticon1, subimage, subhead, subhist
;	common opticon3, imscaled, imdisp
;	common opticon4, subimscaled, subimdisp
;	common opticon5, im_fh, sub_fh, flat_fh
;	common probe_image, win_prof
; PROCEDURE:
;	Big CASE breakdown.
; HISTORY:
;	written: Frank Varosi HSTX/NASA/GSFC 1993.

pro Analyze_Event, event

   common Analyze_Image, analyze_widget
   common opticon0, imaged, imhead, imhist
   common opticon1, subimage, subhead, subhist
   common opticon3, imscaled, imdisp
   common opticon4, subimscaled, subimdisp
   common opticon5, im_fh, sub_fh, flat_fh
   common color_scale, xpos, ypos, win_scal, colorbar, region
   common probe_image, win_prof

     if (event.id EQ analyze_widget.pixel_wid) then begin
		Analyze_Cursor, analyze_widget
		return
	endif

     WIDGET_CONTROL, event.id, GET_UVALUE=uval
     WIDGET_CONTROL, analyze_widget.status_wid, SET_VAL="function: " + uval(0)
     BELL = string(7b)

     CASE uval(0) OF

	"DONE": BEGIN
			if N_elements( win_prof ) NE 1 then win_prof = -1
			if N_elements( win_scal ) NE 1 then win_scal = -1

			iconify_windows, [ win_prof, win_scal, $
					analyze_widget.imspec.window, $
					analyze_widget.imspec.win_info ]

			WIDGET_CONTROL, analyze_widget.pixel_wid, TIMER=0

			if (analyze_widget.group GT 0) then begin
				WIDGET_CONTROL, event.top, MAP=0
				WIDGET_CONTROL, analyze_widget.group, $
						/SHOW,ICON=0,BAD=id
			  endif else begin
				analyze_widget.base = 0
				WIDGET_CONTROL, event.top, /DESTROY
			   endelse

			analyze_widget.active = 0
			return
		  END

	"XLOADCT":	XLoadCT, GROUP=event.top
	"ADJCT":	AdjCT
	"COLTAB":	Color_Tables, "Analyze_Image",  $
				IMAGE_WINDOW=analyze_widget.imspec.window

	"REDUCF":	analyze_widget.reducf = 2^(event.value)
	"REDUCSUB":	analyze_widget.reducsub = 2^(event.value)
	"MAGF":		analyze_widget.magf = event.value + 1

	"SCALING": BEGIN
			k = fix( uval(1) )
			analyze_widget.imspec(k).scaling = event.value
			analyze_widget.imspec(k).rescale = 1
		   END

	"ZOOM":	BEGIN
			WIDGET_CONTROL, event.id, SENSITIVE=0
			zoomw, analyze_widget.imspec(0).window
		END

	"EXTRACT": BEGIN

		sim = size( imaged )

		if (sim(0) EQ 2) AND $
		   (analyze_widget.imspec(0).window GE 0) then begin

			WIDGET_CONTROL, event.id, SENSITIVE=0
			imspec = analyze_widget.imspec(0)
			wshow, imspec.window, ICON=0

			subimage = extract_region( imaged, START_XY=subxy, $
						WINDOW=imspec.window, /LIMIT, $
						WPOS_XY=[imspec.wx,imspec.wy],$
						MAG=1.0/analyze_widget.reducf )
			sim = size( subimage )
			subxy = subxy + [ imhead.x0, imhead.y0 ]
			print," extracted subimage:"
			print," size =", sim(1:2)
			print," from x-y Location =",subxy
			sub_Loc = transpose( [[subxy],[subxy+sim(1:2)-1]] )
			analyze_widget.sub_Loc = sub_Loc

			if intersection( imhead.hot_Loc, sub_Loc, sh, ss ) $
								then begin
				subimage = Long( subimage )
				subimage(ss(0),ss(2)) = $
					imhead.hotspot(sh(0):sh(1),sh(2):sh(3))
			   endif

			subhead = imhead
			subhead.filename = ""
			subhead.flat_field = ""
			subhead.x0 = subxy(0)
			subhead.y0 = subxy(1)
			sim = size( subimage )
			subhead.nx = sim(1)
			subhead.ny = sim(2)
			subhead.psize = sim(3)
			subhead.pbytes = 2^(sim(3)-1)
			subhead.max = ceil( max( subimage, imax, MIN=minsim ) )
			subhead.max_Loc = [ imax MOD sim(1), imax/sim(1) ]
			subhead.min = minsim

			if (subhead.pbytes LE 2) AND $
			   (subhead.min LT 0) then begin
				print," minimum of image is negative"+string(7b)
				print," possible overflow of 16-bits"
				if yes_no_menu( /BIN,/NO, $
					"convert image to 32-bits" ) then begin
					subimage = Long( subimage ) AND 65535
					subhead.max = max( subimage, imax, $
								MIN=mini )
					subhead.max_Loc = [ imax MOD sim(1), $
								imax/sim(1) ]
					subhead.min = mini
					print," converted to 32-bits"
				   endif
			   endif

			subhead.counts = total( subimage > 0 )
			print," total counts =", subhead.counts
			print," maximum pixel counts =",subhead.max
			if (subhead.min EQ subhead.max) then subhead.min = 0

			if (subhead.counts GT 0) AND (sim(3) LT 3) then begin
				subhist = histogram( subimage, MIN=0, $
						MAX=( subhead.max>1 )<32000 )
				subhead.stdev = stdev( subimage, mean )
				subhead.mean = mean
			  endif else begin
				subhist = 0
				subhead.stdev = 0
				subhead.mean = 0
			   endelse

			imspec = analyze_widget.imspec(1)
			copy_struct, subhead, imspec
			ss = strtrim( sim(1:2),2 )
			ss = " (" + ss(0) + "," + ss(1) + ") "
			sL = strtrim( subxy,2 )
			sL = " (" + sL(0) +  "," + sL(1) + ") "
			imspec.name = "Subimage"
			imspec.title = ss + "Region at " + sL + "of Image"
			imspec.rescale = 1
			analyze_widget.imspec(1) = imspec
			message,"subimage extraction completed"+BELL,/INFO
			box_erase

		   endif else message,"first display an image"+BELL,/INFO
		END

	"FLAT":	   if Check_Subimage( uval ) then Flat_Field, event,/REDISPLAY

	"EFFIC": if Check_Subimage( uval ) then $
		get_Effic_Data, subimage, subhead, analyze_widget.imspec(1)

	"SCATTER": BEGIN
		if Check_Subimage( uval ) then begin
			get_Scat_Data, analyze_widget.imspec(1), GROUP=event.top
			return
		   endif
		END

	  else:	BEGIN

		if N_elements( uval ) GT 1 then begin

			index = fix( uval(1) )
			imspec = analyze_widget.imspec(index)

			if (index EQ 0) then begin

				if N_struct( imhead ) GT 0 then begin
				   if (imspec.stime NE imhead.stime) then begin
					copy_struct, imhead, imspec
					imspec.rescale = 1
				     endif
				  endif

				imspec.name = "Image"
				imspec.title = imspec.stime

				Analysis_Task, event, imspec, $
						imaged, imhead, $
						imscaled, imdisp, $
						imhist, im_fh, UVAL=uval, $
						MAGF=(1.0/analyze_widget.reducf)
			  endif else begin

				if strlen( imspec.name ) LE 0 then $
						imspec.name = "Subimage"

				if (analyze_widget.reducsub GT 1) then $
					Magf = (1.0/analyze_widget.reducsub) $
				  else	Magf = analyze_widget.magf

				Analysis_Task, event, imspec, $
					subimage, subhead, $
					subimscaled, subimdisp, $
					subhist, sub_fh, UVAL=uval, MAGF=Magf
			   endelse

			analyze_widget.imspec(index) = imspec

		  endif else begin

			help,/st,event
			message,uval(0)+" not yet implemented",/INFO
			return
		   endelse
		  END
     ENDCASE

    if uval(0) NE "XLOADCT" then begin
	WIDGET_CONTROL, analyze_widget.base, /SHOW, ICON=0
	WIDGET_CONTROL, event.id, /SENSITIVE
	if uval(0) EQ "PROBE" then WIDGET_CONTROL, event.id, /INPUT_FOCUS
     endif

    WIDGET_CONTROL, analyze_widget.status_wid, SET_VALUE="function: "
END

;+
; NAME:
;	Analyze_Image
;
; PURPOSE:
;	Create a widget to interactively control/execute the display & analysis
;	of images obtained from MAMA or CCD detector integrations.
;
; CALLING:
;	Analyze_Image
;
; KEYWORD INPUTS:
;
;	GROUP = the ID of the widget that calls this procedure,
;		if specified, death of caller results in death of this widget.
;
;	PARENT = the ID of the widget which is to be the top level base of the
;		new widget created by this procedure. If specified,
;		this Analyze_Image widget is just defined but not realized.
;
;	XPOS, YPOS = desired position on screen of free base (no parent) widget.
;
; EFFECTS:
;	Initiates the Xmanager if it is not already running.
;
; EXTERNAL CALLS:
;	pro Xmanager
;	pro widget_Location
;	function CW_Bgroup
;	function widget_Tree_Map
; COMMON BLOCKS:
;	common Analyze_Image, analyze_widget
; PROCEDURE:
;	Create the analyze_widget structure variable.
;	Create and register the widgets with the Xmanager.
;	Selection events are then processed by pro Analyze_Event.
; HISTORY:
;	Frank Varosi NASA/GSFC 1993.
;-

pro Analyze_Image, GROUP=group, PARENT=parent, XPOS=xpos, YPOS=ypos

   common Analyze_Image, analyze_widget

	if N_struct( analyze_widget ) NE 1 then begin

		window,/FREE,XS=2,YS=2		;to get current !D.table_size
		wdelete,!D.window

		image_spec = {	IMSPEC_V3,	name:""			,$
						title:""		,$
						filename:"",		$
						directory:"",		$
						flat_field:"",		$
						stime:"",		$
						x0:0, y0:0,		$
						nx:0, ny:0,		$
						ydhms:intarr(5),	$
						counts:0L,		$
						dtime:0.0,		$
						WaveLength:0.0,		$
						max:0L,			$
						min:0L,			$
						scaling:"Linear"	,$
						topval:!D.table_size-2	,$
						max_method:"auto"	,$
						max_disp:0L		,$
						rescale:1		,$
						magf:0.0		,$
						window:-1		,$
						wx:46, wy:40		,$
						win_info:-1		,$
						dir_io:"~/opt/"		,$
						text_wid:0L		}

		analyze_widget = {	base:0L			,$
					parent:0L		,$
					group:0L		,$
					active:0		,$
					reducf:4		,$
					sub_Loc:intarr(2,2)	,$
					reducsub:2		,$
					magf:1			,$
					status_wid:0L		,$
					px:-1			,$
					py:-1			,$
					pixel_wid:0L		,$
					pixel_timer:0.2		,$
			imspec: replicate( image_spec, 3 )	,$
			wmap: replicate( widget_Tree_Map(), 70 ) }
	   endif

	if WIDGET_INFO( analyze_widget.base, /VALID ) then begin
		message,"Analyze_Image widget exists...now showing",/INFO
		WIDGET_CONTROL, analyze_widget.base, /SHOW, ICON=0
		analyze_widget.active = 1
		WIDGET_CONTROL, analyze_widget.pixel_wid, /TIMER
		return
	   endif

	if N_elements( parent ) EQ 1 then begin

		base = WIDGET_BASE( parent, /FRAME, /ROW )
		label = WIDGET_LABEL( base, VALUE = "Analyze Image" )
		analyze_widget.base = -base
		analyze_widget.parent = parent

	  endif else begin

		base = WIDGET_BASE( TIT="Analyze Image",/ROW )
		analyze_widget.base = base
		analyze_widget.parent = 0
		if N_elements( group ) EQ 1 then analyze_widget.group = group $
					    else analyze_widget.group = 0
	   endelse

	baseL = WIDGET_BASE( base, /FRAME, /COLUMN, SPACE=4 )
	baseR = WIDGET_BASE( base, /FRAME, /COLUMN, SPACE=4 )

	b = WIDGET_BUTTON( baseL, VAL="XLOADCT", UVAL="XLOADCT" )
	b = WIDGET_BUTTON( baseL, VAL="Color Tables", UVAL="COLTAB" )
	b = WIDGET_BUTTON( baseL, VAL="Adjust Color Table", UVAL="ADJCT" )

	b = CW_Bgroup( baseL, "  " + strtrim( [1,2,4,8], 2 ) + "  ", $
			/ROW, /EXCLUSIVE, UVAL="REDUCF", $
			LABEL_TOP="Image Display Reduction Factor", $
		SET_VAL= nint( aLog( analyze_widget.reducf )/aLog(2) ) > 0 )

	imspecs = analyze_widget.imspec
	Buttons = ["Linear","Logarithmic"]
	ws = where( imspecs(0).scaling EQ Buttons ) > 0

	b = CW_Bgroup( baseL, Buttons, /ROW,/EXCLUSIVE,/RETURN_NAME, $
				LABEL_TOP="Display Scaling:", $
				UVAL=["SCALING",'0'], SET_VAL=ws(0) )

	b1 = WIDGET_BUTTON( baseL, VAL="DISPLAY image", UVAL=["DISPLAY",'0'] )
	b = WIDGET_BUTTON( baseL, VAL="PROBE image", UVAL=["PROBE",'0'] )
	b = WIDGET_BUTTON( baseL, VAL="STORE image", UVAL=["STORE",'0'] )

	if strlen( imspecs(0).dir_io ) GT 20 then dirv = "Dir = " $
						else dirv = "Directory = "

	imspecs(0).text_wid = WIDGET_TEXT( baseL,/FRAME,/EDIT, $
					UVAL = [ "DIRECTORY", '0' ], $
					VAL = dirv + imspecs(0).dir_io )

	b = WIDGET_BUTTON( baseL, VAL="LOAD image", UVAL=["LOAD",'0'] )
	b = WIDGET_BUTTON( baseL, VAL="PRINT image", UVAL=["PRINT",'0'] )

	analyze_widget.status_wid = WIDGET_TEXT( baseL,/FRAME,VAL="function: " )
	analyze_widget.pixel_wid = WIDGET_TEXT( baseL, /FRAME, VAL="(CURSOR)" )

	b = WIDGET_BUTTON( baseR, UVAL="EXTRACT", VAL="EXTRACT sub-image" )

	b = CW_Bgroup( baseR, "  " + strtrim( [1,2,4,8], 2 ) + "  ", $
			/ROW, /EXCLUSIVE, UVAL="REDUCSUB", $
			LABEL_TOP="Sub-image Display Reduction Factor", $
		SET_VAL= nint( aLog( analyze_widget.reducsub )/aLog(2) ) > 0 )

	b = CW_Bgroup( baseR, "  " + strtrim( indgen( 6 ) + 1, 2 ) + "  ", $
			/ROW, /EXCLUSIVE, $
			LABEL_TOP="Sub-image Display Magnification", $
			UVAL="MAGF", SET_VAL=(analyze_widget.magf-1) > 0 )

	Buttons = ["Linear","Logarithmic"]
	ws = where( imspecs(1).scaling EQ Buttons ) > 0

	b = CW_Bgroup( baseR, Buttons, /ROW,/EXCLUSIVE,/RETURN_NAME, $
				LABEL_TOP = "Sub-image Scaling:", $
				UVAL=["SCALING",'1'], SET_VAL=ws(0) )

	b = WIDGET_BUTTON( baseR, VAL="DISPLAY sub-image",UVAL=["DISPLAY",'1'] )
	b = WIDGET_BUTTON( baseR, VAL="PROBE sub-image", UVAL=["PROBE",'1'] )
	b = WIDGET_BUTTON( baseR, VAL="STORE sub-image", UVAL=["STORE",'1'] )
 
	if strlen( imspecs(1).dir_io ) GT 20 then dirv = "Dir = " $
						else dirv = "Directory = "

	imspecs(1).text_wid = WIDGET_TEXT( baseR,/FRAME,/EDIT, $
					UVAL = [ "DIRECTORY", '1' ], $
					VAL = dirv + imspecs(1).dir_io )
	analyze_widget.imspec = imspecs
	b = WIDGET_BUTTON( baseR, VAL="LOAD sub-image", UVAL=["LOAD",'1'] )
	b = WIDGET_BUTTON( baseR, VAL="PRINT sub-image", UVAL=["PRINT",'1'] )

	b = WIDGET_BUTTON( baseR, VAL="FLAT Field", UVAL="FLAT" )
	b = WIDGET_BUTTON( baseR, VAL="EFFICIENCY  measurement", UVAL="EFFIC" )
	b = WIDGET_BUTTON( baseR, VAL="SCATTER  measurement", UVAL="SCATTER" )

	if N_elements( parent ) NE 1 then begin
		b = WIDGET_BUTTON( baseL, VALUE="DONE", UVALUE="DONE" )
		WIDGET_CONTROL, base, /REALIZE
		widget_Location, base, XPOS=xpos, YPOS=ypos
		WIDGET_CONTROL, b1, /INPUT_FOCUS
		WIDGET_CONTROL, analyze_widget.pixel_wid, /TIMER
	   endif

	analyze_widget.wmap = widget_Tree_Map( base )
	analyze_widget.active = 1

	Xmanager, "Analyze", base, GROUP=group
END