;+

;FUNCTION:	peak_2d(dat,ENERGY=en,ERANGE=er,EBINS=ebins,ANGLE=an,ARANGE=ar,BINS=bins)

;INPUT:	

;	dat:	structure,	2d data structure filled by get_eesa_surv, get_eesa_burst, etc.

;KEYWORDS

;	ENERGY:	fltarr(2),	optional, min,max energy range for integration

;	ERANGE:	fltarr(2),	optional, min,max energy bin numbers for integration

;	EBINS:	bytarr(na),	optional, energy bins array for integration

;					0,1=exclude,include,  

;					na = dat.nenergy

;	ANGLE:	fltarr(2),	optional, min,max pitch angle range for integration

;	ARANGE:	fltarr(2),	optional, min,max angle bin numbers for integration

;	BINS:	bytarr(nb),	optional, angle bins array for integration

;					0,1=exclude,include,  

;					nb = dat.ntheta

;	BINS:	bytarr(na,nb),	optional, energy/angle bins array for integration

;					0,1=exclude,include

;PURPOSE:

;	Returns the energy of the energy flux peak, eV

;NOTES:	

;	Function normally called by "get_2dt.pro" to generate 

;	time series data for "tplot.pro".

;

;CREATED BY:

;	J.McFadden

;LAST MODIFICATION:

;	97-6-4		J.McFadden

;-

function peak_2d,dat2,ENERGY=en,ERANGE=er,EBINS=ebins,ANGLE=an,ARANGE=ar,BINS=bins



peak = 0.



if dat2.valid eq 0 then begin

  print,'Invalid Data'

  return, peak

endif



dat = conv_units(dat2,"eflux")		; Use Energy Flux

na = dat.nenergy

nb = dat.nbins

	

ebins2=replicate(1b,na)

if keyword_set(en) then begin

	ebins2(*)=0

	er2=[energy_to_ebin(dat,en)]

	if er2(0) gt er2(1) then er2=reverse(er2)

	ebins2(er2(0):er2(1))=1

endif

if keyword_set(er) then begin

	ebins2(*)=0

	er2=er

	if er2(0) gt er2(1) then er2=reverse(er2)

	ebins2(er2(0):er2(1))=1

endif

if keyword_set(ebins) then ebins2=ebins



bins2=replicate(1b,nb)

if keyword_set(an) then begin

	if ndimen(an) ne 1 or dimen1(an) ne 2 then begin

		print,'Error - angle keyword must be fltarr(2)'

	endif else begin

		bins2=angle_to_bins(dat,an)

	endelse

endif

if keyword_set(ar) then begin

	bins2(*)=0

	if ar(0) gt ar(1) then begin

		bins2(ar(0):nb-1)=1

		bins2(0:ar(1))=1

	endif else begin

		bins2(ar(0):ar(1))=1

	endelse

endif

if keyword_set(bins) then bins2=bins



if ndimen(bins2) ne 2 then bins2=ebins2#bins2





data = dat.data*bins2

if ndimen(data) eq 2 then spec = total(data,2) else spec=data

maxspec = max(spec,index)

if ndimen(dat.energy) eq 2 then peak = dat.energy(index,0) else peak = dat.energy(index)



return, peak



end



