Commit fcbd3183 authored by jclamber's avatar jclamber

unsio demo python program

git-svn-id: http://svn.oamp.fr/repos/unsio/trunk@178 ce2cc22f-6084-46ce-a062-084b172ee5dc
parent 3b9863b3
# -----------------------------------------------------------------------
# The following program shows how to use UNSIO library from
# a Python program
#
# This program reads an unsio compatible snapshot from the command line
# and save it in gadget2 format
#
# Syntaxe : unsio_fortran myinput myoutput select_comp select_time
#
# myinput -> an unsio compatible input snapshot
# myoutput -> output filename
# select_comp -> the component to be saved
# select_time -> a range of time which select input snapshot
#
# -----------------------------------------------------------------------
# For more information about how to use UNSIO, visit:
# http://projets.lam.fr/projects/unsio/
# -----------------------------------------------------------------------
# Copyright : Jean-Charles Lambert
# -----------------------------------------------------------------------
# unsio module (do not forget to update PYTHONPATH environment variable with
# py_unsio location path)
from py_unsio import *
import numpy as np
# cmd line
import sys, getopt
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# printProp
# print properties for the component comp given as argument
def printProp(uns,comp):
info="""\
----------------------------------------------
Component : [%s]
----------------------------------------------
"""
print info % (comp)
# return a 1D numpy data array with mass
ok,data=uns.getArrayF(comp,"mass")
if ok:
print "mass =",data
# return a 1D numpy data array with pos
ok,pos=uns.getArrayF(comp,"pos")
if ok:
print "pos =",pos
# return a 1D numpy data array with vel
ok,vel=uns.getArrayF(comp,"vel")
if ok:
print "vel =",vel
# return a 1D numpy data array with rho
ok,rho=uns.getArrayF(comp,"rho")
if ok:
print "rho =",rho
# return a 1D numpy data array with temperature
ok,temp=uns.getArrayF(comp,"temp")
if ok:
print "temp =",temp
# return a 1D numpy data array with hsml
ok,hsml=uns.getArrayF(comp,"hsml")
if ok:
print "hsml =",hsml
# return a 1D numpy data array with particles age
ok,age=uns.getArrayF(comp,"age")
if ok:
print "age =",age
# return a 1D numpy data array with mettalicity
ok,metal=uns.getArrayF(comp,"metal")
if ok:
print "metal =",metal
# return a 1D numy data array with id
ok,indexes=uns.getArrayI(comp,"id")
if ok:
print "indexes =", indexes
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# compute
# loop on all selected time steps and print out properties
# for every components from "comp" variable
def compute(file,comp,times):
print "file=",file," comp=",comp, " times=",times
# instantiate a CunsIn object, here we request to load "all" components
uns=CunsIn(file,"all",times);
# load frame
while (uns.nextFrame("")): # load snasphot
ok,tsnap=uns.getValueF("time") # return snasphot time
print "Snapshot time : ","%.03f"%tsnap
# loop on all components stored in comp variable
for onecomp in (comp.split(",")):
printProp(uns,onecomp) # print properties for the component
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# main
def main(argv):
prog = argv.pop(0) # program name
file=''
comp='gas,disk,stars,halo,bulge,bndry'
times='all'
try:
opts,args=getopt.getopt(argv,"hi:c:t:",["in=","comp=","times="])
except getopt.GetoptError:
print "\nUnknown parameters, please check ....\n\n"
printHelp(prog)
sys.exit()
for opt, arg in opts:
if opt == '-h':
printHelp(prog)
sys.exit()
elif opt in ("-i", "--in"):
file = arg
elif opt in ("-c", "--comp"):
comp = arg
elif opt in ("-t", "--times"):
times = arg
if (file != ''):
compute(file,comp,times)
else:
printHelp(prog)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# printHelp
def printHelp(prog):
help= """\
--------------------------------------------------
Print out properties for components of an UNS file
--------------------------------------------------
Syntaxe : %s -i <inputfile> -c <components> -t <times>
Notes :
inputfile : UNS input snapshot
components : specify just one or a coma separeted list of components
among => disk,gas,stars,halo,bulge,bndry
exemple : -c disk,stars
times : selected time
can be "-t all" (for all time steps)
one specific time "-t 10"
a range of times "-t 5:6"
"""
print help % (prog)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# main
if __name__ == "__main__":
main(sys.argv[0:])
# unsio
# -----------------------------------------------------------------------
# The following program shows how to use UNSIO library from
# a Python program
#
# This program reads an unsio compatible snapshot from the command line
# and save it in gadget2 format
#
# Syntaxe : unsio_fortran myinput myoutput select_comp select_time
#
# myinput -> an unsio compatible input snapshot
# myoutput -> output filename
# select_comp -> the component to be saved
# select_time -> a range of time which select input snapshot
#
# -----------------------------------------------------------------------
# For more information about how to use UNSIO, visit:
# http://projets.lam.fr/projects/unsio/
# -----------------------------------------------------------------------
# Copyright : Jean-Charles Lambert
# -----------------------------------------------------------------------
# unsio module (do not forget to update PYTHONPATH environment variable with
# py_unsio location path)
from py_unsio import *
import numpy as np
# cnd line
# cmd line
import sys, getopt
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# printProp
# print properties for the component comp given as argument
def printProp(uns,comp):
info="""\
----------------------------------------------
Component : [%s]
----------------------------------------------
"""
print info % (comp)
# return a 1D numpy data array with mass
ok,data=uns.getArrayF(comp,"mass")
if ok:
print "mass =",data
# return a 1D numpy data array with pos
ok,pos=uns.getArrayF(comp,"pos")
if ok:
print "pos =",pos
# return a 1D numpy data array with vel
ok,vel=uns.getArrayF(comp,"vel")
if ok:
print "vel =",vel
# return a 1D numpy data array with rho
ok,rho=uns.getArrayF(comp,"rho")
if ok:
print "rho =",rho
# return a 1D numpy data array with temperature
ok,temp=uns.getArrayF(comp,"temp")
if ok:
print "temp =",temp
# return a 1D numpy data array with hsml
ok,hsml=uns.getArrayF(comp,"hsml")
if ok:
print "hsml =",hsml
# return a 1D numpy data array with particles age
ok,age=uns.getArrayF(comp,"age")
if ok:
print "age =",age
# return a 1D numpy data array with mettalicity
ok,metal=uns.getArrayF(comp,"metal")
if ok:
print "metal =",metal
# return a 1D numy data array with id
ok,indexes=uns.getArrayI(comp,"id")
if ok:
print "indexes =", indexes
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# compute
# loop on all selected time steps and print out properties
# for every components from "comp" variable
def compute(file,comp,times):
print "file=",file," comp=",comp, " times=",times
# instantiate a CunsIn object, here we request to load "all" components
uns=CunsIn(file,"all",times);
# load frame
while (uns.nextFrame("")): # load snasphot
ok,tsnap=uns.getValueF("time") # return snasphot time
print "Snapshot time : ","%.03f"%tsnap
# loop on all components stored in comp variable
for onecomp in (comp.split(",")):
printProp(uns,onecomp) # print properties for the component
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# main
def main(argv):
prog = argv.pop(0) # program name
file=''
comp=''
prop=''
comp='gas,disk,stars,halo,bulge,bndry'
times='all'
try:
opts,args=getopt.getopt(argv,"hi:c:p:",["in=","comp=","prop="])
opts,args=getopt.getopt(argv,"hi:c:t:",["in=","comp=","times="])
except getopt.GetoptError:
print prog,' -i <inputfile> -c <component> -p <properties>'
sys.exit(2)
print "\nUnknown parameters, please check ....\n\n"
printHelp(prog)
sys.exit()
for opt, arg in opts:
if opt == '-h':
print prog,' -i <inputfile> -c <component> -p <properties>'
printHelp(prog)
sys.exit()
elif opt in ("-i", "--in"):
file = arg
elif opt in ("-c", "--comp"):
comp = arg
elif opt in ("-p", "--prop"):
prop = arg
elif opt in ("-t", "--times"):
times = arg
compute(file,comp,prop)
def compute(file,comp,prop):
print "file=",file," comp=",comp," prop=",prop
time="all"
uns=CunsIn(file,comp,time);
# load frame
ok=uns.nextFrame("")
print ok
# get data
ok,data=uns.getArrayF(comp,prop)
print ok
print data
if (prop=="pos" or prop=="vel" or prop=="acc"):
# reshap 1D to 2D
data=np.reshape(data,(-1,3))
if (file != ''):
compute(file,comp,times)
else:
printHelp(prog)
# get col 1 to x
x = data[:,0]
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# printHelp
def printHelp(prog):
help= """\
--------------------------------------------------
Print out properties for components of an UNS file
--------------------------------------------------
Syntaxe : %s -i <inputfile> -c <components> -t <times>
Notes :
inputfile : UNS input snapshot
# get col 2 to y
y = data[:,1]
components : specify just one or a coma separeted list of components
among => disk,gas,stars,halo,bulge,bndry
exemple : -c disk,stars
times : selected time
can be "-t all" (for all time steps)
one specific time "-t 10"
a range of times "-t 5:6"
"""
print help % (prog)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# main
if __name__ == "__main__":
main(sys.argv[0:])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment