introduced use of choice for argparse where applicable

This commit is contained in:
Martin Diehl 2014-07-24 18:47:09 +00:00
parent d5fcda8a29
commit 38048a09f2
11 changed files with 92 additions and 94 deletions

View File

@ -262,46 +262,47 @@ Produce VTK file from data field. Coordinates are taken from (consecutive) x, y,
""",version = string.replace(scriptID,'\n','\\n') """,version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-s', '--scalar', action='extend', dest='scalar', type='string', \ sepChoices = ['n','t','s']
help='list of single scalars to visualize', metavar = '<string LIST>') parser.add_option('-s', '--scalar', dest='scalar', action='extend', type='string', metavar = '<string LIST>',
parser.add_option( '--double', action='extend', dest='double', type='string', \ help='list of single scalars to visualize')
help='list of two scalars to visualize', metavar = '<string LIST>') parser.add_option( '--double', dest='double', action='extend', type='string', metavar = '<string LIST>',
parser.add_option( '--triple', action='extend', dest='triple', type='string', \ help='list of two scalars to visualize')
help='list of three scalars to visualize', metavar = '<string LIST>') parser.add_option( '--triple', dest='triple', action='extend', type='string', metavar = '<string LIST>',
parser.add_option( '--quadruple', action='extend', dest='quadruple', type='string', \ help='list of three scalars to visualize')
help='list of four scalars to visualize', metavar = '<string LIST>') parser.add_option( '--quadruple', dest='quadruple', action='extend', type='string', metavar = '<string LIST>',
parser.add_option('-v', '--vector', action='extend', dest='vector', type='string', \ help='list of four scalars to visualize')
help='list of vectors to visualize', metavar = '<string LIST>') parser.add_option('-v', '--vector', dest='vector', action='extend', type='string', metavar = '<string LIST>',
parser.add_option('-t', '--tensor', action='extend', dest='tensor', type='string', \ help='list of vectors to visualize')
help='list of tensors to visualize', metavar = '<string LIST>') parser.add_option('-t', '--tensor', dest='tensor', action='extend', type='string', metavar = '<string LIST>',
parser.add_option('-d', '--deformation', dest='defgrad', action='store', type='string', \ help='list of tensors to visualize')
help='heading of deformation gradient columns [%default]', metavar = 'string') parser.add_option('-d', '--deformation', dest='defgrad', action='store', type='string', metavar = 'string',
parser.add_option('--reference', dest='undeformed', action='store_true',\ help='heading of deformation gradient columns [%default]')
help='map results to reference (undeformed) configuration [%default]') parser.add_option('--reference', dest='undeformed', action='store_true',
parser.add_option('-c','--cell', dest='cell', action='store_true',\ help='map results to reference (undeformed) configuration [%default]')
help='data is cell-centered [%default]') parser.add_option('-c','--cell', dest='cell', action='store_true',
parser.add_option('-p','--vertex', dest='cell', action='store_false',\ help='data is cell-centered [%default]')
help='data is vertex-centered') parser.add_option('-p','--vertex', dest='cell', action='store_false',
parser.add_option('--mesh', dest='output_mesh', action='store_true', \ help='data is vertex-centered')
help='produce VTK mesh file [%default]') parser.add_option('--mesh', dest='output_mesh', action='store_true',
parser.add_option('--nomesh', dest='output_mesh', action='store_false', \ help='produce VTK mesh file [%default]')
help='omit VTK mesh file') parser.add_option('--nomesh', dest='output_mesh', action='store_false',
parser.add_option('--points', dest='output_points', action='store_true', \ help='omit VTK mesh file')
help='produce VTK points file [%default]') parser.add_option('--points', dest='output_points', action='store_true',
parser.add_option('--nopoints', dest='output_points', action='store_false', \ help='produce VTK points file [%default]')
help='omit VTK points file') parser.add_option('--nopoints', dest='output_points', action='store_false',
parser.add_option('--separator', dest='separator', action='store', type='string', \ help='omit VTK points file')
help='data separator (t(ab), n(ewline), s(pace)) [%default]', metavar = 'string') parser.add_option('--separator', dest='separator', action='store', type='choice', choices=sepChoices, metavar='string',
parser.add_option('--scaling', dest='scaling', action='extend', type='string', \ help='data separator (%s) [t]'%(','.join(map(str,sepChoices))))
help='scaling of fluctuation', metavar = '<float LIST>') parser.add_option('--scaling', dest='scaling', action='extend', type='string',
parser.add_option('-u', '--unitlength', dest='unitlength', action='store', type='float', \ help='scaling of fluctuation', metavar = '<float LIST>')
help='set unit length for 2D model [%default]', metavar = 'float') parser.add_option('-u', '--unitlength', dest='unitlength', action='store', type='float', metavar = 'float',
parser.add_option('--filenodalcoords', dest='filenodalcoords', action='store', type='string', \ help='set unit length for 2D model [%default]')
help='ASCII table containing nodal coords', metavar = 'string') parser.add_option('--filenodalcoords', dest='filenodalcoords', action='store', type='string', metavar = 'string',
parser.add_option('--labelnodalcoords', dest='labelnodalcoords', action='store', type='string', nargs=3, \ help='ASCII table containing nodal coords')
help='labels of nodal coords in ASCII table %default', metavar = 'string string string') parser.add_option('--labelnodalcoords', dest='labelnodalcoords', action='store', type='string', nargs=3,
parser.add_option('-l', '--linear', dest='linearreconstruction', action='store_true',\ help='labels of nodal coords in ASCII table %default', metavar = 'string string string')
help='use linear reconstruction of geometry [%default]') parser.add_option('-l', '--linear', dest='linearreconstruction', action='store_true',
help='use linear reconstruction of geometry [%default]')
parser.set_defaults(defgrad = 'f') parser.set_defaults(defgrad = 'f')
parser.set_defaults(separator = 't') parser.set_defaults(separator = 't')
@ -330,7 +331,6 @@ options.scaling = map(float, options.scaling)
if np.any(options.scaling != 1.0) and options.linearreconstruction: print 'cannot scale for linear reconstruction' if np.any(options.scaling != 1.0) and options.linearreconstruction: print 'cannot scale for linear reconstruction'
if np.any(options.scaling != 1.0) and options.filenodalcoords != '': print 'cannot scale when reading coordinate from file' if np.any(options.scaling != 1.0) and options.filenodalcoords != '': print 'cannot scale when reading coordinate from file'
options.separator = options.separator.lower()
for filename in args: for filename in args:
if not os.path.exists(filename): if not os.path.exists(filename):

View File

@ -25,10 +25,10 @@ Example: distance to IP coordinates -- "math.sqrt( #ip.x#**2 + #ip.y#**2 + round
""", version = string.replace(scriptID,'\n','\\n') """, version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-l','--label', dest='labels', action='extend', type='string', \ parser.add_option('-l','--label', dest='labels', action='extend', type='string', metavar='<string LIST>',
help='(list of) new column labels', metavar='<string LIST>') help='(list of) new column labels')
parser.add_option('-f','--formula', dest='formulas', action='extend', type='string', \ parser.add_option('-f','--formula', dest='formulas', action='extend', type='string', metavar='<string LIST>',
help='(list of) formulas corresponding to labels', metavar='<string LIST>') help='(list of) formulas corresponding to labels')
parser.set_defaults(labels= []) parser.set_defaults(labels= [])
parser.set_defaults(formulas= []) parser.set_defaults(formulas= [])

View File

@ -21,9 +21,9 @@ deformation gradient and first Piola--Kirchhoff stress.
""", version = string.replace(scriptID,'\n','\\n') """, version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-f','--defgrad', dest='defgrad', action='store', type='string', metavar='string', \ parser.add_option('-f','--defgrad', dest='defgrad', action='store', type='string', metavar='string',
help='heading of columns containing deformation gradient [%default]') help='heading of columns containing deformation gradient [%default]')
parser.add_option('-p','--stress', dest='stress', action='store', type='string', metavar='string', \ parser.add_option('-p','--stress', dest='stress', action='store', type='string', metavar='string',
help='heading of columns containing first Piola--Kirchhoff stress [%default]') help='heading of columns containing first Piola--Kirchhoff stress [%default]')
parser.set_defaults(defgrad = 'f') parser.set_defaults(defgrad = 'f')
parser.set_defaults(stress = 'p') parser.set_defaults(stress = 'p')

View File

@ -20,13 +20,13 @@ Operates on periodic ordered three-dimensional data sets.
""", version = string.replace(scriptID,'\n','\\n') """, version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('--no-shape','-s', dest='noShape', action='store_false', \ parser.add_option('--no-shape','-s', dest='noShape', action='store_false',
help='do not calcuate shape mismatch [%default]') help='do not calcuate shape mismatch [%default]')
parser.add_option('--no-volume','-v', dest='noVolume', action='store_false', \ parser.add_option('--no-volume','-v', dest='noVolume', action='store_false',
help='do not calculate volume mismatch [%default]') help='do not calculate volume mismatch [%default]')
parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string', \ parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string',
help='column heading for coordinates [%default]') help='column heading for coordinates [%default]')
parser.add_option('-f','--deformation', dest='defgrad', action='store', type='string', metavar='string ', \ parser.add_option('-f','--deformation', dest='defgrad', action='store', type='string', metavar='string ',
help='column heading for coordinates [%defgrad]') help='column heading for coordinates [%defgrad]')
parser.set_defaults(noVolume = False) parser.set_defaults(noVolume = False)
parser.set_defaults(noShape = False) parser.set_defaults(noShape = False)

View File

@ -22,11 +22,11 @@ Deals with both vector- and tensor-valued fields.
""", version = string.replace(scriptID,'\n','\\n') """, version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string', \ parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string',
help='column heading for coordinates [%default]') help='column heading for coordinates [%default]')
parser.add_option('-v','--vector', dest='vector', action='extend', type='string', metavar='<string LIST>', \ parser.add_option('-v','--vector', dest='vector', action='extend', type='string', metavar='<string LIST>',
help='heading of columns containing vector field values') help='heading of columns containing vector field values')
parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='<string LIST>', \ parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='<string LIST>',
help='heading of columns containing tensor field values') help='heading of columns containing tensor field values')
parser.set_defaults(coords = 'ip') parser.set_defaults(coords = 'ip')
parser.set_defaults(vector = []) parser.set_defaults(vector = [])

View File

@ -21,11 +21,11 @@ Operates on periodic ordered three-dimensional data sets.
""", version = string.replace(scriptID,'\n','\\n') """, version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string', \ parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string',
help='column heading for coordinates [%default]') help='column heading for coordinates [%default]')
parser.add_option('-d','--defgrad', dest='defgrad', action='store', type='string', metavar='string', \ parser.add_option('-d','--defgrad', dest='defgrad', action='store', type='string', metavar='string',
help='heading of columns containing tensor field values') help='heading of columns containing tensor field values')
parser.add_option('-l', '--linear', dest='linearreconstruction', action='store_true',\ parser.add_option('-l', '--linear', dest='linearreconstruction', action='store_true',
help='use linear reconstruction of geometry [%default]') help='use linear reconstruction of geometry [%default]')
parser.set_defaults(coords = 'ip') parser.set_defaults(coords = 'ip')
parser.set_defaults(defgrad = 'f' ) parser.set_defaults(defgrad = 'f' )

View File

@ -27,7 +27,7 @@ Add column(s) containing determinant of requested tensor column(s).
""", version=string.replace(scriptID,'\n','\\n') """, version=string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='<string LIST>', \ parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='<string LIST>',
help='heading of columns containing tensor field values') help='heading of columns containing tensor field values')
parser.set_defaults(tensor = []) parser.set_defaults(tensor = [])

View File

@ -28,9 +28,9 @@ Add column(s) containing deviator of requested tensor column(s).
""", version = string.replace(scriptID,'\n','\\n') """, version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='<string LIST>', \ parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='<string LIST>',
help='heading of columns containing tensor field values') help='heading of columns containing tensor field values')
parser.add_option('-s','--spherical', dest='hydrostatic', action='store_true',\ parser.add_option('-s','--spherical', dest='hydrostatic', action='store_true',
help='also add sperical part of tensor (hydrostatic component, pressure)') help='also add sperical part of tensor (hydrostatic component, pressure)')
parser.set_defaults(hydrostatic = False) parser.set_defaults(hydrostatic = False)
parser.set_defaults(tensor = []) parser.set_defaults(tensor = [])

View File

@ -9,7 +9,7 @@ import damask
scriptID = '$Id$' scriptID = '$Id$'
scriptName = scriptID.split()[1] scriptName = scriptID.split()[1]
accuracyChoices = ['2','4','6','8']
# -------------------------------------------------------------------- # --------------------------------------------------------------------
# MAIN # MAIN
@ -23,15 +23,16 @@ Deals with both vector- and tensor-valued fields.
""", version = string.replace(scriptID,'\n','\\n') """, version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('--fdm', dest='accuracy', action='extend', type='string', metavar='<int LIST>', \ accuracyChoices = ['2','4','6','8']
parser.add_option('--fdm', dest='accuracy', action='extend', type='string', metavar='<int LIST>',
help='degree of central difference accuracy (%s)'%(','.join(accuracyChoices))) help='degree of central difference accuracy (%s)'%(','.join(accuracyChoices)))
parser.add_option('--fft', dest='fft', action='store_true', \ parser.add_option('--fft', dest='fft', action='store_true',
help='calculate divergence in Fourier space') help='calculate divergence in Fourier space')
parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar = 'string', \ parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar = 'string',
help='column heading for coordinates [%default]') help='column heading for coordinates [%default]')
parser.add_option('-v','--vector', dest='vector', action='extend', type='string', metavar='<string LIST>', \ parser.add_option('-v','--vector', dest='vector', action='extend', type='string', metavar='<string LIST>',
help='heading of columns containing vector field values') help='heading of columns containing vector field values')
parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='<string LIST>', \ parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='<string LIST>',
help='heading of columns containing tensor field values') help='heading of columns containing tensor field values')
parser.set_defaults(coords = 'ip') parser.set_defaults(coords = 'ip')
parser.set_defaults(accuracy = []) parser.set_defaults(accuracy = [])
@ -43,10 +44,9 @@ parser.set_defaults(tensor = [])
if len(options.vector) + len(options.tensor) == 0: if len(options.vector) + len(options.tensor) == 0:
parser.error('no data column specified...') parser.error('no data column specified...')
if not set(options.accuracy).issubset(set(accuracyChoices)):
for choice in options.accuracy: parser.error('accuracy must be chosen from %s...'%(', '.join(accuracyChoices)))
if choice not in accuracyChoices:
parser.error('accuracy must be chosen from %s...'%(', '.join(accuracyChoices)))
if options.fft: options.accuracy.append('FFT') if options.fft: options.accuracy.append('FFT')
if not options.accuracy: if not options.accuracy:
parser.error('no accuracy selected') parser.error('no accuracy selected')

View File

@ -38,10 +38,10 @@ based on given cubic stiffness values C11, C12, and C44 in consecutive columns.
""", version = string.replace(scriptID,'\n','\\n') """, version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-c','--stiffness', dest='vector', action='extend', type='string', metavar='<string LIST>', \ parser.add_option('-c','--stiffness', dest='vector', action='extend', type='string', metavar='<string LIST>',
help='heading of column containing C11 (followed by C12, C44) field values') help='heading of column containing C11 (followed by C12, C44) field values')
parser.add_option('-d','--direction', \ parser.add_option('-d','--direction', \
'--hkl', dest='hkl', action='store', type='int', nargs=3, metavar='int int int', \ '--hkl', dest='hkl', action='store', type='int', nargs=3, metavar='int int int',
help='direction of elastic modulus %default') help='direction of elastic modulus %default')
parser.set_defaults(vector = []) parser.set_defaults(vector = [])
parser.set_defaults(hkl = [1,1,1]) parser.set_defaults(hkl = [1,1,1])

View File

@ -36,9 +36,10 @@ def periodic_3Dpad(array, rimdim=(1,1,1)):
# -------------------------------------------------------------------- # --------------------------------------------------------------------
features = [ \ features = [ \
{'aliens': 1, 'names': ['boundary','biplane'],}, {'aliens': 1, 'name': 'biplane'},
{'aliens': 2, 'names': ['tripleline',],}, {'aliens': 1, 'name': 'boundary'},
{'aliens': 3, 'names': ['quadruplepoint',],} {'aliens': 2, 'name': 'tripleline'},
{'aliens': 3, 'name': 'quadruplepoint'}
] ]
neighborhoods = { neighborhoods = {
@ -87,34 +88,31 @@ boundaries, triple lines, and quadruple points.
""", version = string.replace(scriptID,'\n','\\n') """, version = string.replace(scriptID,'\n','\\n')
) )
parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string',
parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string', \
help='column heading for coordinates [%default]') help='column heading for coordinates [%default]')
parser.add_option('-i','--identifier', dest='id', action='store', type='string', metavar = 'string', \ parser.add_option('-i','--identifier', dest='id', action='store', type='string', metavar = 'string',
help='heading of column containing grain identifier [%default]') help='heading of column containing grain identifier [%default]')
parser.add_option('-t','--type', dest='type', action='extend', type='string', metavar='<string LIST>', \ parser.add_option('-t','--type', dest='type', action='extend', type='string', metavar='<string LIST>',
help='feature type (%s)'%(', '.join(map(lambda x:', '.join(x['names']),features)))) help='feature type (%s)'%(', '.join(map(lambda x:', '.join([x['name']]),features))))
parser.add_option('-n','--neighborhood',dest='neigborhood', action='store', type='string', metavar='int', \ parser.add_option('-n','--neighborhood',dest='neigborhood', action='store', type='choice',
help='type of neighborhood (%s)'%(', '.join(neighborhoods.keys()))) choices=neighborhoods.keys(), metavar='string',
parser.set_defaults(coords = 'ip') help='type of neighborhood (%s) [neumann]'%(', '.join(neighborhoods.keys())))
parser.set_defaults(type = []) parser.set_defaults(type = [])
parser.set_defaults(coords = 'ip')
parser.set_defaults(id = 'texture') parser.set_defaults(id = 'texture')
parser.set_defaults(neighborhood = 'neumann') parser.set_defaults(neighborhood = 'neumann')
(options,filenames) = parser.parse_args() (options,filenames) = parser.parse_args()
options.neighborhood = options.neighborhood.lower() if len(options.type) == 0: parser.error('please select a feature type')
if options.neighborhood not in neighborhoods: if not set(options.type).issubset(set(map(lambda x: x['name'],features))):
parser.error('unknown neighborhood %s!'%options.neighborhood) parser.error('type must be chosen from (%s)...'%(', '.join(map(lambda x:', '.join([x['name']]),features))))
if 'biplane' in options.type and 'boundary' in options.type:
parser.error("please select only one alias for 'biplane' and 'boundary'")
feature_list = [] feature_list = []
for i,feature in enumerate(features): for i,feature in enumerate(features):
for name in feature['names']: if feature['name'] in options.type: feature_list.append(i) # remember valid features
for type in options.type:
if name.startswith(type):
feature_list.append(i) # remember valid features
break
# ------------------------------------------ setup file handles ----------------------------------- # ------------------------------------------ setup file handles -----------------------------------
files = [] files = []
@ -143,7 +141,7 @@ for file in files:
# ------------------------------------------ assemble header --------------------------------------- # ------------------------------------------ assemble header ---------------------------------------
for feature in feature_list: for feature in feature_list:
table.labels_append('ED_%s(%s)'%(features[feature]['names'][0],options.id)) # extend ASCII header with new labels table.labels_append('ED_%s(%s)'%(features[feature]['name'],options.id)) # extend ASCII header with new labels
table.head_write() table.head_write()