Coverage for ase/cli/convert.py: 72.13%
61 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
« prev ^ index » next coverage.py v7.16.2, created at 2026-11-27 15:21 +0000
1# Note:
2# Try to avoid module level import statements here to reduce
3# import time during CLI execution
6class CLICommand:
7 """Convert between file formats.
9 Use "-" for stdin/stdout.
10 See "ase info --formats" for known formats.
11 """
13 @staticmethod
14 def add_arguments(parser):
15 add = parser.add_argument
16 add(
17 '-v',
18 '--verbose',
19 action='store_true',
20 help='Print names of converted files',
21 )
22 add('input', nargs='+', metavar='input-file')
23 add(
24 '-i',
25 '--input-format',
26 metavar='FORMAT',
27 help='Specify input FORMAT',
28 )
29 add('output', metavar='output-file')
30 add(
31 '-o',
32 '--output-format',
33 metavar='FORMAT',
34 help='Specify output FORMAT',
35 )
36 add(
37 '-f',
38 '--force',
39 action='store_true',
40 help='Overwrite an existing file',
41 )
42 add(
43 '-n',
44 '--image-number',
45 default=':',
46 metavar='NUMBER',
47 help='Pick images from trajectory. NUMBER can be a '
48 'single number (use a negative number to count from '
49 'the back) or a range: start:stop:step, where the '
50 '":step" part can be left out - default values are '
51 '0:nimages:1.',
52 )
53 add(
54 '-e',
55 '--exec-code',
56 help='Python code to execute on each atoms before '
57 'writing it to output file. The Atoms object is '
58 'available as `atoms`. Set `atoms.info["_output"] = False` '
59 'to suppress output of this frame.',
60 )
61 add(
62 '-E',
63 '--exec-file',
64 help='Python source code file to execute on each '
65 'frame, usage is as for -e/--exec-code.',
66 )
67 add(
68 '-a',
69 '--arrays',
70 help='Comma-separated list of atoms.arrays entries to include '
71 'in output file. Default is all entries.',
72 )
73 add(
74 '-I',
75 '--info',
76 help='Comma-separated list of atoms.info entries to include '
77 'in output file. Default is all entries.',
78 )
79 add(
80 '-s',
81 '--split-output',
82 action='store_true',
83 help='Write output frames to individual files. '
84 'Output file name should be a format string with '
85 'a single integer field, e.g. out-{:0>5}.xyz',
86 )
87 add(
88 '--read-args',
89 nargs='+',
90 action='store',
91 default={},
92 metavar='KEY=VALUE',
93 help='Additional keyword arguments to pass to `ase.io.read()`.',
94 )
95 add(
96 '--write-args',
97 nargs='+',
98 action='store',
99 default={},
100 metavar='KEY=VALUE',
101 help='Additional keyword arguments to pass to `ase.io.write()`.',
102 )
104 @staticmethod
105 def run(args, parser):
106 import os
108 from ase.io import read, write
110 if args.verbose:
111 print(', '.join(args.input), '->', args.output)
112 if args.arrays:
113 args.arrays = [k.strip() for k in args.arrays.split(',')]
114 if args.verbose:
115 print('Filtering to include arrays: ', ', '.join(args.arrays))
116 if args.info:
117 args.info = [k.strip() for k in args.info.split(',')]
118 if args.verbose:
119 print('Filtering to include info: ', ', '.join(args.info))
120 if args.read_args:
121 args.read_args = eval('dict({})'.format(', '.join(args.read_args)))
122 if args.write_args:
123 args.write_args = eval(
124 'dict({})'.format(', '.join(args.write_args))
125 )
127 configs = []
128 for filename in args.input:
129 atoms = read(
130 filename,
131 args.image_number,
132 format=args.input_format,
133 **args.read_args,
134 )
135 if isinstance(atoms, list):
136 configs.extend(atoms)
137 else:
138 configs.append(atoms)
140 new_configs = []
141 for atoms in configs:
142 if args.arrays:
143 atoms.arrays = {k: atoms.arrays[k] for k in args.arrays}
144 if args.info:
145 atoms.info = {k: atoms.info[k] for k in args.info}
146 if args.exec_code:
147 # avoid exec() for Py 2+3 compat.
148 eval(compile(args.exec_code, '<string>', 'exec'))
149 if args.exec_file:
150 eval(
151 compile(open(args.exec_file).read(), args.exec_file, 'exec')
152 )
153 if '_output' not in atoms.info or atoms.info['_output']:
154 new_configs.append(atoms)
155 configs = new_configs
157 if not args.force and os.path.isfile(args.output):
158 parser.error(f'File already exists: {args.output}')
160 if args.split_output:
161 for i, atoms in enumerate(configs):
162 write(
163 args.output.format(i),
164 atoms,
165 format=args.output_format,
166 **args.write_args,
167 )
168 else:
169 write(
170 args.output,
171 configs,
172 format=args.output_format,
173 **args.write_args,
174 )