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 __init__(self, *args, **kws): assert args, 'expected at least one argument' if type(args[0]) is types.StringType: if kws.has_key('only_structure') and kws['only_structure']: self.fromfile(args[0], 1) else: self.fromfile(args[0]) return else: structure = args[0] args = list(args)[1:] if not common.is_dataset(structure): raise TypeError, 'argument structure must be StructuredPoints|StructuredGrid|UnstructuredGrid|RectilinearGrid|PolyData but got %s' % ( type(structure)) self.structure = structure for a in args: if common.is_string(a): if len(a) > 255: self.skipping('striping header string to a length =255') self.header = a[:255] elif common.is_pointdata(a): self.point_data = a elif common.is_celldata(a): self.cell_data = a else: self.skipping('unexpexted argument %s' % (type(a))) if self.header is None: self.header = 'Really cool data' self.warning('Using header=%s' % ( ` self.header `)) if self.point_data is None and self.cell_data is None: self.warning('No data defined') if self.point_data is not None: s = self.structure.get_size() s1 = self.point_data.get_size() if s1 != s: raise ValueError, 'DataSet (size=%s) and PointData (size=%s) have different sizes' % ( s, s1) else: self.point_data = PointData() if self.cell_data is not None: s = self.structure.get_cell_size() s1 = self.cell_data.get_size() if s1 != s: raise ValueError, 'DataSet (cell_size=%s) and CellData (size=%s) have different sizes' % ( s, s1) else: self.cell_data = CellData()