function cal_straylight,POSITION_REF_POINT,PSF=PSF,MODEL=MODEL,$
	  SUN_IM=SUN_IM,U_PIXEL=U_PIXEL,CDS=CDS,SUMER=SUMER,$
	  S_PIXEL=S_PIXEL,POS_CENTDISK=POS_CENTDISK,$
	  MAX_LIMIT=MAX_LIMIT,MIN_LIMIT=MIN_LIMIT,$
	  CONTRI_IM=CONTRI_IM,D_PIXEL_REF=D_PIXEL_REF,$
	  SUN_MODEL=SUN_MODEL
;----------------------------------------------------------	  	  
;+
;NAME: 
;   	CAL_CDS_STRAYLIGHT
; Project:
;       SOHO - CDS
;
;CREATED BY:	David Christophe, IAS, march 6, 1999
;
;CALLING SEQUENCE:
;   		
; TOTAL_CONTRI_PSF=cal_straylight(POSITION_REF_POINT,/U_PIXEL,/CDS..)
;
;PURPOSE: 
;    
;
;
;INPUTS:
;
;
;   POSITION_REF_POINT : 
;
;	     containts the position of the point or points for which
;	     one wants to determine the instrumental component. The
;	     input parameter of is a array at two dimensions t(2, *),
;	     where t(0, *) corresponds to the coordinates in the
;	     East-West direction and y in the South-North direction
;	     (Cartesian coordinates). These values are expressed by
;	     default in arcsec and a heliocentric system. They can be
;	     given in the image pixel device (by including /U_PIXEL);
;	     the size of the images is 1024 X 1024 pixels.
;   			  
;  OPTIONAL:
;
;
;   PSF: the point spread function.
;   	
;   	- in the case or one treats the CDS data, this parameter 
;	contains the wave length to which corresponds these data;
;	a function called CAL\_CDS\_PSF\_COEFF\_WAVE, undertakes to
;	calculate the corresponding PSF function.
;
;   	- in the case or one treats the SUMERS data, this parameter is
;	not called. The function is already integrated in the function.
;	(for a wavelength between 1000 and 1500 \AA).
;
;   	- for other instruments, PSF contains parameters describing
;	the function of diffusion. That are the coefficients of the
;	polynomial (PSF=[a,b,c,..]) describing the function in a log-log
;	scale. That is:
;
;   	APSF(R)=a+bR+cR$^2$+dR$^3$+......
;	APSF=log(psf(r)) and R=log(r).
;   	of course this function must be normalized as the one of CDS.
;
;   	To select the CDS or SUMER instruments, one will add /CDS
;	parameter or /SUMER. By default, it is the third case that is
;	taken in account (others instruments).	
;
;   Brightness of the sun : two choices.
; 		    
;   MODEL : 
;
;	distribution of the solar radial brigthness. This brightness
;	is supposed uniform in first approximation (radial variation
;	only). The data are contained in a table with two column (2,*).
;	The first correspond (0,*) to the radial coordinate (in arcsec)
;	with the origin on the disk center for each point describing the
;	disk brightness about which the intensity (arbitrary units) are
;	contained in second column (1,*). This code make then a sun image
;	(1024,1024). For more rapidity, it's better to introduce
;   	directly a sun image with the following parameter.
;
;   SUM_IM  :
;	
;	brightness image of sun (deduce of the observations or of a model).
;   	The dimension of data table is (1024,1024).
; 
;   in the case of these two parameters are omitted, the brightness of
;   each pixel of the sun image is equal a 1.
;
;   S_PIXEL :
;
;     size of a pixel of the sun image in arcsec. By default is 2.92969
;     arcsec.
; 
;   POS_CENTDISK :
;
;       position of the center of the disk in the image contained in the
;	SUM_IM parameter in pixel (x,y). By default, this parameter is
;	equal to :[511,511].
;
;   MIN_LIMIT :
;
;       minimal (in arcsec) distance of reference point considered
;	for the calculation of the straylight contribution in arcsec.
;   	This value by default is 10 arsec. But below the definition of
;   	PSF is not certain.
;
;   MAX_LIMIT :
;
;	maximal distance of the reference point for the calculation
;   	of the straylight contribution in arcsec.
;
;
;OUPUTS:
;
;   TOTAL_CONTRI_PSF:
;
;	result of calcul fo straylight for each reference point.
;
;  OPTIONAL: 
;
;    Remark : if you put one of the following parameters, the parameter
;	      of output must be different of zero.
; ex:CTOTAL_CONTRI_PSF=cal_straylight(POSITION_REF_POINT,..,CONTRI_IM=A,..)
;   with the contains of A different of zero. It's because of logical
;   test.
;    
;   CONTRI_IM :
;
;	this parameter contains the detail of the straylight calculation.
;   	It's mean the contribution of each point of the image of sun 
;   	described by a matrix(1024,1024). If you select this parameter
;	it would be better to bound the number of the reference points,
;	because of space memory in IDL window.
;
;   SUN_MODEL :
;
;	in the case of you use MODEL parameter, you can get the
;	correspondant image with this parameter.
;
; MODIFICATION HISTORY:
;
;*********************************************************************
;**
;-
IF NOT KEYWORD_SET(R_SUN) THEN R_SUN=960.
IF NOT KEYWORD_SET(S_PIXEL) THEN S_PIXEL=2.92969
			;size of a pixel in arsec by default

IF NOT KEYWORD_SET(MIN_LIMIT) THEN MIN_LIMIT=10. 
;
;   	    minimal (in arcsec) distance of reference point considered
;   	    for the calculation of the straylight contribution in arcsec.
;   	    This value by default is 10 arsec. But below the definition of
;   	    PSF is not certain.	
			
;*********************************************************************
;determination of the coefficients discribing the PSF at the wavelength lambda.
;*
IF KEYWORD_SET(CDS) THEN BEGIN
print,'cds'
coeff_psf=cal_cds_psf_coeff_wave(PSF)
ENDIF ELSE BEGIN

 IF KEYWORD_SET(SUMER) THEN BEGIN
 print,'sumer'
  coeff_psf=[-1.24958,-2.96214,-1.90038,1.58348,-0.407651,0.0338351]
 ENDIF ELSE BEGIN
 coeff_psf=PSF
 print,'autre'
 ENDELSE
ENDELSE

;**
IF NOT KEYWORD_SET(POS_CENTDISK) THEN POS_DISK=[511,511] $
ELSE POS_DISK=POS_CENTDISK

;***Unity of POSITION_REF_POINT
;**
position_refpt_pixel=POSITION_REF_POINT
;Dimension of POSITION_REF_POINT
nb_pf=size(position_refpt_pixel)
IF nb_pf(0) EQ 1 THEN begin
TOTAL_CONTRI_PSF=fltarr(2)
max_cycle=1
ENDIF ELSE BEGIN
max_cycle=nb_pf(2)
TOTAL_CONTRI_PSF=fltarr(max_cycle)
ENDELSE

IF NOT KEYWORD_SET(U_PIXEL) THEN BEGIN
position_refpt_pixel(0,*)=(POSITION_REF_POINT(0,*)/S_PIXEL)+POS_DISK(0,0)
position_refpt_pixel(1,*)=(POSITION_REF_POINT(1,*)/S_PIXEL)+POS_DISK(1,0)
ENDIF
 
psf_contribution=fltarr(1024,1024)

;** 
D_PIXEL_MODEL_res=cal_distance_ref_point(POS_DISK,S_PIXEL)

;********************************************************************
;************			SUN'S IMAGE		 ************
;**

sun_model=fltarr(1024,1024)

IF KEYWORD_SET(MODEL) THEN BEGIN	

limit_model_sdisk=where(D_PIXEL_MODEL_res le max(MODEL(0,*)))
d_pixel_lim_model=D_PIXEL_MODEL_res(limit_model_sdisk)
BR_tamp=10^interpol(alog10(MODEL(1,*)),MODEL(0,*),d_pixel_lim_model)
sun_model(*,*)=0.
sun_model(limit_model_sdisk)=BR_tamp

;*************************************************************************
;***
ENDIF ELSE BEGIN	
 IF KEYWORD_SET(SUN_IM) THEN sun_model=SUN_IM $
 ELSE sun_model(*,*)=1

ENDELSE

;*********************************************************************
IF KEYWORD_SET(CONTRI_IM) THEN BEGIN
 IF nb_pf(0) eq 1 THEN contri_im=fltarr(1024,1024) $
 else contri_im=fltarr(1024,1024,max_cycle)
 
ENDIF 
 
;*********************************************************************
;*********************************************************************

FOR I=0,(max_cycle-1) DO BEGIN

;*********************************************************************

D_PIXEL_REF=cal_distance_ref_point(position_refpt_pixel(*,I),S_PIXEL)
print,I,position_refpt_pixel(*,i)
;*****
;limit of calculation


IF KEYWORD_SET(MAX_LIMIT) THEN BEGIN
ref_region=where((D_PIXEL_REF lt MIN_LIMIT) or (D_PIXEL_MODEL_res gt MAX_LIMIT))
PIXEL_LIMIT=where((D_PIXEL_REF ge MIN_LIMIT) and (D_PIXEL_MODEL_res le MAX_LIMIT))

ENDIF ELSE BEGIN
ref_region=where(D_PIXEL_REF lt MIN_LIMIT)
PIXEL_LIMIT=where(D_PIXEL_REF ge MIN_LIMIT)
ENDELSE

D_PIXEL_REF=D_PIXEL_REF(PIXEL_LIMIT)

;***
;calcul of straylight contribution 

log_distance_PIXEL_REF=alog10(D_PIXEL_REF)
psf_contribution(PIXEL_LIMIT)=10^poly(log_distance_PIXEL_REF,coeff_psf)

IF KEYWORD_SET(CONTRI_IM) THEN BEGIN
 conti=psf_contribution*sun_model*S_PIXEL^2
 conti(ref_region)=min(conti(PIXEL_LIMIT))*1.e-2
 r_y=position_refpt_pixel(0,i)
 r_x=position_refpt_pixel(1,i)
 c_x=findgen(16)+r_x-8
 c_y=findgen(16)+r_y-8
 conti(c_x,r_y-1:r_y+1)=min(conti)
 conti(r_x-1:r_x+1,c_y)=min(conti)
 CONTRI_IM(*,*,I)=conti
 

ENDIF
TOTAL_CONTRI_PSF(I)=total(psf_contribution*sun_model)*S_PIXEL^2
print,TOTAL_CONTRI_PSF(I),position_refpt_pixel(*,I)

ENDFOR

RETURN,TOTAL_CONTRI_PSF

END
