Viewing contents of file '../idllib/uit/pro/ctvscl.pro'
PRO CTVSCL,IMAGE,P1, P2,P3,P4,PIXMAP=PX,MINDATA=mind, MAXDATA = maxd,TOP=top,$
    TITLE=title, log = log, Field = fld, Sigrange = Sigrange,SILENT = Silent
;+
; NAME:
;	CTVSCL
; PURPOSE:
;	Load an array to the image display after byte scaling.  It differs from 
;	the IDL procedure TVSCL in the following ways:
;
;	(1)  The default MIN and MAX for the scaling is computed by calculating
;		the sky value and variance of the image
;	(2)  The size and position of the image is stored in the common block
;		IMAGES.
;	(3)  The user is warned if the image array is larger than the screen 
;		size
;	(4)  The window is resized to the size of the image, unless the position
;		parameter is given.
;	(5)  Negative numbers may be used for the X and Y parameters to extract 
;		a subarray of a large image.
;	(6)  The MAXD and MIND keywords can be used to specify the range of the
;		image to be scaled.
;	(7)  An image title can be displayed by either supplying a FITS header
;		or by use of the TITLE keyword.
;	(8)  The /LOG keyword lets one take the LOG of the image before scaling
;
; CALLING SEQUENCE:
;	CTVSCL,IM,PIXMAP=1	    ;Scale & display image in center of current channel
;	CTVSCL,IM,X,Y,HEADER
;	CTVSCL,IM,HEADER[,X,Y,FIELD]
;	CTVSCL,IM,HEADER[,POS]
;	CTVSCL,IM,POS,HEADER
;	CTVSCL,IM,X,Y,FIELD, $
;            [/PIXMAP,MAXD = maxd, MIND = mind, TOP = top, TITLE = title,/LOG,
;		SIGRANGE =[s1,s2],/SILENT ]
;
;	CTVSCL will accept up to 4 parameters following the IM parameter- up 
;	to 3 numbers and up to one string array.  The string array is assumed 
;	to be a FITS header and can be placed arbitrarily among the number 
;	parameters.  The definition of the numerical parameters is dependent 
;	upon the number of them: 
;
;	1 number	POSITION 
;	2 numbers	X, Y	(in order of appearance in procedure call)
;	3 numbers	X, Y, FIELD. (in order of appearance in procedure call)
;	FIELD may also be specified as a keyword.	
;
; INPUTS:
;	IM - 2-d image array to be loaded to the image window
;
; OPTIONAL INPUT PARAMETERS:
;	X - X position of display, starting from the lower left hand corner.
;	Y - Y position of display, starting from the lower left hand corner.
;		If X or Y are not specified, the current window position is 
;		used.
;		If X and Y are negative, then a 512 x 512 subimage is extracted
;		from IM beginning at position X,Y.
;		Note that the maximum and minimum values of the subimage (not 
;		the original image) are used to scale into a byte array
;	POSITION - Scalar giving position on screen where image will be
;		displayed.  Position 0 is in the upper left had corner and
;		positions run from left to right, and from top to bottom.
;		See documentation for TV for information on POSITION.
;	FIELD - Image channel to be loaded.  If omitted, the current image
;		channel (from the common block TV) is used.
;	HEADER = FITS header for image, from which a summary is extracted and
;		written to the window's title bar.
;
; OPTIONAL KEYWORD INPUTS:
;	PIXMAP - A keyword parameter indicating whether the image should be
;		written to a pixmap instead of a visible window.  If set and 
;		non-zero, the image is written to a pixmap.  The PIXMAP 
;		keyword may be used with any of the four calling sequences.
;	MAXDATA - Maximum value of IM to consider before scaling, scalar
;	MINDATA - Minimum value of IM to consider before scaling, scalar
;	SILENT - If present and non-zero, all print-to-screen commands are 
;		suppressed.
;	SIGRANGE - Two-element vector containing range of scaling (*sigma, with
;		respect to average.) Default is [-0.5,12].  USE OF SIGRANGE 
;		OVER-RIDES MINDATA, MAXDATA.
;	TOP - Maximum value of the scaled result, scalar
;		The MAXDATA, MINDATA, and TOP keyword values are passed 
;		directly to the BYTSCL procedure -- see documentation for BYTSCL
;	TITLE - A scalar string to be displayed as the title to the image
;		window.
;	LOG - If present and non-zero, LOG10 of IMAGE is used.
;	FIELD - acts same as FIELD parameter
;
; OUTPUTS:
;	None.
;
; EXAMPLE:
;	One has a 2048 x 2048 image array BIGIM, and associated FITS header, H.
;	Display a 512 x 512 subimage starting at pixel (800,800) and include 
;	a title extracted from the header
;
;            IDL> ctvscl,IM,-800,-800,H
;
; COMMON BLOCKS:
;	The current image plane is read from the common block TV.  The lower
;	left hand corner and the image size are stored in the common block
;	IMAGES.
;
; RESTRICTIONS:
;	Program can only store the size and starting position of a single
;	image on a given image frame.
;
; MODIFICATION HISTORY:
;	Adapted to workstations.  M. Greason, May 1990.
;	Added TITLE keyword, J. Isensee, September 9, 1991.
;	Added new default scaling, HEADER parameter, LOG, SIGRANGE keywords  
;				J. Offenberg Oct, 1991
;	Made more robust scaling, fix position parameter problems WBL May 1992
;-
on_error,2
COMMON TV, chan, zoom, xroam, yroam
COMMON IMAGES, x00, y00, xsize, ysize
;
;			Check the image array for validity.
;
npar = n_params(0)

IF npar EQ 0 THEN BEGIN		;Image array supplied?
	print,string(7B), 'Calling Sequence - CTVSCL,image,[x,y,field]'
	RETURN
ENDIF
sz = size(image)		;Warn user if image isn't two dimensional.
IF (sz(0) NE 2) THEN $
    message,'Image array (first parameter) is not 2 dimensional'

;
;	Check parameters 2,3,4,5 for existance, type and number.  
;	Separate numerical parameters from FITS header.
;

HDR_YN = 0	;All variables ending in _YN are booleans describing the 
Y_YN = 0	;types of input parameters given.
FIELD_YN = 0
POS_YN = 0
TRBL_YN = 0
NP = Intarr(4)
Index =0 

IF npar GE 2 then CTVParams, NP,P1, Header,HDR_YN,Index,trbl_yn
IF npar GE 3 then CTVParams, NP,P2, Header,HDR_YN,Index,trbl_yn
IF npar GE 4 then CTVParams, NP,P3, Header,HDR_YN,Index,trbl_yn
IF npar GE 5 then CTVParams, NP,P4, Header,HDR_YN,Index,trbl_yn

CASE Index OF
0:	Begin		;No plotting paramaters given
	POS_YN = 0 & X_YN = 0 & Y_YN = 0 & FIELD_YN = 0
	X = 0 & Y = 0
	end
1:	Begin		;POSITION parameter
	POS_YN = 1 & X_YN = 0 & Y_YN = 0 & FIELD_YN = 0
	X = NP(0) & Y = 0
	END
2:	Begin		;X & Y parameters
	POS_YN = 0 & X_YN = 1 & Y_YN = 1 & FIELD_YN = 0
	X = NP(0) & Y = NP(1)
	END
3:	Begin		;X, Y & FIELD parameters
	POS_YN = 0 & X_YN = 1 & Y_YN = 1 & FIELD_YN = 1
	X = NP(0) & Y = NP(1) & FIELD = NP(2)
	END
4:	Begin		;Uh, oh.
	message,"TOO MANY NUMERICAL PARAMETERS: 3 AT MOST!",/inf
	return
	end
else:   begin		;For debugging purposes.  Should never come up.
	message,"Encountered a problem.  Program quitting.",/inf
	return
	endELSE
endCASE
;	
;			Check FIELD keyword
IF keyword_set(Fld) then begin
	Field_yn = 1
	field = fld
    endIF
;
if not keyword_set(TOP) then top = 255

setrange = 0             ;Bit 1 Min Value set,  Bit 2 Max value set
if n_elements(mind) eq 1 then setrange = setrange + 1
if n_elements(maxd) eq 1 then setrange = setrange + 2
;
;			Check the PIXMAP keyword.
;
p = keyword_set(px)
;
;			If a window index has been supplied, make the
;			corresponding window the active window.
;
IF not(FIELD_YN) THEN field = chan
chan, field
;
;			Set window title
;
wtitle = 'IDL '+ strtrim(field,2)

IF keyword_set(TITLE) then wtitle = wtitle + '   ' + title $
ELSE IF HDR_YN then wtitle = wtitle + '    ' + Headerstring(Header)

;
;			Initialize window variables.
;
device, GET_SCREEN_SIZE = screen        ;Get maximum screen size
wi = !d.window
xwsz = !d.x_vsize
ywsz = !d.y_vsize
;
;			Check for a small window / screen.
;
rszflg = 0
IF (sz(1) GT xwsz) OR (sz(2) GT ywsz) THEN BEGIN
    IF ((sz(1) GT screen(0)) OR (sz(2) GT screen(1) )) AND $
        ((x GE 0) AND (y GE 0)) THEN $
           message,'Image array too large for the display screen'
    xwsz = sz(1)
    ywsz = sz(2)
    rszflg = 1
ENDIF
;
;			If a negative position was specified, extract subimage.
;
    IF (x LT 0) OR (y LT 0) THEN BEGIN
        largeim = 1  & npar = -1
        xst = abs(x) & xfn = (xst + 511) < (sz(1) - 1)
        yst = abs(y) & yfn = (yst + 511) < (sz(2) - 1)
        xwsz = xfn - xst + 1
        ywsz = yfn - yst + 1
        x_yn = 0 & y_yn = 0
        subim = image(xst:xfn,yst:yfn)
    ENDIF ELSE BEGIN
        largeim = 0
        xwsz = xwsz < sz(1)
        ywsz = ywsz < sz(2)
    ENDELSE

;			Open window, display image.

 device, WINDOW_STATE=opnd, GET_WINDOW_POSITION = Gwpos, GET_SCREEN_SIZE=screen

if not pos_yn then begin
   if (not opnd(chan)) or (!D.X_VSIZE NE xwsz) or (!D.Y_VSIZE NE ywsz) $
       or keyword_set(TITLE) or HDR_YN  then begin

		IF not(X_YN and Y_YN) then BEGIN
			XZ = Gwpos(0)
			YZ = Gwpos(1)
		endIF ELSE BEGIN
			XZ = X
			YZ = Y	
		endELSE

		IF (XZ + xwsz) GT screen(0) then xz = screen(0) - xwsz
		IF (YZ + ywsz) GT screen(1) then yz = screen(1) - ywsz
		xz = xz > 0
		YZ = yz > 0

	window, chan, xsize=xwsz, ysize=ywsz, xpos=xz, ypos=yz, $
		pixmap=p, title = wtitle
    endif
endif
CASE setrange OF	;Fill in missing scaling parameters
    3: begin	;Do nothing.  Everything has been chosen
	end 
    2: mind = min(image)
    1: maxd = max(image)
    0: begin
        if keyword_set(LOG) then begin
               iF LARGEIM then $
                    maxd = max(subim,MIN=mind) else $
                    maxd = max(image,MIN=mind) 
        endif else begin
            if not keyword_set(SIGRANGE) then sigrange = [-.25,12]
            if LARGEIM then sky, subim, avg, sigma, /SILENT else $
	                    SKY, Image, Avg, Sigma,/Silent
            if sigma GT 0. then begin
   	        mind = avg + Sigrange(0)*sigma
	        maxd = avg + Sigrange(1)*sigma
            endif else begin 
               iF LARGEIM then $
                    maxd = max(subim,MIN=mind) else $
                    maxd = max(image,MIN=mind)
             endelse 
	endelse
        end
    ENDCASE


IF NOT(Keyword_set(SILENT)) and not keyword_set(LOG) THEN $	
 message,"Image scaled between "+strn(mind)+" and "+strn(maxd),/INF

IF keyword_set(LOG) then BEGIN
	if not keyword_set(SILENT) then message,"Taking LOG(10) of IMAGE",/inf
	q = alog10(lindgen(32767)+1)
        mind = alog10(mind>1)  & maxd = alog10(maxd)
   endIF
;

if not pos_yn then begin
iF LARGEIM then begin

        if keyword_set(LOG) then $
           tv,bytscl(q(image(xst:xfn,yst:yfn)),MIN = mind, MAX=maxd,TOP = top) $
	else $
           tv,bytscl(image(xst:xfn,yst:yfn),min = mind,max=maxd,top=top)
       
endif else begin 

        if keyword_set(LOG) then $
	   tv, bytscl(q(image),MIN = mind, MAX = maxd, TOP = top)	$
        else $
	   tv, bytscl(image,MIN = mind, MAX = maxd, TOP =top)    

endelse
endif else begin 
iF LARGEIM then begin

        if keyword_set(LOG) then $
         tv,bytscl(q(image(xst:xfn,yst:yfn)),MIN = mind, MAX=maxd,TOP = top),x $
	else $
           tv,bytscl(image(xst:xfn,yst:yfn),min = mind,max=maxd,top=top),x
       
endif else begin 

        if keyword_set(LOG) then $
	   tv, bytscl(q(image),MIN = mind, MAX = maxd, TOP = top),x	$
        else $
	   tv, bytscl(image,MIN = mind, MAX = maxd, TOP =top),x

endelse
endelse
;			Fill common block variables.
;
    x00(chan) = x < 0
    y00(chan) = y < 0
    xsize(chan) = sz(1)
    ysize(chan) = sz(2)
    zoom(chan) = 1
;
return
end