Add tool for converting gadget3 ICs to EAGLE ICs in HDF format#104
Open
j-davies-astro wants to merge 6 commits intopynbody:masterfrom
Open
Add tool for converting gadget3 ICs to EAGLE ICs in HDF format#104j-davies-astro wants to merge 6 commits intopynbody:masterfrom
j-davies-astro wants to merge 6 commits intopynbody:masterfrom
Conversation
cphyc
reviewed
Mar 8, 2022
| from scipy.io import FortranFile | ||
| import h5py as h5 | ||
|
|
||
| """ |
Collaborator
There was a problem hiding this comment.
It is common to put the docstring at the very beginning of the script file rather than after the imports.
tools/gadget3_to_eagleHDF.py
Outdated
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument( | ||
| 'input_ICs', | ||
| type=str, |
Collaborator
There was a problem hiding this comment.
There is actually a special argument file type to handle files
Suggested change
| type=str, | |
| type=argparse.FileType(mode="br"), |
Note that you need to open it in binary format for FortranFile to be happy.
tools/gadget3_to_eagleHDF.py
Outdated
Comment on lines
16
to
29
| def ics2hdf(ics_file): | ||
|
|
||
| raw_ics = os.path.abspath(ics_file) | ||
|
|
||
| # Do quick checks to make sure input is valid | ||
| assert os.path.isfile(raw_ics),'Input ICs file does not exist.' | ||
| assert raw_ics.split('.')[-1] == 'gadget3','Input ICs not valid. Please input GADGET3 binary ICs (.gadget3)' | ||
|
|
||
| out_ics = raw_ics[:-7] + 'hdf5' | ||
|
|
||
| print('Reading binary file '+raw_ics) | ||
|
|
||
| # Open the FORTRAN unformatted binary ICs | ||
| f = FortranFile(raw_ics, 'r') |
Collaborator
There was a problem hiding this comment.
Assuming you use the trick bellow, this'll have to be changed into
Suggested change
| def ics2hdf(ics_file): | |
| raw_ics = os.path.abspath(ics_file) | |
| # Do quick checks to make sure input is valid | |
| assert os.path.isfile(raw_ics),'Input ICs file does not exist.' | |
| assert raw_ics.split('.')[-1] == 'gadget3','Input ICs not valid. Please input GADGET3 binary ICs (.gadget3)' | |
| out_ics = raw_ics[:-7] + 'hdf5' | |
| print('Reading binary file '+raw_ics) | |
| # Open the FORTRAN unformatted binary ICs | |
| f = FortranFile(raw_ics, 'r') | |
| def ics2hdf(ics_file): | |
| assert ics_file.name.split('.')[-1] == 'gadget3', 'Input ICs not valid. Please input GADGET3 binary ICs (.gadget3)' | |
| out_ics = raw_ics.name[:-7] + "hdf5" | |
| print(f"Reading binary file {raw_ics}") | |
| # Open the FORTRAN unformatted binary ICs | |
| f = FortranFile(ics_file, 'r') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I've been using this script to convert gadget3 ICs (as outputted by genetIC) to EAGLE-compatible HDF5 ICs for a long while now, and I thought it might be useful to include it in the tools here. I was spurred on to do this because Gandhali may need to write something similar, or adapt this script, to create ICs compatible with IllustrisTNG.