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::FormatterStorage> 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");
187 auto boost = std::dynamic_pointer_cast<dafPersist::BoostStorage>(storage);
189 LOGL_DEBUG(_log,
"ExposureFormatter write BoostStorage");
190 boost->getOArchive() & *ip;
191 LOGL_DEBUG(_log,
"ExposureFormatter write end");
194 auto fits = std::dynamic_pointer_cast<dafPersist::FitsStorage>(storage);
196 LOGL_DEBUG(_log,
"ExposureFormatter write FitsStorage");
199 LOGL_DEBUG(_log,
"ExposureFormatter write end");
202 auto db =
dynamic_cast<dafPersist::DbStorage*
>(storage.get());
204 LOGL_DEBUG(_log,
"ExposureFormatter write DbStorage");
207 std::shared_ptr<daf::base::PropertySet> wcsProps = ip->
getWcs()->getFitsMetadata();
210 std::shared_ptr<daf::base::PropertySet> dp = ip->
getMetadata();
212 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
213 "Unable to retrieve metadata from MaskedImage's Image");
217 std::string itemName = additionalData->get<std::string>(
"itemName");
218 std::string tableName = itemName;
219 if (_policy->exists(itemName)) {
220 std::shared_ptr<pex::policy::Policy> itemPolicy = _policy->getPolicy(itemName);
221 if (itemPolicy->exists(
"TableName")) {
222 tableName = itemPolicy->getString(
"TableName");
225 if (tableName !=
"Raw_Amp_Exposure" && tableName !=
"Science_Amp_Exposure") {
226 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
227 "Unknown table name for persisting Exposure to DbStorage: " + tableName +
228 "for item " + itemName);
230 db->setTableForInsert(tableName);
240 if (tableName ==
"Raw_Amp_Exposure") {
241 db->setColumn<
long long>(
"rawAmpExposureId", ampExposureId);
242 db->setColumn<
long long>(
"rawCCDExposureId", ccdExposureId);
243 db->setColumn<
long long>(
"rawFPAExposureId", fpaExposureId);
245 db->setColumn<
long long>(
"scienceAmpExposureId", ampExposureId);
246 db->setColumn<
long long>(
"scienceCCDExposureId", ccdExposureId);
247 db->setColumn<
long long>(
"scienceFPAExposureId", fpaExposureId);
248 db->setColumn<
long long>(
"rawAmpExposureId", ampExposureId);
253 db->setColumn<
int>(
"ampId", ampId);
256 setColumn<std::string>(db,
"url", additionalData,
"StorageLocation.FitsStorage");
259 setColumn<std::string>(db,
"ctype1", wcsProps,
"CTYPE1");
260 setColumn<std::string>(db,
"ctype2", wcsProps,
"CTYPE2");
261 setColumn<float, double>(db,
"crpix1", wcsProps,
"CRPIX1");
262 setColumn<float, double>(db,
"crpix2", wcsProps,
"CRPIX2");
263 setColumn<double>(db,
"crval1", wcsProps,
"CRVAL1");
264 setColumn<double>(db,
"crval2", wcsProps,
"CRVAL2");
265 if (tableName ==
"Raw_Amp_Exposure") {
266 setColumn<double>(db,
"cd11", wcsProps,
"CD1_1");
267 setColumn<double>(db,
"cd21", wcsProps,
"CD2_1");
268 setColumn<double>(db,
"cd12", wcsProps,
"CD1_2");
269 setColumn<double>(db,
"cd22", wcsProps,
"CD2_2");
271 setColumn<double>(db,
"cd1_1", wcsProps,
"CD1_1");
272 setColumn<double>(db,
"cd2_1", wcsProps,
"CD2_1");
273 setColumn<double>(db,
"cd1_2", wcsProps,
"CD1_2");
274 setColumn<double>(db,
"cd2_2", wcsProps,
"CD2_2");
277 if (tableName ==
"Science_Amp_Exposure") {
279 setColumn<float, double>(db,
"photoFlam", dp,
"PHOTFLAM");
280 setColumn<float, double>(db,
"photoZP", dp,
"PHOTZP");
286 LOGL_DEBUG(_log,
"ExposureFormatter write end");
289 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Exposure");
292 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
294 std::shared_ptr<dafPersist::FormatterStorage> storage,
295 std::shared_ptr<daf::base::PropertySet> additionalData) {
296 LOGL_DEBUG(_log,
"ExposureFormatter read start");
298 auto boost = std::dynamic_pointer_cast<dafPersist::BoostStorage>(storage);
300 LOGL_DEBUG(_log,
"ExposureFormatter read BoostStorage");
303 boost->getIArchive() & *ip;
304 LOGL_DEBUG(_log,
"ExposureFormatter read end");
307 auto fits = std::dynamic_pointer_cast<dafPersist::FitsStorage>(storage);
309 LOGL_DEBUG(_log,
"ExposureFormatter read FitsStorage");
311 if (additionalData->exists(
"llcX")) {
312 int llcX = additionalData->get<
int>(
"llcX");
313 int llcY = additionalData->get<
int>(
"llcY");
314 int width = additionalData->get<
int>(
"width");
315 int height = additionalData->get<
int>(
"height");
319 if (additionalData->exists(
"imageOrigin")) {
320 std::string originStr = additionalData->get<std::string>(
"imageOrigin");
321 if (originStr ==
"LOCAL") {
323 }
else if (originStr ==
"PARENT") {
326 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
327 (boost::format(
"Unknown ImageOrigin type %s specified in additional"
328 "data for retrieving Exposure from fits") %
336 LOGL_DEBUG(_log,
"ExposureFormatter read end");
339 auto db =
dynamic_cast<dafPersist::DbStorage*
>(storage.get());
341 LOGL_DEBUG(_log,
"ExposureFormatter read DbStorage");
344 std::string itemName = additionalData->get<std::string>(
"itemName");
345 std::string tableName = itemName;
346 if (_policy->exists(itemName)) {
347 std::shared_ptr<pex::policy::Policy> itemPolicy = _policy->getPolicy(itemName);
348 if (itemPolicy->exists(
"TableName")) {
349 tableName = itemPolicy->getString(
"TableName");
352 if (tableName !=
"Raw_Amp_Exposure" && tableName !=
"Science_Amp_Exposure") {
353 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
354 "Unknown table name for retrieving Exposure from DbStorage: " + tableName +
355 " for item " + itemName);
357 db->setTableForQuery(tableName);
360 db->condParam<int64_t>(
"id", additionalData->getAsInt64(
"ampExposureId"));
361 if (tableName ==
"Raw_Amp_Exposure") {
362 db->setQueryWhere(
"rawAmpExposureId = :id");
364 db->setQueryWhere(
"scienceAmpExposureId = :id");
367 db->outColumn(
"url");
369 if (tableName ==
"Science_Amp_Exposure") {
371 db->outColumn(
"ctype1");
372 db->outColumn(
"ctype2");
373 db->outColumn(
"crpix1");
374 db->outColumn(
"crpix2");
375 db->outColumn(
"crval1");
376 db->outColumn(
"crval2");
377 db->outColumn(
"cd11");
378 db->outColumn(
"cd21");
379 db->outColumn(
"cd12");
380 db->outColumn(
"cd22");
383 db->outColumn(
"photoFlam");
384 db->outColumn(
"photoZP");
390 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unable to retrieve row");
394 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Non-unique Exposure retrieved");
404 db->getColumnByPos<std::string>(0));
405 std::shared_ptr<daf::base::PropertySet> dp = ip->
getMetadata();
408 int filterId = db->getColumnByPos<
int>(1);
409 std::string filterName = lookupFilterName(db, filterId);
410 dp->set(
"FILTER", filterName);
418 LOGL_DEBUG(_log,
"ExposureFormatter read end");
421 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Exposure");
424 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
426 dafBase::Persistable*, std::shared_ptr<dafPersist::FormatterStorage>,
427 std::shared_ptr<daf::base::PropertySet>) {
430 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unexpected call to update for Exposure");
433 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
434 template <
class Archive>
436 Archive& ar,
unsigned int const, dafBase::Persistable* persistable) {
437 LOGL_DEBUG(_log,
"ExposureFormatter delegateSerialize start");
441 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Serializing non-Exposure");
443 std::shared_ptr<image::Wcs> wcs = ip->
getWcs();
445 LOGL_DEBUG(_log,
"ExposureFormatter delegateSerialize end");
448 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
449 std::shared_ptr<daf::persistence::Formatter>
451 std::shared_ptr<pex::policy::Policy> policy) {
452 typedef std::shared_ptr<daf::persistence::Formatter> FormPtr;
457 #define INSTANTIATE(I, M, V) \
458 template class ExposureFormatter<I, M, V>; \
459 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_oarchive>( \
460 boost::archive::text_oarchive&, unsigned int const, dafBase::Persistable*); \
461 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_iarchive>( \
462 boost::archive::text_iarchive&, unsigned int const, dafBase::Persistable*); \
463 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_oarchive>( \
464 boost::archive::binary_oarchive&, unsigned int const, dafBase::Persistable*); \
465 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_iarchive>( \
466 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.