-
Notifications
You must be signed in to change notification settings - Fork 8
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
j-davies-astro
wants to merge
6
commits into
pynbody:master
Choose a base branch
from
j-davies-astro:gadget3_to_eagle
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e969889
Add tool for converting gadget3 ICs to EAGLE ICs in HDF format
76ab465
Add tool for converting gadget3 ICs to EAGLE ICs in HDF format
j-davies-astro b3674d4
Merge branch 'gadget3_to_eagle' of github.com:j-davies-astro/genetIC …
aff87b7
IO improvements
j-davies-astro 690f79b
Merge branch 'pynbody:master' into gadget3_to_eagle
j-davies-astro 5034c11
Merge branch 'pynbody:master' into gadget3_to_eagle
j-davies-astro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
""" | ||
Script for converting .gadget3 binary ICs to the hdf5 format, for use with the EAGLE code. | ||
|
||
Adapted from an IDL script by Rob Crain, by Jonathan Davies, 2021. | ||
|
||
Usage: ics2hdf.py ics_file | ||
* Creates an hdf5 file in the same directory as the original ICs, with the same name. | ||
""" | ||
|
||
import numpy as np | ||
import os | ||
import argparse | ||
from scipy.io import FortranFile | ||
import h5py as h5 | ||
|
||
def ics2hdf(ics_file): | ||
|
||
# Do quick check to make sure input is valid | ||
assert ics_file.name.split('.')[-1] == 'gadget3','Input ICs not valid. Please input GADGET3 binary ICs (.gadget3)' | ||
|
||
out_ics = ics_file.name[:-7] + 'hdf5' | ||
|
||
print(f"Reading binary file {ics_file.name}") | ||
|
||
# Open the FORTRAN unformatted binary ICs | ||
f = FortranFile(ics_file, 'r') | ||
|
||
# Read the header with the proper types | ||
header = f.read_record('(6,)i4','(6,)f8','f8','f8','i4','i4','(6,)i4','i4','i4','f8','f8','f8','f8','i4','i4','(6,)i4','i4','i4','(56,)b') | ||
|
||
hdict = {} | ||
hdict['NumPart_ThisFile'] = header[0] | ||
hdict['MassTable'] = header[1] | ||
hdict['ExpansionFactor'] = header[2][0] | ||
hdict['Time'] = header[2][0] | ||
hdict['Redshift'] = header[3][0] | ||
hdict['Flag_Sfr'] = header[4][0] | ||
hdict['Flag_Feedback'] = header[5][0] | ||
hdict['NumPart_Total'] = header[6] | ||
hdict['Flag_Cooling'] = header[7][0] | ||
hdict['NumFilesPerSnapshot'] = header[8][0] | ||
hdict['BoxSize'] = header[9][0] | ||
hdict['Omega0'] = header[10][0] | ||
hdict['OmegaLambda'] = header[11][0] | ||
hdict['HubbleParam'] = header[12][0] | ||
hdict['Flag_StellarAge'] = header[13][0] | ||
hdict['Flag_Metals'] = header[14][0] | ||
hdict['NumPart_Total_HighWord'] = header[15] | ||
hdict['Flag_DoublePrecision'] = header[17][0] | ||
|
||
# Correction to header (from Rob Crain's original IDL script): | ||
hdict['Flag_DoublePrecision'] = np.int32(0) | ||
|
||
# Pointer to this dict element for later convenience | ||
npart = hdict['NumPart_ThisFile'] | ||
|
||
# Get the particle array lengths | ||
tot_npart = npart[1] + npart[2] | ||
|
||
# Load the particles | ||
pos = f.read_record('(%d,3)f4'%(tot_npart)) | ||
vel = f.read_record('(%d,3)f4'%(tot_npart)) | ||
ids = f.read_record('(%d,)i8'%(tot_npart)) | ||
|
||
|
||
# Write out the ICs in hdf5 format | ||
|
||
print('Writing hdf5 file to '+out_ics) | ||
|
||
out = h5.File(out_ics,'w') | ||
|
||
print('Writing header...') | ||
|
||
out_header = out.create_group('Header') | ||
|
||
for key in hdict.keys(): | ||
out_header.attrs.create(key,data=hdict[key]) | ||
|
||
offset = 0 | ||
for ptype in range(len(npart)): | ||
|
||
num = npart[ptype] | ||
if num == 0: | ||
continue | ||
|
||
print('Writing PartType'+str(ptype)+'...') | ||
|
||
out_pgroup = out.create_group('PartType%i'%(ptype)) | ||
|
||
out_pgroup.create_dataset('Coordinates',data=pos[offset:offset+num,:]) | ||
out_pgroup.create_dataset('Velocity',data=vel[offset:offset+num,:]) | ||
out_pgroup.create_dataset('ParticleIDs',data=ids[offset:offset+num]) | ||
|
||
offset += num | ||
|
||
print('Done!') | ||
|
||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description=__doc__) | ||
parser.add_argument( | ||
'input_ICs', | ||
type=argparse.FileType(mode="br"), | ||
help="The input .gadget3 binary ICs file" | ||
) | ||
args = parser.parse_args() | ||
ics2hdf(args.input_ICs) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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.
There was a problem hiding this comment.
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.