PRO RH_GET_HEADER, $
;     entree
	num_freq,   dsmf,   ipolar,     npew,    npns,	$
	imstd,      imstf,				$
	champ_Rs,   tb,     nbre_enrgt,   		$
	mmax,       mmin,				$
;     sortie
	hdr0,     hdr1,					$
;     entree optionnelle                             	$
	str_dp,   temps_integ = temps_integ

;+ ***********************************************************************
; NAME:
;	RH_GET_HEADER
; PURPOSE:
;	Cette procedure remplit les header fits primaire et d'extension.
; CATEGORY:
;	Traitement de fichiers NRH
; CALLING SEQUENCE:
;   RH_GET_HEADER, $
;     entree
;	num_freq,   dsmf,   ipolar,   npew,    npns,	$
;	imstd,      imstf,				$
;	champ_Rs,   tb,     nbre_enrgt,   		$
;	mmax,       mmin,				$
;     sortie
;	hdr0,     hdr1,					$
;     entree optionnelle                             	$
;	str_dp,   temps_integ = temps_integ
;
; INPUTS :
;	num_freq	numero de la frequence ( de 0 a entfi.nf -1 )
;	dsmf		tableau nom du fichier a ecrire
;	ipolar		0 sans polar,=1 avec polar
;	npew, npns	nb de points de l'image 2D en EW et NS
;	imstd, imstf	heure de debut et de fin (msec)
;	champ_Rs	largeur du champ de l'image (Rs)
;	tb		1 unites des images temperature de brillance
;	nbre_enrgt	nbre d'enregistrements, ie d'images dans le fichier 
;			    fits de sortie.
;	mmax,mmin	max et min des fichiers.
; OUTPUTS : 
;	hdr0		tableau du header primaire
;	hdr1		tableau du header d'extension
; KEYWORDS :
;       temps_integ    reserve au traitement des images composites RH + GMRT
;                             pour les parametres "exptime", "timestep"
;                             et tdelt1. Ecrase entfi.itg .
; OPTIONNEL  INPUT :
;       str_dp    : structure des parametres de calcul de l'image
;                      figurant dans le header fits ( parametre
;                      pouvant etre absent dans les versions anciennes.)
;                                
;                     
; COMMON BLOCKS :
;	RH	: communique avec les routines de lecture des donnees
;		  brutes RH
;	
; MODIFICATION HISTORY :
; 01 mar   	creation par A. Bouteille a partir de D2D_FITS.PRO . Ne traite
;		  qu'une frequence a la fois. Renvoie les header en sortie
; 04 jun 30	ajout du mot-cle  temps_integ  pour pouvoir produire des
;		  headers de fichiers fits d'images composite RH + GMRT.
;		  Dans ce cas les donnees RH a haute cadence sont integrees
;		  sur le temps d'integration du GMRT (~2 sec), et la cadence
;		  de sortie (variables "exptime" et "timestep") est evidemment
;		  celle du GMRT et ne peut etre deduite des headers du fichier
;		  de donnees RH.
;		amelioration presentation.

;-**************************************************************************
    @rh_common.inc
	; dans : 
	;    /usr/local/ssw/radio/nrh/idl/dp_soft_rh/routine_rh/rh_common.inc
;
; Imposition du type des arguments d'entree (certains pouvant etre modifie par
	; le choix de l'utilisateur)
    champ_Rs = float(champ_Rs)
    ipolar   = fix (ipolar)
    npew     = fix (npew)
    npns     = fix (npns)

; Definition de la cadence se sortie des images fits
    timestep = entfi.itg 				; RH seul.
    if (n_elements(temps_integ) ne 0)   then  $
	timestep = temps_integ				; RH + GMRT.

; Creation du header modele
    RH_CR_HEADER_PR,  hdr0					; HDU primaire
    SXADDPAR,         hdr0,  'NRH_DATA',  '2DI ',' 2 dimensions images  '
    SXADDPAR,         hdr0,  'ORIGIN ',   'dpatchfits','  '		
    RH_CR_HEADER_BT,  hdr1		; HDU de type extension BINARY TABLE.

; Date de creation du fichier (conversion de systime (0) en une chaine de 
	; caracteres, par ex :   Fri Sep 12 12:00:17 1997)
    date  = bin_date ( systime())
    cdate = RH_HDR_TIME([date(2), date(1), date(0)]) 
    SXADDPAR,  hdr0,'DATE    '  , cdate
    chdat = RH_HDR_TIME (entfi.dat)
    SXADDPAR,  hdr0, 'DATE-OBS', chdat
    SXADDPAR,  hdr1, 'DATE-OBS', chdat

;   print,'entfi.mer',entfi.mer

    if (entfi.mer(3) eq 100) then  entfi.mer(3) = 99
    chmer = string(entfi.mer, format='(i2.2, ":" ,i2.2, ":", i2.2, ".", i2.2)')
    chdecl=string(entfi.dec,format='(i3.2,"D",i3.2,"M",i3.2,".",i3.2)')
	;  Rem : la declinaison peut etre <0 ( signe - devant chaque element 
	;	  du tableau)
;   print, ' declinaison', entfi.dec, chdecl
;   stop

    SXADDPAR,  hdr1, 'HMER',  chmer,       'heure du meridien'
    SXADDPAR,  hdr1, 'DECL',  chdecl,      'declinaison en degres'
    SXADDPAR,  hdr1, 'BUNIT', 'SFU/PIXEL', ' brightness in sfu/pixel        '
    if (tb  eq  1)  then  $
       SXADDPAR,  hdr1, 'BUNIT', 'TB', 'brightness temperature, in K degrees'
	; Rem : 2 mots-cle pour definir l'anti-aliasing :
   	;	. UV_COV_A : presence des antennes "anti-aliasing"
	;	. UV_COV_C : interpolation du pave central dans le plan u,v

    SXADDPAR,  hdr1, 'UV_COV_A', 'NO    ', $
		     'ANTENNAS for low 50m odd multiple baselines'
    if (n_elements(str_dp)  ne  0)  then begin
        CASE  str_dp.i_pave_plein  OF
	    '0' : begin
		ch_pave  = 'NO      '
		com_pave = 'INTERPOLATION for 50m odd multiple baseline'
	    end
	    '1' : begin
		ch_pave  = 'YES    '
		com_pave = 'INTERPOLATION for 50m odd multiple baseline'
             end
        ENDCASE
      
        SXADDPAR,  hdr1, 'UV_COV_C' ,ch_pave, com_pave

	    ; Rem : il y a 2 mots_cle pour definir sur quels domaines s'appli-
	    ;	      quent les 2 etapes du clean :
	    ;	      .  CLN_SRCH    search  dirty components
	    ; 	      .  CLN_RSTR    restore clean components
        clean = str_dp.mode_clean
        CASE clean OF
         '0' : begin
               ch_clean_srch = 'NONE'
               com_clean_srch = 'clean : search dirty components'
               ch_clean_rstr = 'NONE'
               com_clean_rstr = 'clean : restore clean components'
             end
         '1' : begin
               ch_clean_srch = 'FULL IMAGE'
               com_clean_srch = 'clean : search dirty components'
               ch_clean_rstr = 'FULL IMAGE'
               com_clean_rstr = 'clean : restore clean components'
               end
         '2' : begin
               ch_clean_srch = 'FULL IMAGE'
               com_clean_srch = 'clean : search dirty components'
               ch_clean_rstr = 'OUTSIDE DOMAIN'
               com_clean_rstr = 'clean : restore clean components'
               end
         '3' : begin
               ch_clean_srch = 'INSIDE DOMAIN'
               com_clean_srch = 'clean : search dirty components'
               ch_clean_rstr = 'NONE'
               com_clean_rstr = 'clean : restore clean components'
             end
         '4' : begin
               ch_clean_srch = 'INSIDE DOMAIN'
               com_clean_srch = 'clean : search dirty components'
               ch_clean_rstr = 'INSIDE DOMAIN'
               com_clean_rstr = 'clean : restore clean components'
             end
         ENDCASE

        SXADDPAR, hdr1, 'CLN_SRCH', ch_clean_srch, com_clean_srch
        SXADDPAR, hdr1, 'CLN_RSTR', ch_clean_rstr, com_clean_rstr
    endif

; Mise a jour de la partie du header dependant de npew et npns
	; Attention : si -32768  <=  nptot  <=  32767  pas de pb
	;           sinon passer en long integer!
    nptot = 4l + 4l + (long(npew) * long(npns) *4l) * (ipolar + 1)
    SXADDPAR, hdr1, 'NAXIS1  ', nptot, '4 + 4 + ndim*ndim*4 + ndim*ndim*4 ' 
    chform = strcompress(string(long(npew) * long(npns)) + 'E', /remove_all)
    SXADDPAR, hdr1, 'TFORM3  ', chform, ' ndim*ndim ARRAY, type real*4'
    SXADDPAR, hdr1, 'TFORM4  ' ,chform, ' ndim*ndim ARRAY, type real*4'
    chdim = strcompress('(' + string(npew) + ',' + string(npns) + ')', $
								/remove_all)
    SXADDPAR ,hdr1, 'TDIM3   ', chdim, 'two dimensional array'
    SXADDPAR, hdr1, 'TDIM4   ', chdim,'two dimensional array'
    npmil = npew / 2l
    SXADDPAR, hdr1, 'CRPIX1  ', npmil, 'SUN CENTER X, in pixels'      
    SXADDPAR, hdr1, 'CRPIX2  ', npmil, 'SUN CENTER Y, in pixels'
	; Si l'image occupe champ_Rs rayons solaires : nb de pixel par rayon 
	;	solaire.
    nprad = npew / champ_Rs
    SXADDPAR, hdr1, 'SOLAR_R ', nprad, 'SOLAR RADIUS, in pixels'

; Mise a jour de la partie du header dependant de ipolar
    nb_col = 4
    if( ipolar eq 0) then begin
	nb_col = 3
	SXADDPAR, hdr0 ,'PHYSPARA', 'STOKESI','     '
	SXDELPAR, hdr1, ['TFIELDS', 'TFORM4', 'TTYPE4' ,'TDIM4']
	SXADDPAR, hdr1, 'TFIELDS', 3, ' 3 COLUMNS BINARY TABLE', $
							after = 'GCOUNT'
;   stop
    endif 

; Mise a jour de la partie du header dependant de la frequence
    SXADDPAR, hdr0, 'FILENAME', dsmf, '  '
    c =299.792
    wl=c/(entfi.frq(num_freq)/10.) 
;   chfreq = string(format='(f5.1, "Mhz")', entfi.frq(num_freq) / 10.)
    SXADDPAR, hdr1, 'FREQ    ', entfi.frq(num_freq) / 10., '  '
    SXADDPAR, hdr0, 'WAVELNTH', wl, '    '
    numr = nbre_enrgt
    SXADDPAR, hdr1, 'NAXIS2  ', numr, 'number of rows '
	; heures ?
    chtime = RH_HDR_TIME(entfi.dat, imstd)
    SXADDPAR, hdr0, 'DATE_OBS', chtime,'   '
    ctime = msh(imstd)
    SXADDPAR, hdr1, 'TIM_STR', ctime, '   '
    chtime = rh_hdr_time(entfi.dat, imstf)
    SXADDPAR, hdr0, 'DATE_END', chtime, '   '
    ctime = msh(imstf)
    SXADDPAR, hdr1, 'TIM_END',  ctime, '   '
    SXADDPAR, hdr1, 'TRVAL1  ', imstd, '   '
; deltat
;   print, 'timestep', timestep
    if entfi.comp eq 0 then begin
	SXADDPAR, hdr1, 'TDELT1  ', timestep, '   '
    endif else begin
	SXADDPAR, hdr1, 'TDELT1  ', -1, '   '
    endelse
    SXADDPAR, hdr1, 'EXPTIME', timestep / 1000., 'SAMPLING PERIOD, in seconds'
    SXADDPAR, hdr1, 'TIMESTEP', timestep/1000., $
		'INTEGRATION TIME, in seconds'

; Valeur max et valeur min
    SXADDPAR, hdr1, 'DATAMIN ' ,mmin, '   '
    SXADDPAR, hdr1, 'DATAMAX ', mmax, '   '
; Ajout des arguments d'appel de RH_DPATCHFITS_NRH en commentaire en fin de
	;   header
	; Chaque parametre des str_dp est inscrit dans le header.
    if (n_elements(str_dp) ne 0) then begin
	ntg  = n_tags(str_dp)
	tagn = tag_names(str_dp)
        i_comm = 0
	for i = 0, ntg - 1  do begin 
	    ndim = n_elements(str_dp.(i))
            typ_tag = size(str_dp.(i),/type)
; quand le parametre est un tableau, on ecrit 8 elements par ligne
            if ndim eq 1 then begin
                comment = string(str_dp.(i))
	        SXADDPAR,  hdr1, strcompress(string('COMM' + string(i_comm)), $
		/remove_all), tagn(i), strcompress(comment)
                i_comm = i_comm + 1
            end else begin
; on decoupe par ligne de 8 elements
                nb_l = 8
                for j = 0, ndim-1, nb_l do begin
	            comment = ''
	            for k = j, (j+ nb_l-1) < (ndim - 1) do begin
; typ_tag =2 ou 3 le parametre est de type entier 16 ou 32 bits
                        if typ_tag eq 2 or typ_tag eq 3 then $
                        comment = comment + string(str_dp.(i)(k), format='(i5)')
; typ_tag =4 le parametre est de type flottant
                        if typ_tag eq 4 then $
                        comment = comment + string(str_dp.(i)(k),format='(f5.2)')
                    endfor
	            SXADDPAR,  hdr1, strcompress(string('COMM' + string(i_comm)), $
		    /remove_all), tagn(i), comment
                        i_comm = i_comm + 1
                endfor
            endelse
        endfor     ; ndim non 1
    endif
    end		; FIN DE RH_GET_HEADER .





