;+
; PROJECT:
;   HESSI
; NAME:
;   HSI_ANNSEC_COORD
;
; PURPOSE:
;   This procedure computes the coordinates of the annular sector map in Cartesian
;   coordinates.
; CATEGORY:
;   HESSI, IMAGE, UTIL, ANNSEC
;
; CALLING SEQUENCE:
;   Hsi_annsec_coord, hsi_img_obj, x, y, map_ptr=map_ptr
;
; CALLS:
;   HESSI OBJECTS, HSI_ANNSEC_BPROJ_WEIGHT, DATATYPE
;
; INPUTS:
;   Hsi_img_obj - object containing calibrated eventlist object.  Should also contain modpat control object.
;   ;
;
; OPTIONAL KEYWORD INPUTS:
;   modpat_ptr - Pointer array which stores the universal modulation pattern map structure.
;     Used in for testing only.  Normally, the object carries the modulation pattern structure.
;
; OUTPUTS:
;   x - x coordinate in arcsec from Sun Center of annular sector map, dimensioned same as image.
;   y - y coordinate in arcsec from Sun Center of annular sector map, dimensioned same as image.
;   rmap - radial coordinate of annular sector map. Not necessarily from sun center, arcsec.
;   thetamap - azimuthal coordinate of annular sector map.  In radians.
;
; OPTIONAL OUTPUTS:
;   XY_STR - {  x:x, y:y }
;
; KEYWORDS:
;   none
; COMMON BLOCKS:
;   none
;
; SIDE EFFECTS:
;   none
;
; RESTRICTIONS:
;   none
;
; PROCEDURE:
;   Uses trigrid interpolation between the two systems.
;
; MODIFICATION HISTORY:
;   Version 1, richard.schwartz@gsfc.nasa.gov, 17-may-2000
;   Version 2. richard.schwartz@gsfc.nasa.gov, 28-nov-2000
;     removed unnecessary block to validate pointer again.
;   22-aug-2001,
;   6-sep-2003 extract xyoffset from image_object
;   19-nov-2004, richard.schwartz@gsfc.nasa.gov, changed to
;     USED_XYOFFSET, it is the value for the image consistent
;     with update.  Set at the end of calib_evenlist::process
;     much faster retrieval.
;   19-apr-2017, RAS, added XY_STR output
;-

pro hsi_annsec_coord, hsi_img_obj, x, y, rmap, thetamap, $
  XY_STR = xy_str, $
  MODPAT_PTR=modpat_ptr


  nvalid = 0



  if size(modpat_ptr,/tname) ne 'POINTER' then $
    modpat_ptr = hsi_img_obj-> getdata(class_name='hsi_modul_pattern')



  if nvalid eq 0 then valid = where(ptr_valid( modpat_ptr))

  modpat = *(modpat_ptr[valid[0]])


  image_dim = modpat.image_dim
  rmap_dim  = modpat.rmap_dim
  ;xyoffset  = hsi_img_obj->Get(/xyoffset)
  ;Change to USED_XYOFFSET, 19-nov-2004, ras
  xyoffset  = hsi_img_obj->Get(/used_xyoffset)
  xaxis_fov = 1
  r0_offset = modpat.r0_offset

  rmap    = *modpat.annsec_center_ptr

  if (xyoffset[0] eq 0. and xyoffset[1] eq 0.) then theta0 = 0. else $  ;kim 04-apr-2002
    theta0  = atan(xyoffset[1], xyoffset[0])
  thetamap= (findgen(rmap_dim[1])- (rmap_dim[1]-1)/2.0)*modpat.dphi + theta0


  if xaxis_fov then begin
    thetamap = thetamap - theta0
    x = rmap # cos( thetamap ) + xyoffset[0] - r0_offset
    y = rmap # sin( thetamap ) + xyoffset[1]
  endif else begin
    x = rmap # cos( thetamap )
    y = rmap # sin( thetamap )
  endelse
  if arg_present( xy_str ) then xy_str = { x:x, y:y }
end




