[3dem] help on python script to open mrc file

David Stokes stokes at nyu.edu
Mon Sep 5 07:37:42 PDT 2011


  I think this should works for standard MRC files:

#!/usr/bin/python

# read an mrc file
from numpy import *
import struct

class mrc_image:
     def __init__(self,filename):
         self.numbytes1=56           # 56 long ints
         self.numbytes2=80*10          # 10 header lines of 80 chars each
         self.filename=filename

     def read(self):
         input_image=open(self.filename,'rb')
         self.header1=input_image.read(self.numbytes1*4)
         self.header2=input_image.read(self.numbytes2)
         byte_pattern='=' + 'l'*self.numbytes1   #'=' required to get machine independent standard size
         self.dim=struct.unpack(byte_pattern,self.header1)[:3]   #(dimx,dimy,dimz)
         self.imagetype=struct.unpack(byte_pattern,self.header1)[3]  #0: 8-bit signed, 1:16-bit 
signed, 2: 32-bit float, 6: unsigned 16-bit (non-std)
         if (self.imagetype == 0):
             imtype='b'
         elif (self.imagetype ==1):
             imtype='h'
         elif (self.imagetype ==2):
             imtype='f4'
         elif (self.imagetype ==6):
             imtype='H'
         else:
             type='unknown'   #should put a fail here
         input_image_dimension=(self.dim[1],self.dim[0])  #2D images assumed
         
self.image_data=fromfile(file=input_image,dtype=imtype,count=self.dim[0]*self.dim[1]).reshape(input_image_dimension)
         input_image.close()


On 09/05/2011 09:31 AM, C.J. wrote:
> Dear all,
>
> I want to write a python script to process mrc file.
> But I don't to how to open the mrc file. Is the mrc file text format or other?
> I would be appreciated of you if you would help me with the codes.
>

-- 
David L. Stokes
Skirball Institute, NYU Medical Center
tel: 212-263-1580
http://saturn.med.nyu.edu/~stokes
----
New York Structural Biology Center
tel: 212-939-0660 x9116
http://cryoem.nysbc.org/
fax: 646-219-0300



More information about the 3dem mailing list