;+
; PROJECT:
;	HESSI
; NAME:
;	HSI_ANNSEC_MPAT
;
; PURPOSE:
;	This routine builds modulation patterns on an annular sector grid.
;
; CATEGORY:
;	HESSI, UTIL, ANNSEC
;
; CALLING SEQUENCE:
;	mpat = hsi_annsec_mpat( i1, i2, cbe_obj, det_index, harmonic, map_ptr=map_ptr)
;
; CALLS:
;	HSI_ANNSEC_MAP
;
; INPUTS:
;       I1 - index of first modulation pattern.
;	I2 - index of last modulation pattern.
;	Cbe_obj - object containing hsi_calib_eventlist 
;	det_index
;	harmonic

; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	MAP_PTR - pointer containing annular sector modulation patterns. If not passed,
;	this pointer must be available through cbe_obj.
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;	Version 1, richard.schwartz@gsfc.nasa.gov
;	Version 1.1, richard.schwartz 11-jun-2000.
;
;-
function hsi_annsec_mpat,  i1, i2, cbe_obj, det_index, harmonic,  $
	map_ptr=map_ptr


twopi = 2.0 * !pi
if size(/tname,cbe_obj) eq 'POINTER' then cbe = cbe_obj else $
cbe = cbe_obj->getdata(class='hsi_calib_eventlist')
if n_elements(cbe) gt 1 then cbe = cbe[det_index,harmonic]
npat = i2-i1+1

mpat=hsi_annsec_map(cbe_obj,map_ptr,det_index, harmonic=harmonic, $
period=period)
rmap_dim  = mpat.rmap_dim
phase_ptr = mpat.phase_ptr
total_phi = mpat.total_phi


map_index = (*phase_ptr)[i1:i2].map_index


cos_factor = float(cos((*phase_ptr)[i1:i2].dphase)* $
	(*cbe)[i1:i2].modamp*(*cbe)[i1:i2].gridtran)
sin_factor = float(sin((*phase_ptr)[i1:i2].dphase)* $
	(*cbe)[i1:i2].modamp*(*cbe)[i1:i2].gridtran)*(*phase_ptr)[i1:i2].scoef


npixel = long(rmap_dim[0]*rmap_dim[1])-1
out = fltarr(long(rmap_dim[0])*rmap_dim[1],npat)

for i=0L,i2-i1 do begin
    index1  = map_index[i] * rmap_dim[0]

    index2 = index1 + npixel
    out[0,i]  = (*cbe)[i+i1].gridtran +(*mpat.cmap_ptr)[index1:index2] * cos_factor[i] - $
    (*mpat.smap_ptr)[index1:index2] * sin_factor[i]
    endfor



return,  reform(out, rmap_dim[0], rmap_dim[1], npat,/over)
end
