LSSTApplications  18.1.0
LSSTDataManagementBasePackage
ApCorrMap.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008-2014 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #include <memory>
25 
30 #include "lsst/afw/table/io/Persistable.cc"
31 
32 namespace lsst {
33 namespace afw {
34 
37 
38 namespace image {
39 
40 // Even though this static const member is set in the header, it needs to be declared here if we need
41 // to take its address (and Swig might).
42 std::size_t const ApCorrMap::MAX_NAME_LENGTH;
43 
44 std::shared_ptr<math::BoundedField> const ApCorrMap::operator[](std::string const& name) const {
45  Iterator i = _internal.find(name);
46  if (i == _internal.end()) {
48  (boost::format("Aperture correction with name '%s' not found") % name).str());
49  }
50  return i->second;
51 }
52 
53 std::shared_ptr<math::BoundedField> const ApCorrMap::get(std::string const& name) const {
54  Iterator i = _internal.find(name);
55  if (i == _internal.end()) {
57  }
58  return i->second;
59 }
60 
62  if (name.size() > MAX_NAME_LENGTH) {
63  throw LSST_EXCEPT(
65  (boost::format("Aperture correction name '%s' exceeds size limit of %d characters") % name %
66  MAX_NAME_LENGTH)
67  .str());
68  }
69  _internal.insert(std::make_pair(name, field));
70 }
71 
72 namespace {
73 
74 struct PersistenceHelper {
75  table::Schema schema;
76  table::Key<std::string> name;
77  table::Key<int> field;
78 
79  static PersistenceHelper const& get() {
80  static PersistenceHelper const instance;
81  return instance;
82  }
83 
84 private:
85  PersistenceHelper()
86  : schema(),
87  name(schema.addField<std::string>("name", "name of the aperture correction",
88  ApCorrMap::MAX_NAME_LENGTH)),
89  field(schema.addField<int>("field", "archive ID of the BoundedField object")) {
90  schema.getCitizen().markPersistent();
91  }
92 };
93 
94 class ApCorrMapFactory : public table::io::PersistableFactory {
95 public:
96  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
97  CatalogVector const& catalogs) const override {
98  PersistenceHelper const& keys = PersistenceHelper::get();
99  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
100  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
101  std::shared_ptr<ApCorrMap> result = std::make_shared<ApCorrMap>();
102  for (table::BaseCatalog::const_iterator i = catalogs.front().begin(); i != catalogs.front().end();
103  ++i) {
104  result->set(i->get(keys.name), archive.get<math::BoundedField>(i->get(keys.field)));
105  }
106  return result;
107  }
108 
109  ApCorrMapFactory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
110 };
111 
112 std::string getApCorrMapPersistenceName() { return "ApCorrMap"; }
113 
114 ApCorrMapFactory registration(getApCorrMapPersistenceName());
115 
116 } // namespace
117 
118 bool ApCorrMap::isPersistable() const noexcept {
119  for (Iterator i = begin(); i != end(); ++i) {
120  if (!i->second->isPersistable()) return false;
121  }
122  return true;
123 }
124 
125 std::string ApCorrMap::getPersistenceName() const { return getApCorrMapPersistenceName(); }
126 
127 std::string ApCorrMap::getPythonModule() const { return "lsst.afw.image"; }
128 
129 void ApCorrMap::write(OutputArchiveHandle& handle) const {
130  PersistenceHelper const& keys = PersistenceHelper::get();
131  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
132  for (Iterator i = begin(); i != end(); ++i) {
133  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
134  record->set(keys.name, i->first);
135  record->set(keys.field, handle.put(i->second));
136  }
137  handle.saveCatalog(catalog);
138 }
139 
141  Internal replacement;
142  for (Iterator i = begin(); i != end(); ++i) {
143  replacement[i->first] = (*i->second) * scale;
144  }
145  _internal = replacement;
146  return *this;
147 }
148 
149 std::shared_ptr<typehandling::Storable> ApCorrMap::cloneStorable() const {
150  return std::make_unique<ApCorrMap>(*this);
151 }
152 
153 } // namespace image
154 } // namespace afw
155 } // namespace lsst
static std::shared_ptr< T > dynamicCast(std::shared_ptr< Persistable > const &ptr)
Dynamically cast a shared_ptr.
Definition: Persistable.cc:18
int put(Persistable const *obj, bool permissive=false)
Save an object to the archive and return a unique ID that can be used to retrieve it from an InputArc...
An object passed to Persistable::write to allow it to persist itself.
table::Key< int > field
Definition: ApCorrMap.cc:77
A thin wrapper around std::map to allow aperture corrections to be attached to Exposures.
Definition: ApCorrMap.h:45
Reports attempts to exceed implementation-defined length limits for some classes. ...
Definition: Runtime.h:76
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:109
py::object result
Definition: schema.cc:418
daf::base::PropertySet * set
Definition: fits.cc:884
STL class.
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
A base class for image defects.
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
table::Schema schema
Definition: ApCorrMap.cc:75
T make_pair(T... args)
table::Key< std::string > name
Definition: ApCorrMap.cc:76
CatalogIterator< typename Internal::const_iterator > const_iterator
Definition: Catalog.h:111
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
T size(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
Image< LhsPixelT > & operator*=(Image< LhsPixelT > &lhs, Image< RhsPixelT > const &rhs)
Multiply lhs by Image rhs (i.e. pixel-by-pixel multiplication) where types are different.
Definition: Image.cc:698
Internal::const_iterator Iterator
Iterator type returned by begin() and end().
Definition: ApCorrMap.h:54
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
int end
std::shared_ptr< RecordT > addNew()
Create a new record, add it to the end of the catalog, and return a pointer to it.
Definition: Catalog.h:485