Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Note: 

2# Try to avoid module level import statements here to reduce 

3# import time during CLI execution 

4 

5 

6class CLICommand: 

7 """Convert between file formats. 

8 

9 Use "-" for stdin/stdout. 

10 See "ase info --formats" for known formats. 

11 """ 

12 

13 @staticmethod 

14 def add_arguments(parser): 

15 add = parser.add_argument 

16 add('-v', '--verbose', action='store_true', 

17 help='Print names of converted files') 

18 add('input', nargs='+', metavar='input-file') 

19 add('-i', '--input-format', metavar='FORMAT', 

20 help='Specify input FORMAT') 

21 add('output', metavar='output-file') 

22 add('-o', '--output-format', metavar='FORMAT', 

23 help='Specify output FORMAT') 

24 add('-f', '--force', action='store_true', 

25 help='Overwrite an existing file') 

26 add('-n', '--image-number', 

27 default=':', metavar='NUMBER', 

28 help='Pick images from trajectory. NUMBER can be a ' 

29 'single number (use a negative number to count from ' 

30 'the back) or a range: start:stop:step, where the ' 

31 '":step" part can be left out - default values are ' 

32 '0:nimages:1.') 

33 add('-e', '--exec-code', 

34 help='Python code to execute on each atoms before ' 

35 'writing it to output file. The Atoms object is ' 

36 'available as `atoms`. Set `atoms.info["_output"] = False` ' 

37 'to suppress output of this frame.') 

38 add('-E', '--exec-file', 

39 help='Python source code file to execute on each ' 

40 'frame, usage is as for -e/--exec-code.') 

41 add('-a', '--arrays', 

42 help='Comma-separated list of atoms.arrays entries to include ' 

43 'in output file. Default is all entries.') 

44 add('-I', '--info', 

45 help='Comma-separated list of atoms.info entries to include ' 

46 'in output file. Default is all entries.') 

47 add('-s', '--split-output', action='store_true', 

48 help='Write output frames to individual files. ' 

49 'Output file name should be a format string with ' 

50 'a single integer field, e.g. out-{:0>5}.xyz') 

51 add('--read-args', nargs='+', action='store', 

52 default={}, metavar="KEY=VALUE", 

53 help='Additional keyword arguments to pass to ' 

54 '`ase.io.read()`.') 

55 add('--write-args', nargs='+', action='store', 

56 default={}, metavar="KEY=VALUE", 

57 help='Additional keyword arguments to pass to ' 

58 '`ase.io.write()`.') 

59 

60 @staticmethod 

61 def run(args, parser): 

62 import os 

63 from ase.io import read, write 

64 

65 if args.verbose: 

66 print(', '.join(args.input), '->', args.output) 

67 if args.arrays: 

68 args.arrays = [k.strip() for k in args.arrays.split(',')] 

69 if args.verbose: 

70 print('Filtering to include arrays: ', ', '.join(args.arrays)) 

71 if args.info: 

72 args.info = [k.strip() for k in args.info.split(',')] 

73 if args.verbose: 

74 print('Filtering to include info: ', ', '.join(args.info)) 

75 if args.read_args: 

76 args.read_args = eval("dict({0})" 

77 .format(', '.join(args.read_args))) 

78 if args.write_args: 

79 args.write_args = eval("dict({0})" 

80 .format(', '.join(args.write_args))) 

81 

82 configs = [] 

83 for filename in args.input: 

84 atoms = read(filename, args.image_number, 

85 format=args.input_format, **args.read_args) 

86 if isinstance(atoms, list): 

87 configs.extend(atoms) 

88 else: 

89 configs.append(atoms) 

90 

91 new_configs = [] 

92 for atoms in configs: 

93 if args.arrays: 

94 atoms.arrays = dict((k, atoms.arrays[k]) for k in args.arrays) 

95 if args.info: 

96 atoms.info = dict((k, atoms.info[k]) for k in args.info) 

97 if args.exec_code: 

98 # avoid exec() for Py 2+3 compat. 

99 eval(compile(args.exec_code, '<string>', 'exec')) 

100 if args.exec_file: 

101 eval(compile(open(args.exec_file).read(), args.exec_file, 

102 'exec')) 

103 if "_output" not in atoms.info or atoms.info["_output"]: 

104 new_configs.append(atoms) 

105 configs = new_configs 

106 

107 if not args.force and os.path.isfile(args.output): 

108 parser.error('File already exists: {}'.format(args.output)) 

109 

110 if args.split_output: 

111 for i, atoms in enumerate(configs): 

112 write(args.output.format(i), atoms, 

113 format=args.output_format, **args.write_args) 

114 else: 

115 write(args.output, configs, format=args.output_format, 

116 **args.write_args)