
;+ *************************************************************************************
; NAME:
;	SCAN_SOURCES
;
; PURPOSE:
;	determine tous les volumes gaussiens contenus dans les images d'un
;	fichier RH 2D dans un interval de temps et dans une region donnes.
;
; CATEGORY:
;	RH_2D, Recherche de positions
;
; CALLING SEQUENCE:
;	SCAN_SOURCES, Fichimg, Fichsour, Str2d
;
; INPUTS:
;	Fichimg:	fichier contenant les images à scanner
;	Str2d:		structure de type str_inf2d
;
; OPTIONAL INPUTS:
;	Fichcent:	nom du fichier binaire dans lequel est stocke la description
;		des volumes gaussiens (defaut centres.dat)
;
; OUTPUTS:
;	Cette procedure cree un fichier binaire qui contient la description
;	de tous les volumes gaussiens trouves.
;
; COMMON BLOCKS:
;	LIMIT,str_lim: limite le temps et la region
;	OPTPOSI, str_opt: options du scanning
;
; EXAMPLE:
;	SCAN_SOURCES, 'nrh2_1640_h60_19970707_104509:73_i.fts', 'cent2301_2.dat', str
;
; MODIFICATION HISTORY:
;	Ecrit par: 	C. Renie decembre,1997
;	mars, 1998: 	Scanning sur une region
;			Parametrage du scanning
;	juillet 1998:	le 1er parametre n'est plus un tableau mais le nom du fichier
;	3/5/1999:	utilisation de READ_NRHIM pour la lecture des images
;			d'ou ajout de str2d en parametre
;+ *************************************************************************************

PRO SCAN_SOURCES, fichimg, fichcent, str_2d
COMMON LIMIT,str_lim
COMMON OPTPOSI, str_opt

	nbmax = str_opt.Nbmax & facteur=str_opt.Facteur & seuil=str_opt.Seuil
	fmax=str_opt.fmax/100.0
	idhdeb = TIME_IND_NRH(fichimg, str_lim.Hd)
	idhfin = TIME_IND_NRH(fichimg, str_lim.Hf)
	
	tgauss=REPLICATE({GAUSS, max:0.0, xmax:0.0, ymax:0.0, $
			gra:0.0, grb:0.0, tet:0.0}, nbmax)
	
	IF N_PARAMS() LT 2 THEN fichcent = 'centres.dat'
	
	nbcentre = 0
	OPENW, filetmp, fichcent, /GET_LUN
	
	FOR i=idhdeb, idhfin DO BEGIN
		;images = MRDFITS(fichimg, 1, Header, RANGE = [i, i],/SILENT)
		;tab = images.stokesi
		READ_NRHIM, fichimg, '00:00:00', str_2d, tab, str_im, INDIC=i
		tgauss(*).max = 0.0
		tabtrv = tab(str_lim.Xd:str_lim.Xf, str_lim.Yd:str_lim.Yf)
		extract_sources, tabtrv, tgauss, FACT=facteur, NBMAX=nbmax, $
		SEUIL=seuil, FMAX=fmax
		test = (tgauss(*).max NE -1.0) AND (tgauss(*).max NE 0.0)
		goodvalues = WHERE (test EQ 1, count)
		IF count NE 0 THEN BEGIN
			nbcentre = nbcentre + count
			FOR j=0, count-1 DO BEGIN
				tgauss(j).xmax=tgauss(j).xmax+str_lim.Xd*1.
				tgauss(j).ymax=tgauss(j).ymax+str_lim.Yd*1.
				WRITEU, filetmp, HMS(str_im.time), -1.0, tgauss(j)
			ENDFOR
		ENDIF
		;PRINT, 'Image n°', i,' Nb de centres = ', nbcentre
	ENDFOR
	
	FREE_LUN, filetmp
	
END
