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