; Copyright (c) 1998-2010 A.P. Hitchcock  All rights reserved
;+
;NAME:
;	AX_SDF
;
;LAST CHANGED: ----------------------------------- 20-Jul-10
;
;PURPOSE:
;	This set of procedures is a widget to read in files
; in the ascii self defining format (SDF) developed initally for ALS BL 5.3.2 STXM
; The widget uses read_sdf and parse_sdf to perform the file input.
;
;CATEGORY:
;	AXIS: utility
;
; AX_SDF_EVENT ROUTINES:
; TEXT_SDF_FNAME		modify filename; when changed, execute BUT_SDF_FNAME_BROWSE to get parameters
; BUT_SDF_FNAME_BROWSE 	browse for new filename; if found, load new parameters
; BUT_SDF_DISPLAY_HDR   show parameters
; BUT_SDF_OK			proceed with loading SDF file into AXIS
; BUT_SDF_CANCEL		abort read in
; WID_DLIST_CHANNEL		drop list used to display and select the data channel
; WID_DLIST_REGION		drop list to display and select region
;
;COMMON BLOCKS:
;	@AXIS_COM	standard set of common blocks
;	@SDF_COM 	structure with header info
;
;MODIFICATION HISTORY:
; (25-feb-01 aph) first written using IDL 5.2 GUIBuilder
; (27-jun-01 aph) add groupID for better feedback on reads
; (29-jul-01 aph) add regions selector to the panel; introduced sdf_com; reverted to pickfile2
; (19-aug-01 aph) adapt to regions change & change in structure names; define regions & channels
; (04-nov-01 aph) add sdf_lastfile to sdf_com to save last name
; (09-nov-01 aph) activate all_regions read-in for spectra and images
; (26-nov-01 aph) complete use of name label as a way to select file
; 				  add specific image read-in for stacks - incomplete !!
; (31-dec-01 aph) make defaut width of display parameters = 132
; (17-feb-02 aph) 'motor scan'; single images from sequences
; (11-dec-02 aph) VLM images read-in
; (17-oct-04 aph) separate PATH box; reload last file info  if possible
; (28-oct-04 aph) ensure works if SDF_Path or SDF_lastfile are not defined (at startup)
; (19-feb-05 aph) stack-map, single image read in added
; (12-mar-05 aph) make single image read-in work with IDL5.2
; (17-Jun-05 aph) channels and regions working for map and 1-image read-in
; (13-Nov-05 aph) got rid of last call involving 'group=SDF_ID'
; (22-Mar-06 aph) add Normalize indicator on panel
; (11-mar-07 aph) add normalization control to panel (sets norm_flag)
; (18-May-07 aph) correct read-in of 1-image for multi-channel data (B. Watts)
; (01-Feb-09 aph) add xy_correct option
; (19-Feb-10 aph) correct number of channels in print tag
; (20-Jul-10 aph) replace TEXT_SDF_(CHANNEL, REGION, IMAGE)
;                 with WID_DLIST_(CHANNEL, REGION, IMAGE) (routine name error)
;-

pro TEXT_SDF_FNAME, Event
on_error,2
@axis_com
@sdf_com
; get the changed name
id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_FNAME')
WIDGET_CONTROL,id, Sensitive=1, Get_value = fname
IF fname(0) EQ sdf_lastfile then return
; generate full name with path
pfname = sdf_path + fname

; -- check if the filename exists - if not assume this is just a trap out of
; -- the user starting to type a changed name or a mistake
t = findfile(pfname(0), count=t_count) & if t_count EQ 0 then return
;print, 'New filename is ', fname(0)
sdf_lastfile = fname(0)

; update details
 BUT_SDF_FNAME_BROWSE, Event, file = fname(0)

end

;-----------------------------------------------------------------
pro BUT_SDF_FNAME_BROWSE, Event, file=file
on_error,2
@axis_com
@sdf_com
; get new filename
if NOT keyword_set(file) then begin
	fname = pickfile2(Title='Select SDF file', Filter = '*.hdr')
endif else fname = file
if strlen(fname) GT 0 then begin
	t = ax_name(fname)
	sdf_path = t(0)
	sdf_lastfile = t(1)          ; ensure the filename is saved for future use
; update sdf_path and file_name in the text box
	id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_PATH')
	WIDGET_CONTROL,id, Set_value = sdf_path
	id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_FNAME')
	WIDGET_CONTROL,id, Set_value = sdf_lastfile
; read file and update type
	fname = sdf_path + sdf_lastfile + '.hdr'
;	print, ' Reading in SDF file: ', fname
	widget_control, /hourglass
	sdf = read_sdf(fname, /header_only)
; update the type
	id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_TYPE')
	WIDGET_CONTROL,id, Sensitive=1, Set_value = sdf.Type

	if sdf.Type NE 'VLM Image' then begin
	;  ------------- update data channel information
		nchnl = n_elements(sdf.Channel_labels)
		SDF_Channel_List = strarr(nchnl)
		for i = 0, nchnl(0)-1 do SDF_Channel_List(i) = sdf.Channel_labels[i].Name
		id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_CHANNEL')
		WIDGET_CONTROL,id, Set_VALUE = SDF_Channel_List, sensitive = 1
	; ------------- update region information
	; currently there are no names associated with regions; can only set number of elements
		n_regions = sdf.n_regions
		if n_regions GT 1 then SDF_region_List = strarr(n_regions+1) else SDF_region_List = ' '
		for i = 0, n_regions-1 do SDF_Region_List(i) = 'Region '+ strtrim(string(i+1),2)
		if n_regions GT 1 then SDF_Region_List(n_regions) = 'all regions'
		id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_REGION')
		WIDGET_CONTROL,id, Set_VALUE = SDF_Region_List, Sensitive=1

	; ---------- update image number information (for NEXAFS image sequences)
		if sdf.Type EQ 'NEXAFS Image Scan' then begin
			SDF_Image_List = strtrim(string(sdf.StackAxis.Points, format='(F7.2)'),2)
			id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_IMAGE')
			widget_control, id, Set_Value=SDF_Image_List, sensitive = 1
			id = widget_info(Event.top, FIND_BY_UNAME = 'BUT_SDF_STACK_MAP')
			widget_control, id, sensitive = 1
			id = widget_info(Event.top, FIND_BY_UNAME = 'BUT_SDF_STACK_IMAGE')
			widget_control, id, sensitive = 1
		endif else begin
		id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_IMAGE')
			widget_control, id, sensitive = 0
			id = widget_info(Event.top, FIND_BY_UNAME = 'BUT_SDF_STACK_MAP')
			widget_control, id, sensitive = 0
			id = widget_info(Event.top, FIND_BY_UNAME = 'BUT_SDF_STACK_IMAGE')
			widget_control, id, sensitive = 0
		endelse
	endif

; ----------- activate 'Show parameters' button
	id = widget_info(Event.top, FIND_BY_UNAME ='BUT_SDF_DISPLAY_HDR')
  widget_control, id, sensitive = 1

endif
end

pro WID_DLIST_CHANNEL, Event
on_error,2
@axis_com
@sdf_com
; get the changed data channel
id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_CHANNEL')
WIDGET_CONTROL,id, Sensitive=1, Get_value = sdf_ch
WIDGET_CONTROL,id, SET_DROPLIST_SELECT = sdf_ch
print, 'New channel selected ', sdf_ch
end

pro WID_DLIST_REGION, Event
on_error,2
@axis_com
@sdf_com
; get the changed region
id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_REGION')
WIDGET_CONTROL,id, Sensitive=1, Get_value = sdf_region
WIDGET_CONTROL,id, SET_DROPLIST_SELECT = sdf_region
end

pro WID_DLIST_IMAGE, Event
on_error,2
@axis_com
@sdf_com
; get the selected image
id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_IMAGE')
WIDGET_CONTROL,id, Sensitive=1, Get_value = sdf_image
;WIDGET_CONTROL,id, SET_DROPLIST_SELECT = sdf_region
end

;-----------------------------------------------------------------
pro BUT_SDF_DISPLAY_HDR, Event, group=wGroup
on_error,2
@axis_com
@sdf_com
; get the path & filename
id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_PATH')
WIDGET_CONTROL,id, Get_value = path
id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_FNAME')
WIDGET_CONTROL,id, Get_value = fname
if strlen(fname(0)) GT 0 then begin
	fname = sdf_path + fname(0) + '.hdr'
	XDisplayfile,fname, done_button = 'dismiss', title = fname, group = wGroup, width=132
endif
end

;-----------------------------------------------------------------
pro BUT_SDF_STACK_MAP, Event
@axis_com
@sdf_com
on_error,2

print, 'Analysing stack as a 2-image map'
; ----------- get the path & filename ----------
id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_PATH')
WIDGET_CONTROL,id, Get_value = path
id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_FNAME')
WIDGET_CONTROL,id, Sensitive=1, Get_value = fname
fname = path + fname + '.hdr'
print, ' Reading in SDF file: ', fname
; -------------- get the data channel ------------------
id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_CHANNEL')
this_channel = 1+ WIDGET_INFO(id, /Droplist_select)
; print, 'channel is ', this_channel
; ----------- get the region -------
id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_REGION')
this_region = 1 + widget_info(id, /DROPLIST_SELECT)
; print, 'region is ', this_region

widget_control, /hourglass
sdf = read_sdf(fname(0), channel= this_channel, region = this_region, /map, group=SDF_ID)

WIDGET_CONTROL, Event.Top, /DESTROY
end

;-------------------------Read in a single image from a stack ----------------------------------------
pro BUT_SDF_STACK_IMAGE, Event
@axis_com
@sdf_com
on_error,2

id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_IMAGE')
sdf_image_index = WIDGET_INFO(id, /Droplist_select)

; get the selected image
id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_IMAGE')
WIDGET_CONTROL,id, Sensitive=1, Get_value = sdf_image

 print, 'Extracting single image at E = ', sdf_image(sdf_image_index)
sdf_image_index = fix(sdf_image_index) +1	; kluge to avoid problem that IDL uses 0 as false !
; get the path & filename
id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_PATH')
WIDGET_CONTROL,id, Get_value = path
id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_FNAME')
WIDGET_CONTROL,id, Sensitive=1, Get_value = fname
fname = path + fname + '.hdr'
; print, ' Reading in SDF file: ', fname
;; get the data channel
id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_CHANNEL')
this_channel = 1 + WIDGET_INFO(id, /Droplist_select)
 print, 'channel is ', this_channel
; nchnls = n_elements(sdf_ch)		; from pre-17jun05 version - WHY ??
; get the region
id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_REGION')
this_region = 1 + widget_info(id, /DROPLIST_SELECT)
 print, 'region is ', this_region

widget_control, /hourglass
sdf = read_sdf(fname(0), channel= this_channel, region = this_region, one_image=sdf_image_index, group=SDF_ID)

WIDGET_CONTROL, Event.Top, /DESTROY
end


;-----------------------------------------------------------------
pro BUT_SDF_OK, Event
on_error,2
@axis_com
@sdf_com
; get the path & filename
id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_PATH')
WIDGET_CONTROL,id, Get_value = path
id = widget_info(Event.top, FIND_BY_UNAME = 'TEXT_SDF_FNAME')
WIDGET_CONTROL,id, Sensitive=1, Get_value = fname
fname = path + fname + '.hdr'
print, ' Reading in SDF file: ', fname

; get the data channel
id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_CHANNEL')
sdf_ch = WIDGET_INFO(id, /Droplist_select)
nchnls = n_elements(sdf_ch)
widget_control, /hourglass
; -------------- kluge to adapt for VLM images -----------
if sdf.Type NE 'VLM Image' then begin
	; get the region
	n_regions = sdf.n_regions
	id = widget_info(Event.top, FIND_BY_UNAME = 'WID_DLIST_REGION')
	sdf_region = WIDGET_INFO(id, /Droplist_select)
	region = sdf_region(0) + 1
	if  region LE n_regions then begin
	; read the data file for a defined region
		nchnls=n_elements(SDF_Channel_List)
		print, 'Reading channel ', string(sdf_ch(0)+1,format='(i2)'), ' of ', string(nchnls,format='(i2)'), $
		' channels.  Region ', string(region,format='(i2)'), ' of ', string(n_regions,format='(i2)'), ' regions.'
		sdf = read_sdf(fname(0), channel=sdf_ch(0)+1, region = region, group=SDF_ID)
	endif else begin		; read all regions
		print, 'Reading channel ', string(sdf_ch(0)+1,format='(i2)'), ' of ', string(nchnls,format='(i2)'), $
		' channels.  All ', string(n_regions,format='(i2)'), ' regions.'
		sdf = read_sdf(fname(0), channel=sdf_ch(0)+1, region = 0, /all_regions, group=SDF_ID)
	endelse

; ************ seems like this is set to read only channel 1 !! **************************

endif else sdf = read_sdf(fname(0), channel= 1, group=SDF_ID)

WIDGET_CONTROL, Event.Top, /DESTROY
return
end

;-----------------------------------------------------------------

pro BUT_SDF_NORMALIZE, Event
@axis_com
@sdf_com
on_error,2
if norm_flag EQ 1 then norm_flag = 0 else  norm_flag = 1
; print, ' normalzation status changed to ', norm_flag
 id = widget_info(Event.top, FIND_BY_UNAME = 'BUT_SDF_NORMALIZE')
 WIDGET_CONTROL,id, set_button=norm_flag
end

;-----------------------------------------------------------------

pro BUT_SDF_XY_CORRECT, Event
@axis_com
@sdf_com
on_error,2
if xy_correct_flag EQ 1 then xy_correct_flag = 0 else  xy_correct_flag = 1
; print, ' xy_correct status changed to ', xy_correct_flag
 id = widget_info(Event.top, FIND_BY_UNAME = 'BUT_SDF_XY_CORRECT')
 WIDGET_CONTROL,id, set_button=xy_correct_flag
end
;-----------------------------------------------------------------

pro BUT_SDF_CANCEL, Event
@axis_com
@sdf_com
on_error,2
sdf = 0
WIDGET_CONTROL, Event.Top, /DESTROY
end


;-----------------------------------------------------------------
; ****************************************************************
;-----------------------------------------------------------------

pro BASE_SDF_event, Event
@axis_com
@sdf_com

  wWidget =  Event.top

  case Event.id of

    Widget_Info(wWidget, FIND_BY_UNAME='TEXT_SDF_FNAME'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_TEXT_CH' )then $
        TEXT_SDF_FNAME, Event
    end
      Widget_Info(wWidget, FIND_BY_UNAME='WID_DLIST_CHANNEL'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_TEXT_STR' )then $
        WID_DLIST_CHANNEL, Event
    end
      Widget_Info(wWidget, FIND_BY_UNAME='WID_DLIST_REGION'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_TEXT_STR' )then $
        WID_DLIST_REGION, Event
    end
      Widget_Info(wWidget, FIND_BY_UNAME='WID_DLIST_IMAGE'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_TEXT_STR' )then $
        WID_DLIST_IMAGE, Event
    end
    Widget_Info(wWidget, FIND_BY_UNAME='BUT_SDF_FNAME_BROWSE'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_BUTTON' )then $
        BUT_SDF_FNAME_BROWSE, Event
    end
    Widget_Info(wWidget, FIND_BY_UNAME='TEXT_SDF_TYPE_TITLE_0'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_TEXT_DEL' )then $
        BUT_SDF_FNAME_BROWSE, Event
    end
    Widget_Info(wWidget, FIND_BY_UNAME='BUT_SDF_DISPLAY_HDR'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_BUTTON' )then $
        BUT_SDF_DISPLAY_HDR, Event
    end
    Widget_Info(wWidget, FIND_BY_UNAME='BUT_SDF_OK'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_BUTTON' )then $
        BUT_SDF_OK, Event
    end
    Widget_Info(wWidget, FIND_BY_UNAME='BUT_SDF_CANCEL'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_BUTTON' )then $
        BUT_SDF_CANCEL, Event
    end
    Widget_Info(wWidget, FIND_BY_UNAME='BUT_SDF_STACK_MAP'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_BUTTON' )then $
        BUT_SDF_STACK_MAP, Event
    end
    Widget_Info(wWidget, FIND_BY_UNAME='BUT_SDF_STACK_IMAGE'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_BUTTON' )then $
        BUT_SDF_Stack_IMAGE, Event
    end
    Widget_Info(wWidget, FIND_BY_UNAME='BUT_SDF_NORMALIZE'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_BUTTON' )then $
        BUT_SDF_NORMALIZE, Event
    end
    Widget_Info(wWidget, FIND_BY_UNAME='BUT_SDF_XY_CORRECT'): begin
      if( Tag_Names(Event, /STRUCTURE_NAME) eq 'WIDGET_BUTTON' )then $
        BUT_SDF_XY_CORRECT, Event
    end
    else: print, 'Undefined action'
  endcase

end

;-----------------------------------------------------------------
; ****************************************************************
;-----------------------------------------------------------------


pro BASE_SDF, GROUP_LEADER=wGroup,  file=file, _EXTRA=_VWBExtra_
@axis_com
@sdf_com
;  Resolve_Routine, 'ax_sdf_eventcb'     ; Load event callback routines
t = size(wGroup)
IF t(1) EQ 0 THEN modal_val=0 else modal_val=1

;print, wgroup, 'modal_val = ' , modal_val

BASE_SDF = Widget_Base( GROUP_LEADER=wGroup, UNAME='BASE_SDF',  $
      TITLE='Read Self Defining Format files', $
       modal = modal_val, /column)
row1 = widget_base(base_sdf,/row)
col1 = widget_base(row1,/column)
row = widget_base(base_sdf,/row)

tlabel = widget_label(row,value='Path ')
TEXT_SDF_PATH = Widget_Text(row, UNAME='TEXT_SDF_PATH',  $
     /FRAME,  XSIZE=50)

row = widget_base(base_sdf,/row)
tlabel = widget_label(row,value='File   ')
TEXT_SDF_FNAME = Widget_Text(row, UNAME='TEXT_SDF_FNAME',  $
      /FRAME,/EDITABLE ,ALL_EVENTS = 1 , KBRD_FOCUS_EVENTS = 0, XSIZE=30)
tlabel = widget_label(row,value='')
BUT_SDF_FNAME_BROWSE = Widget_Button(row,  $
      UNAME='BUT_SDF_FNAME_BROWSE', /ALIGN_CENTER ,VALUE='Browse')
tlabel = widget_label(row,value='    ')
BUT_SDF_DISPLAY_HDR = Widget_Button(row,  $
      UNAME='BUT_SDF_DISPLAY_HDR' ,  $    ;XOFFSET=320,
      /ALIGN_CENTER ,VALUE='Parameters')


row = widget_base(base_sdf,/row)
tlabel = widget_label(row,value='Type ')
TEXT_SDF_TYPE = Widget_Text(row, UNAME='TEXT_SDF_TYPE',  $
      EDITABLE=0 ,/ALL_EVENTS ,VALUE=[ 'Type', '', '', '' ], $
      /ALIGN_CENTER , SCR_YSIZE=20, XSIZE=20)
tlabel = widget_label(row,value='   ')
BUT_SDF_Stack_Map = Widget_Button(row, $
       UNAME='BUT_SDF_STACK_MAP', VALUE='Map')
tlabel = widget_label(row,value='   ')
BUT_SDF_Stack_Image = Widget_Button(row, $
       UNAME='BUT_SDF_STACK_IMAGE',  VALUE='1 image')
tlabel = widget_label(row,value='   ')
BUT_SDF_CANCEL = Widget_Button(row, UNAME='BUT_SDF_CANCEL',  $
      /ALIGN_CENTER ,VALUE='Cancel')
tlabel = widget_label(row,value='       ')
BUT_SDF_OK = Widget_Button(row, UNAME='BUT_SDF_OK',  $ ;SCR_YSIZE=40,
      /ALIGN_CENTER, VALUE='  OK  ')

row = widget_base(base_sdf,/row)

 WID_BASE_1 = Widget_Base(row, UNAME='WID_BASE_1',COLUMN=2 ,/NONEXCLUSIVE)

;tlabel = widget_label(row,value='I-ring norm?')
BUT_SDF_NORMALIZE = Widget_Button(wid_base_1, UNAME='BUT_SDF_NORMALIZE', $
        /ALIGN_LEFT, VALUE='I-ring norm?')

BUT_SDF_XY_CORRECT = Widget_Button(wid_base_1, UNAME='BUT_SDF_XY_CORRECT', $
         /ALIGN_LEFT, VALUE='xy correct ?') ;  ,  XOFFSET=100

SDF_Channel_List =['PMT','OSA','Analog']
SDF_Ch = 0
WID_DLIST_CHANNEL = Widget_Droplist(row, /DYNAMIC_RESIZE,  $
       VALUE = SDF_Channel_List,   $  ; XOFFSET=21, /ALIGN_LEFT,
      UNAME='WID_DLIST_CHANNEL', TITLE='Channel')

SDF_Region_List =['region 1']
SDF_region = 0
WID_DLIST_REGION = Widget_Droplist(row, /DYNAMIC_RESIZE, $
       VALUE = SDF_Region_List, $  ; XOFFSET=170,
      UNAME='WID_DLIST_REGION', TITLE='Region')

; row = widget_base(base_sdf,/row)
  SDF_Image_List =['']
  SDF_Im = 0
  WID_DLIST_IMAGE = Widget_Droplist(row,  $
      /DYNAMIC_RESIZE, VALUE = SDF_Image_List, $
      UNAME='WID_DLIST_IMAGE', TITLE='Image # ')  ; ,XOFFSET=290,


; ----------- start display without active displays / controls
  widget_control, WID_DLIST_CHANNEL, sensitive = 0
  widget_control, WID_DLIST_REGION, sensitive = 0
  widget_control, WID_DLIST_IMAGE, sensitive = 0
  widget_control, TEXT_SDF_TYPE, sensitive = 0
  widget_control, BUT_SDF_DISPLAY_HDR, sensitive = 0
  widget_control, BUT_SDF_Stack_Map, sensitive = 0
  widget_control, BUT_SDF_Stack_Image, sensitive = 0
  Widget_Control, /REALIZE, BASE_SDF

; ----------- set to current value of normalization flag
  if norm_flag EQ 1 then widget_control, BUT_SDF_Normalize, set_button = 1 $
     else widget_control, BUT_SDF_Normalize, set_button = 0

; ----------- set to current value of xy_correct_flag
  if xy_correct_flag EQ 1 then widget_control, BUT_SDF_xy_correct, set_button = 1 $
     else widget_control, BUT_SDF_xy_correct, set_button = 0

; ------ set last path and file name if they exist
; ------ use keyowrd file if provided
if keyword_set(file) then begin		; assume file contains path
	t = ax_name(file)
	sdf_path = t(0)
  	sdf_lastfile = t(1)
endif
; load last path information
t1 = size(sdf_path,/type)
if t1(0) NE 0 then begin
	widget_control, TEXT_SDF_PATH, set_value = sdf_path
; load last file information
	t2 = size(sdf_lastfile,/type)
	if t2(0) NE 0 then begin
		widget_control, TEXT_SDF_FNAME, set_value = sdf_lastfile
; get details of this file if it is valid
		fname = sdf_path + sdf_lastfile + '.hdr'

		t = findfile(fname(0), count=t_count)
		if t_count NE 0 then begin
; read file and update type
				widget_control, /hourglass
				sdf = read_sdf(fname, /header_only)
; Assume the file is actually an SDF type file and read its header
;update the type
			WIDGET_CONTROL,TEXT_SDF_TYPE, Sensitive=1, Set_value = sdf.Type
			if sdf.Type NE 'VLM Image' then begin
;  ------------- update data channel information
				nchnl = n_elements(sdf.Channel_labels)
				SDF_Channel_List = strarr(nchnl)
				for i = 0, nchnl(0)-1 do SDF_Channel_List(i) = sdf.Channel_labels[i].Name
				WIDGET_CONTROL,WID_DLIST_CHANNEL, Set_VALUE = SDF_Channel_List, sensitive = 1
; ------------- update region information
		; currently there are no names associated with regions; can only set number of elements
				n_regions = sdf.n_regions
				if n_regions GT 1 then SDF_region_List = strarr(n_regions+1) else SDF_region_List = ' '
				for i = 0, n_regions-1 do SDF_Region_List(i) = 'Region '+ strtrim(string(i+1),2)
				if n_regions GT 1 then SDF_Region_List(n_regions) = 'all regions'
				WIDGET_CONTROL,WID_DLIST_REGION, Set_VALUE = SDF_Region_List, Sensitive=1

; ---------- update image number information (for NEXAFS image sequences)
				if sdf.Type EQ 'NEXAFS Image Scan' then begin
					SDF_Image_List = strtrim(string(sdf.StackAxis.Points, format='(F7.2)'),2)
					widget_control, WID_DLIST_IMAGE, Set_Value=SDF_Image_List, sensitive = 1
					widget_control, BUT_SDF_Stack_Map, sensitive = 1
					widget_control, BUT_SDF_Stack_Image, sensitive = 1
				endif else begin
					widget_control, WID_DLIST_IMAGE, sensitive = 0
					widget_control, BUT_SDF_Stack_Map, sensitive = 0
					widget_control, BUT_SDF_Stack_Image, sensitive = 0
				endelse
; ----------- activate 'Show parameters' button
			  widget_control, BUT_SDF_DISPLAY_HDR, sensitive = 1
			endif
		endif else print, 'last SDF_Path or SDF_Filename not valid'
	endif else print, 'last SDF_Filename not defined'
endif else print, 'last SDF_Path not defined'

XManager, 'BASE_SDF', BASE_SDF
SDF_ID = BASE_SDF

end

;-----------------------------------------------------------------
; ****************************************************************
;-----------------------------------------------------------------

function ax_sdf, file = file, GROUP_LEADER=wGroup, _EXTRA=_VWBExtra_
on_error,2
@axis_com
@sdf_com

sdf = ' '
if keyword_set(file) then begin
	BASE_SDF, GROUP_LEADER=wGroup, file=file, _EXTRA=_VWBExtra_
endif else begin
	BASE_SDF, GROUP_LEADER=wGroup, _EXTRA=_VWBExtra_
endelse

return, sdf
end
