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 if (additionalData) {
204 }
catch (std::exception
const& exc) {
205 LOGLS_WARN(_log,
"Unable to construct Exposure write options (" << exc.what() <<
206 "); writing with default options");
210 ip->
writeFits(fits->getPath(), imageOptions, maskOptions, varianceOptions);
211 LOGL_DEBUG(_log,
"ExposureFormatter write end");
214 auto db =
dynamic_cast<dafPersist::DbStorage*
>(storage.get());
216 LOGL_DEBUG(_log,
"ExposureFormatter write DbStorage");
219 std::shared_ptr<daf::base::PropertySet> wcsProps = ip->
getWcs()->getFitsMetadata();
222 std::shared_ptr<daf::base::PropertySet> dp = ip->
getMetadata();
224 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
225 "Unable to retrieve metadata from MaskedImage's Image");
229 std::string itemName = additionalData->get<std::string>(
"itemName");
230 std::string tableName = itemName;
231 if (_policy->exists(itemName)) {
232 std::shared_ptr<pex::policy::Policy> itemPolicy = _policy->getPolicy(itemName);
233 if (itemPolicy->exists(
"TableName")) {
234 tableName = itemPolicy->getString(
"TableName");
237 if (tableName !=
"Raw_Amp_Exposure" && tableName !=
"Science_Amp_Exposure") {
238 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
239 "Unknown table name for persisting Exposure to DbStorage: " + tableName +
240 "for item " + itemName);
242 db->setTableForInsert(tableName);
252 if (tableName ==
"Raw_Amp_Exposure") {
253 db->setColumn<
long long>(
"rawAmpExposureId", ampExposureId);
254 db->setColumn<
long long>(
"rawCCDExposureId", ccdExposureId);
255 db->setColumn<
long long>(
"rawFPAExposureId", fpaExposureId);
257 db->setColumn<
long long>(
"scienceAmpExposureId", ampExposureId);
258 db->setColumn<
long long>(
"scienceCCDExposureId", ccdExposureId);
259 db->setColumn<
long long>(
"scienceFPAExposureId", fpaExposureId);
260 db->setColumn<
long long>(
"rawAmpExposureId", ampExposureId);
265 db->setColumn<
int>(
"ampId", ampId);
268 setColumn<std::string>(db,
"url", additionalData,
"StorageLocation.FitsStorage");
271 setColumn<std::string>(db,
"ctype1", wcsProps,
"CTYPE1");
272 setColumn<std::string>(db,
"ctype2", wcsProps,
"CTYPE2");
273 setColumn<float, double>(db,
"crpix1", wcsProps,
"CRPIX1");
274 setColumn<float, double>(db,
"crpix2", wcsProps,
"CRPIX2");
275 setColumn<double>(db,
"crval1", wcsProps,
"CRVAL1");
276 setColumn<double>(db,
"crval2", wcsProps,
"CRVAL2");
277 if (tableName ==
"Raw_Amp_Exposure") {
278 setColumn<double>(db,
"cd11", wcsProps,
"CD1_1");
279 setColumn<double>(db,
"cd21", wcsProps,
"CD2_1");
280 setColumn<double>(db,
"cd12", wcsProps,
"CD1_2");
281 setColumn<double>(db,
"cd22", wcsProps,
"CD2_2");
283 setColumn<double>(db,
"cd1_1", wcsProps,
"CD1_1");
284 setColumn<double>(db,
"cd2_1", wcsProps,
"CD2_1");
285 setColumn<double>(db,
"cd1_2", wcsProps,
"CD1_2");
286 setColumn<double>(db,
"cd2_2", wcsProps,
"CD2_2");
289 if (tableName ==
"Science_Amp_Exposure") {
291 setColumn<float, double>(db,
"photoFlam", dp,
"PHOTFLAM");
292 setColumn<float, double>(db,
"photoZP", dp,
"PHOTZP");
298 LOGL_DEBUG(_log,
"ExposureFormatter write end");
301 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Exposure");
304 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
306 std::shared_ptr<dafPersist::FormatterStorage> storage,
307 std::shared_ptr<daf::base::PropertySet> additionalData) {
308 LOGL_DEBUG(_log,
"ExposureFormatter read start");
310 auto boost = std::dynamic_pointer_cast<dafPersist::BoostStorage>(storage);
312 LOGL_DEBUG(_log,
"ExposureFormatter read BoostStorage");
315 boost->getIArchive() & *ip;
316 LOGL_DEBUG(_log,
"ExposureFormatter read end");
319 auto fits = std::dynamic_pointer_cast<dafPersist::FitsStorage>(storage);
321 LOGL_DEBUG(_log,
"ExposureFormatter read FitsStorage");
323 if (additionalData->exists(
"llcX")) {
324 int llcX = additionalData->get<
int>(
"llcX");
325 int llcY = additionalData->get<
int>(
"llcY");
326 int width = additionalData->get<
int>(
"width");
327 int height = additionalData->get<
int>(
"height");
331 if (additionalData->exists(
"imageOrigin")) {
332 std::string originStr = additionalData->get<std::string>(
"imageOrigin");
333 if (originStr ==
"LOCAL") {
335 }
else if (originStr ==
"PARENT") {
338 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
339 (boost::format(
"Unknown ImageOrigin type %s specified in additional"
340 "data for retrieving Exposure from fits") %
348 LOGL_DEBUG(_log,
"ExposureFormatter read end");
351 auto db =
dynamic_cast<dafPersist::DbStorage*
>(storage.get());
353 LOGL_DEBUG(_log,
"ExposureFormatter read DbStorage");
356 std::string itemName = additionalData->get<std::string>(
"itemName");
357 std::string tableName = itemName;
358 if (_policy->exists(itemName)) {
359 std::shared_ptr<pex::policy::Policy> itemPolicy = _policy->getPolicy(itemName);
360 if (itemPolicy->exists(
"TableName")) {
361 tableName = itemPolicy->getString(
"TableName");
364 if (tableName !=
"Raw_Amp_Exposure" && tableName !=
"Science_Amp_Exposure") {
365 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
366 "Unknown table name for retrieving Exposure from DbStorage: " + tableName +
367 " for item " + itemName);
369 db->setTableForQuery(tableName);
372 db->condParam<int64_t>(
"id", additionalData->getAsInt64(
"ampExposureId"));
373 if (tableName ==
"Raw_Amp_Exposure") {
374 db->setQueryWhere(
"rawAmpExposureId = :id");
376 db->setQueryWhere(
"scienceAmpExposureId = :id");
379 db->outColumn(
"url");
381 if (tableName ==
"Science_Amp_Exposure") {
383 db->outColumn(
"ctype1");
384 db->outColumn(
"ctype2");
385 db->outColumn(
"crpix1");
386 db->outColumn(
"crpix2");
387 db->outColumn(
"crval1");
388 db->outColumn(
"crval2");
389 db->outColumn(
"cd11");
390 db->outColumn(
"cd21");
391 db->outColumn(
"cd12");
392 db->outColumn(
"cd22");
395 db->outColumn(
"photoFlam");
396 db->outColumn(
"photoZP");
402 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unable to retrieve row");
406 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Non-unique Exposure retrieved");
416 db->getColumnByPos<std::string>(0));
417 std::shared_ptr<daf::base::PropertySet> dp = ip->
getMetadata();
420 int filterId = db->getColumnByPos<
int>(1);
421 std::string filterName = lookupFilterName(db, filterId);
422 dp->set(
"FILTER", filterName);
430 LOGL_DEBUG(_log,
"ExposureFormatter read end");
433 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Exposure");
436 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
438 dafBase::Persistable*, std::shared_ptr<dafPersist::FormatterStorage>,
439 std::shared_ptr<daf::base::PropertySet>) {
442 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Unexpected call to update for Exposure");
445 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
446 template <
class Archive>
448 Archive& ar,
unsigned int const, dafBase::Persistable* persistable) {
449 LOGL_DEBUG(_log,
"ExposureFormatter delegateSerialize start");
453 throw LSST_EXCEPT(pex::exceptions::RuntimeError,
"Serializing non-Exposure");
455 std::shared_ptr<image::Wcs> wcs = ip->
getWcs();
457 LOGL_DEBUG(_log,
"ExposureFormatter delegateSerialize end");
460 template <
typename ImagePixelT,
typename MaskPixelT,
typename VariancePixelT>
461 std::shared_ptr<daf::persistence::Formatter>
463 std::shared_ptr<pex::policy::Policy> policy) {
464 typedef std::shared_ptr<daf::persistence::Formatter> FormPtr;
469 #define INSTANTIATE(I, M, V) \
470 template class ExposureFormatter<I, M, V>; \
471 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_oarchive>( \
472 boost::archive::text_oarchive&, unsigned int const, dafBase::Persistable*); \
473 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_iarchive>( \
474 boost::archive::text_iarchive&, unsigned int const, dafBase::Persistable*); \
475 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_oarchive>( \
476 boost::archive::binary_oarchive&, unsigned int const, dafBase::Persistable*); \
477 template void ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_iarchive>( \
478 boost::archive::binary_iarchive&, unsigned int const, dafBase::Persistable*);
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Options for writing an image to FITS.
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::int32_t MaskPixel
default type for Masks and MaskedImage Masks
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.
#define INSTANTIATE(FROMSYS, TOSYS)