Commit 96bc836d authored by LAMBERT Jean-charles's avatar LAMBERT Jean-charles

many updates

parent a0e8527e
from __future__ import print_function from __future__ import print_function
from unsio.py_unsio import *
try: try:
from unsio.py_unsio import * from unsio.py_unsio import *
except: except:
......
This diff is collapsed.
#!/usr/bin/env python
from __future__ import print_function
import numpy as np
import unsio
#
# class CSsnapshot
#
class CUNS_IN:
"""Input Operations on UNS snapshots"""
__uns=None
__verbose=False
__float32=True
__loaded=False
simname=None
select=None
#
# constructor
#
def __init__(self,simname,select="all",times="all",float32=True,verbose=False,verbose_debug=False):
#select=select.encode('ascii')
#times=times.encode('ascii')
self.__verbose=verbose
self.simname=simname
self.select=select
self.__vdebug=verbose_debug
if self.__vdebug:
print(">> 32 bits floating value",float32)
if simname is not None:
#self.simname=self.simname.encode('ascii')
if float32:
if self.__vdebug:
print("32 bits",self.simname,self.select,times,verbose,type(self.simname),type(self.select))
self.__uns=unsio.CunsIn(self.simname,self.select,times,verbose)
else:
if self.__vdebug:
print("64 bits",simname,select,times,verbose)
self.__uns=unsio.CunsInD(self.simname,self.select,times,verbose)
if not self.__uns.isValid():
raise RuntimeError("UNS not valid")
else:
None
def debugOn(self):
self.__vdebug=True
#
# nextFrame
#
def nextFrame(self,bits=""):
"""
Load the next snapshot. Data retreival depend from bits (array selected) and
components selected during instanciation
Argument :
for bits explanation, please visit :
https://projets.lam.fr/projects/unsio/wiki/UnsioVariablesUtilisation#How-to-select-data-at-loading
Return :
1 if success, 0 otherwise
"""
if not self.__uns.isValid():
raise RuntimeError("UNS not valid")
else:
ok=self.__uns.nextFrame(bits)
if ok:
self.__loaded=True
return ok
#
# close
#
def close(self):
if not self.__uns.isValid():
raise RuntimeError("UNS not valid")
self.__uns.close()
#
# kill
#
def kill(self):
if not self.__uns.isValid():
raise RuntimeError("UNS not valid")
self.close()
#
# getInterfaceType
#
def getInterfaceType(self):
"""
return uns snapshot's Interface type
"""
if not self.__uns.isValid():
raise RuntimeError("UNS not valid")
return self.__uns.getInterfaceType()
#
# getFileStructure
#
def getFileStructure(self):
"""
return uns snapshot's File structure
"""
if not self.__uns.isValid():
raise RuntimeError("UNS not valid")
return self.__uns.getFileStructure()
#
# getFileName
#
def getFileName(self):
"""
return uns snapshot's File name
"""
if not self.__uns.isValid():
raise RuntimeError("UNS not valid")
return self.__uns.getFileName()
#
# getData
#
def getData(self,select,tag=None,data_type=np.float32):
"""
General method to get Data from uns snapshot
Argument:
- select : component or list of components (ex: gas or gas,stars)
- tag : pos,vel,mass,rho...etc see https://projets.lam.fr/projects/unsio/wiki/GetDataDescription
IMPORTANT : if 'tag' is None, then 'select' is treated as 'tag' (see above)
Return :
status,numpy_array (if tag is not None)
status,value (if tag is None)
in both case, status=1 if success, 0 otherwise
"""
if not self.__uns.isValid():
raise RuntimeError("UNS not valid")
if not self.__loaded:
raise RuntimeError("Snapshot not loaded")
status=1
if tag != "id":
ret_data=np.zeros(0,dtype=data_type) # initalyse an empty array
else:
ret_data=np.zeros(0,dtype=np.int32) # initalyse an empty array
for comp in select.split(","):
if self.__vdebug:
print ("comp :", comp)
ok,type,data=self.__getData(comp,tag,data_type)
if (ok) :
if type==2: # array
ret_data = np.append(ret_data,data)
else:
ret_data = data
else:
status=0 # one component missing
if self.__vdebug and type==2:
print ("ok=",ok," data=",data.size," ret_data=",ret_data.size)
return status,ret_data
#
# __getData
#
def __getData(self,comp_value,tag=None,data_type=np.float32):
if self.__vdebug:
print ("array=",tag,data_type)
if tag is None: # only one value
tab_value_F=["time"]
tab_value_I=["nbody","nsel"]
ok=0
ret_data=np.zeros(1,dtype=data_type)
data=0
# proceed __uns.getValue()
if tab_value_F.count(comp_value) > 0:
ok,ret_data[0]=self.__uns.getValueF(comp_value)
data=float(ret_data[0])
else:
if tab_value_I.count(comp_value) > 0:
ok,ret_data[0]=self.__uns.getValueI(comp_value)
data=int(ret_data[0])
#ret_data[0]=data
if self.__vdebug:
print ("ok,data",ok,data)
return ok,1,data
else:
if tag != "id":
ok,data=self.__uns.getArrayF(comp_value,tag)
else:
ok,data=self.__uns.getArrayI(comp_value,tag)
return ok,2,data
#!/usr/bin/env python
from __future__ import print_function
import numpy as np
import unsio
#
# class CSsnapshot
#
class CUNS_OUT:
"""Output Operations on UNS snapshots"""
__uns=None
__verbose=False
__verbose_debug=False
__float32=True
__loaded=False
__outfile=None
__uns_type=None
select=None
__is_valid=False
__data_type=None
# -----------------------------------------------------
# constructor
#
def __init__(self,outfile, uns_type, float32=True, verbose=False, verbose_debug=False):
"""
"""
self.__float32 = float32
self.__outfile = outfile
self.__uns_type = uns_type
self.__verbose = verbose
self.__verbose_debug = verbose_debug
if self.__float32:
if self.__verbose_debug:
print("saving in SINGLE precision floating values")
self.__data_type = np.float32
self.__uns = unsio.CunsOut(self.__outfile,self.__uns_type)
else:
if self.__verbose_debug:
print("saving in DOUBLE precision floating values")
self.__data_type = np.float64
self.__uns = unsio.CunsOutD(self.__outfile,self.__uns_type)
self.__is_valid = True
# -----------------------------------------------------
# setData
#
def setData(self,data_array,select,tag=None):
"""
General method to set Data to uns snapshot
Argument:
- data_array : 1D numpy_array or value to save
- select : component or list of components (ex: gas or gas,stars)
- tag : pos,vel,mass,rho...etc see https://projets.lam.fr/projects/unsio/wiki/GetDataDescription
IMPORTANT : if 'tag' is None, then 'select' is treated as 'tag' (see above)
Return :
status : 1 if success, 0 otherwise
"""
if not self.__is_valid:
raise RuntimeError("CUNS_OUT is not valid")
if self.__verbose_debug:
print("setData : [select=%s, tag=%s]"%(select,tag))
ok=0
if tag is None: # only one value
tab_value_F=["time"]
tab_value_I=["nbody","nsel"]
if tab_value_F.count(select) > 0:
ok=self.__uns.setValueF(select,data_array)
else:
if tab_value_I.count(select) > 0:
ok=self.__uns.setValueI(select,data_array)
data=int(ret_data[0])
else:
print("Unknown select[%s] ..."%(select))
else:
if tag != "id": #not id, then we save float arrays
ok = self.__uns.setArrayF(select,tag,data_array)
else: # save id's array of integers
ok = self.__uns.setArrayI(select,tag,data_array)
return ok
# -----------------------------------------------------
# save
#
def save(self):
if not self.__is_valid:
raise RuntimeError("CUNS_OUT is not valid")
self.__uns.save()
# -----------------------------------------------------
# close
#
def close(self):
if not self.__is_valid:
raise RuntimeError("CUNS_OUT is not valid")
self.__uns.close()
...@@ -7,7 +7,8 @@ import numpy as np ...@@ -7,7 +7,8 @@ import numpy as np
import tempfile import tempfile
import unsio import unsio
from unsio.csnapshot import * from unsio.input import *
from unsio.output import *
from .check_platform import * from .check_platform import *
import subprocess import subprocess
...@@ -31,15 +32,16 @@ class CTestunsio: ...@@ -31,15 +32,16 @@ class CTestunsio:
__fdout = None __fdout = None
__out = None __out = None
__singke = None
# ----------------------------------------------------- # -----------------------------------------------------
# #
def __init__(self,nbody=None,seed=666,single=True,verbose=None, uns2uns=False, out=None): def __init__(self,nbody=None,seed=666,single=True,verbose=None, uns2uns=False, out=None):
self.__nbody = nbody self.__nbody = nbody
self.__verbose = verbose self.__verbose = verbose
self.__seed = seed self.__seed = seed
self.__single = single self.__single = single
self.__uns2uns = uns2uns self.__uns2uns = uns2uns
self.__out = out self.__out = out
if out is not None: if out is not None:
try: try:
...@@ -53,15 +55,15 @@ class CTestunsio: ...@@ -53,15 +55,15 @@ class CTestunsio:
print("library version [%s]"%(unsio.getVersion()),file=self.__fdout) print("library version [%s]"%(unsio.getVersion()),file=self.__fdout)
print("python-unsio version [%s]"%(unsio.__version__),file=self.__fdout) print("python-unsio version [%s]"%(unsio.__version__),file=self.__fdout)
print("-----------------------------------------------------------",file=self.__fdout) print("-----------------------------------------------------------",file=self.__fdout)
# detect plateform # detect plateform
info=CPlateform(fdout=self.__fdout) info=CPlateform(fdout=self.__fdout)
info.display() info.display()
self.__initSeed(self.__seed) self.__initSeed(self.__seed)
# ----------------------------------------------------- # -----------------------------------------------------
# #
def __initSeed(self,seed=None): def __initSeed(self,seed=None):
if seed is None: if seed is None:
seed=self.__seed seed=self.__seed
...@@ -69,7 +71,7 @@ class CTestunsio: ...@@ -69,7 +71,7 @@ class CTestunsio:
np.random.seed(seed) np.random.seed(seed)
# ----------------------------------------------------- # -----------------------------------------------------
# #
def __dataF(self,n): def __dataF(self,n):
if (self.__single): if (self.__single):
x=np.float32(np.random.sample(n)) x=np.float32(np.random.sample(n))
...@@ -77,17 +79,19 @@ class CTestunsio: ...@@ -77,17 +79,19 @@ class CTestunsio:
x=np.float64(np.random.sample(n)) x=np.float64(np.random.sample(n))
return x return x
# ----------------------------------------------------- # -----------------------------------------------------
# #
def __saveArray(self,comp,attr,dim,real=True): def __saveArray(self,comp,attr,dim,real=True):
if real: if real:
data=self.__dataF(self.__nbody*dim) data=self.__dataF(self.__nbody*dim)
ok=self.__unso.setArrayF(comp,attr,data) # save real arrays # ok=self.__unso.setArrayF(comp,attr,data) # save real arrays
ok = self.__unso.setData(data,comp,attr)
else: else:
ok=self.__unso.setArrayI(comp,attr,np.arange(self.__nbody*dim,dtype=np.int32)) # save real arrays # ok=self.__unso.setArrayI(comp,attr,np.arange(self.__nbody*dim,dtype=np.int32)) # save real arrays
ok = self.__unso.setData(np.arange(self.__nbody*dim,dtype=np.int32),comp,attr)
# ----------------------------------------------------- # -----------------------------------------------------
# #
def saveModel(self,filename=None,unstype="gadget3",single=True): def saveModel(self,filename=None,unstype="gadget3",single=True):
""" """
save model in requested format save model in requested format
...@@ -103,22 +107,21 @@ class CTestunsio: ...@@ -103,22 +107,21 @@ class CTestunsio:
self.__model_file = filename self.__model_file = filename
fff = self.__model_file fff = self.__model_file
## SAVE FILE
# instantiate output object
if (single): if (single):
print("SINGLE precision floating values",file=self.__fdout) print("SINGLE precision floating values",file=self.__fdout)
self.__unso=unsio.CunsOut(self.__model_file,unstype); # output file
else: else:
print("DOUBLE precision floating values",file=self.__fdout) print("DOUBLE precision floating values",file=self.__fdout)
self.__unso=unsio.CunsOutD(self.__model_file,unstype); # output file ## SAVE FILE
# instantiate output object
self.__unso=CUNS_OUT(self.__model_file,unstype, float32=single, verbose_debug=self.__verbose)
print("\nSaving in ",unstype," format......",file=self.__fdout) print("\nSaving in ",unstype," format......",file=self.__fdout)
self.__unso.setValueF("time",0) # save time # self.__unso.setValueF("time",0) # save time
self.__unso.setData(0,"time")
select_comp = self.__comp # comp for gadget2 gadget3 select_comp = self.__comp # comp for gadget2 gadget3
if unstype=="nemo": if unstype=="nemo":
select_comp = self.__compNemo # comp for nemo select_comp = self.__compNemo # comp for nemo
#for comp,all_array in select_comp.iteritems(): #for comp,all_array in select_comp.iteritems():
for comp,all_array in select_comp.items(): for comp,all_array in select_comp.items():
print ("[%-6s] : "%(comp),file=self.__fdout,end="") print ("[%-6s] : "%(comp),file=self.__fdout,end="")
...@@ -127,7 +130,7 @@ class CTestunsio: ...@@ -127,7 +130,7 @@ class CTestunsio:
dim=block_array[0] dim=block_array[0]
for array in block_array[1:]: for array in block_array[1:]:
print(" %s"%(array),file=self.__fdout, end="") print(" %s"%(array),file=self.__fdout, end="")
self.__saveArray(comp,array,dim,real=True) # save real self.__saveArray(comp,array,dim,real=True) # save real
# save integer arrays # save integer arrays
dim=self.__1Dint[0] dim=self.__1Dint[0]
...@@ -142,20 +145,20 @@ class CTestunsio: ...@@ -142,20 +145,20 @@ class CTestunsio:
if self.__uns2uns : # test uns2uns if self.__uns2uns : # test uns2uns
ff = tempfile.NamedTemporaryFile() ff = tempfile.NamedTemporaryFile()
myfile = ff.name myfile = ff.name+"_"+unstype
ff.close() ff.close()
if single: if single:
outfloat="float=t" outfloat="float=t"
else: else:
outfloat="float=f" outfloat="float=f"
cmd="uns2uns in=%s out=%s select=%s type=%s %s"%(self.__model_file,myfile,"all",unstype,outfloat) cmd="uns2uns in=%s out=%s select=%s type=%s %s"%(self.__model_file,myfile,"all",unstype,outfloat)
if __verbose: if self.__verbose:
print("<%s>"%(cmd),file=self.__fdout) print("<%s>"%(cmd),file=self.__fdout)
#subprocess.call([cmd],shell=True) #subprocess.call([cmd],shell=True)
subprocess.call(["uns2uns","in="+self.__model_file,"out="+myfile,"select=all","type="+unstype,outfloat]) subprocess.call(["uns2uns","in="+self.__model_file,"out="+myfile,"select=all","type="+unstype,outfloat])
#sys.exit() # sys.exit()
os.remove(self.__model_file) os.remove(self.__model_file)
self.__model_file=myfile self.__model_file=myfile
...@@ -163,7 +166,7 @@ class CTestunsio: ...@@ -163,7 +166,7 @@ class CTestunsio:
print("Outfile = [%s]"%(self.__model_file),file=self.__fdout) print("Outfile = [%s]"%(self.__model_file),file=self.__fdout)
# ----------------------------------------------------- # -----------------------------------------------------
# #
def __compareArray(self,comp,attr,dim,real=True): def __compareArray(self,comp,attr,dim,real=True):
if real:# float if real:# float
...@@ -184,19 +187,19 @@ class CTestunsio: ...@@ -184,19 +187,19 @@ class CTestunsio:
sys.exit() sys.exit()
# ----------------------------------------------------- # -----------------------------------------------------
# #
def __compareModel(self,unstype="gadget3",single=True): def __compareModel(self,unstype="gadget3",single=True):
""" """
load model from disk and compare with generated values load model from disk and compare with generated values
""" """
self.__initSeed() # reset random generator self.__initSeed() # reset random generator
if not os.path.isfile(self.__model_file): if not os.path.isfile(self.__model_file):
print("File [%s] does not exist, aborting..\n"%(self.__model_file),file=self.__fdout) print("File [%s] does not exist, aborting..\n"%(self.__model_file),file=self.__fdout)
sys.exit() sys.exit()
# instantiate CSnapshot object # instantiate CSnapshot object
self.__unsi=CSnapshot(self.__model_file,float32=single) self.__unsi=CUNS_IN(self.__model_file,float32=single)
self.__unsi.nextFrame() # load snaphot self.__unsi.nextFrame() # load snaphot
select_comp = self.__comp # comp for gadget2 gadget3 select_comp = self.__comp # comp for gadget2 gadget3
...@@ -223,7 +226,7 @@ class CTestunsio: ...@@ -223,7 +226,7 @@ class CTestunsio:
self.__unsi.close() self.__unsi.close()
# ----------------------------------------------------- # -----------------------------------------------------
# #
def testIO(self): def testIO(self):
""" """
test models snasphot that unsio knows howto dump on a file test models snasphot that unsio knows howto dump on a file
...@@ -236,7 +239,8 @@ class CTestunsio: ...@@ -236,7 +239,8 @@ class CTestunsio:
print ("\n\nComparing model [%s]"%(mm),file=self.__fdout) print ("\n\nComparing model [%s]"%(mm),file=self.__fdout)
self.__compareModel(unstype=mm,single=self.__single) self.__compareModel(unstype=mm,single=self.__single)
# remove temporary file # remove temporary file
os.remove(self.__model_file) if not self.__uns2uns: # remove only if not using uns2uns
os.remove(self.__model_file)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# commandLine, parse the command line # commandLine, parse the command line
def commandLine(): def commandLine():
...@@ -259,7 +263,7 @@ def commandLine(): ...@@ -259,7 +263,7 @@ def commandLine():
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# commandLine, parse the command line # commandLine, parse the command line
def process(args): def process(args):
uns=CTestunsio(nbody=args.nbody,single=not args.double, uns2uns=args.uns2uns, out=args.out) uns=CTestunsio(nbody=args.nbody,single=not args.double, uns2uns=args.uns2uns, out=args.out, verbose=args.verbose)
#uns.saveModel("") #uns.saveModel("")
uns.testIO() uns.testIO()
...@@ -268,4 +272,3 @@ def process(args): ...@@ -268,4 +272,3 @@ def process(args):
# main program # main program
if __name__ == '__main__': if __name__ == '__main__':
commandLine() commandLine()
...@@ -48,7 +48,7 @@ class build_py(_build_py, object): ...@@ -48,7 +48,7 @@ class build_py(_build_py, object):
# #
MAJOR = '0' MAJOR = '0'
MINOR = '9' MINOR = '9'
MICRO = '2rc2' MICRO = '3rc1'
VERSION = '%s.%s.%s' % (MAJOR, MINOR, MICRO) VERSION = '%s.%s.%s' % (MAJOR, MINOR, MICRO)
# #
......
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