Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/trexio_tools/converters/convert_back_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ def run_converter(filename_from, filename_to, back_end_to, back_end_from=None, o
"""The high-level converter function."""

import os
try:
import trexio
except ImportError as exc:
raise ImportError("trexio Python package is not installed.") from exc
import trexio

try:
# For proper python package
Expand All @@ -115,6 +112,9 @@ def run_converter(filename_from, filename_to, back_end_to, back_end_from=None, o
else:
raise ValueError(f'Output file {filename_to} already exists. Consider using the `-w` argument.')

if back_end_from is None:
back_end_from = trexio.TREXIO_AUTO

with trexio.File(filename_from, 'r', back_end_from) as trexio_file_from:
with trexio.File(filename_to, 'w', back_end_to) as trexio_file_to:
data_handler(trexio_file_from, trexio_file_to)
Expand Down
12 changes: 1 addition & 11 deletions src/trexio_tools/converters/convert_from.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
from .orca_to_trexio import orca_to_trexio as run_orca
from .crystal_to_trexio import crystal_to_trexio as run_crystal

try:
import trexio
except ImportError as exc:
raise ImportError("trexio Python package is not installed.") from exc
import trexio

try:
from resultsFile import getFile, a0, get_lm
Expand Down Expand Up @@ -759,13 +756,6 @@ def run_molden(trexio_file, filename, normalized_basis=True, multiplicity=None,

def run(trexio_filename, filename, filetype, back_end, spin=None, motype=None):

if os.path.exists(trexio_filename):
print(f"TREXIO file {trexio_filename} already exists and will be removed before conversion.")
if back_end == trexio.TREXIO_HDF5:
os.remove(trexio_filename)
else:
raise NotImplementedError(f"Please remove the {trexio_filename} directory manually.")

if "pyscf" not in filetype.lower():
trexio_file = trexio.File(trexio_filename, mode='w', back_end=back_end)

Expand Down
9 changes: 1 addition & 8 deletions src/trexio_tools/converters/convert_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
from . import cart_sphe as cart_sphe
import numpy as np

try:
import trexio
except:
print("Error: The TREXIO Python library is not installed")
sys.exit(1)
import trexio

"""
Converter from trexio to fcidump
Expand Down Expand Up @@ -606,11 +602,8 @@ def run(trexio_filename, filename, filetype, spin_order):
run_spherical(trexio_file, filename)
# elif filetype.lower() == "normalized_aos":
# run_normalized_aos(trexio_file, filename)
# elif filetype.lower() == "gamess":
# run_resultsFile(trexio_file, filename)
elif filetype.lower() == "fcidump":
run_fcidump(trexio_file, filename, spin_order)
# elif filetype.lower() == "molden":
else:
raise NotImplementedError(f"Conversion from TREXIO to {filetype} is not supported.")

6 changes: 0 additions & 6 deletions src/trexio_tools/converters/orca_to_trexio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ def orca_to_trexio(
with open(orca_json, 'r') as f:
data = json.load(f)

import os
try:
os.remove(filename)
except:
print(f"File {filename} does not exist.")

# trexio back end handling
if back_end.lower() == "hdf5":
trexio_back_end = trexio.TREXIO_HDF5
Expand Down
40 changes: 24 additions & 16 deletions src/trexio_tools/trexio_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
trexio check-basis [-n N_POINTS] [-b BACK_END] TREXIO_FILE
trexio check-mos [-n N_POINTS] [-b BACK_END] TREXIO_FILE
trexio convert-to -t TYPE -o OUTPUT_FILE [-y SPIN_ORDER] TREXIO_FILE
trexio convert-from -t TYPE -i INPUT_FILE [-b BACK_END] [-x MO_TYPE] [-m MULTIPLICITY] TREXIO_FILE
trexio convert-from -t TYPE -i INPUT_FILE [-b BACK_END] [-x MO_TYPE] [-m MULTIPLICITY] [-w OVERWRITE] TREXIO_FILE
trexio convert-backend -i INPUT_FILE -o OUTPUT_FILE -b BACK_END -j TREX_JSON_FILE [-s BACK_END_FROM] [-w OVERWRITE]
trexio (-h | --help)

Expand All @@ -19,7 +19,7 @@
-s, --back_end_from=BACK_END [hdf5 | text | auto] The input TREXIO back end. [default: auto]
-m, --multiplicity=MULTIPLICITY Spin multiplicity for the Crystal converter.
-j, --json=TREX_JSON_FILE TREX configuration file (in JSON format).
-w, --overwrite=OVERWRITE Overwrite flag for the conversion of back ends. [default: True]
-w, --overwrite=OVERWRITE Overwrite the output TREXIO file if it already exists. [default: True]
-t, --type=TYPE [gaussian | gamess | pyscf | orca | crystal | fcidump | molden | cartesian ] File format.
-x, --motype=MO_TYPE Type of the molecular orbitals. For example, GAMESS has RHF, MCSCF, GUGA, and Natural as possible MO types.
-y, --spin_order=TYPE [block | interleave] How to organize spin orbitals when converting to FCIDUMP [default: block]
Expand All @@ -30,24 +30,35 @@
import os


def main(filename=None, args=None):
def remove_trexio_file(filename:str, overwrite:bool) -> None:
"""Remove the TREXIO file/directory if it exists."""
if os.path.exists(filename):
if overwrite:
if '*' in filename:
raise ValueError(f'TREXIO filename {filename} contains * symbol. Are you sure?')
os.system(f'rm -rf -- {filename} ')
else:
raise Exception(f'Output file {filename} already exists but overwrite option is not provided. Consider using the `-w` CLI argument.')

return

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suppose that a user calls trexio_convert -w $HOME/$MY_TREXIO_FILE, this wil call rm -rf -- $HOME/$MY_TREXIO_FILE. If the user made a mistake in his/her script and forgot to define $MY_TREXIO_FILE, it wil make rm -rf $HOME/. :-(
It would be safer to try to open the trexio file first and check something inside it like the trexio metadata, so that we are sure that the thing we remove is indeed a trexio file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, please have a look.


def main(filename=None, args=None) -> None:
"""Main entry point"""

if filename is None and args is None:
args = docopt(__doc__)
filename = args["TREXIO_FILE"]

if args["--n_points"]:
n_points = int(args["--n_points"])
else:
n_points = 81
n_points = int(args["--n_points"]) if args["--n_points"] else 81

overwrite = not str(args["--overwrite"]).lower() in ['false', '0']

if args["convert-backend"]:
if str(args["--overwrite"]).lower() in ['false', '0']:
overwrite = False
else:
overwrite = True
print(f'File {args["--output"]} will be overwritten.')
remove_trexio_file(args["--output"], overwrite)

if args["convert-from"]:
remove_trexio_file(args["TREXIO_FILE"], overwrite)

if args["--back_end"]:
if str(args["--back_end"]).lower() == "hdf5":
Expand All @@ -74,9 +85,7 @@ def main(filename=None, args=None):
else:
back_end_from = trexio.TREXIO_AUTO

spin = None
if args["--multiplicity"]:
spin = int(args["--multiplicity"])
spin = int(args["--multiplicity"]) if args["--multiplicity"] else None

if args["check-basis"]:
trexio_file = trexio.File(filename, 'r', back_end=back_end)
Expand Down Expand Up @@ -109,7 +118,6 @@ def main(filename=None, args=None):
args["--output"],
back_end_to=back_end,
back_end_from=back_end_from,
overwrite=overwrite,
json_filename=args["--json"]
)

Expand Down