IMP logo
IMP Reference Guide  2.18.0
The Integrative Modeling Platform
MRCReaderWriter.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em/MRCReaderWriter.h
3  * \brief Classes to read or write MRC files.
4  *
5  * Copyright 2007-2022 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPEM_MRC_READER_WRITER_H
10 #define IMPEM_MRC_READER_WRITER_H
11 
12 #include <IMP/em/em_config.h>
13 #include "MapReaderWriter.h"
14 #include "internal/MRCHeader.h"
15 #include <string>
16 
17 IMPEM_BEGIN_NAMESPACE
18 
19 /** A class to read and write MRC files. */
20 class IMPEMEXPORT MRCReaderWriter : public MapReaderWriter {
21  public:
22  //! Empty constructor
24  //! Constructor
25  /**
26  * \param[in] fn name of the file to open or write
27  */
28  MRCReaderWriter(std::string fn) { filename = fn; }
29 #if !defined(DOXYGEN) && !defined(SWIG)
30  //! Reads an MRC file and translates the header to the general DensityHeader
31  void read(const char *fn_in, float **data, DensityHeader &head) override;
32 
33  //! Writes an MRC file from the data and the general DensityHeader
34  void write(const char *fn_out, const float *data,
35  const DensityHeader &head) override;
36 #endif
37 
38  private:
39  //! By default the data are read into the grid of the class, but an external
40  //! pointer to another grid can be specified
41  void read() { read(&grid); }
42 
43  void read(float **pt);
44  //! reads the header
45  void read_header();
46  //! reads the data
47  void read_data(float *pt);
48  //! Transpose data from section/row/col to z/y/x if needed
49  void transpose_data_to_zyx(float **pt);
50  //! reads data of size 8-bit
51  void read_8_data(float *pt);
52  //! reads data of size 32-bit
53  void read_32_data(float *pt);
54  void read_grid(void *pt, size_t size, size_t n);
55  void seek_to_data();
56  //! Write function
57  /**
58  * \param[in] fn name of the file to write
59  */
60  void write(const char *fn) { return write(fn, grid); }
61 
62  //! Write function
63  /**
64  * \param[in] fn name of the file to write
65  * \param[in] pt pointer to the data to write
66  */
67  void write(const char *fn, const float *pt);
68  //! Writes the header
69  /**
70  * \param[in] s stream to write the header
71  */
72  void write_header(std::ofstream &s);
73  //! Writes data
74  /**
75  * \param[in] s stream to write the data
76  * \param[in] pt pointer to the data
77  */
78  void write_data(std::ofstream &s, const float *pt);
79 
80  //! Name of the file
81  std::string filename;
82  //! file stream for the file read
83  std::fstream fs;
84  //! The header of the file
85  internal::MRCHeader header;
86  //! The grid of data. The data is stored in the grid with the convention
87  //! that the order of indexes is z,y,x
88  float *grid;
89 
91 };
92 
93 IMPEM_END_NAMESPACE
94 
95 #endif /* IMPEM_MRC_READER_WRITER_H */
The base class to handle reading and writing of density maps.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
MRCReaderWriter(std::string fn)
Constructor.
An abstract class for reading a map.
MRCReaderWriter()
Empty constructor.