Project

General

Profile

PythonExample ยป uns_demo.py

python unsio demo program - Jean-charles Lambert, 01/30/2014 07:57 PM

 
1
# -----------------------------------------------------------------------
2
# The following program shows how to use UNSIO library from
3
# a Python program
4
#
5
# This program reads an unsio compatible snapshot from the command line
6
# and save it in gadget2 format
7
#
8
# Syntaxe : uns_demo.py -i <inputfile> -o <outfile> -c <components> -t <times>
9
#
10
# inputfile  : UNS input snapshot
11
# outfile    : gadget2 out filename
12
# components : specify just one or a coma separeted list of components
13
#              among => disk,gas,stars,halo,bulge,bndry
14
#              example : -c disk,stars
15
# times      : selected time
16
#              for all time steps, use "-t all"
17
#              for one specific time, use "-t 10"
18
#              for a range of times, use "-t 5:6"
19
#
20
# -----------------------------------------------------------------------
21
# For more information about how to use UNSIO, visit:
22
# http://projets.lam.fr/projects/unsio/
23
# -----------------------------------------------------------------------
24
#  Copyright Jean-Charles LAMBERT (CeSAM)- 2008-2014
25
#
26
#  e-mail:   Jean-Charles.Lambert@lam.fr                                      
27
#  address:  Centre de donneeS Astrophysique de Marseille (CeSAM)         
28
#            Laboratoire d'Astrophysique de Marseille                          
29
#            Pole de l'Etoile, site de Chateau-Gombert                         
30
#            38, rue Frederic Joliot-Curie                                     
31
#            13388 Marseille cedex 13 France                                   
32
#            CNRS U.M.R 6110
33
# -----------------------------------------------------------------------
34

    
35
# unsio module loading
36
# ==> do not forget to update PYTHONPATH environment variable with
37
#     py_unsio location path
38
from py_unsio import *
39
import numpy as np
40
# cmd line
41
import sys, getopt
42

    
43
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44
# printAndSaveProp
45
# print properties for the component comp given as argument
46
# and save them
47
def printAndSaveProp(uns,unso,comp):
48
    info="""\
49
    ----------------------------------------------
50
    Component : [%s]
51
    ----------------------------------------------
52
    """
53
    print info % (comp)
54
    # return a 1D numpy data array with mass
55
    ok,mass=uns.getArrayF(comp,"mass")
56
    if ok:
57
        print "mass =",mass
58
        unso.setArrayF(comp,"mass",mass) # save mass
59
        
60
    # return a 1D numpy data array with pos
61
    ok,pos=uns.getArrayF(comp,"pos")
62
    if ok:
63
        print "pos =",pos
64
        unso.setArrayF(comp,"pos",pos) # save pos
65
        
66
    # return a 1D numpy data array with vel
67
    ok,vel=uns.getArrayF(comp,"vel")
68
    if ok:
69
        print "vel =",vel
70
        unso.setArrayF(comp,"vel",vel) # save vel
71

    
72
    # return a 1D numpy data array with rho
73
    ok,rho=uns.getArrayF(comp,"rho")
74
    if ok:
75
        print "rho =",rho
76
        unso.setArrayF(comp,"rho",rho) # save rho
77

    
78
    # return a 1D numpy data array with temperature
79
    ok,temp=uns.getArrayF(comp,"temp")
80
    if ok:
81
        print "temp =",temp
82
        unso.setArrayF(comp,"temp",temp) # save temperature
83

    
84
    # return a 1D numpy data array with hsml
85
    ok,hsml=uns.getArrayF(comp,"hsml")
86
    if ok:
87
        print "hsml =",hsml
88
        unso.setArrayF(comp,"hsml",hsml) # save hsml
89

    
90
    # return a 1D numpy data array with particles age
91
    ok,age=uns.getArrayF(comp,"age")
92
    if ok:
93
        print "age =",age
94
        unso.setArrayF(comp,"age",age) # save age
95

    
96
    # return a 1D numpy data array with mettalicity
97
    ok,metal=uns.getArrayF(comp,"metal")
98
    if ok:
99
        print "metal =",metal
100
        unso.setArrayF(comp,"metal",metal) # save mettalicity
101

    
102

    
103
    # return a 1D numy data array with id
104
    ok,indexes=uns.getArrayI(comp,"id")
105
    if ok:
106
        print "indexes =", indexes
107
        unso.setArrayI(comp,"id",indexes) # save id
108

    
109
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
110
# compute
111
# loop on all selected time steps and print out properties
112
# for every components from "comp" variable
113
# and save them in a gadegt2 file
114
def compute(file,out,comp,times):
115
    print "file=",file, " outfile=",out," comp=",comp, " times=",times
116

    
117
    # instantiate a CunsIn object, here we request to load "all" components
118
    uns=CunsIn(file,"all",times)
119

    
120
    # load frame
121
    cpt=0
122
    while (uns.nextFrame("")):  # load every snasphots
123

    
124
        nameout=out+".%05d"%cpt # create out filename
125
        print "Filename Out =",nameout
126
        # instantiate a CunsOut object in "gadget2" format 
127
        unso=CunsOut(nameout,"gadget2")
128

    
129
        ok,tsnap=uns.getValueF("time") # return snasphot time
130
        print "Snapshot time : ","%.03f"%tsnap
131
        unso.setValueF("time",tsnap) # save snapshot time
132
        
133
        # loop on all components stored in comp variable
134
        for onecomp in (comp.split(",")):
135
            printAndSaveProp(uns,unso,onecomp) # print properties for the component
136

    
137
        unso.save()  # save snasphot
138
        cpt+=1 # one more frame
139
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
140
# main
141
def main(argv):
142
    prog = argv.pop(0) # program name
143
    infile=''
144
    out=''
145
    comp='gas,disk,stars,halo,bulge,bndry'
146
    times='all'
147

    
148
    try:
149
        opts,args=getopt.getopt(argv,"hi:o:c:t:",["in=","out=","comp=","times="])
150

    
151
    except getopt.GetoptError:
152
        print "\nUnknown parameters, please check ....\n\n"
153
        printHelp(prog)
154
        sys.exit()
155
    for opt, arg in opts:
156
        if opt == '-h':
157
            printHelp(prog)
158
            sys.exit()
159
        elif opt in ("-i", "--in"):
160
            infile = arg
161
        elif opt in ("-o", "--out"):
162
            out = arg
163
        elif opt in ("-c", "--comp"):
164
            comp = arg
165
        elif opt in ("-t", "--times"):
166
            times = arg
167

    
168

    
169
    if (infile != '' and out != ''):
170
        compute(infile,out,comp,times)
171
    else:
172
        print "\nYou must provide input and output files.....\n\n"
173
        printHelp(prog)
174
        
175
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
176
# printHelp
177
def printHelp(prog):
178
    help= """\
179
    --------------------------------------------------
180
    Print out components properties of an UNS file
181
    and save them in a gadget2 file
182
    --------------------------------------------------
183
    
184
    Syntaxe : %s  -i <inputfile> -o <outfile> -c <components> -t <times>
185
    Example : %s  -i gtr118_1912 -o myfile.g2 -c gas,stars
186
    
187
    Notes :
188
        inputfile  : UNS input snapshot
189

    
190
        outfile    : gadget2 out filename
191
        
192
        components : specify just one or a coma separeted list of components
193
                     among => disk,gas,stars,halo,bulge,bndry
194
                     exemple : -c disk,stars
195
                     
196
        times      : selected time
197
                     for all time steps, use "-t all"
198
                     for one specific time, use "-t 10"
199
                     for a range of times, use "-t 5:6"
200

    
201
    """
202
    print help % (prog,prog)
203

    
204
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
205
# main
206
if __name__ == "__main__":
207
    main(sys.argv[0:])
    (1-1/1)