|
1 | 1 | import sys
|
2 | 2 | from argparse import ArgumentParser
|
3 | 3 |
|
| 4 | +from gooey import Gooey, GooeyParser |
| 5 | + |
4 | 6 | from diffpy.labpdfproc.functions import CVE_METHODS, apply_corr, compute_cve
|
5 | 7 | from diffpy.labpdfproc.tools import known_sources, load_metadata, preprocessing_args
|
6 | 8 | from diffpy.utils.parsers.loaddata import loadData
|
7 | 9 | from diffpy.utils.scattering_objects.diffraction_objects import XQUANTITIES, Diffraction_object
|
8 | 10 |
|
9 | 11 |
|
| 12 | +def define_arguments(): |
| 13 | + args = [ |
| 14 | + { |
| 15 | + "name": ["mud"], |
| 16 | + "help": "Value of mu*D for your sample. Required.", |
| 17 | + "type": float, |
| 18 | + }, |
| 19 | + { |
| 20 | + "name": ["input"], |
| 21 | + "help": ( |
| 22 | + "The filename(s) or folder(s) of the datafile(s) to load. " |
| 23 | + "Required.\nSupply a space-separated list of files or directories. " |
| 24 | + "Long lists can be supplied, one per line, in a file with name " |
| 25 | + "file_list.txt. If one or more directory is provided, all valid " |
| 26 | + "data-files in that directory will be processed. Examples of valid " |
| 27 | + "inputs are 'file.xy', 'data/file.xy', 'file.xy, data/file.xy', " |
| 28 | + "'.' (load everything in the current directory), 'data' (load " |
| 29 | + "everything in the folder ./data), 'data/file_list.txt' (load " |
| 30 | + "the list of files contained in the text-file called " |
| 31 | + "file_list.txt that can be found in the folder ./data), " |
| 32 | + "'./*.chi', 'data/*.chi' (load all files with extension .chi in the " |
| 33 | + "folder ./data)." |
| 34 | + ), |
| 35 | + "nargs": "+", |
| 36 | + "widget": "MultiFileChooser", |
| 37 | + }, |
| 38 | + { |
| 39 | + "name": ["-a", "--anode-type"], |
| 40 | + "help": ( |
| 41 | + f"The type of the x-ray source. Allowed values are " |
| 42 | + f"{*[known_sources], }. Either specify a known x-ray source or specify wavelength." |
| 43 | + ), |
| 44 | + "default": "Mo", |
| 45 | + }, |
| 46 | + { |
| 47 | + "name": ["-w", "--wavelength"], |
| 48 | + "help": ( |
| 49 | + "X-ray source wavelength in angstroms. Not needed if the anode-type " |
| 50 | + "is specified. This wavelength will override the anode wavelength if both are specified." |
| 51 | + ), |
| 52 | + "type": float, |
| 53 | + }, |
| 54 | + { |
| 55 | + "name": ["-o", "--output-directory"], |
| 56 | + "help": ( |
| 57 | + "The name of the output directory. If not specified " |
| 58 | + "then corrected files will be written to the current directory. " |
| 59 | + "If the specified directory doesn't exist it will be created." |
| 60 | + ), |
| 61 | + "default": None, |
| 62 | + }, |
| 63 | + { |
| 64 | + "name": ["-x", "--xtype"], |
| 65 | + "help": ( |
| 66 | + f"The quantity on the independent variable axis. Allowed " |
| 67 | + f"values: {*XQUANTITIES, }. If not specified then two-theta " |
| 68 | + f"is assumed for the independent variable." |
| 69 | + ), |
| 70 | + "default": "tth", |
| 71 | + }, |
| 72 | + { |
| 73 | + "name": ["-c", "--output-correction"], |
| 74 | + "help": ( |
| 75 | + "The absorption correction will be output to a file if this " |
| 76 | + "flag is set. Default is that it is not output." |
| 77 | + ), |
| 78 | + "action": "store_true", |
| 79 | + }, |
| 80 | + { |
| 81 | + "name": ["-f", "--force-overwrite"], |
| 82 | + "help": "Outputs will not overwrite existing file unless --force is specified.", |
| 83 | + "action": "store_true", |
| 84 | + }, |
| 85 | + { |
| 86 | + "name": ["-m", "--method"], |
| 87 | + "help": ( |
| 88 | + f"The method for computing absorption correction. Allowed methods: {*CVE_METHODS, }. " |
| 89 | + f"Default method is polynomial interpolation if not specified. " |
| 90 | + ), |
| 91 | + "default": "polynomial_interpolation", |
| 92 | + }, |
| 93 | + { |
| 94 | + "name": ["-u", "--user-metadata"], |
| 95 | + "help": ( |
| 96 | + "Specify key-value pairs to be loaded into metadata using the format key=value. " |
| 97 | + "Separate pairs with whitespace, and ensure no whitespaces before or after the = sign. " |
| 98 | + "Avoid using = in keys. If multiple = signs are present, " |
| 99 | + "only the first separates the key and value. " |
| 100 | + "If a key or value contains whitespace, enclose it in quotes. " |
| 101 | + "For example, facility='NSLS II', 'facility=NSLS II', beamline=28ID-2, " |
| 102 | + "'beamline'='28ID-2', 'favorite color'=blue, are all valid key=value items. " |
| 103 | + ), |
| 104 | + "nargs": "+", |
| 105 | + "metavar": "KEY=VALUE", |
| 106 | + }, |
| 107 | + { |
| 108 | + "name": ["-n", "--username"], |
| 109 | + "help": ( |
| 110 | + "Username will be loaded from config files. Specify here " |
| 111 | + "only if you want to override that behavior at runtime. " |
| 112 | + ), |
| 113 | + "default": None, |
| 114 | + }, |
| 115 | + { |
| 116 | + "name": ["-e", "--email"], |
| 117 | + "help": ( |
| 118 | + "Email will be loaded from config files. Specify here " |
| 119 | + "only if you want to override that behavior at runtime. " |
| 120 | + ), |
| 121 | + "default": None, |
| 122 | + }, |
| 123 | + { |
| 124 | + "name": ["-z", "--z-scan-file"], |
| 125 | + "help": "Path to the z-scan file to be loaded to determine the mu*D value", |
| 126 | + "default": None, |
| 127 | + "widget": "FileChooser", |
| 128 | + }, |
| 129 | + ] |
| 130 | + return args |
| 131 | + |
| 132 | + |
10 | 133 | def get_args(override_cli_inputs=None):
|
11 | 134 | p = ArgumentParser()
|
12 |
| - p.add_argument("mud", help="Value of mu*D for your " "sample. Required.", type=float) |
13 |
| - p.add_argument( |
14 |
| - "input", |
15 |
| - nargs="+", |
16 |
| - help=( |
17 |
| - "The filename(s) or folder(s) of the datafile(s) to load. " |
18 |
| - "Required.\nSupply a space-separated list of files or directories. " |
19 |
| - "Long lists can be supplied, one per line, in a file with name " |
20 |
| - "file_list.txt. If one or more directory is provided, all valid " |
21 |
| - "data-files in that directory will be processed. Examples of valid " |
22 |
| - "inputs are 'file.xy', 'data/file.xy', 'file.xy, data/file.xy', " |
23 |
| - "'.' (load everything in the current directory), 'data' (load " |
24 |
| - "everything in the folder ./data), 'data/file_list.txt' (load " |
25 |
| - "the list of files contained in the text-file called " |
26 |
| - "file_list.txt that can be found in the folder ./data), " |
27 |
| - "'./*.chi', 'data/*.chi' (load all files with extension .chi in the " |
28 |
| - "folder ./data)." |
29 |
| - ), |
30 |
| - ) |
31 |
| - p.add_argument( |
32 |
| - "-a", |
33 |
| - "--anode-type", |
34 |
| - help=( |
35 |
| - f"The type of the x-ray source. Allowed values are " |
36 |
| - f"{*[known_sources], }. Either specify a known x-ray source or specify wavelength." |
37 |
| - ), |
38 |
| - default="Mo", |
39 |
| - ) |
40 |
| - p.add_argument( |
41 |
| - "-w", |
42 |
| - "--wavelength", |
43 |
| - help=( |
44 |
| - "X-ray source wavelength in angstroms. Not needed if the anode-type " |
45 |
| - "is specified. This wavelength will override the anode wavelength if both are specified." |
46 |
| - ), |
47 |
| - default=None, |
48 |
| - type=float, |
49 |
| - ) |
50 |
| - p.add_argument( |
51 |
| - "-o", |
52 |
| - "--output-directory", |
53 |
| - help=( |
54 |
| - "The name of the output directory. If not specified " |
55 |
| - "then corrected files will be written to the current directory. " |
56 |
| - "If the specified directory doesn't exist it will be created." |
57 |
| - ), |
58 |
| - default=None, |
59 |
| - ) |
60 |
| - p.add_argument( |
61 |
| - "-x", |
62 |
| - "--xtype", |
63 |
| - help=( |
64 |
| - f"The quantity on the independent variable axis. Allowed " |
65 |
| - f"values: {*XQUANTITIES, }. If not specified then two-theta " |
66 |
| - f"is assumed for the independent variable." |
67 |
| - ), |
68 |
| - default="tth", |
69 |
| - ) |
70 |
| - p.add_argument( |
71 |
| - "-c", |
72 |
| - "--output-correction", |
73 |
| - action="store_true", |
74 |
| - help=( |
75 |
| - "The absorption correction will be output to a file if this " |
76 |
| - "flag is set. Default is that it is not output." |
77 |
| - ), |
78 |
| - ) |
79 |
| - p.add_argument( |
80 |
| - "-f", |
81 |
| - "--force-overwrite", |
82 |
| - action="store_true", |
83 |
| - help="Outputs will not overwrite existing file unless --force is specified.", |
84 |
| - ) |
85 |
| - p.add_argument( |
86 |
| - "-m", |
87 |
| - "--method", |
88 |
| - help=( |
89 |
| - f"The method for computing absorption correction. Allowed methods: {*CVE_METHODS, }. " |
90 |
| - f"Default method is polynomial interpolation if not specified. " |
91 |
| - ), |
92 |
| - default="polynomial_interpolation", |
93 |
| - ) |
94 |
| - p.add_argument( |
95 |
| - "-u", |
96 |
| - "--user-metadata", |
97 |
| - metavar="KEY=VALUE", |
98 |
| - nargs="+", |
99 |
| - help=( |
100 |
| - "Specify key-value pairs to be loaded into metadata using the format key=value. " |
101 |
| - "Separate pairs with whitespace, and ensure no whitespaces before or after the = sign. " |
102 |
| - "Avoid using = in keys. If multiple = signs are present, only the first separates the key and value. " |
103 |
| - "If a key or value contains whitespace, enclose it in quotes. " |
104 |
| - "For example, facility='NSLS II', 'facility=NSLS II', beamline=28ID-2, " |
105 |
| - "'beamline'='28ID-2', 'favorite color'=blue, are all valid key=value items. " |
106 |
| - ), |
107 |
| - ) |
108 |
| - p.add_argument( |
109 |
| - "-n", |
110 |
| - "--username", |
111 |
| - help=( |
112 |
| - "Username will be loaded from config files. Specify here " |
113 |
| - "only if you want to override that behavior at runtime. " |
114 |
| - ), |
115 |
| - default=None, |
116 |
| - ) |
117 |
| - p.add_argument( |
118 |
| - "-e", |
119 |
| - "--email", |
120 |
| - help=( |
121 |
| - "Email will be loaded from config files. Specify here " |
122 |
| - "only if you want to override that behavior at runtime. " |
123 |
| - ), |
124 |
| - default=None, |
125 |
| - ) |
126 |
| - p.add_argument( |
127 |
| - "-z", |
128 |
| - "--z-scan-file", |
129 |
| - help="Path to the z-scan file to be loaded to determine the mu*D value", |
130 |
| - default=None, |
131 |
| - ) |
| 135 | + for arg in define_arguments(): |
| 136 | + kwargs = {key: value for key, value in arg.items() if key != "name" and key != "widget"} |
| 137 | + p.add_argument(*arg["name"], **kwargs) |
132 | 138 | args = p.parse_args(override_cli_inputs)
|
133 | 139 | return args
|
134 | 140 |
|
135 | 141 |
|
| 142 | +@Gooey(required_cols=1, optional_cols=1, program_name="Labpdfproc GUI") |
| 143 | +def gooey_parser(): |
| 144 | + p = GooeyParser() |
| 145 | + for arg in define_arguments(): |
| 146 | + kwargs = {key: value for key, value in arg.items() if key != "name"} |
| 147 | + p.add_argument(*arg["name"], **kwargs) |
| 148 | + args = p.parse_args() |
| 149 | + return args |
| 150 | + |
| 151 | + |
136 | 152 | def main():
|
137 |
| - args = get_args() |
| 153 | + args = gooey_parser() if len(sys.argv) == 1 or "--gui" in sys.argv else get_args() |
138 | 154 | args = preprocessing_args(args)
|
139 | 155 |
|
140 | 156 | for filepath in args.input_paths:
|
|
0 commit comments