-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadspecscan.py
More file actions
110 lines (98 loc) · 3.97 KB
/
Copy pathreadspecscan.py
File metadata and controls
110 lines (98 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# playing with read a single scan out of the spec file and save it to a new text file
# utilizing the spec2nexus package
import os
import numpy as np
from spec2nexus import spec
# flags
# write output to data file(s)
# write_file == 1 : copy the single scan with all the comments to a separate file
# write_file == 2 : copy the data with scanCmd, scanTime, and column label
# write_file == 3 : copy the specific columns from the data to a separate file.
# write_file == 99: special case, write the data file with one column label/values replaced.
write_file = 99
# verbose mode on/off
verbose = 1
# SPEC file path, absolute, not the format
# For Linux system
#specdatapath = os.path.join("/home/33id/", "data/hui/20180607/ternaryalloy/")
# For Windows system, Note the double \\
#specdatapath = os.path.join("Z:\\", "data\\hui\\20180607\\ternaryalloy\\")
specdatapath = os.path.join("C:\\", "work\\Users\\YCao\LSFO\\")
sampleName = "LSFO_001_Cryo"
specdatafile = os.path.join(specdatapath, "%s.spec") % sampleName
specData = spec.SpecDataFile(specdatafile)
# list of scans need to integrate, for example:
#scans = [12, ]
scans = list( range(28,37) )
# selected column label or positioner label.
col_label = ["TwoTheta", "L", "seconds", "I00", "trans", "corrdet", "Energy", "I0", "imroi1", "ksamx", "ksamy"]
for scannum in scans:
scan = specData.getScan(scannum)
scan.interpret()
output_scann = scan.scanNum
data_pnts = len( scan.data[ scan.L[0] ] )
output_data = []
if write_file >= 1:
#output_filename = os.path.join(specdatapath, "analysis_runtime", sampleName, "%s_S%04d.txt") % (sampleName, scannum)
#output_filename = os.path.join("D:\\UniWork\\Users\\HuiJie\\", sampleName, "%s_S%04d.dat") % (sampleName, scannum)
output_filename = os.path.join(specdatapath, "%s_S%04d.dat") % (sampleName, scannum)
# generate the directory if not exist yet.
if not os.path.exists(os.path.dirname(output_filename)):
try:
os.makedirs(os.path.dirname(output_filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
outfile = open(output_filename, 'w+')
#outfile.write('## SPECfile = %s\n' % specdatafile)
if verbose:
print("Scan# = ", scannum, ": ", scan.scanCmd)
if write_file == 1:
outfile.write(scan.raw)
elif write_file == 2:
outfile.write("#S %s %s\n" % (output_scann, scan.scanCmd) )
outfile.write("#D %s\n" % scan.date)
outfile.write("#L ")
for label in scan.L:
outfile.write("%s " % label)
outfile.write("\n")
for line in scan.data_lines:
outfile.write("%s\n" % line)
elif write_file == 3:
outfile.write("#S %s %s\n" % (output_scann, scan.scanCmd) )
outfile.write("#D %s\n" % scan.date)
outfile.write("#L ")
outfmt = " "
for label in col_label:
if label in scan.L:
outfile.write("%14s " % label)
output_data.append( np.array(scan.data[label]) )
outfmt = outfmt + "%15.4f "
elif label in scan.positioner:
outfile.write("%14s " % label)
output_data.append(scan.positioner[label]*np.ones(data_pnts))
outfmt = outfmt + "%15.4f "
outfile.write("\n")
output_data = np.transpose(output_data)
np.savetxt(outfile, output_data, fmt = outfmt)
elif write_file == 99:
# replace the data column
if scannum in [29, 32, 35]:
scan.data["Temp_sa"]=[0.000037]*len(scan.data["Temp_sa"])
else:
scan.data["Temp_sa"]=[0.0234]*len(scan.data["Temp_sa"])
outfmt = ""
for label in scan.L:
output_data.append(np.array(scan.data[label]) )
outfmt += "%.6g "
# replacee the label
ind = scan.L.index("Temp_sa")
scan.L[ind] = "trans"
out_label = f"#L " + " ".join(_str for _str in scan.L) +"\n"
outfile.write(out_label)
output_data = np.transpose(output_data)
np.savetxt(outfile, output_data, fmt = outfmt)
if write_file >= 1:
outfile.close()
if verbose:
print("Data written to " + output_filename + ".")