Commit 173dea99 authored by LAMBERT Jean-charles's avatar LAMBERT Jean-charles
Browse files

python movie class

parent e47e50c0
#!/usr/bin/env python
from __future__ import print_function
from uns_simu import *
from csnapshot import *
from c2dplot import *
import py_unstools # rectify swig
from multiprocessing import Lock
import time
import os
#
# ----
#
class CMovie:
"""
Create movie from UNS data
"""
#
# ----
#
# constructor
__analysis=None
__newradius=None
__prop=None
__extdir=None
__select=None
__basedir=None
__percen=None
__lock=None
def __init__(self,analysis=None,verbose=False,verbose_debug=False):
"""
Constructor of CMovie class
- analysis : is a class object instantiate from CUnsAnalysis class
"""
self.__vdebug=verbose_debug
self.__verbose=verbose
if analysis is not None:
self.__analysis=analysis
self.__smartAnalysisInit()
#
# smartAnalysis
#
def smartAnalysis(self,analysis=None):
"""
Main core function to compute MOVIE on current snapshot store in data_analysis
"""
if analysis is None:
data=self.__analysis
else:
data=analysis
uns_snap=data.uns_snap # link to UNS object
ok,time=uns_snap.getData("time")
print("Core [%d] time <%f>"%(data.core_id,time))
# loop on all existing component
for comp,prop,percen,newradius,extdir,fdir in self.__comp_data:
t_file=fdir+"/.time_completed"
if not self.__checkTimeExist(time,t_file): # time not computed yet
filename=uns_snap.getFileName()
basefile=os.path.basename(filename)
no=int(basefile.split("_")[1]) # compute no
c=C2dplot() # object
if prop=="none":
c.draw(uns_snap=uns_snap,select=comp,outdev=fdir+"/frame",no=no,prop=prop,cb=0,rrange=30.,times=time.item())
#
# __checkTimeExist
#
def __checkTimeExist(self,time,t_file):
"""
check in t_file, if time has been already computed
*IN*
time : current time
t_file : file with time value
*OUT*
boolean : True if time exist, False otherwise
"""
if os.path.isfile(t_file):
try:
f=open(t_file,"r")
while True:
atime=float(f.readline())
if (atime-0.001)<time and (atime+0.001)>time:
f.close()
return True
else:
f.close()
return False
except EOFError:
pass
return False
#
# __smartAnalysisInit
#
def __smartAnalysisInit(self):
"""
start some initialisations
"""
data=self.__analysis
if not hasattr(data,'movie_select'):
print("\n\nYou must set a fied [movie_select] in your <data> object, aborting...\n\n")
sys.exit()
data.movie_select=data.movie_select.replace(" ","") # remove blank
### build COM Dir
if hasattr(data,'movie_dir'):
self.__movie_dir=data.movie_dir
#
else: # default simdir simulation
self.__movie_dir=data.sim_info['dir']+"/ANALYSIS/movie"
if self.__vdebug:
print("MOVIE DIR = ", self.__movie_dir, data.sim_info['name'])
self.simname=data.sim_info['name']
self.__comp_data =[] # list to store compononent to proceed
self.__newradius_0=[] # list to store newradius 0
self.__parseSelect(data)
self.__createDir(data)
#
# __parseSelect
#
def __parseSelect(self,data):
"""
parse selected string
"""
### re build select component variable according to components existing at mid
### simulation time (pre-computed by cuns_analysis.py and set to data.list_components
self.__new_select=""
for colon_s in data.movie_select.split(":"): # colon separate analysis
print ("COLON_S =",colon_s)
comp,prop,percen,newradius,extdir=colon_s.split("#")
xx=data.list_components.find(comp) # find if component exist
if xx==-1: # comp not exist
print("CMovie#Warning : component <%s> from select <%s> does not exist...\n"\
%(comp,colon_s))
else: # comp exist
fdir=self.__movie_dir+"/work/rsnap_"+newradius+"_pcen_"+\
percen+"_prop_"+prop+"/"+comp
self.__comp_data.append([comp,prop,float(percen),int(newradius),extdir,fdir])
#
# __createDir
#
def __createDir(self,data):
"""
create directories for each existing components
"""
for comp,prop,percen,newradius,extdir,fdir in self.__comp_data:
# check newradius
radius=-1
if newradius==0: #we must compute radius of the last snapshot
s=CSnapshot(data.slist[-1],comp)
ok=s.nextFrame("xv")
if ok:
status,radius=s.getMaxRadius(select=comp,percen=99.)
else:
print ("Cannot compute newradius_0 for [%s] on file "%(comp,data.slist[-1]))
s.close() # close test snapshot
print("Comp [%s] newradius_0 = <%f>"%(comp,radius))
self.__newradius_0.append([radius]) # store newradius_0
data.lock[data.lock_id].acquire() # lock process
# build directory
if (not os.path.isdir(fdir)) :
try:
print("Core ID ",data.core_id," create directory [%s]\n"%(fdir))
os.makedirs(fdir)
except OSError:
print("Unable to create directory [%s]\n"%(fdir))
data.lock[data.lock_id].release()
sys.exit()
data.lock[data.lock_id].release() # release process
Supports Markdown
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