	pro xhxt_event, ev

common xhxt, ID, INDEX, DATA, mytime, ave1, intervals

ymar = [2,0.2]

if strlowcase(strmid(!version.OS, 0, 3))  eq 'win' then Delim = '\' else Delim = '/'

Sz = size(ave1)

	IF ev.id eq ID.Draw THEN BEGIN

Wset, ID.Win
xy = (convert_coord(ev.x, ev.y, /dev, /to_data))([0,1])

range = (convert_coord([0,0], [0,!d.y_size], /dev, /to_data))(1, [0,1])
range = transpose(range)

	if ev.press eq 1 then ID.pressleft =1
	if ev.release eq 1 then ID.pressleft =0
	if ev.press eq 4 then ID.pressright =1
	if ev.release eq 4 then ID.pressright =0

	if ev.release eq 1 then if ID.Mode eq 'Select' then begin
ID.firstleft = 1
ID.firstright = 1

if ID.nint eq 0 then intervals(0,ID.nint) = smh(ID.left*3600d0) else begin
intervals(0,ID.nint) = (intervals(1,ID.nint-1) = smh(ID.left*3600d0))
endelse

ID.nint=ID.nint+1

	endif

	if ev.release eq 4 then if ID.Mode eq 'Select' then begin
ID.firstleft = 1
ID.firstright = 1

if ID.nint eq 0 then intervals(0,ID.nint) = smh(ID.right*3600d0) else begin
intervals(0,ID.nint+1) = (intervals(1,ID.nint) = smh(ID.right*3600d0))
endelse

ID.nint=ID.nint+1

	endif

	if ID.pressleft eq 1 and ID.Mode ne 'Mark' then begin
device, set_graph = 6
		if ID.firstleft ne 1 then begin
plots, ID.left, range, col = 127
		endif else ID.firstleft = 0
plots, xy(0), range, col = 127
ID.left = xy(0)
device, set_graph = 3
	endif

	if ID.pressright eq 1  and ID.Mode ne 'Mark' then begin
device, set_graph = 6
		if ID.firstright ne 1 then begin
plots, ID.right, range, col = 127
		endif else ID.firstright = 0
plots, xy(0), range, col = 127
ID.right = xy(0)
device, set_graph = 3
	endif

		if ID.mode eq 'Mark' then begin
	if ev.press eq 1 then begin
intv = transpose(intervals(*,0:ID.nint-1))
number = (where(xy(0) ge hmsd(intv(*,0)) and xy(0) le hmsd(intv(*,1))))(0)

if number lt 0 then return

intervals(2, number) = '1'
x0=hmsd(intv(number, 0))
x1=hmsd(intv(number, 1))
polyfill, [x0, x1, x1, x0, x0], range([0,0,1,1,0]), /line_fill, col = 150, $
	orient = 45

	endif



	if ev.press eq 4 then begin
	endif
		endif

	if ID.pressleft eq 1 or ID.pressright eq 1 then begin


	if Sz(0) gt 1 then begin
N = (where(mytime ge xy(0)*3600.))(0) > 0
widget_control, ID.Label, set_val = smh((xy(0)+24*(xy(0) lt 0))*3600d0) + ', ' + $
strcompress(string(ave1(N,0), ave1(N, 1), ave1(N,2), ave1(N,3), format = $
	"('L: ', i5, ', M1:', i5, ', M2:', i5, ', H:', i5)"))
	endif else $
widget_control, ID.Label, set_val = smh((xy(0)+24*(xy(0) lt 0))*3600d0)


	endif

empty
	return
	ENDIF

widget_control, ev.id, get_uval = uv

	CASE uv OF

'Done': begin
	widget_control, ev.top, /destroy

	end

'Wcalc':	wcalc

'Load': begin

file = pickfile(filt = 'hda*', /read, path = ID.path, file = ID.file)
if file eq '' then return

ID.firstleft = 1
ID.firstright = 1

widget_control, /hour
rd_xda, file, -1,index,data

ave = ave_cts(index, data, time=time1)

daycnv, index.gen.day - 1 + 2443874.5d0, year, month, day

dates = string(day, form='(i2.2)')+'/'+string(month, form='(i2.2)')+ '/'+$
	string(year mod 100, form='(i2.2)')

times = index.gen.time/1d3

mytime = time1+times(0)

ave1 = transpose(reform(ave, 4, n_elements(ave)/4))
mytime = reform(mytime, n_elements(mytime))

Wset, ID.Win
!p.multi=[0,1,4]

for j=0,3 do $
plot, mytime/3600d0, ave1(*, j), xtickf = 'ut_tick0', $
	ytit = (['L','M1', 'M2', 'H'])(j), $
	color = 0, background = !d.n_colors-1, /yno, $
	chars = ID.chars, ymar = ymar

!p.multi=0

ID.left = !x.crange(0)
ID.right = !x.crange(1)

intervals = strarr(3,100)
intervals(2, *) = '0'

ID.firstleft = (ID.firstright = 1)
ID.file = file
ID.path = subdir(file)
ID.left = (ID.right=0.)
ID.Mode = 'Expand'
ID.nint = 0
ID.pressleft= (ID.pressright = 0)

path = ID.path
chars = ID.chars

save, path, chars, file = getenv('gr_root')+ Delim + 'xhxt.ini'

	end

'Expand': 	begin
ID.Mode = 'Expand'
Wset, ID.Win
!p.multi=[0,1,4]

xrange = [ID.left, ID.right]
xrange = xrange(sort(xrange))

for j=0,3 do $
plot, mytime/3600d0, ave1(*, j), xtickf = 'ut_tick0',  $
	ytit = (['L','M1', 'M2', 'H'])(j), $
	color = 0, background = !d.n_colors-1, $
	xran = xrange, /yno, chars = ID.chars, ymar = ymar
!p.multi=0


ID.firstleft = 1
ID.firstright = 1

		end

'Whole': 	begin

ID.Mode = 'Expand'
Wset, ID.Win
!p.multi=[0,1,4]


for j=0,3 do $
plot, mytime/3600d0, ave1(*, j), xtickf = 'ut_tick0', $
	ytit = (['L','M1', 'M2', 'H'])(j), $
	color = 0, background = !d.n_colors-1, /yno, $
	chars = ID.chars, ymar = ymar

!p.multi=0

ID.firstleft = 1
ID.firstright = 1

		end

'Select':	begin
ID.Mode = 'Select'
ID.firstleft = 1
ID.firstright = 1
		end

'Mark':		begin
ID.Mode = 'Mark'
ID.firstleft = 1
ID.firstright = 1
		end

'Save Data':	begin
file = pickfile(filt = '*.sav', /write)
if file eq '' then return

save, INDEX, DATA, mytime, ave1, file = file
		end

'Save All':	begin
file = pickfile(filt = '*.sav', /write)
if file eq '' then return
intervals1 = intervals(*, 0:ID.nint-1)
save, INDEX, DATA, mytime, ave1, intervals1, file = file
file = pickfile(filt = '*.txt', /write)
if file eq '' then return

	openw, lun, file, /get
for j=0, ID.nint-2 do printf, lun, intervals1(*,j)
	free_lun, lun

		end

'Save Intervals':	begin

file = pickfile(filt = '*.txt', /write)
if file eq '' then return

intervals1 = intervals(*, 0:ID.nint-1)
	openw, lun, file, /get
for j=0, ID.nint-2 do printf, lun, intervals1(*,j)
	free_lun, lun

		end


	ELSE:
	ENDCASE

empty


	end



	pro xhxt, output


common xhxt, ID, INDEX, DATA, mytime, ave1, intervals

intervals = strarr(3,100)
intervals(2, *) = '0'

ID =  {Draw:0L, Win:0L, Label:0L, firstleft:1, firstright:1, $
	file:'', path :'/parithi2/grechnev/data/hxt', $
	left:0., right:0., mode:'Expand', pressleft:0, pressright:0, $
	nint:0, chars:2.}

if strlowcase(strmid(!version.OS, 0, 3))  eq 'win' then Delim = '\' else Delim = '/'

sav_file = (findfile(getenv('gr_root')+ Delim + 'xhxt.ini'))(0)

	if sav_file ne '' then begin
restore, sav_file
ID.path = path
ID.chars = chars
	endif

mainbase = widget_base(tit = 'HXT viewer', /colu)

menubase = widget_base(mainbase, /row)

button = widget_button(menubase, val = 'Done', uval = 'Done')
button = widget_button(menubase, val = 'File', /menu)
	button1 = widget_button(button, val = 'Load', uval = 'Load')
	button1 = widget_button(button, val = 'Save', /menu)
		button2 = widget_button(button1, val = 'Data', uval = 'Save Data')
		button2 = widget_button(button1, val = 'Intervals', uval = 'Save Intervals')
		button2 = widget_button(button1, val = 'All', uval = 'Save All')
button = widget_button(menubase, val = 'Tools', /menu)
	button1 = widget_button(button, val = 'Expand', uval = 'Expand')
	button1 = widget_button(button, val = 'Whole range', uval = 'Whole')
	button1 = widget_button(button, val = 'Select intervals', uval = 'Select')
	button1 = widget_button(button, val = 'Mark intervals', uval = 'Mark')
	button1 = widget_button(button, val = 'Calculator', uval = 'Wcalc')

device, get_screen = screen

ID.Draw = widget_draw(mainbase, xsiz = screen(0)*0.97, ys = screen(1)*0.85, $
	/motion, /button)
ID.Label = widget_label(mainbase, val ='', /dynam)

widget_control, mainbase, /real
widget_control, ID.Draw, get_val = tmp
ID.Win = tmp
wset, tmp
plot, findgen(100), back = !d.n_colors-1, xst = 5, yst = 5, /nodata
empty

xmanager, 'xhxt', mainbase

	end