Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/analysis_task.pro'
;+
; NAME:
;	Analysis_Task
;
; PURPOSE:
;	Process button events which are user requests to display,
;	analyze, store/Load, etc..., the image data aquired.
;	This is the next event handler routine,
;	called by Analyze_Event for additional generic processing.
;
; CALLING:
;	Analysis_Task, event, imspec, imaged, im_hdr, imscaled, imdisp, hfits
;
; INPUT:
;	event = structure variable defining the widget event.
;	imspec = structure variable specifying how the image is displayed.
;	imaged = the image data array
;	im_hdr = structure variable containing header info about image.
;	imscaled = the image array scaled into bytes.
;	imdisp = the byte image array magnified or reduced for display.
;	hfits = the FITS header if an image file is read or written.
;
; KEYWORDS:
;	UVALUE = user value of widget event,
;		if not given it is retrieved from event.
;	MAGF = desired magnification (or reduction) of image display.
;
; EXTERNAL CALLS:
;	functions get_words, get_window, scale_image, probe_image
;	pro box_erase
;	pro gscat_box_draw	(to tell get_Scat_Data when it is new display)
;	pro X_Var_Edit
;	pro analysis_info
;	functions get_text_input, check_dir, substwid
;	function PickFile
;	functions store_image, Load_image
; COMMON BLOCKS:
;	common Analysis_Task, def_hdr	;default header for files.
;	common adjct_map, color_map
; PROCEDURE:
;	Big CASE statement.
; HISTORY:
;	Frank Varosi NASA/GSFC 1993.
;	F.V. 1993: added default structure template for file headers.
;-

pro Analysis_Task, event, imspec, imaged, im_hdr, $
		imscaled, imdisp, imhist, hfits, UVALUE=uval, MAGF=magf

  common Analysis_Task, def_hdr
  common adjct_map, color_map

     if N_elements( uval ) LE 0 then WIDGET_CONTROL, event.id, GET_UVALUE=uval
     if N_elements( uval ) EQ 1 then uval = [uval,""]
     BELL = string( 7b )

     if (uval(0) NE "DIRECTORY") then begin
	if (uval(0) NE "LOAD") then begin
		sim = size( imaged )
		if (sim(0) NE 2) OR (N_struct( im_hdr ) NE 1) then begin
			message,"no image to " + uval(0) + BELL,/INFO
			return
		   endif
	   endif
	WIDGET_CONTROL, event.id, SENS=0, BAD=bad
	endif

     if N_elements( magf ) NE 1 then begin
		imspec.rescale = 1
		magf = imspec.magf
	endif

     CASE uval(0) OF

	"DISPLAY": BEGIN

		if (imspec.scaling EQ "Logarithmic") then begin
			Log10 = 1
			scale = "LOG10"
		  endif else begin
			Log10 = 0
			scale = "LINEAR"
		   endelse

		WIDGET_CONTROL,/HOURGLASS
		title = imspec.name + " = " + imspec.title
		maxim = imspec.max > 1
		minim = imspec.min > 0
		sim = size( imaged )

		if (imspec.rescale) then begin
			message,"re-Scaling...",/INFO
			imspec.rescale = 0
			imspec.magf = 0
			imscaled = scale_image( imaged,1,MIN=minim,MAX=maxim,$
						TOP=imspec.topval, LOG=Log10 )
			if (imspec.window GE 0) then wdelete,imspec.window
			if (imspec.win_info GE 0) then wdelete,imspec.win_info
		   endif

		sims = size( imscaled )

		if (imspec.magf NE magf) then begin
		    npix = sims(1)*sims(2)
		    Mpix = 1024L*1024
		    while (npix * magf^2 GT Mpix) do begin
			if (magf LE 1) then magf = magf/2. else magf = magf-1
			message,"limiting image display magnification to: " + $
				strtrim( magf,2 ) + BELL, /INFO
		     endwhile
		    imspec.magf = magf
		    s = fix( sims * magf )
		    if (magf LT 1) then begin
			L = fix( s/magf )-1
			imdisp = rebin( imscaled(0:L(1),0:L(2)),s(1),s(2),/SAM )
		     endif else imdisp = rebin( imscaled, s(1), s(2), /SAMPLE )
		   endif

		color_scale, minim,maxim, imspec.topval,scale,title,LOGMIN=Log10
		analysis_info, im_hdr, imspec, info1, info2

		simd = size( imdisp )
		xsiz = ( simd(1) + 1.5*imspec.wx ) > 400
		ysiz = simd(2) + 1.5*imspec.wy + !D.y_ch_size*N_elements(info1)
		win = imspec.window
		get_window, win, TITLE=title, XS=xsiz, YS=ysiz, $
				XPOS=!DEVX-xsiz, YPOS=!DEVY-ysiz
		wshow, win, ICON=0
		imspec.window = win
		wait,0.1
		erase
		wait,0.1
		shift = 1.0/magf
		xran = imspec.x0 + [ 0, imspec.nx ] - shift
		yran = imspec.y0 + [ 0, imspec.ny ] - shift
		posxy = [imspec.wx, imspec.wy]
		posxy = [ posxy-1, posxy + [simd(1),simd(2)] ]
		plot, xran,yran ,/NODATA, XSTY=1,XRAN=xran, YSTY=1,YRAN=yran, $
				FONT=0, TICKLEN=-0.02, POSITION=posxy, /DEVICE
		tv, imdisp, imspec.wx, imspec.wy
		printw,info1
		empty

		if (imspec.name EQ "Subimage") then gscat_box_draw,/CLEAR

		xsiz = 80 * !D.x_ch_size
		ysiz = N_elements( info2 ) * !D.y_ch_size
		win = imspec.win_info
		get_window, win, TITLE=title, XS=xsiz, YS=ysiz, $
					XPOS=!DEVX-xsiz, YPOS=20
		imspec.win_info = win
		wshow, imspec.win_info, ICON=0
		erase
		wait,0.1
		printw,info2
		empty
		END

	"PRINT": BEGIN

		sims = size( imscaled )
		simd = size( imdisp )

		if (sims(0) NE 2) then begin
			message,"must display an image first"+BELL,/INFO
			return
		   endif

		if min( simd(1:2) LT sims(1:2) ) then begin
			menu = ["image resolution?"," ",$
				"as displayed"," ",	$
				"FULL image resolution"	]
			psres = next_word( menu( wmenu( menu,IN=2,TIT=0 ) >0 ) )
		  endif else psres = "FULL"

		devsav = !D.name
		bitspix = 8
		HC = set_Hard_Copy( bitspix, sims(1), sims(2) )

		if strlen( HC.file ) LE 0 then begin
			set_plot,devsav
			return
		   endif

		Ncol = N_elements( color_map )
		if (Ncol LE 0) then begin
			color_map = bindgen( !D.table_size )
			Ncol = !D.table_size
		   endif
		grey_map = reverse( bytscl( indgen( Ncol ) ) )
		!P.font = 0
		print," writing graphics to disk file: " + HC.file
		print," this will take a few seconds..."
		device, FILE=HC.file,/HELVETICA
		xran = imspec.x0 + [ 0, imspec.nx ]
		yran = imspec.y0 + [ 0, imspec.ny ]
		posxy = HC.posxy + [ -0.001, -0.001, 0.001, 0.001 ]
		plot, xran,yran ,/NODATA, XSTY=1, XRAN=xran, $
					  YSTY=1, YRAN=yran, $
					  POSITION=posxy, TICKLEN=-0.02
		posxy = HC.posxy
		nxsiz = posxy(2)-posxy(0)
		nysiz = posxy(3)-posxy(1)

		if (HC.type EQ "color") then begin

			print,Ncol," pseudo-Colors for image..."

			if (psres EQ "FULL") then begin

				tv, imscaled, /NORM, posxy(0), posxy(1), $
						XSIZ=nxsiz, YSIZ=nysiz
				print," FULL image resolution..."

			  endif else begin

				tv, imdisp, /NORM, posxy(0), posxy(1), $
						XSIZ=nxsiz, YSIZ=nysiz
				print," resolution as displayed..."
			   endelse

		  endif else begin

			print, 2^bitspix," grey Levels for image..."
			gmap = grey_map( color_map )

			if (psres EQ "FULL") then begin

				tv, gmap( imscaled ),/NORM,posxy(0),posxy(1), $
							XSIZ=nxsiz, YSIZ=nysiz
				print," FULL image resolution..."

			  endif else begin

				tv, gmap( imdisp ),/NORM,posxy(0),posxy(1), $
							XSIZ=nxsiz, YSIZ=nysiz
				print," resolution as displayed..."
			   endelse
		   endelse

		analysis_info, im_hdr, imspec, info1, info2, /HARDCOPY
		top = 0.94
		topd = 0.015
		tsiz = 0.7
		for i=0,N_elements( info1 )-1 do $
				xyouts, 0.1, top-i*topd, info1(i),/NORM,SIZ=tsiz
		empty
		erase	;this is just to eject page and goto next page.
		top = 0.9
		topd = 0.025
		device,/COURIER
		for i=0,N_elements( info2 )-1 do $
					xyouts, 0.1, top-i*topd, info2(i),/NORM
		device,/CLOSE
		device,/HELVETICA

		if (HC.action EQ "print") then $
			print_graphics, HC.file, HC.type, N_COPY=HC.ncopy $
		  else 	print,"done!"
		print,BELL
		HC.file=""
		!P.font = -1
		set_plot,devsav
		END

	"PROBE": BEGIN
			probe_image, imaged, /VARIABLE_BOX, /SHOWINDOW, $
					MAGF=imspec.magf, WIN=imspec.window, $
					XPOS=imspec.wx, YPOS=imspec.wy, $
					XOFF=imspec.x0, YOFF=imspec.y0, $
					PROFILE = im_hdr.grating + "  [ " + $
						im_hdr.filename + " ]  " + $
						im_hdr.stime
			box_erase
		   END

	"DIRECTORY": BEGIN
		WIDGET_CONTROL, event.id, GET_VALUE=value
		vals = get_words( value, DELIM = "=" )
		vald = strtrim( vals(N_elements(vals)-1), 2 )
		dir = dir_path( get_words( vald, DEL="/" ) )
		if strpos( vald, "/" ) EQ 0 then dir = "/" + dir
		f = findfile( substwid( dir ) + "..", COUNT=nf )
		if (nf GT 0) then imspec.dir_io = dir $
			else print,"directory: " + dir + " does NOT exist!"+BELL
		if strlen( imspec.dir_io ) GT 20 then dirv = "Dir = " $
						else dirv = "Directory = "
		WIDGET_CONTROL, imspec.text_wid, SET_VAL = dirv + imspec.dir_io
		END

	"STORE": BEGIN

		if N_struct( def_hdr ) NE 1 then begin

			def_hdr ={ Header_Info_MAMA,	$
					filename:"",	$
					directory:"",	$
					grating:"",	$
					Lamp:"",	$
					object:"",	$
					environment:"VACUUM",$
					measurement:"",	$
					comment:""	}

    			if (im_hdr.detector EQ "CCD") then begin
				def_hdr ={ Header_Info_CCD,	$
						filename:"",	$
						directory:"",	$
						grating:"",	$
						Lamp:"",	$
						object:"",	$
						measurement:"",	$
						comment:""	}
			   endif
		   endif

		if (strlen( im_hdr.measurement ) GT 0) AND $
		   (strlen( im_hdr.environment ) GT 0) AND $
		   (strlen( im_hdr.grating ) GT 0) then $
					copy_struct, im_hdr, def_hdr
		def_hdr.filename = ""
		def_hdr.directory = imspec.dir_io

		X_Var_Edit, def_hdr, GR=event.top	;user must set fields...

		nch = strlen( def_hdr.directory )
		if strpos( def_hdr.directory, "/", nch-2 ) NE (nch-1) then $
				def_hdr.directory = def_hdr.directory + "/"

		dir = check_dir( substwid( def_hdr.directory ) )

		if strlen( dir ) LE 0 then begin
			def_hdr.filename = ""
			print,"directory: " + def_hdr.directory + $
				" does NOT exist!" + BELL
		  endif else def_hdr.directory = dir

		if (strlen( def_hdr.filename ) GT 0) AND $
		   (strlen( def_hdr.grating ) GT 0) then begin

			copy_struct, def_hdr, im_hdr
			store_image, im_hdr, imaged, hfits, $
					COMPRESS= yes_no_menu( "compress",/BIN )
			copy_struct, im_hdr, imspec

		  endif else $
			print," nothing stored:  need grating & file names"+BELL
		END

	"LOAD": BEGIN

		file = PickFile( GROUP=event.top, PATH=imspec.dir_io,  $
				FILTER = "*.fits*", NOCONFIRM=fix( uval(1) ), $
				TIT="select  " + imspec.name + " file to Read" )

		if strlen( file ) LE 0 then begin
			print," nothing loaded"
			return
		   endif

		WIDGET_CONTROL,/HOURGLASS
		Load_image, file, im_hdr, imaged, hfits

		if (im_hdr.counts GT 0) AND (im_hdr.max GT 0) then begin
			imhist = histogram( imaged, MIN=0,MAX=im_hdr.max<32000 )
		  endif else imhist = 0

		copy_struct, im_hdr, imspec
		imspec.rescale = 1
		imspec.title = imspec.stime
		print," finished reading" + BELL
		END

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