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