function make_3dmap, dat,nx,ny, highest=highest
if n_elements(nx) eq 0 then nx = 64
if n_elements(ny) eq 0 then ny = 32
str_element,dat,'bins',bins
if n_elements(bins) eq dat.nenergy*dat.nbins then $
bins = total(bins,1) gt 0
if n_elements(bins) eq 0 then bins = replicate(1b,dat.nbins)
phi = total(dat.phi,1,/nan)/total(finite(dat.phi),1)
theta = total(dat.theta,1,/nan)/total(finite(dat.theta),1)
dphi = total(dat.dphi,1,/nan)/total(finite(dat.dphi),1)
dtheta = total(dat.dtheta,1,/nan)/total(finite(dat.dtheta),1)
map = replicate(-1,nx,ny)
nbins = n_elements(phi)
p1 = round((phi-dphi/2.)*nx/360.)
p2 = round((phi+dphi/2.)*nx/360.)-1
t1 = round((theta-dtheta/2.+90.)*ny/180.)
t2 = round((theta+dtheta/2.+90.)*ny/180.)-1
for b1=0,nbins-1 do begin
if keyword_set(highest) then b=b1 else b=nbins-b1-1
if (bins[b] gt 0) and (p2(b) ne -1) and (p1(b) ne -1) then begin
p = indgen(p2(b)-p1(b)+1) + p1(b)
p = (p+nx) mod nx
t = indgen(t2(b)-t1(b)+1) + t1(b)
t = (t+ny) mod ny
for i=0,n_elements(t)-1 do map(p,t(i)) = b
endif
endfor
return,map
end