Viewing contents of file '../idllib/ghrs/pro/boxcount.pro'
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+
;
;*NAME:
;	BOXCOUNT
;
;*PURPOSE:
;   Determine integrated counts within selected regions. The regions are
;	specified by registering the cursor on a contour map.
;
;*CALLING SEQUENCE:
;	BOXCOUNT,map,x,y,COUNTS,CPP,XPOS,YPOS
;
;*PARAMETERS:
;	input:   map  -contour map, assumed already on screen
;	 	x,y  - x,y coordinates used in the plot
;	output:  counts - output counts in the boxes
;	 	cpp    -counts/per pixel in the boxes
;	 	xpos, ypos  - x and y vertices of the selected boxes
;
;*EXAMPLES:
;	ta_mosaic,'z0j50102m',map		; 5x5 mosaic
;	siz= size(map)
;	xsiz= findgen(siz(1))
;	ysiz= findgen(siz(2))
;	contour, map
;	boxcount,map,xsiz,ysiz,counts,cpp,xpos,ypos
;		1) place cursor on lower-left corner of region of interest
;		2) place cursor on upper-right corner of region of interest
;		3) repeat steps 1-2 for each region of interest.
;		3) type 0 to exit.
;
;*RESTRICTIONS:
;*NOTES:
;	Use cursor to select each box. Register cursor at lower left and then
;	upper right hand corner of region of interest.
;
;*MODIFICATION HISTORY:
;	Bruce Altner July 1990
;	31-OCT-1991 	JKF/ACC		-changed cursor call to work with MAC.
;       13-May-1992   	DJN/CSC  -checks for neg values in selected box. Sets
;				   any negative values found to zero prior to
;				   totally up the integrated number of counts.
;	18-may-1992	JKF/ACC		- fixed bug in domain limits of plot.
;-
;-------------------------------------------------------------------------------
PRO BOXCOUNT,map,x,y,COUNTS,CPP,XPOS,YPOS

on_error,2
if n_params(0) lt 1 then $
	message,' Calling Sequence- BOXCOUNT,MAP,X,Y,COUNTS,CPP,XPOS,YPOS'

;
; Allocate output arrays.
;
XPOS=FLTARR(5,100)
YPOS=FLTARR(5,100)
COUNTS=FLTARR(100)
AREAS=FLTARR(100)

I=0
!ERR=0
S=SIZE(MAP) & NX=S(1) & NY=S(2)
;
; Special case for Tektronix and non-Tektronix terminals.
;
if !d.name eq 'TEK' then begin
	device,gin_char=6
	err_val = "60
end else err_val = 4

;
; Loop on all regions of interest...0 to exit
;
print,' Select (lower/left) and (upper/right) corners of regions of interest'
WHILE !ERR NE err_val DO BEGIN
	;
	; Register cursor (lower left)
	;
	if !d.name eq 'TEK' then $
		cursor,X1,Y1,/data $
	else cursor,X1,Y1,/data,/down
	IF !ERR EQ err_val THEN GOTO,SKIP
	;
	; Register cursor (upper right)
	;
	if !d.name eq 'TEK' then $
		cursor,X2,Y2,/data $
	else cursor,X2,Y2,/data,/down

	IX=[X1,X2,X2,X1,X1]
	IY=[Y1,Y1,Y2,Y2,Y1]
	OPLOT,iX,iY			; draw box around region of interest.
	TABINV,X,X1,IX1 & IX1=FIX(IX1+.5)
	TABINV,Y,Y1,IY1 & IY1=FIX(IY1+.5)
	TABINV,X,X2,IX2 & IX2=FIX(IX2+.5)
	TABINV,Y,Y2,IY2 & IY2=FIX(IY2+.5)
	;
	; Restrict box to within the valid image dimensions...dont go outside.
	;
        IX1=IX1>0<(NX-1)
        IX2=IX2>0<(NX-1)
        IY1=IY1>0<(NY-1)
        IY2=IY2>0<(NY-1)
	;
	; Sort the positions, incase user did not register values left to right.
	;
	IF IX1 GT IX2 THEN BEGIN
         IXT=IX1
         IX1=IX2
         IX2=IXT
        END
        IF IY1 GT IY2 THEN BEGIN
         IYT=IY1
         IY1=IY2
         IY2=IYT
        END

	;
	; Determine if any negative values (i.e. outside the map) are in
	;  	in the map. If so, display warning message.
	;
	check_neg = where( MAP(IX1:IX2,IY1:IY2) lt 0.0, found)
	if found gt 0 then xyouts,.1,.05,/norm, $
		"Warning: Negative values in box will be set to zero."

	;
	; Compute results for each region of interest.
	;
	COUNTS(I)=TOTAL(MAP(IX1:IX2,IY1:IY2)>0)	; force >0	(DJN 5/12/92)
        XPOS(0,I)=IX
        YPOS(0,I)=IY
        AREAS(I)=(IX2-IX1)*(IY2-IY1)    ;TOTAL NUMBER OF PIXELS
        I=I+1
END

skip:
;
; Truncate results.
;
counts=counts(0:i-1)
xpos=xpos(*,0:i-1)
ypos=ypos(*,0:i-1)
areas=areas(0:i-1)
;
; Compute counts/per pixel in the boxes
;
cpp=counts/areas

RETURN
END