;$Id: lib_file.pro,v 1.3 2005/02/09 14:02:26 azuah Exp $
;*******************************************************************************
;23456789 123456789 123456789 123465798 123456789 123456789 123465789 123456789
;       10        20        30        40        50        60        70        80
;*******************************************************************************
;###############################################################################
; LICENSE:
;  The software in this file is written by an employee of 
;  National Institute of Standards and Technology 
;  as part of the DAVE software project.
;
;  The DAVE software package is not subject to copyright protection
;  and is in the public domain. It should be considered as an
;  experimental neutron scattering data reduction, visualization, and
;  analysis system. As such, the authors assume no responsibility
;  whatsoever for its use, and make no guarantees, expressed or
;  implied, about its quality, reliability, or any other
;  characteristic. The use of certain trade names or commercial
;  products does not imply any endorsement of a particular product,
;  nor does it imply that the named product is necessarily the best
;  product for the stated purpose. We would appreciate acknowledgment
;  if the DAVE software is used of if the code in this file is
;  included in another product.
;
;###############################################################################
function file_read_spe_one $
	; INPUT
	, file = file $
	; ETC
	, _extra = _extra

	if n_elements( file ) ne 1 then begin
		file = dialog_pickfile( /read $
			, title = 'Please choose a SPE file to read.' $
			, filter = [ '*.spe', '*.spe.gz' ] $
			)
	endif

	if not file_test( file ) then begin
		message, 'Unable to comply.', /continue
		message, 'The file does not exist.', /continue
		return, 0
	endif

	compress = strmatch( file, '*.gz', /fold_case )

	title = ''
	xtitle = ''
	ytitle = ''
	ftitle = ''

	first_line = ' '
	openr, lun, file,/get_lun, compress=compress
	readf, lun, first_line

	if strmatch( first_line, '*[a-z]*', /fold_case ) then begin
		title = first_line
	endif else begin
		; Rewind the file.
		; The following is probably allowed,
		;  but not recommended for compressed files.
		; point_lun, lun, 0

		print, first_line
		message, 'Retrieving the file again.', /continue
		free_lun, lun
		openr, lun, file, /get_lun, compress=compress

	endelse

	readf, lun, n_x, n_y

	x =	dblarr(n_x)
	y = dblarr(n_y)
	f =	dblarr(n_x, n_y)
	e = f
	f_vec = dblarr(n_y)
	e_vec = f_vec
	junk = ''

	readf, lun, xtitle
	readf, lun, x
	readf, lun, ytitle
	readf, lun, y

	for i_x = 0, n_x - 1 do begin

 		readf, lun, ftitle
 		readf, lun, f_vec
 		f[i_x, *] = f_vec
 		readf, lun, junk
 		readf, lun, e_vec
 		e[i_x, *] = e_vec

	endfor

	free_lun, lun

	return, obj_new('oDave' $
		, qty = f $
		, err = e $
		, xdata = x $
		, ydata = y $
		; , /no_copy $
		)

io_error:
	message, 'An I/O Error Occurred While Reading a SPE file.', /continue
	return, 0

end
;*******************************************************************************
;23456789 123456789 123456789 123465798 123456789 123456789 123465789 123456789
;       10        20        30        40        50        60        70        80
;*******************************************************************************
; FILE_DIALOG_READ
;
; if file_dialog_read(/multiple_files, filter=['*.spe'], files=files) then
;	oData -> read...
; endif
;
function file_dialog_read $
	; INPUT
	, multiple_files = multiple_files $
	, filter = filter $
	; OUTPUT
	, files = files $
	; OPTION
	, _extra=_extra

	if keyword_set( multiple_files ) then begin
		title_str = 'files'
	endif else begin
		multiple_files = 0
		title_str = 'a file'
	endelse

	if n_elements( filter ) eq 0 then begin
		filter = [ '*' ]
	endif

	files = dialog_pickfile( /read $
		, multiple_files = multiple_files $
		, title = 'Please select ' + title_str + ' to read.' $
		, filter = filter $
		)

	if files[0] eq '' then begin
		message, 'No file selected.', /continue
		return, 0
	endif

	return, 1

end

;
; FILE_DIALOG_WRITE
;
; if file_dialog_write( filter = ['*.spe'], files = files ) then begin
;	oData -> write...
; endif
;
function file_dialog_write $
	; INPUT
	, filter = filter $
	; OUTPUT
	, file = file $
	; OPTION
	, _extra = _extra

	if n_elements( filter ) eq 0 then filter = [ '*' ]

	file = dialog_pickfile( /write $
		, title = 'Please select a file to write.' $
		, filter = filter $
		)

	if file eq '' then begin
		message, 'No file selected.', /continue
		return, 0
	endif

	return, 1
end

;
; FILE_NAME_CHANGE
;
; new_name = file_name_change( file, '.txt', '_ive.txt')
;
function file_name_change $
	; INPUT
	, FileName $
	, from_str $
	, to_str

	FileName_copy = FileName
	pos = strpos( FileName, from_str, /reverse_search )
	Front = strmid(FileName, 0, pos)
	Back = strmid(FileName, pos + strlen( from_str ))
	return, Front + to_str + Back

end
