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
|
# Note:
# Try to avoid module level import statements here to reduce
# import time during CLI execution
class CLICommand:
"""Convert between file formats.
Use "-" for stdin/stdout.
See "ase info --formats" for known formats.
"""
@staticmethod
def add_arguments(parser):
add = parser.add_argument
add('-v', '--verbose', action='store_true',
help='Print names of converted files')
add('input', nargs='+', metavar='input-file')
add('-i', '--input-format', metavar='FORMAT',
help='Specify input FORMAT')
add('output', metavar='output-file')
add('-o', '--output-format', metavar='FORMAT',
help='Specify output FORMAT')
add('-f', '--force', action='store_true',
help='Overwrite an existing file')
add('-n', '--image-number',
default=':', metavar='NUMBER',
help='Pick images from trajectory. NUMBER can be a '
'single number (use a negative number to count from '
'the back) or a range: start:stop:step, where the '
'":step" part can be left out - default values are '
'0:nimages:1.')
add('-e', '--exec-code',
help='Python code to execute on each atoms before '
'writing it to output file. The Atoms object is '
'available as `atoms`. Set `atoms.info["_output"] = False` '
'to suppress output of this frame.')
add('-E', '--exec-file',
help='Python source code file to execute on each '
'frame, usage is as for -e/--exec-code.')
add('-a', '--arrays',
help='Comma-separated list of atoms.arrays entries to include '
'in output file. Default is all entries.')
add('-I', '--info',
help='Comma-separated list of atoms.info entries to include '
'in output file. Default is all entries.')
add('-s', '--split-output', action='store_true',
help='Write output frames to individual files. '
'Output file name should be a format string with '
'a single integer field, e.g. out-{:0>5}.xyz')
add('--read-args', nargs='+', action='store',
default={}, metavar="KEY=VALUE",
help='Additional keyword arguments to pass to '
'`ase.io.read()`.')
add('--write-args', nargs='+', action='store',
default={}, metavar="KEY=VALUE",
help='Additional keyword arguments to pass to '
'`ase.io.write()`.')
@staticmethod
def run(args, parser):
import os
from ase.io import read, write
if args.verbose:
print(', '.join(args.input), '->', args.output)
if args.arrays:
args.arrays = [k.strip() for k in args.arrays.split(',')]
if args.verbose:
print('Filtering to include arrays: ', ', '.join(args.arrays))
if args.info:
args.info = [k.strip() for k in args.info.split(',')]
if args.verbose:
print('Filtering to include info: ', ', '.join(args.info))
if args.read_args:
args.read_args = eval("dict({})"
.format(', '.join(args.read_args)))
if args.write_args:
args.write_args = eval("dict({})"
.format(', '.join(args.write_args)))
configs = []
for filename in args.input:
atoms = read(filename, args.image_number,
format=args.input_format, **args.read_args)
if isinstance(atoms, list):
configs.extend(atoms)
else:
configs.append(atoms)
new_configs = []
for atoms in configs:
if args.arrays:
atoms.arrays = {k: atoms.arrays[k] for k in args.arrays}
if args.info:
atoms.info = {k: atoms.info[k] for k in args.info}
if args.exec_code:
# avoid exec() for Py 2+3 compat.
eval(compile(args.exec_code, '<string>', 'exec'))
if args.exec_file:
eval(compile(open(args.exec_file).read(), args.exec_file,
'exec'))
if "_output" not in atoms.info or atoms.info["_output"]:
new_configs.append(atoms)
configs = new_configs
if not args.force and os.path.isfile(args.output):
parser.error(f'File already exists: {args.output}')
if args.split_output:
for i, atoms in enumerate(configs):
write(args.output.format(i), atoms,
format=args.output_format, **args.write_args)
else:
write(args.output, configs, format=args.output_format,
**args.write_args)
|