pro rh_rdimcube, fich, tab_image, $
		 hdr0, hdr1,	$
	         FREQ=ifreq, HDEB=ihdeb, HFIN=ihfin, POLAR= ipolar, $
                 SIZE= size, N_SOLAR_RADIUS= n_sol_rd, TB= tb, nfrq=nfrq, CLEAN=CLEAN
;+ ***********************************************************************
; NAME:
;	RH_RDIMCUBE.PRO 
; PURPOSE: 
;	-lecture et calcul d'un cube de donnees RH
;
; CATEGORY:
;	Traitement de fichiers NRH
; CALLING SEQUENCE
;	rh_rdimcube, fich, tab_image, hdr0, hdr1, $
;		     FREQ=ifreq, HDEB=ihdeb, HFIN=ihfin, POLAR= ipolar, $
;                    SIZE= size, N_SOLAR_RADIUS= n_sol_rd, TB= tb
;
; INPUTS:
;	Fich : nom du fichier a traiter
; KEYWORDS:
;	HDEB    : heure de debut  'HH:MM:SS:MMM'
;		  par defaut heure de debut du fichier
;	HFIN    : heure de fin    'HH:MM:SS:MMM'
;		  par defaut = HDEB ( une seule image calculee)
;	POLAR   : =0 sans polar ( STOKESI seul)
;		  =1 avec polar ( STOKESI + STOKESV)
;		  par defaut POLAR = 0
;       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 en temperature de brillance
;		  par defaut 1
; OUTPUTS:
;	TAB_IMAGE : tableau de structures contenant le cube d'images
;         si ipolar=0      time,numim,stokesi(128,128)
;         si ipolar=1	   time,numim,stokesi(128,128),stokesv(128,128)
;	HDR0	: header fits primaire
;	HDR1	: header d'extension #1
; COMMON BLOCKS:
;	RH	: communique avec les routines de lecture des donnees
;		  brutes RH
; EXAMPLE:
;IDL> rh_rdimcube,'/data/nancay/2q000807.01',tab_image, hdr0,hdr1,	$
;                  freq=1,hdeb='08:43:00',hfin='09:30:00'
;IDL>	help,tab_image
;IDL>      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]
; Utilisation :
; read_nrh,'2q040412.01',index,tab_image
; index2map, index,tab_image,map
; plot_map,map	
; MODIFICATION HISTORY:
;	Ecrit par A. Bouteille ( 24 Aout 2000)
;	PSH, 2008/09/15: added output keyword 'nfrq': this allows one to know how many freqencies there are... (necessissated only 1 line of code, besides adding output keyword in routine declaration...)
;	PSH, 2008/09/22: passing along keyword  CLEAN...
;-**************************************************************************
    @rh_common.inc
; Ouverture du fichier Nancay
    IF NOT (RH_OPEN(FICH)) then begin
	print ,'ERROR RH_OPEN : ',fich
	rh_close           ;il faut liberer le lun!
	return
    endif
    rh_close
    if n_params() lt 2 then BEGIN
        print,' ***ERROR*** RH_RDIMCUBE : insufficient number of parameters'
	print,' input parameters : 1-   FILENAME'
        print,' ouput parameters : '
        print,' 		1-IMAGE (required)'
	print,'			2-PRIMARY HEADER   (optional)'
	print,'			3-EXTENSION HEADER (optional)'
	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'
        return
    endif   
; informations sur la valeur des frequences
nfrq=entfi.nf	;PSH 2008/09/15
    for i=0,entfi.nf-1 do print,' freq = ',i,'  ',entfi.frq(i)/10,' MHz'
    nof=0
    if keyword_set(ifreq) then nof = ifreq
    if nof lt 0 or nof gt entfi.nf-1 then begin
         print,' frequency number out of range'
         print,' freq = 0'
         nof=0 & ifreq = 0
    endif
; extraction d'un cube d'harmoniques
    rh_rdcube, fich, tab_harm, $
               FREQ=ifreq, HDEB=ihdeb, HFIN=ihfin, POLAR= ipolar
; calcul du cube d'images
    rh_imcube, Tab_Harm, Tab_Image, $
               SIZE= size, N_SOLAR_RADIUS= n_sol_rd, TB=Tb, CLEAN=CLEAN
;
; construction des headers primaire et d'extension 
    dsmf = ' '
    nb_im_tot = n_elements(tab_image)
    npew = (size(tab_image.stokesi))(1)
    npns = npew
    imstd = tab_image(0).time
    imstf = tab_image(nb_im_tot-1).time    
    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
    tag_n=tag_names(tab_image)
    ipol=0
    if tag_n(n_elements(tag_n)-1) eq 'STOKESV' then ipol=1
    tb = 1
    numrec = nb_im_tot
    mmax = max(tab_image.stokesi)
    mmin = min(tab_image.stokesi)
    rh_get_header, nof, dsmf, ipol, npew, npns, imstd, imstf,	$
                larg, tb, numrec, mmax, mmin,			$
		hdr0, hdr1
    sxaddpar, hdr0, 'ORIGIN ','rh_rdimcube','  '
    return
    end
