def define_python(ename, args, source=None, file=None, etype='scalar', echo=False): """ Defines a new python expression. If expression with given name already exists the new definition will replace it. """ etype = etype.lower() if not etype in ["scalar", "vector", "curve", "array", "tensor"]: raise VisItException("Unsupported expression type: %s" % etype) delete(ename) if exists(ename): raise VisItException("Cannot redefine database expression: %s" % ename) kwargs = {"name": ename, "args": args, "type": etype} if not source is None: kwargs["source"] = source if not file is None: kwargs["file"] = file visit.DefinePythonExpression(**kwargs) if echo: print "[Created Python Expression: %s ]" % ename
def query(qname, args=None, rmode="value", echo=False, msg_lvl=2, **kwargs): """ Executes a query and returns result based on 'rmode'. rmode={'value','string',object'} Raises an exception if an error occurs. """ if echo: print "[Query: %s(%s)]" % (qname, str(args)) prev_lvl = visit.SuppressMessages(msg_lvl) if args is None: qres = visit.Query(qname, **kwargs) else: # call w/ args + kwargs cargs = [qname] cargs.extend(args) qres = visit.Query(*cargs, **kwargs) if qres == 0: visit.SuppressMessages(prev_lvl) raise VisItException("Execution of query: '%s' failed." % qname) if rmode == "value": res = visit.GetQueryOutputValue() elif rmode == "string" or rmode == "text" or rmode == "txt": res = visit.GetQueryOutputString() elif rmode == "object": res = visit.GetQueryOutputObject() else: visit.SuppressMessages(prev_lvl) raise VisItException("Unsupported query result mode: %s" % rmode) visit.SuppressMessages(prev_lvl) return res
def close(ename=None): """ Closes VisIt's Compute Engine. """ if ename is None: if visit.CloseComputeEngine() != 1: raise VisItException("Failed to close compute engine.") else: if visit.CloseComputeEngine(ename) != 1: raise VisItException("Failed to close compute engine '%s'." % ename)
def list_input_files(ipattern): match = re.search("%[0-9]*d", ipattern) if match is None: raise VisItException( "Could not determine input sequence pattern (%[0-9]d)") i = 0 ifs = [] while os.path.isfile(ipattern % i): ifs.append(ipattern % i) i += 1 if len(ifs) == 0: raise VisItException("Could not find files matching input pattern %s" % ipattern) return ifs
def execute(self, cname, curves): """ Merges the samples from a set of curves into a new curve. Treats each curve as a segment and sews things together at first() & last() samples. """ res = Curve(cname) # find sensible ordering starting with the curve that contains # the first end point. pts = {} for c in curves: if not c.first() in pts.keys(): pts[c.first()] = [0, c] if not c.last() in pts.keys(): pts[c.last()] = [0, c] pts[c.first()][0] += 1 pts[c.last()][0] += 1 curr = None for k, v in pts.items(): if v[0] == 1 and v[1].first() == k: curr = v[1] break if curr is None: raise VisItException( "Could not find start point w/ edge degree=1!") res_order = [curr] rcurves = [] for c in curves: if c != curr: rcurves.append(c) # build chain from remaining curves while len(rcurves) > 0: found = False for c in rcurves: if curr.last() == c.last() or curr.first() == c.first(): c.reverse_samples() if c.first() == curr.last(): res_order.append(c) rcurves.remove(c) curr = c found = True break if not found: raise VisItException("Could not find curve join point!") for r in res_order: for s in r.samples: if len(res.samples) == 0 or not res.samples[-1] == s: res.samples.append(Sample(s.x, s.y)) return res
def open(**kwargs): """ Launch VisIt compute engine on the current host. """ args = {"ppn": 1, "part": None, "bank": None, "rtime": None, "vdir": None} if not kwargs.has_key("method"): hname = hostname(False) if not hosts().has_key(hname): raise VisItException("Unsupported host: '%s'" % hname) host = hosts(args["vdir"])[hname] # prep args for launch args["host"] = host.name args.update(host.defaults) args.update(host.params) args["method"] = host.launch_method(args["part"]) elif kwargs["method"] == "slurm": args["host"] = hostname(False) if os.environ.has_key("SLURM_JOB_NUM_NODES"): nnodes = int(os.environ["SLURM_JOB_NUM_NODES"]) ppn = int(os.environ["SLURM_CPUS_ON_NODE"]) nprocs = ppn * nnodes args["nprocs"] = nprocs args["ppn"] = ppn kwargs["method"] = "srun" else: args["host"] = hostname(False) args.update(kwargs) return launch(**args)
def launch(host, nprocs, ppn, method, part, bank, rtime, vdir, extra_args=None): """ Launch helper. """ msg = "[%s: opening engine" % host if not part is None: msg += " on %s" % part msg += "]" print msg nnodes = int(math.ceil(float(nprocs) / float(ppn))) ehost = host if host == hostname(False): ehost = "localhost" args = ["-l", method] if not method == "serial": args.extend(["-np", str(nprocs)]) if not vdir is None: args.extend(["-dir", vdir]) if not part is None: args.extend(["-nn", str(nnodes), "-p", part]) if not extra_args is None: args.extend(extra_args) if method == "msub/srun": rtime = str(rtime) if rtime.count(":") == 0: rtime += ":00" args.extend(["-b", bank, "-t", rtime]) if visit.OpenComputeEngine(ehost, args) != 1: raise VisItException("Failed to open compute engine on %s." % host) return visit.GetEngineList()[0]
def __init__(self, name="", samples=None): """ Curve Constructor. """ self.name = name if samples is None: self.samples = [] else: # Check if samples is an ndarray, if so # convert to a list of Samples. if using_numpy and isinstance(samples, npy.ndarray): lshape = len(samples.shape) if lshape == 1: r = samples.shape samples = [Sample(i, samples[i]) for i in xrange(r)] elif lshape == 2: r, c = samples.shape samples = [ Sample(samples[i, 0], samples[i, 1]) for i in xrange(r) ] else: # error msg = "Cannot convert ndarry w/ shape %s to Curve " % str( lshape) raise VisItException(msg) self.samples = samples self.__xmap = None
def encode(ipattern, ofile, fdup=None, etype=None, stereo=False): """ Encodes a sequence of images into a movie. Requires ipattern to use printf style # format like "file%04d.png". """ ipattern = os.path.abspath(ipattern) ofile = os.path.abspath(ofile) if etype is None: # get from ofile base, ext = os.path.splitext(ofile) if ext[0] == '.': ext = ext[1:] etype = ext if not etype in encoders(): raise VisItException("Unsupported encoder: %s" % etype) patterns = {"full": ipattern} lnks = [] if not fdup is None and not stereo: patterns, lnks = gen_symlinks(ipattern, fdup) if stereo: patterns, lnks = gen_symlinks_stereo(ipattern, fdup) ret = encode_patterns(patterns, ofile, etype, stereo) if len(lnks) > 0: clean_symlinks(lnks) return ret
def encode_mp4(ipattern, ofile): """ Creates a mp4 video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: if ffmpeg_version() > .09: # two pass support with newer versions requires two calls to ffmpeg cmd = "echo y | %s -f image2 -i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass %d " cmd += "-an -b:v 18000000 -f mp4 %s" # pass 1 cmd_pass1 = cmd % (enc_bin, ipattern, 1, ofile) res = sexe(cmd_pass1) if res == 0: # pass 2 cmd_pass2 = cmd % (enc_bin, ipattern, 2, ofile) res = sexe(cmd_pass2) else: cmd = "echo y | %s -f image2 -i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-mbd -rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass 1/2 " cmd += "-an -b 18000000 -f mp4 %s" cmd = cmd % (enc_bin, ipattern, ofile) res = sexe(cmd) return res else: raise VisItException("ffmpeg not found: Unable to encode mp4.")
def encode_divx(ipattern, ofile, input_frame_rate, output_frame_rate): """ Creates divx avi video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not enc_bin is None: if ffmpeg_version() > .09: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += ffmpeg_input_type(ipattern) cmd += "-i %s -vcodec mpeg4 -q:v 1 -f avi " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += "-vtag DX50 -an %s " else: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += ffmpeg_input_type(ipattern) cmd += "-i %s -vcodec mpeg4 -qscale 1 -f avi " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += "-vtag DX50 -an %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd, echo=True) else: raise VisItException("ffmpeg not found: Unable to encode divx avi.")
def encode_avi(ipattern, ofile, input_frame_rate, output_frame_rate): """ Creates an avi video file (mjpeg) using ffmpeg. """ enc_bin = ffmpeg_bin() if not enc_bin is None: if ffmpeg_version() > .09: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += "-f image2 -i %s -vcodec mjpeg -q:v 1 -an " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += " %s " else: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += "-f image2 -i %s -vcodec mjpeg -qscale 1 -an " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += " %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd, echo=True) else: raise VisItException("ffmpeg not found: Unable to encode avi.")
def extract(ifile, opattern): """ Extracts a sequence of images from a a movie. Requires ipattern to use printf style # format like "file%04d.png". """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: cmd = "%s -i %s -f image2 %s" % (enc_bin, ifile, opattern) sexe(cmd) else: raise VisItException("ffmpeg not found: Unable to extract frames.")
def encode(ipattern, ofile, fdup=None, etype=None, stereo=False, input_frame_rate=None, output_frame_rate=None): """ Encodes a sequence of images into a movie. Requires ipattern to use printf style # format like "file%04d.png". ipattern -- input file pattern etype -- allows to select which encoder to use ( if not passed the file extension is used to select an encoder Frame Rate Related Options: fdup -- allows you to set an integer number of times to duplicate the input frames as they are passed to the encoder. (the duplication actually happens via symlinks) input_frame_rate -- allows you to set the input frame rate, in frames per second, that the encoder uses. output_frame_rate -- allows you to set the output frame rate, in frames per second, that the encoder uses. Note output formats typically only support a few output fps values. To obtain a perceived fps, the input_frame_rate is a better option to try. """ ipattern = os.path.abspath(ipattern) ofile = os.path.abspath(ofile) if etype is None: # get from ofile base, ext = os.path.splitext(ofile) if ext[0] == '.': ext = ext[1:] etype = ext if not etype in encoders(): raise VisItException("Unsupported encoder: %s" % etype) patterns = {"full": ipattern} lnks = [] if not fdup is None and not stereo: patterns, lnks = gen_symlinks(ipattern, fdup) if stereo: patterns, lnks = gen_symlinks_stereo(ipattern, fdup) ret = encode_patterns(patterns, ofile, etype, stereo, input_frame_rate, output_frame_rate) if len(lnks) > 0: clean_symlinks(lnks) return ret
def python_query(source=None, file=None, vars=None, args=[], rmode="value", echo=False, msg_lvl=2): """ Executes a Python Query and returns result based on 'rmode'. rmode={'value','string',object'} Rasies an exception if an error occurs. """ if echo: print "[PythonQuery]" prev_lvl = visit.SuppressMessages(msg_lvl) kwargs = {} if not source is None: kwargs["source"] = source if not file is None: kwargs["file"] = file if not vars is None: kwargs["vars"] = vars if len(args) > 0: kwargs["args"] = args qres = visit.PythonQuery(**kwargs) if qres == 0: visit.SuppressMessages(prev_lvl) raise VisItException("Execution of PythonQuery failed.") if rmode == "value": res = visit.GetQueryOutputValue() elif rmode == "string": res = visit.GetQueryOutputString() elif rmode == "object": res = visit.GetQueryOutputObject() else: visit.SuppressMessages(prev_lvl) raise VisItException("Unsupported Python Query result mode: %s" % rmode) visit.SuppressMessages(prev_lvl) return res
def gen_symlinks_stereo(ipattern, fdup=None): """ Creates duplicate symlinks to help change effective frame rate of encoded movies. Handles stereo case. """ if fdup is None: fdup = 1 idir, ifile = os.path.split(ipattern) pattern_s = pjoin(idir, "_encode.lnk.%s" % ifile) rbase, rext = os.path.splitext(pattern_s) pattern_l = rbase + ".left" + rext pattern_r = rbase + ".right" + rext ifs = list_input_files(ipattern) ifs_l = [ifs[i] for i in xrange(len(ifs)) if i == 0 or i % 2 == 0] ifs_r = [ifs[i] for i in xrange(len(ifs)) if i % 2 == 1] cnt_l = len(ifs_l) cnt_r = len(ifs_r) # num left frames & num right frames should match if cnt_l != cnt_r: msg = "Different number of left (%d) and right (%d) frames." % (cnt_l, cnt_r) raise VisItException("encode stereo error: %s" % msg) lnks = [] lnk_cnt = 0 frm_cnt = 0 for i in xrange(cnt_l): for j in range(fdup): in_l = ifs_l[i] in_r = ifs_r[i] lnk_0 = pattern_s % lnk_cnt lnk_1 = pattern_s % (lnk_cnt + 1) lnk_l = pattern_l % frm_cnt lnk_r = pattern_r % frm_cnt if platform.system() != "Windows": os.symlink(in_l, lnk_0) os.symlink(in_r, lnk_1) os.symlink(in_l, lnk_l) os.symlink(in_r, lnk_r) else: shutil.copy2(in_l, lnk_0) shutil.copy2(in_r, lnk_1) shutil.copy2(in_l, lnk_l) shutil.copy2(in_r, lnk_r) lnks.extend([lnk_0, lnk_1, lnk_l, lnk_r]) lnk_cnt += 2 frm_cnt += 1 res = {"stereo": pattern_s, "left": pattern_l, "right": pattern_r} return res, lnks
def encode_sm(ipattern, ofile, stereo=False): """ Creates a 'streaming movie' or sm file. """ enc_bin = img2sm_bin() if not enc_bin is None: cmd = "%s -rle" % enc_bin if stereo: cmd += " -stereo" cmd += " %s %s " % (ipattern, ofile) return sexe(cmd) else: raise VisItException( "img2sm not found: Unable to encode streaming movie.")
def encode_avi(ipattern, ofile): """ Creates an avi video file (mjpeg) using ffmpeg. """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: if ffmpeg_version() > .09: cmd = "echo y | %s -f image2 -i %s -vcodec mjpeg -q:v 1 -an %s " else: cmd = "echo y | %s -f image2 -i %s -vcodec mjpeg -qscale 1 -an %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd) else: raise VisItException("ffmpeg not found: Unable to encode avi.")
def encode_sm(ipattern, ofile, stereo=False): """ Creates a 'streaming movie' or sm file. """ enc_bin = img2sm_bin() if not enc_bin is None: cmd = "%s -c rle %s " % (enc_bin, ipattern) if stereo: cmd += " -S " cmd += ofile return sexe(cmd, echo=True) else: raise VisItException( "img2sm not found: Unable to encode streaming movie.")
def define(ename, edef, etype='scalar', echo=False): """ Defines a new expression. If expression with given name already exists the new definition will replace it. """ etype = etype.lower() if not etype in ["scalar", "vector", "curve", "array", "tensor"]: raise VisItException("Unsupported expression type: %s" % etype) delete(ename) if exists(ename): raise VisItException("Cannot redefine database expression: %s" % ename) if etype == "scalar": visit.DefineScalarExpression(ename, edef) elif etype == "vector": visit.DefineVectorExpression(ename, edef) elif etype == "curve": visit.DefineCurveExpression(ename, edef) elif etype == "array": visit.DefineArrayExpression(ename, edef) elif etype == "tensor": visit.DefineTensorExpression(ename, edef) if echo: print "[Expression: %s = '%s']" % (ename, edef)
def open(**kwargs): """ Launch VisIt compute engine on the current host. """ args = {"ppn": 1, "part": None, "bank": None, "rtime": None, "vdir": None} if not kwargs.has_key("method"): hname = hostname(False) # when the visit module is imported (vs used in the CLI), # VISITHOME won't be set, allow user to pass vdir argument # here to locate the host profiles vdir = None if kwargs.has_key("vdir"): vdir = kwargs["vdir"] if not hosts(vdir=vdir).has_key(hname): raise VisItException("Unsupported host: '%s'" % hname) host = hosts(vdir=vdir)[hname] # prep args for launch args["host"] = host.name args.update(host.defaults) args.update(host.params) args["method"] = host.launch_method(args["part"]) elif kwargs["method"] == "slurm": args["host"] = hostname(False) if os.environ.has_key("SLURM_JOB_NUM_NODES"): nnodes = int(os.environ["SLURM_JOB_NUM_NODES"]) ppn = int(os.environ["SLURM_CPUS_ON_NODE"]) nprocs = ppn * nnodes else: raise VisItException( "engine.open(method='slurm') requires " "SLURM_JOB_NUM_NODES and SLURM_CPUS_ON_NODE env vars") args["nprocs"] = nprocs args["ppn"] = ppn kwargs["method"] = "srun" else: args["host"] = hostname(False) args.update(kwargs) return launch(**args)
def encode_divx(ipattern, ofile): """ Creates divx avi video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: if ffmpeg_version() > .09: cmd = "echo y | %s -f image2 -i %s -vcodec mpeg4 -q:v 1 -f avi " cmd += "-vtag DX50 -an %s " else: cmd = "echo y | %s -f image2 -i %s -vcodec mpeg4 -qscale 1 -f avi " cmd += "-vtag DX50 -an %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd) else: raise VisItException("ffmpeg not found: Unable to encode divx avi.")
def encode_mp4(ipattern, ofile, input_frame_rate, output_frame_rate): """ Creates a mp4 video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not enc_bin is None: if ffmpeg_version() > .09: # two pass support with newer versions requires two calls to ffmpeg cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += ffmpeg_input_type(ipattern) cmd += "-i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass %d " cmd += "-passlogfile %s " % ffmpeg_log_file_prefix(ofile) cmd += "-an -b:v 18000000 -f mp4 " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += " %s" # pass 1 cmd_pass1 = cmd % (enc_bin, ipattern, 1, ofile) res = sexe(cmd_pass1, echo=True) if res == 0: # pass 2 cmd_pass2 = cmd % (enc_bin, ipattern, 2, ofile) res = sexe(cmd_pass2, echo=True) else: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += ffmpeg_input_type(ipattern) cmd += "-i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-mbd -rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass 1/2 " cmd += "-an -b 18000000 -f mp4" if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += " %s" cmd = cmd % (enc_bin, ipattern, ofile) res = sexe(cmd, echo=True) # clean up the log file if it exists if os.path.isfile(ffmpeg_log_file_for_pass(ofile)): os.remove(ffmpeg_log_file_for_pass(ofile)) return res else: raise VisItException("ffmpeg not found: Unable to encode mp4.")
def __save_curve(cls,fobj,data, cname = ""): """ Helper to save a single curve. """ if isinstance(data,Curve): if data.name != "": cname = data.name if cname == "": cname = "curve" fobj.write("# %s\n" % cname) if isinstance(data,Curve): for v in data.values(): fobj.write("%s %s\n" % (str(v[0]),str(v[1]))) elif using_numpy and isinstance(data,npy.ndarray): for i in range(data.shape[0]): fobj.write("%s %s\n" % (str(data[i,0]),str(data[i,1]))) else: # error unknown data type msg = "Cannot save curve w/ data object of type: %s" % repr(type(data)) raise VisItException(msg)
def __save_window(self, obase, res, ores, screen_cap, ts): prev_lvl = visit.SuppressMessages(2) res = [int(v) for v in res] if ores is None: ores = res obase = os.path.abspath(obase) odir, ofile = os.path.split(obase) if ts is None: print "[rendering %s/%s.png]" % (odir, ofile) tmp_ofile = "%s___.tmp" % ofile else: print "[rendering %s/%s%04d.png]" % (odir, ofile, ts) tmp_ofile = "%s.%04d___.tmp" % (ofile, ts) sa = visit.SaveWindowAttributes() sa.outputToCurrentDirectory = 0 sa.outputDirectory = odir sa.fileName = tmp_ofile sa.format = sa.PNG sa.width, sa.height = res sa.screenCapture = 0 sa.saveTiled = 0 sa.resConstraint = sa.NoConstraint visit.SetSaveWindowAttributes(sa) a = visit.GetAnnotationAttributes() a.userInfoFlag = 0 visit.SetAnnotationAttributes(a) fname = visit.SaveWindow() if fname == "/dev/null/SaveWindow_Error.txt" or not os.path.isfile( fname): raise VisItException("Error saving window.") des = fname[:fname.find("___.tmp")] des += ".png" shutil.move(fname, des) if ores[0] != res[0] or ores[1] != res[1]: stargs = (res[0], res[1], ores[0], ores[1]) print "[resizing output (from %dx%d to %dx%d)]" % stargs sexe("convert -resize %dx%d %s %s" % (ores[0], ores[1], des, des)) visit.SuppressMessages(prev_lvl) return des
def hosts(vdir=None, legacy=False, reload=False): global __hosts if len(__hosts.keys()) == 0 or reload: __hosts.clear() if legacy: llnl_open, llnl_closed = legacy_vutils_engine_profiles() __hosts.update(llnl_open) __hosts.update(llnl_closed) else: if vdir is None: if not os.environ.has_key("VISITHOME"): raise VisItException( "hosts() requires VISITHOME env var or explicit vdir argument" ) vdir = os.environ["VISITHOME"] hpf_files = glob.glob(pjoin(vdir, ".visit", "hosts", "*.xml")) for hpf_file in hpf_files: try: hpf = HostProfile.load(hpf_file) __hosts[hpf.name] = hpf except: print "[warning: Could not load host profile: '%s']" % hpf_file return __hosts
def launch_method(self,part): if not self.partitions.has_key(part): raise VisItException("Unsupported partition '%s' on host %s" % (part,self.name)) return self.partitions[part]