	pro _s_m_imp_event,ev

	common _s_m_imp, ID, image, header, index, a, data, disk_index, trace

Sz=size(Image)

	if ev.id eq ID.Draw(0) then begin

window_set, ID.Win(0), sc=ID.Sc(0)

widget_control, ID.Label(0), set_val=	$
	string(ev.x/ID.factor, ev.y/ID.factor, $
		image(ev.x/ID.factor>0<(Sz(1)-1), ev.y/ID.factor>0<(Sz(1)-1)), $
		format='(i3, ", ", i3, "; ", g10.3)')


	if id.Mode eq 'Mark' then begin

	CASE ID.Select_Mode OF

'Box':	begin

	tmp=a.a
w_box_cursor,ev,xy,init=a.init,cur=tmp
	a.a=tmp

a.init=0
a.xy=xy
	end

'Trace':	begin

if ev.press then ID.press=1
if ev.release then ID.press=0

if ID.press eq 0 then return

if n_elements(trace) eq 1 then trace=[ev.x, ev.y] else begin

trace=[[trace], [ev.x, ev.y]]

Sz_tr=size(trace)

plots,trace(0,Sz_tr(2)-[1,2]), trace(1,Sz_tr(2)-[1,2]), /dev

empty

endelse

		end


'Triangle':	begin

if ev.press eq 0 then return

Data.Triangle(*, Data.Attempt)=[ev.x, ev.y]

if Data.Attempt then begin

x0=Data.Triangle(0,0)
x1=Data.Triangle(0,1)
y0=Data.Triangle(1,0)
y1=Data.Triangle(1,1)

k=(y1-y0)/(x1-x0)
b=y0-k*x0

wset,ID.Win(0)

plotline, k, [x0, y0], /dev, /noc, col=0;!d.n_colors-1


	CASE 1 OF

(b lt 0) and (k*!d.x_size+b le !d.y_size):	$
Data.Triangle=[[-b/k,0], [!d.x_size, 0], [!d.x_size, !d.x_size*k+b], [-b/k,0]]

(b lt 0) and (k*!d.x_size+b gt !d.y_size) and ((-b/k) gt !d.x_size/2):	$
Data.Triangle=[[-b/k,0], [!d.x_size, 0], [!d.x_size, !d.y_size], [!d.y_size/k-b/k, !d.y_size]]

(b lt 0) and (k*!d.x_size+b gt !d.y_size) and ((-b/k) le !d.x_size/2):	$
Data.Triangle=[[0,0], [-b/k,0], [(!d.y_size-b)/k, !d.y_size], [0, !d.y_size]]

(b ge 0) and (k*!d.x_size+b le !d.y_size/2) and (k*!d.x_size+b ge 0):	$
Data.Triangle=[[0,0], [!d.x_size, 0], [!d.x_size, !d.x_size*k+b], [0,b]]

(b ge 0) and (k*!d.x_size+b gt !d.y_size/2) and (k*!d.x_size+b le !d.y_size):	$
Data.Triangle=[[0,b], [!d.x_size, !d.x_size*k+b], [!d.x_size, !d.y_size], [0, !d.y_size]]

(b ge 0) and (k*!d.x_size+b gt !d.y_size/2) and (k*!d.x_size+b gt !d.y_size):	$
Data.Triangle=[[0,b], [(!d.y_size-b)/k, !d.y_size], [0, !d.y_size], [0, b]]


(b ge 0) and (b le !d.y_size) and (k*!d.x_size+b le 0):		$
Data.Triangle=[[0,0], [-b/k,0], [0,b], [0,0]]

(b gt !d.y_size) and (k*!d.x_size+b le 0) and ((-b/k) le !d.x_size/2):		$
Data.Triangle=[[0,0], [-b/k,0], [(!d.y_size-b)/k,!d.y_size], [0,!d.y_size]]

(b gt !d.y_size) and (k*!d.x_size+b le 0) and ((-b/k) gt !d.x_size/2):		$
Data.Triangle=[[-b/k,0], [!d.x_size,0], [!d.x_size, !d.y_size], [(!d.y_size-b)/k,!d.y_size]]

(b gt !d.y_size) and (k*!d.x_size+b gt 0):		$
Data.Triangle=[[(!d.y_size-b)/k,!d.y_size], [!d.x_size, k*!d.x_size+b], [!d.x_size, !d.y_size], [(!d.y_size-b)/k,!d.y_size]]

;(b gt !d.y_size) and (k*!d.x_size+b gt 0) and ((-b/k) gt !d.x_size/2):		$
;Data.Triangle=[[-b/k,0], [!d.x_size,0], [!d.x_size, !d.y_size], [(!d.y_size-b)/k,!d.y_size]]


ELSE:

	ENDCASE



C=Data.Triangle

polyfill,C(0,*), C(1,*), /dev, col=0


empty

endif

Data.Attempt=(Data.Attempt+1) mod 2


		end


ELSE:

	ENDCASE


	endif else begin
device, /cursor_cross
a.init=1
	endelse





		if ev.press then begin
	if ID.Mode eq 'Profiles' then begin
wset,ID.Win(1)
plot,image(*,ev.y/ID.factor > 0 < (Sz(2)-1)), /yno, col=0, back=!d.n_colors-1, $
	xmar=[6,2], ymar=[2,1], /xst
Scale, tmp, /mem
ID.Sc(1)=tmp


wset,ID.Win(2)
plot,image(ev.x/ID.factor > 0 < (Sz(1)-1),*), indgen(Sz(2)), col=0, back=!d.n_colors-1, $
	xmar=[6,2], ymar=[2,1], /yst
Scale, tmp, /mem
ID.Sc(2)=tmp


	endif
		endif
return
	endif



	if ev.id eq ID.Draw(1) then begin

window_set, ID.Win(1), sc=ID.Sc(1)

widget_control, ID.Label(1), set_val=	$
	string(ev.x/ID.factor, ev.y/ID.factor, format='(i3, ", ", i3)')

return
	endif



	if ev.id eq ID.Draw(2) then begin

window_set, ID.Win(2), sc=ID.Sc(2)

widget_control, ID.Label(2), set_val=	$
	string(ev.x/ID.factor, ev.y/ID.factor, format='(i3, ", ", i3)')

return
	endif



widget_control, ev.id, get_uval=uv, /hourglass

	CASE uv OF

"DONE":		begin
widget_control, ev.top, /destroy
if ID.group_leader ne 0L then if widget_info(ID.group_leader,/valid) then $
	widget_control, ID.group_leader, /show

ID=(image=(header=(index=(a=(data=0)))))

trace=(disk_index=0)


		end

"Xloadct":	begin
widget_control, /hour
	Xloadct
		end

"Calculator":	begin
widget_control, /hour
	Wcalc
		end

"Profiles":	ID.Mode='Profiles'

"Header":	begin
widget_control,/hour
xtext,text=header
		end


"QS_S":		Data.Scaling=1

"Whole_S":	Data.Scaling=0


"Save":	begin

file=ID.File

path=subdir(ID.File)

widget_control, /hour

New_File=pickfile(/write, path=path, file=file)

if New_File eq '' then return

widget_control, /hour

minval=min(image, max=maxval)
bscale=(maxval-minval)*1e-4
bzero=float(minval)
image_to_write=fix((image-bzero)/bscale)
Sz=size(image)

suneph, Data.Date, Data.Time, SUN
solr=SUN.R*!Radeg*3600

ind=(where(strmid(header,0,2) eq 'Ro'))(0)
if ind ge 0 then header=[header(0:ind-1), header(ind+1:*)]


	Ref_Culm=strtrim(fh_r_key(header,'CULMIN', error=error),2)
	ref_decl=hms(strtrim(fh_r_key(header,'DELTA', error=error),2))
	Ref_Freq=fh_r_key(header,'FREQ', error=error)

sxdelpar,header,'CULMIN'
sxdelpar,header,'DELTA'
sxdelpar,header,'FREQ'

sxaddpar,header,'BITPIX',16
sxaddpar,header,'NAXIS1',Sz(1)
sxaddpar,header,'NAXIS2',Sz(2)
sxaddpar,header,'BSCALE',bscale, '  REAL = DATA*BSCALE + BZERO',after='NAXIS2'
sxaddpar,header,'BZERO',bzero, after='BSCALE'
sxaddpar,header,'DATAMIN',Data.Min, after='BZERO'
sxaddpar,header,'DATAMAX',Data.Max, after='DATAMIN'


date=strmid(Data.date,0,2)+'/'+strmid(Data.date,3,2)+'/'+strmid(Data.date,6,2)
time_obs=fh_r_key(header, 'time-obs')

sxdelpar,header,'DATE-OBS'
sxdelpar,header,'TIME-OBS'

sxaddpar,header,'DATE-OBS', date, after='NAXIS2'
sxaddpar,header,'TIME-OBS', time_obs, '  reference time', after='DATE-OBS'

sxaddpar,header,'TSTART', fh_r_key(header, 'BEG-OBS'), after='TIME-OBS'
sxaddpar,header,'TSTOP', fh_r_key(header, 'END-OBS'), after='TSTART'

sxdelpar,header,'BEG-OBS'
sxdelpar,header,'END-OBS'

sxdelpar,header,'MIN'
sxdelpar,header,'MAX'
sxdelpar,header,'DELTA'
sxdelpar,header,'CENTER-X'
sxdelpar,header,'CENTER-Y'
sxdelpar,header,'CREATORS'

sxaddpar,header,'CRVAL1',0., after='DATAMAX',format='f4.2','  disk center X'
sxaddpar,header,'CRVAL2',0., after='CRVAL1',format='f4.2','  disk center Y'

sxaddpar,header,'CTYPE1', 'SOLAR-WEST ',after='CRVAL2', '  oriented heliocentrically'
sxaddpar,header,'CTYPE2', 'SOLAR-NORTH',after='CTYPE1'

sxaddpar,header,'CRPIX1', float(Sz(1)/2)+1,after='CTYPE2', format='f6.2'
sxaddpar,header,'CRPIX2', float(Sz(2)/2)+1,after='CRPIX1', format='f6.2'

sxaddpar,header,'CDELT1', solr/ID.R*ID.Factor, after='CRPIX2', '  arcsec'
sxaddpar,header,'CDELT2', solr/ID.R*ID.Factor, after='CDELT1', '  arcsec'

sxdelpar,header,'TELESCOP'
sxdelpar,header,'OBJECT'

sxaddpar,header,'OBS-FREQ',	'5.7 GHz ',	after='CDELT2'
sxaddpar,header,'OBJECT',	'SUN     ',	after='OBS-FREQ'
sxaddpar,header,'TELESCOP',	'SSRT    ',	'  Siberian Solar Radio Telescope', after='OBJECT'
sxaddpar,header,'INSTRUME',	'MFB     ',	'  multi-frequency filterbank', after='TELESCOP'
sxaddpar,header,'ORIGIN',	'BADARY  ',	'  Radio Astrophysical Observatory', after='INSTRUME'
sxaddpar,header,'DATA-TYP',	'DIRTY_MAP', 	'  calibrated', after='ORIGIN'
sxaddpar,header,'POLARIZ',	'R+L     ',	after='DATA-TYP'


sxdelpar,header,'XSIZE'
sxdelpar,header,'YSIZE'

sxaddpar,header,'DEC', SUN.Decl*!Radeg, '  declination (degree)'
sxaddpar,header,'CULM', smh(SUN.tcul*3600,ms=1), '  culmination at Badary'
sxaddpar,header,'SOLR', solr, '  optical solar radius (arcsec)'
sxaddpar,header,'SOLP', SUN.Dp*!radeg, '  solar polar angle (degree)'
sxaddpar,header,'SOLB', SUN.B0*!radeg, '  solar b0 (degree)'
sxaddpar,header,'CARR-LNG', SUN.Karr*!radeg, '  Carringt. long of the sol. cent. (degree)'

sxaddpar,header,'REF-DEC', Ref_decl, '  reference declination (degree)'
sxaddpar,header,'REF-CULM', Ref_culm, '  reference culmination '
sxaddpar,header,'REF-FREQ', Ref_freq, '  reference frequency (MHz)'


writefits,New_File,image_to_write,header

	end


"Box":		begin
	ID.Mode='Mark'
	ID.Select_Mode='Box'
		end

"Trace":	begin
	ID.Mode='Mark'
	ID.Select_Mode='Trace'
	trace=0
		end

"Triangle":	begin
	ID.Mode='Mark'
	ID.Select_Mode='Triangle'
	Data.Triangle(*,*)=0
		end

"Accept":	begin

wset,ID.Win(0)

	CASE ID.Select_Mode OF

'Box': begin

V_x=[a.xy(0,0),a.xy(1,0),a.xy(1,0),a.xy(0,0),a.xy(0,0)]
V_y=[a.xy(0,1),a.xy(0,1),a.xy(1,1),a.xy(1,1),a.xy(0,1)]

	end


'Trace': begin

V_x=[transpose(trace(0,*)), trace(0,0)]
V_y=[transpose(trace(1,*)), trace(1,0)]
trace=0

	end



ELSE:	return

	ENDCASE

polyfill, V_x, V_y, /dev,col=(Data.Plot_mean-Data.Plot_min)*Data.Plot_factor

ind_cur=polyfillv(V_x/ID.factor, V_y/ID.factor, !d.x_size/ID.factor, !d.y_size/ID.factor)

	if n_elements(index) eq 1 then index=ind_cur else begin
index=[index, ind_cur]
index=index(uniq(index, sort(index)))
	endelse

wset,ID.Win(1)

erase,!d.n_colors-1

temp=bytscl(image)
temp(index)=(Data.Plot_mean-Data.Plot_min)*Data.Plot_factor

Data.Plot_Max=max(temp)/Data.Plot_factor+Data.Plot_min

Sz=size(image)
V_size=!d.y_size < !d.x_size

tvscl,congridg(temp, V_size, V_size)

;tv,Data.factor*(temp-Data.min)

	if Data.Scaling then begin
wset, ID.Win(0)
if ID.factor ne 1 then 	$
tvscl, bytscl(congrid(image, !d.x_size, !d.y_size, /int),max=Data.Plot_Max) else $
tvscl, bytscl(image, max=Data.Plot_Max)
	endif

empty
		end



"Flatten":	begin

	if n_elements(disk_index) le 1 then begin

N=2048
t=findgen(N)/(N-1)*2*!pi

xx=cos(t)*ID.R/ID.factor+ID.Centre(0)/ID.factor
yy=sin(t)*ID.R/ID.factor+ID.Centre(1)/ID.factor

disk_index=polyfillv(xx, yy, Sz(1), Sz(2))

t=(xx=(yy=0))

	endif

Mark=make_array(size=size(image), /byte, val=1b)
Mark(disk_index)=0

Mark(index)=1b

Quiet_Sun=image(where(Mark eq 0))

Data.Mean=total(Quiet_Sun)/n_elements(Quiet_Sun)
Data.Max=max(Quiet_Sun)
Quiet_Sun=0

x=image

x(index)=Data.Mean

Mark=make_array(size=size(x), /byte, val=1b)

Mark(disk_index)=0

Outside_index=where(Mark)

Mark=0

Outside=x(Outside_index)

Mean_Outside=total(Outside)/n_elements(Outside)

x(Outside_index)=x(Outside_index)-Mean_Outside+Data.Mean




;Outside_index=(Outside=0)

Outside=0

Sz=size(x)

if Sz(0) eq 3 then x=total(x,1) else x=float(x)

Sz=size(x)

smooth_width=30
aux_w=smooth_width/2
width=20

x1=replicate(1,aux_w)#total(x(0:width,*),1)/(width+1)
x2=replicate(1,aux_w)#total(x(Sz(2)-width-1:*,*),1)/(width+1)

x=[x1,x,x2]

x1=(x2=0)

x0=total(x(*,0:width),2)/(width+1)#replicate(1,aux_w)
x3=total(x(*,Sz(2)-width-1:*),2)/(width+1)#replicate(1,aux_w)

x=[[x0],[x],[x3]]

x0=(x3=0)

Sz=size(x)

x=(smooth(x,smooth_width))(aux_w:Sz(1)-aux_w-1,aux_w:Sz(1)-aux_w-1)

inside=x(disk_index)

Mean_inside=total(inside)/n_elements(inside)

factor=x/Mean_inside

factor(Outside_index)=1;(x(Outside_index)+Mean_Outside-Data.Mean)/Mean_Outside

factor=smooth(factor,5)


image=image/factor

wset, ID.Win(0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

empty

		end


"Filter":	begin
x=image
x(index)=Data.Mean
x=median(x,3)
x(index)=image(index)
image=temporary(x)

wset,ID.Win(0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

empty
		end



'1D scan':	begin
ID.Mode='1D scan'

wset,ID.Win(1)
plot,total(image,2)/Sz(2), /yno, col=0, back=!d.n_colors-1, $
	xmar=[6,2], ymar=[2,1], /xst
Scale, tmp, /mem
ID.Sc(1)=tmp


wset,ID.Win(2)
plot,total(image,1)/Sz(1),indgen(Sz(2)), col=0, back=!d.n_colors-1, $
	xmar=[6,2], ymar=[2,1], /yst
Scale, tmp, /mem
ID.Sc(2)=tmp

empty

		end

"Load":		begin
widget_control, /hour

filt='*.fit *.fts'

	if ID.file eq '' then begin


	path=getenv('optics_dir')
if path ne '' then file=pickfile(/read, filt=filt, path=path) else $
	file=pickfile(/read, filt=filt)

	endif else begin

	path=subdir(ID.File)
	file=pickfile(/read, filt=filt, path=path, file=ID.File)

	endelse

if file eq '' then return else ID.File=file

ID.Mode ='Init'
Data.Scaling=0
index=0


widget_control, /hour

;	image=rfitsg(file,index=fnum,key_struct=hstruc,header=header,error=err, $
;        	user_struct=ustruc,date_obs=date,time_obs=time ,/sc)

image=readfits(file, header)


;image=image-min(image)

wset, ID.Win(0)

Szx=size(image)

ID.factor=float(!d.x_size)/Szx(1)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

max_val=max(image, min=min_val)

Data.Plot_min=(Data.min=min_val)
Data.Plot_max=(Data.max=max_val)

Data.Plot_factor=float(!d.n_colors)/(max_val-min_val)

Data.Plot_mean=(Data.mean=total(image)/n_elements(image))


empty

Date=fh_r_key(header,'date-obs', error=error)

Dima=fh_r_key(header,'creators', error=error1)

Dima1=fh_r_key(header,'author', error=error1)
Les=fh_r_key(header,'CTYPE1', error=error2)

if (dima1 eq 'S&D') then goto, Label_Dima1


	if (dima ne 'S&D') and (error2 eq 1) then begin

day=strmid(date,3,2)
if strmid(day,0,1) eq ' ' then strput,day,'0',0
month=strmid(date,0,2)
if strmid(month,0,1) eq ' ' then strput,month,'0',0

Data.date=day+' '+month+' '+strmid(date,6,2)

	endif else Data.date=date

Data.Time=fh_r_key(header,'time-obs', error=error, /char)

suneph, Data.Date, Data.Time, Sun

Radius=fh_r_key(header,'radius', error=error)
if error then Radius=SUN.R*!radeg*60

X_origin=fh_r_key(header,'x-origin', error=error)
Y_origin=fh_r_key(header,'y-origin', error=error)

X_obs=fh_r_key(header,'X-obs', error=error)
if error then X_obs=fh_r_key(header,'X-extent', error=error)

	if error then begin
	X_obs=fh_r_key(header,'Xsize', error=error)
	X_obs=hms(strtrim(X_obs,2))*60
	endif

Y_obs=fh_r_key(header,'Y-obs', error=error)
if error then Y_obs=fh_r_key(header,'Y-extent', error=error)

	if error then begin
	Y_obs=fh_r_key(header,'Ysize', error=error)
	Y_obs=hms(strtrim(Y_obs,2))*60
	endif

X_cent=fh_r_key(header,'center-X', error=error)
if error then X_cent=0.d0

Y_cent=fh_r_key(header,'center-Y', error=error)
if error then Y_cent=0.d0

	if dima eq 'S&D' then begin
X_cent=0
X_origin=-X_obs/2
Y_cent=0
Y_origin=-Y_obs/2
	endif


P0=fh_r_key(header,'P0', error=error)
if error then P0=SUN.dp*!radeg

ID.R=Radius*Szx(1)/X_obs*ID.factor

ID.Centre=		[(X_cent-X_origin)/X_obs*!d.x_size, $
		(Y_cent-Y_origin)/Y_obs*!d.y_size]


	if error2 eq 0 then begin
Radius=sxpar(header, 'solr')/60.
X_obs=sxpar(header, 'cdelt1')*512./60
ID.R=Radius*Szx(1)/X_obs*ID.factor
crpix1=sxpar(header, 'CRPIX1')-1
crpix2=sxpar(header, 'CRPIX2')-1
if crpix1 lt Szx(1)/3. then crpix1=Szx(1)/2.
if crpix2 lt Szx(2)/3. then crpix2=Szx(2)/2.
ID.Centre=[CRPIX1/(Szx(1))*!d.x_size, CRPIX2/(Szx(2))*!d.y_size]
	endif


Label_dima1:

	if dima1 eq 'S&D' then begin


Data.Date=sxpar(header,'date-obs')
Data.Time=sxpar(header,'time-obs')

suneph, Data.Date, Data.Time, Sun

Radius=SUN.R*!radeg*60

X_cent=0.d0
Y_cent=0.d0

crpix1=sxpar(header, 'CRPIX1')-1
crpix2=sxpar(header, 'CRPIX2')-1

X_obs=sxpar(header, 'cdelt1')*Szx(1)/60.
ID.R=Radius*Szx(1)/X_obs*ID.factor

Y_obs=sxpar(header, 'cdelt2')*Szx(2)/60.

ID.Centre=[CRPIX1/(Szx(1))*!d.x_size, CRPIX2/(Szx(2))*!d.y_size]

	endif


draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3

widget_control, ID.Label(3), set_val=			$
	'File: '+(name_extract(ID.File))(0)+',  '+	$
	Date_string(Data.Date)+				$
	',  '+Data.Time+' UT'

empty

		end

'Moire':	begin

widget_control,/hour

ID.Mode ='Moire'

x=image

N=2048
t=findgen(N)/(N-1)*2*!pi

xx=cos(t)*ID.R/ID.factor+ID.Centre(0)/ID.factor
yy=sin(t)*ID.R/ID.factor+ID.Centre(1)/ID.factor

disk_index=polyfillv(xx, yy, Sz(1), Sz(2))
t=(xx=(yy=0))


Mark=make_array(size=size(image), /byte, val=1b)
Mark(disk_index)=0

Mark(index)=1b

Quiet_Sun=image
ind=(where(Mark eq 0))
Quiet_Sun(where(Mark eq 1))=0

Data.Mean=total(Quiet_Sun(ind))/n_elements(ind)
Data.Plot_Max=max(Quiet_Sun)

Quiet_Sun(where(Mark eq 1))=Data.Mean

Quiet_Sun=(Mark=0)

x(index)=Data.mean

Sz=size(x)

if Sz(0) eq 3 then x=total(x,1) else x=float(x)

Sz=size(x)

smooth_width=100
aux_w=smooth_width/2
width=10


;level_x=(total(x(0:width,*),1)/(width+1)+	$
;	total(x(Sz(2)-width-1:*,*),1)/(width+1))*0.5
;level_x=level_x-smooth(level_x,width)

;x=x-replicate(1,Sz(1))#level_x

;level_y=(total(x(*,0:width),2)/(width+1)+	$
;	total(x(*,Sz(2)-width-1:*),2)/(width+1))*0.5

;x=x-level_y#replicate(1,Sz(2))

;Data.min=min(x)

x=x-Data.min

x1=replicate(1.,aux_w)#total(x(0:width,*),1)/(width+1)
x2=replicate(1.,aux_w)#total(x(Sz(2)-width-1:*,*),1)/(width+1)

x=[x1,x,x2]

x1=(x2=0)

x0=total(x(*,0:width),2)/(width+1)#replicate(1.,aux_w)
x3=total(x(*,Sz(2)-width-1:*),2)/(width+1)#replicate(1.,aux_w)

x=[[x0],[x],[x3]]

x0=(x3=0)

Sz=size(x)

b_hor=total(x,1)
a_hor=b_hor/smooth(b_hor,smooth_width)
a_h=replicate(1.,Sz(2))#a_hor
a_h=min(a_h)/a_h
x=x*a_h

a_hor=(b_hor=0)

b_ver=total(x,2)

x=0

a_ver=b_ver/smooth(b_ver,smooth_width)

a_v=a_ver#replicate(1.,Sz(2))
a_v=min(a_v)/a_v

a_ver=(b_ver=0)

image=image	*a_h(aux_w:Sz(1)-aux_w-1,aux_w:Sz(1)-aux_w-1)	$
		*a_v(aux_w:Sz(1)-aux_w-1,aux_w:Sz(1)-aux_w-1)

wset, ID.Win(0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

empty

		end


'Rotate':	begin

	wset,ID.Win(0)
image=rotate(image,7)

suneph,Data.Date,Data.Time,sun

image=rot(image, sun.dp*!Radeg, /int, miss=0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

		end

'X+':	begin
	wset,ID.Win(0)
image=shift(image,1,0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'X+2':	begin
	wset,ID.Win(0)
image=shift(image,2,0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'X+5':	begin
	wset,ID.Win(0)
image=shift(image,5,0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'X-':	begin
	wset,ID.Win(0)
image=shift(image,-1,0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'X-2':	begin
	wset,ID.Win(0)
image=shift(image,-2,0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'X-5':	begin
	wset,ID.Win(0)
image=shift(image,-5,0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end


'Y+':	begin
	wset,ID.Win(0)
image=shift(image,0,1)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'Y+2':	begin
	wset,ID.Win(0)
image=shift(image,0,2)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'Y+5':	begin
	wset,ID.Win(0)
image=shift(image,0,5)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'Y-':	begin
	wset,ID.Win(0)
image=shift(image,0,-1)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'Y-2':	begin
	wset,ID.Win(0)
image=shift(image,0,-2)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end

'Y-5':	begin
	wset,ID.Win(0)
image=shift(image,0,-5)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3
	end



'Calibrate':	begin

widget_control,/hour

ID.Mode ='Calibrate'

x=image

N=2048
t=findgen(N)/(N-1)*2*!pi

Radio=1.07

xx=cos(t)*ID.R/ID.factor*Radio +ID.Centre(0)/ID.factor
yy=sin(t)*ID.R/ID.factor*Radio +ID.Centre(1)/ID.factor

disk_index0=polyfillv(xx, yy, Sz(1), Sz(2))
xx=(yy=0)

Mark=make_array(size=size(image), /byte, val=1b)
Mark(disk_index0)=0
ind=(where(Mark eq 1))
Sky=total(image(ind))/n_elements(ind)
image=image-Sky

Radio=0.9

xx=cos(t)*ID.R/ID.factor*Radio +ID.Centre(0)/ID.factor
yy=sin(t)*ID.R/ID.factor*Radio +ID.Centre(1)/ID.factor

disk_index=polyfillv(xx, yy, Sz(1), Sz(2))
xx=(yy=0)

Mark=make_array(size=size(image), /byte, val=1b)
Mark(disk_index)=0
Outside_index=where(Mark)
Mark(index)=1b

ind=(where(Mark eq 0))

Data.Mean=total(image(ind))/n_elements(ind)

image=image/Data.Mean*1.6e4

x = image

Radio=1.02

xx=cos(t)*ID.R/ID.factor*Radio +ID.Centre(0)/ID.factor
yy=sin(t)*ID.R/ID.factor*Radio +ID.Centre(1)/ID.factor

disk_index0=polyfillv(xx, yy, Sz(1), Sz(2))
xx=(yy=0)

Mark=make_array(size=size(image), /byte, val=1b)
Mark(disk_index0)=0
Outside_index=where(Mark)
Mark(index)=1b
Mark(Outside_index)=0

ind = (where(Mark eq 1))
if ind(0) ge 0 then x(ind) = 1.6e4


Mark=make_array(size=size(x), /byte, val=0b)
Mark(index)=1b
Mark(disk_index0)=0

ind = (where(Mark eq 1))
if ind(0) ge 0 then x(ind) = 0


slope = rebin(float(sfit(rebin(x, Sz(1)/4, Sz(2)/4), 1)), Sz(1), Sz(2))

slope = slope-mean(slope)

image = image-slope

Data.Min=min(image, max=amax)
Data.Max=amax

		end

'Resize':	begin

suneph, Data.Date, Data.Time, SUN
solr=SUN.R*!Radeg*3600

pix1=solr/ID.R
pix=4.91104

Sz=size(image)

factor=Sz(1)/512.*pix1/pix
ID.factor=float(!d.x_size)/512.
ID.Centre=200
ID.R=ID.R*factor

image=congridg(image, 512, 512)
image=rot(image, 0, factor, /int, miss=0)

if ID.factor ne 1 then tvscl, congrid(image, !d.x_size, !d.y_size, /int) else $
	tvscl, image

draw_circle,ID.Centre, ID.R, /noer, /axes, linest=1, a_lines=3


		end


ELSE:

	ENDCASE

empty
	end



	pro _s_m_imp, group_leader=group_leader

	common _s_m_imp, ID, image, header, index, a, data, disk_index, trace


if xregistered('_s_m_imp') then return

image=bytarr(512,512)

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

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

ID={group_leader:group_leader, draw:lonarr(3), Win:lonarr(3), Label:lonarr(4), $
	Sc:replicate(Ax,3), Mode:'Init', press:0,	$
	file:'', R:0., Centre: [0.,0.], factor:1., Mark:0L, 	$
	Moire:0L, Flatten:0L, Filter:0L, Select_Mode:'Box'}

Data={Date:' ', Time:' ', Mean:0., Min:0., factor:1., Triangle:fltarr(2,4), $
	Attempt:0, Max:0., Scaling:0, $
	Plot_Mean:0., Plot_Min:0., Plot_Max:0., Plot_factor:1.}

Ax=0

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.}

a={init:1, xy:intarr(2,2), a:init_structure}


bm_box= [		$
	[000B, 000B],	$
	[000B, 000B],	$
	[000B, 000B],	$
	[248B, 031B],	$
	[008B, 016B],	$
	[008B, 016B],	$
	[008B, 016B],	$
	[008B, 016B],	$
	[008B, 016B],	$
	[008B, 016B],	$
	[008B, 016B],	$
	[008B, 016B],	$
	[248B, 031B],	$
	[000B, 000B],	$
	[000B, 000B],	$
	[000B, 000B]	$
			]

bm_triangle=	[	$
	[000B, 000B],	$
	[000B, 000B],	$
	[000B, 000B],	$
	[248B, 031B],	$
	[016B, 016B],	$
	[032B, 016B],	$
	[064B, 016B],	$
	[128B, 016B],	$
	[000B, 017B],	$
	[000B, 018B],	$
	[000B, 020B],	$
	[000B, 024B],	$
	[000B, 016B],	$
	[000B, 000B],	$
	[000B, 000B],	$
	[000B, 000B]	$
	]

bm_trace=	[	     	$
	[000B, 000B],	$
	[192B, 001B],	$
	[032B, 002B],	$
	[016B, 004B],	$
	[008B, 008B],	$
	[008B, 016B],	$
	[008B, 032B],	$
	[004B, 032B],	$
	[004B, 032B],	$
	[004B, 032B],	$
	[004B, 016B],	$
	[004B, 008B],	$
	[008B, 008B],	$
	[112B, 008B],	$
	[128B, 007B],	$
	[000B, 000B]	$
	]

bm_OK=	[	     	$
	[000B, 000B],	$
	[000B, 000B],	$
	[000B, 000B],	$
	[056B, 132B],	$
	[068B, 068B],	$
	[130B, 036B],	$
	[130B, 020B],	$
	[130B, 012B],	$
	[130B, 020B],	$
	[130B, 036B],	$
	[068B, 068B],	$
	[056B, 132B],	$
	[000B, 000B],	$
	[000B, 000B],	$
	[000B, 000B],	$
	[000B, 000B]	$
	]


mainbase=widget_base(tit='SSRT map enhancer',/colu)
menubase=widget_base(mainbase,/row)

button=widget_button(menubase, val='DONE', uval='DONE')
button=widget_button(menubase, val='File', /menu)
	junk=widget_button(button, val='Load', uval='Load')
	junk=widget_button(button, val='Header', uval='Header')
	shift_but=widget_button(button, val='Centering', /menu)
		X_shift_but=widget_button(shift_but, val='X', /menu)
		X_shift_but_plus=widget_button(X_shift_but, val='+', /menu)

		button0=widget_button(X_shift_but_plus, val='+1', uval='X+')
		button0=widget_button(X_shift_but_plus, val='+2', uval='X+2')
		button0=widget_button(X_shift_but_plus, val='+5', uval='X+5')

		X_shift_but_minus=widget_button(X_shift_but, val='-', /menu)

		button0=widget_button(X_shift_but_minus, val='-1', uval='X-')
		button0=widget_button(X_shift_but_minus, val='-2', uval='X-2')
		button0=widget_button(X_shift_but_minus, val='-5', uval='X-5')

		Y_shift_but=widget_button(shift_but, val='Y', /menu)
		Y_shift_but_plus=widget_button(Y_shift_but, val='+', /menu)

		button0=widget_button(Y_shift_but_plus, val='+1', uval='Y+')
		button0=widget_button(Y_shift_but_plus, val='+2', uval='Y+2')
		button0=widget_button(Y_shift_but_plus, val='+5', uval='Y+5')

		Y_shift_but_minus=widget_button(Y_shift_but, val='-', /menu)

		button0=widget_button(Y_shift_but_minus, val='-1', uval='Y-')
		button0=widget_button(Y_shift_but_minus, val='-2', uval='Y-2')
		button0=widget_button(Y_shift_but_minus, val='-5', uval='Y-5')



;		button0=widget_button(shift_but, val='X-', uval='X-'

;		Y_shift_but=widget_button(shift_but, val='Y', /menu)
;		button0=widget_button(shift_but, val='X+', uval='X+')
;		button0=widget_button(shift_but, val='X-', uval='X-'

;		button0=widget_button(shift_but, val='X+', uval='X+')
;		button0=widget_button(shift_but, val='X-', uval='X-')
;		button0=widget_button(shift_but, val='Y+', uval='Y+')
;		button0=widget_button(shift_but, val='Y-', uval='Y-')
;	ID.Mark=widget_button(button, val='Mark sources', /menu)
;		junk=widget_button(ID.Mark, val='Enter mode', uval='Box')
;		junk=widget_button(ID.Mark, val='Accept', uval='Accept')
	ID.Moire=widget_button(button, val='Remove moire', uval='Moire')
	calib_button=widget_button(button, val='Calibrate', uval='Calibrate')
	rot_button=widget_button(button, val='Rotate', uval='Rotate')
	Resize_button=widget_button(button, val='Resize to NRH', uval='Resize')
;	ID.Flatten=widget_button(button, val='Flatten', uval='Flatten')
;	ID.Filter=widget_button(button, val='Filter', uval='Filter')
	junk=widget_button(button, val='Save', uval='Save')

button=widget_button(menubase, val='Tools', /menu)
	junk=widget_button(button, val='Palette', uval='Xloadct')
	junk=widget_button(button, val='Scaling', /menu)
	junk1=widget_button(junk, val='Whole range', uval='Whole_S')
	junk1=widget_button(junk, val='Quiet Sun', uval='QS_S')
	junk=widget_button(button, val='Profiles', uval='Profiles')
	junk=widget_button(button, val='1D scan', uval='1D scan')
	junk=widget_button(button, val='Calculator', uval='Calculator')


rowbase=widget_base(mainbase,/row)
leftbase=widget_base(rowbase,/colu)
rightbase=widget_base(rowbase,/colu)

DrawSize=400

edit_base=widget_base(leftbase,/row)

button=widget_button(edit_base, val=bm_box, uval='Box')
;button=widget_button(edit_base, val=bm_triangle, uval='Triangle')
button=widget_button(edit_base, val=bm_trace, uval='Trace')
button=widget_button(edit_base, val=bm_OK, uval='Accept')

	if !version.release ge 4 then begin

ID.Label(3)=widget_label(leftbase, val=' ', /dyn)
ID.Draw(0)=widget_draw(leftbase, xs=DrawSize, ys=DrawSize, $
	/button, /motion, /fra)
ID.Label(0)=widget_label(leftbase, val=' ', /fra, /dyn)

	for j=1,2 do begin
ID.Draw(j)=widget_draw(rightbase, xs=DrawSize*0.8, ys=DrawSize*0.55, $
	/button, /motion, /fra)
ID.Label(j)=widget_label(rightbase, val=' ', /fra, /dyn)
	endfor

	endif else begin

ID.Label(3)=widget_label(leftbase, val=' ')
ID.Draw(0)=widget_draw(leftbase, xs=DrawSize, ys=DrawSize, /button, $
	/motion, /fra)
ID.Label(0)=widget_label(leftbase, val=' ', /fra)

	for j=1,2 do begin
ID.Draw(j)=widget_draw(rightbase, xs=DrawSize*0.8, ys=DrawSize*0.55, $
	/button, /motion, /fra)
ID.Label(j)=widget_label(rightbase, val=' ', /fra)
	endfor
	endelse

widget_control, mainbase, /real, /hour

	for j=0,2 do begin

widget_control, ID.Draw(j), get_val=tmp
	ID.Win(j)=tmp

wset, ID.Win(j)

plot,indgen(10),xst=4,yst=4,/nod,back=!d.n_colors-1
Scale, tmp, /mem
ID.Sc(j)=tmp


	endfor



xmanager,'_s_m_imp',mainbase

	end