Coverage for cclib/scripts/ccwrite.py : 85%
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#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright (c) 2017, the cclib development team
5#
6# This file is part of cclib (http://cclib.github.io) and is distributed under
7# the terms of the BSD 3-Clause License.
9import argparse
10import logging
11import os.path
12import sys
14from cclib.parser import ccData
15from cclib.io import ccopen
16from cclib.io import ccwrite
19def main():
21 parser = argparse.ArgumentParser()
23 parser.add_argument('outputtype',
24 choices=('json', 'cjson', 'cml', 'xyz', 'molden', 'wfx'),
25 help='the output format to write (json/cjson are identical)')
26 parser.add_argument('compchemlogfile',
27 nargs='+',
28 help='one or more computational chemistry output files to parse and convert')
30 parser.add_argument('-v', '--verbose',
31 action='store_true',
32 help='more verbose parsing output (only errors by default)')
34 parser.add_argument('-g', '--ghost',
35 type=str,
36 default=None,
37 help='Symbol to use for ghost atoms')
39 parser.add_argument('-t', '--terse',
40 action='store_true',
41 help='CJSON by default is not indented for readability, saves space (indented for readability\'s sake)')
43 parser.add_argument('-u', '--future',
44 action='store_true',
45 help='use experimental features (currently optdone_as_list)')
47 parser.add_argument('-i', '--index',
48 type=int,
49 default=None,
50 help='optional zero-based index for which structure to extract')
52 args = parser.parse_args()
54 outputtype = args.outputtype
55 filenames = args.compchemlogfile
56 verbose = args.verbose
57 terse = args.terse
58 future = args.future
59 index = args.index
60 ghost = args.ghost
62 for filename in filenames:
64 # We might want to use this option in the near future.
65 ccopen_kwargs = dict()
66 if future:
67 ccopen_kwargs['future'] = True
69 print("Attempting to parse {}".format(filename))
70 log = ccopen(filename, **ccopen_kwargs)
72 if not log:
73 print("Cannot figure out what type of computational chemistry output file '{}' is.".format(filename))
74 print("Report this to the cclib development team if you think this is an error.")
75 sys.exit()
77 if verbose:
78 log.logger.setLevel(logging.INFO)
79 else:
80 log.logger.setLevel(logging.ERROR)
81 data = log.parse()
83 print("cclib can parse the following attributes from {}:".format(filename))
84 hasattrs = [' {}'.format(attr) for attr in ccData._attrlist if hasattr(data, attr)]
85 print('\n'.join(hasattrs))
87 # Write out to disk.
88 outputdest = '.'.join([os.path.splitext(os.path.basename(filename))[0], outputtype])
89 ccwrite_kwargs = dict()
90 if future:
91 ccwrite_kwargs['future'] = True
92 if ghost:
93 ccwrite_kwargs['ghost'] = ghost
94 # For XYZ files, write the last geometry unless otherwise
95 # specified.
96 if not index:
97 index = -1
98 ccwrite_kwargs['jobfilename'] = filename
100 # The argument terse presently is only applicable to
101 # CJSON/JSON formats
102 ccwrite(data, outputtype, outputdest, indices=index, terse=terse,
103 **ccwrite_kwargs)
106if __name__ == "__main__":
107 main()