v0.14.0
convert.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 import sys
5 import platform
6 import argparse
7 import subprocess
8 from os import path
9 from itertools import filterfalse
10 
11 if platform.system() == 'Linux':
12  import multiprocessing as mp
13 if platform.system() == 'Darwin':
14  try:
15  import multiprocess as mp
16  except ImportError:
17  print('On macOS this script requires multiprocess package for distributing input files between multiple processes.\nThis package could not be found on your system, it could be installed using e.g. "pip install multiprocess".\n')
18  exit()
19 if platform.system() == 'Windows':
20  print('This script is not supported on Windows\n')
21  exit()
22 
23 
24 def print_progress(iteration, total, decimals=1, bar_length=50):
25  str_format = "{0:." + str(decimals) + "f}"
26  percents = str_format.format(100 * (iteration / float(total)))
27  filled_length = int(round(bar_length * iteration / float(total)))
28  bar = '█' * filled_length + '-' * (bar_length - filled_length)
29 
30  sys.stdout.write('\r |%s| %s%s (%s of %s)' %
31  (bar, percents, '%', str(iteration), str(total)))
32  sys.stdout.flush()
33 
34 def is_not_h5m(file):
35  return not file.endswith('h5m')
36 
38  file_vtk = path.splitext(file)[0] + ".vtk"
39  if path.exists(file_vtk):
40  return path.getmtime(file) < path.getmtime(file_vtk)
41  return False
42 
43 def mb_convert(file):
44  file = path.splitext(file)[0]
45  p = subprocess.Popen(["mbconvert", file + ".h5m", file + ".vtk"],
46  stdout=subprocess.PIPE, stderr=subprocess.PIPE)
47  (out, err) = p.communicate()
48  lock.acquire()
49  if p.returncode:
50  print('\n' + err.decode())
51  n.value += 1
52  print_progress(n.value, N.value)
53  lock.release()
54 
55 def init(l):
56  global lock
57  lock = l
58 
59 if __name__ == '__main__':
60  parser = argparse.ArgumentParser(
61  description="Convert multiple h5m files to vtk files using the mbconvert tool (part of the MOAB library). The input files can be distributed between multiple processes to speed-up the conversion.")
62  parser.add_argument(
63  "file", help="list of h5m files or a regexp mask", nargs='+')
64  parser.add_argument("-np", help="number of processes", type=int, default=1)
65  parser.add_argument('--platform', help=argparse.SUPPRESS, action='store_true')
66  args = parser.parse_args()
67 
68  if args.platform:
69  print('Platform: {}'.format(platform.system()))
70 
71  file_list = list(filterfalse(is_not_h5m, args.file))
72  if not len(file_list):
73  print("No h5m files were found with the given name/mask")
74  exit()
75 
76  file_list = list(filterfalse(is_older_than_vtk, file_list))
77  if not len(file_list):
78  print("All found h5m files are older than corresponding vtk files")
79  exit()
80 
81  N = mp.Value('i', len(file_list))
82  n = mp.Value('i', 0)
83 
84  l = mp.Lock()
85  pool = mp.Pool(processes=args.np, initializer=init, initargs=(l,))
86 
87  print_progress(n.value, N.value)
88  pool.map(mb_convert, file_list)
89  pool.close()
90  pool.join()
91 
92  print('\n')
93  exit()
convert.mb_convert
def mb_convert(file)
Definition: convert.py:43
convert.is_not_h5m
def is_not_h5m(file)
Definition: convert.py:34
convert.is_older_than_vtk
def is_older_than_vtk(file)
Definition: convert.py:37
convert.int
int
Definition: convert.py:64
convert.print_progress
def print_progress(iteration, total, decimals=1, bar_length=50)
Definition: convert.py:24
convert.init
def init(l)
Definition: convert.py:55