function je_2d_new,dat2,ENERGY=en,ERANGE=er,EBINS=ebins,ANGLE=an,ARANGE=ar,BINS=bins
common last_pot,pot
if not keyword_set(pot) then pot=0.
eflux2dz = 0.
if dat2.valid eq 0 then begin
print,'Invalid Data'
return, eflux2dz
endif
dat = conv_units(dat2,"df")
na = dat.nenergy
nb = dat.nbins
charge=1.
value=0 & str_element,dat,'charge',value
if value le 0 or value ge 0 then value=value else value=0
if value ne 0 then charge=dat.charge
if ((value eq 0) and (dat.mass lt 0.00010438871)) then charge=-1.
value=0 & str_element,dat,'sc_pot',value
if value le 0 or value ge 0 then value=value else value=0
sc_pot = value*1.2 + 1.
if value eq 0 then sc_pot=pot
if sc_pot gt 66.*1.2 and charge eq -1. then begin
peak_e=cold_peak_2d(dat,energy=[66.*1.2,1000.])
ind = where(dat.energy(*,0) eq peak_e)
ind1 = ind
maxcnt=max(dat.data(ind1,*),ind2)
d0=dat.data(ind1,ind2)
e0=dat.energy(ind1,ind2)
dm=dat.data(ind1-1,ind2)
em=dat.energy(ind1-1,ind2)
dp=dat.data(ind1+1,ind2)
ep=dat.energy(ind1+1,ind2)
if dm ge dp then sc_pot=(dm*em+d0*e0)/(dm+d0)
if dm lt dp then sc_pot=(dp*ep+d0*e0)/(dp+d0)
endif
if value ne 0 then pot=sc_pot
if dat.data_name ne 'HSPAD' and dat.energy(0)-dat.denergy(0) lt dat.sc_pot then begin
tmpmin=min(abs(dat.energy(*,0)-sc_pot),ind1)
tmpmax=max(dat.data(ind1,*),ind2)
th=dat.theta(ind1,ind2)
if (sc_pot gt 20. and th ne 0. and th ne 180.) then begin
if th le 90. then begin
rot_ind=-fix(th/15.)
if dat.data(ind1,ind2-1) lt dat.data(ind1,ind2+1) then rot_ind=rot_ind-1
dat3=dat
dat3.data=shift(dat3.data,0,rot_ind)
dat.data(ind1-2:ind1+1,*)=dat3.data(ind1-2:ind1+1,*)
endif
if th gt 90. then begin
rot_ind=fix(180-th/15.)
if dat.data(ind1,ind2-1) gt dat.data(ind1,ind2+1) then rot_ind=rot_ind+1
dat3=dat
dat3.data=shift(dat3.data,0,rot_ind)
dat.data(ind1-2:ind1+1,*)=dat3.data(ind1-2:ind1+1,*)
endif
endif
endif
energy=dat.energy+(charge*(sc_pot)/abs(charge))
emin=energy-dat.denergy/2.>0.
emax=energy+dat.denergy/2.>0.
dat.energy=(emin+emax)/2.
dat.denergy=(emax-emin)
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
energy = dat.energy
denergy = dat.denergy
theta = dat.theta/!radeg
dtheta = dat.dtheta/!radeg
mass = dat.mass
value=0 & str_element,dat,'domega',value
if n_elements(value) ne 1 then domega = dat.domega
if max(dat.theta) gt 200. then begin
if (theta(0,0) eq theta(na-1,0)) then nna=0 else nna=na-1
if ndimen(dtheta) eq 1 then dtheta=replicate(1.,na)#dtheta
domega = theta
for a=0,nna do begin
for b=0,nb-1 do begin
if (abs(theta(a,b)-!pi) lt dtheta(a,b)/2.) then begin
th1 = (!pi+theta(a,b)-dtheta(a,b)/2.)/2.
dth1 = (!pi-th1)
th2 = (!pi+theta(a,b)+dtheta(a,b)/2.)/2.
dth2 = (th2-!pi)
domega(a,b)=2.*!pi*(abs(sin(th1))*sin(dth1)*cos(th1)*cos(dth1) + abs(sin(th2))*sin(dth2)*cos(th1)*cos(dth2))
endif else if (abs(theta(a,b)-2*!pi) lt dtheta(a,b)/2.) then begin
th1 = (2.*!pi+theta(a,b)-dtheta(a,b)/2.)/2.
dth1 = (2.*!pi-th1)
th2 = (2.*!pi+theta(a,b)+dtheta(a,b)/2.)/2.
dth2 = (th2-2.*!pi)
domega(a,b)=2.*!pi*(abs(sin(th1))*sin(dth1)*cos(th1)*cos(dth1) + abs(sin(th2))*sin(dth2)*cos(th1)*cos(dth2))
endif else if (abs(theta(a,b)) lt dtheta(a,b)/2.) then begin
th1 = (theta(a,b)-dtheta(a,b)/2.)/2.
dth1 = abs(th1)
th2 = (theta(a,b)+dtheta(a,b)/2.)/2.
dth2 = (th2)
domega(a,b)=2.*!pi*(abs(sin(th1))*sin(dth1)*cos(th1)*cos(dth1) + abs(sin(th2))*sin(dth2)*cos(th1)*cos(dth2))
endif else begin
th1 = theta(a,b)
dth1 = dtheta(a,b)/2.
domega(a,b)=2.*!pi*abs(sin(th1))*sin(dth1)*(cos(th1)*cos(dth1))
endelse
endfor
endfor
if (nna eq 0) then for a=1,na-1 do domega(a,*)=domega(0,*)
endif else domega=domega*cos(theta)
eflux2dz = total(data*denergy*energy^2*domega)*(mass)^(-2.)*(2.)*(1.e5)*1.6e-12
return, eflux2dz
end