-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcli.py
More file actions
187 lines (169 loc) · 5.19 KB
/
cli.py
File metadata and controls
187 lines (169 loc) · 5.19 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
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
"""dspeed's command line interface utilities."""
from __future__ import annotations
import argparse
import os
import sys
from . import __version__, build_dsp, logging
def dspeed_cli():
"""dspeed's command line interface.
Defines the command line interface (CLI) of the package, which exposes some
of the most used functions to the console. This function is added to the
``entry_points.console_scripts`` list and defines the ``dspeed`` executable
(see ``setuptools``' documentation). To learn more about the CLI, have a
look at the help section:
.. code-block:: console
$ dspeed --hep
"""
parser = argparse.ArgumentParser(
prog="dspeed",
description="""Process LH5 raw files and produce a
dsp file using a JSON configuration""",
)
# global options
parser.add_argument(
"--version", action="store_true", help="""Print dspeed version and exit"""
)
parser.add_argument(
"--verbose",
"-v",
action="store_true",
help="""Increase the program verbosity""",
)
parser.add_argument(
"--debug",
"-d",
action="store_true",
help="""Increase the program verbosity to maximum""",
)
# build_dsp
parser.add_argument(
"raw_lh5_file",
nargs="+",
help="""Input raw LH5 file. Can be a single file or a list of them""",
)
parser.add_argument(
"--config",
"-c",
required=True,
help=""""JSON file holding configuration of signal processing
routines""",
)
parser.add_argument(
"--hdf5-groups",
"-g",
nargs="*",
default=None,
help="""Name of group in the LH5 file. By default process all base
groups. Supports wildcards""",
)
parser.add_argument(
"--output",
"-o",
default=None,
help="""Name of output file, if only one is supplied. By default,
output to <input-filename>_dsp.lh5""",
)
parser.add_argument(
"--database",
"-D",
default=None,
help="""JSON file to read database parameters from. Should be nested
dict with channel at the top level, and parameters below that""",
)
parser.add_argument(
"--output-pars",
"-p",
nargs="*",
default=None,
help="""List of additional output DSP parameters written to file. By
default use the "outputs" list defined in in the JSON
configuration file""",
)
parser.add_argument(
"--max-rows",
"-n",
default=None,
type=int,
help="""Number of rows to process. By default do the whole file""",
)
parser.add_argument(
"--block",
"-b",
default=16,
type=int,
help="""Number of waveforms to process simultaneously. Default is
16""",
)
parser.add_argument(
"--chunk",
"-k",
default=3200,
type=int,
help="""Number of waveforms to read from disk at a time. Default is
3200""",
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"--overwrite",
"-w",
action="store_const",
const="r",
dest="writemode",
default="r",
help="""Overwrite file if it already exists. Default option""",
)
group.add_argument(
"--update",
"-u",
action="store_const",
const="u",
dest="writemode",
help="""Update existing file with new values. Useful with the --output-pars
option""",
)
group.add_argument(
"--append",
"-a",
action="store_const",
const="a",
dest="writemode",
help="""Append values to existing file""",
)
args = parser.parse_args()
if args.verbose:
logging.setup(logging.DEBUG)
elif args.debug:
logging.setup(logging.DEBUG, logging.root)
else:
logging.setup()
if args.version:
print(__version__) # noqa: T201
sys.exit()
if len(args.raw_lh5_file) > 1 and args.output is not None:
raise NotImplementedError("not possible to set multiple output file names yet")
out_files = []
if len(args.raw_lh5_file) == 1:
if args.output is None:
basename = os.path.splitext(os.path.basename(args.raw_lh5_file[0]))[0]
basename = basename.removesuffix("_raw")
out_files.append(f"{basename}_dsp.lh5")
else:
out_files.append(args.output)
else:
for file in args.raw_lh5_file:
basename = os.path.splitext(os.path.basename(file))[0]
basename = basename.removesuffix("_raw")
out_files.append(f"{basename}_dsp.lh5")
for i in range(len(args.raw_lh5_file)):
build_dsp(
args.raw_lh5_file[i],
out_files[i],
args.config,
lh5_tables=args.hdf5_groups,
database=args.database,
outputs=args.output_pars,
n_entries=args.max_rows,
write_mode=args.writemode,
buffer_len=args.chunk,
block_width=args.block,
)