LSSTApplications  11.0-13-gbb96280,12.1+18,12.1+7,12.1-1-g14f38d3+72,12.1-1-g16c0db7+5,12.1-1-g5961e7a+84,12.1-1-ge22e12b+23,12.1-11-g06625e2+4,12.1-11-g0d7f63b+4,12.1-19-gd507bfc,12.1-2-g7dda0ab+38,12.1-2-gc0bc6ab+81,12.1-21-g6ffe579+2,12.1-21-gbdb6c2a+4,12.1-24-g941c398+5,12.1-3-g57f6835+7,12.1-3-gf0736f3,12.1-37-g3ddd237,12.1-4-gf46015e+5,12.1-5-g06c326c+20,12.1-5-g648ee80+3,12.1-5-gc2189d7+4,12.1-6-ga608fc0+1,12.1-7-g3349e2a+5,12.1-7-gfd75620+9,12.1-9-g577b946+5,12.1-9-gc4df26a+10
LSSTDataManagementBasePackage
ExposureFormatter.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
38 #ifndef __GNUC__
39 # define __attribute__(x) /*NOTHING*/
40 #endif
41 static char const* SVNid __attribute__((unused)) =
42  "$Id$";
43 
44 #include <cstdint>
45 #include <iostream>
46 #include <string>
47 
48 #include "boost/serialization/shared_ptr.hpp"
49 #include <boost/archive/binary_iarchive.hpp>
50 #include <boost/archive/binary_oarchive.hpp>
51 #include <boost/archive/text_iarchive.hpp>
52 #include <boost/archive/text_oarchive.hpp>
53 
54 #include "lsst/daf/base.h"
55 #include "lsst/pex/exceptions.h"
56 #include "lsst/daf/persistence.h"
57 #include "lsst/log/Log.h"
64 #include "lsst/afw/image/Wcs.h"
65 
66 // #include "lsst/afw/image/LSSTFitsResource.h"
67 
68 namespace {
69 LOG_LOGGER _log = LOG_GET("afw.ExposureFormatter");
70 }
71 
72 namespace afwGeom = lsst::afw::geom;
73 namespace afwForm = lsst::afw::formatters;
74 namespace afwImg = lsst::afw::image;
75 namespace dafBase = lsst::daf::base;
76 namespace dafPersist = lsst::daf::persistence;
77 
78 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
80 public:
81  static std::string name();
82 };
83 
84 template<>
86  static std::string name = "ExposureU";
87  return name;
88 }
89 template<>
91  static std::string name = "ExposureI";
92  return name;
93 }
94 template<>
96  static std::string name = "ExposureF";
97  return name;
98 }
99 template<>
101  static std::string name = "ExposureD";
102  return name;
103 }
104 template<>
106  static std::string name = "ExposureL";
107  return name;
108 }
109 
110 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
112  MaskPixelT,
113  VariancePixelT>::registration(
116  createInstance);
117 
118 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
121  lsst::daf::persistence::Formatter(typeid(this)), _policy(policy) {
122 }
123 
124 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
126 }
127 
130 static std::string lookupFilterName(
132  int filterId
133  ) {
134  db->setTableForQuery("Filter");
135  db->outColumn("filterName");
136  db->condParam<int>("id", filterId);
137  db->setQueryWhere("filterId = :id");
138  db->query();
139  if (!db->next() || db->columnIsNull(0)) {
140  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
141  (boost::format("Unable to get name for filter id: %d") % filterId).str());
142  }
143  std::string filterName = db->getColumnByPos<std::string>(0);
144  if (db->next()) {
145  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
146  (boost::format("Multiple names for filter id: %d") % filterId).str());
147 
148  }
149  db->finishQuery();
150  return filterName;
151 }
152 
153 
157 template <typename T>
158 static void setColumn(
160  std::string const& colName,
162  std::string const& propName
163  ) {
164  if (!source->exists(propName)) {
165  db->setColumnToNull(colName);
166  } else {
167  db->setColumn<T>(colName, source->get<T>(propName));
168  }
169 }
170 
175 template <typename T1, typename T2>
176 static void setColumn(
178  std::string const& colName,
180  std::string const& propName
181  ) {
182  if (!source->exists(propName)) {
183  db->setColumnToNull(colName);
184  } else {
185  db->setColumn<T1>(colName, static_cast<T1>(source->get<T2>(propName)));
186  }
187 }
188 
189 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
191  dafBase::Persistable const* persistable,
192  dafPersist::Storage::Ptr storage,
193  lsst::daf::base::PropertySet::Ptr additionalData) {
194  LOGL_DEBUG(_log, "ExposureFormatter write start");
196  dynamic_cast<afwImg::Exposure<ImagePixelT, MaskPixelT, VariancePixelT> const*>(persistable);
197  if (ip == 0) {
198  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Exposure");
199  }
200  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
201  LOGL_DEBUG(_log, "ExposureFormatter write BoostStorage");
202  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
203  boost->getOArchive() & *ip;
204  LOGL_DEBUG(_log, "ExposureFormatter write end");
205  return;
206  }
207  else if (typeid(*storage) == typeid(dafPersist::FitsStorage)) {
208  LOGL_DEBUG(_log, "ExposureFormatter write FitsStorage");
209  dafPersist::FitsStorage* fits = dynamic_cast<dafPersist::FitsStorage*>(storage.get());
210 
211  ip->writeFits(fits->getPath());
212  LOGL_DEBUG(_log, "ExposureFormatter write end");
213  return;
214  } else if (typeid(*storage) == typeid(dafPersist::DbStorage)) {
215  LOGL_DEBUG(_log, "ExposureFormatter write DbStorage");
216  dafPersist::DbStorage* db = dynamic_cast<dafPersist::DbStorage*>(storage.get());
217 
218  // Get the Wcs headers.
220  ip->getWcs()->getFitsMetadata();
221 
222  // Get the image headers.
224  if (!dp) {
225  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
226  "Unable to retrieve metadata from MaskedImage's Image");
227  }
228 
229  // Select a table to insert into based on the itemName.
230  std::string itemName = additionalData->get<std::string>("itemName");
231  std::string tableName = itemName;
232  if (_policy->exists(itemName)) {
233  lsst::pex::policy::Policy::Ptr itemPolicy = _policy->getPolicy(itemName);
234  if (itemPolicy->exists("TableName")) {
235  tableName = itemPolicy->getString("TableName");
236  }
237  }
238  if (tableName != "Raw_Amp_Exposure" &&
239  tableName != "Science_Amp_Exposure") {
240  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
241  "Unknown table name for persisting Exposure to DbStorage: " +
242  tableName + "for item " + itemName);
243  }
244  db->setTableForInsert(tableName);
245 
246  // Set the identifier columns.
247 
248  int ampId = extractAmpId(additionalData);
249  // int ccdId = extractCcdId(additionalData);
250  int64_t fpaExposureId = extractFpaExposureId(dp);
251  int64_t ccdExposureId = extractCcdExposureId(dp);
252  int64_t ampExposureId = extractAmpExposureId(dp);
253 
254  if (tableName == "Raw_Amp_Exposure") {
255  db->setColumn<long long>("rawAmpExposureId", ampExposureId);
256  db->setColumn<long long>("rawCCDExposureId", ccdExposureId);
257  db->setColumn<long long>("rawFPAExposureId", fpaExposureId);
258  }
259  else { // Science_Amp_Exposure
260  db->setColumn<long long>("scienceAmpExposureId", ampExposureId);
261  db->setColumn<long long>("scienceCCDExposureId", ccdExposureId);
262  db->setColumn<long long>("scienceFPAExposureId", fpaExposureId);
263  db->setColumn<long long>("rawAmpExposureId", ampExposureId);
266  }
267 
268  db->setColumn<int>("ampId", ampId);
269 
270  // Set the URL column with the location of the FITS file.
271  setColumn<std::string>(db, "url",
272  additionalData, "StorageLocation.FitsStorage");
273 
274 
275  // Set the Wcs information columns.
276  setColumn<std::string>(db, "ctype1", wcsProps, "CTYPE1");
277  setColumn<std::string>(db, "ctype2", wcsProps, "CTYPE2");
278  setColumn<float, double>(db, "crpix1", wcsProps, "CRPIX1");
279  setColumn<float, double>(db, "crpix2", wcsProps, "CRPIX2");
280  setColumn<double>(db, "crval1", wcsProps, "CRVAL1");
281  setColumn<double>(db, "crval2", wcsProps, "CRVAL2");
282  if (tableName == "Raw_Amp_Exposure") {
283  setColumn<double>(db, "cd11", wcsProps, "CD1_1");
284  setColumn<double>(db, "cd21", wcsProps, "CD2_1");
285  setColumn<double>(db, "cd12", wcsProps, "CD1_2");
286  setColumn<double>(db, "cd22", wcsProps, "CD2_2");
287  }
288  else {
289  setColumn<double>(db, "cd1_1", wcsProps, "CD1_1");
290  setColumn<double>(db, "cd2_1", wcsProps, "CD2_1");
291  setColumn<double>(db, "cd1_2", wcsProps, "CD1_2");
292  setColumn<double>(db, "cd2_2", wcsProps, "CD2_2");
293  }
294 
295 
296  if (tableName == "Science_Amp_Exposure") {
297  // Set calibration data columns.
298  setColumn<float, double>(db, "photoFlam", dp, "PHOTFLAM");
299  setColumn<float, double>(db, "photoZP", dp, "PHOTZP");
300  }
301 
302  // Phew! Insert the row now.
303  db->insertRow();
304 
305  LOGL_DEBUG(_log, "ExposureFormatter write end");
306  return;
307  }
308  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Exposure");
309 }
310 
311 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
313  dafPersist::Storage::Ptr storage,
314  lsst::daf::base::PropertySet::Ptr additionalData) {
315  LOGL_DEBUG(_log, "ExposureFormatter read start");
316  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
317  LOGL_DEBUG(_log, "ExposureFormatter read BoostStorage");
318  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
321  boost->getIArchive() & *ip;
322  LOGL_DEBUG(_log, "ExposureFormatter read end");
323  return ip;
324  } else if (typeid(*storage) == typeid(dafPersist::FitsStorage)) {
325  LOGL_DEBUG(_log, "ExposureFormatter read FitsStorage");
326  dafPersist::FitsStorage* fits = dynamic_cast<dafPersist::FitsStorage*>(storage.get());
327  afwGeom::Box2I box;
328  if (additionalData->exists("llcX")) {
329  int llcX = additionalData->get<int>("llcX");
330  int llcY = additionalData->get<int>("llcY");
331  int width = additionalData->get<int>("width");
332  int height = additionalData->get<int>("height");
333  box = afwGeom::Box2I(afwGeom::Point2I(llcX, llcY), afwGeom::Extent2I(width, height));
334  }
336  if(additionalData->exists("imageOrigin")){
337  std::string originStr = additionalData->get<std::string>("imageOrigin");
338  if(originStr == "LOCAL") {
339  origin = afwImg::LOCAL;
340  } else if (originStr == "PARENT") {
341  origin = afwImg::PARENT;
342  } else {
343  throw LSST_EXCEPT(
344  lsst::pex::exceptions::RuntimeError,
345  (boost::format("Unknown ImageOrigin type %s specified in additional"
346  "data for retrieving Exposure from fits")%originStr
347 
348  ).str()
349  );
350  }
351  }
354  fits->getPath(), box, origin);
355  LOGL_DEBUG(_log, "ExposureFormatter read end");
356  return ip;
357  } else if (typeid(*storage) == typeid(dafPersist::DbStorage)) {
358  LOGL_DEBUG(_log, "ExposureFormatter read DbStorage");
359  dafPersist::DbStorage* db = dynamic_cast<dafPersist::DbStorage*>(storage.get());
360 
361  // Select a table to retrieve from based on the itemName.
362  std::string itemName = additionalData->get<std::string>("itemName");
363  std::string tableName = itemName;
364  if (_policy->exists(itemName)) {
365  lsst::pex::policy::Policy::Ptr itemPolicy =
366  _policy->getPolicy(itemName);
367  if (itemPolicy->exists("TableName")) {
368  tableName = itemPolicy->getString("TableName");
369  }
370  }
371  if (tableName != "Raw_Amp_Exposure" &&
372  tableName != "Science_Amp_Exposure") {
373  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
374  "Unknown table name for retrieving Exposure from DbStorage: " +
375  tableName + " for item " + itemName);
376  }
377  db->setTableForQuery(tableName);
378 
379  // Set the identifier column tests.
380  db->condParam<int64_t>("id", additionalData->getAsInt64("ampExposureId"));
381  if (tableName == "Raw_Amp_Exposure") {
382  db->setQueryWhere("rawAmpExposureId = :id");
383  }
384  else { // Science_Amp_Exposure
385  db->setQueryWhere("scienceAmpExposureId = :id");
386  }
387 
388  db->outColumn("url");
389 
390  if (tableName == "Science_Amp_Exposure") {
391  // Set the Wcs information columns.
392  db->outColumn("ctype1");
393  db->outColumn("ctype2");
394  db->outColumn("crpix1");
395  db->outColumn("crpix2");
396  db->outColumn("crval1");
397  db->outColumn("crval2");
398  db->outColumn("cd11");
399  db->outColumn("cd21");
400  db->outColumn("cd12");
401  db->outColumn("cd22");
402 
403  // Set calibration data columns.
404  db->outColumn("photoFlam");
405  db->outColumn("photoZP");
406  }
407 
408  // Phew! Run the query.
409  db->query();
410  if (!db->next()) {
411  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unable to retrieve row");
412  }
413  // ...
414  if (db->next()) {
415  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Non-unique Exposure retrieved");
416  }
417  db->finishQuery();
418 
420  // - KTL - 2007-11-29
421 
422  // Restore image from FITS...
426 
427  // Look up the filter name given the ID.
428  int filterId = db->getColumnByPos<int>(1);
429  std::string filterName = lookupFilterName(db, filterId);
430  dp->set("FILTER", filterName);
431 
432  // Set the image headers.
433  // Set the Wcs headers in ip->_wcs.
434 
436  // with values from database. - KTL - 2007-12-18
437 
438  LOGL_DEBUG(_log, "ExposureFormatter read end");
439  return ip;
440  }
441  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Exposure");
442 }
443 
444 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
449  )
450 {
452  // - KTL - 2007-11-29
453  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Exposure");
454 }
455 
456 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT> template <class Archive>
458  Archive& ar, unsigned int const, dafBase::Persistable* persistable
459  )
460 {
461  LOGL_DEBUG(_log, "ExposureFormatter delegateSerialize start");
464  if (ip == 0) {
465  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Exposure");
466  }
467  PTR(afwImg::Wcs) wcs = ip->getWcs();
468  ar & *ip->getMetadata() & ip->_maskedImage & wcs;
469  LOGL_DEBUG(_log, "ExposureFormatter delegateSerialize end");
470 }
471 
472 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
474  MaskPixelT,
475  VariancePixelT>::createInstance(
477  typedef typename lsst::daf::persistence::Formatter::Ptr FormPtr;
479 }
480 
482 #define INSTANTIATE(I, M, V) \
483  template class afwForm::ExposureFormatter<I, M, V>; \
484  template void afwForm::ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_oarchive>( \
485  boost::archive::text_oarchive &, unsigned int const, dafBase::Persistable *); \
486  template void afwForm::ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_iarchive>( \
487  boost::archive::text_iarchive &, unsigned int const, dafBase::Persistable *); \
488  template void afwForm::ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_oarchive>( \
489  boost::archive::binary_oarchive &, unsigned int const, dafBase::Persistable *); \
490  template void afwForm::ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_iarchive>( \
491  boost::archive::binary_iarchive &, unsigned int const, dafBase::Persistable *);
492 
494 INSTANTIATE(int, afwImg::MaskPixel, afwImg::VariancePixel)
495 INSTANTIATE(float, afwImg::MaskPixel, afwImg::VariancePixel)
496 INSTANTIATE(double, afwImg::MaskPixel, afwImg::VariancePixel)
497 INSTANTIATE(uint64_t, afwImg::MaskPixel, afwImg::VariancePixel)
std::uint16_t MaskPixel
Interface for WcsFormatter class.
std::shared_ptr< Policy > Ptr
Definition: Policy.h:172
bool columnIsNull(int pos)
Determine if the value of a column is NULL.
Definition: DbStorage.cc:270
void condParam(std::string const &paramName, T const &value)
Bind a value to a WHERE condition parameter.
Definition: DbStorage.cc:218
Interface for TanWcsFormatter class.
#define LOG_LOGGER
Definition: Log.h:712
table::Key< std::string > name
Definition: ApCorrMap.cc:71
Interface for ExposureFormatter class.
daf_persistence package header file
Include files required for standard LSST Exception handling.
virtual void setTableForQuery(std::string const &tableName, bool isExpr=false)
Set the table to query (single-table queries only).
Definition: DbStorage.cc:175
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:46
std::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
tbl::Key< int > wcs
void writeFits(std::string const &fileName) const
Write an Exposure to a regular multi-extension FITS file.
Definition: Exposure.cc:249
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:107
virtual void setQueryWhere(std::string const &whereClause)
Set the condition for the WHERE clause of the query.
Definition: DbStorage.cc:242
Construct a static instance of this helper class to register a Formatter subclass in the FormatterReg...
Definition: Formatter.h:138
Class for FITS file storage.
Definition: FitsStorage.h:52
virtual boost::archive::text_oarchive & getOArchive(void)
Get a boost::serialization archive suitable for output.
virtual lsst::daf::base::Persistable * read(lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Read a Persistable instance from a Storage instance.
virtual bool next(void)
Move to the next (first) row of the query result.
Definition: DbStorage.cc:255
#define INSTANTIATE(T)
#define LOGL_DEBUG(logger, message...)
Log a debug-level message using a varargs/printf style interface.
Definition: Log.h:513
An integer coordinate rectangle.
Definition: Box.h:53
Class for database storage.
Definition: DbStorage.h:63
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
int64_t extractCcdExposureId(boost::shared_ptr< PropertySet const > const &properties)
Definition: Utils.cc:123
LSST DM logging module built on log4cxx.
#define __attribute__(x)
Class implementing persistence and retrieval for Exposures.
void ImageT ImageT int float saturatedPixelValue int const width
Definition: saturated.cc:44
lsst::daf::base::PropertySet::Ptr getMetadata() const
Return flexible metadata.
Definition: Exposure.h:165
virtual void setTableForInsert(std::string const &tableName)
Set the table to insert rows into.
Definition: DbStorage.cc:143
std::shared_ptr< Storage > Ptr
Definition: Storage.h:62
virtual void outColumn(std::string const &columnName, bool isExpr=false)
Request a column in the query output.
Definition: DbStorage.cc:194
Interface for PropertySetFormatter class.
Formatting utilities.
static std::string name()
MaskedImageT _maskedImage
Definition: Exposure.h:306
int64_t extractFpaExposureId(boost::shared_ptr< PropertySet const > const &properties)
Definition: Utils.cc:81
void ImageT ImageT int float saturatedPixelValue int const height
Definition: saturated.cc:44
void setColumn(std::string const &columnName, T const &value)
Set the value to insert in a given column.
Definition: DbStorage.cc:152
static void delegateSerialize(Archive &ar, unsigned int const version, lsst::daf::base::Persistable *persistable)
virtual boost::archive::text_iarchive & getIArchive(void)
Get a boost::serialization archive suitable for input.
boost::shared_ptr< Wcs const > getWcs() const
Definition: Exposure.h:157
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
Definition: Exception.h:46
ExposureFormatter(lsst::pex::policy::Policy::Ptr policy)
virtual void write(lsst::daf::base::Persistable const *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Write a Persistable instance to a Storage instance.
int64_t extractAmpExposureId(boost::shared_ptr< PropertySet const > const &properties)
Definition: Utils.cc:134
virtual std::string const & getPath(void)
Return the pathname for the FITS file.
Definition: FitsStorage.cc:109
#define PTR(...)
Definition: base.h:41
virtual void query(void)
Execute the query.
Definition: DbStorage.cc:248
T const & getColumnByPos(int pos)
Get the value of a column of the query result row by position.
Definition: DbStorage.cc:263
virtual void update(lsst::daf::base::Persistable *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Update an existing Persistable instance with information from an additional Storage instance...
virtual void finishQuery(void)
Indicate that query processing is finished.
Definition: DbStorage.cc:278
virtual void setColumnToNull(std::string const &columnName)
Set a given column to NULL.
Definition: DbStorage.cc:159
Class for boost::serialization storage.
Definition: BoostStorage.h:59
Base class for all persistable classes.
Definition: Persistable.h:74
std::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:85
#define LOG_GET(logger)
Returns a Log object associated with logger.
Definition: Log.h:83
float VariancePixel
! default type for Masks and MaskedImage Masks
int extractAmpId(boost::shared_ptr< PropertySet const > const &properties)
Definition: Utils.cc:109