From 38048a09f20a53876ac3ce6dd344e0f92a1d16d5 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 24 Jul 2014 18:47:09 +0000 Subject: [PATCH] introduced use of choice for argparse where applicable --- processing/post/3Dvisualize.py | 82 ++++++++++----------- processing/post/addCalculation.py | 8 +- processing/post/addCauchy.py | 4 +- processing/post/addCompatibilityMismatch.py | 8 +- processing/post/addCurl.py | 6 +- processing/post/addDeformedConfiguration.py | 6 +- processing/post/addDeterminant.py | 2 +- processing/post/addDeviator.py | 4 +- processing/post/addDivergence.py | 20 ++--- processing/post/addEhkl.py | 4 +- processing/post/addEuclideanDistance.py | 42 +++++------ 11 files changed, 92 insertions(+), 94 deletions(-) diff --git a/processing/post/3Dvisualize.py b/processing/post/3Dvisualize.py index c2e1c8404..cb291448f 100755 --- a/processing/post/3Dvisualize.py +++ b/processing/post/3Dvisualize.py @@ -262,46 +262,47 @@ Produce VTK file from data field. Coordinates are taken from (consecutive) x, y, """,version = string.replace(scriptID,'\n','\\n') ) -parser.add_option('-s', '--scalar', action='extend', dest='scalar', type='string', \ - help='list of single scalars to visualize', metavar = '') -parser.add_option( '--double', action='extend', dest='double', type='string', \ - help='list of two scalars to visualize', metavar = '') -parser.add_option( '--triple', action='extend', dest='triple', type='string', \ - help='list of three scalars to visualize', metavar = '') -parser.add_option( '--quadruple', action='extend', dest='quadruple', type='string', \ - help='list of four scalars to visualize', metavar = '') -parser.add_option('-v', '--vector', action='extend', dest='vector', type='string', \ - help='list of vectors to visualize', metavar = '') -parser.add_option('-t', '--tensor', action='extend', dest='tensor', type='string', \ - help='list of tensors to visualize', metavar = '') -parser.add_option('-d', '--deformation', dest='defgrad', action='store', type='string', \ - help='heading of deformation gradient columns [%default]', metavar = 'string') -parser.add_option('--reference', dest='undeformed', action='store_true',\ - help='map results to reference (undeformed) configuration [%default]') -parser.add_option('-c','--cell', dest='cell', action='store_true',\ - help='data is cell-centered [%default]') -parser.add_option('-p','--vertex', dest='cell', action='store_false',\ - help='data is vertex-centered') -parser.add_option('--mesh', dest='output_mesh', action='store_true', \ - help='produce VTK mesh file [%default]') -parser.add_option('--nomesh', dest='output_mesh', action='store_false', \ - help='omit VTK mesh file') -parser.add_option('--points', dest='output_points', action='store_true', \ - help='produce VTK points file [%default]') -parser.add_option('--nopoints', dest='output_points', action='store_false', \ - help='omit VTK points file') -parser.add_option('--separator', dest='separator', action='store', type='string', \ - help='data separator (t(ab), n(ewline), s(pace)) [%default]', metavar = 'string') -parser.add_option('--scaling', dest='scaling', action='extend', type='string', \ - help='scaling of fluctuation', metavar = '') -parser.add_option('-u', '--unitlength', dest='unitlength', action='store', type='float', \ - help='set unit length for 2D model [%default]', metavar = 'float') -parser.add_option('--filenodalcoords', dest='filenodalcoords', action='store', type='string', \ - help='ASCII table containing nodal coords', metavar = 'string') -parser.add_option('--labelnodalcoords', dest='labelnodalcoords', action='store', type='string', nargs=3, \ - help='labels of nodal coords in ASCII table %default', metavar = 'string string string') -parser.add_option('-l', '--linear', dest='linearreconstruction', action='store_true',\ - help='use linear reconstruction of geometry [%default]') +sepChoices = ['n','t','s'] +parser.add_option('-s', '--scalar', dest='scalar', action='extend', type='string', metavar = '', + help='list of single scalars to visualize') +parser.add_option( '--double', dest='double', action='extend', type='string', metavar = '', + help='list of two scalars to visualize') +parser.add_option( '--triple', dest='triple', action='extend', type='string', metavar = '', + help='list of three scalars to visualize') +parser.add_option( '--quadruple', dest='quadruple', action='extend', type='string', metavar = '', + help='list of four scalars to visualize') +parser.add_option('-v', '--vector', dest='vector', action='extend', type='string', metavar = '', + help='list of vectors to visualize') +parser.add_option('-t', '--tensor', dest='tensor', action='extend', type='string', metavar = '', + help='list of tensors to visualize') +parser.add_option('-d', '--deformation', dest='defgrad', action='store', type='string', metavar = 'string', + help='heading of deformation gradient columns [%default]') +parser.add_option('--reference', dest='undeformed', action='store_true', + help='map results to reference (undeformed) configuration [%default]') +parser.add_option('-c','--cell', dest='cell', action='store_true', + help='data is cell-centered [%default]') +parser.add_option('-p','--vertex', dest='cell', action='store_false', + help='data is vertex-centered') +parser.add_option('--mesh', dest='output_mesh', action='store_true', + help='produce VTK mesh file [%default]') +parser.add_option('--nomesh', dest='output_mesh', action='store_false', + help='omit VTK mesh file') +parser.add_option('--points', dest='output_points', action='store_true', + help='produce VTK points file [%default]') +parser.add_option('--nopoints', dest='output_points', action='store_false', + help='omit VTK points file') +parser.add_option('--separator', dest='separator', action='store', type='choice', choices=sepChoices, metavar='string', + help='data separator (%s) [t]'%(','.join(map(str,sepChoices)))) +parser.add_option('--scaling', dest='scaling', action='extend', type='string', + help='scaling of fluctuation', metavar = '') +parser.add_option('-u', '--unitlength', dest='unitlength', action='store', type='float', metavar = 'float', + help='set unit length for 2D model [%default]') +parser.add_option('--filenodalcoords', dest='filenodalcoords', action='store', type='string', metavar = 'string', + help='ASCII table containing nodal coords') +parser.add_option('--labelnodalcoords', dest='labelnodalcoords', action='store', type='string', nargs=3, + help='labels of nodal coords in ASCII table %default', metavar = 'string string string') +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(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.filenodalcoords != '': print 'cannot scale when reading coordinate from file' -options.separator = options.separator.lower() for filename in args: if not os.path.exists(filename): diff --git a/processing/post/addCalculation.py b/processing/post/addCalculation.py index 09a9bf26f..fbda3513a 100755 --- a/processing/post/addCalculation.py +++ b/processing/post/addCalculation.py @@ -25,10 +25,10 @@ Example: distance to IP coordinates -- "math.sqrt( #ip.x#**2 + #ip.y#**2 + round """, version = string.replace(scriptID,'\n','\\n') ) -parser.add_option('-l','--label', dest='labels', action='extend', type='string', \ - help='(list of) new column labels', metavar='') -parser.add_option('-f','--formula', dest='formulas', action='extend', type='string', \ - help='(list of) formulas corresponding to labels', metavar='') +parser.add_option('-l','--label', dest='labels', action='extend', type='string', metavar='', + help='(list of) new column labels') +parser.add_option('-f','--formula', dest='formulas', action='extend', type='string', metavar='', + help='(list of) formulas corresponding to labels') parser.set_defaults(labels= []) parser.set_defaults(formulas= []) diff --git a/processing/post/addCauchy.py b/processing/post/addCauchy.py index 5c8131d2b..a134b22ce 100755 --- a/processing/post/addCauchy.py +++ b/processing/post/addCauchy.py @@ -21,9 +21,9 @@ deformation gradient and first Piola--Kirchhoff stress. """, 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]') -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]') parser.set_defaults(defgrad = 'f') parser.set_defaults(stress = 'p') diff --git a/processing/post/addCompatibilityMismatch.py b/processing/post/addCompatibilityMismatch.py index d14d85bad..1a9f1a8c7 100755 --- a/processing/post/addCompatibilityMismatch.py +++ b/processing/post/addCompatibilityMismatch.py @@ -20,13 +20,13 @@ Operates on periodic ordered three-dimensional data sets. """, 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]') -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]') -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]') -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]') parser.set_defaults(noVolume = False) parser.set_defaults(noShape = False) diff --git a/processing/post/addCurl.py b/processing/post/addCurl.py index 8cc814799..da33fd9bf 100755 --- a/processing/post/addCurl.py +++ b/processing/post/addCurl.py @@ -22,11 +22,11 @@ Deals with both vector- and tensor-valued fields. """, 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]') -parser.add_option('-v','--vector', dest='vector', action='extend', type='string', metavar='', \ +parser.add_option('-v','--vector', dest='vector', action='extend', type='string', metavar='', help='heading of columns containing vector field values') -parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='', \ +parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='', help='heading of columns containing tensor field values') parser.set_defaults(coords = 'ip') parser.set_defaults(vector = []) diff --git a/processing/post/addDeformedConfiguration.py b/processing/post/addDeformedConfiguration.py index 8215c6182..fb340f7ba 100755 --- a/processing/post/addDeformedConfiguration.py +++ b/processing/post/addDeformedConfiguration.py @@ -21,11 +21,11 @@ Operates on periodic ordered three-dimensional data sets. """, 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]') -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') -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]') parser.set_defaults(coords = 'ip') parser.set_defaults(defgrad = 'f' ) diff --git a/processing/post/addDeterminant.py b/processing/post/addDeterminant.py index 471ac0c09..d9f8489e1 100755 --- a/processing/post/addDeterminant.py +++ b/processing/post/addDeterminant.py @@ -27,7 +27,7 @@ Add column(s) containing determinant of requested tensor column(s). """, version=string.replace(scriptID,'\n','\\n') ) -parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='', \ +parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='', help='heading of columns containing tensor field values') parser.set_defaults(tensor = []) diff --git a/processing/post/addDeviator.py b/processing/post/addDeviator.py index ca700c8f4..72fb87d9d 100755 --- a/processing/post/addDeviator.py +++ b/processing/post/addDeviator.py @@ -28,9 +28,9 @@ Add column(s) containing deviator of requested tensor column(s). """, version = string.replace(scriptID,'\n','\\n') ) -parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='', \ +parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='', 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)') parser.set_defaults(hydrostatic = False) parser.set_defaults(tensor = []) diff --git a/processing/post/addDivergence.py b/processing/post/addDivergence.py index 04a0bc329..961e93b9f 100755 --- a/processing/post/addDivergence.py +++ b/processing/post/addDivergence.py @@ -9,7 +9,7 @@ import damask scriptID = '$Id$' scriptName = scriptID.split()[1] -accuracyChoices = ['2','4','6','8'] + # -------------------------------------------------------------------- # MAIN @@ -23,15 +23,16 @@ Deals with both vector- and tensor-valued fields. """, version = string.replace(scriptID,'\n','\\n') ) -parser.add_option('--fdm', dest='accuracy', action='extend', type='string', metavar='', \ +accuracyChoices = ['2','4','6','8'] +parser.add_option('--fdm', dest='accuracy', action='extend', type='string', metavar='', 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') -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]') -parser.add_option('-v','--vector', dest='vector', action='extend', type='string', metavar='', \ +parser.add_option('-v','--vector', dest='vector', action='extend', type='string', metavar='', help='heading of columns containing vector field values') -parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='', \ +parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string', metavar='', help='heading of columns containing tensor field values') parser.set_defaults(coords = 'ip') parser.set_defaults(accuracy = []) @@ -43,10 +44,9 @@ parser.set_defaults(tensor = []) if len(options.vector) + len(options.tensor) == 0: parser.error('no data column specified...') - -for choice in options.accuracy: - if choice not in accuracyChoices: - parser.error('accuracy must be chosen from %s...'%(', '.join(accuracyChoices))) +if not set(options.accuracy).issubset(set(accuracyChoices)): + parser.error('accuracy must be chosen from %s...'%(', '.join(accuracyChoices))) + if options.fft: options.accuracy.append('FFT') if not options.accuracy: parser.error('no accuracy selected') diff --git a/processing/post/addEhkl.py b/processing/post/addEhkl.py index 93db8c0d6..6efcb2b9c 100755 --- a/processing/post/addEhkl.py +++ b/processing/post/addEhkl.py @@ -38,10 +38,10 @@ based on given cubic stiffness values C11, C12, and C44 in consecutive columns. """, version = string.replace(scriptID,'\n','\\n') ) -parser.add_option('-c','--stiffness', dest='vector', action='extend', type='string', metavar='', \ +parser.add_option('-c','--stiffness', dest='vector', action='extend', type='string', metavar='', help='heading of column containing C11 (followed by C12, C44) field values') 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') parser.set_defaults(vector = []) parser.set_defaults(hkl = [1,1,1]) diff --git a/processing/post/addEuclideanDistance.py b/processing/post/addEuclideanDistance.py index 65245796f..3a75f6d80 100755 --- a/processing/post/addEuclideanDistance.py +++ b/processing/post/addEuclideanDistance.py @@ -36,9 +36,10 @@ def periodic_3Dpad(array, rimdim=(1,1,1)): # -------------------------------------------------------------------- features = [ \ - {'aliens': 1, 'names': ['boundary','biplane'],}, - {'aliens': 2, 'names': ['tripleline',],}, - {'aliens': 3, 'names': ['quadruplepoint',],} + {'aliens': 1, 'name': 'biplane'}, + {'aliens': 1, 'name': 'boundary'}, + {'aliens': 2, 'name': 'tripleline'}, + {'aliens': 3, 'name': 'quadruplepoint'} ] neighborhoods = { @@ -87,34 +88,31 @@ boundaries, triple lines, and quadruple points. """, 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]') -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]') -parser.add_option('-t','--type', dest='type', action='extend', type='string', metavar='', \ - help='feature type (%s)'%(', '.join(map(lambda x:', '.join(x['names']),features)))) -parser.add_option('-n','--neighborhood',dest='neigborhood', action='store', type='string', metavar='int', \ - help='type of neighborhood (%s)'%(', '.join(neighborhoods.keys()))) -parser.set_defaults(coords = 'ip') +parser.add_option('-t','--type', dest='type', action='extend', type='string', metavar='', + help='feature type (%s)'%(', '.join(map(lambda x:', '.join([x['name']]),features)))) +parser.add_option('-n','--neighborhood',dest='neigborhood', action='store', type='choice', + choices=neighborhoods.keys(), metavar='string', + help='type of neighborhood (%s) [neumann]'%(', '.join(neighborhoods.keys()))) parser.set_defaults(type = []) +parser.set_defaults(coords = 'ip') parser.set_defaults(id = 'texture') parser.set_defaults(neighborhood = 'neumann') (options,filenames) = parser.parse_args() -options.neighborhood = options.neighborhood.lower() -if options.neighborhood not in neighborhoods: - parser.error('unknown neighborhood %s!'%options.neighborhood) - +if len(options.type) == 0: parser.error('please select a feature type') +if not set(options.type).issubset(set(map(lambda x: x['name'],features))): + 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 = [] for i,feature in enumerate(features): - for name in feature['names']: - for type in options.type: - if name.startswith(type): - feature_list.append(i) # remember valid features - break - + if feature['name'] in options.type: feature_list.append(i) # remember valid features # ------------------------------------------ setup file handles ----------------------------------- files = [] @@ -143,7 +141,7 @@ for file in files: # ------------------------------------------ assemble header --------------------------------------- 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()