Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/dev_setup_load.pro'
;+
; NAME:
;	dev_setup_Load
;
; PURPOSE:
;	Restore from a file the saved structure variable(s)
;	for particular GPIB device(s), and then command the
;	device axes to move to the positions contained in structure.
;
;	Default is to assume widgets (e.g. Klinger_Control) are being used
;	and the move commands are generated by setting automatic timers
;	on the move buttons. Therefore, if a move button does not exist
;	(hidden axis controls) then that axis is not moved.
;
;	Filename is always of form: "dev_setup." + dev_name.* , so that
;	the last field identifies different setups for each device.
;	The PickFile widget will allow user to select from such a list.
;
; CALLING:
;	dev_setup_Load, dev_name
; INPUT:
;	dev_name = string, name of device, thus specifying a filename
;		filter to search for saved setups of desired device.
; KEYWORDS:
;	FILENAME = optional string specifying the exact filename containing
;		the device structure variable to restore, 
;
;	/WAIT : wait until each move command is completed.
;		Default is to command each device/axis to move asynchronously,
;		and in the case of multiple axes on one controller,
;		automatically wait until other axis is finished, then move.
; EXTERNAL CALLS:
;	pro move_device
;	function N_struct
;	function VarType
;	function get_dev_num
;	function PickFile
; COMMON BLOCKS:
;	common gpib, devices	;structured array of device names, handles, etc.
; HISTORY:
;	Written: Frank Varosi NASA/GSFC 1993.
;	F.V. 1994, new method: set timers on move button of widgets.
;-

pro dev_setup_Load, dev_name, FILENAME=file, WAIT=wsec

   common gpib, devices

	if N_struct( devices ) LE 0 then begin
		message,"no devices, must call Find_Devices first",/INFO
		return
	   endif

	if VarType( file ) NE "STRING" then begin

		if VarType( dev_name ) NE "STRING" then begin
			message,"must specify device name or filename",/INFO
			return
		   endif

		file = PickFile( FILT="dev_setup." + dev_name + ".*",/NOCONF, $
				TITLE="Select Device Setup File:" )

		if strlen( file ) LE 0 then begin
			print,"nothing restored"
			return
		   endif
	   endif

	print," restoring device positions from file:  ",file
	restore,file,/VERB
	help,device

	if N_struct( device ) LE 0 then begin
		message,"no device structure variable found in file",/INFO
		return
	   endif

	dev_nums = get_dev_num( device.name )
	w = where( dev_nums GE 0, ndev )
	if (ndev LE 0) then  return  else  dev_nums = dev_nums(w)

	if NOT yes_no_menu( "move device axes",/BIN ) then return

	for id=0,ndev-1 do begin

	    dn = dev_nums(id)
	    match, devices(dn).axis.name, device(id).axis.name, mx, rx

	    if mx(0) GE 0 then begin

		devices(dn).axis(mx).pos_move = device(id).axis(rx).pos_move

		if keyword_set( wsec ) then begin

			for k=0,N_elements(mx)-1 do $
				move_device, NUM_DEV=dn, NUM_AX=mx(k), WAIT=wsec

		 endif else begin

			widm = devices(dn).axis(mx).wid.move

			for k=0,N_elements(mx)-1 do $
				Widget_Control, widm(k), TIMER=k+0.1, BAD_ID=bid
		  endelse

	     endif else message,"axes do not match for device: " + $
				device(id).name,/INFO
	  endfor
END