pro pr_image, data, side, nomark=nomark, raw=raw, bin=bin, index=index
;
;+
;NAME:
;	pr_image  
;PURPOSE:
;	To print the average DN level of a box to be marked by the
;	user.  It can also display the raw values.
;CALLING SEQUENCE:
;	pr_image, data
;	pr_image, data, 10
;	pr_image, data, 20, /nomark, /raw, bin=4
;	pr_image, data, index=index
;INPUT:
;	data	- 2-D data image array
;OPTIONAL INPUT:
;	side	- Number of pixels on a side (if not passed, the
;		  default is 9
;	nomark	- If present, the location on the image which 
;		  is selected is NOT marked (default is to mark it)
;	raw	- If present, print out the raw data values also
;	bin	- The rebin size used to display the image
;	index	- The index structure that goes with the data image.
;HISTORY:
;	Written 20-Nov-91 by M.Morrison
;	 7-May-92 (MDM) - Added capability to print out the CCD address
;			  (need to pass in the index for that to work)
;	15-Oct-92 (MDM) - Modified call to DRAW_BOXCORN to add 1 to x1 and y1
;			  to encircle the selected pixels more properly
;			- Corrected the sub-image selected when "side" was
;			  an even number (it used to give 17x17 when side
;			  was set to 16)
;-
siz = size(data)
nx = siz(1)
ny = siz(2)
;
if (n_elements(bin) eq 0) then bin=1
if (n_elements(side) eq 0) then side = 9
;
tvscl, rebin(data, nx*bin, ny*bin, /sample)
;
qdone = 0
while not qdone do begin
    print, '* Use left button to mark the center
    print, '  (box size will be ' + strtrim(side,2) + ')'
    print, '* Use the middle button to mark two corners
    print, '* Use the right button to exit'

    wait, 0.3
    cursor, x, y, /device
    button = !err
    case button of
	1: begin	;left button
		print, 'Center location: ', x, y

		if (n_elements(index) ne 0) then begin
		    xx0 = get_pix_coor(index, offset=x/bin)					;added 7-May-92
		    yy0 = get_pix_coor(index, offset=y/bin, /line)				;added 7-May-92
		    print, 'Location in full res "IDL array" coordinates: ', xx0, yy0		;added 7-May-92
		    xx0 = get_pix_coor(index, offset=x/bin, /img)				;added 7-May-92
		    print, 'Location in full res CCD "IMG" coordinates:   ', xx0, yy0		;added 7-May-92
		end

		x0 = (x/bin-side/2)
		y0 = (y/bin-side/2)
		x1 = x0 + side-1
		y1 = y0 + side-1
	   end
	2: begin	;middle button
		wait, 0.5
		print, 'Mark the other corner'
		cursor, x2, y2, /device
		x0=(x<x2)/bin
		x1=(x>x2)/bin
		y0=(y<y2)/bin
		y1=(y>y2)/bin
	   end
	4: begin	;right button
	   end
    endcase
    ;
    if (button ne 4) then begin
	x0 = x0 > 0 < (nx-1)
	x1 = x1 > 0 < (nx-1)
	y0 = y0 > 0 < (nx-1)
	y1 = y1 > 0 < (nx-1)
	if (not keyword_set(nomark)) then draw_boxcorn, x0*bin, y0*bin, (x1+1)*bin, (y1+1)*bin, /device
	;
	print, 'Region marked is: ', strtrim(x0,2), ':', strtrim(x1,2), ',  ', $
				strtrim(y0,2), ':', strtrim(y1,2)
	temp = data(x0:x1, y0:y1)
	ny0 = y1-y0+1
	nx0 = x1-x0+1
	avg = total(temp) / n_elements(temp)
	print, 'Average is: ', avg
	;
	if (keyword_set(raw)) then begin
	    print, '      ', bindgen(nx0)+x0
	    for i=ny0-1,0,-1 do begin		;reverse the order printed out (to match the image)
		print, i+y0, temp(*,i)
	    end
	end
	;
	;print, '* Use left button to select another region (image refresh)
	;print, '* Use middle button to select another region (no image refreshed)
	;print, '* Use right button to exit'
	;wait, 0.5
	;cursor, x, y, /device
	;button = !err
	;if (button eq 1) then tvscl, rebin(data, nx*bin, ny*bin, /sample)
	;if (button ne 4) then wait, 0.5
    end
    ;
    if (button eq 4) then qdone = 1
end
;
end

