EMAN2
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes
EMAN::MrcIO Class Reference

MRC file = header + data (nx x ny x nz). More...

#include <mrcio.h>

Inheritance diagram for EMAN::MrcIO:
Inheritance graph
[legend]
Collaboration diagram for EMAN::MrcIO:
Collaboration graph
[legend]

List of all members.

Classes

struct  FeiMrcExtHeader
 The extended header used by Fei MRC image. More...
struct  FeiMrcHeader
 Extended MRC format for tomography As used by Fei; original definition of extended header by Dave Agard and Bram Koster Contact Dustin Morado <Dustin.Morado@uth.tmc.edu> for details. More...
struct  MrcHeader

Public Member Functions

 MrcIO (const string &filename, IOMode rw_mode=READ_ONLY)
 ~MrcIO ()
int read_ctf (Ctf &ctf, int image_index=0)
 Read CTF data from this image.
void write_ctf (const Ctf &ctf, int image_index=0)
 Write CTF data to this image.
int get_nimg ()
 Return the number of images in this image file.

Static Public Member Functions

static bool is_valid (const void *first_block, off_t file_size=0)
static int get_mode_size (int mm)
static int to_em_datatype (int mrcmode)
static int to_mrcmode (int em_datatype, int is_complex)

Public Attributes

 DEFINE_IMAGEIO_FUNC
MrcHeader mrch
FeiMrcHeader feimrch

Private Types

enum  MrcMode {
  MRC_UCHAR = 0, MRC_SHORT, MRC_FLOAT, MRC_SHORT_COMPLEX,
  MRC_FLOAT_COMPLEX, MRC_USHORT = 6, MRC_UCHAR3 = 16, MRC_CHAR,
  MRC_UNKNOWN
}
enum  { MRC_NUM_LABELS = 10, MRC_LABEL_SIZE = 80, NUM_4BYTES_PRE_MAP = 52, NUM_4BYTES_AFTER_MAP = 3 }

Private Member Functions

void swap_header (MrcHeader &mrch)
void update_stats (void *data, size_t size)
 This is a utility function used to calculate new min/max/mean/sigma when write MRC file as 16 bit or 8 bit.
int read_mrc_header (Dict &dict, int image_index=0, const Region *area=0, bool is_3d=false)
int read_fei_header (Dict &dict, int image_index=0, const Region *area=0, bool is_3d=false)
int transpose (float *data, int nx, int ny, int nz) const

Static Private Member Functions

static int generate_machine_stamp ()
 generate the machine stamp used in MRC image format.

Private Attributes

string filename
IOMode rw_mode
FILE * mrcfile
int mode_size
union {
   MrcHeader   mrch
   FeiMrcHeader   feimrch
}; 
bool isFEI
bool is_stack
int stack_size
int is_ri
bool is_big_endian
bool is_new_file
bool initialized
bool is_transpose

Static Private Attributes

static const char * CTF_MAGIC = "!-"
static const char * SHORT_CTF_MAGIC = "!$"

Detailed Description

MRC file = header + data (nx x ny x nz).

A MRC image file stores 1D, 2D or 3D image. The image's dimensions and pixel type are defined in the header.

Definition at line 48 of file mrcio.h.


Member Enumeration Documentation

anonymous enum [private]
Enumerator:
MRC_NUM_LABELS 
MRC_LABEL_SIZE 
NUM_4BYTES_PRE_MAP 
NUM_4BYTES_AFTER_MAP 

Definition at line 79 of file mrcio.h.

enum EMAN::MrcIO::MrcMode [private]
Enumerator:
MRC_UCHAR 
MRC_SHORT 
MRC_FLOAT 
MRC_SHORT_COMPLEX 
MRC_FLOAT_COMPLEX 
MRC_USHORT 
MRC_UCHAR3 
MRC_CHAR 
MRC_UNKNOWN 

Definition at line 67 of file mrcio.h.

                             {
                        MRC_UCHAR = 0,
                        MRC_SHORT,
                        MRC_FLOAT,
                        MRC_SHORT_COMPLEX,
                        MRC_FLOAT_COMPLEX,
                        MRC_USHORT = 6,         //non-standard
                        MRC_UCHAR3 = 16,        //unsigned char * 3, for rgb data, non-standard
                        MRC_CHAR,
                        MRC_UNKNOWN
                };

Constructor & Destructor Documentation

MrcIO::MrcIO ( const string &  filename,
IOMode  rw_mode = READ_ONLY 
) [explicit]

Definition at line 53 of file mrcio.cpp.

References is_big_endian, EMAN::ByteOrder::is_host_big_endian(), and mrch.

:       filename(mrc_filename), rw_mode(rw), mrcfile(0), mode_size(0),
                isFEI(false), is_ri(0), is_new_file(false), initialized(false),
                is_transpose(false), is_stack(false), stack_size(1)
{
        memset(&mrch, 0, sizeof(MrcHeader));
        is_big_endian = ByteOrder::is_host_big_endian();
}
MrcIO::~MrcIO ( )

Definition at line 62 of file mrcio.cpp.

References mrcfile.

{
        if (mrcfile) {
                fclose(mrcfile);
                mrcfile = NULL;
        }
}

Member Function Documentation

int MrcIO::generate_machine_stamp ( ) [static, private]

generate the machine stamp used in MRC image format.

Definition at line 1517 of file mrcio.cpp.

References EMAN::ByteOrder::is_host_big_endian().

{
        int stamp = 0;
        char *p = (char *) (&stamp);

        if (ByteOrder::is_host_big_endian()) {
                p[0] = 0x11;
                p[1] = 0x11;
                p[2] = 0;
                p[3] = 0;
        }
        else {
                p[0] = 0x44;
                p[1] = 0x41;
                p[2] = 0;
                p[3] = 0;
        }

        return stamp;
}
int MrcIO::get_mode_size ( int  mm) [static]

Definition at line 1399 of file mrcio.cpp.

References MRC_CHAR, MRC_FLOAT, MRC_FLOAT_COMPLEX, MRC_SHORT, MRC_SHORT_COMPLEX, MRC_UCHAR, and MRC_USHORT.

Referenced by is_valid().

{
        MrcIO::MrcMode m = static_cast < MrcMode > (mm);

        int msize = 0;
        switch (m) {
        case MRC_CHAR:
        case MRC_UCHAR:
                msize = sizeof(char);
                break;
        case MRC_SHORT:
        case MRC_USHORT:
        case MRC_SHORT_COMPLEX:
                msize = sizeof(short);
                break;
        case MRC_FLOAT:
        case MRC_FLOAT_COMPLEX:
                msize = sizeof(float);
                break;
        default:
                msize = 0;
        }

        return msize;
}
int MrcIO::get_nimg ( ) [virtual]

Return the number of images in this image file.

Reimplemented from EMAN::ImageIO.

Definition at line 1544 of file mrcio.cpp.

References EMAN::ImageIO::init(), and stack_size.

{
        init();

        return stack_size;
}
bool MrcIO::is_valid ( const void *  first_block,
off_t  file_size = 0 
) [static]

Definition at line 182 of file mrcio.cpp.

References data, ENTERFUNC, EXITFUNC, get_mode_size(), EMAN::ByteOrder::is_data_big_endian(), EMAN::ByteOrder::is_host_big_endian(), LOGWARN, MRC_FLOAT_COMPLEX, MRC_SHORT_COMPLEX, MRC_UCHAR, MRC_UNKNOWN, nx, ny, and EMAN::ByteOrder::swap_bytes().

Referenced by EMAN::EMUtil::fast_get_image_type(), and EMAN::EMUtil::get_image_type().

{
        ENTERFUNC;

        if (!first_block) {
                return false;
        }

        const int *data = static_cast < const int *>(first_block);
        int nx = data[0];
        int ny = data[1];
        int nz = data[2];
        int mrcmode = data[3];
        int nsymbt = data[23];  // this field specify the extra bytes for symmetry information

        bool data_big_endian = ByteOrder::is_data_big_endian(&nz);

        if (data_big_endian != ByteOrder::is_host_big_endian()) {
                ByteOrder::swap_bytes(&nx);
                ByteOrder::swap_bytes(&ny);
                ByteOrder::swap_bytes(&nz);
                ByteOrder::swap_bytes(&mrcmode);
                ByteOrder::swap_bytes(&nsymbt);
        }

        if (mrcmode == MRC_SHORT_COMPLEX || mrcmode == MRC_FLOAT_COMPLEX) {
                nx *= 2;
        }

        const int max_dim = 1 << 20;

        if ((mrcmode >= MRC_UCHAR && mrcmode < MRC_UNKNOWN) &&
                (nx > 1 && nx < max_dim) && (ny > 0 && ny < max_dim)
                 && (nz > 0 && nz < max_dim)) {

//#ifndef SPIDERMRC // Spider MRC files don't satisfy the following test

                if (file_size > 0) {
                        off_t file_size1 = (off_t)nx * (off_t)ny * (off_t)nz *
                                (off_t)get_mode_size(mrcmode) +
                                (off_t)sizeof(MrcHeader) + nsymbt;

                        if (file_size == file_size1) {
                                return true;
                        }

//                      return false;

         // when size doesn't match, print error message instead of make it fail

                        LOGWARN("image size check fails, still try to read it...");
                }
                else {
                        return true;
                }

//#endif // SPIDERMRC

                return true;
        }

        EXITFUNC;

        return false;
}
int MrcIO::read_ctf ( Ctf ctf,
int  image_index = 0 
) [virtual]

Read CTF data from this image.

Parameters:
ctfUsed to store the CTF data.
image_indexThe index of the image to read.
Returns:
0 if OK; 1 if error.

Reimplemented from EMAN::ImageIO.

Definition at line 1353 of file mrcio.cpp.

References CTF_MAGIC, ENTERFUNC, EXITFUNC, EMAN::Ctf::from_string(), EMAN::ImageIO::init(), EMAN::MrcIO::MrcHeader::labels, and mrch.

Referenced by read_mrc_header().

{
        ENTERFUNC;

        init();

        size_t n = strlen(CTF_MAGIC);
        int err = 1;

        if (strncmp(&mrch.labels[0][0], CTF_MAGIC, n) == 0) {
                err = ctf.from_string(string(&mrch.labels[0][n]));
        }

        EXITFUNC;

        return err;
}
int MrcIO::read_fei_header ( Dict dict,
int  image_index = 0,
const Region area = 0,
bool  is_3d = false 
) [private]

Definition at line 427 of file mrcio.cpp.

References EMAN::MrcIO::FeiMrcExtHeader::a_tilt, EMAN::MrcIO::FeiMrcHeader::alpha, EMAN::MrcIO::FeiMrcHeader::amax, EMAN::MrcIO::FeiMrcHeader::amean, EMAN::MrcIO::FeiMrcHeader::amin, EMAN::MrcIO::FeiMrcExtHeader::appliedDefocus, EMAN::MrcIO::FeiMrcExtHeader::b_tilt, EMAN::MrcIO::FeiMrcHeader::beta, EMAN::MrcIO::FeiMrcExtHeader::binning, EMAN::ImageIO::check_region(), EMAN::MrcIO::FeiMrcExtHeader::defocus, EMAN::MrcIO::FeiMrcHeader::dvid, ENTERFUNC, EXITFUNC, EMAN::MrcIO::FeiMrcExtHeader::exp_time, feimrch, filename, EMAN::MrcIO::FeiMrcHeader::gamma, EMAN::EMUtil::get_region_dims(), EMAN::MrcIO::FeiMrcExtHeader::ht, EMAN::MrcIO::FeiMrcHeader::idtype, ImageReadException, EMAN::ImageIO::init(), EMAN::MrcIO::FeiMrcHeader::ispg, EMAN::MrcIO::FeiMrcHeader::labl, EMAN::MrcIO::FeiMrcHeader::lens, EMAN::MrcIO::FeiMrcExtHeader::magnification, EMAN::MrcIO::FeiMrcHeader::mapc, EMAN::MrcIO::FeiMrcHeader::mapr, EMAN::MrcIO::FeiMrcHeader::maps, EMAN::MrcIO::FeiMrcHeader::max2, EMAN::MrcIO::FeiMrcHeader::max3, EMAN::MrcIO::FeiMrcHeader::max4, EMAN::MrcIO::FeiMrcExtHeader::mean_int, EMAN::MrcIO::FeiMrcHeader::min2, EMAN::MrcIO::FeiMrcHeader::min3, EMAN::MrcIO::FeiMrcHeader::min4, EMAN::MrcIO::FeiMrcHeader::mode, mrcfile, EMAN::MrcIO::FeiMrcHeader::mx, EMAN::MrcIO::FeiMrcHeader::my, EMAN::MrcIO::FeiMrcHeader::mz, EMAN::MrcIO::FeiMrcHeader::nd1, EMAN::MrcIO::FeiMrcHeader::nd2, EMAN::MrcIO::FeiMrcHeader::next, EMAN::MrcIO::FeiMrcHeader::nlabl, EMAN::MrcIO::FeiMrcHeader::nsymbt, EMAN::MrcIO::FeiMrcHeader::numfloats, EMAN::MrcIO::FeiMrcHeader::numintegers, EMAN::MrcIO::FeiMrcHeader::nx, EMAN::MrcIO::FeiMrcHeader::nxstart, EMAN::MrcIO::FeiMrcHeader::ny, EMAN::MrcIO::FeiMrcHeader::nystart, EMAN::MrcIO::FeiMrcHeader::nz, EMAN::MrcIO::FeiMrcHeader::nzstart, EMAN::MrcIO::FeiMrcExtHeader::pixel_size, portable_fseek(), EMAN::MrcIO::FeiMrcHeader::sub, EMAN::MrcIO::FeiMrcExtHeader::tilt_axis, EMAN::MrcIO::FeiMrcHeader::tiltangles, to_em_datatype(), EMAN::MrcIO::FeiMrcHeader::vd1, EMAN::MrcIO::FeiMrcHeader::vd2, EMAN::MrcIO::FeiMrcExtHeader::x_shift, EMAN::MrcIO::FeiMrcExtHeader::x_stage, EMAN::MrcIO::FeiMrcHeader::xlen, EMAN::MrcIO::FeiMrcHeader::xorg, EMAN::MrcIO::FeiMrcExtHeader::y_shift, EMAN::MrcIO::FeiMrcExtHeader::y_stage, EMAN::MrcIO::FeiMrcHeader::ylen, EMAN::MrcIO::FeiMrcHeader::yorg, EMAN::MrcIO::FeiMrcExtHeader::z_stage, EMAN::MrcIO::FeiMrcHeader::zfac, EMAN::MrcIO::FeiMrcHeader::zlen, and EMAN::MrcIO::FeiMrcHeader::zorg.

{
        ENTERFUNC;

        if(image_index < 0) {
                image_index = 0;
        }

        init();

        check_region(area, FloatSize(feimrch.nx, feimrch.ny, feimrch.nz), is_new_file, false);

        int xlen = 0, ylen = 0, zlen = 0;

        EMUtil::get_region_dims(area, feimrch.nx, & xlen, feimrch.ny, & ylen, feimrch.nz, & zlen);

        dict["nx"] = xlen;
        dict["ny"] = ylen;
        dict["nz"] = zlen;
        dict["FEIMRC.nx"] = feimrch.nx;
        dict["FEIMRC.ny"] = feimrch.ny;
        dict["FEIMRC.nz"] = feimrch.nz;

        dict["datatype"] = to_em_datatype(feimrch.mode);        //=1, FEI-MRC file always use short for data type

        dict["FEIMRC.nxstart"] = feimrch.nxstart;
        dict["FEIMRC.nystart"] = feimrch.nystart;
        dict["FEIMRC.nzstart"] = feimrch.nzstart;

        dict["FEIMRC.mx"] = feimrch.mx;
        dict["FEIMRC.my"] = feimrch.my;
        dict["FEIMRC.mz"] = feimrch.mz;

        dict["FEIMRC.xlen"] = feimrch.xlen;
        dict["FEIMRC.ylen"] = feimrch.ylen;
        dict["FEIMRC.zlen"] = feimrch.zlen;

        dict["FEIMRC.alpha"] = feimrch.alpha;
        dict["FEIMRC.beta"] = feimrch.beta;
        dict["FEIMRC.gamma"] = feimrch.gamma;

        dict["FEIMRC.mapc"] = feimrch.mapc;
        dict["FEIMRC.mapr"] = feimrch.mapr;
        dict["FEIMRC.maps"] = feimrch.maps;

        dict["FEIMRC.minimum"] = feimrch.amin;
        dict["FEIMRC.maximum"] = feimrch.amax;
        dict["FEIMRC.mean"] = feimrch.amean;
        dict["mean"] = feimrch.amean;

        dict["FEIMRC.ispg"] = feimrch.ispg;
        dict["FEIMRC.nsymbt"] = feimrch.nsymbt;

        dict["apix_x"] = feimrch.xlen / feimrch.mx;
        dict["apix_y"] = feimrch.ylen / feimrch.my;
        dict["apix_z"] = feimrch.zlen / feimrch.mz;

        dict["FEIMRC.next"] = feimrch.next;     //offset from end of header to the first dataset
        dict["FEIMRC.dvid"] = feimrch.dvid;
        dict["FEIMRC.numintegers"] = feimrch.numintegers;
        dict["FEIMRC.numfloats"] = feimrch.numfloats;
        dict["FEIMRC.sub"] = feimrch.sub;
        dict["FEIMRC.zfac"] = feimrch.zfac;

        dict["FEIMRC.min2"] = feimrch.min2;
        dict["FEIMRC.max2"] = feimrch.max2;
        dict["FEIMRC.min3"] = feimrch.min3;
        dict["FEIMRC.max3"] = feimrch.max3;
        dict["FEIMRC.min4"] = feimrch.min4;
        dict["FEIMRC.max4"] = feimrch.max4;

        dict["FEIMRC.idtype"] = feimrch.idtype;
        dict["FEIMRC.lens"] = feimrch.lens;
        dict["FEIMRC.nd1"] = feimrch.nd1;
        dict["FEIMRC.nd2"] = feimrch.nd2;
        dict["FEIMRC.vd1"] = feimrch.vd1;
        dict["FEIMRC.vd2"] = feimrch.vd2;

        for(int i=0; i<9; i++) {        // 9 tilt angles
                char label[32];
                sprintf(label, "MRC.tiltangles%d", i);
                dict[string(label)] = feimrch.tiltangles[i];
        }

        dict["FEIMRC.zorg"] = feimrch.zorg;
        dict["FEIMRC.xorg"] = feimrch.xorg;
        dict["FEIMRC.yorg"] = feimrch.yorg;

        dict["FEIMRC.nlabl"] = feimrch.nlabl;

        for (int i = 0; i < feimrch.nlabl; i++) {
                char label[32];
                sprintf(label, "MRC.label%d", i);
                dict[string(label)] = string(feimrch.labl[i], 80);
        }

        /* Read extended image header by specified image index */

        FeiMrcExtHeader feiexth;

        portable_fseek(mrcfile, sizeof(FeiMrcHeader)+sizeof(FeiMrcExtHeader)*image_index, SEEK_SET);

        if (fread(&feiexth, sizeof(FeiMrcExtHeader), 1, mrcfile) != 1) {
                throw ImageReadException(filename, "FEI MRC extended header");
        }

        dict["FEIMRC.a_tilt"] = feiexth.a_tilt;
        dict["FEIMRC.b_tilt"] = feiexth.b_tilt;

        dict["FEIMRC.x_stage"] = feiexth.x_stage;
        dict["FEIMRC.y_stage"] = feiexth.y_stage;
        dict["FEIMRC.z_stage"] = feiexth.z_stage;

        dict["FEIMRC.x_shift"] = feiexth.x_shift;
        dict["FEIMRC.y_shift"] = feiexth.y_shift;

        dict["FEIMRC.defocus"] = feiexth.defocus;
        dict["FEIMRC.exp_time"] = feiexth.exp_time;
        dict["FEIMRC.mean_int"] = feiexth.mean_int;
        dict["FEIMRC.tilt_axis"] = feiexth.tilt_axis;

        dict["FEIMRC.pixel_size"] = feiexth.pixel_size;
        dict["FEIMRC.magnification"] = feiexth.magnification;
        dict["FEIMRC.ht"] = feiexth.ht;
        dict["FEIMRC.binning"] = feiexth.binning;
        dict["FEIMRC.appliedDefocus"] = feiexth.appliedDefocus;

        // remainder 16 4-byte floats not used

        EXITFUNC;

        return 0;
}
int MrcIO::read_mrc_header ( Dict dict,
int  image_index = 0,
const Region area = 0,
bool  is_3d = false 
) [private]

Definition at line 260 of file mrcio.cpp.

References EMAN::MrcIO::MrcHeader::alpha, EMAN::MrcIO::MrcHeader::amax, EMAN::MrcIO::MrcHeader::amean, EMAN::MrcIO::MrcHeader::amin, EMAN::MrcIO::MrcHeader::beta, EMAN::ImageIO::check_region(), ENTERFUNC, EXITFUNC, filename, EMAN::MrcIO::MrcHeader::gamma, EMAN::Region::get_ndim(), EMAN::EMUtil::get_region_dims(), ImageReadException, EMAN::ImageIO::is_complex_mode(), is_stack, is_transpose, EMAN::MrcIO::MrcHeader::ispg, EMAN::MrcIO::MrcHeader::labels, EMAN::MrcIO::MrcHeader::machinestamp, EMAN::MrcIO::MrcHeader::mapc, EMAN::MrcIO::MrcHeader::mapr, EMAN::MrcIO::MrcHeader::maps, EMAN::MrcIO::MrcHeader::mode, mrch, EMAN::MrcIO::MrcHeader::mx, EMAN::MrcIO::MrcHeader::my, EMAN::MrcIO::MrcHeader::mz, EMAN::MrcIO::MrcHeader::nlabels, EMAN::MrcIO::MrcHeader::nsymbt, EMAN::MrcIO::MrcHeader::nx, EMAN::MrcIO::MrcHeader::nxstart, EMAN::MrcIO::MrcHeader::ny, EMAN::MrcIO::MrcHeader::nystart, EMAN::MrcIO::MrcHeader::nz, EMAN::MrcIO::MrcHeader::nzstart, EMAN::Region::origin, read_ctf(), EMAN::MrcIO::MrcHeader::rms, EMAN::Transform::set_rotation(), EMAN::Transform::set_trans(), to_em_datatype(), EMAN::EMAN1Ctf::to_vector(), EMAN::MrcIO::MrcHeader::xlen, EMAN::MrcIO::MrcHeader::xorigin, EMAN::MrcIO::MrcHeader::ylen, EMAN::MrcIO::MrcHeader::yorigin, EMAN::MrcIO::MrcHeader::zlen, and EMAN::MrcIO::MrcHeader::zorigin.

{
        ENTERFUNC;

        if (image_index < 0) {
                image_index = 0;
        }

        if (image_index != 0  &&  ! is_stack) {
                throw ImageReadException(filename,
                        "no stack allowed for MRC image. For take 2D slice out of 3D image, "
                        "read the 3D image first, then use get_clip().");
        }

        check_region(area, FloatSize(mrch.nx, mrch.ny, mrch.nz), is_new_file, false);

        int xlen = 0, ylen = 0, zlen = 0;

        EMUtil::get_region_dims(area, mrch.nx, & xlen, mrch.ny, & ylen, mrch.nz, & zlen);

        dict["nx"] = xlen;
        dict["ny"] = ylen;
        dict["nz"] = zlen;
        dict["MRC.nx"] = mrch.nx;
        dict["MRC.ny"] = mrch.ny;
        dict["MRC.nz"] = mrch.nz;

        dict["datatype"] = to_em_datatype(mrch.mode);

        dict["MRC.nxstart"] = mrch.nxstart;
        dict["MRC.nystart"] = mrch.nystart;
        dict["MRC.nzstart"] = mrch.nzstart;

        dict["MRC.mx"] = mrch.mx;
        dict["MRC.my"] = mrch.my;
        dict["MRC.mz"] = mrch.mz;

        dict["MRC.xlen"] = mrch.xlen;
        dict["MRC.ylen"] = mrch.ylen;
        dict["MRC.zlen"] = mrch.zlen;

        dict["MRC.alpha"] = mrch.alpha;
        dict["MRC.beta"] = mrch.beta;
        dict["MRC.gamma"] = mrch.gamma;

        dict["MRC.mapc"] = mrch.mapc;
        dict["MRC.mapr"] = mrch.mapr;
        dict["MRC.maps"] = mrch.maps;

        dict["MRC.minimum"] = mrch.amin;
        dict["MRC.maximum"] = mrch.amax;
        dict["MRC.mean"] = mrch.amean;
        dict["minimum"] = mrch.amin;
        dict["maximum"] = mrch.amax;
        dict["mean"] = mrch.amean;

        dict["MRC.ispg"] = mrch.ispg;
        dict["MRC.nsymbt"] = mrch.nsymbt;

        float apx = mrch.xlen / mrch.mx;
        float apy = mrch.ylen / mrch.my;
        float apz = mrch.zlen / mrch.mz;

        if (apx > 1000 || apx < 0.01) {
                dict["apix_x"] = 1.0f;
        }
        else {
                dict["apix_x"] = apx;
        }

        if (apy > 1000 || apy < 0.01) {
                dict["apix_y"] = 1.0f;
        }
        else {
                dict["apix_y"] = apy;
        }

        if (apz > 1000 || apz < 0.01) {
                dict["apix_z"] = 1.0f;
        }
        else {
                dict["apix_z"] = apz;
        }

        if (area) {
                dict["origin_x"] = mrch.xorigin + mrch.xlen * area->origin[0];
                dict["origin_y"] = mrch.yorigin + mrch.xlen * area->origin[1];

                if (area->get_ndim() == 3 && mrch.nz > 1) {
                        dict["origin_z"] = mrch.zorigin + mrch.xlen * area->origin[2];
                }
                else {
                        dict["origin_z"] = mrch.zorigin;
                }
        }
        else {
                dict["origin_x"] = mrch.xorigin;
                dict["origin_y"] = mrch.yorigin;
                dict["origin_z"] = mrch.zorigin;
        }

        if (is_complex_mode()) {
                dict["is_complex"] = 1;
                dict["is_complex_ri"] = 1;
        }

        dict["MRC.machinestamp"] = mrch.machinestamp;

        dict["MRC.rms"] = mrch.rms;
        dict["sigma"] = mrch.rms;
        dict["MRC.nlabels"] = mrch.nlabels;

        for (int i = 0; i < mrch.nlabels; i++) {
                char label[32];
                sprintf(label, "MRC.label%d", i);
                dict[string(label)] = string(mrch.labels[i],80);
        }

        EMAN1Ctf ctf_;

        if (read_ctf(ctf_) == 0) {
                vector<float> vctf = ctf_.to_vector();
                dict["ctf"] = vctf;
        }

        if (is_transpose) {
                dict["nx"] = ylen;
                dict["ny"] = xlen;
                dict["MRC.nx"] = mrch.ny;
                dict["MRC.ny"] = mrch.nx;
                dict["MRC.mx"] = mrch.my;
                dict["MRC.my"] = mrch.mx;
                dict["apix_x"] = mrch.ylen / mrch.my;
                dict["apix_y"] = mrch.xlen / mrch.mx;
                dict["origin_x"] = mrch.yorigin;
                dict["origin_y"] = mrch.xorigin;
                dict["MRC.nxstart"] = mrch.nystart;
                dict["MRC.nystart"] = mrch.nxstart;
        }

        Transform * trans = new Transform();

        if (is_transpose) {
                trans->set_trans(mrch.nystart, mrch.nxstart, mrch.nzstart);
                trans->set_rotation(Dict("type", "imagic", "alpha", mrch.alpha,
                                                                                 "beta", mrch.beta, "gamma", mrch.gamma));
        }
        else {
                trans->set_trans(mrch.nxstart, mrch.nystart, mrch.nzstart);
                trans->set_rotation(Dict("type", "imagic", "alpha", mrch.alpha,
                                                                                 "beta", mrch.beta, "gamma", mrch.gamma));
        }
        
        if (zlen <= 1) {
                dict["xform.projection"] = trans;
        }
        else {
                dict["xform.align3d"] = trans;
        }

        if (trans) {delete trans; trans = NULL;}

        EXITFUNC;

        return 0;
}
void MrcIO::swap_header ( MrcHeader mrch) [private]
int MrcIO::to_em_datatype ( int  mrcmode) [static]
int MrcIO::to_mrcmode ( int  em_datatype,
int  is_complex 
) [static]
int MrcIO::transpose ( float *  data,
int  nx,
int  ny,
int  nz 
) const [private]

Definition at line 1551 of file mrcio.cpp.

References copy(), x, and y.

{
        float * tmp = new float[xlen*ylen];

        for(size_t z=0; z<(size_t)zlen; ++z) {
                for(size_t y=0; y<(size_t)ylen; ++y) {
                        for(size_t x=0; x<(size_t)xlen; ++x) {
                                tmp[x*ylen+y] = data[z*xlen*ylen+y*xlen+x];
                        }
                }

                std::copy(tmp, tmp+xlen*ylen, data+z*xlen*ylen);
        }

        delete [] tmp;

        return 0;
}
void MrcIO::update_stats ( void *  data,
size_t  size 
) [private]

This is a utility function used to calculate new min/max/mean/sigma when write MRC file as 16 bit or 8 bit.

It will write new set of min/max/mean/sigma to mrch. this function needs get the output data storage type from mrch.mode.

Definition at line 1208 of file mrcio.cpp.

References EMAN::MrcIO::MrcHeader::amax, EMAN::MrcIO::MrcHeader::amean, EMAN::MrcIO::MrcHeader::amin, filename, ImageWriteException, InvalidCallException, max, mean(), min, EMAN::MrcIO::MrcHeader::mode, MRC_CHAR, MRC_SHORT, MRC_SHORT_COMPLEX, MRC_UCHAR, MRC_USHORT, mrcfile, mrch, portable_fseek(), EMAN::MrcIO::MrcHeader::rms, sqrt(), and v.

{
        float  v;       // variable to hold pixel value
        double sum;
        double square_sum;
        double mean;
        double sigma;
        double vv;
        float  min, max;
        
        signed char    *  scdata = NULL;
        unsigned char  *  cdata  = NULL;
        short          *  sdata  = NULL;
        unsigned short *  usdata = NULL;

        bool use_schar  = (mrch.mode == MRC_CHAR);
        bool use_uchar  = (mrch.mode == MRC_UCHAR);
        bool use_short  = (mrch.mode == MRC_SHORT || mrch.mode == MRC_SHORT_COMPLEX);
        bool use_ushort = (mrch.mode == MRC_USHORT);
        
        if (use_uchar) {
                max    = 0.0;
                min    = UCHAR_MAX;
                cdata  = (unsigned char *) data;
        }
        else if (use_schar) {
                max    = SCHAR_MIN;
                min    = SCHAR_MAX;
                scdata = (signed char *) data;
        }
        else if (use_short) {
                max    = (float) SHRT_MIN;
                min    = (float) SHRT_MAX;
                sdata  = (short *) data;
        }
        else if (use_ushort) {
                max    = 0.0f;
                min    = (float) USHRT_MAX;
                usdata = (unsigned short *) data;
        }
        else {
                throw InvalidCallException("This function is used to write 8bit/16bit mrc file only.");
        }

        sum = 0.0;

        for (size_t i = 0; i < size; i++) {
                if (use_uchar) {
                        v = (float) (cdata[i]);
                }
                else if (use_schar) {
                        v = (float) (scdata[i]);
                }
                else if (use_short) {
                        v = (float) (sdata[i]);
                }
                else {
                        v = (float) (usdata[i]);
                }

                if (v < min) min = v;
                if (v > max) max = v;

                sum = sum + v;
        }

        if (size > 0) {
                mean = sum / (double) size;
        }
        else {
                mean = 0.0;
        }

        square_sum = 0.0;

        for (size_t i = 0; i < size; i++) {
                if (use_uchar) {
                        v = (float) (cdata[i]);
                }
                else if (use_schar) {
                        v = (float) (scdata[i]);
                }
                else if (use_short) {
                        v = (float) (sdata[i]);
                }
                else {
                        v = (float) (usdata[i]);
                }

                vv = v - mean;

                square_sum = square_sum  +  vv * vv;
        }

        if (size > 1) {
                sigma = std::sqrt(square_sum / (double) (size-1));
        }
        else {
                sigma = 0.0;
        }

        /* change mrch.amin / amax / amean / rms here */

        mrch.amin  = min;
        mrch.amax  = max;
        mrch.amean = mean;
        mrch.rms   = sigma;
        
//      MrcHeader mrch2 = mrch;
//
// endian issue, can't get use_host_endian argument
//      bool opposite_endian = false;
//
//      if (!is_new_file) {
//              if (is_big_endian != ByteOrder::is_host_big_endian()) {
//                      opposite_endian = true;
//              }
//
//              portable_fseek(mrcfile, 0, SEEK_SET);
//      }
//      
//      if (opposite_endian || !use_host_endian) {
//              swap_header(mrch2);
//      }

        portable_fseek(mrcfile, 0, SEEK_SET);
        
        if (fwrite(& mrch, sizeof(MrcHeader), 1, mrcfile) != 1) {
                throw ImageWriteException(filename, "Error writing MRC header to update statistics.");
        }
        
        portable_fseek(mrcfile, sizeof(MrcHeader), SEEK_SET);
}
void MrcIO::write_ctf ( const Ctf ctf,
int  image_index = 0 
) [virtual]

Write CTF data to this image.

Parameters:
ctfCtf instance storing the CTF data.
image_indexThe index of the image to write.
Returns:
0 if OK; 1 if error.

Reimplemented from EMAN::ImageIO.

Definition at line 1371 of file mrcio.cpp.

References CTF_MAGIC, ENTERFUNC, EXITFUNC, filename, ImageWriteException, EMAN::ImageIO::init(), EMAN::MrcIO::MrcHeader::labels, mrcfile, mrch, and EMAN::Ctf::to_string().

{
        ENTERFUNC;

        init();

        string ctf_str = ctf.to_string();

#ifdef _WIN32
        _snprintf(&mrch.labels[0][0],80, "%s%s", CTF_MAGIC, ctf_str.c_str());
#else
         snprintf(&mrch.labels[0][0],80, "%s%s", CTF_MAGIC, ctf_str.c_str());
#endif  //_WIN32

        rewind(mrcfile);

        if (fwrite(&mrch, sizeof(MrcHeader), 1, mrcfile) != 1) {
                throw ImageWriteException(filename, "write CTF info to header failed");
        }

        EXITFUNC;
}

Member Data Documentation

union { ... } [private]
const char * MrcIO::CTF_MAGIC = "!-" [static, private]

Definition at line 261 of file mrcio.h.

Referenced by read_ctf(), and write_ctf().

Definition at line 54 of file mrcio.h.

Definition at line 272 of file mrcio.h.

Referenced by read_fei_header().

string EMAN::MrcIO::filename [private]

Definition at line 265 of file mrcio.h.

Referenced by read_fei_header(), read_mrc_header(), update_stats(), and write_ctf().

bool EMAN::MrcIO::initialized [private]

Definition at line 285 of file mrcio.h.

Definition at line 283 of file mrcio.h.

Referenced by MrcIO().

bool EMAN::MrcIO::is_new_file [private]

Definition at line 284 of file mrcio.h.

int EMAN::MrcIO::is_ri [private]

Definition at line 282 of file mrcio.h.

bool EMAN::MrcIO::is_stack [private]

Definition at line 279 of file mrcio.h.

Referenced by read_mrc_header().

bool EMAN::MrcIO::is_transpose [private]

Definition at line 286 of file mrcio.h.

Referenced by read_mrc_header().

bool EMAN::MrcIO::isFEI [private]

Definition at line 276 of file mrcio.h.

int EMAN::MrcIO::mode_size [private]

Definition at line 268 of file mrcio.h.

FILE* EMAN::MrcIO::mrcfile [private]

Definition at line 267 of file mrcio.h.

Referenced by read_fei_header(), update_stats(), write_ctf(), and ~MrcIO().

Definition at line 271 of file mrcio.h.

Referenced by MrcIO(), read_ctf(), read_mrc_header(), update_stats(), and write_ctf().

Definition at line 266 of file mrcio.h.

const char * MrcIO::SHORT_CTF_MAGIC = "!$" [static, private]

Definition at line 262 of file mrcio.h.

int EMAN::MrcIO::stack_size [private]

Definition at line 280 of file mrcio.h.

Referenced by get_nimg().


The documentation for this class was generated from the following files: