Scripts: volumecenter.py

File volumecenter.py, 1.3 KB (added by goddard, 15 years ago)
Line 
1#
2# Place a marker at the center of mass of a map for the region above the
3# displayed contour level.
4#
5# Open a map file and then open this script to place the center marker.
6#
7# To place markers on more than one map, choose the map in the volume dialog
8# by clicking on its histogram or name (so the name above the histogram is
9# highlighted) and then open this script (File / Open...).
10#
11
12# Get highlighted volume from volume dialog
13import VolumeViewer as VV
14v = VV.active_volume()
15
16# Find highest displayed contour level.
17level = max(v.surface_levels)
18
19# Get 3-d array of map values.
20m = v.data.full_matrix()
21
22# Find indices of map values above displayed threshold.
23kji = (m >= level).nonzero()
24
25# Compute total mass above threshold.
26msum = m[kji].sum()
27
28# Compute mass-weighted center
29center = [(i*m[kji]).sum()/msum for i in kji]
30center.reverse() # k,j,i -> i,j,k index order
31print 'Center of mass grid index for', v.name, center
32
33# Convert index units to physical units.
34c = v.data.ijk_to_xyz(center)
35
36# Create marker model with same id number as volume.
37import VolumePath as VP
38mset = VP.Marker_Set(v.name + ' center')
39mset.marker_molecule(model_id = (v.id, v.subid))
40
41# Place marker at center position.
42color = (1,1,0,1) # (red, green, blue, opacity).
43mset.place_marker(c, rgba = color, radius = min(v.data.step))