30 #define __attribute__(x)
38 #include "boost/serialization/shared_ptr.hpp"
39 #include <boost/archive/binary_iarchive.hpp>
40 #include <boost/archive/binary_oarchive.hpp>
41 #include <boost/archive/text_iarchive.hpp>
42 #include <boost/archive/text_oarchive.hpp>
44 #include "lsst/daf/base.h"
45 #include "lsst/pex/exceptions.h"
46 #include "lsst/daf/persistence.h"
47 #include "lsst/log/Log.h"
48 #include "lsst/daf/persistence/PropertySetFormatter.h"
59 LOG_LOGGER _log = LOG_GET(
"afw.ExposureFormatter");
64 namespace formatters {
66 namespace dafBase = lsst::daf::base;
67 namespace dafPersist = lsst::daf::persistence;
69 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
72 static std::string
name();
77 static std::string
name =
"ExposureU";
82 static std::string
name =
"ExposureI";
87 static std::string
name =
"ExposureF";
92 static std::string
name =
"ExposureD";
97 static std::string
name =
"ExposureL";
101 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
102 daf::persistence::FormatterRegistration
107 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
109 std::shared_ptr<pex::policy::Policy> policy)
110 : daf::persistence::Formatter(typeid(this)), _policy(policy) {}
112 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
121 static std::string lookupFilterName(dafPersist::DbStorage* db,
int filterId) {
122 db->setTableForQuery(
"Filter");
123 db->outColumn(
"filterName");
124 db->condParam<
int>(
"id", filterId);
125 db->setQueryWhere(
"filterId = :id");
127 if (!db->next() || db->columnIsNull(0)) {
128 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
129 (boost::format(
"Unable to get name for filter id: %d") % filterId).str());
131 std::string filterName = db->getColumnByPos<std::string>(0);
133 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
134 (boost::format(
"Multiple names for filter id: %d") % filterId).str());
149 template <
typename T>
150 static void setColumn(dafPersist::DbStorage* db, std::string
const& colName,
151 std::shared_ptr<daf::base::PropertySet> source, std::string
const& propName) {
152 if (!source->exists(propName)) {
153 db->setColumnToNull(colName);
155 db->setColumn<T>(colName, source->get<T>(propName));
163 template <
typename T1,
typename T2>
164 static void setColumn(dafPersist::DbStorage* db,
165 std::string
const& colName,
166 std::shared_ptr<daf::base::PropertySet> source,
167 std::string
const& propName
169 if (!source->exists(propName)) {
170 db->setColumnToNull(colName);
172 db->setColumn<T1>(colName,
static_cast<T1
>(source->get<T2>(propName)));
176 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
178 dafBase::Persistable
const* persistable, std::shared_ptr<dafPersist::Storage> storage,
179 std::shared_ptr<daf::base::PropertySet> additionalData) {
180 LOGL_DEBUG(_log,
"ExposureFormatter write start");
184 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Persisting non-Exposure");
186 if (
typeid(*storage) ==
typeid(dafPersist::BoostStorage)) {
187 LOGL_DEBUG(_log,
"ExposureFormatter write BoostStorage");
188 dafPersist::BoostStorage* boost =
dynamic_cast<dafPersist::BoostStorage*
>(storage.get());
189 boost->getOArchive() & *ip;
190 LOGL_DEBUG(_log,
"ExposureFormatter write end");
192 }
else if (
typeid(*storage) ==
typeid(dafPersist::FitsStorage)) {
193 LOGL_DEBUG(_log,
"ExposureFormatter write FitsStorage");
194 dafPersist::FitsStorage* fits =
dynamic_cast<dafPersist::FitsStorage*
>(storage.get());
197 LOGL_DEBUG(_log,
"ExposureFormatter write end");
199 }
else if (
typeid(*storage) ==
typeid(dafPersist::DbStorage)) {
200 LOGL_DEBUG(_log,
"ExposureFormatter write DbStorage");
201 dafPersist::DbStorage* db =
dynamic_cast<dafPersist::DbStorage*
>(storage.get());
204 std::shared_ptr<daf::base::PropertySet> wcsProps = ip->
getWcs()->getFitsMetadata();
207 std::shared_ptr<daf::base::PropertySet> dp = ip->
getMetadata();
209 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
210 "Unable to retrieve metadata from MaskedImage's Image");
214 std::string itemName = additionalData->get<std::string>(
"itemName");
215 std::string tableName = itemName;
216 if (_policy->exists(itemName)) {
217 std::shared_ptr<pex::policy::Policy> itemPolicy = _policy->getPolicy(itemName);
218 if (itemPolicy->exists(
"TableName")) {
219 tableName = itemPolicy->getString(
"TableName");
222 if (tableName !=
"Raw_Amp_Exposure" && tableName !=
"Science_Amp_Exposure") {
223 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
224 "Unknown table name for persisting Exposure to DbStorage: " + tableName +
225 "for item " + itemName);
227 db->setTableForInsert(tableName);
237 if (tableName ==
"Raw_Amp_Exposure") {
238 db->setColumn<
long long>(
"rawAmpExposureId", ampExposureId);
239 db->setColumn<
long long>(
"rawCCDExposureId", ccdExposureId);
240 db->setColumn<
long long>(
"rawFPAExposureId", fpaExposureId);
242 db->setColumn<
long long>(
"scienceAmpExposureId", ampExposureId);
243 db->setColumn<
long long>(
"scienceCCDExposureId", ccdExposureId);
244 db->setColumn<
long long>(
"scienceFPAExposureId", fpaExposureId);
245 db->setColumn<
long long>(
"rawAmpExposureId", ampExposureId);
250 db->setColumn<
int>(
"ampId", ampId);
253 setColumn<std::string>(db,
"url", additionalData,
"StorageLocation.FitsStorage");
256 setColumn<std::string>(db,
"ctype1", wcsProps,
"CTYPE1");
257 setColumn<std::string>(db,
"ctype2", wcsProps,
"CTYPE2");
258 setColumn<float, double>(db,
"crpix1", wcsProps,
"CRPIX1");
259 setColumn<float, double>(db,
"crpix2", wcsProps,
"CRPIX2");
260 setColumn<double>(db,
"crval1", wcsProps,
"CRVAL1");
261 setColumn<double>(db,
"crval2", wcsProps,
"CRVAL2");
262 if (tableName ==
"Raw_Amp_Exposure") {
263 setColumn<double>(db,
"cd11", wcsProps,
"CD1_1");
264 setColumn<double>(db,
"cd21", wcsProps,
"CD2_1");
265 setColumn<double>(db,
"cd12", wcsProps,
"CD1_2");
266 setColumn<double>(db,
"cd22", wcsProps,
"CD2_2");
268 setColumn<double>(db,
"cd1_1", wcsProps,
"CD1_1");
269 setColumn<double>(db,
"cd2_1", wcsProps,
"CD2_1");
270 setColumn<double>(db,
"cd1_2", wcsProps,
"CD1_2");
271 setColumn<double>(db,
"cd2_2", wcsProps,
"CD2_2");
274 if (tableName ==
"Science_Amp_Exposure") {
276 setColumn<float, double>(db,
"photoFlam", dp,
"PHOTFLAM");
277 setColumn<float, double>(db,
"photoZP", dp,
"PHOTZP");
283 LOGL_DEBUG(_log,
"ExposureFormatter write end");
286 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unrecognized Storage for Exposure");
289 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
291 std::shared_ptr<dafPersist::Storage> storage,
292 std::shared_ptr<daf::base::PropertySet> additionalData) {
293 LOGL_DEBUG(_log,
"ExposureFormatter read start");
294 if (
typeid(*storage) ==
typeid(dafPersist::BoostStorage)) {
295 LOGL_DEBUG(_log,
"ExposureFormatter read BoostStorage");
296 dafPersist::BoostStorage* boost =
dynamic_cast<dafPersist::BoostStorage*
>(storage.get());
299 boost->getIArchive() & *ip;
300 LOGL_DEBUG(_log,
"ExposureFormatter read end");
302 }
else if (
typeid(*storage) ==
typeid(dafPersist::FitsStorage)) {
303 LOGL_DEBUG(_log,
"ExposureFormatter read FitsStorage");
304 dafPersist::FitsStorage* fits =
dynamic_cast<dafPersist::FitsStorage*
>(storage.get());
306 if (additionalData->exists(
"llcX")) {
307 int llcX = additionalData->get<
int>(
"llcX");
308 int llcY = additionalData->get<
int>(
"llcY");
309 int width = additionalData->get<
int>(
"width");
310 int height = additionalData->get<
int>(
"height");
314 if (additionalData->exists(
"imageOrigin")) {
315 std::string originStr = additionalData->get<std::string>(
"imageOrigin");
316 if (originStr ==
"LOCAL") {
318 }
else if (originStr ==
"PARENT") {
321 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
322 (boost::format(
"Unknown ImageOrigin type %s specified in additional"
323 "data for retrieving Exposure from fits") %
331 LOGL_DEBUG(_log,
"ExposureFormatter read end");
333 }
else if (
typeid(*storage) ==
typeid(dafPersist::DbStorage)) {
334 LOGL_DEBUG(_log,
"ExposureFormatter read DbStorage");
335 dafPersist::DbStorage* db =
dynamic_cast<dafPersist::DbStorage*
>(storage.get());
338 std::string itemName = additionalData->get<std::string>(
"itemName");
339 std::string tableName = itemName;
340 if (_policy->exists(itemName)) {
341 std::shared_ptr<pex::policy::Policy> itemPolicy = _policy->getPolicy(itemName);
342 if (itemPolicy->exists(
"TableName")) {
343 tableName = itemPolicy->getString(
"TableName");
346 if (tableName !=
"Raw_Amp_Exposure" && tableName !=
"Science_Amp_Exposure") {
347 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
348 "Unknown table name for retrieving Exposure from DbStorage: " + tableName +
349 " for item " + itemName);
351 db->setTableForQuery(tableName);
354 db->condParam<int64_t>(
"id", additionalData->getAsInt64(
"ampExposureId"));
355 if (tableName ==
"Raw_Amp_Exposure") {
356 db->setQueryWhere(
"rawAmpExposureId = :id");
358 db->setQueryWhere(
"scienceAmpExposureId = :id");
361 db->outColumn(
"url");
363 if (tableName ==
"Science_Amp_Exposure") {
365 db->outColumn(
"ctype1");
366 db->outColumn(
"ctype2");
367 db->outColumn(
"crpix1");
368 db->outColumn(
"crpix2");
369 db->outColumn(
"crval1");
370 db->outColumn(
"crval2");
371 db->outColumn(
"cd11");
372 db->outColumn(
"cd21");
373 db->outColumn(
"cd12");
374 db->outColumn(
"cd22");
377 db->outColumn(
"photoFlam");
378 db->outColumn(
"photoZP");
384 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unable to retrieve row");
388 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Non-unique Exposure retrieved");
398 db->getColumnByPos<std::string>(0));
399 std::shared_ptr<daf::base::PropertySet> dp = ip->
getMetadata();
402 int filterId = db->getColumnByPos<
int>(1);
403 std::string filterName = lookupFilterName(db, filterId);
404 dp->set(
"FILTER", filterName);
412 LOGL_DEBUG(_log,
"ExposureFormatter read end");
415 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unrecognized Storage for Exposure");
418 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
420 dafBase::Persistable*, std::shared_ptr<dafPersist::Storage>,
421 std::shared_ptr<daf::base::PropertySet>) {
424 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unexpected call to update for Exposure");
427 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
428 template <
class Archive>
430 Archive& ar,
unsigned int const, dafBase::Persistable* persistable) {
431 LOGL_DEBUG(_log,
"ExposureFormatter delegateSerialize start");
435 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Serializing non-Exposure");
437 std::shared_ptr<image::Wcs> wcs = ip->
getWcs();
439 LOGL_DEBUG(_log,
"ExposureFormatter delegateSerialize end");
442 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
443 std::shared_ptr<daf::persistence::Formatter>
445 std::shared_ptr<pex::policy::Policy> policy) {
446 typedef std::shared_ptr<daf::persistence::Formatter> FormPtr;
451 #define INSTANTIATE(I, M, V) \
452 template class ExposureFormatter<I, M, V>; \
453 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_oarchive>( \
454 boost::archive::text_oarchive&, unsigned int const, dafBase::Persistable*); \
455 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_iarchive>( \
456 boost::archive::text_iarchive&, unsigned int const, dafBase::Persistable*); \
457 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_oarchive>( \
458 boost::archive::binary_oarchive&, unsigned int const, dafBase::Persistable*); \
459 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_iarchive>( \
460 boost::archive::binary_iarchive&, unsigned int const, dafBase::Persistable*);
std::uint16_t MaskPixel
default type for Masks and MaskedImage Masks
A class to contain the data, WCS, and other information needed to describe an image of the sky...
void writeFits(std::string const &fileName) const
Write an Exposure to a regular multi-extension FITS file.
An integer coordinate rectangle.
table::Key< std::string > name
std::shared_ptr< Wcs const > getWcs() const
float VariancePixel
default type for MaskedImage variance images
std::shared_ptr< lsst::daf::base::PropertySet > getMetadata() const
Return flexible metadata.