Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tool for converting gadget3 ICs to EAGLE ICs in HDF format #104

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

j-davies-astro
Copy link

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.

from scipy.io import FortranFile
import h5py as h5

"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is common to put the docstring at the very beginning of the script file rather than after the imports.

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
'input_ICs',
type=str,
Copy link
Collaborator

Choose a reason for hiding this comment

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

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.

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')
Copy link
Collaborator

Choose a reason for hiding this comment

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

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')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants