28 #ifndef DOXYGEN // Doxygen doesn't like includes inside namespaces
34 using namespace posix;
42 #include "lsst/pex/exceptions.h"
43 #include "boost/any.hpp"
51 #define FITS_SIZE 2880
56 Card(
const std::string &
name,
bool val,
const char *commnt =
"")
57 : keyword(name), value(val), comment(commnt) {}
58 Card(
const std::string &name,
int val,
const char *commnt =
"")
59 : keyword(name), value(val), comment(commnt) {}
60 Card(
const std::string &name,
double val,
const char *commnt =
"")
61 : keyword(name), value(val), comment(commnt) {}
62 Card(
const std::string &name,
float val,
const char *commnt =
"")
63 : keyword(name), value(val), comment(commnt) {}
64 Card(
const std::string &name,
const std::string &val,
const char *commnt =
"")
65 : keyword(name), value(val), comment(commnt) {}
66 Card(
const std::string &name,
const char *val,
const char *commnt =
"")
67 : keyword(name), value(std::string(val)), comment(commnt) {}
71 int write(
int fd,
int ncard,
char *record)
const;
81 int Card::write(
int fd,
int ncard,
char *record)
const {
82 char *card = &record[80 * ncard];
84 if (value.type() ==
typeid(std::string)) {
85 const char *str = boost::any_cast<std::string>(value).c_str();
86 if (keyword ==
"" || keyword ==
"COMMENT" || keyword ==
"END" || keyword ==
"HISTORY") {
87 sprintf(card,
"%-8.8s%-72s", keyword.c_str(), str);
89 sprintf(card,
"%-8.8s= '%s' %c%-*s", keyword.c_str(), str, (comment ==
"" ?
' ' :
'/'),
90 (
int)(80 - 14 - strlen(str)), comment.c_str());
93 sprintf(card,
"%-8.8s= ", keyword.c_str());
95 if (value.type() ==
typeid(bool)) {
96 sprintf(card,
"%20s", boost::any_cast<bool>(value) ?
"T" :
"F");
97 }
else if (value.type() ==
typeid(int)) {
98 sprintf(card,
"%20d", boost::any_cast<int>(value));
99 }
else if (value.type() ==
typeid(double)) {
100 sprintf(card,
"%20.10f", boost::any_cast<double>(value));
101 }
else if (value.type() ==
typeid(float)) {
102 sprintf(card,
"%20.7f", boost::any_cast<float>(value));
105 sprintf(card,
" %c%-48s", (comment ==
"" ?
' ' :
'/'), comment.c_str());
112 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Cannot write header record");
127 void flip_high_bit(
char *arr,
130 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
131 (boost::format(
"Attempt to bit flip odd number of bytes: %d") % n).str());
134 unsigned short *uarr =
reinterpret_cast<unsigned short *
>(arr);
135 for (
unsigned short *
end = uarr + n / 2; uarr <
end; ++uarr) {
145 void swap_2(
char *arr,
148 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
149 (boost::format(
"Attempt to byte swap odd number of bytes: %d") % n).str());
152 for (
char *
end = arr + n; arr <
end; arr += 2) {
161 void swap_4(
char *arr,
164 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
165 (boost::format(
"Attempt to byte swap non-multiple of 4 bytes: %d") % n).str());
168 for (
char *
end = arr + n; arr <
end; arr += 4) {
181 void swap_8(
char *arr,
184 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
185 (boost::format(
"Attempt to byte swap non-multiple of 8 bytes: %d") % n).str());
188 for (
char *
end = arr + n; arr <
end; arr += 8) {
204 int write_fits_hdr(
int fd,
int bitpix,
int naxis,
int *naxes, std::list<Card> &cards,
212 Card card(
"SIMPLE",
true);
213 ncard = card.write(fd, ncard, record);
215 Card card(
"XTENSION",
"IMAGE");
216 ncard = card.write(fd, ncard, record);
220 Card card(
"BITPIX", bitpix);
221 ncard = card.write(fd, ncard, record);
224 Card card(
"NAXIS", naxis);
225 ncard = card.write(fd, ncard, record);
227 for (i = 0; i < naxis; i++) {
228 char key[] =
"NAXIS.";
229 sprintf(key,
"NAXIS%d", i + 1);
230 Card card(key, naxes[i]);
231 ncard = card.write(fd, ncard, record);
234 Card card(
"EXTEND",
true,
"There may be extensions");
235 ncard = card.write(fd, ncard, record);
240 for (std::list<Card>::const_iterator card = cards.begin(); card != cards.end(); card++) {
241 ncard = card->write(fd, ncard, record);
245 Card card(
"END",
"");
246 ncard = card.write(fd, ncard, record);
250 ncard = card.write(fd, ncard, record);
259 void pad_to_fits_record(
int fd,
263 const int bytes_per_pixel = (bitpix > 0 ? bitpix : -bitpix) / 8;
264 int nbyte = npixel * bytes_per_pixel;
270 memset(record,
' ', nbyte);
271 if (write(fd, record, nbyte) != nbyte) {
272 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
273 "error padding file to multiple of fits block size");
278 int write_fits_data(
int fd,
int bitpix,
char *begin,
char *
end) {
279 const int bytes_per_pixel = (bitpix > 0 ? bitpix : -bitpix) / 8;
281 #if defined(LSST_LITTLE_ENDIAN) // we'll need to byte swap FITS
282 if (bytes_per_pixel > 1) {
288 bool allocated =
false;
289 if (swap_bytes || bitpix == 16) {
290 buff =
new char[
FITS_SIZE * bytes_per_pixel];
294 int nbyte = end - begin;
296 for (
char *ptr = begin; ptr !=
end; nbyte -= nwrite, ptr += nwrite) {
297 if (end - ptr < nwrite) {
302 memcpy(buff, ptr, nwrite);
304 flip_high_bit(buff, nwrite);
307 if (bytes_per_pixel == 2) {
308 swap_2(buff, nwrite);
309 }
else if (bytes_per_pixel == 4) {
310 swap_4(buff, nwrite);
311 }
else if (bytes_per_pixel == 8) {
312 swap_8(buff, nwrite);
314 fprintf(stderr,
"You cannot get here\n");
319 memcpy(buff, ptr, nwrite);
320 flip_high_bit(buff, nwrite);
326 if (write(fd, buff, nwrite) != nwrite) {
327 perror(
"Error writing image: ");
337 return (nbyte == 0 ? 0 : -1);
341 addWcs(std::string
const & wcsName, std::list<Card> & cards,
int x0=0.0,
int y0=0.0)
343 cards.push_back(Card(str(boost::format(
"CRVAL1%s") % wcsName),
344 x0,
"(output) Column pixel of Reference Pixel"));
345 cards.push_back(Card(str(boost::format(
"CRVAL2%s") % wcsName),
346 y0,
"(output) Row pixel of Reference Pixel"));
347 cards.push_back(Card(str(boost::format(
"CRPIX1%s") % wcsName), 1.0,
348 "Column Pixel Coordinate of Reference"));
349 cards.push_back(Card(str(boost::format(
"CRPIX2%s") % wcsName), 1.0,
350 "Row Pixel Coordinate of Reference"));
351 cards.push_back(Card(str(boost::format(
"CTYPE1%s") % wcsName),
"LINEAR",
"Type of projection"));
352 cards.push_back(Card(str(boost::format(
"CTYPE1%s") % wcsName),
"LINEAR",
"Type of projection"));
353 cards.push_back(Card(str(boost::format(
"CUNIT1%s") % wcsName),
"PIXEL",
"Column unit"));
354 cards.push_back(Card(str(boost::format(
"CUNIT2%s") % wcsName),
"PIXEL",
"Row unit"));
362 template <
typename ImageT>
371 std::list<Card> cards;
375 int bitpix = lsst::afw::fits::getBitPix<typename ImageT::Pixel>();
377 cards.push_back(Card(
"BZERO", 32768.0,
""));
378 cards.push_back(Card(
"BSCALE", 1.0,
""));
380 }
else if (bitpix == 0) {
381 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unsupported image type");
386 addWcs(
"A", cards, data.getX0(), data.getY0());
393 cards.push_back(Card(
"OBJECT", title,
"Image being displayed"));
401 typedef std::vector<std::string> NameList;
403 std::shared_ptr<image::Wcs> newWcs = Wcs->
clone();
404 newWcs->shiftReferencePixel(-data.getX0(), -data.getY0());
406 std::shared_ptr<lsst::daf::base::PropertySet> metadata = newWcs->getFitsMetadata();
408 NameList paramNames = metadata->paramNames();
410 for (NameList::const_iterator i = paramNames.begin(), end = paramNames.end(); i !=
end; ++i) {
411 if (*i ==
"SIMPLE" || *i ==
"BITPIX" || *i ==
"NAXIS" || *i ==
"NAXIS1" || *i ==
"NAXIS2" ||
412 *i ==
"XTENSION" || *i ==
"PCOUNT" || *i ==
"GCOUNT") {
415 std::type_info
const &type = metadata->typeOf(*i);
416 if (type ==
typeid(
bool)) {
417 cards.push_back(Card(*i, metadata->get<
bool>(*i)));
418 }
else if (type ==
typeid(
int)) {
419 cards.push_back(Card(*i, metadata->get<
int>(*i)));
420 }
else if (type ==
typeid(
float)) {
421 cards.push_back(Card(*i, metadata->get<
float>(*i)));
422 }
else if (type ==
typeid(
double)) {
423 cards.push_back(Card(*i, metadata->get<
double>(*i)));
425 cards.push_back(Card(*i, metadata->get<std::string>(*i)));
434 naxes[0] = data.getWidth();
435 naxes[1] = data.getHeight();
437 write_fits_hdr(fd, bitpix, naxis, naxes, cards, 1);
438 for (
int y = 0; y != data.getHeight(); ++y) {
439 if (write_fits_data(fd, bitpix, (
char *)(data.row_begin(y)), (
char *)(data.row_end(y))) < 0) {
440 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
441 (boost::format(
"Error writing data for row %d") % y).str());
445 pad_to_fits_record(fd, data.getWidth() * data.getHeight(), bitpix);
448 template <
typename ImageT>
455 if ((filename.c_str())[0] ==
'|') {
456 const char *cmd = filename.c_str() + 1;
457 while (isspace(*cmd)) {
461 fd = fileno(popen(cmd,
"w"));
463 fd = creat(filename.c_str(), 777);
467 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
468 (boost::format(
"Cannot open \"%s\"") % filename).str());
473 }
catch (lsst::pex::exceptions::Exception &) {
482 #define INSTANTIATE(IMAGET) \
483 template void writeBasicFits(int, IMAGET const &, image::Wcs const *, char const *); \
484 template void writeBasicFits(std::string const &, IMAGET const &, image::Wcs const *, char const *)
486 #define INSTANTIATE_IMAGE(T) INSTANTIATE(lsst::afw::image::Image<T>)
487 #define INSTANTIATE_MASK(T) INSTANTIATE(lsst::afw::image::Mask<T>)
489 INSTANTIATE_IMAGE(std::uint16_t);
490 INSTANTIATE_IMAGE(
int);
491 INSTANTIATE_IMAGE(
float);
492 INSTANTIATE_IMAGE(
double);
493 INSTANTIATE_IMAGE(std::uint64_t);
495 INSTANTIATE_MASK(std::uint16_t);
virtual std::shared_ptr< Wcs > clone(void) const
void writeBasicFits(int fd, ImageT const &data, image::Wcs const *Wcs, char const *title)
Implementation of the WCS standard for a any projection.
bool any(CoordinateExpr< N > const &expr)
Return true if any elements are true.
table::Key< std::string > name
std::int32_t MaskPixel
default type for Masks and MaskedImage Masks
lsst::daf::base::PropertySet PropertySet