;	(1-sep-92)
function hist_scale,img,bright_limit=bright_limit,qtest=qtest
;+
;  NAME:
;    hist_scale
;  PURPOSE:
;    Find the max value of an array which has an obvious peak, 
;    but throw out spurious high values.
;  INPUTS:
;    img = Array containing an image
;  OPTIONAL INPUT KEYWORDS:
;    qtest = 1: Print status messages
;          = 2: Print messages and plot histogram to current window
;    bright_limit = Fraction of the histogram to cut (from the top).
;		    Default value = 0.5
;  RETURNED:
;    The maximum value which meets the selection criteria
;  METHOD:
;    Takes a histogram of the image.  Then it takes only those bins
;    which are >1, then >2, etc.  Then find the number between the max
;    and bright_limit * max.  Iterate until this number does not change
;    by a more than hist_min (see definition in code).
;
;    This routine should work properly on images that contain negative values.
;
;    The routine is designed to work on data sets which have an obvious
;    maximum, such as an optical solar image.
;    The results may not be reliable for essentially flat images.
;
;  MODIFICATION HISTORY:
;    Written,  1-sep-92, JRL
;-

if n_elements(bright_limit) eq 0 then 	$
  bright_limit = .5             ; Fraction of peak limb signal to cut
hist_min = .05                  ; Percentage change limit

tot_pix = n_elements(img)
hh = histogram(img)      	; Histogram of image
prev_max2 = 0

j = 0 & not_finished = 1
while( not_finished ) do begin
   ii=where(hh gt j)
   minx = min(img)
   maxx = minx + max( (lindgen(n_elements(hh)))(ii) )
   
   kk=where((img gt minx + bright_limit*(maxx-minx)) and (img le maxx),num_pix)
     if j gt 0 then begin	; Assume each loop will result in more pixels
        ccc_percent = (float(num_pix) - prev_num_pix) / prev_num_pix
        if (ccc_percent lt hist_min) and                   $
                (float(num_pix)/tot_pix gt .005) then not_finished = 0
     endif			; j gt 0
   if j eq 0 then begin
      ccc_percent = -9999
      prev_num_pix = num_pix
      prev_maxx = max(img)
   endif
   if keyword_set(qtest) then begin
       print,'j,prev,num,ccc=',j,prev_num_pix,num_pix,ccc_percent,	$
		maxx,prev_maxx,prev_max2,format='(a,3i10,f13.5,3i10)'
       if qtest ge 2 then begin
          xx = min(img) + indgen(n_elements(hh))
	  nn = n_elements(ii) / 2
          yrng = [0, 7 * total(hh(ii(nn:*))) / n_elements(hh(ii(nn:*)))]
          plot,xx(ii),hh(ii),yrange=yrng,title='j='+strtrim(j,2)+'; change='+strtrim(ccc_percent,2)
          bell = string(7b)
          ans = '' & if qtest lt 3 then read,'Pause '+bell+bell,ans
       endif
   endif
 
   j=j+1
   if j gt 10 then begin         ; Prevent an inifinite search situation
      print,'*** Warning:  hist_scale did not find a good cutoff'
      print,'***           The resulting max value may be bad'
      not_finished = 0
   endif
   if not_finished then begin
     prev_max2 = prev_maxx
     prev_maxx = maxx
     prev_num_pix = num_pix
   endif
endwhile

; The next statement is designed for good images
if (abs(prev_max2-maxx) / float(abs(maxx))) lt 0.1 then maxx = prev_max2
return,maxx
end

