	pro source_size_model,time

Common Exch_source_size,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SUN,Par,P_save,Results,Fileres,Flux


suneph,Moment.Date,time,SUN
INT_ORD,Data.Interf ne 'E-W',Rec,SUN,P,Nord,Ord,Chan
model=CHECKVIS(Rec,Nord,Chan)

z=Stokes(*,Data.number)-Flux.min

N_div=8.

D_chan=(chan(2,0)-chan(0,0))/N_div

	for j=0,Nord-1 do begin
gate0=(chan(0,j)+D_Chan+[0,(N_div-2)*D_chan]) > 0 < (Data.N_channels-1)
if j ne 0 then gate=[gate,gate0] else gate=gate0
	endfor

	for j=0,Nord-1 do begin
if j eq 0 then head=z(gate(2*j):gate(2*j+1)) else $
	head=[head,z(gate(2*j):gate(2*j+1))]
if j eq 0 then divider=model(gate(2*j):gate(2*j+1)) else $
	divider=[divider,model(gate(2*j):gate(2*j+1))]
	endfor

factor=min(smooth(median(head,3),3)/divider)

Flux={Nord:Nord,Ord:Ord,Chan:Chan,factor:factor,model:model,min:Flux.min, $
weight:0.}

	end


	pro source_size_res

;	This routine saves results obtained with the routine SOURCE_SIZE
;	into the file *******.rsz (****** - Date)

Common Exch_source_size,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SUN,Par,P_save,Results,Fileres,Flux

WIDGET_CONTROL,/hourglass

Rads=!DPi/180/3600

Fileres=newfilename(model=strcompress(Moment.Date,/rem),filt='*.rsz',	$
	path=getenv('results'))

	CASE !version.OS OF

'windows':	Delim='\'

'Win32':	Delim='\'

ELSE:		Delim='/'

	ENDCASE


Fileres=getenv('results')+Delim+Fileres


	openw,lun,Fileres,/get_lun

	Com_format= $
 ["('Bandwidth, kHz',T25,'Beam FWHM',T40,'Spreading factor')", $
 "('(CCD elements ',T20,'Arcsec',T30,'Channels')", $
 "('    integrated)')", $
 "(60('-'))"]

	First_words=["(T12,'0 (0 CCD)'","(T8,'292 (2 CCD)'","(T8,'583 (4 CCD)'"]

printf,lun,format="(T30,'SOURCE SIZE PROCESSING',/,/)"
printf,lun,format="(60('-'),/)"

printf,lun,Date_string(Moment.Date),Results.Current_Time, Results.Base_Time, $
Data.Dt*(Data.number-Data.Base_scan),format= $
"(T10,'DATE: ',A11,',  SPIKE: ',A12,' UT,',/,'Base scan: ',A12,' UT','   (time difference = ',F8.3,' sec)',/)"

printf,lun,Data.interf,format="(T10,'INTERFEROMETER  ',A3,'  PROCESSED')"
printf,lun,format="(60('-'),/)"

printf,lun,Results.Width, Results.width/Results.Beam_chan(0)*Results.Beam(0)/Rads, $
	format="('Response size = ',F5.1,' channels = ',F5.1,' arcsec')"

		FOR j=0,2 DO BEGIN
	if Results.width gt Results.Beam_chan(j) then printf,lun, Results.Width0(j), $
Results.width0(j)/Results.Beam_chan(0)*Results.Beam(0)/Rads, j*2, format= $
"('Source size = ',F5.1,' channels = ',F5.1,' arcsec  (',I1,' CCD channels integrated)')" $
	else printf,lun,Results.Width,Results.Beam_chan(j),format= $
"('Response size of ',F5.1,' observed less then beam width ',F5.1,'  (',I1,' CCD channels integrated)')"
		ENDFOR

printf,lun,format="(30('-'),/)"

printf,lun,fix(Results.ysmax+0.5),Results.sigma, Results.imax+1, $
	format="('Amplitude = ',I4,';   Sigma = ',F7.2,';    Channel number = ',F5.1,'  (1 - 192)',/)"

printf,lun,format="(60('-'),/)"

for j=0,3 do printf,lun,format=Com_format(j)

for j=0,2 do printf,lun,Results.Beam(j)/Rads,Results.Beam(j)/Results.Spacing, $
Results.Beam(j)/Results.Beam(0), $
	format=First_words(j)+",T30,F4.1,T45,F6.2,T55,F4.2)"

printf,lun,format="(60('-'),/)"

printf,lun,Results.Spacing/!Pi*180*3600,format= $
"('Spacing between frequency lobes (peak to peak) = ',F5.1,' arcsec',/)"

printf,lun,Results.Tmod(0),Results.Tmod(1), $
	1/Results.Tmod(0),1/Results.Tmod(1),format= $
"('Period of the beam-induced modulation = ',F6.2,' (fmin) ...',/,F6.2,' (fmax) sec',' (frequency = ',F6.2,' ... ',F6.2,' Hz)',/)"

free_lun,lun

flush,lun

	end


	pro source_size_c_u,x

;	This routine is called when the widget SOURCE_SIZE dies.
;	It restores system variable !P,
;	restores vector drawn font index 3,
;	saves coordinates of spots in file *******.spt (****** - Date),
;	minimizes memory allocated by variables contained in
;	common block Exch_source.



Common Exch_source_size,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SUN,Par,P_save,Results,Fileres,Flux

WIDGET_CONTROL,/hourglass

!P=P_save

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

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

	end


	pro source_size_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_size,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SUN,Par,P_save,Results,Fileres,Flux

X=indgen(Data.N_channels)+1

;	****** DRAW TV WINDOW ******

wset,Id.win(0)	&	erase
tv_axes,Stokes,/scale,font=0,xoffset=30
Scale,temp,/mem	&	SC.Tv=temp

;	****** DRAW SURFACE WINDOW ******

wset,Id.win(1)	&	erase
surface,Stokes,xmar=[4.2],ymar=[3,0.5],/hor


;	****** Fictive drawing SCAN WINDOW ******

wset,Id.win(2)	&	erase
plot,X,X,xst=4,yst=4,/nodata
Scale,temp,/mem	&	SC.Scan=temp

;	****** Fictive drawing WINDOW for differential scan ******

wset,Id.win(3)	&	erase
plot,X,X,xst=4,yst=4,/nodata
Scale,temp,/mem	&	SC.Dif=temp

;	****** Fictive drawing Show WINDOW ******

wset,Id.win(4)	&	erase
plot,X,X,xst=4,yst=4,/nodata
Scale,temp,/mem	&	SC.Show=temp

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



	pro Source_size_event,ev


;	Event loop for routine SOURCE_SIZE


Common Exch_source_size,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SUN,Par,P_save,Results,Fileres,Flux

X=indgen(Data.N_channels)+1

		;** PROCESS DRAWABLE EVENTS **

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

window_set,ID.Win(0),scale=Sc.Tv

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

N=long(p(1) < ((size(Stokes))(2)-1L) > 0L )

Current_Time=time_outvalue(N, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)

WIDGET_CONTROL,ID.Timelabel,set_val=' Scan '+strtrim(N,2)+', '+Current_Time

		if ev.press then begin

WIDGET_CONTROL,/hourglass

X_mark=(convert_coord(Data.N_channels+10, 0, /DATA, /TO_DEVICE))(0)

draw_marker,[X_mark,Data.Marker],col=!P.Background, 0.8, /left, /fill, /dev
draw_marker,[X_mark,ev.y],col=!P.color, 0.8, /left, /fill, /dev

Data.Marker=ev.y

Data.number=N > 1


wset,ID.Win(2)
plot,X,Stokes(*,Data.number),font=0, yticks=4, xmargin=[5,1], ymargin=[2,2], $
	tit=' Scan  '+strtrim(Data.number,2)+',  '+Current_Time, $
		xminor=4, yminor=4, xstyle=1
oplot,X,Stokes(*,Data.number)-Stokes(*,Data.number-1),/noc

oplot,Flux.model*Flux.factor+Flux.min,col=Ini.colors(5)

Scale,temp,/mem	&	SC.Scan=temp
Modestring='Sc '+strtrim(Data.number,2)+' - Sc '+strtrim(Data.Base_scan,2)
Base_Time=time_outvalue(Data.Base_scan, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)
wset,ID.Win(3)
plot,X,Stokes(*,Data.number)-Data.base,font=0, yticks=4,  $
xmargin=[5,1], ymargin=[2,2], tit=Modestring+',  '+Base_Time, $
		xminor=4, yminor=4, xstyle=1
Scale,temp,/mem	&	SC.Dif=temp
		endif
return
	ENDIF

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

window_set,ID.Win(2),scale=Sc.Scan

p=(convert_coord(ev.x, ev.y, /TO_DATA, /DEVICE))([0,1])
p(0)=p(0) < ((size(Stokes))(1)-1) > 0
WIDGET_CONTROL,ID.Valuelabel(0),set_val='Channel '+strtrim(fix(p(0)+1),2)+ $
	',  value = '+strtrim(fix(p(1)),2)

		IF ev.press THEN BEGIN

	CASE 1 OF

Flux.Model(p(0)) eq 0:	begin
			Flux.min=p(1)
			end

ELSE:			begin
			Flux.Factor=(p(1)-Flux.min)/(Flux.Model(p(0)))
			end
	ENDCASE

Current_Time=time_outvalue(Data.Number, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)

plot,X,Stokes(*,Data.number),font=0, yticks=4, xmargin=[5,1], ymargin=[2,2], $
	tit=' Scan  '+strtrim(Data.number,2)+',  '+Current_Time, $
		xminor=4, yminor=4, xstyle=1
oplot,X,Stokes(*,Data.number)-Stokes(*,Data.number-1),/noc

oplot,Flux.model*Flux.factor+Flux.min,col=Ini.colors(5)

Scale,temp,/mem	&	SC.Scan=temp


		ENDIF


return
	ENDIF

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

window_set,ID.Win(3),scale=Sc.Dif

p=(convert_coord(ev.x, ev.y, /TO_DATA, /DEVICE))([0,1])
p(0)=p(0) < ((size(Stokes))(1)-1) > 0
WIDGET_CONTROL,ID.Valuelabel(1),set_val='Channel '+strtrim(fix(p(0)+1),2)+ $
	',  value ='+strtrim(fix(p(1)),2)

		if ev.press then begin

Width_process:

WIDGET_CONTROL,/HOUR

Current_Time=time_outvalue(Data.number, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)

Dif_scan=Stokes(*,Data.number)-Data.base-Data.Line

peak=select_peak(Dif_scan,p(0))

	CASE Data.Method OF

'Full':	begin

fwhm_value=fwhm(X,Dif_scan, /follow,x_peak=p(0)+1)
area=[p(0)-fwhm_value*0.8 > 0 < peak(0), $
	p(0)+fwhm_value*0.8 < (n_elements(Dif_scan)-1) > peak(1)]
Sc_max=max(Dif_scan(area(0):area(1)),N_max)
N_max=N_max+area(0)
peak=[N_max-fwhm_value*0.8 > 0 < peak(0), $
	N_max+fwhm_value*0.8 < (n_elements(Dif_scan)-1)  > peak(1)]

	end

ELSE:	begin

fwhm_value=fwhm(X(peak(0):peak(1)),Dif_scan(peak(0):peak(1)))

	end

	ENDCASE


	if Data.interf eq 'E-W' then begin
Beam_chan=par.beamEWchan		&	Beam=par.beamEW
Spacing=par.SpacingEW		&	Tmod=par.TmodEW
	endif else begin
Beam_chan=par.beamSNchan		&	Beam=par.beamSN
Spacing=par.SpacingSN			&	Tmod=par.TmodSN
	endelse

Width=2*(fwhm_value > Beam_chan(1))
borders=[fix(peak(0)-Width) > 0,fix(peak(1)+Width+0.5) < (Data.N_channels-1)]

Scan_max=max(Stokes(*,Data.number)-Data.Base,min=Scan_min)

Base_Time=time_outvalue(Data.Base_scan, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)
Modestring='Sc '+strtrim(Data.number,2)+' - Sc '+strtrim(Data.Base_scan,2)

wset,ID.Win(3)
plot,X,Stokes(*,Data.number)-Data.Base,font=0, yticks=4, xmargin=[5,1], $
	ymargin=[2,2], tit=Modestring+',  '+Base_Time, xminor=4, yminor=4, xstyle=1

Scale,temp,/mem	&	SC.Dif=temp

for j=0,1 do plots, [1,1]*borders(j)+1,[Scan_min < 0,Scan_max],linestyle=1*Data.M

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

full=(max(Flux.chan) > (Data.N_channels-1))-(min(Flux.chan) < 0)+1
X_model=findgen(full)+(min(Flux.chan) < 0)+1

order=ord_recognize(Data.number,Flux.Nord,Flux.Ord,Flux.Chan)
index=(where(order eq Flux.ord))(0)

Z_model=sqrt(1-((X_model-Flux.Chan(1,index))/	$
	((Flux.Chan(2,index)-Flux.Chan(0,index))/2))^2 > 0)

Flux.weight=120./(total(Z_model)*Flux.factor)

peak=select_peak(Dif_scan,p(0))

Flux_value=total(Dif_scan(borders(0):borders(1)) > 0)*Flux.weight

WIDGET_CONTROL,ID.Infolabel3,set_val='Flux = '+	$
	strtrim(string(Flux_value,format='(f6.1)'),2)


wset,ID.Win(4)

N=borders(1)-borders(0)+1
xx=indgen(N)+borders(0)
y=Dif_scan(borders(0):borders(1))

s=10.	&   T=findgen(N*s)/s+borders(0)

peak_area=[peak(0)-borders(0), peak(1)-borders(0)]*s

ys=SPLINE(xx,Y,T)
ysmax=max(ys(peak_area(0):peak_area(1)),imax,min=ysmin)

plot_stick,X,Dif_scan, xran=borders, yran=[ysmin < 0, ysmax*1.1 > 0], $
	tit=Current_Time, subt='Base scan: '+Base_Time, $
	font=0, xmargin=[5,1.5], ymargin=[4.5,2], $
	xminor=4, yminor=4, xticks=3,  yticks=4, col=200

Scale,temp,/mem	&	SC.Show=temp

oplot,T+1,ys,lines=0*Data.M,clip=[borders(0),ysmin < 0,borders(1),ysmax*1.1]

width=fwhm(T(Peak_area(0):Peak_area(1)),ys(Peak_area(0):Peak_area(1)), $
	x_peak=T(0)+peak_area(0)/s+imax/s, follow=(Data.Method eq 'Full'))

width0=sqrt((width^2-Beam_chan^2) > 0)

peak_area1=[borders(0)-3*width > 0,borders(1)+3*width < (Data.N_channels-1)]

z=(sinc((0.885892*!dpi/2)*((T-borders(0))*s-(imax+peak_area(0)))/ $
(Beam_chan(1)*s/2)))^2*ysmax

oplot,T+1,z,lines=1*Data.M,clip=[borders(0),ysmin < 0,borders(1),ysmax*1.1],color=100
	Rads=!DPi/180/3600
source=strtrim(string(width0(1)/Beam_chan(0)*Beam(0)/Rads,format='(F5.1)'),2)+'" = '

window_set,ID.Win(3),scale=SC.Dif
for j=0,1 do plots, [1,1]*peak_area1(j)+1,[Scan_min/2,Scan_max/2],linestyle=2*Data.M, $
	color=100

sigma=stdev([(Stokes(*,Data.number)-Data.Base)(0:peak_area1(0)), $
	(Stokes(*,Data.number)-Data.Base)(peak_area1(1):*)])

WIDGET_CONTROL,ID.InfoLabel1, set_val= $
	'Source = '+source+strtrim(string(width0(1),format='(F5.1)'),2)+' chan.'
WIDGET_CONTROL,ID.InfoLabel2, set_val= $
	'Sigma = '+strtrim(string(sigma,format='(F7.2)'),2)

Results={X:X, Dif_scan:Dif_scan,borders:borders,Current_time:Current_time, $
Base_time:Base_time,s:s,T:T,ys:ys,ysmax:ysmax,ysmin:ysmin,  $
imax:T(0)+peak_area(0)/s+imax/s, $
z:z,Beam:Beam, Beam_chan:Beam_chan, Spacing:Spacing, Tmod:Tmod, $
Width:Width, Width0:Width0, peak:peak,sigma:sigma}

		endif
return
	ENDIF


	IF ev.id eq ID.View(4) THEN BEGIN
window_set,ID.Win(4),scale=Sc.Show

p=(convert_coord(ev.x, ev.y, /TO_DATA, /DEVICE))([0,1])
p(0)=p(0) < ((size(Stokes))(1)-1) > 0
WIDGET_CONTROL,ID.Valuelabel(1),set_val='Channel '+strtrim(fix(p(0)+1),2)+ $
	',  value ='+strtrim(fix(p(1)),2)
		if ev.press then begin
window_set,ID.Win(3),scale=Sc.Dif
goto, Width_process
		endif
	ENDIF


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



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



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

	CASE wuv OF

"DONE"	:  	WIDGET_CONTROL,ev.top,/DEST

"XMTool":	begin
		WIDGET_CONTROL,/hourglass
		XMTool,group=ev.top
		end

"Xloadct":	begin
		WIDGET_CONTROL,/hourglass
		Xloadct,group=ev.top
		end


"Calculator":	begin
		WIDGET_CONTROL,/hourglass
		wcalc
		end

"Help"	:	begin
		WIDGET_CONTROL,/hourglass

	CASE !version.OS OF

'windows':	Delim='\'

'Win32':	Delim='\'

ELSE:		Delim='/'

	ENDCASE


xtext,file=getenv('help_dir')+Delim+'source_s.hlp',group=ev.top
		end

"VC":	spawn,'vc'

"NC":	spawn,'nc'

"DOS"	:	spawn

"Archiver":	begin
		WIDGET_CONTROL,/hourglass
		pushd,getenv('spk_dat')
		spawn,'rar'
		popd
		end

"Parameters":	begin
		WIDGET_CONTROL,/hourglass
		param_ssrt,time=Moment.time, Date=Moment.Date,Rec=Rec,group=ev.top
		end

"Base":		begin
Data.Base_scan=Data.number
Data.Base=Data.Base+Stokes(*,Data.Base_scan)
Modestring='Sc '+strtrim(Data.number,2)+' - Sc '+strtrim(Data.Base_scan,2)
Time=time_outvalue(Data.Base_scan, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)

wset,ID.Win(3)
plot,X,Stokes(*,Data.number)-Data.Base,font=0, yticks=4, xmargin=[5,1],  $
	ymargin=[2,2], tit=Modestring+',  '+Time, xminor=4, yminor=4, xstyle=1

Scale,temp,/mem	&	SC.Dif=temp
		end

"Nothing":	begin
Data.Line=(Data.Base=fltarr(Data.N_channels))
Modestring='Sc '+strtrim(Data.number,2)
Time=time_outvalue(Data.Base_scan, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)

wset,ID.Win(3)
plot,X,Stokes(*,Data.number)-Data.Base,font=0, yticks=4, xmargin=[5,1],  $
	ymargin=[2,2], tit=Modestring+',  '+Time, xminor=4, yminor=4, xstyle=1

Scale,temp,/mem	&	SC.Dif=temp
		end

"Level":	begin

if n_tags(Results) gt 2 then peak=Results.peak else begin
	tmp=Stokes(*,Data.number)
	amax=max(tmp,imax)
	peak=select_peak(tmp,imax)
endelse

level=Stokes(peak(0),Data.number) < Stokes(peak(1),Data.number)

Data.Base=Data.Base+level
Modestring='Sc '+strtrim(Data.number,2)
Time=time_outvalue(Data.Base_scan, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)

wset,ID.Win(3)
plot,X,Stokes(*,Data.number)-Data.Base,font=0, yticks=4, xmargin=[5,1],  $
	ymargin=[2,2], tit=Modestring+',  '+Time, xminor=4, yminor=4, xstyle=1

Scale,temp,/mem	&	SC.Dif=temp
		end

"Line":	begin

if n_tags(Results) gt 2 then peak=Results.peak else begin
	tmp=Stokes(*,Data.number)
	amax=max(tmp,imax)
	peak=select_peak(tmp,imax)
endelse

k=(Stokes(peak(1),Data.number)-Data.Base(peak(1))- $
	(Stokes(peak(0),Data.number)-Data.Base(peak(0))))/(peak(1)-peak(0))

Data.Line=Stokes(peak(0),Data.number)-Data.Base(peak(0))+k*(X-peak(0))

Modestring='Sc '+strtrim(Data.number,2)
Time=time_outvalue(Data.Base_scan, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)

wset,ID.Win(3)
plot,X,Stokes(*,Data.number)-Data.Base,font=0, yticks=4, xmargin=[5,1],  $
	ymargin=[2,2], tit=Modestring+',  '+Time, xminor=4, yminor=4, xstyle=1

oplot,data.line,lines=2*Data.M

Scale,temp,/mem	&	SC.Dif=temp
		end

"Zoom":		begin
	WIDGET_CONTROL,/hourglass
for j=0,1 do WIDGET_CONTROL,ID.Togglebase(j),map=j
wset,ID.Win(5)

Sz=Size(Results)

	IF Sz(n_elements(Sz)-2) ne 8L THEN BEGIN
xyouts,0.5,0.5,'Nothing to show',/norm,align=0.5,font=0
return
	ENDIF

plot_stick,Results.X,Results.Dif_scan, xran=Results.borders, $
	yran=[Results.ysmin < 0, Results.ysmax*1.1], /xst, $
	tit=Results.Current_Time, subt='Base scan: '+Results.Base_Time, $
	font=0, xmargin=[5,1.5], ymargin=[4.5,2], $
	xminor=4, yminor=4,  $ ;xticks=3,
	yticks=4, col=200

oplot, Results.T+1, Results.ys,lines=0*Data.M, $
clip=[Results.borders(0), Results.ysmin < 0, $
	Results.borders(1), Results.ysmax*1.1]

oplot, Results.T+1, Results.z,lines=1*Data.M,clip=[Results.borders(0), $
	Results.ysmin < 0, Results.borders(1),Results.ysmax*1.1],color=100

	Rads=!DPi/180/3600

Response=strtrim(string(Results.width/Results.Beam_chan(0)*Results.Beam(0)/Rads, $
format='(F5.1)'),2)+'" = '

WIDGET_CONTROL,ID.ZoomLabel1,set_val=$
	'Response size = '+Response+strtrim(string(Results.width,format='(F5.1)'),2)+ $
	' channels;  Beam = '+strtrim(string(Results.Beam(1)/Rads,format='(F5.1)'),2)+ $
	'" = '+strtrim(string(Results.Beam_chan(1),format='(F5.1)'),2)+' channels'

Source=strtrim(string(Results.width0(1)/Results.Beam_chan(0)*Results.Beam(0)/Rads, $
format='(F5.1)'),2)+'" = '

WIDGET_CONTROL,ID.ZoomLabel2,set_val=$
	'Source size = '+Source+strtrim(string(Results.width0(1),format='(F5.1)'),2)+ $
	' channels;  Sigma (ampl.) = '+strtrim(string(Results.sigma,format='(F7.2)'),2)

		end

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

"Local":		begin
		Data.Method='Local'
WIDGET_CONTROL,ID.FWHM_Label,set_val='FWHM: '+Data.Method
		end

"Full":		begin
		Data.Method='Full'
WIDGET_CONTROL,ID.FWHM_Label,set_val='FWHM: '+Data.Method
		end

"Results":	begin
		source_size_res
		WIDGET_CONTROL,/hourglass
		xtext,file=Fileres
		end

"Zero":		begin
WIDGET_CONTROL,ID.Zero,get_val=tmp
tmp=strtrim(tmp(0),2)
Flux.min=float(tmp)
WIDGET_CONTROL,ID.Zero,set_val=tmp
		end

"Lshift":	begin
Flux.Model=Shift(Flux.Model,-1)
Current_Time=time_outvalue(Data.Number, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)

wset,ID.Win(2)
plot,X,Stokes(*,Data.number),font=0, yticks=4, xmargin=[5,1], ymargin=[2,2], $
	tit=' Scan  '+strtrim(Data.number,2)+',  '+Current_Time, $
		xminor=4, yminor=4, xstyle=1
oplot,X,Stokes(*,Data.number)-Stokes(*,Data.number-1),/noc

oplot,Flux.model*Flux.factor+Flux.min,col=Ini.colors(5)

Scale,temp,/mem	&	SC.Scan=temp

		end

"Rshift":	begin
Flux.Model=Shift(Flux.Model,1)

Current_Time=time_outvalue(Data.Number, time=time_str_to_sec(Moment.time),  $
	Dt=Data.Dt*Data.multi)

wset,ID.Win(2)
plot,X,Stokes(*,Data.number),font=0, yticks=4, xmargin=[5,1], ymargin=[2,2], $
	tit=' Scan  '+strtrim(Data.number,2)+',  '+Current_Time, $
		xminor=4, yminor=4, xstyle=1
oplot,X,Stokes(*,Data.number)-Stokes(*,Data.number-1),/noc

oplot,Flux.model*Flux.factor+Flux.min,col=Ini.colors(5)

Scale,temp,/mem	&	SC.Scan=temp

		end


ELSE:
	ENDCASE

empty

	end


pro source_size,output, group_leader=group_leader,Date=Date, $
time=time,Receiver=Receiver,input=input,Dt=Dt,multi=multi, $
interferometer=interferometer

Common Exch_source_size,ID,SC,Moment,Data,Ini,Rec,Stokes,SSRT, $
	SUN,Par,P_save,Results,Fileres,Flux



if xregistered('source_size') then return


if n_elements(Dt) le 0 then Dt=0.056d0
if n_elements(multi) le 0 then multi=1
if n_elements(interferometer) le 0 then interferometer='E-W'
if n_elements(group_leader) le 0 then group_leader = 0L

Sum_chan=[176,192]
Rads=!DPi/180d0/3600d0

ID={View:Lonarr(6), Win:Lonarr(6), Label:0L, ToggleBase:[0L,0L], $
Leftbase:[0L,0L], ZoomLabel1:0L , ZoomLabel2:0L , Timelabel:0L, $
InfoLabel1:0L, InfoLabel2:0L, group_leader:group_leader, $
ValueLabel:[0L,0L], FWHM_Label:0L, BaseBut:0L, SpikeBut:0L, $
InfoLabel3:0L, Zero:0L}

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

SC={Tv:Ax, Scan:Ax, Dif:Ax, Show:Ax}

	Ax=0

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

WIDGET_CONTROL,/hourglass
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)

N_channels=Sum_Chan(Receiver)

Data={Zoom:0, Press:0, Release:0, Mode:'Follow', Mouse:0, Optics:'', $
	N_channels:N_channels, Base: fltarr(N_channels), Line: fltarr(N_channels), $
	GridColor:255B-Ini.colors(8), GridType:'Heliographical', Dt:Dt, multi:multi, $
	interf:interferometer, Base_scan:0L, Spike_scan:-1L, number:0L, M:M(0), $
	Marker:0, Method:'Local'}

Stokes=input

Flux={min:float(min(Stokes > 0))}



if n_elements(group_leader) le 0 then group_leader = 0L

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(Receiver) le 0 then begin
Rec=0   &	read,'Receiver (0-MFB, 1-AOR) - ', Rec
	endif else Rec=Receiver

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


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

SSRT={Fi:51.7575d0*!DPi/180, F0:F0, Df:Df,	$
P:P, NoEW:NnEW, OEW:NordEW, CEW:ChanEW, $
Q:Q, NoSN:NnSN, OSN:NordSN, CSN:ChanSN}

source_size_model,time

dxsize=30.	&	dxe=25.
sz=(size(Stokes))([1,2])

	device,set_graphics_function=3
	device,get_scr=scr

Width0=(sz(0) > 150)+dxsize+dxe
Width1=(Width2=(scr(0)*0.95-Width0)/2.)
Gap=90.

	Xs=	[[[Width0,(scr(1)-Gap)/2.]#replicate(1,2)],  $
		[[Width1,(scr(1)-Gap)/2.3]#replicate(1,2)],  $
		[Width2,(scr(1)-Gap)*0.7], $
		[scr(0)*0.8,scr(1)*0.8]]

;***** Drawing widget

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

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

;***** Left Base

Wholebase=widget_base(ID.ToggleBase(0),/column)
Upperbase=widget_base(Wholebase,/row)

 XPdMenu,	['"DONE"			DONE',	$

		'"Tools"	{',			$
			'"Zoom"			Zoom',		$
			'"FWHM Method" {',			$
				'"Local peak"	Local',		$
				'"Full"		Full','}',		$
			'"Calculator"		Calculator', 	$
			'"Parameters"		Parameters', 	$
			'"Results"	Results',		$
			'"Color table"		Xloadct',	$
			'"XManager Tool"	XMTool',	$
			'"DOS"			DOS', 		$
			'"Norton Commander"	{','"NC"	NC',	$
						'"VC"	VC','}',	$
			'"Archiver"	Archiver','}',		$

		'"Subtract" {',					$
			'"Base scan"	Base',			$
			'"Line"		Line',			$
			'"Level"		Level',			$
			'"Nothing"	Nothing','}',		$

		'"Help"			Help'], Upperbase

R_shift = 	[				$
		[000B, 000B],			$
		[000B, 000B],			$
		[016B, 000B],			$
		[032B, 000B],			$
		[064B, 000B],			$
		[128B, 000B],			$
		[000B, 001B],			$
		[000B, 002B],			$
		[000B, 004B],			$
		[000B, 002B],			$
		[000B, 001B],			$
		[128B, 000B],			$
		[064B, 000B],			$
		[032B, 000B],			$
		[016B, 000B],			$
		[000B, 000B]			$
		]


L_shift = 	[				$
		[000B, 000B],			$
		[000B, 016B],			$
		[000B, 008B],			$
		[000B, 004B],			$
		[000B, 002B],			$
		[000B, 001B],			$
		[128B, 000B],			$
		[064B, 000B],			$
		[032B, 000B],			$
		[064B, 000B],			$
		[128B, 000B],			$
		[000B, 001B],			$
		[000B, 002B],			$
		[000B, 004B],			$
		[000B, 008B],			$
		[000B, 000B]			$
		]

Emptystring=string(replicate('20'xb,20))

Interf_label=WIDGET_LABEL(Upperbase, /fra,val=' '+interferometer+' ')
Shift_base=WIDGET_BASE(Upperbase,/row)
Shift_button0=WIDGET_BUTTON(Shift_base,val=L_shift,uval='Lshift')
Shift_button1=WIDGET_BUTTON(Shift_base,val=R_shift,uval='Rshift')
Zero_base=WIDGET_BASE(Upperbase,/row,/fra)
Zero_label=WIDGET_LABEL(Zero_base,val=' Zero: ')
ID.Zero=WIDGET_TEXT(Zero_base, val=string(Flux.min,format='(f8.1)'),/edit,uval='Zero',xsiz=10)

Savebase=WIDGET_BASE(Upperbase,/row,/nonexcl,/fra)
But=WIDGET_BUTTON(Savebase,val='Save results',uval='Save')

if strmid(!version.release, 0, 1) lt 5 then $
ID.Timelabel=WIDGET_LABEL(Upperbase, val=Emptystring+Emptystring+Emptystring) else $
ID.Timelabel=WIDGET_LABEL(Upperbase, val=Emptystring+Emptystring+Emptystring, /dynam)

LeftTogglebase=widget_base(Wholebase)

for j=0,1 do ID.Leftbase(j)=widget_base(LeftTogglebase,/row)

viewbase0=WIDGET_BASE(ID.Leftbase(0), /colu)

	for j=0,1 do begin
ID.view(j)=WIDGET_DRAW(viewbase0, XS=Xs(0,j), YS=Xs(1,j), /motion, /button_events)
	endfor

viewbase1=WIDGET_BASE(ID.Leftbase(0), /colu)

	for j=2,3 do begin
ID.view(j)=WIDGET_DRAW(viewbase1, XS=Xs(0,j), YS=Xs(1,j), /motion, /button_events)
if strmid(!version.release, 0, 1) lt 5 then $
ID.ValueLabel(j-2)=WIDGET_LABEL(viewbase1, val='       ' ,/frame) else $
ID.ValueLabel(j-2)=WIDGET_LABEL(viewbase1, val='       ' ,/frame, /dynam)
	endfor

viewbase2=WIDGET_BASE(ID.Leftbase(0), /colu)

ID.FWHM_Label=WIDGET_LABEL(viewbase2,val='FWHM: '+Data.Method)

ID.view(4)=WIDGET_DRAW(viewbase2, XS=Xs(0,4), YS=Xs(1,4), /motion, /button_events,/frame)


;	INFO:

	if Data.interf eq 'E-W' then begin
Beam=Par.BeamEW(1)/Rads
Spacing=par.SpacingEW/Rads
	endif else begin
Beam=Par.BeamSN(1)/Rads
Spacing=par.SpacingSN/Rads
	endelse

infobase=WIDGET_BASE(viewbase2,/colu,/frame)
label=WIDGET_LABEL(infobase,val='Beam =    '+string(Beam,format='(F5.1)')+'"')
label=WIDGET_LABEL(infobase,val='Spacing = '+string(Spacing,format='(F5.1)')+'"')
ID.InfoLabel1=WIDGET_LABEL(infobase,val='Source size = '+emptystring)
ID.InfoLabel2=WIDGET_LABEL(infobase,val='Sigma = '+emptystring)
ID.InfoLabel3=WIDGET_LABEL(infobase,val='Flux = '+emptystring)

ZoomBase=WIDGET_BASE(ID.ToggleBase(1),/row)
junk=WIDGET_BASE(ZoomBase,/colu)
junk1=WIDGET_BUTTON(junk,VAL='QUIT',uval='QuitZoom')
junk=WIDGET_BASE(ZoomBase,/colu)
ID.view(5)=WIDGET_DRAW(junk, XS=Xs(0,5), YS=Xs(1,5), /motion, /button_events)

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

ID.ZoomLabel1=WIDGET_LABEL(junk,val=Emptystring)
ID.ZoomLabel2=WIDGET_LABEL(junk,val=Emptystring)

	endif else begin

	if strmid(!version.release, 0, 1) lt 5 then begin
ID.ZoomLabel1=WIDGET_LABEL(junk,val=Emptystring)
ID.ZoomLabel2=WIDGET_LABEL(junk,val=Emptystring)
	endif else begin
ID.ZoomLabel1=WIDGET_LABEL(junk,val=Emptystring, /dynam)
ID.ZoomLabel2=WIDGET_LABEL(junk,val=Emptystring, /dynam)
	endelse

	endelse

;*****

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
	endfor

WIDGET_CONTROL,/hour

circ
source_size_draw_win

xmanager,'Source_size',Mainbase,group=group_leader, cleanup='source_size_c_u'

end