pro rh_rdint, fich, hd, hf, nof, ipol, tab_harm
;+ ***********************************************************************
; NAME:
;	RH_RDINT.PRO 
; PURPOSE: 
;	-sous programme de RH_RDCUBE
;	-lecture d'un cube de donnees RH non comprimees
;   routines utilisees : RH_OPEN(..,/SEL) et RH_READSEL
;   routines utilisees pour la recherche d'heure RH_HDEB et RH_HFIN
; CATEGORY:
;	Traitement de fichiers NRH
; CALLING SEQUENCE
;	rh_rdint, fich, hd, hf, nof, ipol, tab_harm
;
; INPUTS:
;	Fich  : nom du fichier a traiter
;	hd    : heure de debut  (h,m,s)
;	hf    : heure de fin    (h,m,s)
;	Ipol  : =0 sans polar 
;		  =1 avec polar
; OUTPUTS:
;	tab_harm : tableau de structures contenant le cube de donnees lues
;	   time,numim,stokesi(2,nb_harm)			si ipol=0
;          time,numim,stokesi(2,nb_harm),stokesv(2,nb_harm)	si ipol=1
; COMMON BLOCKS:
;	RH	: communique avec les routines de lecture des donnees
;		  brutes RH
; MODIFICATION HISTORY:
;	Ecrit par A. Bouteille ( 24 Aout 2000)
;       Ajout antennes Anti Alias nb_harm : 576--> 648 harmoniques
;-**************************************************************************
    @rh_common.inc
 
; Ouverture du fichier Nancay avec selection d'une frequence

    IF NOT (RH_OPEN(FICH,/SEL)) then begin
       rh_close
       return
    endif

    nb_harm = entfi.nval/4
    ind = where (hd ne hf, count)	    
; k0 Heure de debut
    rh_hdeb, hd, k0, h
    hd = h
;    print, format='("Debut  ", 3(i2.2,":"),i2.2)', hd
    if count ne 0 then begin
; k1 = heure de fin
        rh_hfin, hf, k1, h
        hf = h
    end else begin
	k1 = k0
        hf=hd
    endelse 
;
; Declaration de la structure de sortie
    if ipol eq 0 then harm= { date:intarr(3), time:0L, nscan:0L, freq:0.0, $
                             stokesi:fltarr(2,nb_harm)} $
    else harm= { date:intarr(3), time:0L, nscan:0L, freq:0.0, $
                             stokesi:fltarr(2,nb_harm), $
                             stokesv:fltarr(2,nb_harm)}
    ;endif
    tab_harm = replicate(harm, k1-k0+1)

;    print,'itg',entfi.itg

; Boucle sur les scans.
    temps = [3600000L, 60000L, 1000L, 10L]
    npol0=0 & npol1=1
    klu = k0
    n_scan = 0L			; initialisation du numero de scan traite.
    FOR klu= k0, k1 DO BEGIN
        rh_readsel, klu, nof, npol0
	ihms = transpose(temps)#bfi(nof).h		; heure en msec.
	pt = reform (lufi.pt, 2 , nb_harm)
	    ; pt	fltarr(2, nb_harm) 
	    ;		    1er indice: cos ou sin, 
	    ;		    2eme indice: balaie les nb_harm harm.
        tab_harm(n_scan).date = entfi.dat
	tab_harm(n_scan).time = ihms
	tab_harm(n_scan).nscan = n_scan
	tab_harm(n_scan).freq = float (entfi.frq(nof)) / 10
        tab_harm(n_scan).stokesi = pt
        if ipol eq 1 then begin
            rh_readsel, klu, nof, npol1
	    pt = reform (lufi.pt, 2 , nb_harm)
	    tab_harm(n_scan).stokesv = pt
        endif
	n_scan = n_scan+1L
    ENDFOR
    rh_close
    return
end

pro rh_rdcmp, fich, hd, hf, nof, ipol, tab_harm
;+ ***********************************************************************
; NAME:
;	RH_RDCMP.PRO 
; PURPOSE:
;	-sous programme de RH_RDCUBE 
;	-lecture d'un cube de donnees RH comprimees
; CATEGORY:
;	Traitement de fichiers NRH
; CALLING SEQUENCE
;	rh_rdcmp,fich, hd, hf, nof, ipol, tab_harm
;   routines utilisees pour la lecture: RH_OPEN(..,/MONO) et RH_READCH
;   routine utilisee pour la recherche d'heure RH_LECDEB
; INPUTS:
;	Fich : nom du fichier a traiter
;	hd    : heure de debut  (h,m,s)
;	hf    : heure de fin    (h,m,s)
;	Ipol  :   =0 sans polar 
;		  =1 avec polar
; OUTPUTS:
;	tab_harm : tableau de structures contenant le cube de donnees lues
;		   time,numim,stokesi(2,nb_harm)
; avec ipol=1	   time,numim,stokesi(2,nb_harm),stokesv(2,nb_harm)
; COMMON BLOCKS:
;	RH	: communique avec les routines de lecture des donnees
;		  brutes RH
;	
; MODIFICATION HISTORY:
;	Ecrit par A. Bouteille ( 24 Aout 2000)
;-**************************************************************************
    @rh_common.inc 
; Ouverture du fichier Nancay en /mono 
    IF NOT (RH_OPEN(FICH,/mono)) then begin
	print ,'pb rh_open',fich
        rh_close
	return
    endif
    nb_harm = entfi.nval/4
    temps = [3600000L, 60000L, 1000L, 10L]
    msd   = (transpose(temps) # long(hd)) (0)	; h debut en msec
    msf   = (transpose(temps) # long(hf)) (0)	; - fin   -------
; Calcul des indices des records de debut et de fin
    nbim = -1
    if (msd eq msf)  then nbim = 1   
    RH_LECDEB, msd, msf, kdeb, kfin
    if (nbim eq 1)  then kdeb = kfin
; Si klumax n'est pas une frequence selectionnee, on demande a lire une 
	; frequence au dela de klumax et on tombe sur EOF.
    sel_freq=['N','N','N','N','N','N','N','N','N','N']
    sel_freq(nof)='Y'
    isel = where (sel_freq eq 'Y')
    nsel = n_elements (isel)
    if ( kfin eq entfi.klumax)  then $
	kfin = entfi.klumax -entfi.nf + isel(nsel-1) + 1
    print, '   Numeros de enregistrements:   debut        fin      fin fichier'
    print, '                         ', kdeb, kfin, entfi.klumax

    klu = kdeb
    rh_readch, klu, sel_freq
    print, '   heure debut:', bfi.h     

; Declaration de la structure de sortie
    npew = 128 
    RH_DP_INFO, fich, ch, hd, hf, ipol, npew,  $
        dsmf, numcnt, taille_mega

    harmnp= { date:intarr(3), time:0L, nscan:0L, freq:0.0, $
                     stokesi:fltarr(2,nb_harm)}

    harmp= { date:intarr(3), time:0L, nscan:0L, freq:0.0, $
                   stokesi:fltarr(2,nb_harm), $
                   stokesv:fltarr(2,nb_harm)}
    tab_harm = replicate(harmnp, numcnt(nof))

;    print,'itg',entfi.itg

; Boucle sur les scans.
    klu = kdeb
    n_scan = 0L			; initialisation du numero de scan traite.
    numim = 0L
    while (klu le kfin)  do begin
;    while n_scan lt numcnt(nof) do begin	; 
        RH_READCH, klu, sel_freq
        if klu gt kfin then goto, suite
;	print,klu
        nof=bfi.nof
	freq = float (entfi.frq(nof)) / 10
	nsa  = long (bfi.c)			; nbre de scans comprimes. 
	ihms = transpose(temps)#bfi.h		; heure en msec.
	pt = lufi.pt
	pt = reform (pt, 2 , 2 , entfi.nval / 4)
	    ; pt	fltarr(4, nb_harm) puis fltarr(2, 2, nb_harm). Table des harm.
	    ;		    1er indice: cos ou sin, 
	    ;		    2eme ind: non polar ou polar, 
	    ;		    3eme indice: balaie les nb_harm harm.
	tab_harm(n_scan).time = ihms
	tab_harm(n_scan).nscan = numim
	tab_harm(n_scan).freq = float (entfi.frq(nof)) / 10
        tab_harm(n_scan).stokesi = pt(*,0,*)
        if ipol eq 1 then tab_harm(n_scan).stokesv = pt(*,1,*)
	n_scan = n_scan+1
	numim = numim + nsa +1L
        klu=klu+1l
    endwhile
suite:
    rh_close
    return
end

pro rh_rdcube, fich, tab_harm, $
               freq=ifreq, hdeb=ihdeb, hfin=ihfin, polar= ipolar
;+ ***********************************************************************
; NAME:
;	RH_RDCUBE.PRO 
; PURPOSE: 
;	-lecture d'un cube de donnees RH
;  Pour les donnees non comprimees ( fichiers 2d ou 2q, 2i ) appel a RH_RDINT
;  Pour les donnees haute resolution comprimees ( fichiers 2c) appel a RH_RDCMP
; CATEGORY:
;	Traitement de fichiers NRH
; CALLING SEQUENCE
;	rh_rdcube, fich, tabharm, $
;                  freq=ifreq, hdeb=ihdeb, hfin=ihfin, polar= ipolar
;
; 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
; OUTPUTS:
;	tab_harm : tableau de structures contenant le cube de donnees lues
;		   time,numim,stokesi(2,nb_harm)
; avec ipolar=1	   time,numim,stokesi(2,nb_harm),stokesv(2,nb_harm)
; COMMON BLOCKS:
;	RH	: communique avec les routines de lecture des donnees
;		  brutes RH
; EXAMPLE:
;IDL>	rh_rdcube,'/juke/128sec/2Q2000_3/2q000807.01',tabharm, $
;                  freq=1,hdeb='8:43',hfin='9:0'
;IDL>	help,tab_harm
;	TAB_HARM        STRUCT    = -> HARMNP Array[11]
;IDL>	help,tab_harm,/struct
;** Structure HARMNP, 4 tags, length=5204:
;   DATE            INTARR(3)
;   TIME            LONG          31293050
;   NSCAN           LONG                 0
;   FREQ            FLOAT           236.600
;   STOKESI         FLOAT     Array[2, nb_harm]
;	
; MODIFICATION HISTORY:
;	Ecrit par A. Bouteille ( 24 Aout 2000)
;-**************************************************************************
    @rh_common.inc
    if n_params() ne 2 then begin
        print,' PB WITH PARAMETERS : '
        print,'    rh_rdcube,''/juke/128sec/2Q2000_3/2q000807.01'',str_harm,'
        print,'    freq=1,hdeb=''8:43'',hfin=''9:0'''
        return
    endif
; Ouverture du fichier Nancay
    IF NOT (RH_OPEN(FICH)) then begin
	print ,'pb rh_open',fich
        rh_close
	return
    endif
    rh_close
    comp=entfi.comp
; par defaut on traite la frequence 0
    nof=0
    if keyword_set(ifreq) then nof = ifreq
    if nof lt 0 or nof gt entfi.nf-1 then begin
         nof=0 & ifreq = 0
    endif
; par defaut on traite STOKESI seule
    ipol=0
    if  keyword_set(ipolar) then ipol=ipolar

; si les mot-cles hdeb et hfin absents, on ne traite que la 1ere image de la 
;  frequence demandee 
    hd=entfi.hdeb & hf=hd
    if keyword_set(ihdeb) then begin
        if (size(ihdeb))(0) ne 0 then begin ;ihdeb = '9:48' string
            print,' Erreur format HDEB string : HDEB=''9:48'''
            return
        endif
	hdms = hms(ihdeb,tab=hd)
;       hd=([0L,0L,0L,0L])& hd(0:n_elements(ihdeb)-1)=ihdeb
        if total(hd) eq 0 then hd=entfi.hdeb
        if keyword_set(ihfin) then begin
                if (size(ihfin))(0) ne 0 then begin ; HFIN = '9:48' string
                print,' Erreur format HFIN string : HFIN=''9:48'''
                return
            endif
	    hfms = hms(ihfin,tab=hf)
;	    hf=([24L,0L,0L,0L])& hf(0:n_elements(ihfin)-1)=ihfin
        end else begin
; si le mot cle ihfin est absent, on ne traite qu'une seule image
	    hf = hd
	endelse
    end else begin	; not keyword_set (ihdeb)
	if keyword_set(ihfin) then begin
	    hfms = hms(ihfin,tab=hf)
;	    hf=([24L,0L,0L,0L])& hf(0:n_elements(ihfin)-1)=ihfin
        endif
    endelse
;   if comp eq 1 then print,' compressed data'
    if comp eq 0 then rh_rdint, fich, hd, hf, nof, ipol , tab_harm $
    else  rh_rdcmp, fich, hd, hf, nof, ipol, tab_harm
    return
    end
