Coverage for cclib/scripts/ccframe.py : 56%
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) 2019, 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"""Script for writing data tables from computational chemistry files."""
10import argparse
11import os.path
12import sys
14from cclib.io import ccopen
15from cclib.io import ccframe
16from cclib.parser.utils import find_package
18_has_pandas = find_package("pandas")
19if _has_pandas:
20 import pandas as pd
23def process_logfiles(filenames, output, identifier):
24 df = ccframe([ccopen(path) for path in filenames])
25 if output is not None:
26 outputtype = os.path.splitext(os.path.basename(output))[1][1:]
27 if not outputtype:
28 raise RuntimeWarning(
29 "The output type could not be determined from the given path, "
30 "not writing DataFrame to disk"
31 )
33 if outputtype in {'csv'}:
34 df.to_csv(output, mode='w')
35 elif outputtype in {'h5', 'hdf', 'hdf5'}:
36 df.to_hdf(output, mode='w', key=identifier)
37 elif outputtype in {'json'}:
38 df.to_json(output)
39 elif outputtype in {'pickle', 'pkl'}:
40 df.to_pickle(output)
41 elif outputtype in {'xlsx'}:
42 writer = pd.ExcelWriter(output, mode='w')
43 # This overwrites previous sheets
44 # (see https://stackoverflow.com/a/42375263/4039050)
45 df.to_excel(writer, sheet_name=identifier)
46 writer.save()
47 else:
48 print(df)
51def main():
52 parser = argparse.ArgumentParser()
53 parser.add_argument('-O', '--output',
54 help=('the output document to write, including an '
55 'extension supported by pandas '
56 '(csv, h5/hdf/hdf5, json, pickle/pkl, xlsx)'))
57 parser.add_argument('compchemlogfiles', metavar='compchemlogfile',
58 nargs='+',
59 help=('one or more computational chemistry output '
60 'files to parse and convert'))
61 parser.add_argument('--identifier',
62 default='logfiles',
63 help=('name of sheet which will contain DataFrame, if '
64 'writing to an Excel file, or identifier for '
65 'the group in HDFStore, if writing a HDF file'))
66 parser.add_argument('-f', '--force', action='store_true',
67 help=('overwrite output file in case it already exists'))
68 args = parser.parse_args()
69 if args.output is not None and not args.force and os.path.exists(args.output):
70 parser.exit(1, 'failure: exiting to avoid overwriting existing file "{}"\n'.format(args.output))
72 process_logfiles(args.compchemlogfiles, args.output, args.identifier)
75if __name__ == "__main__":
76 main()