lsst.afw  13.0-49-g6f23dda
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 /*
26  * Implementation of ExposureFormatter class
27  */
28 
29 #ifndef __GNUC__
30 #define __attribute__(x) /*NOTHING*/
31 #endif
32 static char const* SVNid __attribute__((unused)) = "$Id$";
33 
34 #include <cstdint>
35 #include <iostream>
36 #include <string>
37 
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>
43 
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"
54 #include "lsst/afw/image/Wcs.h"
55 
56 // #include "lsst/afw/image/LSSTFitsResource.h"
57 
58 namespace {
59 LOG_LOGGER _log = LOG_GET("afw.ExposureFormatter");
60 }
61 
62 namespace lsst {
63 namespace afw {
64 namespace formatters {
65 
66 namespace dafBase = lsst::daf::base;
67 namespace dafPersist = lsst::daf::persistence;
68 
69 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
71 public:
72  static std::string name();
73 };
74 
75 template <>
77  static std::string name = "ExposureU";
78  return name;
79 }
80 template <>
82  static std::string name = "ExposureI";
83  return name;
84 }
85 template <>
87  static std::string name = "ExposureF";
88  return name;
89 }
90 template <>
92  static std::string name = "ExposureD";
93  return name;
94 }
95 template <>
97  static std::string name = "ExposureL";
98  return name;
99 }
100 
101 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
102 daf::persistence::FormatterRegistration
106 
107 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
109  std::shared_ptr<pex::policy::Policy> policy)
110  : daf::persistence::Formatter(typeid(this)), _policy(policy) {}
111 
112 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
114 
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");
126  db->query();
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());
130  }
131  std::string filterName = db->getColumnByPos<std::string>(0);
132  if (db->next()) {
133  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
134  (boost::format("Multiple names for filter id: %d") % filterId).str());
135  }
136  db->finishQuery();
137  return filterName;
138 }
139 
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);
154  } else {
155  db->setColumn<T>(colName, source->get<T>(propName));
156  }
157 }
158 
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
168  ) {
169  if (!source->exists(propName)) {
170  db->setColumnToNull(colName);
171  } else {
172  db->setColumn<T1>(colName, static_cast<T1>(source->get<T2>(propName)));
173  }
174 }
175 
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");
182  dynamic_cast<image::Exposure<ImagePixelT, MaskPixelT, VariancePixelT> const*>(persistable);
183  if (ip == 0) {
184  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Persisting non-Exposure");
185  }
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");
191  return;
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());
195 
196  ip->writeFits(fits->getPath());
197  LOGL_DEBUG(_log, "ExposureFormatter write end");
198  return;
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());
202 
203  // Get the Wcs headers.
204  std::shared_ptr<daf::base::PropertySet> wcsProps = ip->getWcs()->getFitsMetadata();
205 
206  // Get the image headers.
207  std::shared_ptr<daf::base::PropertySet> dp = ip->getMetadata();
208  if (!dp) {
209  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
210  "Unable to retrieve metadata from MaskedImage's Image");
211  }
212 
213  // Select a table to insert into based on the itemName.
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");
220  }
221  }
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);
226  }
227  db->setTableForInsert(tableName);
228 
229  // Set the identifier columns.
230 
231  int ampId = extractAmpId(additionalData);
232  // int ccdId = extractCcdId(additionalData);
233  int64_t fpaExposureId = extractFpaExposureId(dp);
234  int64_t ccdExposureId = extractCcdExposureId(dp);
235  int64_t ampExposureId = extractAmpExposureId(dp);
236 
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);
241  } else { // Science_Amp_Exposure
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);
246  // @todo Check that rawCCDExposureId == scienceCCDExposureId --
247  // KTL -- 2008-01-25
248  }
249 
250  db->setColumn<int>("ampId", ampId);
251 
252  // Set the URL column with the location of the FITS file.
253  setColumn<std::string>(db, "url", additionalData, "StorageLocation.FitsStorage");
254 
255  // Set the Wcs information columns.
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");
267  } else {
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");
272  }
273 
274  if (tableName == "Science_Amp_Exposure") {
275  // Set calibration data columns.
276  setColumn<float, double>(db, "photoFlam", dp, "PHOTFLAM");
277  setColumn<float, double>(db, "photoZP", dp, "PHOTZP");
278  }
279 
280  // Phew! Insert the row now.
281  db->insertRow();
282 
283  LOGL_DEBUG(_log, "ExposureFormatter write end");
284  return;
285  }
286  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Unrecognized Storage for Exposure");
287 }
288 
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");
301  return ip;
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());
305  geom::Box2I box;
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");
311  box = geom::Box2I(geom::Point2I(llcX, llcY), geom::Extent2I(width, height));
312  }
314  if (additionalData->exists("imageOrigin")) {
315  std::string originStr = additionalData->get<std::string>("imageOrigin");
316  if (originStr == "LOCAL") {
317  origin = image::LOCAL;
318  } else if (originStr == "PARENT") {
319  origin = image::PARENT;
320  } else {
321  throw LSST_EXCEPT(pex::exceptions::RuntimeError,
322  (boost::format("Unknown ImageOrigin type %s specified in additional"
323  "data for retrieving Exposure from fits") %
324  originStr
325 
326  ).str());
327  }
328  }
330  new image::Exposure<ImagePixelT, MaskPixelT, VariancePixelT>(fits->getPath(), box, origin);
331  LOGL_DEBUG(_log, "ExposureFormatter read end");
332  return ip;
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());
336 
337  // Select a table to retrieve from based on the itemName.
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");
344  }
345  }
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);
350  }
351  db->setTableForQuery(tableName);
352 
353  // Set the identifier column tests.
354  db->condParam<int64_t>("id", additionalData->getAsInt64("ampExposureId"));
355  if (tableName == "Raw_Amp_Exposure") {
356  db->setQueryWhere("rawAmpExposureId = :id");
357  } else { // Science_Amp_Exposure
358  db->setQueryWhere("scienceAmpExposureId = :id");
359  }
360 
361  db->outColumn("url");
362 
363  if (tableName == "Science_Amp_Exposure") {
364  // Set the Wcs information columns.
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");
375 
376  // Set calibration data columns.
377  db->outColumn("photoFlam");
378  db->outColumn("photoZP");
379  }
380 
381  // Phew! Run the query.
382  db->query();
383  if (!db->next()) {
384  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Unable to retrieve row");
385  }
386  // ...
387  if (db->next()) {
388  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Non-unique Exposure retrieved");
389  }
390  db->finishQuery();
391 
392  // @todo Should really have FITS be a separate Storage.
393  // - KTL - 2007-11-29
394 
395  // Restore image from FITS...
398  db->getColumnByPos<std::string>(0));
399  std::shared_ptr<daf::base::PropertySet> dp = ip->getMetadata();
400 
401  // Look up the filter name given the ID.
402  int filterId = db->getColumnByPos<int>(1);
403  std::string filterName = lookupFilterName(db, filterId);
404  dp->set("FILTER", filterName);
405 
406  // Set the image headers.
407  // Set the Wcs headers in ip->_wcs.
408 
409  // @todo Need to implement overwriting of FITS metadata PropertySet
410  // with values from database. - KTL - 2007-12-18
411 
412  LOGL_DEBUG(_log, "ExposureFormatter read end");
413  return ip;
414  }
415  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Unrecognized Storage for Exposure");
416 }
417 
418 template <typename ImagePixelT, typename MaskPixelT, typename VariancePixelT>
420  dafBase::Persistable*, std::shared_ptr<dafPersist::Storage>,
421  std::shared_ptr<daf::base::PropertySet>) {
423  // - KTL - 2007-11-29
424  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Unexpected call to update for Exposure");
425 }
426 
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");
434  if (ip == 0) {
435  throw LSST_EXCEPT(pex::exceptions::RuntimeError, "Serializing non-Exposure");
436  }
437  std::shared_ptr<image::Wcs> wcs = ip->getWcs();
438  ar& * ip->getMetadata() & ip->_maskedImage& wcs;
439  LOGL_DEBUG(_log, "ExposureFormatter delegateSerialize end");
440 }
441 
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;
448 }
449 
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*);
461 
464 INSTANTIATE(float, image::MaskPixel, image::VariancePixel)
465 INSTANTIATE(double, image::MaskPixel, image::VariancePixel)
466 INSTANTIATE(uint64_t, image::MaskPixel, image::VariancePixel)
468 }
469 }
470 } // end afw::formatters
std::uint16_t MaskPixel
default type for Masks and MaskedImage Masks
int64_t extractFpaExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int64_t extractAmpExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int64_t extractCcdExposureId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
int extractAmpId(std::shared_ptr< lsst::daf::base::PropertySet const > const &properties)
static void delegateSerialize(Archive &ar, unsigned int const version, lsst::daf::base::Persistable *persistable)
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
A class to contain the data, WCS, and other information needed to describe an image of the sky...
Definition: Exposure.h:76
#define INSTANTIATE(TYPE)
void writeFits(std::string const &fileName) const
Write an Exposure to a regular multi-extension FITS file.
Definition: Exposure.cc:138
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
An integer coordinate rectangle.
Definition: Box.h:55
Class implementing persistence and retrieval for Exposures.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
#define __attribute__(x)
std::shared_ptr< Wcs const > getWcs() const
Definition: Exposure.h:209
virtual void write(lsst::daf::base::Persistable const *persistable, std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
afw::table::Key< afw::table::Array< ImagePixelT > > image
static std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)
float VariancePixel
default type for MaskedImage variance images
std::shared_ptr< lsst::daf::base::PropertySet > getMetadata() const
Return flexible metadata.
Definition: Exposure.h:219