
;+ ***********************************************************************
; NAME:
;	SBTRBCKGRD
;
; PURPOSE:
; PROCEDURE DE SOUSTRACTION D'UN FOND A UN TABLEAU CONTENANT DES IMAGES
; EN FONCTION DU TEMPS PROVENANT DE LA FONCTION LEC1D (IMAGE 1D DU RH)
; (TABLEAU DE 2D CONTENANT UN SEUL TYPE DE DONNEES 'INTENSITE OU POLARISATION')
; (TABLEAU DE 3D CONTENANT LES 2 TYPE DE DONNEES 'INTENSITE ET POLARISATION')
;
; CATEGORY:
;	NRH1 Calculs
;
; CALLING SEQUENCE:
;	SBTRBCHGRD, Tab, Lun, , Profbckgr, Message
;
; INPUTS:
;                        TAB ...... Tableau issu de LEC1D contenant les images
;                                  (2 dimensions) 
;                           TAB(num_Canal,Temps)
;                                  (3 Dim sinon )
;                           TAB(Num_Canal,Temps,type)
;                                               type = 0 ---> Intensite
;                                               type = 1 ---> Polarisation
;                        LUN ......Numero d'unite logique dont x est issu.
;
; OPTIONAL INPUTS:
;	Non
;	
; KEYWORD PARAMETERS:
;	HEURES	Tableau d'heures de debut et de fin 
;				('hh:mn:ss:ccc','hh:mn:ss:ccc')
;
; OUTPUTS:
;	TAB	Apres soustraction
;
;	PROFBCKGRD ...	Profil representant le fond (I, V)
;
; COMMON BLOCKS:
;	NRH1_GLOBAL
;
; MODIFICATION HISTORY:  (bonmartin@obspm.fr)
;	03/12/98	Adapte du logiciel XHELIO (JB)
;-*******************************************************************

PRO sbtrbckgrd, x, lun, HEURES=heures, profbckgr, message


;* -------------------------------------------- *
;*     Declaration des variables globales       *
;* -------------------------------------------- *
;
   COMMON NRH1_GLOBAL,nomfich,FichInfo
;
;* ----------------------------------------------------------- *
;*	Test de validite de la syntaxe de la procedure         *
;* ----------------------------------------------------------- *
;
  IF (N_PARAMS() LE 1) THEN BEGIN
	message = 'Erreur de syntaxe: Parametres manquants'
	message = message + 'Syntaxe: SBTRBCKGRD, varx, infovarx' + $
		 'HEURES=[heure de depart, heure de fin]'
	RETURN
  ENDIF


;* ----------------------------------------------------------------------- * 
;*	Memorisation de la taille du tableau; controle de la presence de V *
;* ------------------------------------------------------------------------* 
;*
s = size(x)   

IF ((s(0) LT 2) OR (s(0) GT 3)) THEN BEGIN
        message = 'Taille du tableau incompatible' + $
                'Le tableau traite doit provenir de la fonction LEC1D'
        return
ENDIF

;
;* ------------------------------------------------------------- * 
;*	gestion de l'intervalle de temps de visualisation        *
;* --------------------------------------------------------------*

IF NOT KEYWORD_SET(heures) THEN BEGIN
	message = 'Select time for background subtraction'
	RETURN
ENDIF
file=Fichinfo(lun)
verif_heures, file, HEURES=heures, numix1, numix2

IF numix2 LT numix1 THEN BEGIN
	message = 'The selected time interval for background subtraction'  $
	+ 'is not contained in the file'
	RETURN
ENDIF
;-----------------------------------------------------
; Calcul du profil moyenne sur l'intervalle HEURES, soustraction a x

nbprofs = numix2-numix1+1
message = "Background: average of scans # " + STRING(numix1) + " to " $
	+ STRING( numix2 )
IF s(0) GT 2 THEN BEGIN
	profbckgr = TOTAL(x(*,numix1:numix2,*), 2) / FLOAT(nbprofs)
	FOR i =0, s(2)-1 DO x(*,i,*) = x(*,i,*) - profbckgr
ENDIF ELSE BEGIN
	profbckgr = TOTAL(x(numix1:numix2,*), 1) / FLOAT(nbprofs)
	FOR i =0, s(1)-1 DO x(i,*) = x(i,*) - profbckgr
ENDELSE

END
