;------------------------------------------------------------------
;+
; NAME:
;	ELEFLUX
; PURPOSE:
;	This function finds magnetic field elements in a magnetogram.
;	It returns the maximum value, the location of the maximum
;	value, the size in pixels, the average field value of
;	each element (which can be related to the flux), and the element
;	number for each
;	magnetic element found.  An element is defined as a contiguous
;	region in the image with the absolute value greater than
;	a set cutoff value.  Only elements with a size greater than
;	the minimum value are returned.
; CATEGORY:
;	POLAR
; CALLING SEQUENCE:
;	retval = ELEFLUX(mag,cutoff,minsize,maxsize,ycut)
; INPUTS:
;	mag        = input magnetogram
;	cutoff     = minimum field strength of a region
;	minsize    = minimum size of a region
;	maxsize    = maximum size of a region
;	ycut       = y cutoff value
; KEYWORD PARAMETERS:
; OUTPUTS:
;	retval
; COMMON BLOCKS:
; NOTES:
; MODIFICATION HISTORY:
;	J. Varsik,	13 May 1996
;-
;-------------------------------------------------------------------

function eleflux,mag,cutoff,minsize, maxsize, ycut

    magsiz = size(mag)
    xax = magsiz(1)

    retval = replicate({regf, id: 0, max: 0.0, xpos: 0, ypos: 0, favg: 0,$
                       size: 0}, 400)

    absmag = abs(mag)
    absmagc = absmag gt cutoff
    amagrl = label_region(absmagc)
    hmag   = histogram(amagrl,reverse_indices=r)

    print,"   reg no.     max.              x        y       flux    size"
    l = 0
    for i = 0, n_elements(hmag)-1 do begin
        p = r(r(i):r(i+1)-1)
        qa = absmag(p)
        qnum = n_elements(qa)
        if ((qnum gt minsize) and (qnum lt maxsize)) then begin
              qamax = max(qa,j)
	      q = mag(p)
	      qavg = mean(q)
	      qmax = q(j)
              k = p(j)
              ix = k mod xax
              iy = k / xax
              if ycut gt 180 then begin
                if iy lt ycut then begin
                  print,i,qmax,ix,iy,qavg,qnum
                  retval(l) = {regf,i,qmax,ix,iy,qavg,qnum}
                  l = l + 1
                endif
              endif else begin
                if iy lt ycut then begin
                  print,i,qmax,ix,iy,qavg,qnum
                  retval(l) = {regf,i,qmax,ix,iy,qavg,qnum}
                  l = l + 1
                endif
              endelse
        endif
    endfor

    retval = retval(0:l-1)
    return,retval
end