#!/usr/bin/env python import os,re,sys,math,numpy,skfmm,string,damask from scipy import ndimage from optparse import OptionParser, Option # ----------------------------- class extendableOption(Option): # ----------------------------- # used for definition of new option parser action 'extend', which enables to take multiple option arguments # taken from online tutorial http://docs.python.org/library/optparse.html ACTIONS = Option.ACTIONS + ("extend",) STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",) TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",) ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",) def take_action(self, action, dest, opt, value, values, parser): if action == "extend": lvalue = value.split(",") values.ensure_value(dest, []).extend(lvalue) else: Option.take_action(self, action, dest, opt, value, values, parser) def periodic_3Dpad(array, rimdim=(1,1,1)): rimdim = numpy.array(rimdim,'i') size = numpy.array(array.shape,'i') padded = numpy.empty(size+2*rimdim,array.dtype) padded[rimdim[0]:rimdim[0]+size[0], rimdim[1]:rimdim[1]+size[1], rimdim[2]:rimdim[2]+size[2]] = array p = numpy.zeros(3,'i') for side in xrange(3): for p[(side+2)%3] in xrange(padded.shape[(side+2)%3]): for p[(side+1)%3] in xrange(padded.shape[(side+1)%3]): for p[side%3] in xrange(rimdim[side%3]): spot = (p-rimdim)%size padded[p[0],p[1],p[2]] = array[spot[0],spot[1],spot[2]] for p[side%3] in xrange(rimdim[side%3]+size[side%3],size[side%3]+2*rimdim[side%3]): spot = (p-rimdim)%size padded[p[0],p[1],p[2]] = array[spot[0],spot[1],spot[2]] return padded # -------------------------------------------------------------------- # MAIN # -------------------------------------------------------------------- features = [ \ {'aliens': 1, 'names': ['boundary','biplane'],}, {'aliens': 2, 'names': ['tripleline',],}, {'aliens': 3, 'names': ['quadruplepoint',],} ] neighborhoods = { 'neumann':numpy.array([ [-1, 0, 0], [ 1, 0, 0], [ 0,-1, 0], [ 0, 1, 0], [ 0, 0,-1], [ 0, 0, 1], ]), 'moore':numpy.array([ [-1,-1,-1], [ 0,-1,-1], [ 1,-1,-1], [-1, 0,-1], [ 0, 0,-1], [ 1, 0,-1], [-1, 1,-1], [ 0, 1,-1], [ 1, 1,-1], [-1,-1, 0], [ 0,-1, 0], [ 1,-1, 0], [-1, 0, 0], # [ 1, 0, 0], [-1, 1, 0], [ 0, 1, 0], [ 1, 1, 0], [-1,-1, 1], [ 0,-1, 1], [ 1,-1, 1], [-1, 0, 1], [ 0, 0, 1], [ 1, 0, 1], [-1, 1, 1], [ 0, 1, 1], [ 1, 1, 1], ]) } parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """ Add column containing Euclidean distance to grain structural features: boundaries, triple lines, and quadruple points. """ + string.replace('$Id$','\n','\\n') ) parser.add_option('-g','--geom', dest='geom', action='store', type='string', \ help='optional geometry file', \ metavar='') parser.add_option('-i','--identifier', dest='id', action='store', type='string', \ help='heading of column containing grain identifier [%default]', \ metavar='