// Astrophysics Science Division, // NASA/ Goddard Space Flight Center // HEASARC // http://heasarc.gsfc.nasa.gov // e-mail: ccfits@legacy.gsfc.nasa.gov // // Original author: Ben Dorman #ifndef PHDUT_H #define PHDUT_H #include "PrimaryHDU.h" #include #include namespace CCfits { template void PHDU::read (std::valarray& image) { long init(1); long nElements(std::accumulate(naxes().begin(),naxes().end(),init, std::multiplies())); read(image,1,nElements,static_cast(0)); } template void PHDU::read (std::valarray& image, long first,long nElements) { read(image, first,nElements,static_cast(0)); } template void PHDU::read (std::valarray& image, long first, long nElements, S* nullValue) { makeThisCurrent(); if ( PrimaryHDU* phdu = dynamic_cast*>(this) ) { // proceed if cast is successful. const std::valarray& __tmp = phdu->readImage(first,nElements,nullValue); image.resize(__tmp.size()); image = __tmp; } else { if (bitpix() == Ifloat) { PrimaryHDU& phdu = dynamic_cast&>(*this); float nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); } else if (bitpix() == Idouble) { PrimaryHDU& phdu = dynamic_cast&>(*this); double nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); } else if (bitpix() == Ibyte) { PrimaryHDU& phdu = dynamic_cast&>(*this); unsigned char nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); } else if (bitpix() == Ilong) { if ( zero() == ULBASE && scale() == 1) { PrimaryHDU& phdu = dynamic_cast&>(*this); unsigned INT32BIT nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); } else { PrimaryHDU& phdu = dynamic_cast&>(*this); INT32BIT nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); } } else if (bitpix() == Ilonglong) { PrimaryHDU& phdu = dynamic_cast&>(*this); LONGLONG nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); } else if (bitpix() == Ishort) { if ( zero() == USBASE && scale() == 1) { PrimaryHDU& phdu = dynamic_cast&>(*this); unsigned short nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); } else { PrimaryHDU& phdu = dynamic_cast&>(*this); short nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(first,nElements, &nulVal)); } } else { throw CCfits::FitsFatal(" casting image types "); } } } template void PHDU::read (std::valarray& image, const std::vector& first, long nElements, S* nullValue) { makeThisCurrent(); long firstElement(0); long dimSize(1); std::vector inputDimensions(naxis(),1); size_t sNaxis = static_cast(naxis()); size_t n(std::min(sNaxis,first.size())); std::copy(&first[0],&first[0]+n,&inputDimensions[0]); for (long i = 0; i < naxis(); ++i) { firstElement += ((inputDimensions[i] - 1)*dimSize); dimSize *=naxes(i); } ++firstElement; read(image, firstElement,nElements,nullValue); } template void PHDU::read (std::valarray& image, const std::vector& first, long nElements) { read(image, first,nElements,static_cast(0)); } template void PHDU::read (std::valarray& image, const std::vector& firstVertex, const std::vector& lastVertex, const std::vector& stride, S* nullValue) { makeThisCurrent(); if (PrimaryHDU* phdu = dynamic_cast*>(this)) { const std::valarray& __tmp = phdu->readImage(firstVertex,lastVertex,stride,nullValue); image.resize(__tmp.size()); image = __tmp; } else { // FITSutil::fill will take care of sizing. if (bitpix() == Ifloat) { float nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); PrimaryHDU& phdu = dynamic_cast&>(*this); FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); } else if (bitpix() == Idouble) { PrimaryHDU& phdu = dynamic_cast&>(*this); double nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); } else if (bitpix() == Ibyte) { PrimaryHDU& phdu = dynamic_cast&>(*this); unsigned char nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); } else if (bitpix() == Ilong) { if ( zero() == ULBASE && scale() == 1) { PrimaryHDU& phdu = dynamic_cast&>(*this); unsigned INT32BIT nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); } else { PrimaryHDU& phdu = dynamic_cast&>(*this); INT32BIT nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); } } else if (bitpix() == Ilonglong) { PrimaryHDU& phdu = dynamic_cast&>(*this); LONGLONG nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); } else if (bitpix() == Ishort) { if ( zero() == USBASE && scale() == 1) { PrimaryHDU& phdu = dynamic_cast&>(*this); unsigned short nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); } else { PrimaryHDU& phdu = dynamic_cast&>(*this); short nulVal(0); if (nullValue) nulVal = static_cast(*nullValue); FITSUtil::fill(image,phdu.readImage(firstVertex,lastVertex,stride,&nulVal)); } } else { throw CCfits::FitsFatal(" casting image types "); } } } template void PHDU::read (std::valarray& image, const std::vector& firstVertex, const std::vector& lastVertex, const std::vector& stride) { read(image, firstVertex,lastVertex,stride,static_cast(0)); } template void PHDU::write(long first, long nElements, const std::valarray& data, S* nullValue) { makeThisCurrent(); if (PrimaryHDU* image = dynamic_cast*>(this)) { image->writeImage(first,nElements,data,nullValue); } else { if (bitpix() == Ifloat) { std::valarray __tmp; PrimaryHDU& phdu = dynamic_cast&>(*this); FITSUtil::fill(__tmp,data); float* pfNullValue = 0; float fNullValue = 0.0; if (nullValue) { fNullValue = static_cast(*nullValue); pfNullValue = &fNullValue; } phdu.writeImage(first,nElements,__tmp, pfNullValue); } else if (bitpix() == Idouble) { std::valarray __tmp; PrimaryHDU& phdu = dynamic_cast&>(*this); FITSUtil::fill(__tmp,data); double* pdNullValue = 0; double dNullValue = 0.0; if (nullValue) { dNullValue = static_cast(*nullValue); pdNullValue = &dNullValue; } phdu.writeImage(first,nElements,__tmp, pdNullValue); } else if (bitpix() == Ibyte) { PrimaryHDU& phdu = dynamic_cast&>(*this); std::valarray __tmp; FITSUtil::fill(__tmp,data); unsigned char *pbNull=0; unsigned char bNull=0; if (nullValue) { bNull = static_cast(*nullValue); pbNull = &bNull; } phdu.writeImage(first,nElements,__tmp, pbNull); } else if (bitpix() == Ilong) { if ( zero() == ULBASE && scale() == 1) { PrimaryHDU& phdu = dynamic_cast&>(*this); std::valarray __tmp; FITSUtil::fill(__tmp,data); unsigned INT32BIT *plNull=0; unsigned INT32BIT lNull=0; if (nullValue) { lNull = static_cast(*nullValue); plNull = &lNull; } phdu.writeImage(first,nElements,__tmp, plNull); } else { PrimaryHDU& phdu = dynamic_cast&>(*this); std::valarray __tmp; FITSUtil::fill(__tmp,data); INT32BIT *plNull=0; INT32BIT lNull=0; if (nullValue) { lNull = static_cast(*nullValue); plNull = &lNull; } phdu.writeImage(first,nElements,__tmp, plNull); } } else if (bitpix() == Ilonglong) { PrimaryHDU& phdu = dynamic_cast&>(*this); std::valarray __tmp; FITSUtil::fill(__tmp,data); LONGLONG *pllNull=0; LONGLONG llNull=0; if (nullValue) { llNull = static_cast(*nullValue); pllNull = &llNull; } phdu.writeImage(first,nElements,__tmp, pllNull); } else if (bitpix() == Ishort) { if ( zero() == USBASE && scale() == 1) { PrimaryHDU& phdu = dynamic_cast&>(*this); std::valarray __tmp; FITSUtil::fill(__tmp,data); unsigned short *psNull=0; unsigned short sNull=0; if (nullValue) { sNull = static_cast(*nullValue); psNull = &sNull; } phdu.writeImage(first,nElements,__tmp, psNull); } else { PrimaryHDU& phdu = dynamic_cast&>(*this); std::valarray __tmp; FITSUtil::fill(__tmp,data); short *psNull=0; short sNull=0; if (nullValue) { sNull = static_cast(*nullValue); psNull = &sNull; } phdu.writeImage(first,nElements,__tmp,psNull); } } else { FITSUtil::MatchType errType; throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType())); } } } template void PHDU::write(long first, long nElements, const std::valarray& data) { write(first, nElements, data, static_cast(0)); } template void PHDU::write(const std::vector& first, long nElements, const std::valarray& data, S* nullValue) { makeThisCurrent(); size_t n(first.size()); long firstElement(0); long dimSize(1); for (long i = 0; i < n; ++i) { firstElement += ((first[i] - 1)*dimSize); dimSize *=naxes(i); } ++firstElement; write(firstElement,nElements,data,nullValue); } template void PHDU::write(const std::vector& first, long nElements, const std::valarray& data) { makeThisCurrent(); size_t n(first.size()); long firstElement(0); long dimSize(1); for (long i = 0; i < n; ++i) { firstElement += ((first[i] - 1)*dimSize); dimSize *=naxes(i); } ++firstElement; write(firstElement,nElements,data); } template void PHDU::write(const std::vector& firstVertex, const std::vector& lastVertex, const std::vector& stride, const std::valarray& data) { makeThisCurrent(); try { PrimaryHDU& image = dynamic_cast&>(*this); image.writeImage(firstVertex,lastVertex,stride,data); } catch (std::bad_cast) { // write input type S to Image type... if (bitpix() == Ifloat) { PrimaryHDU& phdu = dynamic_cast&>(*this); size_t n(data.size()); std::valarray __tmp(n); for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; phdu.writeImage(firstVertex,lastVertex,stride,__tmp); } else if (bitpix() == Idouble) { PrimaryHDU& phdu = dynamic_cast&>(*this); size_t n(data.size()); std::valarray __tmp(n); for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; phdu.writeImage(firstVertex,lastVertex,stride,__tmp); } else if (bitpix() == Ibyte) { PrimaryHDU& phdu = dynamic_cast&>(*this); size_t n(data.size()); std::valarray __tmp(n); for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; phdu.writeImage(firstVertex,lastVertex,stride,__tmp); } else if (bitpix() == Ilong) { if ( zero() == ULBASE && scale() == 1) { PrimaryHDU& phdu = dynamic_cast&>(*this); size_t n(data.size()); std::valarray __tmp(n); for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; phdu.writeImage(firstVertex,lastVertex,stride,__tmp); } else { PrimaryHDU& phdu = dynamic_cast&>(*this); size_t n(data.size()); std::valarray __tmp(n); for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; phdu.writeImage(firstVertex,lastVertex,stride,__tmp); } } else if (bitpix() == Ilonglong) { PrimaryHDU& phdu = dynamic_cast&>(*this); size_t n(data.size()); std::valarray __tmp(n); for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; phdu.writeImage(firstVertex,lastVertex,stride,__tmp); } else if (bitpix() == Ishort) { if ( zero() == USBASE && scale() == 1) { PrimaryHDU& phdu = dynamic_cast&>(*this); size_t n(data.size()); std::valarray __tmp(n); for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; phdu.writeImage(firstVertex,lastVertex,stride,__tmp); } else { PrimaryHDU& phdu = dynamic_cast&>(*this); size_t n(data.size()); std::valarray __tmp(n); for (size_t j= 0; j < n; ++j) __tmp[j] = data[j]; phdu.writeImage(firstVertex,lastVertex,stride,__tmp); } } else { FITSUtil::MatchType errType; throw FITSUtil::UnrecognizedType(FITSUtil::FITSType2String(errType())); } } } } // namespace CCfits #endif