| 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.""" |
| 9 |
|
| 10 |
import argparse |
| 11 |
import os.path |
| 12 |
import sys |
| 13 |
|
| 14 |
from cclib.io import ccopen |
| 15 |
from cclib.io import ccframe |
| 16 |
from cclib.parser.utils import find_package |
| 17 |
|
| 18 |
_has_pandas = find_package("pandas") |
| 19 |
if _has_pandas: |
| 20 |
import pandas as pd |
| 21 |
|
| 22 |
|
| 23 |
def 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 |
) |
| 32 |
|
| 33 |
if outputtype in {'csv'}: |
| 34 |
df.to_csv(output) |
| 35 |
elif outputtype in {'h5', 'hdf', 'hdf5'}: |
| 36 |
df.to_hdf(output, 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) |
| 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) |
| 49 |
|
| 50 |
|
| 51 |
def 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 |
args = parser.parse_args() |
| 67 |
process_logfiles(args.compchemlogfiles, args.output, args.identifier) |
| 68 |
|
| 69 |
|
| 70 |
if __name__ == "__main__": |
| 71 |
main() |