Package Volume :: Package IO :: Module dxReader
[hide private]
[frames] | no frames]

Source Code for Module Volume.IO.dxReader

 1  import Numeric 
 2  from string import split 
 3  from  Volume.Grid3D import Grid3DF, Grid3DSI, Grid3DUC 
 4   
5 -class ReadDX:
6
7 - def __init__(self):
8 self.header = {} 9 self.filename = None
10 11
12 - def read(self, filename, normalize):
13 self.filename = filename 14 data = self.readAllLines(filename) 15 return self.parse(data, normalize)
16 17
18 - def readAllLines(self, filename):
19 # open file to read 20 f = open(filename, 'r') 21 data = f.readlines() 22 f.close() 23 return data
24 25
26 - def parse(self, data, normalize):
27 28 h = self.header = {} 29 30 # read header (need some specs here) 31 w = split(data[4]) 32 nx, ny, nz = int(w[5]), int(w[6]), int(w[7]) 33 h['nx']=nx; h['ny']=ny; h['nz']=nz 34 35 w = split(data[5]) 36 ox, oy, oz = float(w[1]), float(w[2]), float(w[3]) 37 h['origin']= (ox, oy, oz) 38 39 w = split(data[6]) 40 h['stepx'] = sx = float(w[1]) 41 42 w = split(data[7]) 43 h['stepy'] = sy = float(w[2]) 44 45 w = split(data[8]) 46 h['stepz'] = sz = float(w[3]) 47 48 self.data = array = Numeric.zeros( (nx,ny,nz), Numeric.Float32) 49 values = map(split, data[11:-5]) 50 ind=0 51 size = nx*ny*nz 52 for line in values: 53 if ind>=size: 54 break 55 l = len(line) 56 array.flat[ind:ind+l] = map(float, line) 57 ind = ind + l 58 59 stepSize = [h['stepx'], h['stepy'], h['stepz']] 60 volume = Grid3DF(self.data, h['origin'], stepSize, h) 61 return volume
62 63
64 - def describe(self):
65 print "DX file: ", self.filename 66 print "nx= ", self.header['nx'] 67 print "ny= ", self.header['ny'] 68 print "nz= ", self.header['nz'] 69 #print "xlen= ", self.header['xlen'] 70 #print "ylen= ", self.header['ylen'] 71 #print "zlen= ", self.header['zlen'] 72 print "min= ", min(self.data.flat) 73 print "max= ", max(self.data.flat) 74 #print "mean= ", self.header['amean'] 75 print "origin= ", self.header['origin']
76 77 78 if __name__=='__main__': 79 reader = ReadDX() 80 header, vol = reader.read("pot-0mM.dx") 81 reader.describe() 82