#!/usr/bin/env python import os,sys,math,string,numpy 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) # -------------------------------------------------------------------- # MAIN # -------------------------------------------------------------------- parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """ Converts EBSD data from cubic ang files into description for spectral solver (*.geom + material.config) Can discriminate two phases depending on threshold value """ + string.replace('$Id$','\n','\\n') ) parser.add_option('-c','--column', dest='column', type='int', \ help='data column to separate phase 1 and 2 [%default]') parser.add_option('-t','--threshold', dest='threshold', type='float', \ help='threshold used to separate phases. value < threshold: phase = 1') parser.set_defaults(column = 1) parser.set_defaults(threshold = sys.maxint) (options,filenames) = parser.parse_args() # ------------------------------------------ setup file handles --------------------------------------- eulers = numpy.array([0.0,0.0,0.0],'f') geomdim = numpy.array([0.0,0.0,0.0],'f') res = numpy.array([0,0,1],'i') files = [] if filenames == []: files.append({'name':'STDIN', 'input':sys.stdin, 'material':sys.stdout, 'geom':sys.stdout}) else: for name in filenames: if os.path.exists(name): files.append( {'name':name, 'input':open(name),\ 'material':open(os.path.splitext(name)[0]+'_material.config', 'w'),\ 'geom':open(os.path.splitext(name)[0]+'.geom', 'w')}) # ------------------------------------------ loop over input files --------------------------------------- for file in files: point=0 if file['name'] != 'STDIN': print file['name'] file['material'].write('#---\n\n#---\n'+ '\n[SX]\ntype isostrain\nNgrains 1\n\n'+ '#---\n\n#---\n\n') tempPart2= '#---\n\n#---\n\n' for line in file['input']: lineSplit=line.split() if line[0]=='#': if len(lineSplit)>2: if line.split()[2]=='HexGrid': print 'The file is a hex grid file. Convert it first to sqr grid' sys.exit() if lineSplit[1]=='XSTEP:': stepSizeX = float(lineSplit[2]) if lineSplit[1]=='YSTEP:': stepSizeY = float(lineSplit[2]) if lineSplit[1]=='NCOLS_ODD:': res[0] = int(lineSplit[2]) if lineSplit[1]=='NROWS:': res[1] = int(lineSplit[2]) else: point+=1 eulers = (float(lineSplit[0])/2.0/math.pi*360.0, \ float(lineSplit[1])/2.0/math.pi*360.0, \ float(lineSplit[2])/2.0/math.pi*360.0) if float(lineSplit[options.column-1])