SourceXtractorPlusPlus  0.8
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WCS.cpp
Go to the documentation of this file.
1 
17 /*
18  * WCS.cpp
19  *
20  * Created on: Nov 17, 2016
21  * Author: mschefer
22  */
23 
24 #include <fitsio.h>
25 
26 namespace wcslib {
27 
28 #include <wcslib/wcs.h>
29 #include <wcslib/wcshdr.h>
30 #include <wcslib/wcsfix.h>
31 #include <wcslib/wcsprintf.h>
32 #include <wcslib/getwcstab.h>
33 
34 }
35 
38 #include <boost/algorithm/string/trim.hpp>
39 
40 namespace SourceXtractor {
41 
42 using namespace wcslib;
43 
44 WCS::WCS(const std::string& fits_file_path, int hdu_number) : m_wcs(nullptr, nullptr) {
45  fitsfile *fptr = NULL;
46  int status = 0;
47  fits_open_file(&fptr, fits_file_path.c_str(), READONLY, &status);
48 
49  int hdu_type;
50  fits_movabs_hdu(fptr, hdu_number, &hdu_type, &status);
51 
52  if (status != 0 || hdu_type != IMAGE_HDU) {
53  throw Elements::Exception() << "Can't read WCS information from " << fits_file_path << " HDU " << hdu_number;
54  }
55 
56  int nkeyrec;
57  char* header;
58  fits_hdr2str(fptr, 1, NULL, 0, &header, &nkeyrec, &status);
59 
60  if (hdu_type == IMAGE_HDU) {
61  int nreject = 0, nwcs = 0;
62  wcsprm* wcs;
63  wcspih(header, nkeyrec, WCSHDR_all, 0, &nreject, &nwcs, &wcs);
64  wcsset(wcs);
65 
66  m_wcs = decltype(m_wcs)(wcs, [nwcs](wcsprm* wcs) {
67  int nwcs_copy = nwcs;
68  wcsvfree(&nwcs_copy, &wcs);
69  });
70  }
71 
72  free(header);
73  fits_close_file(fptr, &status);
74 }
75 
77 }
78 
80 
81  // +1 as fits standard coordinates start at 1
82  double pc_array[2] {image_coordinate.m_x + 1, image_coordinate.m_y + 1};
83 
84  double ic_array[2] {0, 0};
85  double wc_array[2] {0, 0};
86  double phi, theta;
87 
88  int status = 0;
89  wcsp2s(m_wcs.get(), 1, 1, pc_array, ic_array, &phi, &theta, wc_array, &status);
90 
91  return WorldCoordinate(wc_array[0], wc_array[1]);
92 }
93 
95  double pc_array[2] {0, 0};
96  double ic_array[2] {0, 0};
97  double wc_array[2] {world_coordinate.m_alpha, world_coordinate.m_delta};
98  double phi, theta;
99 
100  int status = 0;
101  wcss2p(m_wcs.get(), 1, 1, wc_array, &phi, &theta, ic_array, pc_array, &status);
102 
103  return ImageCoordinate(pc_array[0] - 1, pc_array[1] - 1); // -1 as fits standard coordinates start at 1
104 }
105 
107  int nkeyrec;
108  char *raw_header;
109 
110  if (wcshdo(WCSHDO_none, m_wcs.get(), &nkeyrec, &raw_header) != 0) {
111  throw Elements::Exception() << "Failed to get the FITS headers for the WCS coordinate system";
112  }
113 
115  for (int i = 0; i < nkeyrec; ++i) {
116  char *hptr = &raw_header[80 * i];
117  std::string key(hptr, hptr + 8);
118  boost::trim(key);
119  std::string value(hptr + 9, hptr + 72);
120  boost::trim(value);
121  if (!key.empty()) {
122  headers.emplace(std::make_pair(key, value));
123  }
124  }
125 
126  free(raw_header);
127  return headers;
128 }
129 
130 }
T empty(T...args)
int wcspih(char *header, int nkeyrec, int relax, int ctrl, int *nreject, int *nwcs, struct wcsprm **wcs)
T free(T...args)
WCS(const std::string &fits_file_path, int hdu_number=1)
Definition: WCS.cpp:44
int wcsvfree(int *nwcs, struct wcsprm **wcs)
STL class.
WorldCoordinate imageToWorld(ImageCoordinate image_coordinate) const override
Definition: WCS.cpp:79
int wcshdo(int relax, struct wcsprm *wcs, int *nkeyrec, char **header)
T make_pair(T...args)
std::map< std::string, std::string > getFitsHeaders() const override
Definition: WCS.cpp:106
#define WCSHDR_all
Definition: WCS.cpp:1064
virtual ~WCS()
Definition: WCS.cpp:76
int wcsp2s(struct wcsprm *wcs, int ncoord, int nelem, const double pixcrd[], double imgcrd[], double phi[], double theta[], double world[], int stat[])
int wcss2p(struct wcsprm *wcs, int ncoord, int nelem, const double world[], double phi[], double theta[], double imgcrd[], double pixcrd[], int stat[])
T c_str(T...args)
T emplace(T...args)
int wcsset(struct wcsprm *wcs)
ImageCoordinate worldToImage(WorldCoordinate world_coordinate) const override
Definition: WCS.cpp:94
std::unique_ptr< wcslib::wcsprm, std::function< void(wcslib::wcsprm *)> > m_wcs
Definition: WCS.h:46