pro COMPUTE_STD_VALS,img,ihdr,nleb,nccd,avgv,sdev,medn,minv,maxv,nzero,mode
;
;
;+
; NAME:
;	COMPUTE_STD_VALS
;
; PURPOSE:
;	This function computes the standard values over regions and other
;	statistical information for monitoring the exposure value.
;
; CATEGORY:
;	DATA_ANAL
;
; CALLING SEQUENCE:
;	COMPUTE_STD_VALS,Img,Ihdr,Avgv,Sdev,Medn,Minv,Maxv,Nzero,Mode
;
; INPUTS:
;	Img:	The 2D image as read in by READFITS
;	Ihdr:	The image header in the LASCO structure
;
; OPTIONAL INPUTS:
;	None
;
; KEYWORD PARAMETERS:
;	None
;
; OUTPUTS:
;	Nleb:	Number of pixels being added together in the LEB
;	Nccd:	Number of pixels being added together on the CCD
;	Avgv:	Average intensity (1025 point floating array)
;	Sdev:	Standard deviation of the average (1025 point floating array)
;	Medn:	Median value (1025 point integer array)
;	Minv:	Minimum value (1025 point integer array)
;	Maxv:	Maximum value (1025 point integer array)
;	Nzero:	Number of zero values (1025 point integer array)
;	Mode:	Mode value (1025 point array)
;
; PROCEDURE:
;	The image is expanded into a full 1024 x 1024 array to account for
;	sub images.  Also leb summing and on-chip summing are accounted for.
;	The image is not normalized by the exposure time and the bias is not
;	subtracted off.  The average value, standard deviation, median 
;	value, minimum (non-zero) value, maximum value and the number of
;	zero pixels in each block are computed.  An array is generated of 
;	the six quantities for the entire array and then the 1024 32x32 
;	pixel blocks.  Each quantity then has an array size of 1025 values.
;	If the image is a subimage then the blocks outside of the subimage
;	would have zero values for the number of zeros.
;
; EXAMPLE:
;	To compute the exposure monitoring information:
;
;		COMPUTE_STD_SUMS,img,hdr,avg,sd,med,minv,maxv,nz
;
; MODIFICATION HISTORY:
; 	Written by:	R.A. Howard, NRL, 10/3/96
;	Modified:	RAH, NRL, 2/20/96  All blocks
;	Modified:	J. S. Morrill, NRL, 4/8-10/96: Added Mode and exp times
;	Modified:	RAH, NRL, 9/23/97  Check for odd image readout size
;	Modified:	RAH, NRL, 5/24/98  Check for underscan pixels
; 
; SCCS variables for IDL use
; 
; %W% %H% :NRL Solar Physics
;
; 
;-
;
;
;   allocate arrays for the ouputs
;
sdev=FLTARR(1025)
avgv=FLTARR(1025)
medn=LONARR(1025)
minv=LONARR(1025)
maxv=LONARR(1025)
nzero=LONARR(1025)
mode=LONARR(1025)
;
hdr = ihdr	; make working copy
;
;  Compute the values over the whole image
;
img=FIXWRAP(img)
b = FLOAT (img)
w = WHERE (img NE 0,nw)
nz = N_ELEMENTS(img) - nw
IF (nw gt 0) THEN BEGIN
   IF (nw EQ 1) THEN sig=0 ELSE sig = STDEV(b(w),avg)
   med = LONG( MEDIAN( img(w) ) )
   mn  = LONG( MIN (img(w),MAX=mx) )
   mx  = LONG( mx )
   hst = histogram(img,min=0)
   IF (N_ELEMENTS(hst) GT 700)  THEN BEGIN
      h1  = median(hst,91)		; 91 point median filter of histogram
      mxx  = max(h1(700:*))
      w   = where(mxx eq h1(700:*))
      med_md = median(w)+700
      avgv(0) = avg
      sdev(0) = sig
      medn(0) = med
      minv(0) = mn
      maxv(0) = mx
      nzero(0) = nz
      mode(0) = med_md 	;ave_md
   ENDIF ELSE nzero(0)=nz
ENDIF ELSE nzero(0)=nz
;
;  Correct the image size for summing, first leb summing then on-chip
;  divide by summing so don't have to when generating superpixels	
;
nleb = hdr.lebxsum * hdr.lebysum	; leb summing
IF (nleb GT 1)  THEN BEGIN
   b = b/nleb
   sz = SIZE(b)	
   b = REBIN (b,sz(1)*hdr.lebxsum,sz(2)*hdr.lebysum)
ENDIF
sumx = hdr.sumcol>1			; definition of sumcol is 0,2,3..
sumy = hdr.sumrow>1
nccd = sumx*sumy			; ccd summing
IF (nccd GT 1) THEN BEGIN
   b = b/nccd
   sz = SIZE(b)
   b = REBIN (b,sz(1)*sumx,sz(2)*sumy)
ENDIF
;
;  Check for an odd image readouts
;
;  First check for underscan pixels
;
IF (hdr.r1col LT 20)  THEN BEGIN
   PRINT,'Underscan Pixels: r1col = ',hdr.r1col
   b = b(20-hdr.r1col:*,*)
   hdr.r1col = 20
ENDIF
;
;  Check for overscan pixels
;
sz=SIZE(b)
nx = sz(1)-1
ny = sz(2)-1
IF ((nx+hdr.r1col-20) GT 1023)  THEN BEGIN
   nx=1043-hdr.r1col
   PRINT,'odd image size,nx,r1col = ',nx,hdr.r1col
   nx=1043-hdr.r1col
ENDIF
IF ((ny+hdr.r1row-1 ) GT 1023)  THEN BEGIN
   PRINT,'odd image size,ny,r1row = ',ny,hdr.r1row
   ny=1024-hdr.r1row
ENDIF
;
;  Insert the image into a full 1024x1024 array
;
full_img = fltarr(1024,1024)
full_img(hdr.r1col-20,hdr.r1row-1) = b(0:nx,0:ny)
;
;  Now compute the sums and the number of zero pixels in each block
;
FOR i=0,31 DO BEGIN
    row = i*32
    FOR j=0,31 DO BEGIN
        col = j*32
        k = i*32 + j + 1
        block = full_img(col:col+31,row:row+31)
        w = WHERE (block NE 0,nw)
        nz = N_ELEMENTS(block) - nw
        IF (nw gt 0) THEN BEGIN
           IF (nw EQ 1) THEN sig=0 ELSE sig = STDEV(block(w),avg)
           med = LONG( MEDIAN( block(w) ) )
           mn  = LONG( MIN (block(w),mx) )
           mx  = LONG( mx )
           a=block
           hst=histogram(a,min=0)
           nbox = n_elements(hst)<21
           IF (nbox GT 2)  THEN h1=median(hst,nbox) ELSE h1=hst
           mxx = max(h1)
           w=where(mxx eq h1)
;           print,median(w)+700
           med_md = median(w)
           avgv(k) = avg
           sdev(k) = sig
           medn(k) = med
           minv(k) = mn
           maxv(k) = mx
           nzero(k) = nz
	   mode(k) = med_md 	;ave_md
;print,md,ave_md
;plot,hist
;stop
;print,'for i = ',i,' & j = ',j,', mode = ',md,', Max(blk) = ',$
;mhist 
        ENDIF ELSE nzero(k)=nz
    ENDFOR
ENDFOR
RETURN
END
