; $Id: dcs_saverawdatafiles.pro,v 1.14 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_saverawdatafiles,dcs_base,davePtr=davePtr,message_str=message_str,$
	logentry=logentry
;************************************************************************************************
;
compile_opt strictarr
;
;
files=dialog_pickfile(title="Select raw DCS data files to be saved:",$
	filter=["*.dcs","*.gz"],path=!dcs_data_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
;

message=[$
	"The selected data files will be corrected if there are bitflips.",$
	"No other data treatment or corrections will be performed.","",$
	"Do you want compressed (gzip'd) output files?"]
compress=dialog_message(message,/question,/default_no) eq "Yes"
;
;	This is required for the call to dcs_sanitycheck
;
runtemperatures=fltarr(nruns)
;
maxbitflips=10
bitflips=lonarr(3,maxbitflips,nruns)+1000
;
; Call dcs_sanitycheck to do the sanity checks.
;
dcs_sanitycheck,DCS_base,files,nruns,913,allchecks=0,silent=0,bitflips,maxbitflips,$
	davePtr=davePtr,runtemperatures,message_str,$
	skiptchanmismatchmes=0
;
for irun=0,nruns-1 do begin
;
	lines="Reading "+files[irun]
	dcs_message_display,lines,base,group_leader=dcs_base,alignment=0
	dcs_readdatafile,files[irun],0,0,"binary",davePtr=davePtr
;
; If necessary perform bitflip correction(s)	to data.
	for k=0,maxbitflips-1 do begin
		if (bitflips[1,k,irun] lt 1000) then begin
			bftim=bitflips[0,k,irun]
			bfdet=bitflips[1,k,irun]
			bfamt=bitflips[2,k,irun]
			x=dialog_message(["A bitflip has been corrected.",$
				"File "+files[irun],$
				"Time channel"+strcompress(bftim),$
				"Detector channel"+strcompress(bfdet),$
				"Change in value"+strcompress(bfamt)])
			if (bfdet lt 913) then begin
				(*(*(*davePtr).dataStrPtr).commonStr.histPtr).qty[bftim,bfdet] = $
					(*(*(*davePtr).dataStrPtr).commonStr.histPtr).qty[bftim,bfdet]-bfamt
			endif else begin
				(*(*(*davePtr).dataStrPtr).specificPtr).histohigh[bftim,bfdet-nhedet] = $
					(*(*(*davePtr).dataStrPtr).specificPtr).histohigh[bftim,bfdet-nhedet]-bfamt
			endelse
		endif
	endfor
;
	filename=files[irun]
	pos=strpos(filename,".gz")
	if (pos gt 0) then filename=strmid(filename,0,pos)
	pos=strpos(filename,"dcs")
	filename=strmid(filename,0,pos)
	pos=strpos(filename,!delimiter,/reverse_search)
	filename=!dcs_outp_directory+strmid(filename,pos+1)+"DAVE"
	if (compress) then filename=filename+".gz"
;
	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
;
	logentry=""
	widget_control,base,/destroy
endfor
;
return
;
end
