
;+ *************************************************************************************
; NAME:
;	PLOT_MAXPOS
;
; PURPOSE:
;	Cette procedure affiche l'image au moment de son flux maximum +
;	toutes les positions trouvees sous forme de contours au moment de leur
;	flux maximum	
;
; CATEGORY:
;	RH_2D, recherche de positions
;
; CALLING SEQUENCE:
;	PLOT_MAXPOS, Fichpos, Fichimg, IDWIN=IdW
;
; INPUTS:
;	Fichpos:	Fichier contenant les positions
;	Fichimg:	Fichier contenant les images associees
;
; KEYWORD:
;	IDWIN:	numero de la fenetre (defaut: nouvelle fenetre)
;
; COMMON BLOCKS:
;	INFO2D, str_inf2d:	pour les coordonnees helio
;	VISU_2D,str_visu:	pour dimension de l'image
;	LIMIT,str_lim:		pour la region selectionnee
;
; MODIFICATION HISTORY:
;	Ecrit par: 	C Renie decembre 1997
;	mars 1998:	Affichage de la region seulement
;	juillet,1998:	Passage du nom de fichier en parametre au lieu du tableau
;	16/10/1998: ajout du keyword IDWIN
;	3/5/1999: utilisation READ_NRHIM au lieu de mrdfits
;- *************************************************************************************


PRO plot_maxpos, fichposi, fichimg, IDWIN=IdW
COMMON INFO2D, str_inf2d
COMMON VISU_2D,str_visu
COMMON LIMIT,str_lim
	
	; recupere le nombre de positions figurant dans le fichier
	hprim = HEADFITS(fichposi)
	nbposi = FXPAR(hprim, 'N_EXTEND')
	valmax = 0.0
	heurmax = 0
	; recherche de l'heure pour laquelle on trouve la position avec un flux max
	FOR num=0, nbposi-1 DO BEGIN
		time = rdfilepos(fichposi, 'TIME', num+1)
		flux = rdfilepos(fichposi, 'FLUX', num+1)
		temp = MAX(flux, index)
		IF valmax LT temp THEN BEGIN
			valmax = temp
			heurmax = time(index)
		ENDIF
	ENDFOR

	id_himg = TIME_IND_NRH(fichimg, MSH(heurmax))

	;calcul du nb de pixels en x et y en fonction de la region selectionnee
	interpx= Fix(str_visu.dim /( str_lim.Xf-str_lim.Xd))
	interpy= Fix(str_visu.dim /( str_lim.Yf-str_lim.Yd))
	interp= interpx
	if interpx gt interpy then interp = interpy
	nbpx = (str_lim.Xf-str_lim.Xd+1)*interp
	nbpy = (str_lim.Yf-str_lim.Yd+1)*interp

	; calcul des coordonnees heliographiques
	Xd = ind_rs(str_lim.Xd, str_inf2d.Xcen, str_inf2d.Ray)
	Xf = ind_rs(str_lim.Xf, str_inf2d.Xcen, str_inf2d.Ray)
	Yd = ind_rs(str_lim.Yd, str_inf2d.Ycen, str_inf2d.Ray)
	Yf = ind_rs(str_lim.Yf, str_inf2d.Ycen, str_inf2d.Ray)
	ECH_RS, Xd, Xf, nbpx, xplot
	ECH_RS, Yd, Yf, nbpy, yplot

	; affichage de l'image pour l'heure donnee
	READ_NRHIM, fichimg, '00:00:00', str_inf2d, image, str_im, INDIC=id_himg
	tabaff = image(str_lim.Xd:str_lim.Xf,str_lim.Yd:str_lim.Yf)
	tabaff=REBIN(tabaff, nbpx, nbpy)
	IF KEYWORD_SET(IdW) THEN WSET, IdW ELSE $
		WINDOW, /FREE, XSIZE=str_visu.dim, YSIZE=str_visu.dim, $
		TITLE=str_im.time
	TVSCL, tabaff
 	
	position = [0,0,nbpx, nbpy]

	; superposition de toutes les positions a l'heure de leur max
	temp = {GAUSS}
	FOR num=0, nbposi-1 DO BEGIN 
	   flux = rdfilepos(fichposi, 'FLUX', num+1)
	   vmax = MAX(flux, index)
	   rdline_fpos, fichposi, num+1, index, temp
	   tabaff = FLTARR(str_inf2d.Npx, str_inf2d.Npy)
	   calc_volgauss, temp, str_inf2d.Npx, str_inf2d.Npy, tabaff
	   tabaff=REBIN(tabaff(str_lim.Xd:str_lim.Xf,str_lim.Yd:str_lim.Yf), nbpx, nbpy)
	   CONTOUR, tabaff, xplot, yplot, XRANGE=[xplot(0), xplot(nbpx-1)], $
	   YRANGE=[yplot(0),yplot(nbpy-1)], $
	   /noerase, XSTYLE=1, YSTYLE=1, POS=position, /DEVICE
	   xcpos = ind_rs(temp.xmax, str_inf2d.Xcen, str_inf2d.Ray)
	   ycpos = ind_rs(temp.ymax, str_inf2d.Ycen, str_inf2d.Ray)
	   XYOUTS, xcpos, ycpos, STRING(CEIL(num), $
	   FORMAT='(I3)'), CHARSIZE=1.5, /DATA
	ENDFOR
	OPLOT_CIRCLE, 1.

END
