def scalars_fromfile(f, n, sl): dataname = sl[0] datatype = sl[1].lower() assert datatype in [ "bit", "unsigned_char", "char", "unsigned_short", "short", "unsigned_int", "int", "unsigned_long", "long", "float", "double", ], ` datatype ` if len(sl) > 2: numcomp = eval(sl[2]) else: numcomp = 1 l = common._getline(f) l = l.split(" ") assert len(l) == 2 and l[0].lower() == "lookup_table" tablename = l[1] scalars = [] while len(scalars) < n: scalars += map(eval, common._getline(f).split(" ")) assert len(scalars) == n return Scalars(scalars, dataname, tablename)
def fromfile(self, filename, only_structure=0): filename = filename.strip() if filename[-4:] != '.vtk': filename += '.vtk' #print 'Reading file',`filename` f = open(filename, 'rb') l = f.readline() if not l.strip().replace(' ', '').lower() == '#vtkdatafileversion2.0': raise TypeError, 'File ' + ` filename ` + ' is not VTK 2.0 format' self.header = f.readline().rstrip() format = f.readline().strip().lower() if format not in ['ascii', 'binary']: raise ValueError, 'Expected ascii|binary but got %s' % ( ` format `) if format == 'binary': raise NotImplementedError, 'reading vtk binary format' l = common._getline(f).lower().split(' ') if l[0].strip() != 'dataset': raise ValueError, 'expected dataset but got %s' % (l[0]) try: ff = eval(l[1] + '_fromfile') except NameError: raise NotImplementedError, '%s_fromfile' % (l[1]) self.structure, l = ff(f, self) for i in range(2): if only_structure: break if not l: break l = [s.strip() for s in l.lower().split(' ')] assert len(l) == 2 and l[0] in ['cell_data', 'point_data'], l[0] data = l[0] n = eval(l[1]) lst = [] while 1: l = common._getline(f) if not l: break sl = [s.strip() for s in l.split()] k = sl[0].lower() if k not in [ 'scalars', 'color_scalars', 'lookup_table', 'vectors', 'normals', 'texture_coordinates', 'tensors', 'field' ]: break try: ff = eval(k + '_fromfile') except NameError: raise NotImplementedError, '%s_fromfile' % (k) lst.append(ff(f, n, sl[1:])) if data == 'point_data': self.point_data = PointData(*lst) if data == 'cell_data': self.cell_data = CellData(*lst) if self.point_data is None: self.point_data = PointData() if self.cell_data is None: self.cell_data = CellData() f.close()
def fromfile(self,filename, only_structure = 0): filename = filename.strip() if filename[-4:]!='.vtk': filename += '.vtk' #print 'Reading file',`filename` f = open(filename,'rb') l = f.readline() fileversion = l.strip().replace(' ','').lower() if not fileversion == '#vtkdatafileversion2.0': print 'File %s is not in VTK 2.0 format, got %s' % (filename, fileversion), print ' but continuing anyway..' self.header = f.readline().rstrip() format = f.readline().strip().lower() if format not in ['ascii','binary']: raise ValueError,'Expected ascii|binary but got %s'%(`format`) if format == 'binary': raise NotImplementedError,'reading vtk binary format' l = common._getline(f).lower().split(' ') if l[0].strip() != 'dataset': raise ValueError,'expected dataset but got %s'%(l[0]) try: ff = eval(l[1]+'_fromfile') except NameError: raise NotImplementedError,'%s_fromfile'%(l[1]) self.structure,l = ff(f,self) for i in range(2): if only_structure: break if not l: break l = [s.strip() for s in l.lower().split(' ')] assert len(l)==2 and l[0] in ['cell_data','point_data'], l[0] data = l[0] n = eval(l[1]) lst = [] while 1: l = common._getline(f) if not l: break sl = [s.strip() for s in l.split()] k = sl[0].lower() if k not in ['scalars','color_scalars','lookup_table','vectors', 'normals','texture_coordinates','tensors','field']: break try: ff = eval(k+'_fromfile') except NameError: raise NotImplementedError,'%s_fromfile'%(k) lst.append(ff(f,n,sl[1:])) if data == 'point_data': self.point_data = PointData(*lst) if data == 'cell_data': self.cell_data = CellData(*lst) if self.point_data is None: self.point_data = PointData() if self.cell_data is None: self.cell_data = CellData() f.close()
def structured_points_fromfile(f, self): l = common._getline(f).split(' ') assert l[0].strip().lower() == 'dimensions' dims = map(eval, l[1:]) assert len(dims) == 3 l = common._getline(f).split(' ') assert l[0].strip().lower() == 'origin' origin = map(eval, l[1:]) assert len(origin) == 3 l = common._getline(f).split(' ') assert l[0].strip().lower() == 'spacing' spacing = map(eval, l[1:]) assert len(spacing) == 3 return StructuredPoints(dims, origin, spacing), common._getline(f)
def structured_points_fromfile(f,self): l = common._getline(f).split(' ') assert l[0].strip().lower() == 'dimensions' dims = map(eval,l[1:]) assert len(dims)==3 l = common._getline(f).split(' ') assert l[0].strip().lower() == 'origin' origin = map(eval,l[1:]) assert len(origin)==3 l = common._getline(f).split(' ') assert l[0].strip().lower() == 'spacing' spacing = map(eval,l[1:]) assert len(spacing)==3 return StructuredPoints(dims,origin,spacing),common._getline(f)
def scalars_fromfile(f,n,sl): dataname = sl[0] datatype = sl[1].lower() assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` if len(sl)>2: numcomp = eval(sl[2]) else: numcomp = 1 l = common._getline(f) l = l.split(' ') assert len(l)==2 and l[0].lower() == 'lookup_table' tablename = l[1] scalars = [] while len(scalars) < n: scalars += map(eval,common._getline(f).split(' ')) assert len(scalars)==n return Scalars(scalars,dataname,tablename)
def normals_fromfile(f,n,sl): dataname = sl[0] datatype = sl[1].lower() assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` normals = [] while len(normals) < 3*n: normals += map(eval,common._getline(f).split(' ')) assert len(normals) == 3*n return Normals(normals,dataname)
def structured_grid_fromfile(f,self): l = common._getline(f).split(' ') assert l[0].strip().lower() == 'dimensions' dims = map(eval,l[1:]) assert len(dims)==3 l = common._getline(f) k,n,datatype = [s.strip().lower() for s in l.split(' ')] if k!='points': raise ValueError, 'expected points but got %s'%(`k`) n = eval(n) assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` points = [] self.message('\tgetting %s points'%n) while len(points) < 3*n: l = common._getline(f) points += map(eval,l.split(' ')) assert len(points)==3*n return StructuredGrid(dims,points),common._getline(f)
def polydata_fromfile(f, self): """Use VtkData(<filename>).""" points = [] vertices = [] lines = [] polygons = [] triangle_strips = [] l = common._getline(f) k, n, datatype = [s.strip().lower() for s in l.split(' ')] if k != 'points': raise ValueError, 'expected points but got %s' % ( ` k `) n = eval(n) assert datatype in [ 'bit', 'unsigned_char', 'char', 'unsigned_short', 'short', 'unsigned_int', 'int', 'unsigned_long', 'long', 'float', 'double' ], ` datatype ` self.message('\tgetting %s points' % n) while len(points) < 3 * n: l = common._getline(f) points += map(eval, l.split(' ')) assert len(points) == 3 * n while 1: l = common._getline(f) if l is None: break sl = l.split(' ') k = sl[0].strip().lower() if k not in ['vertices', 'lines', 'polygons', 'triangle_strips']: break assert len(sl) == 3 n, size = map(eval, [sl[1], sl[2]]) lst = [] while len(lst) < size: l = common._getline(f) lst += map(eval, l.split(' ')) assert len(lst) == size lst2 = [] j = 0 for i in range(n): lst2.append(lst[j + 1:j + lst[j] + 1]) j += lst[j] + 1 exec '%s = lst2' % k return PolyData(points, vertices, lines, polygons, triangle_strips), l
def vectors_fromfile(f,n,sl): dataname = sl[0] datatype = sl[1].lower() assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` vectors = [] while len(vectors) < 3*n: # vectors += map(eval,common._getline(f).split(' ')) vectors += map(eval,common._getline(f).split()) assert len(vectors) == 3*n return Vectors(vectors,dataname)
def rectilinear_grid_fromfile(f,self): l = common._getline(f).split(' ') assert l[0].strip().lower() == 'dimensions' dims = map(eval,l[1:]) assert len(dims)==3 for c in 'xyz': l = common._getline(f) k,n,datatype = [s.strip().lower() for s in l.split(' ')] if k!=c+'_coordinates': raise ValueError, 'expected %s_coordinates but got %s'%(c,`k`) n = eval(n) assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` points = [] while len(points) < n: points += map(eval,common._getline(f).split(' ')) assert len(points)==n exec '%s_coords = points'%c assert map(len,[x_coords,y_coords,z_coords]) == dims return RectilinearGrid(x_coords,y_coords,z_coords),common._getline(f)
def lookup_table_fromfile(f,n,sl): tablename = sl[0] size = eval(sl[1]) table = [] while len(table)<4*size: table += map(eval,common._getline(f).split(' ')) assert len(table) == 4*size table2 = [] for i in range(0,len(table),4): table2.append(table[i:i+4]) return LookupTable(table2,tablename)
def scalars_fromfile(f, n, sl): dataname = sl[0] datatype = sl[1].lower() assert datatype in [ 'bit', 'unsigned_char', 'char', 'unsigned_short', 'short', 'unsigned_int', 'int', 'unsigned_long', 'long', 'float', 'double' ], ` datatype ` if len(sl) > 2: numcomp = eval(sl[2]) else: numcomp = 1 l = common._getline(f) l = l.split(' ') assert len(l) == 2 and l[0].lower() == 'lookup_table' tablename = l[1] scalars = [] while len(scalars) < n: scalars += map(eval, common._getline(f).split(' ')) assert len(scalars) == n return Scalars(scalars, dataname, tablename)
def lookup_table_fromfile(f, n, sl): tablename = sl[0] size = eval(sl[1]) table = [] while len(table) < 4 * size: table += map(eval, common._getline(f).split(' ')) assert len(table) == 4 * size table2 = [] for i in range(0, len(table), 4): table2.append(table[i:i + 4]) return LookupTable(table2, tablename)
def polydata_fromfile(f,self): """Use VtkData(<filename>).""" points = [] vertices = [] lines = [] polygons = [] triangle_strips = [] l = common._getline(f) k,n,datatype = [s.strip().lower() for s in l.split(' ')] if k!='points': raise ValueError, 'expected points but got %s'%(`k`) n = eval(n) assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` self.message('\tgetting %s points'%n) while len(points) < 3*n: l = common._getline(f) points += map(eval,l.split(' ')) assert len(points)==3*n while 1: l = common._getline(f) if l is None: break sl = l.split(' ') k = sl[0].strip().lower() if k not in ['vertices','lines','polygons','triangle_strips']: break assert len(sl)==3 n,size = map(eval,[sl[1],sl[2]]) lst = [] while len(lst) < size: l = common._getline(f) lst += map(eval,l.split(' ')) assert len(lst)==size lst2 = [] j = 0 for i in range(n): lst2.append(lst[j+1:j+lst[j]+1]) j += lst[j]+1 exec '%s = lst2'%k return PolyData(points,vertices,lines,polygons,triangle_strips),l
def color_scalars_fromfile(f, n, sl): assert len(sl) == 2 dataname = sl[0].strip() nvals = eval(sl[1]) scalars = [] while len(scalars) < nvals * n: scalars += map(eval, common._getline(f).split(' ')) assert len(scalars) == nvals * n scalars2 = [] for i in range(0, len(scalars), nvals): scalars2.append(scalars[i:i + nvals]) return ColorScalars(scalars2, dataname)
def field_fromfile(f,n,sl): dataname = sl[0] numarrays = eval(sl[1]) dict = {} for i in range(numarrays): l = common._getline(f).split(' ') assert len(l)==4,`l` name = l[0].strip() numcomps = eval(l[1]) numtuples = eval(l[2]) datatype = l[3].lower() assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` arr = [] while len(arr)<numcomps*numtuples: arr += map(eval,common._getline(f).split(' ')) assert len(arr)==numcomps*numtuples arr2 = [] for j in range(0,numtuples*numcomps,numcomps): arr2.append(arr[j:j+numcomps]) dict[name] = arr2 return Field(dataname,**dict)
def color_scalars_fromfile(f,n,sl): assert len(sl)==2 dataname = sl[0].strip() nvals = eval(sl[1]) scalars = [] while len(scalars)<nvals*n: scalars += map(eval,common._getline(f).split(' ')) assert len(scalars)==nvals*n scalars2 = [] for i in range(0,len(scalars),nvals): scalars2.append(scalars[i:i+nvals]) return ColorScalars(scalars2,dataname)
def tensors_fromfile(f,n,sl): assert len(sl)==2 dataname = sl[0].strip() datatype = sl[1].strip().lower() assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` arr = [] while len(arr)<9*n: arr += map(eval,common._getline(f).split(' ')) assert len(arr)==9*n arr2 = [] for i in range(0,len(arr),9): arr2.append(tuple(map(tuple,[arr[i:i+3],arr[i+3:i+6],arr[i+6:i+9]]))) return Tensors(arr2,dataname)
def texture_coordinates_fromfile(f,n,sl): assert len(sl)==3 dataname = sl[0].strip() dim = eval(sl[1]) datatype = sl[2].strip().lower() assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` arr = [] while len(arr)<dim*n: arr += map(eval,common._getline(f).split(' ')) assert len(arr)==dim*n arr2 = [] for i in range(0,len(arr),dim): arr2.append(arr[i:i+dim]) return TextureCoordinates(arr2,dataname)
def unstructured_grid_fromfile(f, self): l = common._getline(f) k, n, datatype = [s.strip().lower() for s in l.split()] if k != 'points': raise ValueError, 'expected points but got %s' % ( ` k `) n = eval(n) assert datatype in [ 'bit', 'unsigned_char', 'char', 'unsigned_short', 'short', 'unsigned_int', 'int', 'unsigned_long', 'long', 'float', 'double' ], ` datatype ` points = [] self.message('\tgetting %s points' % n) while len(points) < 3 * n: points += map(eval, common._getline(f).split()) assert len(points) == 3 * n l = common._getline(f).split() assert len(l) == 3 and l[0].strip().lower() == 'cells', ` l ` n = eval(l[1]) size = eval(l[2]) lst = [] self.message('\tgetting %s cell indexes' % size) while len(lst) < size: line = common._getline(f) lst += map(eval, line.split()) assert len(lst) == size lst2 = [] j = 0 for i in range(n): lst2.append(lst[j + 1:j + lst[j] + 1]) j += lst[j] + 1 l = common._getline(f).split() assert len(l) == 2 and l[0].strip().lower() == 'cell_types' and eval( l[1]) == n, ` l ` tps = [] self.message('\tgetting %s cell types' % n) while len(tps) < n: tps += map(eval, common._getline(f).split()) assert len(tps) == n dict = {} for i, t in zip(lst2, tps): k = UnstructuredGrid._vtk_cell_types_imap[t] if not dict.has_key(k): dict[k] = [] dict[k].append(i) self.message('\tdone') return UnstructuredGrid(points, **dict), common._getline(f)
def tensors_fromfile(f, n, sl): assert len(sl) == 2 dataname = sl[0].strip() datatype = sl[1].strip().lower() assert datatype in [ 'bit', 'unsigned_char', 'char', 'unsigned_short', 'short', 'unsigned_int', 'int', 'unsigned_long', 'long', 'float', 'double' ], ` datatype ` arr = [] while len(arr) < 9 * n: arr += map(eval, common._getline(f).split(' ')) assert len(arr) == 9 * n arr2 = [] for i in range(0, len(arr), 9): arr2.append( tuple( map(tuple, [arr[i:i + 3], arr[i + 3:i + 6], arr[i + 6:i + 9]]))) return Tensors(arr2, dataname)
def unstructured_grid_fromfile(f,self): l = common._getline(f) k,n,datatype = [s.strip().lower() for s in l.split()] if k!='points': raise ValueError, 'expected points but got %s'%(`k`) n = eval(n) assert datatype in ['bit','unsigned_char','char','unsigned_short','short','unsigned_int','int','unsigned_long','long','float','double'],`datatype` points = [] self.message('\tgetting %s points'%n) while len(points) < 3*n: points += map(eval,common._getline(f).split()) assert len(points)==3*n l = common._getline(f).split() assert len(l)==3 and l[0].strip().lower() == 'cells',`l` n = eval(l[1]) size = eval(l[2]) lst = [] self.message('\tgetting %s cell indexes'%size) while len(lst) < size: line = common._getline(f) lst += map(eval,line.split()) assert len(lst)==size lst2 = [] j = 0 for i in range(n): lst2.append(lst[j+1:j+lst[j]+1]) j += lst[j]+1 l = common._getline(f).split() assert len(l)==2 and l[0].strip().lower() == 'cell_types' and eval(l[1])==n,`l` tps = [] self.message('\tgetting %s cell types'%n) while len(tps) < n: tps += map(eval,common._getline(f).split()) assert len(tps)==n dict = {} for i,t in zip(lst2,tps): k = UnstructuredGrid._vtk_cell_types_imap[t] if not dict.has_key(k): dict[k] = [] dict[k].append(i) self.message('\tdone') return UnstructuredGrid(points,**dict),common._getline(f)