PRO RH_IMCUBE, tab_harm, tab_image, SIZE= size,	NO_ROND = no_rond,	$
	       N_SOLAR_RADIUS= n_sol_rd, TB= tb, CLEAN=CLEAN
;+ ***********************************************************************
; NAME:
;	RH_IMCUBE
; PURPOSE:
;	Cette fonction calcule un cube d'images 2D
;	a partir d'un cube d'harmoniques 2D 
; CATEGORY:
;	Traitement de fichiers NRH
; CALLING SEQUENCE:
;	RH_IMCUBE, Tab_Harm, Tab_Image, SIZE= size, N_SOLAR_RADIUS= n_sol_rd
; INPUTS:  
; tab_harm structure, cube d'harmoniques
;                (structure remplie par RH_RDCUBE)
; OUTPUTS:
; tab_image structure, cube d'images
;                (structure definie ici)
; KEYWORDS:
; SIZE	: taille en X et en Y de l'image  (128, 256 ou 512)
;    par defaut 128 
; N_SOLAR_RADIUS : largeur du soleil en rayons solaires ( 4 ou 6)
;    par defaut 4
; TB	: tb=0 amplitude en sfu    
;	  tb=1 amplitude temperature de brillance
; NO_ROND : =1 image calculee en coord. interferometriques
;           =0 image calculee en coord heliographiques
;     par defaut image "ronde" donc en coord. heliographiques
; EXAMPLE:
;IDL>	rh_imcube,tab_harm,tab_image
;IDL>	help,tab_image
;        TAB_IMAGE       STRUCT    = -> IMAGNP Array[11]
;IDL>	help,tab_image,/struc
;       ** Structure IMAGNP, 4 tags, length=65548:
;	   TIME            LONG          31293050
;	   NSCAN           LONG                 0
;	   FREQ            FLOAT           236.600
;	   STOKESI         FLOAT     Array[128, 128]
;
; MODIFICATION HISTORY:
;	-Ecrit par: A. Bouteille (aout 2000)
;   04 juin 18    : ajout des antennes anti_alias : calcul des images
;                   en utilisant le tableau des correlations calcule
;                   par RH_TAB_CORREL
;	PSH, 2008/09/28: added keyword CLEAN
;-*******************************************************************
; la structure entfi a ete remplie par rh_rdcube
    @rh_common.inc
;
    if n_params() lt 2 then BEGIN
        print,' ***ERROR*** RH_RONDCUBE : Insufficient number of parameters.'
	print,'    input parameter : - cube d''harm.(required)'
        print,'    ouput parameter : - cube d''images (required)'
	print,'    KEYWORDS (optional) : '
        print,'      SIZE   : taille en X et en Y de l''image 128, 256 ou 512'
        print,'               par defaut 128 '
        print,'      N_SOLAR_RADIUS : largeur de l''image en rayons solaires '
        print,'                       ( 4 6 8 or 10) par defaut 4 '
        print,'      TB     : tb=0 amplitude en sfu'    
        print,'               tb=1 amplitude temperature de brillance'
        print,'               par defaut tb=1'
        print,'      NO_ROND: =1 image calculee en coord. interferometriques'
        print,'        (Dans ce cas les 3 mots cles precedents sont inutiles)
        print,'               =0 image calculee en coord heliographiques'
        print,'               par defaut =0'
        return
    endif   
; correction de phase a faire jusqu'au 5/11/1998 non inclus
    julcor=julday(11,5,1998)
    jul=julday(entfi.dat(1),entfi.dat(0),rh_date100(entfi.dat(2),/full))
    icorrec_phase=0
    if(jul-julcor lt 0) then begin
        icorrec_phase=1
        print,' *****correction des phases*****',entfi.nval/4
    endif

; si le keyword NO_ROND est absent, images en coord. heliogr. soleil rond
; /NO_ROND est equivalent a NO_ROND=1 donc rond = 0
    rond = 1
    if n_elements(NO_ROND) ne 0 then rond = (NO_ROND+1) mod 2
; si rond = 1 il faut initialiser malax
    if rond eq 1 then begin
; init de malax
        hmer= fltarr(3)
        hmer(0:2)= float(entFi.mer(0:2))
        hmer(2)=hmer(2)+float(entFi.mer(3))/100.
        dec= fltarr(3)
        dec(0:2)= float(entFi.dec(0:2))
        dec(2)=dec(2)+float(entFi.dec(3))/100.
        sref=string(entFi.ref)
        init_malax, entFi.dat, hmer, dec, sref
    endif
;
    npew=128
    if keyword_set(size) then begin
	if size ne 128 and size ne 256 and size ne 512 then begin
	    print,' SIZE must be egal to 128,256 or 512'
	    print,' Calculations are made with SIZE= 128'
	    size=128l 
	endif 
	npew=size
    endif
    larg=4
    if keyword_set(n_sol_rd) then begin
	if n_sol_rd ne 4 and n_sol_rd ne 6 and n_sol_rd ne 8 $
           and n_sol_rd ne 10 then begin
	    print,' N_SOLAR_RADIUS must be egal to 4 6 8 or 10 '
	    print,' Calculations are made with N_SOLAR_RADIUS = 4'
	    n_sol_rd=4
	endif
	larg=n_sol_rd
    endif
    tb_set = 1
    if keyword_set(tb) then tb_set = tb
; 
    tag_n = tag_names(tab_harm)
    ipol=0
    if tag_n(n_elements(tag_n)-1) eq 'STOKESV' then ipol=1
    nbscan = n_elements(tab_harm)
; Declaration de la structure de sortie
    if ipol eq 0 then begin
	case npew of 
        128 :        imag= {imagnp128, time:0L, nscan:0L, freq:0.0, $
                             stokesi:fltarr(npew,npew)}
        256 :        imag= {imagnp256, time:0L, nscan:0L, freq:0.0, $
                             stokesi:fltarr(npew,npew)}
        512 :        imag= {imagnp512, time:0L, nscan:0L, freq:0.0, $
                             stokesi:fltarr(npew,npew)}
        endcase
    endif
    if ipol ne 0 then begin
	case npew of 
        128 :	imag= {imagp128, time:0L, nscan:0L, freq:0.0, $
                             stokesi:fltarr(npew,npew), $
                             stokesv:fltarr(npew,npew)}
        256 :	imag= {imagp256, time:0L, nscan:0L, freq:0.0, $
                             stokesi:fltarr(npew,npew), $
                             stokesv:fltarr(npew,npew)}
        512 :	imag= {imagp512, time:0L, nscan:0L, freq:0.0, $
                             stokesi:fltarr(npew,npew), $
                             stokesv:fltarr(npew,npew)}
        endcase
    endif
    tab_image = replicate(imag, nbscan)
; boucle de calcul des images
    n_scan = 0L	
    freq = tab_harm(0).freq
    frq_rh = float (entfi.frq) / 10
    nf = (where(frq_rh eq freq))(0)
    pt = fltarr(4,576)
    tab_image.freq=freq
    tab_image.time=tab_harm.time
    tab_image.nscan=tab_harm.nscan
    nb_correl = n_elements(tab_harm(0).stokesi)/2
    rh_tab_correl, tab_harm.date, nb_correl, correl
    for i=0,nbscan-1 do begin
        time=tab_harm(i).time
        sec= time/1000
        heur=sec/3600
        minu=sec/60-heur*60
        seco=sec-(heur*3600+minu*60)
        ms=time-3600000*heur-60000*minu-seco*1000
        hc=intarr(4)
        hc(0)=heur & hc(1)=minu & hc(2)=seco & hc(3)=ms/10
;
        if icorrec_phase eq 1 then begin
	    pt(0:1,*)=tab_harm(i).stokesi
	    if(ipol eq 1) then pt(2:3,*)=tab_harm(i).stokesv
	    rh_corec_phase, nf, pt, hc
	    tab_harm(i).stokesi = pt(0:1,*)
	    if(ipol eq 1) then tab_harm(i).stokesv = pt(2:3,*) 
        endif
        if rond eq 1 then begin ; soleil en coord. heliographiques

		IF KEYWORD_SET(CLEAN) THEN BEGIN
           tab_image(i).stokesi = $
            rh_malcrondclean_im_2d(CLEAN,'non polar',complex(tab_harm(i).stokesi(0,*),    $
                                      tab_harm(i).stokesi(1,*)),   $
                              hc, freq,                            $
                              npew, larg, correl, t_b=tb_set+1)

            if ( ipol eq 1 ) THEN begin
            IF MAX(abs(tab_harm(i).stokesv)) NE 0 THEN tab_image(i).stokesv = $
                rh_malcrondclean_im_2d(CLEAN,'polar',complex(tab_harm(i).stokesv(0,*),    $
                                          tab_harm(i).stokesv(1,*)),   $
                                  hc, freq,                            $
                                  npew, larg, correl, t_b=tb_set+1)
            endif			
		ENDIF ELSE BEGIN
            tab_image(i).stokesi = $
            rh_malcrond_im_2d(complex(tab_harm(i).stokesi(0,*),    $
                                      tab_harm(i).stokesi(1,*)),   $
                              hc, freq,                            $  
                              npew, larg, correl, t_b=tb_set+1)
            if ( ipol eq 1 ) THEN begin
                tab_image(i).stokesv = $
                rh_malcrond_im_2d(complex(tab_harm(i).stokesv(0,*),    $
                                          tab_harm(i).stokesv(1,*)),   $
                                  hc, freq,                            $  
                                  npew, larg, correl, t_b=tb_set+1)
            endif
		ENDELSE
        end else begin    ; soleil en coord. interferometriques
            harm_n = complex(tab_harm(i).stokesi(0,*),    $
                             tab_harm(i).stokesi(1,*))  
            RH_BR_MALC_IM_2D,  harm_N, correl, npew, image
            tab_image(i).stokesi = image
            if ( ipol eq 1 ) THEN begin
                harm_n = complex(tab_harm(i).stokesv(0,*),    $
                                 tab_harm(i).stokesv(1,*))  
                RH_BR_MALC_IM_2D,  harm_N, correl, npew, image
                tab_image(i).stokesv = image
            endif
        endelse
    endfor
    end

