; $Id: dcs_convertsqwifiles.pro,v 1.9 2005/02/14 17:50:33 azuah Exp $
; Written by J.R.D. Copley.
;************************************************************************************************
;###############################################################################
;
; 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 or if the code in this file is
;  included in another product.
;
;###############################################################################
pro dcs_convertsqwifiles,dcs_base,davePtr=davePtr
;************************************************************************************************
;
compile_opt strictarr
;
;
files=dialog_pickfile(title="Select sqw_i data files to be saved:",$
	filter=["*.sqw_i"],path=!dcs_outp_directory,/multiple_files,/must_exist)
	files=files[sort(files)]
nruns=n_elements(files)
if (files[0] eq "") then begin
	x=dialog_message(["No files selected.","Exiting ..."])
	return
endif
;
compress=dialog_message(["Do you want compressed","(gzip'd) output files?"],$
	/question,/default_no) eq "Yes"
;

(*(*davePtr).dataStrPtr).commonStr.instrument = 'DCS'
(*(*davePtr).dataStrPtr).commonStr.ylabel = 'Mean Q = '
(*(*davePtr).dataStrPtr).commonStr.yunits = ''
(*(*davePtr).dataStrPtr).commonStr.ytype = 'points'

for irun=0,nruns-1 do begin
;
	lines="Reading "+files[irun]
	dcs_message_display,lines,base,group_leader=dcs_base,alignment=0

	file=files[irun]
	openr,unit,file,/get_lun
	comments=""
	readf,unit,comments ;read overall comment line
	free_Ptr,(*(*davePtr).dataStrPtr).commonStr.treatmentPtr
	(*(*davePtr).dataStrPtr).commonStr.treatmentPtr = ptr_new(comments)
;
	nspe=1 & nx=1
	readf,unit,nspe,nx ;read number of spectra and number of "x" values
;	print,nspe,nx
	free_Ptr,(*(*davePtr).dataStrPtr).commonStr.histPtr
	(*(*davePtr).dataStrPtr).commonStr.histPtr = $
		ptr_new({qty:fltarr(nx,nspe),err:fltarr(nx,nspe),x:fltarr(nx),y:fltarr(nspe)})
;
	xlabel=""
	readf,unit,xlabel ;read "x" label, e.g. "hw (meV)" (used for plot)
	(*(*davePtr).dataStrPtr).commonStr.xlabel = xlabel
	(*(*davePtr).dataStrPtr).commonStr.xunits = 'energy:meV'
	(*(*davePtr).dataStrPtr).commonStr.xtype = 'points'
;
	x=dblarr(nx)
	readf,unit,x; read values of x
	(*(*(*davePtr).dataStrPtr).commonStr.histPtr).x = x
;
	index0=where(abs(x) eq min(abs(x)),count)
	alabel=""
	auxv=dblarr(nspe,nx)
	ylabel=""
	data=dblarr(nspe,nx)
	elabel=""
	error=dblarr(nspe,nx)
	vector=dblarr(nx)
	title=strarr(nx)
;
	(*(*(*davePtr).dataStrPtr).commonStr.histPtr).y = fltarr(nspe)
	(*(*(*davePtr).dataStrPtr).commonStr.histPtr).qty = fltarr(nx,nspe)
	(*(*(*davePtr).dataStrPtr).commonStr.histPtr).err = fltarr(nx,nspe)
;
	for k=1,nspe do begin
		readf,unit,alabel ;read auxiliary label, e.g. "Q(hw) (A-1)" (not used)
		readf,unit,vector
		auxv[k-1,*]=vector
		title[k-1]="Elastic Q = "+strtrim(string(vector[index0],format='(f8.3)'),2)+" A-1"
		(*(*(*davePtr).dataStrPtr).commonStr.histPtr).y[k-1] = vector[index0]
;		print,title[k-1]
		readf,unit,ylabel ;read "y" label, e.g. "I (Q,hw)" (used for plot)
		(*(*davePtr).dataStrPtr).commonStr.histlabel = ylabel
		(*(*davePtr).dataStrPtr).commonStr.histunits = ''
		readf,unit,vector
		(*(*(*davePtr).dataStrPtr).commonStr.histPtr).qty[*,k-1] = vector
		readf,unit,elabel ;read "e" label, e.g."Error" (not used)
		readf,unit,vector
		(*(*(*davePtr).dataStrPtr).commonStr.histPtr).err[*,k-1] = vector
;		print,"Reading spectrum",k
	endfor
	free_lun,unit
;
	filename=files[irun]
	pos=strpos(filename,".sqw_i")
	filename=strmid(filename,0,pos)+"sqwi.dave"
	widget_control,base,/destroy
;
	lines="Writing file"+filename
	dcs_message_display,lines,base,group_leader=dcs_base,alignment=0
	save,davePtr,filename=filename,compress=compress
;
	widget_control,base,/destroy
endfor
;
return
;
end