;	(4-sep-91)
FUNCTION OFF_AXIS,X0,NX,Y0,NY,SUM_MODE=SUM_MODE,VERSION=VERSION
;+
;  NAME:
;	OFF_AXIS
;
;  PURPOSE:
;	Compute the off-axis sensitivity reponse function for the SXT.
;	The function is normalized to 1.0 on-axis and drops off as the
;	edge of the SXT field of view is reached.
;
; CALLING SEQUENCE:
;	Result_array = OFF_AXIS(x0,nx,y0,ny[,sum_mode=sum_mode,version=version])
;
;	In order to correct an SXT image for off-axis sensitivity, use the
;	following expression:
;
;	IMG = IMG / OFF_AXIS(x0,nx,y0,ny)
;
;	In this case, it is assumed that IMG is an nx by ny array whose
;	starting coordinates are at x0 and y0 (in CCD pixel coordinates).
; INPUTS:
;	X0,Y0	= The starting coordinates in CCD pixel numbers.
;		  X refers to CCD column numbers and Y to row numbers.
;		  (The first non-BLS column is considered X0 = 0.)
;	NX,NY	= The number of columns and rows.
;
; OUTPUTS:
;	Functional result is the off-axis sensitivity response of SXT.
;	The result is a 2-d floating array normalized to 1.0 at the center
;	of the SXT field of view.
;
; OPTIONAL KEYWORD PARAMETERS:
;	VERSION = (Output) String containing the version number of the 
;			   response function.
;	SUM_MODE =(Input)  The assumed summation mode.  0=1x1, 1=2x2, 2=4x4
;			   If not present, SUM_MODE = 0 is assumed.
;		 
; COMMON BLOCKS;
;	None.
;
; FILE I/O:
;	None.
;
; METHOD:
; 	Uses a 2-d polynomial representation to compute the SXT response
;	function for the desired position in the field of view.
;
; MODIFICATION HISTORY:
;	Version 1.0 - JRL, 4-sep-91
	VERSION = 'V1.0 4-sep-91'
;-
; - - - - - - - - - - - - - - - - - - - - - - - - - - - -
;  If no parameters are present, assume information mode
; - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if n_params() eq 0 then return,$
  'Result_array = OFF_AXIS(x0,nx,y0,ny[,sum_mode=sum_mode,version=version])'

; - - - - - - - - - - - - - - - - - - - - - - 
;  Define Off-axis Sensitivity Function
; - - - - - - - - - - - - - - - - - - - - - - 

; The following 3rd order polynomial was determined by fitting MSFC2 data
; which was reported in Calibration Note 18.  
; The data was obtained at Al K (8.34 A).

; The dependent variable is off-axis angle in arc-min.

; VERSION = 'V0.2 8-aug-91'
  coef = [1.0000001,-1.51829033e-19 ,-6.21715607e-04]	; Fit to MSFC2 data

; - - - - - - - - - - - - - - - - - - - - - - 
;  Define Pixel Size and center of CCD
; - - - - - - - - - - - - - - - - - - - - - - 

col_center = long(524.6-26+.5)		; From WSMR 1 results
row_center = long(637.2+.5)
Pixel = 2.45			; Pixel size in arc-min

; - - - - - - - - - - - - - - - - - - - - - - 
;  Make Sure SUM_MODE is defined and valid
; - - - - - - - - - - - - - - - - - - - - - - 

if n_elements(SUM_MODE) eq 0 then SUM_MODE = 0
if (SUM_MODE lt 0) or (SUM_MODE gt 2) then begin
    print,' **** ERROR in OFF_AXIS   ****'
    print,'      SUM_MODE = ',SUM_MODE
    print,'      The valid range is 0 - 2'
    print,'      Changing SUM_MODE to 0'
    SUM_MODE = 0
    ans = '' & read,'* Enter <cr> to continue',ans
endif

ss_mode = (2*Sum_mode) > 1		; convert 0,1,2 to 1,2,4
row_center = row_center / ss_mode
col_center = col_center / ss_mode
Pixel      = Pixel * ss_mode
; - - - - - - - - - - - - - - - - - - - - - - - - - - 
;  Make Sure X0, Y0, NX, and NY are defined and valid
; - - - - - - - - - - - - - - - - - - - - - - - - - - 

if (n_elements(X0) eq 0) or (n_elements(Y0) eq 0) or $
   (n_elements(NX) eq 0) or (n_elements(NY) eq 0) then begin
     print,' *****  ERROR in OFF_AXIS  *****'
     print,' Check the following: '
     help,x0,y0,nx,ny
     return,0.
endif

if (X0 lt 0) or (X0 gt 1023) or (Y0 lt 0) or (Y0 gt 1023) or $
   (NX le 0) or (X0+NX-1 gt 1023) or (NY le 0) or (Y0+NY-1 gt 1023) then begin
     print,' *****  ERROR in OFF_AXIS  *****'
     print,' Check the following: '
     help,x0,y0,nx,ny
     return,0.
endif

; - - - - - - - - - - - - - - - - - - - - - - - - - - 
;  Compute the Off-axis sensitivity response function
; - - - - - - - - - - - - - - - - - - - - - - - - - - 

xx = (X0+indgen(Nx)-col_center)^2
yy = (Y0+indgen(Ny)-row_center)^2
off = sqrt(xx#replicate(1,Nx)+replicate(1,Ny)#yy)

;off = fltarr(NX,NY)
;for i=0,ny-1 do off(0,i) = sqrt(xx+yy(i))	; One row at a time

zz = 60.			; arc-sec/arc-min

off = off * (pixel / zz)	; Distance from on-axis in arc-min
off(0:*) = poly(off,coef)	; Compute off-axis response

return,off
end
