Citlali
Loading...
Searching...
No Matches
fits_io.h
Go to the documentation of this file.
1#pragma once
2
3#include <CCfits/CCfits>
4
9template<file_type_enum file_type, typename ext_hdu_t>
10class fitsIO {
11public:
12 // get logger
13 std::shared_ptr<spdlog::logger> logger = spdlog::get("citlali_logger");
14
15 // filepath
16 std::string filepath;
17
18 // pointer to FITS file
19 std::unique_ptr<CCfits::FITS> pfits;
20
21 // vector of hdu's for easy access
22 std::vector<ext_hdu_t> hdus;
23
24 std::string pixel_axes;
25
26 fitsIO() {}
27
28 // constructor
29 fitsIO(std::string _f, std::string _a) : filepath(_f), pixel_axes(_a) {
30 // read in file
31 if constexpr (file_type==file_type_enum::read_fits) {
32 try {
33 pfits.reset( new CCfits::FITS(filepath, CCfits::Read));
34 logger->info("opened FITS file {}", filepath);
35 }
36 catch (CCfits::FITS::CantOpen) {
37 logger->error("unable to open file {}", filepath);
38 std::exit(EXIT_FAILURE);
39 }
40 }
41
42 // create file
43 else if constexpr (file_type==file_type_enum::write_fits) {
44 try {
45 // ! is the overwrite flag
46 pfits.reset( new CCfits::FITS("!" + filepath + ".fits", CCfits::Write));
47 // write date
48 pfits->pHDU().writeDate();
49 }
50 catch (CCfits::FITS::CantCreate) {
51 logger->error("unable to create file {}", filepath);
52 std::exit(EXIT_FAILURE);
53 }
54 }
55 }
56
57 template <typename Derived>
58 void add_hdu(std::string hdu_name, Eigen::DenseBase<Derived> &data) {
59 // axes in reverse order (cols, rows, pol, freq)
60 std::vector<long> naxes{data.cols(), data.rows(), 1, 1};
61
62 // add an extension hdu to vector
63 hdus.push_back((pfits->addImage(hdu_name,DOUBLE_IMG,naxes)));
64
65 // valarray to copy data into (seems to be necessary)
66 std::valarray<double> temp_data(data.size());
67
68 // copy the data
69 int k = 0;
70 for (int i=0; i<data.rows(); ++i){
71 for (int j=0; j<data.cols(); ++j) {
72 if (pixel_axes != "altaz") {
73 // flip in x direction)
74 temp_data[k] = data(i, data.cols() - j - 1);
75 }
76 else {
77 temp_data[k] = data(i,j);
78 }
79 k++;
80 }
81 }
82
83 // first pixel (starts at 1 I think)
84 long first_pixel = 1;
85
86 // write to the hdu
87 hdus.back()->write(first_pixel, temp_data.size(), temp_data);
88 }
89
90 auto get_hdu(std::string hdu_name) {
91 try {
92 // get extension
93 CCfits::ExtHDU& hdu = pfits->extension(hdu_name);
94
95 // hold image data
96 std::valarray<double> contents;
97
98 // read all user-specifed, coordinate, and checksum keys in the image
99 hdu.readAllKeys();
100 hdu.read(contents);
101
102 // this doesn't print the data, just header info.
103 long ax1(hdu.axis(0));
104 long ax2(hdu.axis(1));
105
106 // holds the image data
107 Eigen::MatrixXd data(ax2,ax1);
108
109 // loop through and copy into eigen matrix
110 Eigen::Index k = 0;
111 for (Eigen::Index i=0; i<ax2; ++i) {
112 for (Eigen::Index j=0; j<ax1; ++j) {
113 data(i,j) = contents[k];
114 k++;
115 }
116 }
117
118 // flip to match internal orientation
119 if (pixel_axes != "altaz") {
120 data.rowwise().reverseInPlace();
121 }
122
123 return data;
124
125 } catch (CCfits::FITS::NoSuchHDU) {
126 logger->error("cannot find {} from {}", hdu_name, filepath);
127 std::exit(EXIT_FAILURE);
128 }
129 }
130
131 template <typename hdu_t, class wcs_t, typename epoch_t>
132 void add_wcs(hdu_t *hdu, wcs_t &wcs, const epoch_t epoch) {
133 // add equinox
134 hdu->addKey("EQUINOX", epoch, "WCS: Equinox");
135
136 for (Eigen::Index i=0; i<wcs.ctype.size(); ++i) {
137 hdu->addKey("CTYPE"+std::to_string(i+1), wcs.ctype[i], "WCS: Projection Type " +std::to_string(i+1));
138 hdu->addKey("CUNIT"+std::to_string(i+1), wcs.cunit[i], "WCS: Axis Unit " +std::to_string(i+1));
139 hdu->addKey("CRVAL"+std::to_string(i+1), wcs.crval[i], "WCS: Ref Pixel Value " +std::to_string(i+1));
140 hdu->addKey("CDELT"+std::to_string(i+1), wcs.cdelt[i], "WCS: Pixel Scale " +std::to_string(i+1));
141 // add one to crpix due to FITS convention
142 hdu->addKey("CRPIX"+std::to_string(i+1), wcs.crpix[i] + 1, "WCS: Ref Pixel " +std::to_string(i+1));
143 }
144 }
145};
Definition fits_io.h:10
fitsIO()
Definition fits_io.h:26
std::string pixel_axes
Definition fits_io.h:24
void add_wcs(hdu_t *hdu, wcs_t &wcs, const epoch_t epoch)
Definition fits_io.h:132
auto get_hdu(std::string hdu_name)
Definition fits_io.h:90
std::vector< ext_hdu_t > hdus
Definition fits_io.h:22
void add_hdu(std::string hdu_name, Eigen::DenseBase< Derived > &data)
Definition fits_io.h:58
std::unique_ptr< CCfits::FITS > pfits
Definition fits_io.h:19
fitsIO(std::string _f, std::string _a)
Definition fits_io.h:29
std::string filepath
Definition fits_io.h:16
std::shared_ptr< spdlog::logger > logger
Definition fits_io.h:13
file_type_enum
Definition fits_io.h:5
@ write_fits
Definition fits_io.h:7
@ read_fits
Definition fits_io.h:6