LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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 "boost/make_shared.hpp"
25 
30 
31 namespace lsst { namespace afw { namespace image {
32 
33 // Even though this static const member is set in the header, it needs to be declared here if we need
34 // to take its address (and Swig might).
35 std::size_t const ApCorrMap::MAX_NAME_LENGTH;
36 
37 PTR(math::BoundedField) const ApCorrMap::operator[](std::string const & name) const {
38  Iterator i = _internal.find(name);
39  if (i == _internal.end()) {
40  throw LSST_EXCEPT(
41  pex::exceptions::NotFoundError,
42  (boost::format("Aperture correction with name '%s' not found") % name).str()
43  );
44  }
45  return i->second;
46 }
47 
48 PTR(math::BoundedField) const ApCorrMap::get(std::string const & name) const {
49  Iterator i = _internal.find(name);
50  if (i == _internal.end()) {
51  return PTR(math::BoundedField)();
52  }
53  return i->second;
54 }
55 
56 void ApCorrMap::set(std::string const & name, PTR(math::BoundedField) field) {
57  if (name.size() > MAX_NAME_LENGTH) {
58  throw LSST_EXCEPT(
59  pex::exceptions::LengthError,
60  (boost::format("Aperture correction name '%s' exceeds size limit of %d characters")
61  % name % MAX_NAME_LENGTH).str()
62  );
63  }
64  _internal.insert(std::make_pair(name, field));
65 }
66 
67 namespace {
68 
69 struct PersistenceHelper {
70  table::Schema schema;
71  table::Key<std::string> name;
72  table::Key<int> field;
73 
74  static PersistenceHelper const & get() {
75  static PersistenceHelper const instance;
76  return instance;
77  }
78 
79 private:
80 
81  PersistenceHelper() :
82  schema(),
83  name(schema.addField<std::string>("name", "name of the aperture correction",
84  ApCorrMap::MAX_NAME_LENGTH)),
85  field(schema.addField<int>("field", "archive ID of the BoundedField object"))
86  {
87  schema.getCitizen().markPersistent();
88  }
89 
90 };
91 
92 class ApCorrMapFactory : public table::io::PersistableFactory {
93 public:
94 
95  virtual PTR(table::io::Persistable)
96  read(InputArchive const & archive, CatalogVector const & catalogs) const {
97  PersistenceHelper const & keys = PersistenceHelper::get();
98  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
99  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
100  PTR(ApCorrMap) result
101  = boost::make_shared<ApCorrMap>();
102  for (
103  table::BaseCatalog::const_iterator i = catalogs.front().begin();
104  i != catalogs.front().end();
105  ++i
106  ) {
107  result->set(i->get(keys.name), archive.get<math::BoundedField>(i->get(keys.field)));
108  }
109  return result;
110  }
111 
112  ApCorrMapFactory(std::string const & name) : afw::table::io::PersistableFactory(name) {}
113 
114 };
115 
116 std::string getApCorrMapPersistenceName() {
117  return "ApCorrMap";
118 }
119 
120 ApCorrMapFactory registration(getApCorrMapPersistenceName());
121 
122 } // anonymous
123 
125  for (Iterator i = begin(); i != end(); ++i) {
126  if (!i->second->isPersistable()) return false;
127  }
128  return true;
129 }
130 
131 std::string ApCorrMap::getPersistenceName() const {
132  return getApCorrMapPersistenceName();
133 }
134 
135 std::string ApCorrMap::getPythonModule() const {
136  return "lsst.afw.image";
137 }
138 
139 void ApCorrMap::write(OutputArchiveHandle & handle) const {
140  PersistenceHelper const & keys = PersistenceHelper::get();
141  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
142  for (Iterator i = begin(); i != end(); ++i) {
143  PTR(table::BaseRecord) record = catalog.addNew();
144  record->set(keys.name, i->first);
145  record->set(keys.field, handle.put(i->second));
146  }
147  handle.saveCatalog(catalog);
148 }
149 
150 void ApCorrMap::operator*=(double const scale) {
151  Internal replacement;
152  for (Iterator i = begin(); i != end(); ++i) {
153  replacement[i->first] = (*i->second)*scale;
154  }
155  _internal = replacement;
156 }
157 
158 }}} // namespace lsst::afw::image
Iterator begin() const
Definition: ApCorrMap.h:52
virtual std::string getPythonModule() const
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: ApCorrMap.cc:135
static std::size_t const MAX_NAME_LENGTH
Maximum number of characters for an aperture correction name (required for persistence).
Definition: ApCorrMap.h:47
void set(std::string const &name, boost::shared_ptr< math::BoundedField > field)
Add or replace an aperture correction.
Definition: ApCorrMap.cc:56
int put(Persistable const *obj, bool permissive=false)
Save a nested Persistable to the same archive.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
for(FootprintList::const_iterator ptr=feet->begin(), end=feet->end();ptr!=end;++ptr)
Definition: saturated.cc:82
CatalogT< BaseRecord > BaseCatalog
Definition: fwd.h:61
An object passed to Persistable::write to allow it to persist itself.
A custom container class for records, based on std::vector.
Definition: Catalog.h:94
afw::table::Schema schema
Definition: GaussianPsf.cc:41
A thin wrapper around std::map to allow aperture corrections to be attached to Exposures.
Definition: ApCorrMap.h:42
#define PTR(...)
Definition: base.h:41
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
virtual std::string getPersistenceName() const
Return the unique name used to persist this object and look up its factory.
Definition: ApCorrMap.cc:131
void operator*=(double const scale)
Scale all fields by a constant.
Definition: ApCorrMap.cc:150
Iterator end() const
Definition: ApCorrMap.h:53
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
virtual bool isPersistable() const
Whether the map is persistable (true IFF all contained BoundedFields are persistable).
Definition: ApCorrMap.cc:124
Base class for all records.
Definition: BaseRecord.h:27
virtual void write(OutputArchiveHandle &handle) const
Write the object to one or more catalogs.
Definition: ApCorrMap.cc:139
boost::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:469
An abstract base class for 2-d functions defined on an integer bounding boxes.
Definition: BoundedField.h:49
Internal::const_iterator Iterator
Iterator type returned by begin() and end(). Dereferences to a pair&lt;string,PTR(BoundedField)&gt;.
Definition: ApCorrMap.h:50
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
std::map< std::string, boost::shared_ptr< math::BoundedField > > Internal
Definition: ApCorrMap.h:43
table::Key< int > field
Definition: ApCorrMap.cc:72