;+
; PROJECT:
;	HESSI
; NAME:
;	HSI_ANNSEC_MAP_INFO
;
; PURPOSE:
;	This procedure calculates various annular sector map building blocks.
;
; CATEGORY:
;	HESSI, IMAGE
;
; CALLING SEQUENCE:
;	hsi_annsec_map_info, cbe_obj, det_index, roll0, mpat, phase_mctr, $
;	  harmonic=harmonic
;
; CALLS:
;	HESSI OBJECTS
;
; INPUTS:
;       Cbe_obj - object containing calibrated eventlist object.
;
;	Det_index     - detector index
;	Roll0   - initial roll angle of calib_eventlist - roll_offset
; OUTPUTS:
;	Mpat - annular sector map structure. partially complete.
;	Phase_mctr - phase at the center of each annular sector map.
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	HARMONIC - default is first harmonic, 2nd harmonic is index 1, 3rd index 2.
;
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;	Version 1, richard.schwartz@gsfc.nasa.gov
;		30-mar-2000.
;	Version 2, ras, 24-apr-2000.
;	Version 3, ras, 8-may-2000.  Changed tag IPI to ISUM.  Changed it's meaning.
;	Also changed meaning of map_index.  Now all map_indices are referenced to all the annsecs.  The map_index
;	has the same meaning regardless of the value of ISUM.  There should only be one value of each MAP_INDEX
;	for a single value of ISUM.  ISUM is used to sum over the coefficients for the chosen maps, i.e.
;	for those values of MAP_INDEX which are used.
;	Version 3.1, ras, change map_range to modpat_skip, 10-may-2000.
;	Version 3.2, ras, extract modul_pattern control values in a single call to the object and pass that
;	structure to rmap_dim.
;-



pro hsi_annsec_map_info, cbe_obj, det_index, roll0, mpat, phase_mctr, $
harmonic=harmonic

twopi = 2.0 * !pi
checkvar,harmonic,0

par = cbe_obj->get(/image_dim,/xyoffset,/pixel_size,/factor_by,/pixel_scale,/modpat_skip,/r0_offset,/xaxis_fov)

image_dim  = par.image_dim
xyoffset   = par.xyoffset
pixel_size = par.pixel_size
factor_by  = par.factor_by > 1.0
pixel_scale= par.pixel_scale > 1
modpat_skip  = par.modpat_skip  > 1
r0_offset  = par.r0_offset
xaxis_fov  = par.xaxis_fov




rmap_dim   = hsi_rmap_dim( par )
;   pixel_scale must be a factor of both elements of rmap_dim
;   pixel_scale is used to set rmap_dim in hsi_rmap_dim

r0_asec    = sqrt( total(xyoffset^2) )

if xaxis_fov then r0_asec   = r0_offset

dphi       = min(pixel_size*pixel_scale) / r0_asec
total_phi  = ceil(twopi/2.0/dphi) + 48 ;cover at least 1/2 rotation plus some extra for convenience
cbe_ptr    =  (cbe_obj->getdata(class='hsi_calib_eventlist'))[det_index,harmonic]
nbin       = n_elements(*cbe_ptr)
gp         = hsi_grid_parameters()
pitch      = gp[det_index].pitch

phi0_rad = (atan(xyoffset[1],xyoffset[0]) + twopi) mod twopi
if xaxis_fov then phi0_rad = 0.0

num_fov_r         =  long(rmap_dim[0])
num_fov_phi       =  long(rmap_dim[1])


phi_map_first     = phi0_rad - gp[det_index].orient  + roll0  $
	- dphi * ((rmap_dim[1]-1)/2. + (rmap_dim[1] mod 2)/2.0)

rmin = r0_asec - num_fov_r/2* (pixel_size[0]*pixel_scale)
rmax = rmin + num_fov_r * (pixel_size[0]*pixel_scale)

area_max = !pi * rmax^2
area_min = !pi * rmin^2
darea    = (area_max-area_min)/num_fov_r
darea    = darea < ( !pi*r0_asec^2/(num_fov_r-1 - num_fov_r/2.0 + 0.5))
area      = area_min+findgen(num_fov_r+1)*darea

annsec_center = $
((findgen(num_fov_r)-num_fov_r/2.+0.5)*darea/!pi + r0_asec^2)^0.5

phz_ptr  = ptr_new( replicate( {map_index:0L, dphase: 0.0, ipi:0L, scoef:0}, nbin))

;
;Compute the area of a pixel, r * dr * dphi
;
area_pixel = avg(interpol(annsec_center[1:*]-annsec_center, $
	findgen(num_fov_r-1)+.5,findgen(num_fov_r))*annsec_center*dphi)


annsec_phi = findgen(total_phi + rmap_dim[1]) * dphi + phi_map_first

wave_number = twopi * (harmonic+1.) / pitch

;phase should correspond to the centers of fields of view.  The
;first field of view starts at rmap_dim[1]/2



phase_mctr    = (wave_number * r0_asec * $
cos( annsec_phi[ ((rmap_dim[1]+1)/2-1) : ((rmap_dim[1]+1)/2+total_phi-1) ] )) mod twopi
;cos( dphi/2.+annsec_phi[ ((rmap_dim[1]+1)/2-1) : ((rmap_dim[1]+1)/2+total_phi-1) ] )) mod twopi

phz_ptr  = ptr_new( replicate( {hsi_annsec_phase_str}, nbin ))


mpat =  {hsi_annsec_map_str,$
  det_index, $
  harmonic, $
  image_dim,$
  pixel_size,$
  factor_by, $
  pixel_scale, $
  modpat_skip, $
  xyoffset, $
  rmap_dim, $
  xaxis_fov, $
  r0_offset, $
  total_phi, $
  dphi, $
  ptr_new(annsec_center), $
  ptr_new(annsec_phi), $
  area_pixel, $
  phz_ptr, $ ;
  ptr_new(), $			;weight_map_ptr
  ptr_new(), $			;cmap_aver_ptr
  ptr_new(), $			;smap_aver_ptr
  ptr_new(cos(cos(annsec_phi)##(wave_number * annsec_center))),$
  ptr_new(sin(cos(annsec_phi)##(wave_number * annsec_center))) }

end
