;+ ***********************************************************************
; NAME:
;	MKFILEPOS
;
; PURPOSE:
;	Cree un fichier de positions a partir d'un fichier de sources trie
;
; CATEGORY:
;	RH_2D, Recherche de position
;
; CALLING SEQUENCE:
;	Fich = MKFILEPOS, Fich1, Fich2, Hdeb
;
; OPTIONAL INPUTS:
;	Fich1:	nom du fichier fits temporaire contenant les dernieres positions
;	Fich2:	nom du fichier images associe
;	Hdeb:	heure de la premiere image (chaine de caracteres)
;
; OUTPUTS:
;	Fich:	nom du fichier fits de positions cree
;
; EXAMPLE:
;	f = MKFILEPOS( 'positions.fits', 'nrh2_1640_h61_19970923_110031:07_c.fts', $
;			'11:00:31:070')
;
; MODIFICATION HISTORY:
;	Ecrit par: C. Renié 07/1998
;	04/1999: transformation en fonction pour retour du nom de fichier
;-*******************************************************************

FUNCTION MKFILEPOS, fichfits, fichimg, hdeb

	reponse = 'Yes'
	; verifie si le fichier existe deja
	fichposi = MOD_NRHFILE( fichimg, CONTENT='s', HEURE=hdeb, DIRECTORY='Current')
	r = FINDFILE(fichposi)
	IF r(0) NE '' THEN BEGIN	; le fichier existe
		text = 'Add positions to the existing file ?'
		reponse = DIALOG_MESSAGE(text, /QUESTION, /CANCEL)
		IF reponse EQ 'No' THEN BEGIN 
			text = 'Replace the existing file ?'
			reponse = DIALOG_MESSAGE(text, /QUESTION, /CANCEL)
			; initialise le fichier si remplacement
			IF reponse EQ 'Yes' THEN create_nrh_pfile, fichposi, fichimg
		ENDIF
	ENDIF
	IF reponse EQ 'Yes' THEN BEGIN
		; recupere le nombre de positions figurant dans le fichier temporaire
		hprim = HEADFITS(fichfits)
		nbposi = FXPAR(hprim, 'N_EXTEND')
		FOR pos=0, nbposi-1 DO BEGIN
			hext = HEADFITS(fichfits, EXTEN=pos+1)
			nbhits = FXPAR(hext, 'NAXIS1')/(FXPAR(hext, 'TFIELDS')*4)
			posi = REPLICATE({temps:0L, source:{GAUSS}, flux:0.0}, nbhits)
			t = rdfilepos(fichfits, 'TIME', pos+1)
			posi.temps = t
			posi.source.max = rdfilepos(fichfits, 'INTENSITY', pos+1)
			posi.source.xmax = rdfilepos(fichfits, 'EWPOS', pos+1)
			posi.source.ymax = rdfilepos(fichfits, 'NSPOS', pos+1)
			posi.source.gra = rdfilepos(fichfits, 'MAJAXIS', pos+1)
			posi.source.grb = rdfilepos(fichfits, 'MINAXIS', pos+1)
			posi.source.tet = rdfilepos(fichfits, 'ANGLE', pos+1)
			posi.flux = rdfilepos(fichfits, 'FLUX', pos+1)
			WRITE_POSINRH, fichposi, fichimg, posi
		ENDFOR
	ENDIF ELSE fichposi = ''

	RETURN, fichposi
END
