	pro put_ar_data, color

Common Exch_source,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SpotCoord,SUN,Par,EW_line_save,SN_line_save,P_save,Factor,Output

Common AR_database, db

if n_elements(color) eq 0 then color=0

	WIDGET_CONTROL,/hour

Db_date=strmid(Moment.date, 6, 2)+'/'+strmid(Moment.date, 3, 2)+'/'+	$
	strmid(Moment.date, 0, 2)

n=(where(db.date eq Db_date))(0)

if n lt 0 then goto, No_AR

entry=db(n > 0)

plots,  64+entry.map.ipeak.x, 64+entry.map.ipeak.y,   $
        /dev, syms=1, psym=8, color=color

plots,  64+entry.map.vpeak.x, 64+entry.map.vpeak.y,   $
        /dev, syms=2, psym=1, color=color

for j=0,entry.nar-1 do begin

xr=entry.region(j).location.x
yr=entry.region(j).location.y

dx=1

	if 	(xr gt dx) and  (yr gt dx) $
		and (xr lt (!d.x_size-dx)) and (yr lt (!d.y_size-dx)) then $
	xyouts, xr+64, yr+64, entry.region(j).name, align=0.5, /dev, font=0, $
		col=color
        endfor

No_AR:

	end


	function rd_c_log,filename,version=version,log=log,date=date,time=time,error=error

F_save=filename


filename=strlowcase((name_extract(filename))(0))

	if n_elements(version) le 0 then version=0
	if n_elements(log) le 0 then log=pickfile(tit='Please select a LOG file')

		if (findfile(log))(0) eq '' then begin
xwarning, ['The log file '+log+' not found.',	$
	'You can only view the image.']
error=1
return,0
		endif


	WIDGET_CONTROL,/hour

A=[0.,0.,0.]
error=0

	openr,lun,log,/get_lun

File_info=strarr(500)
temp=''
N_records=0

	while not eof(lun) do begin
readf,lun,temp
File_info(N_records)=temp
N_records=N_records+1
	endwhile

File_info=File_info(0:N_records-1)
x=File_info

free_lun,lun

Found=(j=0)

	REPEAT BEGIN

b=strpos(x(4*j),',')

	if b ge 0 then begin
file=strmid(x(4*j),0,b)
Number=fix(strmid(x(4*j),b+1,20))

if file eq filename and Number eq version then Found=1

	endif else begin
	file=x(4*j)

if strupcase(file) eq strupcase(filename) then Found=1
	endelse

j=j+1
        ENDREP UNTIL (j gt N_records/4-1) or Found

		if Found then begin

Num=j-1
x=x(4*num:4*num+3)
Date=x(1)
Time=x(2)
x=strtrim(strcompress(x(3)),2)

i1=strpos(x,' ')
i2=strpos(x,' ',i1+1)

A(0)=strmid(x,0,i1)
A(1)=strmid(x,i1+1,i2-i1)
A(2)=strmid(x,i2+1,20)
		endif else begin
xwarning, ['The log file '+log+' not found.',	$
	'You can only view the image.']
error=1
		endelse

filename=F_save

	return, A

	end



	pro source_input_optics


;	This routine loads optical picture into the window ID.Win(4)
;	and sets clipping rectangle in system variable !P corresponding
;	to disable clipping


Common Exch_source,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SpotCoord,SUN,Par,EW_line_save,SN_line_save,P_save,Factor,Output

Common AR_database, db

	WIDGET_CONTROL,/hour

Data.Map=(Num=0)

	if Data.Optics eq '' then path=getenv('optics_dir') $
		else path=subdir(Data.Optics)

	filename=pickfile(path=path,/read,file=Data.Optics)
	if filename eq '' then return

	WIDGET_CONTROL,/hour

Data.Optics=filename
aa=(name_extract(Filename))(1)

Nobeyama=0


	IF strlowcase(strmid(aa,6,1)) eq 'y' THEN BEGIN

YOHKOH=1

Inf1=(Inf2=(Number=(N_size='')))

openr,lun,Filename,/get_lun

Descr=fstat(lun)

readf,lun,Inf1
readf,lun,Number
readf,lun,Inf2
readf,lun,N_size

Inf1=Inf1+string(Number)
Inf2=Inf2+string(N_size)+'x'+strtrim(N_size,2)

Info_strings=strarr(Number)
readf,lun,Info_strings

Num=xselect(Info_strings,info=[Inf1,Inf2],tit='Please select a record')

Offset=Descr.size-1L*N_size*N_size*Number
temp=assoc(lun,bytarr(N_size,N_size),Offset)
Y_data=temp(Num)

	free_lun,lun

Moment.Date=strmid(aa,4,2)+' '+strmid(aa,2,2)+' '+strmid(aa,0,2)
Moment.Time=strmid(Info_strings(Num),16,8)

a=strtrim(Info_strings(Num),2)
version=strmid(a,0,strpos(a,' '))

	ENDIF ELSE BEGIN


	Y_data=bytscl(rd_image(filename, head=head, type=type, /sc))

		if type eq 'FITS' then begin
origin=strtrim(fh_r_key(head, 'origin', error=error),2)
if error then goto, Continue
	if origin eq 'nobeyama radio obs' or origin eq 'BADARY' then begin
Centre=[256., 256.]+64
Radius=fh_r_key(head, 'solr')/fh_r_key(head, 'cdelt1')
Image_Date=fh_r_key(head,'date-obs')
Image_Time=fh_r_key(head,'time-obs')
Nobeyama=1
	endif

version='0'
YOHKOH=0

		endif

Continue:

version='0'
YOHKOH=0

	ENDELSE

Data={PosEW:[-90.,0.], PosSN:[-90.,0.], PosSUN:[0.,0.], $
	BeamEW:0D, B_EW:Data.B_EW, BeamSN:0D, B_SN:Data.B_SN, $
	DxEW:[0D,0D,0D], DyEW:[0D,0D,0D], $
	DxSN:[0D,0D,0D], DySN:[0D,0D,0D], $
	Zoom:0, Press:0, Release:0, 		$
	Mode:'Follow', Mouse:0, Optics:filename, $
	ClearEW:1, ClearSN:1, ClearSUN:1,  $
	GridColor:255B-Ini.colors(8), GridType:'Heliographical', $
	Optics_data:temporary(Y_data),	$
        Point1:[0.,0.], Point2:[0.,0.], Point3:[0.,0.], 	$
	Map:0, Number:strtrim(version,2), $
	Shift_EW:Data.Shift_EW, Shift_SN:Data.Shift_SN}

wset,ID.Win(4)	&	erase,0

Sz=(Size(Data.Optics_data))([1,2])
N_sc=((Sz(0) lt 400) and (Sz(1) lt 400))+1

if Nobeyama then goto, Nob
;*****************************************************************
;	READING THE COORDINATES OF THE SOLAR DISK FROM THE FILE
;*****************************************************************


	CASE !version.OS OF

'windows':	Delim='\'

'Win32':	Delim='\'

ELSE:		Delim='/'

	ENDCASE

f=(name_extract(filename))(0)
bf=byte(f)
ind=where(bf le 57 and bf ge 48)
ff=string(bf(ind))
FileDate=strmid(ff,4,2)+' '+strmid(ff,2,2)+' '+strmid(ff,0,2)

log=subdir(Data.Optics)+Delim+strcompress(FileDate,/rem)+'.crd'

Limb=rd_c_log(filename,version=version,log=log,date=date,time=time,error=error)

	if error then begin
Limb=[[!d.x_size,!d.y_size]/2.,!d.x_size/3.]
Image_time=Moment.time
Image_date=Moment.date
	endif else begin
Image_time=time
Image_date=date
	endelse

Centre=Limb([0,1])
Radius=Limb(2)

Nob:

;*****************************************************************

dx_size=640

X_shift0=(dx_size-512)/2

X_shift=!d.x_size/2.-Centre(0)+X_shift0
Y_shift=!d.y_size/2.-Centre(1)+X_shift0

if N_sc gt 1 then tv,rebin(Data.Optics_data,512,512),X_shift,Y_shift $
else tv,Data.Optics_data,X_shift,Y_shift

WIDGET_CONTROL,ID.Label,set_val=Date_string(Moment.Date)+', '+Moment.Time+' UT'

xyouts,0.01,0.94,/nor,Date_string(Image_Date)+'!C'+Image_Time+' UT',	$
	chars=1.5,col=!d.n_colors-1

SUNo=SUN
suneph,Moment.Date,Moment.Time,SUNo

temp=!p.color		&	!p.color=Data.Gridcolor

	if strmid(!version.release,0,1) lt 5 then begin

!x.style=(!y.style=1)

!x.range=[-1,1]*0.5*!d.x_size/Radius
!y.range=[-1,1]*0.5*!d.y_size/Radius

map_set,SUN.B0*!Radeg,0,0, /grid,/label, glinestyle=Ini.lines(1), $
	/ortho,/noerase,pos=[0,0,1,1],/nobor,latdel=10,londel=10,color=Data.Gridcolor

!x.style=(!y.style=(!x.range=(!y.range=0)))

	endif else begin

map_set,SUN.B0*!Radeg,0,0, /ortho,/noerase,pos=[0,0,1,1],/nobor

!x.s=[0.5*!d.x_size, Radius] / float(!d.x_size)
!y.s=[0.5*!d.y_size, Radius] / float(!d.y_size)

P_clip_save=!P.clip
!p.clip = [0, 0, !d.x_size, !d.y_size]

MAP_GRID, latdel=10, londel=10, color=Data.Gridcolor, glinestyle=Ini.lines(1)

!P.clip=P_clip_save

	endelse


		!p.color=temp

Data.Map=1
scale,temp,/mem
;if Data.GridType eq 'Carrington' then SC.MapAxesK=temp else SC.MapAxes0=temp


scale,MapAxes0,/mem  &  SC.MapAxes0=MapAxes0

Centre0=[!x.window(0)+!x.window(1), !y.window(0)+!y.window(1)]/2.
;SC.R0=!D.x_vsize/2/Factor*[1,1]

SC.R0=Radius

SC.Centre0=(convert_coord(Centre0,/norm,/to_dev))([0,1])
;SC.R0=(convert_coord(R0,/norm,/to_dev))([0,1])

;plotline,1e6,SC.Centre0,/dev	&	plotline,0,SC.Centre0,/dev
plotline,-tan(SUN.Dp),SC.Centre0,/dev,linestyle=Ini.lines(1)

empty
!P.clip=[0,0,1000,1000]

goto, Label00

;***************************************************************


Optics_data=bytarr(400,400,/nozero)	&	date=(time='')

IF Data.Optics eq ''  THEN BEGIN
	filename=pickfile(path=getenv('optics_dir'),filter='*.pho',/read)
	if filename eq '' then return

ENDIF ELSE filename=Data.Optics

	WIDGET_CONTROL,/hour
		openr,lun,filename,/get_lun
	readf,lun,date,time
	readu,lun,Optics_data
		free_lun,lun

Data.Optics=filename

;WIDGET_CONTROL,ID.TimelabelIMG,set_value= $
;	Date_string(date)+',   '+time+' UT'


wset,Id.win(4)
erase,0

V_shift=!d.x_vsize*(Factor-1)/2

N=!d.x_vsize/400

if N eq	1 then tvscl, Optics_data,V_shift,V_shift else $
	tvscl, rebin(Optics_data,N*400,N*400),V_shift,V_shift


Label00:

	put_ar_data

empty
!P.clip=[0,0,1000,1000]
		end




	pro plot_scans, model=model, intensity=intensity, $
polarisation=polarisation,Xmargin=Xmargin,Xrange=Xrange, $
Scales=Scales,Charsize=Charsize,color=color,s_shift=s_shift,OS=OS

Common Exch_source,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SpotCoord,SUN,Par,EW_line_save,SN_line_save,P_save,Factor,Output

Common AR_database, db


; *********************************************************************
;	Draws scans in INTENSITY and POLARISATION
;	as well as the SUN model in the graphics window.
; *********************************************************************

if N_elements(s_shift) le 0 then s_shift=0
if n_elements(color) le 0 then color=0

	!p.multi=[0,0,3]

	Xplot=indgen(n_elements(model))+1

plot,Xplot,model,Xst=9,Xmar=Xmargin,Ymar=[2.5,0.5],Yst=4, $
   Xrange=Xrange,xticklen=0.2,charsiz=Charsize,font=0,col=color
scale,Smodel,/mem

if n_elements(OS) gt 0 then $
oplot,Xplot,OS/1.08,col=color,linest=Ini.lines(1)


		if n_elements(intensity) gt 1 then begin
plot,Xplot+s_shift,intensity,Xst=5,Xmar=Xmargin,Ymar=[1,0],Yst=5, $
   Xrange=Xrange, Yrange=[min(intensity),max(intensity)], $
	charsiz=Charsize,col=color
xyouts,0.02,0.5,'!18I!3',/norm,Charsiz=1.8
		endif else SI=Smodel
scale,SI,/mem

		if n_elements(polarisation) gt 1 then begin
plot,Xplot+s_shift,polarisation,Xst=5,Xmar=Xmargin,Ymar=[1,0],Yst=4, $
	Xrange=Xrange,charsiz=Charsize,col=color
xyouts,0.02,0.25,'!18V!3',/norm,Charsiz=1.8
		endif else SV=Smodel
scale,SV,/mem

SCALES={Smodel:Smodel, SI:SI, SV:SV}

!p.multi=0

end



	pro source_draw_win


;	This routine draws models of the quiet Sun scans
;	as well as the scans themselves if they are available
;	in two windows.
;	Accordingly, in the third window map grid is drawn.


Common Exch_source,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SpotCoord,SUN,Par,EW_line_save,SN_line_save,P_save,Factor,Output

Common AR_database, db


Sum_chan=[176,192]

;	****** DRAW MAIN WINDOW E - W ******

wset,Id.win(0)	&	erase,255
plot_scans, model=CHECKVIS(Rec,SSRT.NoEW,SSRT.CEW), $
	int=Stokes.IEW, pol=Stokes.VEW, Xmar=Ini.PmargX, $
		Xran=[1,Sum_chan(Rec)],Scales=S, $
		Char=Ini.Char, col=0, $
		OS=CHECKVIS(Rec,SSRT.NoEW_OS,SSRT.CEW_OS)

SC.ZEWmain=S.Smodel  &  SC.IEWmain=S.SI  &  SC.VEWmain=S.SV

;	****** DRAW MAIN WINDOW S - N ******

wset,Id.win(1)	&	erase,255
plot_scans, model=CHECKVIS(Rec,SSRT.NoSN,SSRT.CSN), $
	int=Stokes.ISN, pol=Stokes.VSN,	Xmar=Ini.PmargX, $
		Xran=[1,Sum_chan(Rec)],Scales=S, $
		Char=Ini.Char,	col=0, $
		OS=CHECKVIS(Rec,SSRT.NoSN_OS,SSRT.CSN_OS)

SC.ZSNmain=S.Smodel  &  SC.ISNmain=S.SI  &  SC.VSNmain=S.SV

;	****** DRAW SUN MAP WINDOW ******

IF Data.Optics ne ''  THEN source_input_optics ELSE BEGIN

Data.GridType = 'Heliographical'
!p.multi=0
wset,Id.win(4)	&	Erase,255
MarginY=[1.,1.]*0
MarginX=MarginY*!D.Y_CH_SIZE/!D.X_CH_SIZE

Limb=[[!d.x_size,!d.y_size]/2.,!d.x_size/3.]

Centre=Limb([0,1])
Radius=Limb(2)

!x.style=(!y.style=1)

!x.range=[-1,1]*0.5*!d.x_size/Radius
!y.range=[-1,1]*0.5*!d.y_size/Radius

temp=!p.color		&	!p.color=Data.Gridcolor

	if strmid(!version.release,0,1) lt 5 then begin

!x.style=(!y.style=1)

!x.range=[-1,1]*0.5*!d.x_size/Radius
!y.range=[-1,1]*0.5*!d.y_size/Radius

map_set,SUN.B0*!Radeg,0,0, /grid,/label, glinestyle=Ini.lines(1), $
	/ortho,/noerase,pos=[0,0,1,1],/nobor,latdel=10,londel=10,color=Data.Gridcolor

!x.style=(!y.style=(!x.range=(!y.range=0)))

	endif else begin

map_set,SUN.B0*!Radeg,0,0, /ortho,/noerase,pos=[0,0,1,1],/nobor

!x.s=[0.5*!d.x_size, Radius] / float(!d.x_size)
!y.s=[0.5*!d.y_size, Radius] / float(!d.y_size)

P_clip_save=!P.clip
!p.clip = [0, 0, !d.x_size, !d.y_size]

MAP_GRID, latdel=10, londel=10, color=Data.Gridcolor, glinestyle=Ini.lines(1)

!P.clip=P_clip_save

	endelse

!p.color=temp

Data.Map=1

scale,MapAxes0,/mem  &  SC.MapAxes0=MapAxes0

Centre0=[!x.window(0)+!x.window(1), !y.window(0)+!y.window(1)]/2.

SC.R0=Radius

SC.Centre0=(convert_coord(Centre0,/norm,/to_dev))([0,1])

plotline,1e6,SC.Centre0,/dev	&	plotline,0,SC.Centre0,/dev
plotline,-tan(SUN.Dp),SC.Centre0,/dev,linestyle=Ini.lines(1)

!p.color=temp

ENDELSE

	put_ar_data

!P.clip=[0,0,1000,1000]
empty
	end


	pro Zoomed_sun

;	Draws map grid, axes and diurnal parallel in
;	the large window for zoomed Sun image.
;	Accordingly, both FWHM beam positions for
;	both SSRT interferometers are drawn if they are
;	defined.


Common Exch_source,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SpotCoord,SUN,Par,EW_line_save,SN_line_save,P_save,Factor,Output

Common AR_database, db

;	*** ZOOM WINDOW (SUN MAP) ***

window_set,ID.win(5),mul=0

	IF Data.Zoom eq 0 THEN BEGIN

MarginY=[1.,1.]*0.8
MarginX=MarginY*!D.Y_CH_SIZE/!D.X_CH_SIZE
map_set,SUN.B0*!Radeg,0,0,/ort,/nobor,/grid,/lab,tit=' ', glinestyle=Ini.lines(1), $
Xmar=MarginX,Ymar=MarginY,col=Data.GridColor,latd=10,lond=10, $
	latal=1.,lonal=1.

scale,MapAxesZ,/mem	&	SC.MapAxesZ=MapAxesZ

XW=!x.window	&	YW=!y.window

SC.CentreZ=(convert_coord([XW(0)+XW(1),YW(0)+YW(1)]/2.,/nor,/to_dev))([0,1])
SC.RZ=(convert_coord([XW(1)-XW(0),YW(1)-YW(0)]/2.,/nor,/to_dev))([0,1])

plotline,1e6,SC.CentreZ,/dev,col=Data.GridColor,linestyle=Ini.lines(3)
plotline,0,SC.CentreZ,/dev,col=Data.GridColor,linestyle=Ini.lines(3)
plotline,-tan(SUN.Dp),SC.CentreZ,col=Ini.colors(6),/dev,linestyle=Ini.lines(1)

xyouts,0.1,0.95,Date_string(Moment.Date),/nor,col=0
xyouts,0.8,0.95,Moment.Time+' UT',/nor,col=0
Data.Zoom=1
	ENDIF ELSE BEGIN

scale,SC.MapAxesZ,/rec

	ENDELSE

if SpotCoord(2) le -1 then goto,LZoom
sz=size(SpotCoord)
if sz(0) eq 1 then in=1 else in=sz(2)
for i=0,in-1 do plots,SpotCoord([0,1],i),/data,psym=8,syms=0.7

LZoom:

B_EW=SC.CentreZ#[1,1,1]+transpose([[Data.DxEW*SC.RZ(0)],[Data.DyEW*SC.RZ(1)]])
for i=0,2,2 do plotline,tan(!Dpi/2-Par.G_EW)*SC.RZ(1)/SC.RZ(0), $
	B_EW(*,i), col=0, line=Ini.lines(4),/dev

B_SN=SC.CentreZ#[1,1,1]+transpose([[Data.DxSN*SC.RZ(0)],[Data.DySN*SC.RZ(1)]])
for i=0,2,2 do plotline,tan(!Dpi/2-Par.G_SN)*SC.RZ(1)/SC.RZ(0), $
	B_SN(*,i),col=0, line=Ini.lines(2),/dev


plots,[0.91,0.99],[0.115,0.115],lin=ini.lines(4),/nor
plots,[0.91,0.99],[0.075,0.075],lin=ini.lines(2),/nor

xyouts,0.85,0.1,'W-E',/nor,charsiz=1.2,col=0
xyouts,0.85,0.06,'S-N',/nor,charsiz=1.2,col=0

	end




	pro Source_event,ev


;	Event loop for routine SOURCE


Common Exch_source,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SpotCoord,SUN,Par,EW_line_save,SN_line_save,P_save,Factor,Output

Common AR_database, db

	CASE !version.OS OF

'windows':	Plot_color=255b
'Win32':	Plot_color=255b
ELSE:		Plot_color=127b

	ENDCASE


Sum_chan=[176,192]

N=128D		&	D=4.9D
C=2.997925D8		&	Fi=SSRT.Fi

H=SUN.H  &  Decl=SUN.Decl
	P=SSRT.P
NoEW=SSRT.NoEW  &  OEW=SSRT.OEW  &  CEW=SSRT.CEW
A_EW=Par.A_EW		&	G_EW=Par.G_EW
	Q=SSRT.Q
NoSN=SSRT.NoSN  &  OSN=SSRT.OSN  &  CSN=SSRT.CSN
A_SN=Par.A_SN		&	G_SN=Par.G_SN

	P_OS=SSRT.P_OS
NoEW_OS=SSRT.NoEW_OS  &  OEW_OS=SSRT.OEW_OS  &  CEW_OS=SSRT.CEW_OS
	Q_OS=SSRT.Q_OS
NoSN_OS=SSRT.NoSN_OS  &  OSN_OS=SSRT.OSN_OS  &  CSN_OS=SSRT.CSN_OS


Rsol=SUN.R  &  B0=SUN.B0  &  Dp=SUN.Dp

F0=SSRT.F0		&	Df=SSRT.Df


		;** PROCESS DRAWABLE EVENTS **

			FOR j=0,5 do $
		IF ev.id eq ID.View(j) THEN BEGIN
	if ev.press ne 0 then Data.press=1 	;Pressed button?
	if ev.release ne 0 then Data.press=0	;Released button?
		ENDIF

IF ev.id eq ID.View(0) THEN BEGIN

window_set,Id.win(0),sca=SC.IEWmain
temp=(convert_coord(ev.x, ev.y, /DEVICE,  /TO_DATA))([0,1])

WIDGET_CONTROL,ID.LabelEWSN(0), 	$
	set_val=string(temp(0),temp(1),format='(F6.1,",",2X,F6.1)')

	if Data.press then begin

		CASE Data.Mode OF

'Follow':	if not Data.ClearEW then begin

device,set_graphics_function=6
plots,[(convert_coord(Data.PosEW,/to_norm))([0,0])],[0,1], $
	/norm,col=Plot_Color			; Restore precedent lines
Data.PosEW=temp
plots,[(convert_coord(Data.PosEW,/to_norm))([0,0])],[0,1], $
	/norm,col=Plot_Color
window_set,Id.win(4), sca=SC.MapAxes0	;SUN MAP WINDOW
plotline,tan(!Dpi/2-G_EW)*SC.R0(1)/SC.R0(0), $
	Data.B_EW(*,1),/dev, col=Plot_Color
device,set_graphics_function=3
empty
	endif else Data.ClearEW=0

'Scope':	begin
Data.PosEW=temp
for j=0,1 do WIDGET_CONTROL,ID.Leftbase(j),map=j
window_set,Id.win(2)
device,set_graphics_function=3

plot_scans, model=CHECKVIS(Rec,NoEW,CEW), sca=SF, $
  int=Stokes.IEW, pol=Stokes.VEW, Xmar=Ini.PmargX,Char=Ini.Char, $
      Xran=[Data.PosEW(0)-10,Data.PosEW(0)+10],col=0, $
		s_shift=Data.Shift_EW

SC.IEWaux=SF.SI
empty
return
	end

		ELSE:
		ENDCASE
	endif

ENDIF

	IF ev.id eq ID.View(2) THEN BEGIN

window_set,Id.win(2),sca=SC.IEWaux

temp=(convert_coord(ev.x, ev.y, /DEVICE,  /TO_DATA))([0,1])

WIDGET_CONTROL,ID.LabelEWSN(2),set_val= $
string(temp(0),temp(1),format='(F6.1,",",2X,F6.1)')


		if (Data.press gt 0) then begin

Data.PosEW=temp	&	wait,0.2

for j=0,1 do WIDGET_CONTROL,ID.Leftbase(j),map=1-j
WIDGET_CONTROL,ID.LabelEWSN(0),set_val= $
string(Data.PosEW(0),Data.PosEW(1),format='(F6.1,",",2X,F6.1)')

		endif

	ENDIF


	IF ((ev.id eq ID.View(0))  or (ev.id eq ID.View(2))) THEN $
		IF Data.press eq 0 THEN return ELSE BEGIN

if (ev.id eq ID.View(2)) then Data.press=0

;	SUN MAP WINDOW
window_set,Id.win(4), sca=SC.MapAxes0

ChanEWobs=Data.PosEW(0)

OEWobs=ORD_RECOGNIZE(ChanEWobs,NoEW,OEW,CEW)

CoordEW=acos(OEWobs*C/chanfreq(ChanEWobs,Rec)/D)
Data.BeamEW=0.886*C/(N*F0*D*abs(sin(P(1))))*par.BeamEW(1)/par.BeamEW(0)

CoordEW=[CoordEW-Data.BeamEW/2,CoordEW,CoordEW+Data.BeamEW/2]
Data.DyEW=[0D,0D,0D]
Data.DxEW=(P(1)-coordEW)/cos(G_EW)/Rsol

KEW=tan(!Dpi/2-G_EW)
BEW=Data.DyEW-KEW*Data.DxEW

Data.B_EW=SC.Centre0#[1,1,1]+ $
	transpose([[Data.DxEW*SC.R0(0)],[Data.DyEW*SC.R0(1)]])

device,set_graphics_function=6
plotline,tan(!Dpi/2-G_EW)*SC.R0(1)/SC.R0(0), $
   Data.B_EW(*,1),col=Plot_Color,/dev
device,set_graphics_function=3

empty
return

	ENDELSE


	IF ev.id eq ID.View(1) THEN BEGIN

window_set,Id.win(1),sca=SC.ISNmain
temp=(convert_coord(ev.x, ev.y, /DEVICE,  /TO_DATA))([0,1])

WIDGET_CONTROL,ID.LabelEWSN(1),set_val= $
string(temp(0),temp(1),format='(F6.1,",",2X,F6.1)')

	if (Data.press gt 0) then begin

		CASE Data.Mode OF

'Follow':		if Data.ClearSN ne 1 then begin

device,set_graphics_function=6
plots,[(convert_coord(Data.PosSN,/to_norm))([0,0])],[0,1], /norm,col=Plot_Color
Data.PosSN=temp
plots,[(convert_coord(Data.PosSN,/to_norm))([0,0])],[0,1], /norm,col=Plot_Color
window_set,Id.win(4), sca=SC.MapAxes0
plotline,tan(!Dpi/2-G_SN)*SC.R0(1)/SC.R0(0), Data.B_SN(*,1),/dev, col=Plot_Color
device,set_graphics_function=3
empty
		endif else Data.clearSN=0

'Scope':		begin

Data.PosSN=temp
for j=0,1 do WIDGET_CONTROL,ID.Leftbase(j),map=j
window_set,Id.win(3)
device,set_graphics_function=3
plot_scans, model=CHECKVIS(Rec,NoSN,CSN), $
  int=Stokes.ISN, pol=Stokes.VSN, Xmar=Ini.PmargX, sca=SF, $
      Xran=[Data.PosSN(0)-10,Data.PosSN(0)+10],Char=Ini.Char,col=0, $
		s_shift=Data.Shift_SN

SC.ISNaux=SF.SI
empty		&	return
		end

		ELSE:
		ENDCASE
	endif
ENDIF

	IF ev.id eq ID.View(3) THEN BEGIN
window_set,Id.win(3),sca=SC.ISNaux
temp=(convert_coord(ev.x, ev.y, /DEVICE,  /TO_DATA))([0,1])

WIDGET_CONTROL,ID.LabelEWSN(3),set_val= $
	string(temp(0),temp(1),format='(F6.1,",",2X,F6.1)')

		if (Data.press gt 0) then begin
	Data.PosSN=temp	&	wait,0.2
for j=0,1 do WIDGET_CONTROL,ID.Leftbase(j),map=1-j
WIDGET_CONTROL,ID.LabelEWSN(1),set_val= $
	string(Data.PosSN(0),Data.PosSN(1),format='(F6.1,",",2X,F6.1)')
		endif
	ENDIF


	IF ((ev.id eq ID.View(1))  or (ev.id eq ID.View(3))) THEN $
		IF Data.press eq 0 THEN return ELSE BEGIN

if (ev.id eq ID.View(3)) then Data.press=0

;	SUN MAP WINDOW
window_set,Id.win(4), sca=SC.MapAxes0

ChanSNobs=Data.PosSN(0)
OSNobs=ORD_RECOGNIZE(ChanSNobs,NoSN,OSN,CSN)
CoordSN=acos(OSNobs*C/chanfreq(ChanSNobs,Rec)/D)
Data.BeamSN=0.886*C/(N*F0*D*abs(sin(Q(1))))*par.BeamSN(1)/par.BeamSN(0)

CoordSN=[CoordSN-Data.BeamSN/2,CoordSN,CoordSN+Data.BeamSN/2]

Data.DySN=[0D,0D,0D]
Data.DxSN=(CoordSN-Q(1))/abs(cos(G_SN))/Rsol*sign(H)

KSN=tan(!Dpi/2-G_SN)
BSN=Data.DySN-KSN*Data.DxSN

Data.B_SN=SC.Centre0#[1,1,1]+ $
	transpose([[Data.DxSN*SC.R0(0)],[Data.DySN*SC.R0(1)]])

device,set_graphics_function=6
plotline,tan(!Dpi/2-G_SN)*SC.R0(1)/SC.R0(0), $
   Data.B_SN(*,1),col=Plot_Color,/dev
device,set_graphics_function=3

empty
return

	ENDELSE




	IF ev.id eq ID.View(4) THEN BEGIN

window_set,Id.win(4),sca=SC.MapAxes0		;Map Window
temp=(convert_coord(ev.x, ev.y, /DEVICE,  /TO_DATA))([0,1])

WIDGET_CONTROL,ID.MapLabel,set_val= $
		string(temp(0),temp(1),format='(F6.1,",",2X,F6.1)')

	if (ev.press ne 0) and (Data.Mouse eq 1) then begin	; Mark spot
plots,temp,/data,psym=8,syms=0.7	&	Data.Mouse=0
empty		&	return
	endif


	if (Data.press gt 0) then begin

	Data.PosSUN=temp
Xsun=([ev.x,ev.y]-SC.Centre0)/SC.R0

Pobs=P(1)-(Xsun(0)*cos(G_EW)-Xsun(1)*sin(G_EW))*Rsol

window_set,Id.win(0),sca=SC.IEWmain			;Window E-W
F_N=C/(D*cos(Pobs))

device,set_graphics_function=6

if equiv(EW_line_save, fltarr(NoEW))  then New=1 else New=0

		for j=0,NoEW-1 do begin
if (New ne 1)  and (Data.ClearSUN ne 1) then $
plots,[(convert_coord(EW_line_save(j),EW_line_save(j),/to_norm))([0,0])],[0,1], $
	/norm,col=Plot_Color

Cobs=chanfreq(F_N*OEW(j),Rec)
EW_line_save(j)=Cobs
plots,[(convert_coord(Cobs,Cobs,/to_norm))([0,0])],[0,1], $
	/norm,col=Plot_Color
		endfor

Qobs=Q(1)+(Xsun(0)*cos(G_SN)-Xsun(1)*sin(G_SN))*Rsol*sign(SUN.H)* $
	sign(sign(cos(G_SN))+0.5)
window_set,Id.win(1),sca=SC.ISNmain			;Window S-N
F_N=C/(D*cos(Qobs))


if equiv(SN_line_save, fltarr(NoSN)) then New=1 else New=0

		for j=0,NoSN-1 do begin
if (New ne 1)  and (Data.ClearSUN ne 1) then $
plots,[(convert_coord(SN_line_save(j),SN_line_save(j),/to_norm))([0,0])],[0,1], $
	/norm,col=Plot_Color
Cobs=chanfreq(F_N*OSN(j),Rec)
SN_line_save(j)=Cobs
plots,[(convert_coord(Cobs,Cobs,/to_norm))([0,0])],[0,1], $
	/norm,col=Plot_Color
		endfor

device,set_graphics_function=3
		Data.ClearSUN=0
	endif
empty
	return
	ENDIF

	IF ev.id eq ID.View(5) THEN BEGIN
window_set,Id.win(5),sca=SC.MapAxesZ		;Zoom Window
Data.PosSUN=(convert_coord(ev.x, ev.y, /DEVICE,  /TO_DATA))([0,1])

WIDGET_CONTROL,ID.ZoomLabel,set_val= $
	string(Data.PosSUN(0),Data.PosSUN(1),format='(F6.1,",",2X,F6.1)')

if ev.press ne 0  then plots,Data.PosSUN,/data,psym=8,syms=0.7	; Mark spot

empty		&	return
	ENDIF




;**************** OTHER EVENTS **********************



WIDGET_CONTROL,ev.id,GET_UVALUE = wuv,/hourglass

	CASE wuv OF

"DONE"	:  	begin

WIDGET_CONTROL,/hour

		if ID.group_leader ne 0L then begin
if WIDGET_INFO(ID.group_leader,/valid) then $
	WIDGET_CONTROL,ID.group_leader,/show
		endif

!P=P_save

ID=(SC=(Moment=(Data=(Ini=(Rec=(Stokes=(SSRT=0)))))))
P_save=(SpotCoord=(SUN=(Par=(EW_line_save=(SN_line_save=0)))))
xyouts,0,0,'!3 ',/nor


WIDGET_CONTROL,ev.top,/DEST
		end

"QuitZoom":	for j=0,1 do WIDGET_CONTROL,ID.Togglebase(j),map=1-j

"XMTool":	XMTool,group=ev.top

"Xloadct":	Xloadct

"Scope":	Data.Mode='Scope'

"Follow":	Data.Mode='Follow'

"Calculator":	wcalc


"Suncalc":	begin
suncalc,group_leader=ev.top, Moment.Date,Moment.Time

		end

"Help"	:	begin

	CASE !version.OS OF

'windows':	Delim='\'

'Win32':	Delim='\'

ELSE:		Delim='/'

	ENDCASE


xtext,file=getenv('help_dir')+Delim+'source.hlp',group=ev.top
		end
"VC":	spawn,'vc'

"NC":	spawn,'nc'

"DOS"	:	spawn

"Parameters":	param_ssrt,time=Moment.time, Date=Moment.Date,Rec=Rec,group=ev.top

"Preprocessing":	yp,group=ev.top

"Clear":		begin
Data.ClearEW=(Data.ClearSN=(Data.ClearSUN=1))
Data.PosEW=(Data.PosSN=[-90.,0.])	&	Data.PosSUN=[5.,5.]
	source_draw_win
!P.clip=[0,0,1000,1000]
		end

"Slider0":	begin
WIDGET_CONTROL,ID.SliderLabel(0),set_val='Shift = '+ $
	string(ev.value*0.1,format='(f4.1)')

Data.Shift_EW=ev.value*0.1

;	****** DRAW MAIN WINDOW E - W ******

wset,Id.win(0)	&	erase,255
plot_scans, model=CHECKVIS(Rec,SSRT.NoEW,SSRT.CEW), $
	int=Stokes.IEW, pol=Stokes.VEW, Xmar=Ini.PmargX, $
	Xran=[1,Sum_chan(Rec)],Scales=S, $
	Char=Ini.Char,col=0, s_shift=ev.value*0.1, $
		OS=CHECKVIS(Rec,SSRT.NoEW_OS,SSRT.CEW_OS)

SC.ZEWmain=S.Smodel  &  SC.IEWmain=S.SI  &  SC.VEWmain=S.SV

		end

"Slider1":	begin
WIDGET_CONTROL,ID.SliderLabel(1),set_val='Shift = '+ $
	string(ev.value*0.1,format='(f4.1)')

Data.Shift_SN=ev.value*0.1


;	****** DRAW MAIN WINDOW S - N ******

wset,Id.win(1)	&	erase,255
plot_scans, model=CHECKVIS(Rec,SSRT.NoSN,SSRT.CSN), $
	int=Stokes.ISN, pol=Stokes.VSN,	Xmar=Ini.PmargX, $
	Xran=[1,Sum_chan(Rec)],Scales=S, $
	Char=Ini.Char,col=0,s_shift=ev.value*0.1, $
		OS=CHECKVIS(Rec,SSRT.NoSN_OS,SSRT.CSN_OS)

SC.ZSNmain=S.Smodel  &  SC.ISNmain=S.SI  &  SC.VSNmain=S.SV


		end


"E-W FWHM":	begin			;	WINDOW E-W

window_set,Id.win(0),sca=SC.IEWmain
SpacingEW=abs(tan(!Dpi/2-P(1))*Df/F0)
Pos=transpose((convert_coord([[Data.PosEW-Data.BeamEW/SpacingEW/2], $
	[Data.PosEW+Data.BeamEW/SpacingEW/2]],/to_norm))(0,*))
for i=0,1 do plots,[Pos(i),Pos(i)],[0,1], /norm,col=0
empty
		end

"S-N FWHM":	begin			;	WINDOW S-N

window_set,Id.win(1),sca=SC.ISNmain
SpacingSN=abs(tan(!Dpi/2-Q(1))*Df/F0)
Pos=transpose((convert_coord([[Data.PosSN-Data.BeamSN/SpacingSN/2], $
	[Data.PosSN+Data.BeamSN/SpacingSN/2]],/to_norm))(0,*))
for i=0,1 do plots,[Pos(i),Pos(i)],[0,1], /norm,col=0
empty
		end

"SUN FWHM":	begin			;	SUN MAP WINDOW

window_set,Id.win(4), sca=SC.MapAxes0
for i=0,2,2 do plotline,tan(!Dpi/2-G_EW)*SC.R0(1)/SC.R0(0), $
	Data.B_EW(*,i),/dev,col=0
for i=0,2,2 do plotline,tan(!Dpi/2-G_SN)*SC.R0(1)/SC.R0(0), $
	Data.B_SN(*,i),/dev,col=0
empty
		end

"Zoom":		begin
	for j=0,1 do WIDGET_CONTROL,ID.Togglebase(j),map=j
	Zoomed_sun
		end

"Kbrd": BEGIN

kb_in_helio,SpotCoord,prompt='Input spots coordinates', group=ev.top
window_set,Id.win(4), sca=SC.MapAxes0
if SpotCoord(2) le -1 then goto,Lkbrd
sz=size(SpotCoord)
if sz(0) eq 1 then in=1 else in=sz(2)

	FOR i=0,in-1 DO BEGIN
IF SpotCoord(2,i) EQ 1. THEN SpotCoord(*,i)=[(convert_coord(SpotCoord(*,i)*SC.R0+ $
		SC.Centre0,/dev,/to_data))([0,1]),0]
plots,SpotCoord([0,1],i),/data,psym=8,syms=0.7
	ENDFOR
Lkbrd:
WIDGET_CONTROL,ev.top,/show
	END

"File":	begin

	rspotcoord,Moment,Coord,num=in
if in eq 1 then b0=0. else b0=fltarr(1,in)
Coord=[Coord,b0]
if SpotCoord(2) le -1. then SpotCoord=Coord else SpotCoord=[[Coord],[SpotCoord]]
window_set,Id.win(4), sca=SC.MapAxes0

for i=0,in-1 do plots,SpotCoord([0,1],i),/data,psym=8,syms=0.7
WIDGET_CONTROL,ev.top,/show
	end

"Mouse": Data.Mouse=1

"Image":	begin
source_input_optics
WIDGET_CONTROL,ev.top,/show
		end

"White":	Data.Gridcolor=Ini.colors(1)

"Mild":		Data.Gridcolor=Ini.colors(7)

"Medium":	Data.Gridcolor=Ini.colors(8)

"Sharp":	Data.Gridcolor=Ini.colors(9)

"Remove":	begin
if Data.Optics eq '' then erase,255 else source_input_optics
	CASE Data.GridType OF
	'Carrington':	window_set,ID.Win(4),scal=SC.MapAxesK
 	'Heliographical':	window_set,ID.Win(4),scal=SC.MapAxes0
	ELSE:
	ENDCASE
		end

"Carrington":	begin
Data.GridType='Carrington'	&	Lon=SUN.Karr*!Radeg
		end

"Heliographical":begin
Data.GridType = 'Heliographical'	&	Lon=0.
		end

"Axes":		begin
temp=!p.color		&	!p.color=Data.Gridcolor
window_set,ID.Win(4)
plotline,1e6,SC.Centre0,/dev	&	plotline,0,SC.Centre0,/dev
!p.color=temp
		end

"Diurnal parallel":	begin
temp=!p.color		&	!p.color=Data.Gridcolor
window_set,ID.Win(4)
	plotline,-tan(SUN.Dp),SC.Centre0,/dev,linestyle=Ini.lines(1)
!p.color=temp
			end

"AR_White":	put_ar_data, !d.n_colors-1

"AR_Black":     put_ar_data, 0

"PS":			begin

goto,BMP1

set_plot,'PS'

Sum_chan=[176,192]

!x.thick=(!y.thick=(!P.thick=(!P.charthick=2)))


	CASE !version.OS OF

'windows':	Delim='\'

'Win32':	Delim='\'

ELSE:		Delim='/'

	ENDCASE

PS_Filename=getenv('gr_prg')+Delim+	$
	newfilename(model=strcompress(Moment.Date,/rem),filt='*.PS')

device,file=PS_Filename,xsize=17.78,ysize=17.78,yoff=6.3

Data.GridType = 'Heliographical'
!p.multi=0
MarginY=[1.,1.]*0
MarginX=MarginY*!D.Y_CH_SIZE/!D.X_CH_SIZE

map_set,SUN.B0*!Radeg,0,0,/ortho,/nobor,/grid,/lab, glinestyle=Ini.lines(1), $
	Xmar=MarginX,Ymar=MarginY,latdel=10,londel=10

xyouts,0.02,0.95,/nor,Date_string(Moment.Date)+'!C'+Moment.Time

Centre0=[!x.window(0)+!x.window(1), !y.window(0)+!y.window(1)]/2.
R0=[!x.window(1)-!x.window(0), !y.window(1)-!y.window(0)]/2.

Centre0=(convert_coord(Centre0,/norm,/to_dev))([0,1])
R0=(convert_coord(R0,/norm,/to_dev))([0,1])

plotline,1e6,Centre0,/dev	&	plotline,0,Centre0,/dev
plotline,-tan(SUN.Dp),Centre0,/dev,linestyle=Ini.lines(1)

plotline,tan(!Dpi/2-G_EW)*R0(1)/R0(0), $
(Centre0#[1,1,1]+transpose([[Data.DxEW*R0(0)],[Data.DyEW*R0(1)]]))(*,1),/dev,linest=0

;goto, First_only

ChanEWobs=Data.PosEW(0)+2

OEWobs=ORD_RECOGNIZE(ChanEWobs,NoEW,OEW,CEW)

CoordEW=acos(OEWobs*C/chanfreq(ChanEWobs,Rec)/D)
BeamEW=0.886*C/(N*F0*D*abs(sin(P(1))))*par.BeamEW(1)/par.BeamEW(0)

CoordEW=[CoordEW-BeamEW/2,CoordEW,CoordEW+BeamEW/2]
DyEW=[0D,0D,0D]
DxEW=(P(1)-coordEW)/cos(G_EW)/Rsol

KEW=tan(!Dpi/2-G_EW)
BEW=DyEW-KEW*DxEW

B_EW=Centre0#[1,1,1]+ $
	transpose([[DxEW*SC.R0(0)],[DyEW*SC.R0(1)]])

;plotline,tan(!Dpi/2-G_EW)*R0(1)/R0(0), $
;(Centre0#[1,1,1]+transpose([[DxEW*R0(0)],[DyEW*R0(1)]]))(*,1),/dev,linest=5

First_only:

device,/close

PS_Filename=newfilename(model=strcompress(Moment.Date,/rem),filt='*.PS')

device,file=PS_Filename,xsize=17.78,ysize=17.78,yoff=6.3

plot_scans, model=CHECKVIS(Rec,SSRT.NoEW,SSRT.CEW), $
	int=Stokes.IEW, pol=Stokes.VEW, Xmar=Ini.PmargX, $
		Xran=[1,Sum_chan(Rec)],Char=Ini.Char, $
		s_shift=Data.Shift_EW


xyouts,0.02,0.95,/nor,Date_string(Moment.Date)+'!C'+Moment.Time+'!CE-W'

plots,[(convert_coord(Data.PosEW,/to_norm))([0,0])],[0,1], /norm,linest=0
;plots,[(convert_coord(Data.PosEW+2,/to_norm))([0,0])],[0,1], /norm,linest=5

device,/close

PS_Filename=newfilename(model=strcompress(Moment.Date,/rem),filt='*.ps')

device,file=PS_Filename,xsize=17.78,ysize=17.78,yoff=6.3

plot_scans, model=CHECKVIS(Rec,SSRT.NoSN,SSRT.CSN), $
	int=Stokes.ISN, pol=Stokes.VSN, Xmar=Ini.PmargX, $
		Xran=[1,Sum_chan(Rec)],Char=Ini.Char, $
		s_shift=Data.Shift_SN


xyouts,0.02,0.95,/nor,Date_string(Moment.Date)+'!C'+Moment.Time+'!CS-N'

plots,[(convert_coord(Data.PosSN,/to_norm))([0,0])],[0,1], /norm,linest=0

device,/close

	CASE !version.OS OF

'windows':	Initial_device='WIN'

'Win32':	Initial_device='WIN'

ELSE:		Initial_device='X'

	ENDCASE


set_plot,Initial_device
!x.thick=(!y.thick=(!P.thick=(!P.charthick=1)))

BMP1:

Filename0=newfilename(model=strcompress(Moment.Date,/rem)+'s',filt='*.bmp')
wset,ID.win(4)
write_bmp,Filename0,bytscl(tvrd(),top=196b)

Filename1=newfilename(model=strcompress(Moment.Date,/rem)+'s',filt='*.bmp')
wset,ID.win(0)
write_bmp,Filename1,bytscl(tvrd(),top=196b)

Filename2=newfilename(model=strcompress(Moment.Date,/rem)+'s',filt='*.bmp')
wset,ID.win(1)
write_bmp,Filename2,bytscl(tvrd(),top=196b)



			end


ELSE:
	ENDCASE

	IF (wuv eq 'Mild') or (wuv eq 'Medium') or (wuv eq 'Sharp')  THEN BEGIN

;WIDGET_CONTROL,ID.GridTypelabel,set_val=Data.GridType,/hour
if Data.GridType eq 'Carrington' then temp=SC.MapAxesK else temp=SC.MapAxes0
window_set,ID.Win(4),scal=temp
temp=!p.color		&	!p.color=Data.Gridcolor
!P.clip=[0,0,!d.x_size,!d.y_size]
map_grid,/label,latdel=10,londel=10,col=Data.Gridcolor, glinestyle=Ini.lines(1)
!p.color=temp

	ENDIF

	IF (wuv eq 'Heliographical') or (wuv eq 'Carrington')  THEN BEGIN

;WIDGET_CONTROL,ID.GridTypelabel,set_val=Data.GridType,/hour
wset,ID.Win(4)
if Data.Optics eq '' then erase,255 else source_input_optics
temp=!p.color		&	!p.color=Data.Gridcolor


;map_set,SUN.B0*!Radeg,Lon,0, /grid,/label, glinestyle=Ini.lines(1), $
;	/ortho,/noerase,pos=[0,0,1,1],/nobor,latdel=10,londel=10,color=Data.Gridcolor


	if strmid(!version.release,0,1) lt 5 then begin

!x.style=(!y.style=1)

!x.range=[-1,1]*0.5*!d.x_size/Radius
!y.range=[-1,1]*0.5*!d.y_size/Radius

map_set,SUN.B0*!Radeg,Lon,0, /grid,/label, glinestyle=Ini.lines(1), $
	/ortho,/noerase,pos=[0,0,1,1],/nobor,latdel=10,londel=10,color=Data.Gridcolor

!x.style=(!y.style=(!x.range=(!y.range=0)))

	endif else begin

map_set,SUN.B0*!Radeg,Lon,0, /ortho,/noerase,pos=[0,0,1,1],/nobor

!x.s=[0.5*!d.x_size, Radius] / float(!d.x_size)
!y.s=[0.5*!d.y_size, Radius] / float(!d.y_size)

P_clip_save=!P.clip
!p.clip = [0, 0, !d.x_size, !d.y_size]

MAP_GRID, latdel=10, londel=10, color=Data.Gridcolor, glinestyle=Ini.lines(1)

!P.clip=P_clip_save

	endelse


!p.color=temp		&	scale,temp,/mem
if wuv eq 'Carrington' then SC.MapAxesK=temp else SC.MapAxes0=temp

	ENDIF

empty

	end


pro source,output1, group_leader=group_leader,Date=Date, $
time=time,Rec_Type=Rec_Type,iew=iew,vew=vew,isn=isn,vsn=vsn

Common Exch_source,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SpotCoord,SUN,Par,EW_line_save,SN_line_save,P_save,Factor,Output

Common AR_database, db

if xregistered('source') then return


	CASE !version.OS OF

'windows':	begin
		Factor=1.
		Delim='\'
		end

'Win32':	begin
		Factor=1.
		Delim='\'
		end

ELSE:		begin
		Factor=1.04
		Delim='/'
		end


	ENDCASE


if n_elements(group_leader) le 0 then group_leader = 0

WIDGET_CONTROL,/hourglass

SpotCoord=[0.,0.,-1.]

ID={View:Lonarr(6), Win:Lonarr(6), Label:0L, ToggleBase:[0L,0L], $
Leftbase:[0L,0L], LabelEW:0L, LabelEWSN:lonarr(4), MapLabel:0L, 	$
ZoomLabel:0L, Slider:[0L,0L],SliderLabel:[0L,0L],group_leader:group_leader}

Ax={Axes, x:{!Axis}, y:{!Axis}, z:{!Axis}, map:!Map}

SC={ZEWmain:Ax, IEWmain:Ax, VEWmain:Ax, $
ZSNmain:Ax, ISNmain:Ax, VSNmain:Ax, $
ZEWaux:Ax, IEWaux:Ax, VEWaux:Ax,    $
ZSNaux:Ax, ISNaux:Ax, VSNaux:Ax,    $
MapAxes0:Ax, R0:fltarr(2), Centre0:fltarr(2), $
MapAxesK:Ax, RK:fltarr(2), CentreK:fltarr(2), $
MapAxesZ:Ax, RZ:fltarr(2), CentreZ:fltarr(2)}

	Ax=0

M=strlowcase(findfile('vga_drv.rcg'))
	if equiv(M,'') then M=1 else begin
openr,lun,'vga_drv.rcg',/get_lun
readf,lun,M
free_lun,lun
	endelse		;M=0 for L-310 else M=1 (to plot lines with various styles)


Ini={Lines:indgen(5)*M(0),$
	colors:[0B, 	$		; Color Table
		!d.n_colors-1,	$	; Background
		0B,	$		; Main color for inscriptions
		255B,	$		; E-W
		160B,	$		; Reserved
		255B,	$		; S-N
		60B,	$		; Diurnal parallel
		200B,	$		; Mild grid
		100B,	$		; Medium grid
		0B],	$		; Sharp grid
	PmargX:[3.5,3.5], Char:1.5}

temp=make_array(2,3,val=2000D)

Data={PosEW:[-90.,0.], PosSN:[-90.,0.], PosSUN:[5.,5.], $
	BeamEW:0D, B_EW:temp, BeamSN:0D, B_SN:temp, $
	DxEW:[0D,0D,0D], DyEW:[0D,0D,0D], $
	DxSN:[0D,0D,0D], DySN:[0D,0D,0D], $
	Zoom:0, Press:0, Release:0, Mode:'Follow', Mouse:0, Optics:'', $
	ClearEW:1, ClearSN:1, ClearSUN:1,  $
	GridColor:255B-Ini.colors(8), GridType:'Heliographical',	$
	Optics_data:bytarr(640,640),	$
	Point1:[0.,0.], Point2:[0.,0.], Point3:[0.,0.], Map:0, Number:'', $
	Shift_EW:0., Shift_SN:0.}

s=dg_make_struct()

	if n_elements(db) lt 10 then begin
db_file=(findfile(getenv('ar_database')+Delim+'db.sav*'))(0)
		if db_file eq '' then begin
print,'Error: missing database file "db.save". Bye!'
return
		endif
restore,db_file
	endif

if n_elements(iew) le 0 then iew=0
if n_elements(vew) le 0 then vew=0
if n_elements(isn) le 0 then isn=0
if n_elements(vsn) le 0 then vsn=0

Stokes={iew:iew, vew:vew, isn:isn, vsn:vsn}


if n_elements(Date) le 0 then Date=''
if strlen(Date) lt 2 then read,'Date (e.g. 24 08 93) - ', Date
if n_elements(Time) le 0 then Time=''
if strlen(Time) lt 2 then read,'Time (e.g. 07 04 33.457) - ', Time
	Moment={Date:Date, Time:Time}

	if n_elements(Rec_Type) le 0 then begin
Rec=0   &	read,'Receiver (0-MFB, 1-AOR) - ', Rec
	endif else Rec=Rec_Type

P_save=!P
!p.background=Ini.colors(1)   &   !p.color=Ini.colors(2)

Sum_chan=[176,192]
Fmin=chanfreq(1,Rec)
Fmax=chanfreq(Sum_chan(Rec),Rec)
F0=(Fmax+Fmin)/2
Df=(Fmax-Fmin)/(Sum_chan(Rec)-1)

param_ssrt,Date,time,Rec,Par=Par,SUN=SUN,/silent

INT_ORD,0,Rec,SUN,P,NnEW,NordEW,ChanEW
INT_ORD,1,Rec,SUN,Q,NnSN,NordSN,ChanSN				;Radio SUN

INT_ORD,0,Rec,SUN,P_OS,NnEW_OS,NordEW_OS,ChanEW_OS,Radio=1.
INT_ORD,1,Rec,SUN,Q_OS,NnSN_OS,NordSN_OS,ChanSN_OS,Radio=1.	;Optical SUN


SSRT={Fi:51.7575d*!DPi/180, F0:F0, Df:Df,	$
P:P, NoEW:NnEW, OEW:NordEW, CEW:ChanEW, $
Q:Q, NoSN:NnSN, OSN:NordSN, CSN:ChanSN,	$
P_OS:P_OS, NoEW_OS:NnEW_OS, OEW_OS:NordEW_OS, CEW_OS:ChanEW_OS, $
Q_OS:Q_OS, NoSN_OS:NnSN_OS, OSN_OS:NordSN_OS, CSN_OS:ChanSN_OS}


EW_line_save=fltarr(NnEW)
SN_line_save=fltarr(NnSN)


	device,set_graphics_function=3
	device,get_scr=scr

if scr(1) lt 500 then ZoomWin=scr*0.8 else ZoomWin=scr*0.92

if scr(1) lt 1000 then TV_size=512 else TV_size=640

TV_size=[1,1]*TV_size*Factor

Scan_size0=[(scr(0)-TV_size(0))*0.9,scr(1)/2.5]

Scan_size1=[TV_size(0),scr(1)/2.3]

	Xs=	[[[Scan_size0]#replicate(1,2)], $
		[[Scan_size1]#replicate(1,2)],	$
		[TV_size], [ZoomWin]]



;***** Drawing widget

Mainbase= widget_base(/fra, group=group_leader, $
  tit='Coordinates for event of  '+Date_string(Moment.Date))

for j=0,1 do ID.ToggleBase(j)=widget_base(Mainbase)

;***** Left Base

if scr(1) lt 500 then Wholebase=widget_base(ID.ToggleBase(0),/row,/scroll, $
	x_scroll_size=scr(0)*0.96, y_scroll_size=scr(1)*0.91) else $
		Wholebase=widget_base(ID.ToggleBase(0),/row)
LeftTogglebase=widget_base(Wholebase)
for j=0,1 do ID.Leftbase(j)=widget_base(LeftTogglebase,/colu)



 XPdMenu,	['"DONE"			DONE',	$

		'"Tools"	{',			$

			'"Screen"{',			$
				'"Mode"	{',			$
					'"Follow"		Follow',	$
					'"Scope"		Scope',	'}',$
				'"Zoom"			Zoom',	$
				'"Clear"			Clear','}',$

			'"Grid"	{',			$

				'"Diurnal parallel"	Diurnal parallel',	$
				'"Axes"			Axes',		$

				'"Brightness"	{',			$
					'"White"		White',		$
					'"Light"		Mild',		$
					'"Grey"		Medium',	$
					'"Black"		Sharp','}',	$

				'"Longitude"	{',	$
					'"Heliographical"		Heliographical',	$
					'"Carrington"		Carrington','}',$

				'"Remove"	Remove',		$

				'"AR color"	{',			$
					'"White"		AR_White',		$
					'"Black"		AR_Black','}',	$

						'}',	$

			'"Beam FWHM"		{',		$
				'"On the E-W scan"	E-W FWHM',	$
				'"On the S-N scan"	S-N FWHM',	$
				'"On the Sun"	SUN FWHM','}',$

			'"Calculator"		Calculator', $
			'"Coord. converter"	Suncalc', $
			'"Parameters"		Parameters', $

			'"Input of image"	{',			$
;				'"Keyboard"		Kbrd',	$
;				'"Mouse"		Mouse',	$
;				'"File"			File',	$
				'"Optical picture"	Image',	$
				'"Preprocessing"	Preprocessing',	$
						'}',$

			'"Xloadct"		Xloadct',	$

			'"XManager Tool"	XMTool',	$

			'"Shell"			DOS', 		$

;			'"Norton Commander"	{','"NC"	NC',	$
;						'"VC"	VC','}',	$

	       			'}',				$
;		'"PS"			PS',			$
		'"Help"			Help'], ID.Leftbase(0)

;Emptystring=string(0,format='(30(" "))')

Emptystring='    '

ID.label=WIDGET_LABEL(ID.Leftbase(0), val= $
	Emptystring+Time+' UT'+Emptystring+'E-W')

;if scr(1) gt 1000 then	ID.view(4)=WIDGET_DRAW(ID.Leftbase(0), XS=Xs(0,4), $
;	YS=Xs(1,4), /motion, /button, retain=2) else $

Scroll_size=480

if scr(1) gt 1000 then	ID.view(4)=WIDGET_DRAW(ID.Leftbase(0), XS=640, $
	YS=640, /motion, /button, retain=2) else $
			ID.view(4)=WIDGET_DRAW(ID.Leftbase(0), XS=640, $
	YS=640, /motion, /button, retain=2,/scroll,	$
	x_scroll=Scroll_size,y_scroll=Scroll_size)

	if strmid(!version.release,0,1) lt 5 then $
ID.Maplabel=WIDGET_LABEL(ID.Leftbase(0), val= $
	'Diurnal parallel'+Emptystring+'S-N') else $
ID.Maplabel=WIDGET_LABEL(ID.Leftbase(0), val= $
	'Diurnal parallel'+Emptystring+'S-N', /dynam)


	for j=2,3 do begin
ID.view(j)=WIDGET_DRAW(ID.Leftbase(1), XS=Xs(0,j), YS=Xs(1,j), /motion, $
	/button_events, retain=2)
	if strmid(!version.release,0,1) lt 5 then $
ID.LabelEWSN(J)=WIDGET_LABEL(ID.Leftbase(1),val=Emptystring) else $
ID.LabelEWSN(J)=WIDGET_LABEL(ID.Leftbase(1),val=Emptystring, /dynam)
	endfor

ZoomBase=WIDGET_BASE(ID.ToggleBase(1),/row)
junk=WIDGET_BASE(ZoomBase,/colu)
junk1=WIDGET_BUTTON(junk,VAL='DONE',uval='QuitZoom')
junk=WIDGET_BASE(ZoomBase,/colu)
ID.view(5)=WIDGET_DRAW(junk, XS=Xs(0,5), YS=Xs(1,5), /motion, $
	/button_events, retain=2)
	if strmid(!version.release,0,1) lt 5 then $
ID.ZoomLabel=WIDGET_LABEL(junk,val=Emptystring) else $
ID.ZoomLabel=WIDGET_LABEL(junk,val=Emptystring, /dynam)

;***** Right Base
Rightbase=widget_base(Wholebase,/colu)
	for J=0,1 do begin
Slider_Base=WIDGET_BASE(Rightbase,/row)
ID.Slider(J)=WIDGET_SLIDER(Slider_Base,	$
	min=-50,max=50,val=0,uval='Slider'+strtrim(j,2),/suppress)
ID.SliderLabel(J)=WIDGET_LABEL(Slider_Base,val='Shift = 0     ')
ID.view(J)=WIDGET_DRAW(Rightbase, XS=Xs(0,J),YS=Xs(1,J), /motion, /button_events, retain=2)
	if strmid(!version.release,0,1) lt 5 then $
ID.LabelEWSN(J)=WIDGET_LABEL(Rightbase,val=Emptystring) else $
ID.LabelEWSN(J)=WIDGET_LABEL(Rightbase,val=Emptystring, /dynam)

	endfor

;*****


WIDGET_CONTROL,ID.Leftbase(1),map=0
WIDGET_CONTROL,ID.Togglebase(1),map=0

WIDGET_CONTROL,Mainbase,/real,/hour

	for J=0,5 do begin
WIDGET_CONTROL,ID.view(J),GET_VALUE=temp  &  ID.Win(J)=temp
wset,ID.Win(J)
Erase,255
	endfor

if scr(1) lt 1000 then WIDGET_CONTROL,ID.view(4),set_draw_view=[1,1]*(640-Scroll_size)/2

WIDGET_CONTROL,/hour

loadct,Ini.colors(0)
circ
source_draw_win

xmanager,'Source',Mainbase,group=group_leader

	end