IRATE-format / scripts / gb2irate

#!/usr/bin/env python
import sys
from optparse import OptionParser
from irate.gadgetbinary import gbin2irate
from irate.gadgettype2binary import gb2_to_irate

usage = "usage:  %prog input-file [output-file (defaults to input-file+'-irate.hdf5')] [options]"
parser = OptionParser(usage=usage)

parser.add_option("-2","--type2",action="store_true",dest="type2",help="Run with this option if the file you're converting is a type 2 Gadget binary, i.e. if it has strings that identify each block.  Note that attempting to convert a type 1 file with this on will fail, as will attempting to convert a type 2 file with this off.")
parser.add_option("-d",action="store_true",dest="dir",help="Specifies that the given output argument is the output directory, in which case the file is saved in that directory with the default name.  Default = %default",default=False)
parser.add_option("-i","--inits",action="store_true",dest="ics",help="Specifies that the input file is an initial conditions file, in which case the gas densities and smoothing lengths are assumed to be absent.  Note that if this is incorrectly turned on and there is gas present, the code will fail.  Default = %default.",default=False)
parser.add_option("-p","--potential",action="store_true",dest="potential",help="Specifies that gravitational potential is included in the Gadget binary file (enabled in the Makefile).  Default = %default",default=False)
parser.add_option("-a","--acceleartion",action="store_true",dest="accel",help="Specifies that acceleration is included in the Gadget binary file (enabled in the Makefile).  Default = %default",default=False)
parser.add_option("-s","--entropy",action="store_true",dest="entropy",help="Specifies that the rate of change of entropy of gas particles is included in the Gadget binary file (enabled in the Makefile).  Default = %default",default=False)
parser.add_option("-t","--timestep",action="store_true",dest="timestep",help="Specifies that the timestep of each particle is included in the Gadget binary file (enabled in the Makefile).  Default = %default",default=False)

parser.add_option("--t0tree","--gastree",dest="gastree",help="Tree that gas particles will be saved in. Default = %default",default="gas")
parser.add_option("--gasname",dest="gasname",help="Name for group that gas particles are saved in under the gas tree.  Default = %default",default="Gas")
parser.add_option("--t1tree","--halotree",dest="halotree",help="Tree that the particles in Gadget's halo group will be saved in.  Must be either gas, dark, or star.  Default = %default.",default="dark")
parser.add_option("--t1name","--haloname",dest="haloname",help="Name for group that particles in Gadget's halo group are saved under.  Default = %default",default="Halo")
parser.add_option("--t2tree","--disktree",dest="disktree",help="Tree that the particles in Gadget's disk group will be saved in.  Must be either gas, dark, or star.  Default = %default.",default="dark")
parser.add_option("--t2name","--diskname",dest="diskname",help="Name for group that particles in Gadget's disk group are saved under.  Default = %default",default="Disk")
parser.add_option("--t3tree","--bulgetree",dest="bulgetree",help="Tree that the particles in Gadget's bulge group will be saved in.  Must be either gas, dark, or star.  Default = %default.",default="dark")
parser.add_option("--t3name","--bulgename",dest="bulgename",help="Name for group that particles in Gadget's bulge group will be saved under.  Default = %default",default="Bulge")
parser.add_option("--t4tree","--startree",dest="startree",help="Tree that particles in Gadget's star group are saved in.  Must be either gas, dark, or star.  Default = %default.",default="star")
parser.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")
parser.add_option("--t5tree","--bndrytree",dest="bndrytree",help="Tree that the particles in Gadget's boundary group will be saved in.  Must be either gas, dark, or star.  Default = %default.",default="dark")
parser.add_option("--t5name","--bndryname",dest="bndryname",help="Name for group that particles in Gadget's boundary group will be saved under.  Default = %default",default="Boundary")

(ops,args) = parser.parse_args()

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

if len(args) == 1:
    inname = args[0]
    outname = inname+"-irate.hdf5"    #Default to inname + .hdf5 if no outname supplied
elif len(args) == 2:
    if ops.dir:
        inname = args[0]
        if not args[1].endswith('/'):   args[1] = args[1] + '/'
        outname = args[1] + inname.split('/')[-1]+"-irate.hdf5"
    else:
        inname,outname = args
else:
    parser.print_help()
    print "Input file must be specified."
    sys.exit(1337)

ext = ".hdf5"
if not outname.endswith(ext):
    outname = outname+ext

trees = ["gas","dark","star"]   #Valid trees

#Check that all the tree options inputed are valid options
if ops.gastree not in trees:
    print "Sorry, your choice for gas tree is invalid.  It must be gas, dark, or star.  Please try again."
    sys.exit(1337)
    
if ops.halotree not in trees:
    print "Sorry, your choice for halo tree is invalid.  It must be gas, dark, or star.  Please try again."
    sys.exit(1337)
    
if ops.disktree not in trees:
    print "Sorry, your choice for disk tree is invalid.  It must be gas, dark, or star.  Please try again."
    sys.exit(1337)
    
if ops.bulgetree not in trees:
    print "Sorry, your choice for bulge tree is invalid.  It must be gas, dark, or star.  Please try again."
    sys.exit(1337)
    
if ops.startree not in trees:
    print "Sorry, your choice for star tree is invalid.  It must be gas, dark, or star.  Please try again."
    sys.exit(1337)
    
if ops.bndrytree not in trees:
    print "Sorry, your choice for boundary tree is invalid.  Please try again."
    sys.exit(1337)
    
#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)

if ops.type2:
    gb2_to_irate(inname,outname,ops.gastree,ops.gasname,ops.halotree,ops.haloname,ops.disktree,
    ops.diskname,ops.bulgetree,ops.bulgename,ops.startree,ops.starname,ops.bndrytree,ops.bndryname)

else:
    gbin2irate(inname,outname,ops.potential,ops.accel,ops.entropy,ops.timestep,
    ops.gastree,ops.gasname,ops.halotree,ops.haloname,ops.disktree,ops.diskname,
    ops.bulgetree,ops.bulgename,ops.startree,ops.starname,ops.bndrytree,ops.bndryname,ops.ics)
    

    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.