Skip to content

Commit fe00479

Browse files
committed
written from_grid and native for dx and mrc
1 parent b29c1f4 commit fe00479

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

gridData/OpenDX.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,33 @@ def __init__(self,classid='0',components=None,comments=None):
523523
self.components = components
524524
self.comments= comments
525525

526+
@staticmethod
527+
def from_grid(grid, type=None, typequote='"', **kwargs):
528+
comments = [
529+
"OpenDX density file written by gridDataFormats.Grid.export()",
530+
"File format: http://opendx.sdsc.edu/docs/html/pages/usrgu068.htm#HDREDF",
531+
"Data are embedded in the header and tied to the grid positions.",
532+
"Data is written in C array order: In grid[x,y,z] the axis z is fastest",
533+
"varying, then y, then finally x, i.e. z is the innermost loop.",
534+
]
535+
if grid.metadata:
536+
comments.append("Meta data stored with the python Grid object:")
537+
for k in grid.metadata:
538+
comments.append(" " + str(k) + " = " + str(grid.metadata[k]))
539+
comments.append("(Note: the VMD dx-reader chokes on comments below this line)")
540+
541+
components = dict(
542+
positions=gridpositions(1, grid.grid.shape, grid.origin, grid.delta),
543+
connections=gridconnections(2, grid.grid.shape),
544+
data=array(3, grid.grid, type=type, typequote=typequote),
545+
)
546+
dx_field = field('density', components=components, comments=comments)
547+
return dx_field
548+
549+
@property
550+
def native(self):
551+
return self
552+
526553
def _openfile_writing(self, filename):
527554
"""Returns a regular or gz file stream for writing"""
528555
if filename.endswith('.gz'):

gridData/mrc.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ def __init__(self, filename=None, assume_volumetric=False):
100100
self.filename = filename
101101
if filename is not None:
102102
self.read(filename, assume_volumetric=assume_volumetric)
103+
104+
@staticmethod
105+
def from_grid(grid, **kwargs):
106+
mrc_obj = MRC()
107+
108+
mrc_obj.array = grid.grid
109+
mrc_obj.delta = np.diag(grid.delta)
110+
mrc_obj.origin = grid.origin
111+
mrc_obj.rank = 3
112+
113+
if hasattr(grid, "_mrc_header"):
114+
mrc_obj.header = grid._mrc_header
115+
116+
return mrc_obj
117+
118+
@property
119+
def native(self):
120+
"""Native object is the MRC wrapper itself."""
121+
return self
103122

104123
def read(self, filename, assume_volumetric=False):
105124
"""Populate the instance from the MRC/CCP4 file *filename*."""

0 commit comments

Comments
 (0)