;+
;  NAME
;	DOPP_ROLL
;
;  PURPOSE
;  	Calculate the SOHO roll angle from a Dopplergam.  The Dopplergram is assumed to 
;  	be in the form of a square array, reasonably cropped.  We take the inner 4/5 of
;  	a circle centered at the center of the image to be the locus of the Dopplergram.
;
;  CALLING SEQUENCE
;	roll = DOPP_ROLL(img,date)
;
;  INPUT PARAMETERS
;	IMG   - the Dopplergram itself
;	DATE  - A string containing the time and date at which the image was recorded.
;
;  RETURNS
;	ROLL  - the roll angle relative to P=0, in degrees.
;
;  HISTORY
;	Hacked together, 22-March-99, by Craig DeForest
;
;  CAVEATS
;	Don't know which direction the roll is supposed to go yet.
;	Haven't included annual variations in the roll.
;- 	
function dopp_roll,img,date,cross_method=cross_method

; Make sure IDFL doesn't roll over when summing short-integer pixels
fimg = double(img)

; Extract height and width of the image
ww = n_elements(img(*,0))
hh = n_elements(img(0,*))
if(ww ne hh) then print,'%DOPP_ROLL: WARNING -- ww ne hh, using minimum value for radius'

xcoords = (indgen(ww))#(1+intarr(hh))
ycoords = (1+intarr(ww))#(indgen(hh))
rcoords = sqrt((xcoords-ww/2)*(xcoords-ww/2)+(ycoords-hh/2)*(ycoords-hh/2))

rmask = (rcoords lt (ww<hh)*0.4)

if keyword_set(cross_method) then begin
	mleft = (xcoords lt (ycoords < (hh-1-ycoords))) * rmask
	mright= (xcoords gt (ycoords > (hh-1-ycoords))) * rmask
	mtop  = (ycoords gt (xcoords > (ww-1-xcoords))) * rmask
	mbot  = (ycoords lt (xcoords < (ww-1-xcoords))) * rmask
	left  = total(fimg * mleft) / total(mleft)
	right = total(fimg * mright)/ total(mright)
	top   = total(fimg * mtop)  / total(mtop)
	bot   = total(fimg * mbot)  / total(mbot)
end else begin
	left  = total(fimg(0:ww/2-1,*)  * rmask(0:ww/2-1,*))  / total(rmask(0:ww/2-1,*))
	right = total(fimg(ww/2:ww-1,*) * rmask(ww/2:ww-1,*)) / total(rmask(ww/2:ww-1,*))
	top   = total(fimg(*,hh/2:hh-1) * rmask(*,hh/2:hh-1)) / total(rmask(*,hh/2:hh-1))
	bot   = total(fimg(*,0:hh/2-1)  * rmask(*,0:hh/2-1))  / total(rmask(*,0:hh/2-1))
end

roll = atan((top-bot),(right-left)) * 180/!pi

return,roll

end

