Hi Mario. With 'proper' stack files (files which distinguish between sets of 2-D images and 3-D volumes, and have per-image headers, ie - every single format other than MRC), you can write to arbitrary positions in the file. However, when stacking 2-D particles into a 3-D volume, the volume needs to have the correct dimensions before you insert.
>>>> #!/usr/local/EMAN2_daily_20130122/Python/bin/python
>>>> from EMAN2 import *
>>>> import sys
>>>> def main():
>>>> tmp=EMData()
>>>> e=EMData()
>>>> for image_file in sys.argv[1:]:
>>>> print "Este %d" % e.get_zsize()
>>>> if e.get_xsize() == 0:
>>>> e.read_image(image_file)
>>>> e.append_image("one_by_one.hdf",EMUtil.ImageType.IMAGE_HDF,False)
>>>> else:
>>>> e.set_size(e.get_xsize(),e.get_ysize(),e.get_zsize()+1)
>>>> tmp.read_image(image_file)
>>>> tmp.append_image("one_by_one.hdf",EMUtil.ImageType.IMAGE_HDF,False)
>>>> e.insert_clip(tmp,(0,0,e.get_zsize()-1))
>>>>
>>>>
>>>>
>>>> display(e)
>>>> e.write_image("all_at_once.hdf",0,EMUtil.ImageType.IMAGE_HDF,False)
>>>>
>>>>
>>>>
>>>> if __name__=="__main__":
>>>> main()
This is a bit overcomplicated. Let's start with your code, and make it simpler and create a volume stack in RAM:
#!/usr/local/EMAN2_daily_20130122/Python/bin/python
from EMAN2 import *
import sys
def main():
# get the size of the first input (assume the rest are the same size)
hdr=EMData(sys.argv[1:])
nx=hdr["nx"]
ny=hdr["ny"]
# create an empty output volume
outvol=EMData(nx,ny,len(sys.argv)-1)
# loop over the input files (assume each file has 1 image)
for z,image_file in enumerate(sys.argv[1:]):
im=EMData(image_file)
#im.process_inplace(....) # if you want to do something to the image
outvol.insert_clip(im,(0,0,z))
display(outvol)
outvol.write("output.mrc")
if __name__=="__main__":
main()
==============
Now, you could do the same thing by writing to the output file slice by slice. This would appear to avoid the problem of keeping the entire stack in memory while you construct it, however, it doesn't entirely. The trick is that the MRC output volume must be big enough to contain the full stack before you write to it. It will not get bigger dynamically as you insert slices. In essence, this means you have to create an empty volume of the appropriate size in RAM then write it to disk at the beginning. We should have a better solution for this, but frankly until very recently (tomography in IMOD), we have avoided MRC 'stack' files like the plague. They are poorly standardized (no good way to detect a 'stack' vs a 'volume', non-standard 'extended headers' abound, ...), don't have ANY per-image header data, etc. We support them, like we support everything else, but don't like them much.
Note that for any other EMAN supported cryoEM format this would become, the much simpler:
#!/usr/local/EMAN2_daily_20130122/Python/bin/python
from EMAN2 import *
import sys
def main():
for image_file in sys.argv[1:]:
im=EMData(image_file)
#im.process_inplace(....) # if you want to do something to the image
im.write_image("outfile.spi",-1) # -1 appends
if __name__=="__main__":
main()
----------------------------------------------------------------------------
Steven Ludtke, Ph.D.
Professor, Dept of Biochemistry and Mol. Biol. (
www.bcm.edu/biochem)
Co-Director National Center For Macromolecular Imaging (
ncmi.bcm.edu)
Co-Director CIBR Center (
www.bcm.edu/research/cibr)
Baylor College of Medicine
slu...@bcm.edu