PRO calc_reg_growing3,im,cont,filled,filled_contour,quiet=quiet
    contour_number=1
    abs_im=abs(im)
    bin=abs(im)
    IF min(bin) GT 10000 THEN BEGIN
        filled=bin
    filled(*,*)=0
    filled_contour=0
    GOTO, must_be_a_bad_image
    ENDIF
    bin(where(bin lt 50))=0
    bin(where(bin gt 0))=1
    IF NOT KEYWORD_SET(quiet) THEN print,'completed prelims'
    calc_pixel_indices, bin, cont, filled,/quiet
;STOP
    ar_pixels=where(filled eq 4,n_pixels)
    ;maxima=reverse(sort(abs_im(ar_pixels)))
    IF n_pixels eq 0 THEN GOTO, must_be_a_bad_image
    next_contour:
    maxima=(where(abs_im(ar_pixels) eq max(abs_im(ar_pixels))))(0)
    IF NOT KEYWORD_SET(quiet) THEN print,'completed calculating pixel indices'
    ;FOR go_thru_maxima = long(0), N_ELEMENTS(maxima)-1 DO BEGIN
        ;print,'growing',go_thru_maxima,'out of',n_elements(maxima)-1
        IF filled(ar_pixels(maxima)) eq 4 THEN BEGIN  ;checking if maxima is inside the AR boundary
            ;print,'growing',go_thru_maxima,'out of',n_elements(maxima)-1
            ;print,'inside AR'
            printxy2,im,ar_pixels(maxima),xy  ;xy is now pixel index value of a peak , the 'seed'
            seed=[ar_pixels(maxima)] ;initiate region with seed
            filled_area=[seed]
            grow_seed:
            ;candidates=[[xy(0,*)-1,xy(1,*)-1],[xy(0,*),xy(1,*)-1],[xy(0,*)+1,xy(1,*)-1],$
                 ;[xy(0,*)-1,xy(1,*)],[xy(0,*)+1,xy(1,*)],[xy(0,*)-1,xy(1,*)+1],$
             ;[xy(0,*),xy(1,*)+1],[xy(0,*)+1,xy(1,*)+1]]
            candidates_index=fltarr(1)
            FOR each_seed = 0, N_ELEMENTS(seed)-1 DO BEGIN
                possible_candidates=[[xy(0,each_seed)-1,xy(1,each_seed)-1],[xy(0,each_seed),xy(1,each_seed)-1],$
                  [xy(0,each_seed)+1,xy(1,each_seed)-1],[xy(0,each_seed)-1,xy(1,each_seed)],$
               [xy(0,each_seed)+1,xy(1,each_seed)],[xy(0,each_seed)-1,xy(1,each_seed)+1],$
               [xy(0,each_seed),xy(1,each_seed)+1],[xy(0,each_seed)+1,xy(1,each_seed)+1]]
                printindex,im,possible_candidates,possible_candidates_index
               ;account for having too large pixel_indices
               possible_candidates_index = possible_candidates_index (where(possible_candidates_index LT $
                   (size(im))(1)*(size(im))(2) AND possible_candidates_index GT 0))
                FOR each_candidate = 0,N_ELEMENTS(possible_candidates_index)-1 DO BEGIN & $
                    IF filled(possible_candidates_index(each_candidate)) ne 4 $
                   THEN possible_candidates_index(each_candidate) = 0 & $
                    IF (where(candidates_index eq possible_candidates_index(each_candidate)))(0) ne -1 $
                   THEN possible_candidates_index(each_candidate) = 0 & $
    	    	ENDFOR
                ;STOP
                candidates_index=[candidates_index, possible_candidates_index]
               IF (where (candidates_index NE 0))(0) ne -1 THEN $
                   candidates_index=candidates_index(where (candidates_index NE 0))
            ENDFOR
            ;STOP
            newseed=where(filled(candidates_index) eq 4)
            IF MAX(newseed) eq -1 THEN GOTO, stop_growing
            filled(seed)=-10*contour_number
            abs_im(seed)=-10*contour_number
	    ;filled(seed)=-2.*contour_number
	    seed=[candidates_index(newseed)]
            printxy2,im,candidates_index(newseed),newseed
            xy=newseed
            ;STOP
            GOTO, grow_seed ; go back and regrow
            stop_growing:
            ;STOP
	    ;known bug in calc_pixel_indices which gives stray pixels in'filled' which do not belong
	    ;i.e. are not inside any contours in cor_cont.
	    previous_max=max(abs_im(ar_pixels))
	    if max(abs_im(ar_pixels)) eq previous_max then abs_im(where(abs_im eq previous_max)) = 0.
	    ;plot_image,abs_im
	    ;contour,abs_im,level=(-10*contour_number)+0.1,/over
	    contour_number=contour_number+1
	    IF max(abs_im(ar_pixels)) LT 50 THEN GOTO, finished ELSE GOTO, next_contour
        ENDIF
        finished:
    	filled_contour=0
        FOR filled_index=1,contour_number-1 DO filled_contour=[filled_contour,where(filled eq -10.*filled_index),0]
        must_be_a_bad_image:
    ;ENDFOR
END