#!/usr/bin/env python3 import os import sys from io import StringIO from optparse import OptionParser import numpy as np import damask scriptName = os.path.splitext(os.path.basename(__file__))[0] scriptID = ' '.join([scriptName,damask.version]) #-------------------------------------------------------------------------------------------------- # MAIN #-------------------------------------------------------------------------------------------------- parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """ Renumber sorted microstructure indices to 1,...,N. """, version=scriptID) (options, filenames) = parser.parse_args() if filenames == []: filenames = [None] for name in filenames: damask.util.report(scriptName,name) if name is None: virt_file = StringIO(''.join(sys.stdin.read())) geom = damask.Geom.from_file(virt_file) else: geom = damask.Geom.from_file(name) microstructure = geom.get_microstructure() renumbered = np.copy(microstructure) for i, oldID in enumerate(np.unique(microstructure)): renumbered = np.where(microstructure == oldID, i+1, renumbered) geom.set_microstructure(renumbered) geom.add_comment(scriptID + ' ' + ' '.join(sys.argv[1:])) damask.util.croak(geom) if name is None: sys.stdout.write(str(geom.show())) else: geom.to_file(name)