	function equiv, a, b

;+	Returns scalar 1 if input variables of any, but the same type
;	are equal, and scalar 0 if not.
;
;	Written by V.Grechnev, ISTP, 1995.
;-

c = where((a ne b), count)
return, count eq 0

	end


	function subdirec,b

;+	Returns pathname of the subdirectory
;	containing given file.
;-	Input argument - pathname of the file

	CASE !version.OS OF

'windows':	Delim='\'
'Win32':	Delim='\'
ELSE:		Delim='/'

	ENDCASE

	j=0

	while strpos(b,Delim,j) ge 0 do j=j+1
	return,strmid(b,0,j-1)

	end


	pro scale,SAXES,memorize=memorize,recover=recover

;	Saves and restores scaling inherent to a given graphics window.

	CASE 1 OF
keyword_set(memorize):	SAxes={Axes, x:!x, y:!y, z:!z, map:!map}

keyword_set(recover):	BEGIN

!x=SAxes.x
!y=SAxes.y
!z=SAxes.z
!map=SAxes.map
			END
ELSE:
	ENDCASE

		end


	pro a_issue_txt_event,ev

common Exch_hlp,scbase,Txt,Out_Text,lun,Filename

        CASE !version.OS OF

'windows':      prn_funct='copy '+Filename+' prn:'

'Win32':        prn_funct='copy '+Filename+' prn:'

ELSE:           prn_funct='lpr '+Filename

        ENDCASE


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

CASE wuv OF

"QUIT"	:	WIDGET_CONTROL,ev.top,/DESTROY

"File"	:	begin

	file=pickfile(/read)
	if file eq '' then return
	WIDGET_CONTROL,ev.top,/DESTROY
	a_issue_txt, File=File

		end


"PRINT"	:	IF Filename ne '' THEN begin
		flush,lun
		spawn, prn_funct
		wait,1
		endif

ELSE:

ENDCASE

end



   pro a_issue_txt,Text=Text,File=File,group_leader=group_leader, $
identifier=identifier

;	Displays interactively a text. A_issue_txt is similar to XDISPLAYFILE.

common Exch_hlp,scbase,Txt,Out_Text,lun,Filename


IF(XRegistered("a_issue_txt") NE 0) THEN return


	device,get_scr=scr

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

	if n_elements(File) le 0 then begin
Out_text=Text
Filename='prn_file.tmp'

	openw,lun,Filename,/get
for j=0, n_elements(Out_text)-1 do printf, lun, Out_text(j)
	flush, lun
	free_lun,lun

	endif else begin

widget_control,/hour
Filename=File

	openr,lun,file,/get

st=fstat(lun)
aa=bytarr(st.size)
readu,lun,aa
point_lun,lun,0

iii=where(aa eq '0a'xb)
aa=0
N=n_elements(iii)

if st.size-iii(N-1) lt 3 then NN=N-1 else NN=N
Out_text=strarr(NN > 1)

readf,lun,Out_text

	free_lun,lun

	endelse

scbase= widget_base(title='a_issue_txt', group_leader=group_leader, /column)

menubase=widget_base(scbase, /row)

button=widget_button(menubase, val="QUIT", uval="QUIT")
button=widget_button(menubase, val="Load", uval="File")
button=widget_button(menubase, val="Print", uval="PRINT")

Txt = WIDGET_TEXT(SCBASE,/frame,Ysize=n_elements(Out_text) < scr(1)/24 < 30 > 4, $
	value=Out_text,/scroll,uvalue='', xsize=max(strlen(Out_text)) +2 > 10)

WIDGET_CONTROL,scbase, /realize, /hourglass

identifier=scbase

xmanager,'a_issue_txt',SCBASE, GROUP_LEADER = GROUP_LEADER

end



	pro cur_date_save, entry, file

	openw, lun, file, /get

printf, lun, 'Data for '+entry.Date
printf, lun, '____________________________________'
printf, lun, 'Time:               ', entry.time
printf, lun, 'Pos. angle:                ',strtrim(string(entry.PA, format='(f5.1)'),2)
printf, lun, "Lat. of the Sun's center: ", strtrim(string(entry.B0, format='(f4.1)'),2)
printf, lun, 'Number of active regions: ', strtrim(string(entry.Nar, format='(i2)'),2)
printf, lun, 'Ipeak - I_Tb:   ', strtrim(string(entry.map.ipeak.itb, format='(g9.3)'),2)+ ', '+ $
        strtrim(string(entry.map.ipeak.vtb/(entry.map.ipeak.itb > 100)*100, $
        format='(f6.1)'),2)+'%'
printf, lun,  'Vpeak - V_Tb:  ', strtrim(string(entry.map.vpeak.vtb, format='(g10.3)'),2)+ ', '+ $
        strtrim(string(entry.map.vpeak.vtb/(entry.map.vpeak.itb > 100)*100,     $
        format='(f6.1)'),2)+'%'
printf, lun, '____________________________________'
printf, lun, '____________________________________'
printf, lun, '__________Active regions ___________'


	if entry.nar lt 1 then begin
printf, lun, 'No data about active regions'
flush, lun
free_lun, lun
return
	endif


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

printf, lun, '____________________________________'
printf, lun, 'NOAA '+strtrim(entry.region(j).name,2)
printf, lun, '_____________________'
printf, lun, 'Area:               ', entry.region(j).area
printf, lun, 'Type:               ', entry.region(j).type
printf, lun, 'Carr. Long:         ', entry.region(j).carrlng
printf, lun, 'Location:           '
printf, lun, '    Latitude:           ', entry.region(j).location.lat
printf, lun, '    Longitude:          ', entry.region(j).location.lng
printf, lun, '    X:                  ', entry.region(j).location.X
printf, lun, '    Y:                  ', entry.region(j).location.Y
printf, lun, 'Leader:             '
printf, lun, '    Ipeak :             '
printf, lun, '         I_Tb :             ' , entry.region(j).leader.ipeak.itb
printf, lun, '         V_Tb :             ' , entry.region(j).leader.ipeak.vtb
printf, lun, '            X :             ' , entry.region(j).leader.ipeak.X
printf, lun, '            Y :             ' , entry.region(j).leader.ipeak.Y
printf, lun, '    Vpeak :             '
printf, lun, '         I_Tb :             ' , entry.region(j).leader.vpeak.itb
printf, lun, '         V_Tb :             ' , entry.region(j).leader.vpeak.vtb
printf, lun, '            X :             ' , entry.region(j).leader.vpeak.X
printf, lun, '            Y :             ' , entry.region(j).leader.vpeak.Y
printf, lun, '    Mag.field :             ' , entry.region(j).leader.kgauss*0.1
printf, lun, 'Follower:           '
printf, lun, '    Ipeak :             '
printf, lun, '         I_Tb :             ' , entry.region(j).follower.ipeak.itb
printf, lun, '         V_Tb :             ' , entry.region(j).follower.ipeak.vtb
printf, lun, '            X :             ' , entry.region(j).follower.ipeak.X
printf, lun, '            Y :             ' , entry.region(j).follower.ipeak.Y
printf, lun, '    Vpeak :             '
printf, lun, '         I_Tb :             ' , entry.region(j).follower.vpeak.itb
printf, lun, '         V_Tb :             ' , entry.region(j).follower.vpeak.vtb
printf, lun, '            X :             ' , entry.region(j).follower.vpeak.X
printf, lun, '            Y :             ' , entry.region(j).follower.vpeak.Y
printf, lun, '    Mag.field :             ' , entry.region(j).follower.kgauss*0.1

	endfor

	flush, lun
	free_lun, lun


	end


	pro AR_data_save, file, data_array, AR_name

common ardb, db, image_I, image_V, instance, exhaustive, header_I, header_V

	N=n_elements(exhaustive(*,0))


	openw, lun, file, /get

printf, lun, 'Data for NOAA '+AR_name

	for j=0, N-1 do begin

printf, lun, '____________________________________'
printf, lun, 'Date:               ', db(exhaustive(j,0)).date
printf, lun, 'Time:               ', db(exhaustive(j,0)).time
printf, lun, 'Area:               ', data_array(j).area
printf, lun, 'Type:               ', data_array(j).type
printf, lun, 'Carr. Long:         ', data_array(j).carrlng
printf, lun, 'Location:           '
printf, lun, '    Latitude:           ', data_array(j).location.lat
printf, lun, '    Longitude:          ', data_array(j).location.lng
printf, lun, '    X:                  ', data_array(j).location.X
printf, lun, '    Y:                  ', data_array(j).location.Y
printf, lun, 'Leader:             '
printf, lun, '    Ipeak :             '
printf, lun, '         I_Tb :             ' , data_array(j).leader.ipeak.itb
printf, lun, '         V_Tb :             ' , data_array(j).leader.ipeak.vtb
printf, lun, '            X :             ' , data_array(j).leader.ipeak.X
printf, lun, '            Y :             ' , data_array(j).leader.ipeak.Y
printf, lun, '    Vpeak :             '
printf, lun, '         I_Tb :             ' , data_array(j).leader.vpeak.itb
printf, lun, '         V_Tb :             ' , data_array(j).leader.vpeak.vtb
printf, lun, '            X :             ' , data_array(j).leader.vpeak.X
printf, lun, '            Y :             ' , data_array(j).leader.vpeak.Y
printf, lun, '    Mag.field :             ' , data_array(j).leader.kgauss*0.1
printf, lun, 'Follower:           '
printf, lun, '    Ipeak :             '
printf, lun, '         I_Tb :             ' , data_array(j).follower.ipeak.itb
printf, lun, '         V_Tb :             ' , data_array(j).follower.ipeak.vtb
printf, lun, '            X :             ' , data_array(j).follower.ipeak.X
printf, lun, '            Y :             ' , data_array(j).follower.ipeak.Y
printf, lun, '    Vpeak :             '
printf, lun, '         I_Tb :             ' , data_array(j).follower.vpeak.itb
printf, lun, '         V_Tb :             ' , data_array(j).follower.vpeak.vtb
printf, lun, '            X :             ' , data_array(j).follower.vpeak.X
printf, lun, '            Y :             ' , data_array(j).follower.vpeak.Y
printf, lun, '    Mag.field :             ' , data_array(j).follower.kgauss*0.1

	endfor

	flush, lun
	free_lun, lun

	end


        function ardb_read_image,ID,path, error, header, polariz=polariz

error=0


V=strtrim(keyword_set(polariz),2)

        CASE !version.OS OF

'windows':      begin
                Delim='\'
                wildcard=V+'.fit'
                end

'Win32':        begin
                Delim='\'
                wildcard=V+'.fit'
                end

        ELSE:   begin
                Delim='/'
                if fix(V) then wildcard='*scp.fits' else wildcard='*acp.fits'

                end

        ENDCASE

dir=strmid(ID.Date,0,2)+strmid(ID.Date,3,2)
dir_hp='19'+strmid(ID.Date,0,2)

file=findfile(ID.path+'f'+dir+Delim+'s'+dir+strmid(ID.Date,6,2)+wildcard)

if file(0) eq '' then begin
file=findfile(ID.path+dir_hp+Delim+'s'+dir+strmid(ID.Date,6,2)+wildcard)
source='HOMEPAGE'
endif else source='CDROM'

if file(0) eq '' then begin
error=1
widget_control, ID.Info_Label, set_val='Image file not found.'
return, 0
endif


;        CASE strmid(strlowcase(!version.OS),0,3) OF

	CASE !version.OS OF

;'win':      	begin
;if source eq 'CDROM' then file=ID.path+'f'+dir+Delim+file(0) else $
;	file=ID.path+dir_hp+Delim+file(0)
;		end

'windows':      	begin
if source eq 'CDROM' then file=ID.path+'f'+dir+Delim+file(0) else $
	file=ID.path+dir_hp+Delim+file(0)
		end

'Win32':      	begin
	file=file(0)
		end


ELSE:

        ENDCASE

return,rfits(file(0), head=header)

        end



        pro plot_map_grid, ID, entry, I_image, I_header, V_image, V_header, $
		noerase=noerase, error=error


Version=strmid(!version.release,0,3) gt 3.5

error=1

if not keyword_set(noerase) then begin
	for jj=0,1 do begin

wset,ID.Win(jj)
erase
	endfor
endif

widget_control, ID.text(0), $
        set_val=ID.Date
widget_control, ID.Text(1), $
        set_val=' '+strtrim(string(entry.PA, format='(f5.1)'),2)
widget_control, ID.Text(2), $
        set_val=' '+strtrim(string(entry.B0, format='(f4.1)'),2)
widget_control, ID.Text(3), $
        set_val=' '+strtrim(string(entry.Nar, format='(i2)'),2)
widget_control, ID.Text(4), $
        set_val=' '+strtrim(string(entry.map.ipeak.itb, format='(g9.3)'),2)+ ', '+ $
        strtrim(string(entry.map.ipeak.vtb/(entry.map.ipeak.itb > 100)*100, $
        format='(f6.1)'),2)+'%'
widget_control, ID.Text(5), $
        set_val=' '+strtrim(string(entry.map.vpeak.vtb, format='(g10.3)'),2)+ ', '+ $
        strtrim(string(entry.map.vpeak.vtb/(entry.map.vpeak.itb > 100)*100,     $
        format='(f6.1)'),2)+'%'
widget_control, ID.Text(6), $
        set_val=' '+strtrim(string(entry.map.Kgauss*0.1, format='(f5.2)'),2)

	for jj=0,1 do begin

wset,ID.Win(jj)

                if ID.Show_Map then begin
        image=ardb_read_image(ID, path, error, header, polar=jj)
ID.No_Image=error
        if error then begin
widget_control, ID.Info_Label, set_val='Image file not found.'
	erase
        endif else begin
tvscl,image > 0

		if jj then begin
	V_image=image
	V_header=header
		endif else begin
	I_image=image
	I_header=header
		endelse

	endelse

                endif

plots,  entry.map.ipeak.x, entry.map.ipeak.y,   $
        /dev, col=!d.n_colors-1, syms=1, psym=8

plots,  entry.map.vpeak.x, entry.map.vpeak.y,   $
        /dev, col=!d.n_colors-1, syms=2, psym=1

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

Radius=entry.rsun/entry.pixsz

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

map_set,entry.B0,0,0, $ ;/grid, glinestyle=1,      $
        /ortho, /noerase, pos=[0,0,1,1], /nobor;, latdel=10, londel=10

!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

!P.clip=P_clip_save


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

scale,tmp,/mem

if Version then widget_control, ID.Draw(jj), set_uval=tmp, /no_copy else $
	widget_control, ID.Draw(jj), set_uval=tmp

;*************************
        widget_control,ID.buttonbase, get_uval=a

if ID.Zoom_state ne 1 then bias=[0,0] else bias=[a.xy(0,0), a.xy(0,1)]

if ID.Show_Num then for j=0,entry.nar-1 do begin

xr=(entry.region(j).location.x-bias(0))*float(ID.Factor)
yr=(entry.region(j).location.y-bias(1))*float(ID.Factor)

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,yr,entry.region(j).name, align=0.5, /dev, font=0;,col=0
        endfor

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

empty

	endfor

        end



        pro ardb_event, ev

common ardb, db, image_I, image_V, instance, exhaustive, header_I, header_V

Version=strmid(!version.release,0,3) gt 3.5
uv=''

        CASE !version.OS OF

'windows':      Delim='\'

'Win32':        Delim='\'

        ELSE:   Delim='/'

	ENDCASE


        widget_control,ev.top, get_uval=ID

        Win=1-(ev.id eq ID.Draw(0))

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

widget_control, ID.Draw(Win), get_uval=Ax

wset,ID.Win(Win)
scale,Ax,/rec

        widget_control,ID.buttonbase, get_uval=wbc_state

		if ID.Zoom_state ne 1 then begin

	tmp=wbc_state.a
w_box_cursor,ev,xy,init=wbc_state.init,cur=tmp
	wbc_state.a=tmp
wbc_state.init=0
wbc_state.xy=xy
if Version then widget_control,ID.buttonbase, set_uval=wbc_state, /no_copy $
	else widget_control,ID.buttonbase, set_uval=wbc_state

; !!!!!!!!!!!!!!!!! Questionable
if (xy(1,0)-xy(0,0) ne 0) and (xy(1,1)-xy(0,1) ne 0) then $
	widget_control, ID.Info_Label, set_val=' '

bias=[0,0]
		endif else bias=[wbc_state.xy(0,0), wbc_state.xy(0,1)]

x=convert_coord(ev.x, ev.y, /dev, /to_data) > (-90) < 90

Xd=float(ev.x)/ID.Factor+bias(0)
Yd=float(ev.y)/ID.Factor+bias(1)

if ID.Input_path ne 1 then widget_control, ID.Coord, set_val=      $
        string(Xd, Yd, fix(x(0)), fix(x(1)),        $
        format='(i3,", ",i3,"; ", i3,", ", i3)')


if (ID.Show_Map eq 0) or (ID.No_Image eq 1) then Tb=' ' else Tb= $
        string(Image_I(Xd > 0 < 511, Yd > 0 < 511), format='(g10.3)')+ ', '+$
        string(Image_V(Xd > 0 < 511, Yd > 0 < 511), format='(g10.3)')

widget_control, ID.Tb_Label, set_val=Tb

IF ev.press THEN BEGIN

entry=db(ID.Number)

R=fltarr(db(ID.Number).nar > 1)

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

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

R(j)=sqrt((Xd-xr)^2+(Yd-yr)^2)
        endfor

rmin=min(R, index)

widget_control, ID.ar_data(0), set_val= $
        strtrim(string(entry.region(index).Name),2)

Lng=entry.region(index).location.Lng
Lat=entry.region(index).location.Lat

if Lng lt 0 then EW='E' else EW='W'
if Lat lt 0 then SN='S' else SN='N'

widget_control, ID.ar_data(1), set_val= $
        strtrim(string(EW, abs(Lng), SN, abs(Lat), $
	format="(a1, f6.1, '; ', a1, f5.1)") ,2)

widget_control, ID.ar_data(2), set_val= $
        strtrim(string(entry.region(index).Area),2)

widget_control, ID.ar_data(3), set_val= $
        strtrim(string(entry.region(index).Type),2)

widget_control, ID.ar_data(4), set_val= 'I: '+	$
        strtrim(string(entry.region(index).Leader.ipeak.x),2)+ $
        ', '+strtrim(string(entry.region(index).Leader.ipeak.y),2)+ $
	'; V: '+ $
        strtrim(string(entry.region(index).Leader.vpeak.x),2)+ $
        ', '+strtrim(string(entry.region(index).Leader.vpeak.y),2)

widget_control, ID.ar_data(5), set_val= ' '+$
        strtrim(string(entry.region(index).Leader.ipeak.itb, $
		format='(g9.3)'),2)+ ', '+ $
        strtrim(string(entry.region(index).Leader.ipeak.vtb/	$
		(entry.region(index).Leader.ipeak.itb > 100)*100, $
        format='(f6.1)'),2)+'%'

widget_control, ID.ar_data(6), set_val= ' '+$
        strtrim(string(entry.region(index).Leader.vpeak.itb, $
		format='(g9.3)'),2)+ ', '+ $
        strtrim(string(entry.region(index).Leader.vpeak.vtb/	$
		(entry.region(index).Leader.vpeak.itb > 100)*100, $
        format='(f6.1)'),2)+'%'

widget_control, ID.ar_data(7), set_val= $
        strtrim(string(entry.region(index).leader.Kgauss*0.1, format='(f5.2)'),2)

widget_control, ID.ar_data(8), set_val= 'I: '+	$
        strtrim(string(entry.region(index).Follower.ipeak.x),2)+ $
        ', '+strtrim(string(entry.region(index).Follower.ipeak.y),2)+ $
	'; V: '+ $
        strtrim(string(entry.region(index).Follower.vpeak.x),2)+ $
        ', '+strtrim(string(entry.region(index).Follower.vpeak.y),2)

widget_control, ID.ar_data(9), set_val= ' '+$
        strtrim(string(entry.region(index).Follower.ipeak.itb, $
		format='(g9.3)'),2)+ ', '+ $
        strtrim(string(entry.region(index).Follower.ipeak.vtb/	$
		(entry.region(index).Follower.ipeak.itb > 100)*100, $
        format='(f6.1)'),2)+'%'

widget_control, ID.ar_data(10), set_val= ' '+$
        strtrim(string(entry.region(index).Follower.vpeak.itb, $
		format='(g9.3)'),2)+ ', '+ $
        strtrim(string(entry.region(index).Follower.vpeak.vtb/	$
		(entry.region(index).Follower.vpeak.itb > 100)*100, $
        format='(f6.1)'),2)+'%'

widget_control, ID.ar_data(11), set_val= $
        strtrim(string(entry.region(index).follower.Kgauss*0.1, format='(f5.2)'),2)

widget_control, ID.ar_data(12), set_val= $
        strtrim(string(entry.region(index).Carrlng),2)

        for j=0,1 do widget_control,ID.togglebase(j), map=1-j


ENDIF

        goto,return1
        endif


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

widget_control, ID.Draw(2), get_uval=Ax

wset,ID.Win(2)
scale,Ax,/rec

x=convert_coord(ev.x, ev.y, /dev, /to_data)

N=n_elements(exhaustive)

if N eq 0 then goto,return1

monthnames=     ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', $
                 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

index=exhaustive(fix(x(0)+0.5 > 0 < (N/2-1)))

date=strtrim(db(index,0).date,2)
date=strmid(date,6,2)+' '+monthnames(fix(strmid(date,3,2))-1)

if ID.Input_path ne 1 then widget_control, ID.Coord, set_val= $
   date+'; '+strcompress(string(x(1), format='(g10.3)'))

goto, return1
        endif



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

        CASE uv OF

"DONE":         begin
widget_control, ev.top, /dest

                CASE 1 OF

equiv(instance, [1,0]) and (ID.Instance ge 0): instance=[0,0]

equiv(instance, [1,1]) and (ID.Instance ge 0): instance=[1,0]

                ELSE:
                ENDCASE

                end


'Viewer':	begin

file=pickfile(/read)
if file eq '' then return
a_issue_txt,file=file
	end


'Help':	begin

file=findfile('ardb.hlp')
if file(0) eq '' then begin
widget_control, ID.Info_Label, set_val='Help file not found.'
return
endif
a_issue_txt,file=file(0)
	end

'Header': begin
if n_elements (Header_I) le 0 then return
a_issue_txt,text=header_I
	end


"Date": begin
        widget_control, ev.id, get_val=tmp
ID.Date=strtrim(tmp(0),2)

        widget_control, ev.id, set_val=ID.date

n=(where(db.date eq ID.date))(0)
error=n lt 0

ID.Number=n > 0

if error then err_mess=' This date is beyond the database' else err_mess=''

widget_control, ID.Info_Label, set_val=err_mess

        if error then goto, return1

        widget_control,ID.buttonbase, get_uval=wbc_state
wbc_state.init=1
if Version then widget_control, ID.buttonbase, set_uval=wbc_state, /no_copy $
	else widget_control,ID.buttonbase, set_uval=wbc_state

widget_control, ID.Info_Label, set_val=''

plot_map_grid, ID, db(ID.number), I_Image, I_header, V_Image, V_header, error=error
	if error ne 1 then begin
Image_I=I_Image
Header_I=I_Header
Image_V=V_Image
Header_V=V_Header
	endif

;if (ID.Instance ge 0) and ID.Show_Map then Image_I(*,*,ID.Instance)=temporary(Image)

        end


'Day before': begin

error=(ID.Number eq 0)

if error then err_mess=' This date is beyond the database' else err_mess=''
widget_control, ID.Info_Label, set_val=err_mess

if error then return

        widget_control,ID.buttonbase, get_uval=wbc_state
wbc_state.init=1
if Version then widget_control, ID.buttonbase, set_uval=wbc_state, /no_copy $
	else widget_control,ID.buttonbase, set_uval=wbc_state


n=(ID.Number=ID.Number-1 > 0)

ID.Date=db(ID.Number).Date

widget_control, ID.Info_Label, set_val=''

plot_map_grid, ID, db(ID.number), I_Image, I_header, V_Image, V_header, error=error
	if error ne 1 then begin
Image_I=I_Image
Header_I=I_Header
Image_V=V_Image
Header_V=V_Header
	endif


;if (ID.Instance ge 0) and ID.Show_Map then Image_I(*,*,ID.Instance)=temporary(Image)

        end


'Day after': begin
error=ID.Number eq (n_elements(db)-1)

if error then err_mess=' This date is beyond the database' else err_mess=''
widget_control, ID.Info_Label, set_val=err_mess

if error then return

        widget_control,ID.buttonbase, get_uval=wbc_state
wbc_state.init=1
if Version then widget_control, ID.buttonbase, set_uval=wbc_state, /no_copy $
	else widget_control,ID.buttonbase, set_uval=wbc_state


ID.Number=ID.Number+1 < (n_elements(db)-1)

ID.Date=db(ID.Number).Date

widget_control, ID.Info_Label, set_val=''

plot_map_grid, ID, db(ID.number), I_Image, I_header, V_Image, V_header, error=error
	if error ne 1 then begin
Image_I=I_Image
Header_I=I_Header
Image_V=V_Image
Header_V=V_Header
	endif

;if (ID.Instance ge 0) and ID.Show_Map then Image_I(*,*,ID.Instance)=temporary(Image)

        end


'Xloadct':      Xloadct

'Criterion':   begin
        widget_control, ID.Criterion_List, sens=0

	if ID.All_Criteria(ev.index) eq 'Tbr' then begin
widget_control, ID.Tbr_type_button, sens=1
for j=0,2 do widget_control,ID.typebase(j),map=([1,0,0])(j)
return
	endif

	if ID.All_Criteria(ev.index) eq 'Polarization' then begin
widget_control, ID.Pol_type_button, sens=1
for j=0,2 do widget_control,ID.typebase(j),map=([0,1,0])(j)
return
	endif

	if ID.All_Criteria(ev.index) eq 'Magnetic field' then begin
widget_control, ID.MF_type_button, sens=1
for j=0,2 do widget_control,ID.typebase(j),map=([0,0,1])(j)
ID.Bounds=ID.Bounds*10
goto, return1
	endif

Flag = ID.All_Criteria(ev.index) eq 'Name'

widget_control, ID.Itb_button, sens=Flag
widget_control, ID.Area_button, sens=Flag
widget_control, ID.Pol_button, sens=Flag
widget_control, ID.Lat_button, sens=Flag
widget_control, ID.Kgauss_button, sens=Flag

ID.Input=[0,0]
ID.Bounds=ID.Bounds(sort(ID.Bounds))

subscript=db_filter(db, ID.All_Criteria(ev.index), ID.Bounds, error=error, $
        inverse=ID.Inverse, exhaustive=exhaustive, absolute=ID.Abs_val, $
	exclusiv=ID.Exclusively)

N_s=n_elements(subscript)
N_e=n_elements(ID.Entries)

ID.Entries(*)=0
ID.Entries(0:(N_s-1)> 0 <(N_e-1))= subscript(0:(N_s-1)> 0 <(N_e-1))

        if error then begin
widget_control, ID.Info_Label, set_val= 'No entries.'
widget_control, ID.List, set_val=['']
return
        endif else begin

widget_control, ID.Info_Label, set_val= strtrim(N_s, 2)+' entries found.'
widget_control, ID.List, set_val=strtrim(db(subscript).date,2)

        endelse

                end


'Bound0':       begin

        widget_control, ev.id, get_val=tmp
lower=tmp(0)

bf=byte(lower)
digit=where(bf le 57 and bf ge 48)
        if digit(0) lt 0 then begin
        lower=strmid(strcompress(strupcase(lower), /rem),0,1)
        ar_type=['A', 'B', 'G', 'D']
        lower=(where(lower eq ar_type))(0)
        endif

ID.Bounds(0)=strcompress(lower,/rem)
        widget_control, ID.Search_Data(1), /inp

ID.Input(0)=1

        widget_control, ID.Criterion_List, sens=equiv(ID.Input, [1,1])

                end

'Bound1':       begin
        widget_control, ev.id, get_val=tmp
upper=tmp(0)

bf=byte(upper)
digit=where(bf le 57 and bf ge 48)
        if digit(0) lt 0 then begin
        upper=strmid(strcompress(strupcase(upper), /rem),0,1)
        ar_type=['A', 'B', 'G', 'D']
        upper=(where(upper eq ar_type))(0)
        endif

ID.Bounds(1)=strcompress(upper, /rem)
        widget_control, ID.Search_Data(0), /inp

ID.Input(1)=1

        widget_control, ID.Criterion_List, sens=equiv(ID.Input, [1,1])


                end


'Inverse':      ID.Inverse=ev.select

'Abs_val':	ID.Abs_val=ev.select

'Exclusively':	ID.Exclusively=ev.select

'Go to':        begin
ID.Number=ID.entries(ev.index > 0)

widget_control, ID.Info_Label, set_val=''

ID.Date=db(ID.Number).Date

plot_map_grid, ID, db(ID.number), I_Image, I_header, V_Image, V_header, error=error
	if error ne 1 then begin
Image_I=I_Image
Header_I=I_Header
Image_V=V_Image
Header_V=V_Header
	endif

;if (ID.Instance ge 0) and ID.Show_Map then Image_I(*,*,ID.Instance)=temporary(Image)

                end

'Calculator':   Wcalc

'Plot_b':       begin
        for j=0,2 do widget_control,ID.drawbase(j), map=([0,0,1])(j)
	wset,ID.Win(2)

                end


'Map_bI':        begin
        for j=0,2 do widget_control,ID.drawbase(j), map=([1,0,0])(j)
	wset,ID.Win(0)
                end

'Map_bV':        begin
        for j=0,2 do widget_control,ID.drawbase(j), map=([0,1,0])(j)
	wset,ID.Win(1)
                end

'Current date':  for j=0,1 do widget_control,ID.togglebase(j), map=1-j

'Search':       for j=0,1 do widget_control,ID.togglebase(j), map=j

'Show AR':      	for j=0,db(ID.Number).nar-1 do  $
		xyouts, db(ID.Number).region(j).location.x, $
        		db(ID.Number).region(j).location.y, $
        		db(ID.Number).region(j).name,   /dev


"Don't show AR":        begin
ID.Show_Num=0
val='Show AR always'

widget_control, ID.Show_AR(1), set_val=val, set_uval=val
widget_control, ID.Show_AR(0), sens=1


                        end

'Show AR always':       begin

ID.Show_Num=1

val="Don't show AR"

widget_control, ID.Show_AR(1), set_val=val, set_uval=val
widget_control, ID.Show_AR(0), sens=0

for j=0,db(ID.Number).nar-1 do begin

xr=db(ID.Number).region(j).location.x
yr=db(ID.Number).region(j).location.y
xyouts,xr,yr,db(ID.Number).region(j).name,/dev

        endfor

           end


'Show Image':   begin

if ID.path eq '' then begin
widget_control, ID.Info_Label,set_val='Please indicate a path (File - Image path)'
return
endif

	ID.Show_Map=1

plot_map_grid, ID, db(ID.number), I_Image, I_header, V_Image, V_header, error=error
	if error ne 1 then begin
Image_I=I_Image
Header_I=I_Header
Image_V=V_Image
Header_V=V_Header
	endif

	;if (ID.Instance ge 0) and ID.Show_Map then Image_I(*,*,ID.Instance)=temporary(Image)

	ID.Show_Map=0
;endif

                end

"Don't show image":     begin
ID.Show_Map=0
val='Show Image always'

widget_control, ID.Show_Image(1), set_val=val, set_uval=val
widget_control, ID.Show_Image(0), sens=1

                        end

'Show Image always':    begin

ID.Show_Map=1

if ID.path eq '' then begin
widget_control, ID.Info_Label,set_val='Please indicate a path (File - Image path)'
goto, return1
endif


val="Don't show image"

widget_control, ID.Show_Image(1), set_val=val, set_uval=val
widget_control, ID.Show_Image(0), sens=0

plot_map_grid, ID, db(ID.number), I_Image, I_header, V_Image, V_header, error=error
	if error ne 1 then begin
Image_I=I_Image
Header_I=I_Header
Image_V=V_Image
Header_V=V_Header
	endif

;if (ID.Instance ge 0) and ID.Show_Map then Image_I(*,*,ID.Instance)=temporary(Image)

           end

'Nest': ardb

'Polarization':	begin

                     N=n_elements(exhaustive)

if N eq 0 then return

        profile_F=(profile_L=fltarr(N/2))

        for j=0,N/2-1 do begin
profile_L(j)=	db(exhaustive(j,0)).region(exhaustive(j,1)).leader.ipeak.vtb/ $
		(db(exhaustive(j,0)).region(exhaustive(j,1)).leader.ipeak.itb > 400)
profile_F(j)=	db(exhaustive(j,0)).region(exhaustive(j,1)).follower.ipeak.vtb/ $
		(db(exhaustive(j,0)).region(exhaustive(j,1)).follower.ipeak.itb > 400)
        endfor

amax=max(profile_L, min=amin_L) > max(profile_F, min=amin_F)
amin=amin_L < amin_F

wset,ID.Win(2)

plot, profile_L, back=!d.n_colors-1, col=0, yran=[amin, amax], tit= $
        strcompress('NOAA'+string(fix(ID.Bounds(0))))+$
	', degree of polarization', psym=-1, /noc

oplot, profile_F, col=0, linest=1, psym=-5, /noc
plots, !x.crange, [0,0], linest=3, col=0

scale,tmp,/mem
empty

if Version then widget_control, ID.Draw(2), set_uval=tmp, /no_copy $
	else widget_control,ID.Draw(2), set_uval=tmp

        end

'I_Tb': begin
                     N=n_elements(exhaustive)

if N eq 0 then return

        profile_F=(profile_L=fltarr(N/2))

        for j=0,N/2-1 do begin
profile_L(j)=db(exhaustive(j,0)).region(exhaustive(j,1)).leader.ipeak.itb
profile_F(j)=db(exhaustive(j,0)).region(exhaustive(j,1)).follower.ipeak.itb
        endfor

amax=max(profile_L, min=amin_L) > max(profile_F, min=amin_F)
amin=amin_L < amin_F

wset,ID.Win(2)

plot, profile_L, back=!d.n_colors-1, col=0, yran=[amin, amax] > 0, tit= $
        strcompress('NOAA'+string(fix(ID.Bounds(0))))+$
	', brightness temperature', psym=-1, /noc
oplot, profile_F, col=0, linest=1, psym=-5, /noc

scale,tmp,/mem
empty
if Version then widget_control, ID.Draw(2), set_uval=tmp, /no_copy $
	else widget_control,ID.Draw(2), set_uval=tmp

        end


'Kgauss_plot':	 begin
                     N=n_elements(exhaustive)

if N eq 0 then return

        profile_F=(profile_L=fltarr(N/2))

        for j=0,N/2-1 do begin
profile_L(j)=db(exhaustive(j,0)).region(exhaustive(j,1)).leader.kgauss*0.1
profile_F(j)=db(exhaustive(j,0)).region(exhaustive(j,1)).follower.kgauss*0.1
        endfor

amax=max(profile_L, min=amin_L) > max(profile_F, min=amin_F)
amin=amin_L < amin_F

wset,ID.Win(2)

plot, profile_L, back=!d.n_colors-1, col=0, yran=[amin, amax], tit= $
        strcompress('NOAA'+string(fix(ID.Bounds(0))))+$
	', magnetic field', psym=-1, /noc
oplot, profile_F, col=0, linest=1, psym=-5, /noc

scale,tmp,/mem
empty
if Version then widget_control, ID.Draw(2), set_uval=tmp, /no_copy $
	else widget_control,ID.Draw(2), set_uval=tmp

        end


'Area': begin
                     N=n_elements(exhaustive)

if N eq 0 then return

        profile=fltarr(N/2)

for j=0,N/2-1 do profile(j)=db(exhaustive(j,0)).region(exhaustive(j,1)).area

wset,ID.Win(2)

plot, profile, back=!d.n_colors-1, col=0, tit= $
        strcompress('NOAA'+string(fix(ID.Bounds(0))))+	$
	', area', psym=-1, /noc

scale,tmp,/mem
empty
if Version then widget_control, ID.Draw(2), set_uval=tmp, /no_copy $
	else widget_control,ID.Draw(2), set_uval=tmp

        end


'Latitude_plot': begin
                     N=n_elements(exhaustive)

if N eq 0 then return

        profile=fltarr(N/2)

for j=0,N/2-1 do $
	profile(j)=db(exhaustive(j,0)).region(exhaustive(j,1)).location.lat

wset,ID.Win(2)

plot, profile, back=!d.n_colors-1, col=0, tit= $
        strcompress('NOAA'+string(fix(ID.Bounds(0))))+	$
	', latitude', psym=-1, /yno, /noc

scale,tmp,/mem
empty
if Version then widget_control, ID.Draw(2), set_uval=tmp, /no_copy $
	else widget_control,ID.Draw(2), set_uval=tmp

        end


'Zoom':	begin

        widget_control,ID.buttonbase, get_uval=a

if (a.xy(1,0)-a.xy(0,0) eq 0) or (a.xy(1,1)-a.xy(0,1) eq 0) then begin
widget_control, ID.Info_Label, set_val='Please mark an area'
goto, return1
endif

ID.Zoom_state=1
device, /cursor_cross

widget_control, ID.Zoom, set_val='Unzoom', set_uval='Unzoom'

ID.Factor=!d.x_size/(a.xy(1,0)-a.xy(0,0)) < !d.y_size/(a.xy(1,1)-a.xy(0,1))

	FOR Win=0,1 DO BEGIN

wset,ID.Win(Win)

erase

if ID.No_Image ne 1 then begin

	if Win then Image=Image_V else Image=Image_I

tvscl,rebin(image(a.xy(0,0):a.xy(1,0),a.xy(0,1):a.xy(1,1)), $
	(a.xy(1,0)-a.xy(0,0)+1)*ID.Factor, (a.xy(1,1)-a.xy(0,1)+1)*ID.Factor)

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

entry=db(ID.number)

Center=[256-a.xy(0,0), 256-a.xy(0,1)]*ID.Factor

Radius=entry.rsun/entry.pixsz*ID.factor

!x.range=([0., !d.x_size]-Center(0))/Radius
!y.range=([0., !d.y_size]-Center(1))/Radius

map_set,entry.B0,0,0, $ ;/grid, glinestyle=1,      $
        /ortho, /noerase, pos=[0,0,1,1], /nobor;, latdel=10, londel=10

!x.s=[Center(0), Radius] / float(!d.x_size)
!y.s=[Center(1), 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

!P.clip=P_clip_save

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

bias=[a.xy(0,0), a.xy(0,1)]

if ID.Show_Num then for j=0,entry.nar-1 do begin

xr=(entry.region(j).location.x-bias(0))*float(ID.Factor)
yr=(entry.region(j).location.y-bias(1))*float(ID.Factor)

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,yr,entry.region(j).name, align=0.5, /dev

        endfor

scale,tmp,/mem

if Version then widget_control, ID.Draw(Win), set_uval=tmp, /no_copy $
	else widget_control,ID.Draw(Win), set_uval=tmp

a.init=1

widget_control,ID.buttonbase, set_uval=a



	ENDFOR


empty

	end

'Unzoom':  begin


widget_control, ID.Zoom, set_val='Zoom', set_uval='Zoom'

        widget_control,ID.buttonbase, get_uval=wbc_state
wbc_state.init=1
if Version then widget_control, ID.buttonbase, set_uval=wbc_state, /no_copy $
	else widget_control,ID.buttonbase, set_uval=wbc_state

ID.Zoom_state=0

ID.Factor=1

plot_map_grid, ID, db(ID.number), I_Image, I_header, V_Image, V_header, error=error
	if error ne 1 then begin
Image_I=I_Image
Header_I=I_Header
Image_V=V_Image
Header_V=V_Header
	endif


	   end


'Gif':	begin

if !d.window eq ID.Win(0) then $
	F_name=strmid(ID.Date,0,2)+strmid(ID.Date,3,2)+strmid(ID.Date,6,2) $
	else F_name=strtrim(db(exhaustive(0,0)).region(exhaustive(0,1)).name,2)


file=pickfile(/write, filt='*.gif', file=F_name+'.gif')
	if file eq '' then return
widget_control,/hour
write_gif,file,tvrd()

	end

'Window':	begin

win=!d.window

if win eq ID.Win(0) then $
	F_name=strmid(ID.Date,0,2)+strmid(ID.Date,3,2)+strmid(ID.Date,6,2) $
	else F_name='NOAA'+strtrim(db(exhaustive(0,0)).region(exhaustive(0,1)).name,2)


	window,/free,xs=512,ys=512,tit=F_name
device, copy=[0,0,512,512,0,0,win]

		end


'Image path':	begin
path=pickfile(tit='Please select a file to indicate path', path=ID.Path)
if path eq '' then return
widget_control, ID.Info_Label,set_val=subdirec(subdirec(path)),/input
widget_control, ID.Coord,set_val='Please confirm'
ID.Input_Path=1
		end


'Input Image path': 	begin
if ID.Input_Path ne 1 then return
widget_control, ID.Info_Label,get_val=path
image_path=strcompress(path(0),/rem)
widget_control, ID.Info_Label,set_val='Path: '+image_path, input=0
widget_control, ID.Coord,set_val=' '
ID.Path=image_path+Delim
ID.Input_Path=0


Ini_File=(findfile('ardb.ini'))(0)

IF Ini_File ne '' THEN BEGIN

        openr,lun, Ini_File, /get_lun
temp=''

j=-1
        while not EOF(lun) do begin
j=j+1
readf,lun, temp
if j eq 0 then s=temp else s=[s, temp]
        endwhile

        free_lun, lun

ind=(where(strlowcase(strmid(s,0,10)) eq 'image_path'))(0) > 0

ENDIF ELSE BEGIN
s=strarr(1)
ind=0
ENDELSE
s(ind)='IMAGE_PATH '+image_path

	openw, lun, 'ardb.ini', /get
for j=0, n_elements(s)-1 do printf,lun,s(j)
	free_lun,lun

			end

'Cur_date_save':	begin

Dat_name=strmid(ID.Date,0,2)+strmid(ID.Date,3,2)+strmid(ID.Date,6,2)

	file=pickfile(tit='Enter a filename to record text data', $
		file=Dat_name+'.dte', filt='*.dte')
	if file eq '' then return

entry=db(ID.Number)

Cur_date_save, entry, file

			end

'AR_save': begin

	if n_elements(exhaustive) lt 2 then begin
widget_control, ID.Info_Label, set_val='You should previously specify AR!'
return
	endif

	N=n_elements(exhaustive(*,0))

	data_array=replicate(db(0).region(0), N)
	for j=0,N-1 do data_array(j)=db(exhaustive(j,0)).region(exhaustive(j,1))

AR_name=strtrim(data_array(0).name,2)

	file=pickfile(tit='Enter a filename to record text data', $
		file=AR_name+'.ar', filt='*.ar')
	if file eq '' then return


	AR_data_save, file, data_array, AR_name

	   end


        ELSE:
        ENDCASE


stm=strmid(uv, 0, 2)

if stm eq 'L_' or stm eq 'F_' or stm eq 'M_' then begin
type=strmid(uv, 2,2)

ID.Input=[0,0]
ID.Bounds=ID.Bounds(sort(ID.Bounds))

	CASE 1 OF
strlen(uv) eq 4: 	crit='brig'
strmid(uv,2,1) eq 'K': 	crit='magn'
	ELSE:		crit='polarization'
	ENDCASE

widget_control, ID.Tbr_type_button, sens=0
widget_control, ID.Pol_type_button, sens=0
widget_control, ID.MF_type_button, sens=0

subscript=db_filter(db, crit, ID.Bounds, type=type, error=error, $
        inverse=ID.Inverse, exhaustive=exhaustive, absolute=ID.Abs_val, $
	leader=stm eq 'L_', follower=stm eq 'F_', map=stm eq 'M_')

N_s=n_elements(subscript)
N_e=n_elements(ID.Entries)

ID.Entries(*)=0
ID.Entries(0:(N_s-1)> 0 <(N_e-1))= subscript(0:(N_s-1)> 0 <(N_e-1))

        if error then begin
widget_control, ID.Info_Label, set_val= 'No entries.'
widget_control, ID.List, set_val=['']
goto, return1
        endif else begin

widget_control, ID.Info_Label, set_val= strtrim(N_s, 2)+' entries found.'

widget_control, ID.List, set_val=strtrim(db(subscript).date,2)
        endelse


endif

return1:

if uv ne "DONE" then if Version then $
        widget_control,ev.top, set_uval=ID, /no_copy else $
        widget_control,ev.top, set_uval=ID


        empty

        end




        pro ardb

common ardb, db, image_I, image_V, instance, exhaustive, header_I, header_V


Version=strmid(!version.release,0,3) gt 3.5

Image_I=(Image_V=fltarr(512,512))

all_files=['ardb.hlp', 'w_box_cursor.pro', 'db_filter.pro', 'rfits.pro', $
	'mkkey_struct.pro', 'dg_make_struct.pro']


N_files=n_elements(all_files)

error=strarr(N_files)

for j=0, N_files-1 do error(j)=findfile(all_files(j))

err_ind=where(error eq '')

	if err_ind(0) ge 0 then begin
print
print,'Error - missing files:'
print
for j=0, n_elements(err_ind)-1 do print,all_files(err_ind(j))
print
print,'Please place these files into the current directory.'
print
return
	endif


s=dg_make_struct()

	if n_elements(db) lt 10 then begin
db_file=(findfile('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(instance) le 0 then begin
instance=[0,0]
;Image_I=(Image_V=fltarr(512,512,2))
endif

        CASE 1 OF
equiv(instance, [0,0]):         begin
                        instance=[1,0]
                        Flag=0
;               Image_I=(Image_V=fltarr(512,512,2))
                                end

equiv(instance, [1,0]):         begin
                        instance=[1,1]
                        Flag=1
;               Image_I(*,*,1)=(Image_V(*,*,1)=fltarr(512,512))
                                end

equiv(instance, [1,1]):         begin
                        Flag=-1
                                end

        ELSE:
        ENDCASE


ID={Draw:lonarr(3), Win:lonarr(3), Coord:0L, Text:lonarr(10), Drawbase:lonarr(3), $
        tog_button:0L, togglebase:[0L,0L], textbase:[0L,0L], $
        text_button:lonarr(4), ar_data:lonarr(13), Info_Label:0L, $
        Show_AR:lonarr(3), Show_Image:lonarr(3), $
        Search_label:[0L,0L], Search_data:[0L,0L], $
        List:0L, Criterion_List:0L, Tb_Label:0L, $
	Tbr_type_button:0L, Pol_type_button:0L, MF_type_button:0L, $
	buttonbase:0L, 	Zoom:0L, typebase: [0L, 0L, 0L], $

	Itb_button:0L, Area_button:0L, $
	Pol_button:0L, Lat_button:0L, Kgauss_button:0L, $

        Date:'92/07/01', Number:0L, Show_Num:0, Show_Map:0, $
        Criterion:'', Bounds:[0d0,0d0], Inverse:0, Abs_val:0, Exclusively:0, $
        Entries:lonarr(n_elements(db)), Input:[0,0], Path:'', $
        Instance:Flag,  Factor:1, Zoom_state:0, No_Image:1, $
	Input_path:0, $

        All_Criteria:   $
        ['Name', 'Area', 'Type', 'Latitude', 'CarrLng','Longitude', $
	'Polarization', 'Tbr', 'Magnetic field']}

Ini_File=(findfile('ardb.ini'))(0)


        CASE !version.OS OF

'windows':      Delim='\'

'Win32':        Delim='\'

        ELSE:   Delim='/'

	ENDCASE


IF Ini_File ne '' THEN BEGIN

        openr,lun, Ini_File, /get_lun
temp=''

j=-1
        while not EOF(lun) do begin
j=j+1
readf,lun, temp
if j eq 0 then s=temp else s=[s, temp]
        endwhile

        free_lun, lun

ind=(where(strlowcase(strmid(s,0,10)) eq 'image_path'))(0) > 0
ID.path=strcompress(strmid(s(ind), 11, strlen(s(ind))-10),/rem)+Delim

ENDIF

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

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

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

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

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


N=16
a=findgen(N)*(!Pi*2/(N-1))
usersym,cos(a),sin(a)

emptys=string(replicate('20'xb,10))
emptys1_7=string(replicate('20'xb,17))
emptys2=string(replicate('20'xb,20))
emptys3=string(replicate('20'xb,30))
emptys4=string(replicate('20'xb,40))

device,get_scr=screen

mainbase=widget_base(tit='Database of active regions', /colu, uval=ID)

ID.buttonbase=widget_base(mainbase, /row, uval=wbc_state)

butval=['DONE', 'File', 'Help']


junk=widget_button(ID.buttonbase, val='File', /menu)
button=widget_button(junk, val='DONE', uval='DONE')
button=widget_button(junk, val='Header', uval='Header')
button=widget_button(junk, val='Image path', uval='Image path')
	junk1=widget_button(junk, val='Save', /menu)
		junk2=widget_button(junk1, val='Data', /menu)
			button=widget_button(junk2, val='Current date', uval='Cur_date_save')
			button=widget_button(junk2, val='Active region', uval='AR_save')
		junk2=widget_button(junk1, val='Image', /menu)
			button=widget_button(junk2, val='Gif', uval='Gif')
			button=widget_button(junk2, val='PS', uval='PS')
			button=widget_button(junk2, val='Window', uval='Window')
button=widget_button(junk, val='Viewer', uval='Viewer')

button=widget_button(ID.buttonbase, val='?', uval='Help')


ID.Coord=widget_label(ID.buttonbase, val=emptys4, /fra)

        button0=widget_button(ID.buttonbase, val='Tools', /menu)
button=widget_button(button0, val='Colors', uval='Xloadct')
button=widget_button(button0, val='Calculator', uval='Calculator')
ID.Show_AR(0)=widget_button(button0, val='Show AR', uval='Show AR')
ID.Show_AR(1)=widget_button(button0, val='Show AR always', uval='Show AR always')
ID.Show_Image(0)=widget_button(button0, val='Show Image', uval='Show Image')
ID.Show_Image(1)=widget_button(button0, val='Show Image always', uval='Show Image always')
ID.Zoom=widget_button(button0, val='Zoom', uval='Zoom')

button=widget_button(button0, val='Nest', uval='Nest')

        ID.tog_button=widget_button(ID.buttonbase, val='Window', /menu)
        	button=widget_button(ID.tog_button, val='Map I', uval='Map_bI')
        	button=widget_button(ID.tog_button, val='Map V', uval='Map_bV')
        	button=widget_button(ID.tog_button, val='Plot', uval='Plot_b')

db_base=widget_base(mainbase, /row)
Left_base=widget_base(db_base, /colu)


Base_for_label=widget_base(Left_base,/row)
plainbase0=widget_base(Left_base)

right_base=widget_base(db_base,/colu)
right_base1=widget_base(right_base,/colu)
plainbase1=widget_base(right_base)

        for j=0,1 do begin
if (screen(1) lt 700) then $
ID.togglebase(j)=widget_base(plainbase1, /colu, /scroll, $
	y_scroll=screen(1)*0.7, x_scroll=screen(0)*0.28) else $
ID.togglebase(j)=widget_base(plainbase1, /colu)

ID.textbase(j)=widget_base(ID.togglebase(j), /colu,/fra)
widget_control,ID.togglebase(j), map=1-j
        endfor

        button=widget_button(ID.buttonbase, val='Panel', /menu)

butval([0,1,2])=['Current date', 'Search', '3']


for j=0, 1 do $
        ID.text_button(j)=widget_button(button, val=butval(j), uval=butval(j))

ID.Info_Label=widget_text(ID.buttonbase, val=emptys3,xs=50, /fra, /edit, $
	uv='Input Image path')

names=['Date', 'Pos. Ang.', 'B0', 'NAR', 'I_Tb_Peak', 'V_Tb_Peak', 'KGauss']

name_label=lonarr(n_elements(names))

        for j=0,n_elements(names)-1 do begin
junk=widget_base(([right_base1, ID.textbase(0)])(([0,1,1,1,1,1,1])(j)), /row, $
	fra=([1,0,0,0,0,0,0])(j) )
if j eq 0 then left_but=widget_button(junk, val=left_button, uval='Day before')
name_label(j)=widget_label(junk, val=emptys2)
if j ne 0 then ID.text(j)=widget_text(junk, /edit, val=emptys2, uval=names(j)) $
        else ID.text(j)=widget_text(junk, /edit, val=emptys, uval=names(j),xs=12)
if j eq 0 then right_but=widget_button(junk, val=right_button, uval='Day after')
        endfor


if screen(1) le 600 then Base_for_label=right_base1

Label_Base=widget_base(Base_for_label, /row)
label=widget_label(Label_Base, val='I_Tb, V_Tb: ')
ID.Tb_Label=widget_label(Label_Base, val=emptys2+emptys2, /fra)

        for j=0,2 do begin
ID.drawbase(j)=widget_base(plainbase0, /colu)
if j eq 2 then Drawbase1=widget_base(ID.Drawbase(j),/row)
ID.draw(j)=widget_draw(ID.drawbase(j), xs=512, ys=512, /motion, /button, $
        uval=Ax)
widget_control,ID.drawbase(j), map=([1,0,0])(j)
        endfor

ID.Itb_button=widget_button(Drawbase1, val='I_Tb', uval='I_Tb')
ID.Area_button=widget_button(Drawbase1, val='Area', uval='Area')
ID.Pol_button=widget_button(Drawbase1, val='Polarization', uval='Polarization')
ID.Lat_button=widget_button(Drawbase1, val='Latitude', uval='Latitude_plot')
ID.Kgauss_button=widget_button(Drawbase1, val='Kgauss', uval='Kgauss_plot')



dummy_base=widget_base(ID.textbase(0), /row ,/fra)

AR_names=['Name', 'Location', 'Area', 'Type', 'Leader:', $
	'    I_Tb', '    V_Tb', 'KGauss', $
        'Follower:', 	$
	'    I_Tb', '    V_Tb', 'KGauss', $
	'CarrLong']

AR_label=lonarr(n_elements(AR_names))


        for j=0,n_elements(AR_names)-1 do begin
junk=widget_base(ID.textbase(0), /row, fra=0)
AR_label(j)=widget_label(junk, val=emptys1_7)
ID.ar_data(j)=widget_text(junk, val=emptys, uval='',xs=24)
        endfor

        for j=0,1 do begin
junk=widget_base(ID.textbase(1), /row, /fra)
ID.Search_label(j)=widget_label(junk, val=(['Lower', 'Upper'])(j)+':  ')
ID.Search_data(j)=widget_text(junk, val=emptys, uval='Bound'+strtrim(j,2), /edit)
        endfor

junk=widget_base(ID.textbase(1), /row)
junk1=widget_base(junk, /colu)
Crit_lab=widget_label(junk1, val='  Criterion:  ')
junk2=widget_base(junk1, /colu, /nonexcl)
Inv_button=widget_button(junk2, val='Inverse', uval='Inverse')
Abs_button=widget_button(junk2, val='Abs. value', uval='Abs_val')
Exc_button=widget_button(junk2, val='Exclusively', uval='Exclusively')
ID.Criterion_List=widget_list(junk, val=ID.All_Criteria, uval='Criterion', ys=1, /fra)
        widget_control, ID.Criterion_List, sens=0

Plainbuttonbase=widget_base(junk1)
for j=0,2 do ID.typebase(j)=widget_base(Plainbuttonbase,/row)

ID.Tbr_type_button=widget_button(ID.typebase(0), val='Type', /menu)
	button=widget_button(ID.Tbr_type_button, val='Leader', /menu)
		button1=widget_button(button, val='Ipeak', /menu)
			button2=widget_button(button1, val='I_Tb',uval='L_II')
			button2=widget_button(button1, val='V_Tb',uval='L_IV')
		button1=widget_button(button, val='Vpeak', /menu)
			button2=widget_button(button1, val='I_Tb',uval='L_VI')
			button2=widget_button(button1, val='V_Tb',uval='L_VV')
	button=widget_button(ID.Tbr_type_button, val='Follower', /menu)
		button1=widget_button(button, val='Ipeak', /menu)
			button2=widget_button(button1, val='I_Tb',uval='F_II')
			button2=widget_button(button1, val='V_Tb',uval='F_IV')
		button1=widget_button(button, val='Vpeak', /menu)
			button2=widget_button(button1, val='I_Tb',uval='F_VI')
			button2=widget_button(button1, val='V_Tb',uval='F_VV')
	button=widget_button(ID.Tbr_type_button, val='Map', /menu)
		button1=widget_button(button, val='Ipeak', /menu)
			button2=widget_button(button1, val='I_Tb',uval='M_II')
			button2=widget_button(button1, val='V_Tb',uval='M_IV')
		button1=widget_button(button, val='Vpeak', /menu)
			button2=widget_button(button1, val='I_Tb',uval='M_VI')
			button2=widget_button(button1, val='V_Tb',uval='M_VV')

ID.Pol_type_button=widget_button(ID.typebase(1), val='Type', /menu)
	button=widget_button(ID.Pol_type_button, val='Leader', /menu)
			button1=widget_button(button, val='Ipeak',uval='L_I')
			button1=widget_button(button, val='Vpeak',uval='L_I')
	button=widget_button(ID.Pol_type_button, val='Follower', /menu)
			button1=widget_button(button, val='Ipeak',uval='F_I')
			button1=widget_button(button, val='Vpeak',uval='F_I')
	button=widget_button(ID.Pol_type_button, val='Map', /menu)
			button1=widget_button(button, val='Ipeak',uval='M_I')
			button1=widget_button(button, val='Vpeak',uval='M_I')

ID.MF_type_button=widget_button(ID.typebase(2), val='Type', /menu)
	button=widget_button(ID.MF_type_button, val='Leader', uval='L_K')
	button=widget_button(ID.MF_type_button, val='Follower', uval='F_K')
	button=widget_button(ID.MF_type_button, val='Map', uval='M_K')

widget_control, ID.Tbr_type_button, sens=0
widget_control, ID.Pol_type_button, sens=0
widget_control, ID.MF_type_button, sens=0

for j=0,2 do widget_control,ID.typebase(j),map=([1,0,0])(j)

ID.List=widget_list(ID.textbase(1), val=replicate(' ',10), uval='Go to',/fra, ys=10)

widget_control, mainbase, /real, /hour

for j=0,n_elements(names)-1 do widget_control, name_label(j),set_val=names(j)
for j=0,n_elements(AR_names)-1 do widget_control, AR_label(j),set_val=AR_names(j)

widget_control, ID.Itb_button, sens=0
widget_control, ID.Area_button, sens=0
widget_control, ID.Pol_button, sens=0
widget_control, ID.Lat_button, sens=0
widget_control, ID.Kgauss_button, sens=0

        for j=0,2 do begin
widget_control, ID.draw(j), get_val=win
ID.Win(j)=win
        endfor


        for j=0,1 do begin

wset,ID.Win(j)

plot_map_grid, ID, db(ID.number), I_Image, I_header, V_Image, V_header, error=error
	if error ne 1 then begin
Image_I=I_Image
Header_I=I_Header
Image_V=V_Image
Header_V=V_Header
	endif


	endfor

;if (ID.Instance ge 0) and ID.Show_Map then Image_I(*,*,ID.Instance)=temporary(Image)

wset,ID.Win(2)

plot,findgen(10),/nod,xst=4,yst=4,back=!d.n_colors-1

scale,tmp,/mem
empty

if Version then widget_control, ID.Draw(2), set_uval=tmp, /no_copy $
	else widget_control, ID.Draw(2), set_uval=tmp

widget_control, ID.text(0), set_val=ID.Date

if Version then widget_control,mainbase, set_uval=ID, /no_copy else $
        widget_control,mainbase, set_uval=ID

	xmanager, 'ardb', mainbase


        end
