;+
; NAME:
;	MK_RESUME
;
; PURPOSE:
;	Cree un fichier texte contenant une description des positions
;
; CATEGORY:
;	RH_2D, recherche de positions
;
; CALLING SEQUENCE:
;	MK_RESUME, Fichpos, Struct
;
; INPUTS:
;	Fichpos:	fichier contenant les positions
;	Struct:		structure contenant les infos 2d
;
; OUTPUTS:
;	un fichier texte 'resume.txt'
;
; MODIFICATION HISTORY:
; 	Written by:	C. Renié 10/03/1998
;-

PRO mk_resume, fichposi, str_inf2d

	OPENW, file_lun, 'resume.txt', /GET_LUN
	; recupere le nombre de positions figurant dans le fichier
	hprim = HEADFITS(fichposi)
	nbposi = FXPAR(hprim, 'N_EXTEND')
	;max_abs = 0.0 & min_abs = 10.0
	FOR i=0, nbposi-1 DO BEGIN
		;t = MRDFITS(fichposi, i+1, hext, /SILENT)
		hext = HEADFITS(fichposi, EXTEN=i+1)
		nbhits = FXPAR(hext, 'NAXIS1')/(FXPAR(hext, 'TFIELDS')*4)
		mess='Position' + STRING(i, FORMAT='(I3)') + ':'
		PRINTF, file_lun, mess
		temps = rdfilepos(fichposi, 'TIME', i+1)
		mess = ' Heure debut = ' + MSH(temps(0))+ $
		' Heure fin = ' + MSH(temps(nbhits-1)) + $
		' Hits = ' + STRING(nbhits, FORMAT='(I4)')
		PRINTF, file_lun, mess
		t = rdfilepos(fichposi, 'FLUX', i+1)
		max = MAX(t, idmax)
		mmax = TOTAL(t)/nbhits
		mxmax = TOTAL(rdfilepos(fichposi, 'EWPOS', i+1))/nbhits
		mymax = TOTAL(rdfilepos(fichposi, 'NSPOS', i+1))/nbhits
		mxmax = ind_rs(mxmax, str_inf2d.Xcen, str_inf2d.Ray)
		mymax = ind_rs(mymax, str_inf2d.Ycen, str_inf2d.Ray)
		;IF max_abs LT mmax THEN max_abs = mmax
		;IF min_abs GT mmax THEN min_abs = mmax
		xmax = ind_rs((rdfilepos(fichposi, 'EWPOS', i+1))(idmax), str_inf2d.Xcen, str_inf2d.Ray)
		ymax = ind_rs((rdfilepos(fichposi, 'NSPOS', i+1))(idmax), str_inf2d.Ycen, str_inf2d.Ray)
		mess = 'Description du max: heure= '+ MSH(temps(idmax)) + $
		' max= ' + STRING(max, FORMAT='(E9.3)') + $
		' Xmax= ' + STRING(xmax, FORMAT='(F5.2)') + $
		' Ymax= ' + STRING(ymax, FORMAT='(F5.2)')
		PRINTF, file_lun, mess
		mess = 'Description moyenne: max= ' + $
		STRING(mmax, FORMAT='(E9.3)') + ' Xmax= ' + STRING(mxmax, FORMAT='(F5.2)') + $
		' Ymax= ' + STRING(mymax, FORMAT='(F5.2)')
		PRINTF, file_lun, mess
	ENDFOR
	FREE_LUN, file_lun
END

PRO mk_filepostxt, fichposi, FORMAT=format
COMMON INFO2D, str_inf2d

	IF NOT KEYWORD_SET(format) THEN format= 'b'
	IF format EQ 'b' THEN OPENW, file_lun, 'fbrute.txt', /GET_LUN $
	ELSE OPENW, file_lun, 'fhelio.txt', /GET_LUN
	; recupere le nombre de positions figurant dans le fichier
	hprim = HEADFITS(fichposi)
	nbposi = FXPAR(hprim, 'N_EXTEND')
	FOR i=0, nbposi-1 DO BEGIN
		mess='Position' + STRING(i, FORMAT='(I3)') + ':'
		PRINTF, file_lun, mess
		PRINTF, file_lun, 'Image Heure IdX IdY Vmax GRA GRB TET'
		t = MRDFITS(fichposi, i+1, hext, /SILENT)
		nbhits = FXPAR(hext, 'NAXIS1')/(FXPAR(hext, 'TFIELDS')*4)
		FOR j=0, nbhits-1 DO BEGIN
			heure = MSH(t.time(j))
			xmax = t.xmax(j)
			ymax = t.ymax(j)
			max = t.max(j)
			gra = t.gra(j)
			grb = t.grb(j)
			tet = t.tet(j)
			IF format EQ 'b' THEN BEGIN
				mess = heure + STRING(xmax, FORMAT='(I3)') + $
				STRING(ymax, FORMAT='(I3)') + ' ' + STRING(max, FORMAT='(E8.2)') + $
				' ' + STRING(gra, FORMAT='(E8.2)') + ' ' + STRING(grb, FORMAT='(E8.2)') + $
				' ' + STRING(tet*!RADEG, FORMAT='(F5.1)')
			ENDIF ELSE BEGIN
				Xd = ind_rs(0, str_inf2d.Xcen, str_inf2d.Ray)
				Yd = ind_rs(0, str_inf2d.Ycen, str_inf2d.Ray)
				xmax = ind_rs(xmax, str_inf2d.Xcen, str_inf2d.Ray)
				ymax = ind_rs(ymax, str_inf2d.Ycen, str_inf2d.Ray)
				xa=COS(tet)/SQRT(gra)
				ya=SIN(ABS(tet))/SQRT(gra)
				xa = ind_rs(xa, str_inf2d.Xcen, str_inf2d.Ray) - Xd
				ya = ind_rs(ya, str_inf2d.Ycen, str_inf2d.Ray) - Yd
				xb=COS(tet)/SQRT(grb)
				yb=SIN(ABS(tet))/SQRT(grb)
				xb = ind_rs(xb, str_inf2d.Xcen, str_inf2d.Ray) - Xd
				yb = ind_rs(yb, str_inf2d.Ycen, str_inf2d.Ray) - Yd
				gra = SQRT(xa^2 + ya^2)
				grb = SQRT(xb^2 + yb^2)
				mess = heure + ' ' + STRING(xmax, FORMAT='(F5.2)') + ' '+ $
				STRING(ymax, FORMAT='(F5.2)') + ' ' + STRING(max, FORMAT='(E8.2)') + $
				' ' + STRING(gra, FORMAT='(E8.2)') + ' ' + STRING(grb, FORMAT='(E8.2)') + $
				' ' + STRING(tet*!RADEG, FORMAT='(F5.1)')
			ENDELSE
			PRINTF, file_lun, mess
		ENDFOR
	ENDFOR
	FREE_LUN, file_lun
END
