LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
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 "boost/serialization/shared_ptr.hpp"
45 #include <boost/archive/binary_iarchive.hpp>
46 #include <boost/archive/binary_oarchive.hpp>
47 #include <boost/archive/text_iarchive.hpp>
48 #include <boost/archive/text_oarchive.hpp>
49 
50 #include "lsst/daf/base.h"
51 #include "lsst/pex/exceptions.h"
52 #include "lsst/daf/persistence.h"
53 #include "lsst/pex/logging/Trace.h"
60 #include "lsst/afw/image/Wcs.h"
61 
62 #include <iostream>
63 
64 // #include "lsst/afw/image/LSSTFitsResource.h"
65 
66 #define EXEC_TRACE 20
67 static void execTrace(std::string s, int level = EXEC_TRACE) {
68  lsst::pex::logging::Trace("afw.ExposureFormatter", level, s);
69 }
70 
71 namespace afwGeom = lsst::afw::geom;
72 namespace afwForm = lsst::afw::formatters;
73 namespace afwImg = lsst::afw::image;
74 namespace dafBase = lsst::daf::base;
75 namespace dafPersist = lsst::daf::persistence;
76 
77 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
79 public:
80  static std::string name();
81 };
82 
83 template<>
85  static std::string name = "ExposureU";
86  return name;
87 }
88 template<>
90  static std::string name = "ExposureI";
91  return name;
92 }
93 template<>
95  static std::string name = "ExposureF";
96  return name;
97 }
98 template<>
100  static std::string name = "ExposureD";
101  return name;
102 }
103 template<>
105  static std::string name = "ExposureL";
106  return name;
107 }
108 
109 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
111  MaskPixelT,
112  VariancePixelT>::registration(
115  createInstance);
116 
117 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
120  lsst::daf::persistence::Formatter(typeid(this)), _policy(policy) {
121 }
122 
123 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
125 }
126 
129 static std::string lookupFilterName(
131  int filterId
132  ) {
133  db->setTableForQuery("Filter");
134  db->outColumn("filterName");
135  db->condParam<int>("id", filterId);
136  db->setQueryWhere("filterId = :id");
137  db->query();
138  if (!db->next() || db->columnIsNull(0)) {
139  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
140  (boost::format("Unable to get name for filter id: %d") % filterId).str());
141  }
142  std::string filterName = db->getColumnByPos<std::string>(0);
143  if (db->next()) {
144  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
145  (boost::format("Multiple names for filter id: %d") % filterId).str());
146 
147  }
148  db->finishQuery();
149  return filterName;
150 }
151 
152 
156 template <typename T>
157 static void setColumn(
159  std::string const& colName,
161  std::string const& propName
162  ) {
163  if (!source->exists(propName)) {
164  db->setColumnToNull(colName);
165  } else {
166  db->setColumn<T>(colName, source->get<T>(propName));
167  }
168 }
169 
174 template <typename T1, typename T2>
175 static void setColumn(
177  std::string const& colName,
179  std::string const& propName
180  ) {
181  if (!source->exists(propName)) {
182  db->setColumnToNull(colName);
183  } else {
184  db->setColumn<T1>(colName, static_cast<T1>(source->get<T2>(propName)));
185  }
186 }
187 
188 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
190  dafBase::Persistable const* persistable,
191  dafPersist::Storage::Ptr storage,
192  lsst::daf::base::PropertySet::Ptr additionalData) {
193  execTrace("ExposureFormatter write start");
195  dynamic_cast<afwImg::Exposure<ImagePixelT, MaskPixelT, VariancePixelT> const*>(persistable);
196  if (ip == 0) {
197  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Exposure");
198  }
199  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
200  execTrace("ExposureFormatter write BoostStorage");
201  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
202  boost->getOArchive() & *ip;
203  execTrace("ExposureFormatter write end");
204  return;
205  }
206  else if (typeid(*storage) == typeid(dafPersist::FitsStorage)) {
207  execTrace("ExposureFormatter write FitsStorage");
208  dafPersist::FitsStorage* fits = dynamic_cast<dafPersist::FitsStorage*>(storage.get());
209 
210  ip->writeFits(fits->getPath());
211  execTrace("ExposureFormatter write end");
212  return;
213  } else if (typeid(*storage) == typeid(dafPersist::DbStorage)) {
214  execTrace("ExposureFormatter write DbStorage");
215  dafPersist::DbStorage* db = dynamic_cast<dafPersist::DbStorage*>(storage.get());
216 
217  // Get the Wcs headers.
219  ip->getWcs()->getFitsMetadata();
220 
221  // Get the image headers.
223  if (!dp) {
224  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
225  "Unable to retrieve metadata from MaskedImage's Image");
226  }
227 
228  // Select a table to insert into based on the itemName.
229  std::string itemName = additionalData->get<std::string>("itemName");
230  std::string tableName = itemName;
231  if (_policy->exists(itemName)) {
232  lsst::pex::policy::Policy::Ptr itemPolicy = _policy->getPolicy(itemName);
233  if (itemPolicy->exists("TableName")) {
234  tableName = itemPolicy->getString("TableName");
235  }
236  }
237  if (tableName != "Raw_Amp_Exposure" &&
238  tableName != "Science_Amp_Exposure") {
239  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
240  "Unknown table name for persisting Exposure to DbStorage: " +
241  tableName + "for item " + itemName);
242  }
243  db->setTableForInsert(tableName);
244 
245  // Set the identifier columns.
246 
247  int ampId = extractAmpId(additionalData);
248  // int ccdId = extractCcdId(additionalData);
249  int64_t fpaExposureId = extractFpaExposureId(dp);
250  int64_t ccdExposureId = extractCcdExposureId(dp);
251  int64_t ampExposureId = extractAmpExposureId(dp);
252 
253  if (tableName == "Raw_Amp_Exposure") {
254  db->setColumn<long long>("rawAmpExposureId", ampExposureId);
255  db->setColumn<long long>("rawCCDExposureId", ccdExposureId);
256  db->setColumn<long long>("rawFPAExposureId", fpaExposureId);
257  }
258  else { // Science_Amp_Exposure
259  db->setColumn<long long>("scienceAmpExposureId", ampExposureId);
260  db->setColumn<long long>("scienceCCDExposureId", ccdExposureId);
261  db->setColumn<long long>("scienceFPAExposureId", fpaExposureId);
262  db->setColumn<long long>("rawAmpExposureId", ampExposureId);
265  }
266 
267  db->setColumn<int>("ampId", ampId);
268 
269  // Set the URL column with the location of the FITS file.
270  setColumn<std::string>(db, "url",
271  additionalData, "StorageLocation.FitsStorage");
272 
273 
274  // Set the Wcs information columns.
275  setColumn<std::string>(db, "ctype1", wcsProps, "CTYPE1");
276  setColumn<std::string>(db, "ctype2", wcsProps, "CTYPE2");
277  setColumn<float, double>(db, "crpix1", wcsProps, "CRPIX1");
278  setColumn<float, double>(db, "crpix2", wcsProps, "CRPIX2");
279  setColumn<double>(db, "crval1", wcsProps, "CRVAL1");
280  setColumn<double>(db, "crval2", wcsProps, "CRVAL2");
281  if (tableName == "Raw_Amp_Exposure") {
282  setColumn<double>(db, "cd11", wcsProps, "CD1_1");
283  setColumn<double>(db, "cd21", wcsProps, "CD2_1");
284  setColumn<double>(db, "cd12", wcsProps, "CD1_2");
285  setColumn<double>(db, "cd22", wcsProps, "CD2_2");
286  }
287  else {
288  setColumn<double>(db, "cd1_1", wcsProps, "CD1_1");
289  setColumn<double>(db, "cd2_1", wcsProps, "CD2_1");
290  setColumn<double>(db, "cd1_2", wcsProps, "CD1_2");
291  setColumn<double>(db, "cd2_2", wcsProps, "CD2_2");
292  }
293 
294 
295  if (tableName == "Science_Amp_Exposure") {
296  // Set calibration data columns.
297  setColumn<float, double>(db, "photoFlam", dp, "PHOTFLAM");
298  setColumn<float, double>(db, "photoZP", dp, "PHOTZP");
299  }
300 
301  // Phew! Insert the row now.
302  db->insertRow();
303 
304  execTrace("ExposureFormatter write end");
305  return;
306  }
307  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Exposure");
308 }
309 
310 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
312  dafPersist::Storage::Ptr storage,
313  lsst::daf::base::PropertySet::Ptr additionalData) {
314  execTrace("ExposureFormatter read start");
315  if (typeid(*storage) == typeid(dafPersist::BoostStorage)) {
316  execTrace("ExposureFormatter read BoostStorage");
317  dafPersist::BoostStorage* boost = dynamic_cast<dafPersist::BoostStorage*>(storage.get());
320  boost->getIArchive() & *ip;
321  execTrace("ExposureFormatter read end");
322  return ip;
323  } else if (typeid(*storage) == typeid(dafPersist::FitsStorage)) {
324  execTrace("ExposureFormatter read FitsStorage");
325  dafPersist::FitsStorage* fits = dynamic_cast<dafPersist::FitsStorage*>(storage.get());
326  afwGeom::Box2I box;
327  if (additionalData->exists("llcX")) {
328  int llcX = additionalData->get<int>("llcX");
329  int llcY = additionalData->get<int>("llcY");
330  int width = additionalData->get<int>("width");
331  int height = additionalData->get<int>("height");
332  box = afwGeom::Box2I(afwGeom::Point2I(llcX, llcY), afwGeom::Extent2I(width, height));
333  }
335  if(additionalData->exists("imageOrigin")){
336  std::string originStr = additionalData->get<std::string>("imageOrigin");
337  if(originStr == "LOCAL") {
338  origin = afwImg::LOCAL;
339  } else if (originStr == "PARENT") {
340  origin = afwImg::PARENT;
341  } else {
342  throw LSST_EXCEPT(
343  lsst::pex::exceptions::RuntimeError,
344  (boost::format("Unknown ImageOrigin type %s specified in additional"
345  "data for retrieving Exposure from fits")%originStr
346 
347  ).str()
348  );
349  }
350  }
353  fits->getPath(), box, origin);
354  execTrace("ExposureFormatter read end");
355  return ip;
356  } else if (typeid(*storage) == typeid(dafPersist::DbStorage)) {
357  execTrace("ExposureFormatter read DbStorage");
358  dafPersist::DbStorage* db = dynamic_cast<dafPersist::DbStorage*>(storage.get());
359 
360  // Select a table to retrieve from based on the itemName.
361  std::string itemName = additionalData->get<std::string>("itemName");
362  std::string tableName = itemName;
363  if (_policy->exists(itemName)) {
364  lsst::pex::policy::Policy::Ptr itemPolicy =
365  _policy->getPolicy(itemName);
366  if (itemPolicy->exists("TableName")) {
367  tableName = itemPolicy->getString("TableName");
368  }
369  }
370  if (tableName != "Raw_Amp_Exposure" &&
371  tableName != "Science_Amp_Exposure") {
372  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
373  "Unknown table name for retrieving Exposure from DbStorage: " +
374  tableName + " for item " + itemName);
375  }
376  db->setTableForQuery(tableName);
377 
378  // Set the identifier column tests.
379  db->condParam<int64_t>("id", additionalData->getAsInt64("ampExposureId"));
380  if (tableName == "Raw_Amp_Exposure") {
381  db->setQueryWhere("rawAmpExposureId = :id");
382  }
383  else { // Science_Amp_Exposure
384  db->setQueryWhere("scienceAmpExposureId = :id");
385  }
386 
387  db->outColumn("url");
388 
389  if (tableName == "Science_Amp_Exposure") {
390  // Set the Wcs information columns.
391  db->outColumn("ctype1");
392  db->outColumn("ctype2");
393  db->outColumn("crpix1");
394  db->outColumn("crpix2");
395  db->outColumn("crval1");
396  db->outColumn("crval2");
397  db->outColumn("cd11");
398  db->outColumn("cd21");
399  db->outColumn("cd12");
400  db->outColumn("cd22");
401 
402  // Set calibration data columns.
403  db->outColumn("photoFlam");
404  db->outColumn("photoZP");
405  }
406 
407  // Phew! Run the query.
408  db->query();
409  if (!db->next()) {
410  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unable to retrieve row");
411  }
412  // ...
413  if (db->next()) {
414  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Non-unique Exposure retrieved");
415  }
416  db->finishQuery();
417 
419  // - KTL - 2007-11-29
420 
421  // Restore image from FITS...
425 
426  // Look up the filter name given the ID.
427  int filterId = db->getColumnByPos<int>(1);
428  std::string filterName = lookupFilterName(db, filterId);
429  dp->set("FILTER", filterName);
430 
431  // Set the image headers.
432  // Set the Wcs headers in ip->_wcs.
433 
435  // with values from database. - KTL - 2007-12-18
436 
437  execTrace("ExposureFormatter read end");
438  return ip;
439  }
440  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Exposure");
441 }
442 
443 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
448  )
449 {
451  // - KTL - 2007-11-29
452  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Exposure");
453 }
454 
455 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT> template <class Archive>
457  Archive& ar, unsigned int const, dafBase::Persistable* persistable
458  )
459 {
460  execTrace("ExposureFormatter delegateSerialize start");
463  if (ip == 0) {
464  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Exposure");
465  }
466  PTR(afwImg::Wcs) wcs = ip->getWcs();
467  ar & *ip->getMetadata() & ip->_maskedImage & wcs;
468  execTrace("ExposureFormatter delegateSerialize end");
469 }
470 
471 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
473  MaskPixelT,
474  VariancePixelT>::createInstance(
476  typedef typename lsst::daf::persistence::Formatter::Ptr FormPtr;
478 }
479 
481 #define INSTANTIATE(I, M, V) \
482  template class afwForm::ExposureFormatter<I, M, V>; \
483  template void afwForm::ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_oarchive>( \
484  boost::archive::text_oarchive &, unsigned int const, dafBase::Persistable *); \
485  template void afwForm::ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::text_iarchive>( \
486  boost::archive::text_iarchive &, unsigned int const, dafBase::Persistable *); \
487  template void afwForm::ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_oarchive>( \
488  boost::archive::binary_oarchive &, unsigned int const, dafBase::Persistable *); \
489  template void afwForm::ExposureFormatter<I, M, V>::delegateSerialize<boost::archive::binary_iarchive>( \
490  boost::archive::binary_iarchive &, unsigned int const, dafBase::Persistable *);
491 
493 INSTANTIATE(int, afwImg::MaskPixel, afwImg::VariancePixel)
494 INSTANTIATE(float, afwImg::MaskPixel, afwImg::VariancePixel)
495 INSTANTIATE(double, afwImg::MaskPixel, afwImg::VariancePixel)
496 INSTANTIATE(uint64_t, afwImg::MaskPixel, afwImg::VariancePixel)
Interface for WcsFormatter class.
boost::uint16_t MaskPixel
int64_t extractAmpExposureId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int extractAmpId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
Interface for TanWcsFormatter class.
#define EXEC_TRACE
virtual void outColumn(std::string const &columnName, bool isExpr=false)
Definition: DbStorage.cc:194
#define PTR(...)
Definition: base.h:41
table::Key< std::string > name
Definition: ApCorrMap.cc:71
boost::shared_ptr< Formatter > Ptr
Definition: Formatter.h:81
void setColumn(std::string const &columnName, T const &value)
Definition: DbStorage.cc:152
Interface for ExposureFormatter class.
daf_persistence package header file
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:48
virtual boost::archive::text_iarchive & getIArchive(void)
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
virtual std::string const & getPath(void)
Definition: FitsStorage.cc:109
virtual void setQueryWhere(std::string const &whereClause)
Definition: DbStorage.cc:242
MaskedImageT _maskedImage
Definition: Exposure.h:300
definition of the Trace messaging facilities
T const & getColumnByPos(int pos)
Definition: DbStorage.cc:263
tbl::Key< int > wcs
boost::shared_ptr< Policy > Ptr
Definition: Policy.h:172
Implementation of the WCS standard for a any projection.
Definition: Wcs.h:107
limited backward compatibility to the DC2 run-time trace facilities
Definition: Trace.h:93
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
static void delegateSerialize(Archive &ar, unsigned int const version, lsst::daf::base::Persistable *persistable)
void condParam(std::string const &paramName, T const &value)
Definition: DbStorage.cc:218
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 extractFpaExposureId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
#define __attribute__(x)
int64_t extractCcdExposureId(boost::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
void writeFits(std::string const &fileName) const
Write an Exposure to a regular multi-extension FITS file.
Definition: Exposure.cc:237
Class implementing persistence and retrieval for Exposures.
Interface for PropertySetFormatter class.
ExposureFormatter(lsst::pex::policy::Policy::Ptr policy)
static std::string name()
float VariancePixel
! default type for Masks and MaskedImage Masks
lsst::daf::base::PropertySet::Ptr getMetadata() const
Return flexible metadata.
Definition: Exposure.h:162
virtual void setTableForQuery(std::string const &tableName, bool isExpr=false)
Definition: DbStorage.cc:175
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual boost::archive::text_oarchive & getOArchive(void)
#define INSTANTIATE(T)
boost::shared_ptr< Storage > Ptr
Definition: Storage.h:62
virtual lsst::daf::base::Persistable * read(lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Class for boost::serialization storage.
Definition: BoostStorage.h:58
virtual void update(lsst::daf::base::Persistable *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
Base class for all persistable classes.
Definition: Persistable.h:74
virtual void write(lsst::daf::base::Persistable const *persistable, lsst::daf::persistence::Storage::Ptr storage, lsst::daf::base::PropertySet::Ptr additionalData)
virtual void finishQuery(void)
Definition: DbStorage.cc:278
boost::shared_ptr< Wcs const > getWcs() const
Definition: Exposure.h:154
virtual void setTableForInsert(std::string const &tableName)
Definition: DbStorage.cc:143
virtual void setColumnToNull(std::string const &columnName)
Definition: DbStorage.cc:159
Include files required for standard LSST Exception handling.
Formatting utilities.