
;+ ***********************************************************************
; NAME:
;	NRH_IMAGE
;
; PURPOSE:
;	Procedure de trace des images 2D du Radioheliographe (NRH), provenant
;d'un fichier en format FITS:
;		Visualisation des images
;		Coupes
;Utilisee par le widget VISU_2D		
;
; CATEGORY:
;	Visualisation
;
; CALLING SEQUENCE:
;	NRH_IMAGE, Image, Str_im, Str_lim, Str_Visu, Forme
;
; INPUTS:
;	IMAGE:	Tableau des images 2 dimensions a visualiser, resultant de
;		la lecture des donnees au format FITS, tables binaires
;	STR_IM	Structure d'informations d'image
;	STR_LIM		Structure limites de traitement voir RH_2d
;	STR_VISU:	Structure liee a VISU_2D, voir RH_2d.pro pour 
;			la description
;	FORME:	Forme du trace
;		'I' images bitmap
;		'C' contours
;		'X' coupes utilisant la fonction IDL PROFILES
;		'Q' coupe utilisant la fonction IDL PROFILE
;			et trace de la coupe
;
; KEYWORDS:
;	IDWIN	Numero de la fenetre pour la visualisation d'images. Si abscent
;		creation de la fenetre numero 20
;	STK_WIN Incrementation du numero de fenetre pour affichage de Stokes V
;		pour les images et les contours
; EXAMPLE:
;	Data = mrdfits('file', 1, Header)
;
;		Initialiser les valeurs contenues dans STR_VISU
;
;   Visualisation d'une image en bitmap
;	NRH_IMAGE, Data, STR_IM, STR_LIM, STR_VISU, 'I'
;
;   Trace des contours
;	NRH_IMAGE, Data, STR_IM, STR_LIM, STR_VISU, 'C'
;
;
; MODIFICATION HISTORY:
;	Ecrit par:	J Bonmartin le 20/10/97 (bonmartin@obspm.fr)
;	Le 23/04/99 keyword IDWIN (JB)
;	Le 06/05/99 Keyword STK_WIN pour affichage intensite et polar	(JB)
;       Le 22/09/2008 :Bug charsize adaptable pour labels
;       images/contours(AB)
;       2013/05  : ajout de /dev dans appel a tvcircle (evite le
;       cercle deplace et aplati)
;                 pour la couleur blanche du cercle ajout de
;                 color = cgcolor('white',255)
;                 utilisation de cgtext a la place de xyouts (indispensable?)
;       2013/05  : affichage de la polar avec un fond de couleur fixe
;      26/04/2017: Congrid bug is corrected  line 97
;
;-*********************************************************************

PRO NRH_IMAGE, Data,Str_im, Str_lim, Str_visu, forme, SETLABEL = setlabel,$
	IDWIN = Idwin, STKV_WIN=Stkv_win

	interpx= FLOAT(str_visu.dim) /FLOAT( str_lim.xf-str_lim.xd+1)
	interpy= FLOAT(str_visu.dim) /FLOAT( str_lim.yf-str_lim.yd+1)
		interp= interpx
		if interpx gt interpy then interp = interpy
	nbpx = FIX((str_lim.xf-str_lim.xd+1)*interp)
	nbpy = FIX((str_lim.yf-str_lim.yd+1)*interp)

	sz= size(Data)
	Npix = sz(1)
	Xcen = Str_im.Crpixx
	YCEN = Str_im.Crpixy
	Rpix = Str_im.Ray

		set_plot, GETENV('NRH_DEV')

IF KEYWORD_SET(SETLABEL) THEN BEGIN
	XLAB = CRE_LABELNRH(Str_im, setlabel) 
    END ELSE BEGIN
        setlabel = {nrh_str_label,1,1,1,0,0,0,0.0,''}
	XLAB = CRE_LABELNRH(Str_im, setlabel) 
;	XLAB = Str_im.date+'  '+Str_im.freq+' '+Str_im.time
    ENDELSE
    nbp_limx = str_lim.xf-str_lim.xd+1
    nbp_limy = str_lim.yf-str_lim.yd+1
; Pour eviter un congrid sur une image qu on veut laisser intacte :
;    si pas de difference entre taille image et taille de la fenetre 
;    et pas de select region
    if str_visu.dim ne npix or nbp_limx ne nbpx or nbp_limy ne nbpy then begin
       IMAGE= Congrid(Data(str_lim.xd:str_lim.xf,$
                           str_lim.yd:str_lim.yf),nbpx, nbpy,/CUBIC)
    endif else begin
       IMAGE= Data
    endelse

if forme eq 'I' then BEGIN
;    Trace d'images

	if str_visu.print eq 1 then begin
		mydevice = !d.name
		set_plot, 'PS'
		device, Filename = 'img.ps',/Color, Bits=8
	endif
		if str_visu.print eq 0 then BEGIN
		    IF KEYWORD_SET(IDWIN) THEN WSET, Idwin $
		    ELSE BEGIN
			IF KEYWORD_SET(STKV_WIN) THEN Nwin=21 ELSE NWIN = 20
			window,Nwin, title= $
			Str_im.Physpar+' - Im: '+string(str_visu.ind),$
			XSIZE=nbpx,YSIZE = nbpy, $
			XPOS=50+(Nwin-20)*50,YPOS=500-(Nwin-20)*NBPY
		    ENDELSE
                 ENDIF
; essai pour stokesv : ne pas utiliser tvscl, mais tv : prendre le max
; en valeur absolue et tracer entre -max et +max pour voir la polar 
; >0 et <0 avec le meme fond (=128)
                IF KEYWORD_SET(STKV_WIN) THEN BEGIN
                   maxabs = max(abs(image))
                   ima_affich = image*(128./maxabs)+128.
                   tv, ima_affich,/centimeter, XSIZE=15, YSIZE=15
                END ELSE BEGIN

                   tvscl, image,/centimeter, XSIZE=15, YSIZE=15
;		tvimage, image,/centimeter, XSIZE=15, YSIZE=15
                ENDELSE
                color = cgcolor('white',255)
		tvcircle,Str_im.Ray*Interp,(Str_im.Crpixx-str_lim.Xd)*interp,$
			(Str_im.Crpixy-str_lim.Yd)*interp, color=color, /dev
;print,' en pixels, rayon, x et y du centre du cercle optique', Str_im.Ray*Interp, (Str_im.Crpixx-str_lim.Xd)*interp,(Str_im.Crpixy-str_lim.Yd)*interp
;stop
;		xyouts,0.5, 0.01, Xlab, $
		cgtext,0.5, 0.01, Xlab, color='white', $
			ALIGNMENT=0.5,CHARSIZE=setlabel.charsize,/NORMAL

	if str_visu.print eq 1 then begin
		r=widget_message('Output on img.ps')
		device,/close
		set_plot, mydevice
	endif
endif

if forme eq 'C' then begin
;	Trace de contours

		label =Sindgen(str_visu.cont)
		label(0)=1

	NrhLEVEL = NRH_LEVEL_LIN( Image, Str_visu.cont, $
				Str_visu.pcent, Str_visu.mcent)

			Xd = ind_rs(Str_lim.xd, Str_im.Crpixx, Str_im.Ray)
			Xf = ind_rs(Str_lim.xf, Str_im.Crpixx, Str_im.Ray)
		ECH_RS, XD, XF, NBPX, XCONT
			Yd = ind_rs(Str_lim.Yd, Str_im.Crpixy, Str_im.Ray)
			Yf = ind_rs(Str_lim.Yf, Str_im.Crpixy, Str_im.Ray)
		ECH_RS, YD, YF, NBPY, YCONT

	if str_visu.print eq 1 then begin
		mydevice = !d.name
		set_plot, 'PS'
		device, Filename = 'cont.ps',XSIZE=16, YSIZE= 16
	endif
	if str_visu.print ne 1 THEN BEGIN
		IF KEYWORD_SET(STKV_WIN) THEN Nwin=23 ELSE NWIN = 22 
		window,NWIN, TITLE='IDL 3 -'+Str_im.Physpar+' - Im: '+$
				string(str_visu.ind)
	endif

		xp= !d.x_vsize
		yp= !d.y_vsize
		position= [0.1,0.1,0.95*yp/xp,0.95]

		contour, image,Xcont,Ycont,LEVEL=NrhLevel, $
			C_LINESTYLE = (Nrhlevel LT 0.0)*3, $
			Xrange=[Xcont(0),Xcont(nbpx-1)],$
			Yrange=[Ycont(0),Ycont(nbpy-1)],$

			XSTYLE=1,YSTYLE=1,Position = position, $
			/DOWNHILL
	C_labels=label
		radius = 1.
		oplot_circle, radius

		xyouts,0.4,0.02,Xlab, $
			ALIGNMENT=0.5,CHARSIZE=setlabel.charsize,/NORMAL

	if str_visu.print eq 1 then begin
		r=widget_message('Output on cont.ps')
		device,/close
		set_plot, mydevice
	endif

endif

if forme eq 'X' then begin
;Visualisation de coupes en X,Y
		window,24,title=Str_im.Physpar+ '- Im: '+string(str_visu.ind),$
			XSIZE=str_visu.dim,YSIZE = str_visu.dim, $
			XPOS=200, YPOS=500
		tvscl, image,/centimeter, XSIZE=15, YSIZE=15

		xyouts,0.5, 0.01, Xlab, $
			ALIGNMENT=0.5,CHARSIZE=0.5,/NORMAL
		profiles, image
	WDELETE,24
endif

if forme eq 'Q' then begin
; Trace de coupes dans des directions quelconques

		PROF=profil(image,Xpos,Ypos)
			s=size(xpos)
		IF XPOS(0) EQ XPOS(s(1)-1) THEN BEGIN
			ZPOS = YPOS
			YPOS = XPOS
			XPOS = ZPOS
			TAXE= 'Y AXIS'
		END ELSE TAXE = 'X AXIS'

			Xpos = Temporary(Xpos)/interp
		Dx = ind_rs(Xpos(0), Xcen, Rpix)
		Dy = ind_rs(Xpos(s(1)-1), Xcen, Rpix)
		ech_rs, Dx, Dy, s(1),AXE	
		if (str_visu.print eq 1) then begin
			mydevice = !d.name
			set_plot, 'PS'
			device, Filename = 'profi.ps'
		endif else begin
			window,25,title='COUPE'
		endelse

		plot,AXE, PROF
		xyouts,0.5, 0.01,TAXE+XLAB, $
			ALIGNMENT=0.5,CHARSIZE=0.5,/NORMAL

			Ypos = Temporary(Ypos)/interp
			Dp= ind_rs(Ypos(0), Ycen, Rpix)
		XYOUTS,AXE(0),prof(0),string(DP,FORMAT='(f6.2)'),$
			ALIGNMENT=0,/data,Charsize = 0.8

			Dp= ind_rs(Ypos(s(1)-1), Ycen, Rpix)
		XYOUTS,AXE(S(1)-1),prof(s(1)-1), $
			string(DP,FORMAT='(f6.2)'),Charsize= 0.8,$
			ALIGNMENT=1,/data

		if str_visu.print eq 1 then begin
			r=widget_message('Output on profi.ps')
			device,/close
			set_plot, mydevice
		endif
endif

return

END
