forked from diffpy/diffpy.labpdfproc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabpdfprocapp.py
242 lines (225 loc) · 8.26 KB
/
labpdfprocapp.py
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import sys
from argparse import ArgumentParser
from gooey import Gooey, GooeyParser
from diffpy.labpdfproc.functions import CVE_METHODS, apply_corr, compute_cve
from diffpy.labpdfproc.tools import (
known_sources,
load_metadata,
preprocessing_args,
)
from diffpy.utils.diffraction_objects import XQUANTITIES, DiffractionObject
from diffpy.utils.parsers.loaddata import loadData
def define_arguments():
args = [
{
"name": ["mud"],
"help": "Value of mu*D for your sample. Required.",
"type": float,
},
{
"name": ["input"],
"help": (
"The filename(s) or folder(s) of the datafile(s) to load. "
"Required.\n"
"Supply a space-separated list of files or directories. "
"Long lists can be supplied, one per line, "
"in a file with name file_list.txt. "
"If one or more directory is provided, all valid "
"data-files in that directory will be processed. "
"Examples of valid inputs are 'file.xy', 'data/file.xy', "
"'file.xy, data/file.xy', "
"'.' (load everything in the current directory), "
"'data' (load everything in the folder ./data), "
"'data/file_list.txt' (load the list of files "
"contained in the text-file called file_list.txt "
"that can be found in the folder ./data), "
"'./*.chi', 'data/*.chi' "
"(load all files with extension .chi in the folder ./data)."
),
"nargs": "+",
"widget": "MultiFileChooser",
},
{
"name": ["-a", "--anode-type"],
"help": (
f"The type of the x-ray source. "
f"Allowed values are {*known_sources, }. "
f"Either specify a known x-ray source or specify wavelength."
),
"default": None,
},
{
"name": ["-w", "--wavelength"],
"help": (
"X-ray source wavelength in angstroms. "
"Not needed if the anode-type is specified."
),
"type": float,
},
{
"name": ["-o", "--output-directory"],
"help": (
"The name of the output directory. "
"If not specified then corrected files will be "
"written to the current directory. "
"If the specified directory doesn't exist it will be created."
),
"default": None,
},
{
"name": ["-x", "--xtype"],
"help": (
f"The quantity on the independent variable axis. "
f"Allowed values: {*XQUANTITIES, }. "
f"If not specified then two-theta "
f"is assumed for the independent variable."
),
"default": "tth",
},
{
"name": ["-c", "--output-correction"],
"help": (
"The absorption correction will be output to a file "
"if this flag is set. "
"Default is that it is not output."
),
"action": "store_true",
},
{
"name": ["-f", "--force-overwrite"],
"help": "Outputs will not overwrite existing file "
"unless --force is specified.",
"action": "store_true",
},
{
"name": ["-m", "--method"],
"help": (
f"The method for computing absorption correction. "
f"Allowed methods: {*CVE_METHODS, }. "
f"Default method is polynomial interpolation "
f"if not specified."
),
"default": "polynomial_interpolation",
},
{
"name": ["-u", "--user-metadata"],
"help": (
"Specify key-value pairs to be loaded into metadata "
"using the format key=value. "
"Separate pairs with whitespace, "
"and ensure no whitespaces before or after the = sign. "
"Avoid using = in keys. If multiple = signs are present, "
"only the first separates the key and value. "
"If a key or value contains whitespace, enclose it in quotes. "
"For example, facility='NSLS II', "
"'facility=NSLS II', beamline=28ID-2, "
"'beamline'='28ID-2', 'favorite color'=blue, "
"are all valid key=value items."
),
"nargs": "+",
"metavar": "KEY=VALUE",
},
{
"name": ["-n", "--username"],
"help": (
"Username will be loaded from config files. "
"Specify here only if you want to "
"override that behavior at runtime."
),
"default": None,
},
{
"name": ["-e", "--email"],
"help": (
"Email will be loaded from config files. "
"Specify here only if you want to "
"override that behavior at runtime."
),
"default": None,
},
{
"name": ["--orcid"],
"help": (
"ORCID will be loaded from config files. "
"Specify here only if you want to "
"override that behavior at runtime."
),
"default": None,
},
{
"name": ["-z", "--z-scan-file"],
"help": "Path to the z-scan file to be loaded "
"to determine the mu*D value.",
"default": None,
"widget": "FileChooser",
},
]
return args
def get_args(override_cli_inputs=None):
p = ArgumentParser()
for arg in define_arguments():
kwargs = {
key: value
for key, value in arg.items()
if key != "name" and key != "widget"
}
p.add_argument(*arg["name"], **kwargs)
args = p.parse_args(override_cli_inputs)
return args
@Gooey(required_cols=1, optional_cols=1, program_name="Labpdfproc GUI")
def gooey_parser():
p = GooeyParser()
for arg in define_arguments():
kwargs = {key: value for key, value in arg.items() if key != "name"}
p.add_argument(*arg["name"], **kwargs)
args = p.parse_args()
return args
def main():
args = (
gooey_parser()
if len(sys.argv) == 1 or "--gui" in sys.argv
else get_args()
)
args = preprocessing_args(args)
for filepath in args.input_paths:
outfilestem = filepath.stem + "_corrected"
corrfilestem = filepath.stem + "_cve"
outfile = args.output_directory / (outfilestem + ".chi")
corrfile = args.output_directory / (corrfilestem + ".chi")
if outfile.exists() and not args.force_overwrite:
sys.exit(
f"Output file {str(outfile)} already exists. "
f"Please rerun specifying -f if you want to overwrite it."
)
if (
corrfile.exists()
and args.output_correction
and not args.force_overwrite
):
sys.exit(
f"Corrections file {str(corrfile)} "
f"was requested and already exists. "
f"Please rerun specifying -f if you want to overwrite it."
)
xarray, yarray = loadData(filepath, unpack=True)
input_pattern = DiffractionObject(
xarray=xarray,
yarray=yarray,
xtype=args.xtype,
wavelength=args.wavelength,
scat_quantity="x-ray",
name=filepath.stem,
metadata=load_metadata(args, filepath),
)
absorption_correction = compute_cve(
input_pattern, args.mud, args.method, args.xtype
)
corrected_data = apply_corr(input_pattern, absorption_correction)
corrected_data.name = (
f"Absorption corrected input_data: " f"{input_pattern.name}"
)
corrected_data.dump(f"{outfile}", xtype=args.xtype)
if args.output_correction:
absorption_correction.dump(f"{corrfile}", xtype=args.xtype)
if __name__ == "__main__":
main()