Scripts: insightgrid.py

File insightgrid.py, 1.7 KB (added by goddard, 12 years ago)
Line 
1# Insight II grid file format as described in
2#
3# http://hincklab.uthscsa.edu/html/soft_packs/msi_docs/insight980/insight/Utilities.fcs.html
4#
5# Example file header
6#
7# RBT FFT GRID
8# (1F15.10)
9# 20.500 17.500 18.500 90.000 90.000 90.000
10# 41 35 37
11# 1 -33 8 -35 0 -5 32
12# 0.0000000000
13# 0.0000000000
14# ...
15#
16def read_insight_2_grid(path):
17
18 f = open(path, 'r')
19 h = f.readline()
20 if not h.startswith('RBT FFT GRID'):
21 f.close()
22 raise SyntaxError('Bad format: file %s does not start with line "RBT FFT GRID"' % path)
23 valfmt = f.readline()
24 cell = f.readline()
25 alen,blen,clen,alpha,beta,gamma = tuple(float(x) for x in cell.split()) # Unit cell params
26 size = f.readline()
27 asize,bsize,csize = tuple(int(i)+1 for i in size.split())
28 origin = f.readline()
29 fastaxis,a0,a1,b0,b1,c0,c1 = tuple(int(i) for i in origin.split())
30 if fastaxis != 1:
31 raise SyntaxError('File %s, cannot read data with y-axis varying fastest.' % path)
32 from numpy import empty, float32
33 a = empty((csize,bsize,asize), float32)
34 from VolumeData import readarray
35 readarray.read_float_lines(f, a, None)
36 from VolumeData import Array_Grid_Data
37 from os.path import basename
38 step = (alen/(asize-1), blen/(bsize-1), clen/(csize-1))
39 orig = (a0*step[0], b0*step[1], c0*step[2])
40 angles = (alpha, beta, gamma)
41 g = Array_Grid_Data(a, orig, step, angles, name = basename(path))
42 import VolumeViewer
43 v = VolumeViewer.volume_from_grid_data(g)
44
45if len(arguments) == 1:
46 from os import path
47 p = path.expanduser(arguments[0])
48 read_insight_2_grid(p)
49else:
50 from chimera import replyobj
51 replyobj.status('runscript insightgrid.py <filename>')