2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2014-03-04 09:04:34 +05:30
|
|
|
|
2016-03-03 19:23:55 +05:30
|
|
|
import os,string,vtk
|
|
|
|
import numpy as np
|
2014-03-04 09:04:34 +05:30
|
|
|
import damask
|
2016-03-03 19:23:55 +05:30
|
|
|
from optparse import OptionParser
|
2014-03-04 09:04:34 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2014-03-04 09:04:34 +05:30
|
|
|
|
|
|
|
scalingFactor = { \
|
|
|
|
'm': {
|
|
|
|
'm': 1e0,
|
|
|
|
'mm': 1e-3,
|
|
|
|
'µm': 1e-6,
|
|
|
|
},
|
|
|
|
'mm': {
|
|
|
|
'm': 1e+3,
|
|
|
|
'mm': 1e0,
|
|
|
|
'µm': 1e-3,
|
|
|
|
},
|
|
|
|
'µm': {
|
|
|
|
'm': 1e+6,
|
|
|
|
'mm': 1e+3,
|
|
|
|
'µm': 1e0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-03-03 19:23:55 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2014-03-04 09:04:34 +05:30
|
|
|
Produce VTK rectilinear grid from Gwyddion dataset exported as text.
|
|
|
|
""" + string.replace(scriptID,'\n','\\n')
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_option('-s', '--scaling', dest='scaling', type='float',
|
|
|
|
help = 'scaling factor for elevation data [auto]')
|
|
|
|
|
|
|
|
parser.set_defaults(scaling = 0.0)
|
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------ read Gwyddion data ---------------------------------------
|
|
|
|
|
|
|
|
for file in filenames:
|
|
|
|
with open(file,'r') as f:
|
|
|
|
for line in f:
|
|
|
|
pieces = line.split()
|
|
|
|
if pieces[0] != '#': break
|
|
|
|
if len(pieces) < 2: continue
|
|
|
|
if pieces[1] == 'Width:':
|
|
|
|
width = float(pieces[2])
|
|
|
|
lateralunit = pieces[3]
|
|
|
|
if pieces[1] == 'Height:':
|
|
|
|
height = float(pieces[2])
|
|
|
|
lateralunit = pieces[3]
|
|
|
|
if pieces[1] == 'Value' and pieces[2] == 'units:':
|
|
|
|
elevationunit = pieces[3]
|
|
|
|
|
|
|
|
if options.scaling == 0.0:
|
|
|
|
options.scaling = scalingFactor[lateralunit][elevationunit]
|
|
|
|
|
2016-03-03 19:23:55 +05:30
|
|
|
elevation = np.loadtxt(file)*options.scaling
|
2014-03-04 09:04:34 +05:30
|
|
|
|
|
|
|
grid = vtk.vtkRectilinearGrid()
|
|
|
|
grid.SetDimensions(elevation.shape[1],elevation.shape[0],1)
|
|
|
|
|
|
|
|
xCoords = vtk.vtkDoubleArray()
|
2016-03-03 19:23:55 +05:30
|
|
|
for x in np.arange(0.0,width,width/elevation.shape[1],'d'):
|
2014-03-04 09:04:34 +05:30
|
|
|
xCoords.InsertNextValue(x)
|
|
|
|
yCoords = vtk.vtkDoubleArray()
|
2016-03-03 19:23:55 +05:30
|
|
|
for y in np.arange(0.0,height,height/elevation.shape[0],'d'):
|
2014-03-04 09:04:34 +05:30
|
|
|
yCoords.InsertNextValue(y)
|
|
|
|
zCoords = vtk.vtkDoubleArray()
|
|
|
|
zCoords.InsertNextValue(0.0)
|
|
|
|
|
|
|
|
grid.SetXCoordinates(xCoords)
|
|
|
|
grid.SetYCoordinates(yCoords)
|
|
|
|
grid.SetZCoordinates(zCoords)
|
|
|
|
|
|
|
|
vector = vtk.vtkFloatArray()
|
|
|
|
vector.SetName("elevation");
|
|
|
|
vector.SetNumberOfComponents(3);
|
2016-03-03 19:23:55 +05:30
|
|
|
vector.SetNumberOfTuples(np.prod(elevation.shape));
|
|
|
|
for i,z in enumerate(np.ravel(elevation)):
|
2014-03-04 09:04:34 +05:30
|
|
|
vector.SetTuple3(i,0,0,z)
|
|
|
|
|
|
|
|
grid.GetPointData().AddArray(vector)
|
|
|
|
|
|
|
|
writer = vtk.vtkXMLRectilinearGridWriter()
|
|
|
|
writer.SetDataModeToBinary()
|
|
|
|
writer.SetCompressorTypeToZLib()
|
|
|
|
writer.SetFileName(os.path.splitext(file)[0]+'.vtr')
|
|
|
|
if vtk.VTK_MAJOR_VERSION <= 5:
|
|
|
|
writer.SetInput(grid)
|
|
|
|
else:
|
|
|
|
writer.SetInputData(grid)
|
|
|
|
writer.Write()
|