9 from itertools
import filterfalse
11 if platform.system() ==
'Linux':
12 import multiprocessing
as mp
13 if platform.system() ==
'Darwin':
15 import multiprocess
as mp
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')
19 if platform.system() ==
'Windows':
20 print(
'This script is not supported on Windows\n')
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)
30 sys.stdout.write(
'\r |%s| %s%s (%s of %s)' %
31 (bar, percents,
'%', str(iteration), str(total)))
35 return not file.endswith(
'h5m')
38 file_vtk = path.splitext(file)[0] +
".vtk"
39 if path.exists(file_vtk):
40 return path.getmtime(file) < path.getmtime(file_vtk)
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()
50 print(
'\n' + err.decode())
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.")
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()
69 print(
'Platform: {}'.format(platform.system()))
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")
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")
81 N = mp.Value(
'i', len(file_list))
85 pool = mp.Pool(processes=args.np, initializer=init, initargs=(l,))
88 pool.map(mb_convert, file_list)