
;+
; NAME:
;	ThomsonBrightness
; PURPOSE:
;	Calculates weights for integrating Thomson scattering signal
; CATEGORY:
;	Physics: Thomson scattering
; CALLING SEQUENCE:
	FUNCTION ThomsonBrightness, rr_earth, R, F, f3darg,	$
		ut_earth	= ut_earth	, $
		pa_earth	= pa_earth	, $
		elo_earth	= elo_earth , $
		elo_sun 	= elo_sun	, $
		degrees 	= degrees
; INPUTS:
;	rr_earth	array[3]			NOT USED; for 'plain' sky map
;				array[3,N]			NOT USED; for 'transit' sky map
;									NOT USED; heliographic coordinates of Earth for lines of sight
;	R			array[3,N,L,M]		(N,L may be 1 or absent)
;									locations of all segments for all lines of sight
;									in spherical coordinates (usually heliographic)
;									This can be a grid of NxL lines of sight with M
;									segments along each line of sight
;									R[0,*,*,*] : longitude
;									R[1,*,*,*] : latitude
;									R[2,*,*,*] : heliocentric distance (AU)
;	F			array[N,L,M]		normalized densities n*(r/r0)^2 at los segments (electrons/cm^-3)
;	f3darg		array[4]			f3darg[0]	Line of sight step size (AU)
;									f3darg[1]	Limb darkening constant
;									f3darg[2]	Apparent magnitude Sun at 1 AU
;									f3darg[3]	0=Intensity; 1: Tangential; 2: Tang-Rad
;									f3darg[4]	normalization for density (default: 2)
; OPTIONAL INPUT PARAMETERS:
;	/degrees						if set all input angles should be in degrees (default: radians)
;	ut_earth	array[1]			not used
;	pa_earth	array[N,L,M]		not used
;	elo_earth	array[N,L,M]		angle Sun-Earth-LOS segment (i.e. conventional elongation angle)
;	elo_sun		array[N,L,M]		angle Earth-Sun-LOS segment
; OUTPUTS:
;	R			array[N,L,M]		weight factors W(s) at location s along line of sight in S10
;									Intensity B = Sum( W(s) ) = Sum( I(s)n(s)ds )
; INCLUDE:
	@compile_opt.pro						; On error, return to caller
; CALLS:
;	ToRadians, ThomsonElectron, SubArray
; PROCEDURE:
;	ThomsonElectron returns 10^-26 S10. Multiply by the step size along the line
;	of sight in cm (f3darg[0]*!sun.au*10^13) and multiply with the remaining
;	10^-13. The value returned is a brightness in S10 for each line of sight segment.
; MODIFICATION HISTORY:
;	SEP-1999, Paul Hick (UCSD/CASS)
;	SEP-1999, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;		Removed restriction on dimensions of R,F,elo_earth,elo_sun.
;		F, elo_earth, elo_sun should all have the same structure, while
;		R should have an additional leading dimension of size 3.
;-

rpm = ToRadians(degrees=degrees)

rr = SubArray(R, element=2)				; Sun-Electron distance in AU
SinChi = sin( (elo_earth+elo_sun)*rpm )		; Sin (angle Sun-Electron-Observer)

n_norm = n_elements(f3darg) GT 4 ? f3darg[4] : 2

RETURN, !sun.au*f3darg[0]*1.0E-13*(F/rr^n_norm)*ThomsonElectron(SinChi, P, rsun=rr, /au,	$
	udark=f3darg[1], apm=f3darg[2], /s10, tangonly=f3darg[3] EQ 1, tangmrad=f3darg[3] EQ 2)
END
