Source

IRATE-format / scripts / enbid2irate

Full commit
#!/usr/bin/env python
import sys
from optparse import OptionParser,OptionGroup
from irate.enbid import read_and_write_enbid_binary
from numpy import array

usage = "usage:  %prog <EnBiD binary file> <IRATE file> <snapshot number> [options]"
parser = OptionParser(usage=usage)

"""
cosmooptions = OptionGroup(parser,"Cosmology","Specify pieces of the cosmology that aren't included in the Gadget file header.")
cosmooptions.add_option("--s8","--sigma8",dest="sigma8",type="float",help="Optionally provide sigma_8 to store as an attribute to the Cosmology group.  Needed for a valid IRATE file, but only needs to be provided once for each file.  If the output file already contains a value for sigma_8 and it doesn't match the value provided here, the code will exit.",default=None)
cosmooptions.add_option("--ns","--n_s",dest="ns",type="float",help="Optionally provide n_s, the power spectrum tilt, to store as an attribute to the Cosmology group.  Needed for a valid IRATE file, but only needs to be provided once for each file.  If the output file already contains a value for n_s and it doesn't match the value provided here, the code will exit.",default=None)
cosmooptions.add_option("--oB","--omegaB",dest="omegaB",type="float",help="Optionally provide omega_B, present day baryon density, to store as an attribute to the Cosmology group.  Needed for a valid IRATE file, but only needs to be provided once for each file.  If the output file already contains a value for omega_B and it doesn't match the value provided here, the code will exit.",default=None)
"""

outoptions = OptionGroup(parser,"Output Options","Modify the format and organization of the output file.")
outoptions.add_option("--t0name","--gasname",dest="gasname",help="Name for group that gas particles are saved under.  Default = %default",default="Gas")
outoptions.add_option("--t1name","--haloname",dest="haloname",help="Name for group that particles in Gadget's halo group are saved under.  Default = %default",default="Dark_Halo")
outoptions.add_option("--t2name","--diskname",dest="diskname",help="Name for group that particles in Gadget's disk group are saved under.  Default = %default",default="Dark_Disk")
outoptions.add_option("--t3name","--bulgename",dest="bulgename",help="Name for group that particles in Gadget's bulge group will be saved under.  Default = %default",default="Dark_Bulge")
outoptions.add_option("--t4name","--starname",dest="starname",help="Name for group that particles in Gadget's star group will be saved under.  Default = %default",default="Star")
outoptions.add_option("--t5name","--bndryname",dest="bndryname",help="Name for group that particles in Gadget's boundary group will be saved under.  Default = %default",default="Dark_Boundary")
outoptions.add_option("--gname",dest="gname",help="Name given to the datasets that contain EnBiD data.  Default = %default",default="EnBiDDensity")

unitoptions = OptionGroup(parser,"Units","Change default values for length, mass, and velocity units.")
unitoptions.add_option("--ustring",dest="uname",help="Human readable string that specifies the units of length.  Default = %default",default="1e10 M_sun per comoving Mpc^3 h^2")
unitoptions.add_option("--unit",dest="unit",type="float",help="Factor to convert units to CGS.  Default = %default",default=6.767991e-31)
unitoptions.add_option("--hfact",dest="hfact",type="float",help="The exponent of the reduced Hubble Parameter, h, that appears in the units.  Default = %default (h inverse)",default=2)
unitoptions.add_option("--afact",dest="afact",type="float",help="The exponent of the scale factor that is required to convert into physical units.  Default = %default (comoving)",default=-1)

parser.add_option_group(unitoptions)
#parser.add_option_group(cosmooptions)
parser.add_option_group(outoptions)


(ops,args) = parser.parse_args()

if len(sys.argv) == 1:
    parser.print_help()
    sys.exit(1337)

if len(args) < 3:
    parser.print_help()
    sys.exit(1337)
elif len(args) == 3:
    inname,outname,snapnum = args
else:
    print "Unrecognized command line arguments:\n"
    for arg in args[3:]:    print arg

try:
    snapnum = int(snapnum)
except ValueError:
    print "Snapshot number must be an integer."
    sys.exit(1337)

ext = ".hdf5"
if not outname.endswith(ext):
    outname = outname+ext
    
units = array([ops.unit,ops.hfact,ops.afact])
    
#Check that the names are valid (i.e. not of length zero)
if len(ops.gasname) == 0 or len(ops.haloname) == 0 or len(ops.diskname) == 0 or len(ops.bulgename) == 0 or len(ops.starname) == 0 or len(ops.bndryname) == 0:
    print "Sorry, names for groups cannot be length zero.  Please try again."
    sys.exit(1337)
    
#Check that there are no spaces in the names
if " " in ops.gasname or " " in ops.haloname or " " in ops.diskname or " " in ops.bulgename or " " in ops.starname or " " in ops.bndryname:
    print "Sorry, names for groups cannot contain spaces."
    sys.exit(1337)
    
for n in [ops.gasname,ops.haloname,ops.diskname,ops.bulgename,ops.starname,ops.bndryname]:
    if not n.startswith('Gas') and not n.startswith('Dark') and not n.startswith('Star'):
        print "Sorry, names for groups must start with either 'Gas', 'Dark', or 'Star'."

try:
    read_and_write_enbid_binary(inname,outname,snapnum,ops.gname,
    ops.gasname,ops.haloname,ops.diskname,ops.bulgename,ops.starname,ops.bndryname,
    ops.uname,units)
except Exception,e:
    print e.__class__.__name__+':',e
    sys.exit(1337)