;
;+
;
; Displays an image with all the axes, color bar, titles, as
; preparation for making a hardcopy.
;

PRO hard_prep, image, x, y, lo=lo, hi=hi, title=title, xtitle=xtitle, $
		ytitle=ytitle, btitle=btitle, nobar=nobar

;
; INPUT PARAMETERS:
;	image = 2 dimensional array to display.
;
; OPTIONAL INPUT PARAMETERS:
;	x, y = vectors of x, y positions of pixels.  Use to get
;		axes marked correctly
;
; OPTIONAL INPUT KEYWORDS:
;	lo, hi = data values to use for scaling image.  Default is
;		to use image minimum, maximum.
;	title, xtitle, ytitle = plot and axes labels, string.
;	btitle = color bar title, string.
;	nobar = if set, do not make a color bar.  Default displays a bar.
;
; USAGE:
;	Call this procedure to get a window that can be dumped to:
;		Mitsubishi Color Printer with COLOR_COPY.
;		Tektronix Color Printer with HARDCOPY.
;
; RESTRICTIONS: 
;	This procedure will minify large images.  To get maximum
;	resolution from the hardcopy device, print the image alone
;	with a direct call to the hardcopy routine.
; 	
; HISTORY:
;	Written   March 7, 1994   Barry LaBonte
;
;-

; Width of color bar as a fraction of image width
barwid = 0.1
; Width of display as a fraction of the plot area
winplot = 1.2
; Image limits as fraction of console size
fraction = [0.5, 0.7]

sz = SIZE(image)

; Handle inputs
IF( N_ELEMENTS(x) EQ 0) THEN xt = INDGEN(sz(1)) ELSE xt = x
IF( N_ELEMENTS(y) EQ 0) THEN yt = INDGEN(sz(2)) ELSE yt = y

IF( KEYWORD_SET(lo) EQ 0 ) THEN lo = MIN(image)
IF( KEYWORD_SET(hi) EQ 0 ) THEN hi = MAX(image)
IF( KEYWORD_SET(title) EQ 0 ) THEN title=''
IF( KEYWORD_SET(xtitle) EQ 0 ) THEN xtitle=''
IF( KEYWORD_SET(ytitle) EQ 0 ) THEN ytitle=''
IF( KEYWORD_SET(btitle) EQ 0 ) THEN btitle=''

; How big is the console screen?
SPAWN, 'xwininfo -root | grep ''Width'' | awk ''{print $3}'' ', width
width = FIX(width(0))
SPAWN, 'xwininfo -root | grep ''Height'' | awk ''{print $3}''' , height
height = FIX(height(0))
console = [width, height]

; Scale the image
tim = BYTSCL(image, MIN=lo, MAX=hi)

; Make the image a reasonable size
limit = MAX( FLOAT(sz(1:2)) /  FLOAT(console) )
; Image small, magnify
IF( limit LT fraction(0) ) THEN BEGIN
	mag = FIX(fraction(0)/limit)
	tim = REBIN( tim, sz(1)*mag, sz(2)*mag )
	xt = REBIN( xt, sz(1)*mag, /SAMPLE )
	yt = REBIN( yt, sz(2)*mag, /SAMPLE)
ENDIF ELSE BEGIN
; Image big, minify
	IF( limit GT fraction(1) ) THEN BEGIN
		mag = FIX(1. + limit/fraction(1) )
		tim = REBIN( tim, sz(1)/mag, sz(2)/mag )
		xt = REBIN( xt, sz(1)/mag, /SAMPLE)
		yt = REBIN( yt, sz(2)/mag, /SAMPLE )
	ENDIF
ENDELSE
siz = SIZE(tim)

; Open the window, display the image
; Add space for the color bar if needed
sx = siz(1)
IF( KEYWORD_SET(nobar) EQ 0 ) THEN sx = FIX( sx * (1. + barwid*2.5) ) 
sx = FIX(sx * winplot)
sy = FIX(siz(2) * winplot)
; Force window size to be even numbers of pixels
sx = sx + (sx MOD 2)
sy = sy + (sy MOD 2)
WINDOW, xs=sx, ys=sy, /FREE

; Dummy PLOT call to define graphics variables
PLOT, siz(1:2), XSTYLE=4, YSTYLE=4, /NODATA, BACKGROUND=255

; Get plot area
px = !X.WINDOW * !D.X_VSIZE
py = !Y.WINDOW * !D.Y_VSIZE
TV, tim, px(0), py(0)

; Now the axes and titles
PLOT, xt, yt, /NODATA, /NOERASE, XSTYLE=1, YSTYLE=1, /DEVICE, $
	POSITION=[px(0), py(0), px(0)+siz(1)-1, py(0)+siz(2)-1], $
	CHARSIZE=1.2, TITLE=title, XTITLE=xtitle, YTITLE=ytitle, COLOR=0

; Color bar if desired
IF( KEYWORD_SET(nobar) EQ 0 ) THEN BEGIN
	xwid = FIX( siz(1) * barwid )
	xoff = px(0) + siz(1) + (xwid*3)/2
	ramp = FINDGEN(siz(2)) * (FLOAT(hi) - FLOAT(lo))/siz(2) + lo
	ramp = REBIN( REFORM( ramp,1,siz(2) ), xwid ,siz(2) )
	ramp(*,siz(2)-1) = ramp(*,0)
	TV, BYTSCL( ramp, MIN=lo, MAX=hi ), xoff, py(0), /DEVICE
	PLOT,  REFORM( ramp(0,*) ), /NODATA, /NOERASE, $
		XSTYLE=4, YSTYLE=1, /DEVICE, COLOR=0, $
		POSITION=[xoff, py(0), xoff+xwid-1, py(0)+siz(2)-1], $
		CHARSIZE=1.2, YTITLE=btitle
ENDIF

RETURN
END
