;+
; PROJECT:
;   HESSI
;
; NAME:
;   hsi_vis_map2vis, map, xy, uv
;
; PURPOSE:
;   Returns an array of standard RHESSI visibility structures corresponding to an input map defined on a square array of pixels
;
; CATEGORY:
;   hessi/idl/image/visibility/gh
;
; CALLING SEQUENCE:
;   visarray = hsi_vis_map2vis(map, xy, uv)
;
; CALLS:
;   none
;
; INPUTS:
;   map is an nxy x nxy-element square array specifying the flux (photons/cm2/s) at nxy x nxy spatial points.
;   xy is a 2 x nxy element array specifying the E,N displacements in arcseconds of the map axes.  Origin is arbitrary.
;   uv is an 2 x nuv element array specifying the u and v values [arcsec^-1] at which the visibilities
;       are to be calculated.
;
; OPTIONAL INPUTS:
;   none
;
; OUTPUTS:
;   Returns an nuv-element array of standard visibility structures created by {hsi_vis}.
;       See visibilityguide.pdf for a description of the tags in this array.
;
;   Mapcenter is set to the centerpoint of the provided map coordinates.  Mapcenter tags are set accordingly.
;   Subcollimator number is inferred from the spatial frequency and the isc tags are set accordingly.
;   The harmonic is assumed to be 1 and the harmonic tags are set accordingly.
;   chi2 tags are arbitrarily set to 1.
;   sigamp tags are arbitrarily set to 10% of the largest amplitude.
;   erange tags are arbitrarily set to [1,2].
;
; OPTIONAL OUTPUTS:
;   none
;
; KEYWORDS:
;   none
;
; COMMON BLOCKS:
;   none
;
; SIDE EFFECTS:
;   none
;
; RESTRICTIONS:
;   sigamp, chi2 tags should be overwritten by the calling program if errors are relevant.
;   erange tags should be overwritten by the calling program.
;   trange, totflux, tags should be set subsequently by the calling program.
;
; MODIFICATION HISTORY:
;   12-April-2007     Initial version (ghurford@ssl.berkeley.edu)
;   13-April-2007 gh  Preset chi2 tags to 1.
;   14-June-2010  ras, changed nok loop index to longword
;   18-April-2017 ras, updated the blocking and urged using vis_map2vis
;-
FUNCTION hsi_vis_map2vis, map, xy, uv
  ;
  message,/info,'Please use vis_map2vis instead with its more modern and more flexible formulation'
  TWOPI           = 2 * !PI
  nxy             = N_ELEMENTS(xy)/2
  nuv             = N_ELEMENTS(uv)/2
  IF N_ELEMENTS(map) NE nxy^2 THEN MESSAGE, 'Inconsistent input dimensions'
  dummy = WHERE(map LT 0, nneg)
  IF nneg GT 0 THEN MESSAGE, 'Map has one or more negative pixels.'
  visout          = REPLICATE( {hsi_vis}, nuv)            ; Creates an array of standard visibility structures
  visout.u        = REFORM(uv[0,*])
  visout.v        = REFORM(uv[1,*])
  spatfreq2       = ((visout.u^2 + visout.v^2)*2.33^2)
  scn             = ROUND(-ALOG(spatfreq2) / ALOG(3))
  mapcenterx      = AVERAGE(xy[0,*])
  mapcentery      = AVERAGE(xy[1,*])
  visout.isc      = scn-1                                 ; assuming harm=1
  visout.harm     = 1                                     ; assumption
  visout.chi2     = 1.                                    ; arbitrary !!!!!
  visout.xyoffset = [mapcenterx, mapcentery]
  ;
  ; Begin loop over nonzero pixels.
  ok  = WHERE(map NE 0, nok)
  IF nok EQ 0 THEN MESSAGE, 'Empty map'
  FOR n=0L, nok-1 DO BEGIN
    ij              = ARRAY_INDICES(map, ok[n])                      ; 2-element vector with indices corresponding to nonzero map points
    phase           = TWOPI * (uv[0,*]*(xy[0,ij[0]]-mapcenterx) + uv[1,*]*(xy[1,ij[1]]-mapcentery))             ; nuv-element vector
    visout.obsvis   = visout.obsvis + map[ok[n]] * COMPLEX(COS(phase), SIN(phase))     ; nuv-element complex vector
  ENDFOR
  sigamp = 0.1 * MAX(ABS(visout.obsvis))
  visout.sigamp = sigamp                                  ; arbitrary !!!
  visout.erange = [1,2]
  RETURN, visout
END
