def saveNewMrc(mrc_path, arr, n_tzcyx, cal_xyz, wavelengths=None): """ Write a new Mrc file using numpy ndarray 'arr' and tuples of - dimension sizes (nt, nz, nc, ny, nx) and - float pixel calibrations in microns (cal_x, cal_y, cal_z) """ nt, nz, nc, ny, nx = n_tzcyx if not wavelengths: wavelengths = tuple(900 + n for n in range(5)) arr = numpy.reshape(arr, n_tzcyx) # introduce length 1 dimensions arr = arr.transpose([0, 2, 1, 3, 4]) # Mrc output shape "ZWT" hdr = Mrc.makeHdrArray() mrc_mode = Mrc.dtype2MrcMode(arr.dtype) nslices = nt * nz * nc Mrc.init_simple(hdr, mrc_mode, (nslices, ny, nx)) # set default hdr values hdr.NumTimes = nt hdr.NumWaves = nc hdr.ImgSequence = 2 # write in order "ZWT" hdr.d = cal_xyz hdr.wave = wavelengths # write header & slices f_out = file(mrc_path, 'wb') f_out.write(hdr._array.tostring()) for t in range(nt): for c in range(nc): for z in range(nz): arr_yx_yinverted = arr[t, c, z, ::-1, :] f_out.write(arr_yx_yinverted.copy(order='C')) f_out.close() return DataDoc(mrc_path)
def makeHdrFromRdr(rdr): """ rdr: reader object return header """ if hasattr(rdr, 'hdr'): hdr = rdr.hdr else: hdr = Mrc.makeHdrArray() Mrc.init_simple(hdr, Mrc.dtype2MrcMode(rdr.dtype), rdr.shape) hdr.ImgSequence = rdr.imgSequence hdr.NumTimes = rdr.nt hdr.NumWaves = rdr.nw hdr.Num[-1] = rdr.nt * rdr.nw * rdr.nz if len(rdr.wave): if [ 1 for wave in rdr.wave[:rdr.nw] if isinstance(wave, basestring) ]: hdr.wave[:rdr.nw] = 0 else: hdr.wave[:rdr.nw] = rdr.wave[:rdr.nw] hdr.d = rdr.pxlsiz[::-1] if rdr.metadata.has_key('Instrument'): hdr.LensNum = eval( rdr.metadata['Instrument']['Objective']['ID'].split(':')[1]) return hdr
def makeHdr(self): """ make a Mrc header using the available dimension information to export """ if not self.hdr: hdr = Mrc.makeHdrArray() Mrc.init_simple(hdr, Mrc.dtype2MrcMode(self.dtype), self.shape) hdr.ImgSequence = self.imgSequence hdr.NumTimes = self.nt hdr.NumWaves = self.nw hdr.Num[-1] = self.nt * self.nw * self.nz if len(self.wave): hdr.wave[:self.nw] = self.wave[:self.nw] hdr.d = self.pxlsiz[::-1] if 'Instrument' in self.metadata: hdr.hdr.LensNum = eval(self.metadata['Instrument']['Objective']['ID'].split(':')[1]) self.hdr = hdr
def makeHdrFromRdr(rdr): """ rdr: reader object return header """ if hasattr(rdr, 'hdr'): hdr = makeHdr_like(rdr.hdr) else: hdr = Mrc.makeHdrArray() Mrc.init_simple(hdr, Mrc.dtype2MrcMode(rdr.dtype), rdr.shape) hdr.ImgSequence = rdr.imgSequence hdr.NumTimes = rdr.nt hdr.NumWaves = rdr.nw hdr.Num[-1] = rdr.nt * rdr.nw * rdr.nz if len(rdr.wave): if [1 for wave in rdr.wave[:rdr.nw] if isinstance(wave, six.string_types)]: hdr.wave[:rdr.nw] = 0 else: hdr.wave[:rdr.nw] = rdr.wave[:rdr.nw] hdr.d = rdr.pxlsiz[::-1] if 'Instrument' in rdr.metadata: hdr.LensNum = eval(rdr.metadata['Instrument']['Objective']['ID'].split(':')[1]) return hdr