	pro l_r_proc_model

common l_r_proc, Data,array_I,array_V,header_I,header_V,Sc,x,y,Flux,scan_I,scan_V,	$
	record_I,record_V,par,sun

Rec=Data.N_channels eq 192

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

z=x(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:1.}

	end




	pro l_r_proc_event,ev

common l_r_proc, Data,array_I,array_V,header_I,header_V,Sc,x,y,Flux,scan_I,scan_V,	$
	record_I,record_V,par,sun


	for j=0,5 do if ev.id eq Data.View(j) then begin
if Data.lun_I eq 0 then return
if ev.press then Data.press=1
if ev.release then Data.press=0
	endif


if ev.id ne Data.View(0) or Data.mode ne 'Select channels' then device,/cursor_cross


	if ev.id eq Data.View(1) then begin

window_set,Data.Win(1),sc=Sc.W(1)
p=(convert_coord(ev.x,ev.y,/dev,/to_data))([0,1])
widget_control,Data.View_Label(1),set_val=	$
	string(p(0),p(1),format="(f8.1,',  ',f8.1)")


	IF ev.press THEN BEGIN

	if Data.Calibrate then begin

		if Flux.Model(p(0)) eq 0 then begin

if Data.Last eq 0 then Flux.min=p(1)+Flux.min else Flux.min=p(1)

widget_control,Data.Zero,set_val=strtrim(string(p(1),format="(f8.1)"),2)

     		endif else begin

if Data.Last eq 0 then Flux.Factor=p(1)/(Flux.Model(p(0))) else $

Flux.Factor=(p(1)-Flux.min)/(Flux.Model(p(0)))

		endelse


if Data.Object eq 'Integrated' then Object=scan_I else Object=x(Data.Number)

plot,indgen(Data.N_channels)+1,Object > Data.threshold, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min

; channel+1 !!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem
Sc.W(1)=tmp


Data.Last=1

	endif

empty

	ENDIF

	return
	endif



	for j=2,3 do if ev.id eq Data.View(j) then begin

window_set,Data.Win(j),sc=Sc.W(j)
p=(convert_coord(ev.x,ev.y,/dev,/to_data))([0,1])
widget_control,Data.View_Label(j),set_val=	$
	string(p(0),p(1),format="(f8.1,',  ',f8.1)")

	return
	endif

	for j=4,5 do if ev.id eq Data.View(j) then begin

window_set,Data.Win(j),sc=Sc.W(j)
p=(convert_coord(ev.x,ev.y,/dev,/to_data))([0,1])

widget_control,Data.View_Label(j),set_val=	$
	string(p(0),p(1),format="(f8.1,',  ',f8.1)")+',  '+	$
time_outvalue(p(j eq 5), time=hms(Data.start_time)*3600d0, Dt=Data.Dt)

	return
	endif



	if ev.id eq Data.View(0) then begin

Data.Number=ev.y*Data.factor < (Data.Length-1)

widget_control,Data.View_Label(0),set_val=	$
	string(ev.x+1 > 0 < Data.N_channels,Data.Number,format="(f8.1,',  ',f8.1)")+',  '+	$
		time_outvalue(Data.Number, time=hms(Data.start_time)*3600d0, Dt=Data.Dt)

wset,Data.Win(0)

		if Data.Mode eq 'Int_scan' then begin
			if Data.press then begin

if abs(Data.bounds(1)/Data.factor-ev.y) lt abs(Data.bounds(0)/Data.factor-ev.y) $
	then ind=1 else ind=0

draw_marker,[195,Data.bounds(ind)/Data.factor],col=!P.Background, 0.8, /left, /fill, /dev
draw_marker,[195,ev.y > 0 < (Data.Length-1)/Data.factor],col=!P.color, 0.8, /left, /fill, /dev

Data.bounds(ind)=ev.y*Data.factor > 0 < (Data.Length-1)

widget_control,Data.Ready,sens=1
widget_control,Data.info,set_val='After marking, press "Ready"'
			endif

	return
		endif

		if Data.Mode eq 'Select channels' then begin


	tmp=Data.a
w_box_cursor,ev,xy,init=Data.init,cur=tmp
	Data.a=tmp
Data.init=0
Data.xy=xy
Data.channel_bounds=xy(*,0)
	return
		endif


		if ev.release and (Data.Object eq 'Single') then begin

Data.Last=0

for j=0,1 do widget_control,Data.Right_base(j),map=1-j

wset,Data.Win(1)


Object=(x(Data.Number) > Data.threshold-Flux.min)*Flux.weight

plot,indgen(Data.N_channels)+1,Object, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]*Flux.weight)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor*Flux.weight

; channel+1 !!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem
Sc.W(1)=tmp
empty
		endif

return
	endif


WIDGET_CONTROL,ev.id,GET_UVALUE = uv

CASE uv OF

"Done":		begin
WIDGET_CONTROL,ev.top,/DESTROY
	if Data.lun_I ne 0 then free_lun,Data.lun_I
	if Data.lun_V ne 0 then free_lun,Data.lun_V
if Data.group ne 0L then if widget_info(Data.group,/valid) then $
	widget_control,Data.group,/show
!P=Data.P_save
		end

"Xloadct":	begin
	widget_control,/hour
	Xloadct
		end

"Wcalc":	begin
	widget_control,/hour
	Wcalc
		end


"Parameters":		begin
	if Data.lun_I eq 0 then return
	widget_control,/hour
param_ssrt, Data.date, Data.Reference_time, Data.N_channels eq 192, par=par,sun=sun
			end


"Threshold":	begin
WIDGET_CONTROL,Data.thres,get_val=a,/hour
Data.threshold=float(a(0))
WIDGET_CONTROL,Data.thres,set_val=strtrim(string(Data.threshold,format='(f9.1)'),2)

	if Data.Lun_I eq 0 then return

wset,Data.Win(1)

if Data.Object eq 'Integrated' then Object=scan_I else Object=x(Data.Number)

plot,indgen(Data.N_channels)+1,Object > Data.threshold, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min


empty
		end


"Unzoom":	begin

for j=0,1 do widget_control,Data.Zoom_Base(j),map=1-j,/hourglass

Data.Zoom=0

	if Data.Lun_I eq 0 then return

wset,Data.Win(1)

if Data.Object eq 'Integrated' then Object=scan_I else Object=x(Data.Number)

plot,indgen(Data.N_channels)+1,Object > Data.threshold, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min

; channel+1 !!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem
Sc.W(1)=tmp

empty

		end


"Zoom":	begin

for j=0,1 do widget_control,Data.Zoom_Base(j),map=j,/hourglass

Data.Zoom=1

	if Data.Lun_I eq 0 then return

wset,Data.Win(1)

if Data.Object eq 'Integrated' then Object=scan_I else Object=x(Data.Number)

plot,indgen(Data.N_channels)+1,Object > Data.threshold, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min

; channel+1 !!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem
Sc.W(1)=tmp

empty

		end


'Preview':		Data.Mode='Preview'

'Process':		begin

Data.Mode='Int_scan'

Number=(hms(Data.reference_time)-hms(Data.start_time))*3600d0/Data.Dt

	Case 1 OF

Number gt 0 and Number lt Data.Length:	$
	Data.bounds=[-127,127]+Number > 0 < (Data.Length-1)

ELSE:	Data.bounds=[-127,127]+Data.Length/2 > 0 < (Data.Length-1)

	ENDCASE

wset,Data.Win(0)

draw_marker,[195,Data.bounds(0)/Data.factor],col=!P.color, 0.8, /left, /fill, /dev
draw_marker,[195,Data.bounds(1)/Data.factor],col=!P.color, 0.8, /left, /fill, /dev

widget_control,Data.info,set_val='Mark fragment to build integrated scan.',/hour


			end

"_3":		Data.Width=3
"_5":		Data.Width=5
"_7":		Data.Width=7
"_9":		Data.Width=9
"_11":	Data.Width=11
"_13":	Data.Width=13
"_15":	Data.Width=15
"_17":	Data.Width=17


"Zero":		begin
	widget_control,Data.Zero,get_val=tmp,/hour
	Flux.min=tmp(0)
widget_control,Data.Zero,set_val=strtrim(string(Flux.min,format="(f8.1)"),2)
		if Data.lun_I eq 0 then return

		Data.Object='Integrated'

wset,Data.Win(1)
plot,indgen(Data.N_channels)+1,scan_I, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min

; channel+1 !!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem
Sc.W(1)=tmp
empty
		end

"Calibrate":	begin
Data.Calibrate=1
for j=0,1 do widget_control,Data.Calib_base(j), map=1-j
		end

"OK":		begin

if Data.lun_I eq 0 then return

Data.Calibrate=0

for j=0,1 do widget_control,Data.Calib_base(j), map=j,/hour

for j=0,1 do widget_control,Data.Right_base(j),map=j


widget_control,Data.info,set_val='Press "Calibration" or "Results".'

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

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.Channel,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)

Recog=min(scan_I-Flux.min)

if (Recog lt 0) and (abs(Recog) gt max(scan_I-Flux.min)*0.01) then $
	scan_I=scan_I > ((-max(scan_I-Flux.min)*0.01)+Flux.min)

if Data.Object eq 'Integrated' then Object=scan_I else Object=x(Data.Number)

wset,Data.Win(1)

plot,indgen(Data.N_channels)+1,(Object-Flux.min)*Flux.weight, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,ytit='sfu'

oplot,indgen(Data.N_channels)+1,(Flux.model*Flux.factor)*Flux.weight

; channel+1 !!!!!!!!!!!!!!!!!!!

Data.Object='Single'

Scale,tmp,/mem
Sc.W(1)=tmp

wset,Data.Win(2)

plot,indgen(Data.N_channels)+1,(scan_I > Data.threshold -Flux.min )*Flux.weight,	$
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst
oplot,indgen(Data.N_channels)+1,scan_V*Flux.weight*10,/noclip

;  channel + 1 !!!!!!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem	&	Sc.W(2)=tmp

wset,Data.win(0)

half_width=fix(2*par.BeamEWchan(1)+0.5)

Data.width=2*half_width

Data.channel_bounds=Data.channel+[-1,1]*half_width



xy=[[Data.channel_bounds],[0,Data.win_size-1]]


	tmp=Data.a
w_box_cursor,[0,0],xy,init=Data.init,cur=tmp,/put
	Data.a=tmp
Data.init=0
Data.xy=xy

empty

widget_control,Data.info,set_val='Mark channels of interest and press "Ready".'


		end

"Results":	for j=0,1 do widget_control,Data.Right_base(j),map=j

"Calibration":	begin

	for j=0,1 do begin
widget_control,Data.Right_base(j),map=1-j
;widget_control,Data.Calib_base(j), sens=1
	endfor

widget_control,Data.info,set_val='After calibration, press "OK".'
		end

"Integr.":	begin

for j=0,1 do widget_control,Data.Toggle_base(j), map=1-j

widget_control,Data.Toggle_button(0),set_but=1-ev.select

		if Data.lun_I eq 0 then return

if ev.select then Data.Object='Integrated' else Data.Object='Single'

wset,Data.Win(1)
plot,indgen(Data.N_channels)+1,scan_I, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min

; channel+1 !!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem
Sc.W(1)=tmp
empty

		end


"Single":	begin

widget_control,Data.Toggle_button(1),set_but=1-ev.select

for j=0,1 do widget_control,Data.Toggle_base(j), map=j

	if Data.lun_I eq 0 then return

if ev.select then Data.Object='Single' else Data.Object='Integrated'

wset,Data.Win(1)
plot,indgen(Data.N_channels)+1,x(Data.Number) > Data.threshold, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min

; channel+1 !!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem
Sc.W(1)=tmp
empty
		end


"Lshift":	begin
	if Data.lun_I eq 0 then return
Flux.Model=Shift(Flux.Model,-1)
wset,Data.Win(1)

if Data.Object eq 'Integrated' then Object=scan_I else Object=x(Data.Number)

plot,indgen(Data.N_channels)+1,Object > Data.threshold, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min

; channel+1 !!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem
Sc.W(1)=tmp
empty

		end

"Rshift":	begin
	if Data.lun_I eq 0 then return
Flux.Model=Shift(Flux.Model,1)
wset,Data.Win(1)

if Data.Object eq 'Integrated' then Object=scan_I else Object=x(Data.Number)

plot,indgen(Data.N_channels)+1,Object > Data.threshold, $
	xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min

; channel+1 !!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem
Sc.W(1)=tmp
empty
		end


"Open":		begin

widget_control,/hour

File_I=pickfile(/read,path=getenv('spk_dat'),filt='*.awi *.ani')
	if File_I eq '' then return

widget_control,Data.Ready,sens=0

	for j=0,1 do widget_control,Data.Right_base(j),map=j,/hour

if Data.lun_I ne 0L then free_lun,Data.lun_I
if Data.lun_V ne 0L then free_lun,Data.lun_V

filter_V=(name_extract(File_I))(2)

strput,filter_V,'v',2

filter_V='.'+filter_V

	CASE !version.OS OF

'windows':	Delim='\'

'Win32':	Delim='\'

ELSE:		Delim='/'

	ENDCASE

File_V=subdir(File_I)+Delim+(name_extract(File_I))(1)+filter_V

openr,lun_I,File_I,/get_lun
Data.lun_I=lun_I

openr,lun_V,File_V,/get_lun
Data.lun_V=lun_V

widget_control,Data.info,set_val='Reading the file...'

	gr_header,Data.lun_I,offset_I,header_I,/read,$
	comments=comments_I,			$
	Parameter=Parameter_I, 			$
	Interferometer=Interferometer,		$
	source_file=source_file,		$
	first_record=first_record,		$
	Date=Date,	    			$
	Reference_time=Reference_time, 		$
	Reference_Channel=Channel,		$
	Start_time=Start_time,			$
	Receiver=Receiver,     			$
	Dt=Dt,  		       		$
	Length=Length, 				$
	N_channels=N_channels,			$
	Creator=Creator, 			$
	Array_size=Array_size,			$
	Type=Type

	gr_header,Data.lun_V,offset_V,header_V,/read,$
	comments=comments_V, Parameter=Parameter_V


	Data.Dt=Dt
	Data.Date=Date
	Data.Length=Length
	Data.N_channels=N_channels
	Data.Start_time=Start_time
	Data.Reference_time=Reference_time
	Data.Interf=Interferometer
	Data.Channel=Channel


param_ssrt, Data.date, Data.Reference_time, Data.N_channels eq 192, par=par, /si,sun=sun

Data.threshold=(-1000.)*(Data.N_channels ne 192)

x=assoc(Data.lun_I,	$
	make_array(N_channels, $
		type=array_size(n_elements(array_size)-2)),	$
			offset_I)

y=assoc(Data.lun_V,	$
	make_array(N_channels, $
		type=array_size(n_elements(array_size)-2)),	$
			offset_V)

wset,Data.Win(0)

Data.factor=Data.Length/Data.Win_size+1

Arr_length=Data.Win_size < Data.Length/Data.factor

Array_I=(Array_V=intarr(Data.N_channels,Arr_length))

t0=systime(1)	&	Flag=0

	for j=0,Arr_length-1 do begin

Array_I(*,j)=x(j*Data.factor) > Data.threshold
Array_V(*,j)=y(j*Data.factor)

	Delta_t=(systime(1)-t0) mod 1

	if Delta_t ge 0.2 and Delta_t lt 0.4 then Flag=1

	if ((Delta_t ge 0.5) and Flag) then begin
Flag=0
widget_control,Data.info,set_val=	$
	'Reading the file - '+string(float(j)/Arr_length*100,format='(i2,"%")')
	endif

	endfor

erase
tvscl,Array_I

empty

widget_control,Data.info,set_val='Press "Tools--Process" to build integrated scan.'


l_r_proc_model

	end


"Ready":	CASE Data.Mode OF

"Int_scan":	begin

	Data.Mode='Select channels'

	for j=0,1 do begin
widget_control,Data.Right_base(j), map=j
widget_control,Data.Calib_base(j), /sens
widget_control,Data.Toggle_button(j), /sens
widget_control,Data.OK_button,/sens
	endfor


widget_control,Data.info,set_val='Building integrated scan...',/hour

N_array=(scan_I=(scan_V=fltarr(Data.N_channels)))

N_array=N_array+1

Data.bounds=Data.bounds(sort(Data.bounds))

t0=systime(1)

Flag=0

	for j=Data.bounds(0),Data.bounds(1) do begin

index=where(x(j) gt 0)

	if index(0) ge 0 then begin
scan_I(index)=scan_I(index)*(1-1./N_array(index))+(x(j))(index)/N_array(index)
scan_V(index)=scan_V(index)*(1-1./N_array(index))+(y(j))(index)/N_array(index)
N_array(index)=N_array(index)+1
	endif

Delta_t=(systime(1)-t0) mod 1

	if Delta_t ge 0.2 and Delta_t lt 0.4 then Flag=1

	if ((Delta_t ge 0.5) and Flag) then begin
Flag=0
widget_control,Data.info,/hour,set_val=	$
	'Building integrated scan - '+	$
	string(float(j-Data.bounds(0))/(Data.bounds(1)-Data.bounds(0))*100,format='(i2,"%")')
	endif

	endfor

Wset,Data.Win(2)

plot,indgen(Data.N_channels)+1,scan_I,xmar=[6,3],ymar=[2,2],chars=0.8,/xst
oplot,indgen(Data.N_channels)+1,scan_V*10,/noclip

;  channel + 1 !!!!!!!!!!!!!!!!!!!!!!!

Scale,tmp,/mem	&	Sc.W(2)=tmp




widget_control,Data.info,set_val='Press "Calibration" to continue.'

empty

		end


"Select channels":	begin

widget_control,Data.info,set_val='Building time profile...',/hour

record_I=(record_V=intarr(Data.Channel_bounds(1)-Data.Channel_bounds(0)+1,Data.Length))

t0=systime(1)	&	Flag=0

	for j=0, Data.Length-1 do begin
record_I(*,j)=(x(j))(Data.Channel_bounds(0):Data.Channel_bounds(1))
record_V(*,j)=(y(j))(Data.Channel_bounds(0):Data.Channel_bounds(1))

	Delta_t=(systime(1)-t0) mod 1

	if Delta_t ge 0.2 and Delta_t lt 0.4 then Flag=1

	if ((Delta_t ge 0.5) and Flag) then begin
Flag=0
widget_control,Data.info,set_val=	$
	'Building time profile - '+string(float(j)/Data.length*100,format='(i2,"%")')
	endif

	endfor

wset,Data.Win(4)

plot_time,total((record_I-Flux.min >0)*Flux.weight,1),xmar=[6,3],ymar=[2,2],chars=0.8,/ynozero,	$
	dt=Data.dt, start=hms(Data.start_time)*3600d0,	$
		model='hh:mm:ss',xticks=3,xst=8
tt=total((record_I-Flux.min >0)*Flux.weight,1)
time1 = hms(Data.start_time)*3600d0+dindgen((size(record_I))(2))*Data.dt

help,time1
;print,size(record_I)

Dst=Data.start_time
dtt=data.dt
help,dst
print,dtt
	if Data.lun_I eq 0 then return
F_status=fstat(Data.lun_I)

Filenamem=F_status.name
Namem=name_extract(Filenamem)
;print,namem
nam=strmid(Namem(0),0,8)
;print,nam
save,tt,dtt,Dst,time1,filename='E:\Natasha\idl\moskal\data\'+Nam+'.sav'

axis,xaxis=1,/xst, chars=0.8

Scale,tmp,/mem	&	Sc.W(4)=tmp

Wset,Data.Win(5)

widget_control,Data.info,set_val='Building contour map...',/hour

contour,record_I,	$
	indgen(Data.Channel_bounds(1)-Data.Channel_bounds(0)+1)+Data.Channel_bounds(0)+1,	$
		indgen(Data.Length), 	$
			nlev=10,xmar=[6,3],ymar=[2,2],chars=0.8, /yst, /xst

Scale,tmp,/mem	&	Sc.W(5)=tmp

wset,Data.Win(1)

Object=scan_I

Data.Object="Integrated"

plot,indgen(Data.N_channels)+1,scan_I, xmar=[6,3],ymar=[2,2],chars=0.8,/xst,		$
	yran=([0,Flux.factor]+Flux.min)*Data.Zoom

oplot,indgen(Data.N_channels)+1,Flux.model*Flux.factor+Flux.min

; channel+1 !!!!!!!!!!!!!!!!!!!
widget_control,Data.info,set_val='Ready.'
Scale,tmp,/mem
Sc.W(1)=tmp
empty



			end

ELSE:

ENDCASE



"Header_I":	begin
	if Data.lun_I eq 0 then return
	widget_control,/hour
	xtext,text=header_I
		end

"Header_V":	begin
	if Data.lun_V eq 0 then return
	widget_control,/hour
	xtext,text=header_V
		end


"Save":		begin

	if Data.lun_I eq 0 then return

F_status=fstat(Data.lun_I)

	CASE !version.OS OF

'windows':	Delim='\'

'Win32':	Delim='\'

ELSE:		Delim='/'

	ENDCASE

Filename=F_status.name
Name=name_extract(Filename)
path=subdir(Filename)
model=strmid(Name(1),Name(4)-6,6)

	Interferometer=Data.Interf
if Interferometer eq 'E-W' then filter='*.wrs' else filter='*.nrs'

New_Name=newfilename(model=model,filt=filter,path=path)

File=pickfile(path=path,file=New_Name,filt=filter)

if File eq '' then return

if subdir(File) eq '' then File=path+Delim+File


openw,lun,File,/get_lun

widget_control,Data.info,set_val='Writing the file...'

comments=''

if Data.N_channels eq 176 then Receiver = 'FDAS' else Receiver = 'AOR'

Type='Results'

	gr_header,lun,offset,header,/write,version='220196',	$
	comments=comments,			$
	Parameter='Intensity + Polarization', 	$
	Interferometer=Interferometer,		$
	source_file=Name(0),			$
	first_record=0,				$
	Date=Data.Date,	    			$
	Reference_time=Data.Reference_time, 	$
	Reference_Channel=Data.Channel,		$
	Start_time=Data.Start_time,		$
	Receiver=Receiver,     			$
	Dt=Data.Dt,  		       		$
	Length=Data.Length, 			$
	N_channels=Data.N_channels,		$
	Creator='L_r_proc.pro',			$
	Array_size=Array_size,			$
	Type=Type,				$

	Channel_bounds=Data.channel_bounds,	$
	i_scan_bounds=Data.bounds,		$
	d_scan_bounds=Data.bounds,		$
	zero=Flux.min,				$
	factor=Flux.factor,			$
	weight=Flux.weight




point_lun,lun,Offset

d_scan_I=scan_I
d_scan_V=scan_V

writeu,lun,scan_I, scan_V, d_scan_I, d_scan_V,	$
	float((Flux.model*Flux.factor)*Flux.weight),	$
	record_I, record_V


free_lun,lun

widget_control,Data.info,set_val='Done.'
		end


ELSE:

ENDCASE




	end

	pro l_r_proc,group_leader=group_leader

common l_r_proc, Data,array_I,array_V,header_I,header_V,Sc,x,y,Flux,scan_I,scan_V,	$
	record_I,record_V,par,sun


if xregistered('l_r_proc') then return

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

N=6

device,get_scr=scr

Win_size=fix(scr(1)*0.8)


init_structure={w_b_state,	$
	x:0, y:0, press:0, first:1, Xc:[0.,0.], Yc:[0.,0.],	$
	Output:intarr(2,2), stretch:0., move:0.}


Data={view:lonarr(N), win:lonarr(N), prompt:0L, View_Label:lonarr(N), 	$
	lun_I:0L, lun_V:0L, group:group_leader,info:0L,Ready:0L,	$
	Win_size:Win_size, Right_base:[0L,0L], Zero:0L, OK_button:0L,	$
	factor:1, N_channels:192, Dt:0.056d0, Length:0L, P_save:!P,	$
	Start_time:'00:00:00.000', Reference_time:'00:00:00.000',	$
	Interf:'E-W', Number:0L, Date:'00 00 00', Object:'Single',	$
	Channel:0, Calib_base:lonarr(2), Calibrate:1, Width:5, Mode:'Preview',	$
	s:intarr(2,2), press:0, bounds:[0L,0L], Toggle_button:[0L,0L], 	$
	Toggle_base:[0L,0L],	$
	threshold:(-100.), thres:0L, xy:intarr(2,2), a:init_structure, init:1,	$
	channel_bounds:[0,0], Zoom:0, Zoom_Base:[0L,0L], Zoom_button:[0L,0L],	$
	Last:0}

!P.color=0
!P.background=!d.n_colors-1

fontsize=12

;if !version.OS eq 'windows' then font='arial*bold*'+strtrim(fontsize,2) else font=''
;if !version.OS eq 'windows' then font='system*'+strtrim(fontsize,2) else font=''

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

Sc={W:replicate(Ax,N)}

Ax=0

Flux={min:0.}

main_base=widget_base(/colu,group=group_leader,tit='Long record processing')

Upper_base=widget_base(main_base,/row)

 XPdMenu, ['"Done"				Done',	$

	   '"File"	{',				$
			'"Open"		Open',		$
			'"Header I"	Header_I',	$
			'"Header V"	Header_V',	$
			'"Save"		Save',		$
				'}',			$
	   '"Tools"	{',				$
			'"Colors"	Xloadct',	$
			'"Preview"	Preview',	$
			'"Process"	Process',	$
			'"Calculator"	Wcalc',		$
			'"Parameters"	Parameters',	$

				'}',			$

	   '"Help"				HELP'], $
		   Upper_base;,font=font

Emptystring='             '
Em=Emptystring+Emptystring+Emptystring

label_size=Em+Em

;if font ne '' then if fontsize le 16 then
label_size=label_size+Emptystring

Data.info=widget_label(Upper_base,val=label_size,/fra) ;,font=font)

Data.Ready=widget_button(Upper_base,val='Ready',uval='Ready') ;,font=font)
button=widget_button(Upper_base,val='Calibration',uval='Calibration') ;,font=font)
button=widget_button(Upper_base,val='Results',uval='Results') ;,font=font)

widget_control,Data.Ready,sens=0


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]	$
		]


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


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


Row_Base=widget_base(main_base,/row)


Left_base=widget_base(Row_Base,/colu)
Plain_base=widget_base(Row_Base)
for j=0,1 do Data.Right_base(j)=widget_base(Plain_base,colu=([1,2])(j))

xsize=192+20

Data.view(0)=widget_draw(Left_base,xsi=xsize,ysi=Data.Win_size,/fra,/motion, $
	/button)
Data.View_Label(0)=widget_label(Left_base,val=Emptystring,/fra) ;,font=font)

Row_base=widget_base(Data.Right_base(0),/row)

Label_val=['','','Integrated scan',	$
	'Dispersion scan','Time profile','Source positions','']

Shift_button0=WIDGET_BUTTON(Row_base,val=L_shift,uval='Lshift') ;,font=font)
Shift_button1=WIDGET_BUTTON(Row_base,val=R_shift,uval='Rshift') ;,font=font)

Plain_base=widget_base(Row_Base)

	for j=0,1 do begin

Data.Zoom_Base(j)=WIDGET_BASE(Plain_base,/row)

Data.Zoom_button(j)=WIDGET_BUTTON(Data.Zoom_Base(j),	$
	val=([Zoom,Unzoom])([0,2]+j,*),		$
	uval=(['Zoom','Unzoom'])(j)) ;,font=font)

widget_control,Data.Zoom_Base(j),map=1-j

	endfor


Label=widget_label(Row_base,val='Thres.: ') ;,font=font)
Data.thres=widget_text(Row_base,/edit,xsize=8,/fra,uval='Threshold',	$
	val=strtrim(string(Data.threshold,format="(f9.1)"),2)) ;,font=font)

Zero_label=WIDGET_LABEL(Row_base,val=' Zero: ') ;,font=font)
Data.Zero=WIDGET_TEXT(Row_base, val=strtrim(string(Flux.min,format='(f8.1)'),2),	$
	/edit,uval='Zero',xsiz=8,/fra) ;,font=font)

Plain_base=widget_base(Row_base)

for j=0,1 do Data.Calib_base(j)=widget_base(Plain_base,/row)

Object_label=WIDGET_LABEL(Data.Calib_base(0),val=' Scan: ') ;,font=font)

Plain_base=widget_base(Data.Calib_base(0))

values=['Single','Integr.']

	for j=0,1 do begin
Data.Toggle_Base(j)=WIDGET_BASE(Plain_base,/row)
Data.Toggle_button(j)=WIDGET_BUTTON(Data.Toggle_Base(j),val=values(j),uval=values(j)) ;,font=font)
	endfor

Data.OK_button=WIDGET_BUTTON(Data.Calib_base(0),val='OK',uval='OK') ;,font=font)
button=WIDGET_BUTTON(Data.Calib_base(1),val='Calibrate',uval='Calibrate') ;,font=font)

	for j=0,1 do begin
widget_control,Data.Calib_base(j), map=1-j;,sens=1-j
widget_control,Data.Toggle_base(j), map=j
widget_control,Data.Toggle_button(j), sens=0	;set_but=1-j
widget_control,Data.OK_button,sens=0
	endfor

Data.view(1)=widget_draw(Data.Right_base(0),xsi=scr(0)*0.95-xsize,	$
	ysi=scr(0)*0.8-xsize,/fra,/motion, /button)

Data.View_Label(1)=widget_label(Data.Right_base(0),val=Emptystring,/fra) ;,font=font)


	for j=2,N-1 do begin
Labels=widget_label(Data.Right_base(1),val=Label_val(j)) ;,font=font)

Data.view(j)=widget_draw(Data.Right_base(1),/fra,	$
	xsi=(scr(0)*0.95-xsize)/2,ysi=scr(1)*0.66/2, /motion, /button)
Data.View_Label(j)=widget_label(Data.Right_base(1),val=Emptystring,/fra) ;,font=font)

	endfor

for j=0,1 do widget_control,Data.Right_base(j),map=j

widget_control,main_base,/real,/hour
widget_control,Data.info,set_val='Please load a file.'

	for j=0,N-1 do begin
widget_control,Data.View(j),get_val=tmp
Data.Win(j)=tmp
wset,Data.Win(j)

erase,!d.n_colors-1
plot,findgen(10),/noerase,/nodata,xst=4,yst=4
Scale,tmp,/mem
Sc.W(j)=tmp
	endfor


xmanager,'l_r_proc',main_base,group=group_leader

	end