Commit ba716830 authored by Gonzalez, Miguel's avatar Gonzalez, Miguel
Browse files

Added D7 saving routine from A. Wildes and include it in runtime

parent caaef0e4
......@@ -54,6 +54,7 @@ print,'*********'
.run vanadium.pro
.run zero_detectors.pro
.run normalise_cf.pro
.run d7_save
print,'**********'
print,'TOF macros'
......
pro d7_save,win,outfile,header=header,outformat=outformat
;+
; LAMP procedure to save a dataset with arbitrary X and Y parameters
; The dataset can have one, two or three dimensions
; The default output file will contain:
; an array with the Z values (the number of arrays stacked on top of each other)
; an array with the X values
; an array with the Y values
; an array with the data
; an array with the errors on the data
; Note that the array sizes for X, Y, and the data can all be different
; but that the data and errors will have the same array size
; It also assumes that matrices, if stacked, all have the same X- and Y-values
; ARW 04.10.04
;
; NECESSARY INPUT:
; win=the input workspace
; outfile=the output file name (as a string)
;
; OPTIONAL VARIABLES:
; header=a string that will be printed in to the file
; outformat=a string that permits the output format to be changed
; The default output format is noted above, and is best for 2D datasets
; Other supported formats are:
; 'xye' requires 1D datasets with one set of x-values, e.g. specular data
; Will transpose the arrays, and will save with columns:
; x y1 e(y1) y2 e(y2) etc.
; Will not save Z or Y
; Will include a row with column headers
; 'xye[Col1,Col2,Col3,...]' As above, but only saves the desired columns
; The columns must be numbers enclosed by square brackets and separated by commas
;
; e.g. type at LAMP interface:
; d7_save,w10,'delme',header='Throw me away'
; d7_save,w10,'xye[0,2]'
;
; Calls to: d17_errmsg
; Last modified: ARW 23.11.15
; 23.11.15 Fixed bug that would not save in XYE format when error = 0
; 11.12.19 Hacked from d17_save to d7_save
;-
; *** Format for output. Change this if desired
fmatdef='(g20.10," "))'
; *** Check for output format. Uses a variable called 'outflag' Values for outflag are:
; outflag=0 Default
; outflag=1 xye
outflag=0
if n_elements(outformat) gt 0 then begin
nformats=1
outflag=0*indgen(1+nformats)
outflag[1]=strcmp(outformat,'xye',3,/FOLD_CASE)
if total(outflag) eq 0 then begin
print,'WARNING: Output format does not correspond. Default output format will be used'
; lamp_journal, 'WARNING: Output format does not correspond. Default output format will be used'
outflag=0
endif else outflag=where(outflag ne 0)
endif else outformat='a'
; *** Define sizes and check for consistency
emess=0
TAKE_DATP,datp
wsize=size(win)
err=datp.e
esize=size(err)
xdata=datp.x
xsize=size(xdata)
ydata=datp.y
ysize=size(ydata)
zdata=datp.z
zsize=size(zdata)
; *** Convert sizes to number of elements in [X,Y,Z]
wsize=wsize[1:wsize[0]]
if esize[0] ne 0 then esize=esize[1:esize[0]] else esize=0
if xsize[0] ne 0 then xsize=xsize[1:xsize[0]] else xsize=0
if ysize[0] ne 0 then ysize=ysize[1:ysize[0]] else ysize=0
if zsize[0] ne 0 then zsize=zsize[1:zsize[0]] else zsize=0
; *** Check error size against workspace size. Error arrays can be non-existant, or the same size as the workspace
if (esize[0] ne 0) then begin
if array_equal(wsize,esize) ne 1 then begin
print,'ERROR: workspace array and error array have different sizes'
lamp_journal, 'ERROR: workspace array and error array have different sizes'
lamp_message, 'ERROR: workspace array and error array have different sizes'
help,win,err
emess=1
endif
endif else begin
print,'WARNING: workspace error not defined. Errors will not be saved'
lamp_journal, 'WARNING: workspace error not defined. Errors will not be saved'
lamp_message, 'WARNING: workspace error not defined. Errors will not be saved'
endelse
; *** Set wsize to be a 3 element array
for i=0,2-n_elements(wsize) do wsize=[wsize,1]
; *** Check x-axis against workspace size.
; *** X can be non-existant, a 1D array of the same size as the workspace or a 2D array with the in-plane dimensions (not including z)
if xsize[0] ne 0 then begin
if (n_elements(xsize) ne 1) then begin
if (array_equal(xsize[0:1],wsize[0:1]) ne 1) then begin
print,xsize,wsize
print,'ERROR: workspace array size and x-axis array size do not correspond'
lamp_journal, 'ERROR: workspace array size and x-axis array size do not correspond'
lamp_message, 'ERROR: workspace array size and x-axis array size do not correspond'
help,win,xdata
emess=2
endif
endif else begin
if (n_elements(xsize) eq 1) and (xsize[0] ne wsize[0]) then begin
print,'ERROR: Discrepancy between the number of columns in workspace and the number of values on x-axis'
lamp_journal, 'ERROR: Discrepancy between the number of columns in workspace and the number of values on x-axis'
lamp_message, 'ERROR: Discrepancy between the number of columns in workspace and the number of values on x-axis'
help,win,xdata
emess=3
endif
endelse
endif else begin
print,'WARNING: X-axis not defined. Values will not be saved'
lamp_journal, 'WARNING: X-axis not defined. Values will not be saved'
lamp_message, 'WARNING: X-axis not defined. Values will not be saved'
endelse
; *** Check y-axis against workspace size.
; *** Y can be non-existant, a 1D array of the same size as the workspace or a 2D array with the in-plane dimensions (not including z)
if ysize[0] ne 0 then begin
if (n_elements(ysize) ne 1) then begin
if (array_equal(ysize[0:1],wsize[0:1]) ne 1) then begin
print,'ERROR: workspace array size and y-axis array size do not correspond'
lamp_journal, 'ERROR: workspace array size and y-axis array size do not correspond'
lamp_message, 'ERROR: workspace array size and y-axis array size do not correspond'
help,win,ydata
emess=4
endif
endif else begin
if (n_elements(ysize) eq 1) and (ysize[0] ne wsize[1]) then begin
print,'ERROR: Discrepancy between the number of columns in workspace and the number of values on y-axis'
lamp_journal, 'ERROR: Discrepancy between the number of columns in workspace and the number of values on y-axis'
lamp_message, 'ERROR: Discrepancy between the number of columns in workspace and the number of values on y-axis'
help,win,ydata
emess=5
endif
endelse
endif else begin
print,'WARNING: Y-axis not defined. Values will not be saved'
lamp_journal, 'WARNING: Y-axis not defined. Values will not be saved'
lamp_message, 'WARNING: Y-axis not defined. Values will not be saved'
endelse
; *** Check z-axis against workspace size.
; *** Z can be non-existant, or a 1D array of the same size as the workspace dimension
if zsize[0] ne 0 then begin
if (n_elements(zsize) gt 1) then begin
print,'WARNING: Only a 1D array for Z is allowed'
print,'Setting Z-axis values to the number of stacked arrays'
lamp_journal,'WARNING: Only 1D array for Z allowed, Z set to no. stacks'
lamp_message,'WARNING: Only 1D array for Z allowed, Z set to no. stacks'
print,zsize
zdata=indgen(wsize[2])
zsize=n_elements(zdata)
; emess=7
endif else begin
if (zsize[0] ne wsize[2]) then begin
print,'WARNING: Discrepancy between the number of stacked arrays in workspace and the number of values on z-axis'
print,'Setting Z-axis values to the number of stacked arrays'
lamp_journal,'WARNING: Workspace stacks and Z-axis disagree. Z set to no. stacks'
lamp_message,'WARNING: Workspace stacks and Z-axis disagree. Z set to no. stacks'
zdata=indgen(wsize[2])
zsize=n_elements(zdata)
; print,'ERROR: Discrepancy between the number of stacked arrays in workspace and the number of values on z-axis'
; lamp_journal, 'ERROR: Discrepancy between the number of stacked arrays in workspace and the number of values on z-axis'
; help,win,zdata
; emess=8
endif
endelse
endif else begin
print,'WARNING: Z-axis not defined. Values will not be saved'
lamp_journal, 'WARNING: Z-axis not defined. Values will not be saved'
lamp_message, 'WARNING: Z-axis not defined. Values will not be saved'
endelse
; *** If xye[Cols] is chosen, parse the phrase to determine which columns to extract
; *** The assumption is that "columns" are in fact rows in LAMP
if (outflag eq 1) and (strlen(outformat) gt 3) then begin
ycol=long(strsplit(strsplit(strsplit(strmid(outformat,3),'[',/EXTRACT),']',/EXTRACT),',',/EXTRACT))
if n_elements(ycol) eq 0 or max(ycol) gt wsize[1]-1 then begin
print,'ERROR: The workspace has fewer columns than requested'
lamp_journal, 'ERROR: The workspace has fewer columns than requested'
lamp_message, 'ERROR: The workspace has fewer columns than requested'
help,win
emess=9
endif else ydata=ydata[ycol]
endif else begin
if ysize[0] ne 0 then ycol = indgen(ysize[0]) else ycol = 0
endelse
; *** Determine if output filename is a string
if strcmp('STRING',typename(outfile)) le 0 then begin
print,'ERROR: The output filename must be given as a string'
lamp_journal, 'ERROR: The output filename must be given as a string'
lamp_message, 'ERROR: The output filename must be given as a string'
emess=10
return
endif
; *** And tidy up if there has been one or more errors
if emess ne 0 then begin
print,'Fatal error, data not saved'
lamp_journal, 'Fatal error, data not saved'
lamp_message, 'Fatal error, data not saved'
return
endif
; *** Open the output file for writing
get_lun,unit
openw,unit,outfile
if n_elements(header) gt 0 then begin
printf,unit,header
printf,unit,' '
endif
; *** Turn zdata in to a number if it is a NaN
NaNcheck=where(finite(zdata,/NAN) eq 1,count)
if count ne 0 then zdata=1
; *** Format for output.
fmat='('+string(wsize[0])+fmatdef
yfmat=fmat
; *** Now establish which output format to use
case outflag[0] of
0: begin ; *** Default output
; *** Write the z-values to the output file
if n_elements(zdata) gt 1 then begin
printf,unit,'The Z-axis is given by ',datp.z_tit
printf,unit,'Z-axis: ',zdata
endif else zsize=1
; *** Write the x-values to the output file
if xsize[0] ne 0 then begin
if n_elements(xsize) eq 1 then xsize=[xsize,1]
printf,unit,'The X-axis is equivalent to ',datp.x_tit
printf,unit,'X-axis: (',xsize[0],' columns by ',xsize[1],' rows)'
for i=0,xsize[1]-1 do printf,unit,xdata[*,i],FORMAT=fmat
endif
; *** Write the y-values to the output file
if ysize[0] ne 0 then begin
if n_elements(ysize) eq 1 then ysize=[ysize,1]
printf,unit,'The Y-axis is equivalent to ',datp.y_tit
printf,unit,'Y-axis: (',ysize[0],' columns by ',ysize[1],' rows)'
for i=0,ysize[1]-1 do printf,unit,ydata[*,i],FORMAT=yfmat
endif
; *** Write the data to the output file
printf,unit,'Data: (',wsize[2],' arrays of ',wsize[0],' columns by ',wsize[1],' rows)'
for j=0,n_elements(zsize)-1 do begin
printf,unit,'Data in Array ',j+1,', z=',zdata[j]
for i=0,wsize[1]-1 do printf,unit,win[*,i,j],FORMAT=fmat
endfor
; *** Write the errors to the output file
if esize[0] ne 0 then begin
printf,unit,'Error: (',wsize[2],' arrays of ',wsize[0],' columns by ',wsize[1],' rows)'
for j=0,n_elements(zsize)-1 do begin
printf,unit,'Error in Array ',j+1,', z=',zdata[j]
for i=0,wsize[1]-1 do printf,unit,err[*,i,j],FORMAT=fmat
endfor
endif
end
1: begin ; *** xye
; *** Collect the x-values, y-values, data and error and transpose them
ox=transpose(xdata) ; Assumes that x and y are row arrays
; ow=rotate(win,1) ; Data and errors can be a 2D array, but must not have a z component
; oe=rotate(win,1)
ow=transpose(win[*,ycol]) ; Data and errors can be a 2D array, but must not have a z component
if ysize[0] eq 0 then ydata = 0 else ydata=ydata[ycol]
; Case when writing errors
if esize[0] ne 0 then begin
oe=transpose(err[*,ycol])
; *** Unless specified, write a column header
if n_elements(header) gt 0 eq 1 then begin
colhead=' x '
for i=0,n_elements(ydata)-1 do colhead=[colhead+string(ydata[i])+' e'+strtrim(string(ydata[i]),1)]
printf,unit,colhead
endif
; *** Create the output array
wsave=ox
for i=0,n_elements(ydata)-1 do wsave=[wsave,ow[i,*],oe[i,*]]
; Case when no errors exist
endif else begin
; *** Unless specified, write a column header
if n_elements(header) gt 0 eq 1 then begin
colhead=' x '
for i=0,n_elements(ydata)-1 do colhead=[colhead+string(ydata[i])]
printf,unit,colhead
endif
; *** Create the output array
wsave=ox
for i=0,n_elements(ydata)-1 do wsave=[wsave,ow[i,*]]
endelse
; *** And write it to the file
for i=0,n_elements(ox)-1 do printf,unit,wsave[*,i],FORMAT=fmat
end
endcase
close,unit
free_lun,unit
print,'Data correctly saved in to '+outfile
lamp_journal,'Data correctly saved in to '+outfile
lamp_message,'Data correctly saved in to '+outfile
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment