PURIFY
Next-generation radio interferometric imaging
pfitsio.cc
Go to the documentation of this file.
1 #include "purify/config.h"
2 #include "purify/logging.h"
3 
4 #include "purify/pfitsio.h"
5 
6 namespace purify::pfitsio {
7 
8 void write_key(fitsfile *fptr, const std::string &key, const std::string &value,
9  const std::string &comment, int *status) {
10  std::string entry = value;
11  if (fits_write_key(fptr, TSTRING, const_cast<char *>(key.c_str()),
12  reinterpret_cast<void *>(const_cast<char *>(entry.c_str())),
13  const_cast<char *>(comment.c_str()), status))
14  throw std::runtime_error("Problem writing key in fits file: " + key);
15 }
16 
17 void write_key(fitsfile *fptr, const std::string &key, const char *value,
18  const std::string &comment, int *status) {
19  write_key(fptr, key, std::string(value), comment, status);
20 }
21 
22 void write_history(fitsfile *fptr, const std::string &context, const std::string &history,
23  int *status) {
24  const std::string entry = context + ": " + history;
25  if (fits_write_history(fptr, const_cast<char *>(entry.c_str()), status))
26  throw std::runtime_error("Problem writing comments in fits file");
27 }
28 
30 void write2d(const Image<t_real> &eigen_image, const pfitsio::header_params &header,
31  const bool &overwrite) {
32  /*
33  Writes an image to a fits file.
34 
35  image:: image data, a 2d Image.
36  header:: structure containing header information
37  overwrite:: if true, overwrites old fits file with same name
38 
39  */
40 
41  write3d(std::vector<Image<t_real>>(1, eigen_image), header, overwrite);
42 }
43 
44 void write2d(const Image<t_real> &eigen_image, const std::string &fits_name,
45  const std::string &pix_units, const bool &overwrite) {
46  /*
47  Writes an image to a fits file.
48 
49  image:: image data, a 2d Image.
50  fits_name:: string containing the file name of the fits file.
51  pix_units:: units of flux
52  ra:: centre pixel coordinate in ra
53  dec:: centre pixel coordinate in dec
54 
55  */
57  header.fits_name = fits_name;
58  header.pix_units = pix_units;
59 
60  write2d(eigen_image, header, overwrite);
61 }
62 
63 void write3d(const std::vector<Image<t_real>> &eigen_images, const pfitsio::header_params &header,
64  const bool &overwrite) {
65  std::vector<long> naxes = {static_cast<long>(eigen_images.at(0).rows()),
66  static_cast<long>(eigen_images.at(0).cols()),
67  static_cast<long>(eigen_images.size()), 1};
68  std::vector<long> fpixel = {1, 1, 1, 1};
69 
70  Vector<t_real> image = Vector<t_real>::Zero(naxes.at(0) * naxes.at(1) * naxes.at(2));
71  for (int i = 0; i < eigen_images.size(); i++)
72  image.segment(i * naxes.at(0) * naxes.at(1), naxes.at(0) * naxes.at(1)) =
73  Vector<t_real>::Map(eigen_images.at(i).data(), naxes.at(0) * naxes.at(1));
74  write3d<Vector<t_real>>(image, naxes.at(0), naxes.at(1), naxes.at(2), header, overwrite);
75 }
76 
77 void write3d(const std::vector<Image<t_real>> &eigen_images, const std::string &fits_name,
78  const std::string &pix_units, const bool &overwrite) {
79  /*
80  Writes a vector of images to a fits file.
81  image:: image data, a 3d Image.
82  fits_name:: string containing the file name of the fits file.
83  pix_units:: units of flux
84  ra:: centre pixel coordinate in ra
85  dec:: centre pixel coordinate in dec
86 */
88  header.fits_name = fits_name;
89  header.pix_units = pix_units;
90 
91  write3d(eigen_images, header, overwrite);
92 }
93 
95 std::vector<Image<t_complex>> read3d(const std::string &fits_name) {
96  std::vector<Image<t_complex>> eigen_images;
97  Vector<double> image;
98  int rows, cols, channels, pols = 1;
99  read3d<Vector<double>>(fits_name, image, rows, cols, channels, pols);
100  for (int i = 0; i < channels; i++) {
101  Vector<t_complex> eigen_image = Vector<t_complex>::Zero(rows * cols);
102  eigen_image.real() = image.segment(i * rows * cols, rows * cols);
103  eigen_images.push_back(Image<t_complex>::Map(eigen_image.data(), rows, cols));
104  }
105  return eigen_images;
106 }
107 
109 Image<t_complex> read2d(const std::string &fits_name) {
110  /*
111  Reads in an image from a fits file and returns the image.
112 
113  fits_name:: name of fits file
114  */
115 
116  const std::vector<Image<t_complex>> images = read3d(fits_name);
117  return images.at(0);
118 }
119 
120 } // namespace purify::pfitsio
void write2d(const Image< t_real > &eigen_image, const pfitsio::header_params &header, const bool &overwrite)
Write image to fits file.
Definition: pfitsio.cc:30
void write_key(fitsfile *fptr, const std::string &key, const std::string &value, const std::string &comment, int *status)
write key to fits file header
Definition: pfitsio.cc:8
Image< t_complex > read2d(const std::string &fits_name)
Read image from fits file.
Definition: pfitsio.cc:109
void write3d(const std::vector< Image< t_real >> &eigen_images, const pfitsio::header_params &header, const bool &overwrite)
Write cube to fits file using header information.
Definition: pfitsio.cc:63
std::vector< Image< t_complex > > read3d(const std::string &fits_name)
Read cube from fits file.
Definition: pfitsio.cc:95
void write_history(fitsfile *fptr, const std::string &context, const std::string &history, int *status)
write history to fits file
Definition: pfitsio.cc:22