diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3dac900..1bf2f09 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -45,6 +45,9 @@ jobs: trexio convert-to -t cartesian -o trexio_orca_h2o.h5 trexio_orca_h2o_sph.h5 trexio convert-to -t cartesian -o trexio_pyscf_h2o.h5 trexio_pyscf_h2o_sph.h5 echo "== Done conversion ==" + # dummy checks for overwriting + trexio convert-from -t gaussian -i data/chbrclf.log -b hdf5 -w False trexio_gaussi.h5 || echo "Failure as it should" + trexio convert-from -t gaussian -i data/chbrclf.log -b hdf5 -w True trexio_gaussi.h5 && echo "Success as it should" echo "=== Check TREXIO file converted from GAMESS ===" trexio check-mos -n 50 trexio_gamess.h5 > mos-res grep "Norm of the error" < mos-res | grep -Eo "([0-9]+\.[0-9]*|\.?[0-9]+)([eE][+-][0-9]+)?" > error-res diff --git a/src/trexio_tools/converters/convert_back_end.py b/src/trexio_tools/converters/convert_back_end.py index 4dcff17..4511c73 100644 --- a/src/trexio_tools/converters/convert_back_end.py +++ b/src/trexio_tools/converters/convert_back_end.py @@ -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 @@ -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) diff --git a/src/trexio_tools/converters/convert_from.py b/src/trexio_tools/converters/convert_from.py index 0edd6fc..e1ffb0b 100644 --- a/src/trexio_tools/converters/convert_from.py +++ b/src/trexio_tools/converters/convert_from.py @@ -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 @@ -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) diff --git a/src/trexio_tools/converters/convert_to.py b/src/trexio_tools/converters/convert_to.py index 28568b3..1ca9fb4 100755 --- a/src/trexio_tools/converters/convert_to.py +++ b/src/trexio_tools/converters/convert_to.py @@ -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 @@ -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.") diff --git a/src/trexio_tools/converters/orca_to_trexio.py b/src/trexio_tools/converters/orca_to_trexio.py index 65fc7ed..38cf8b8 100644 --- a/src/trexio_tools/converters/orca_to_trexio.py +++ b/src/trexio_tools/converters/orca_to_trexio.py @@ -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 diff --git a/src/trexio_tools/trexio_run.py b/src/trexio_tools/trexio_run.py index e77c634..4af081b 100755 --- a/src/trexio_tools/trexio_run.py +++ b/src/trexio_tools/trexio_run.py @@ -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) @@ -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] @@ -30,24 +30,46 @@ 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: + # dummy check + if '*' in filename: + raise ValueError(f'TREXIO filename {filename} contains * symbol. Are you sure?') + # check that the file is actually TREXIO file + try: + is_trexio = False + with trexio.File(filename, 'r', trexio.TREXIO_AUTO) as tfile: + if trexio.has_metadata_package_version(tfile): + is_trexio = True + + if is_trexio: os.system(f'rm -rf -- {filename}') + + except: + raise Exception(f'Output file {filename} exists but it is not a TREXIO file. Are you sure?') + else: + raise Exception(f'Output file {filename} already exists but overwrite option is not provided. Consider using the `-w` CLI argument.') + + return + + +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": @@ -74,9 +96,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) @@ -109,7 +129,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"] )