lsst.afw  14.0-23-g38c323faa+3
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 
31 namespace lsst {
32 namespace afw {
33 namespace image {
34 
35 // Even though this static const member is set in the header, it needs to be declared here if we need
36 // to take its address (and Swig might).
38 
40  Iterator i = _internal.find(name);
41  if (i == _internal.end()) {
43  (boost::format("Aperture correction with name '%s' not found") % name).str());
44  }
45  return i->second;
46 }
47 
49  Iterator i = _internal.find(name);
50  if (i == _internal.end()) {
52  }
53  return i->second;
54 }
55 
57  if (name.size() > MAX_NAME_LENGTH) {
58  throw LSST_EXCEPT(
60  (boost::format("Aperture correction name '%s' exceeds size limit of %d characters") % name %
62  .str());
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  PersistenceHelper()
81  : schema(),
82  name(schema.addField<std::string>("name", "name of the aperture correction",
83  ApCorrMap::MAX_NAME_LENGTH)),
84  field(schema.addField<int>("field", "archive ID of the BoundedField object")) {
85  schema.getCitizen().markPersistent();
86  }
87 };
88 
89 class ApCorrMapFactory : public table::io::PersistableFactory {
90 public:
91  virtual std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
92  CatalogVector const& catalogs) const {
93  PersistenceHelper const& keys = PersistenceHelper::get();
94  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
95  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
96  std::shared_ptr<ApCorrMap> result = std::make_shared<ApCorrMap>();
97  for (table::BaseCatalog::const_iterator i = catalogs.front().begin(); i != catalogs.front().end();
98  ++i) {
99  result->set(i->get(keys.name), archive.get<math::BoundedField>(i->get(keys.field)));
100  }
101  return result;
102  }
103 
104  ApCorrMapFactory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
105 };
106 
107 std::string getApCorrMapPersistenceName() { return "ApCorrMap"; }
108 
109 ApCorrMapFactory registration(getApCorrMapPersistenceName());
110 
111 } // anonymous
112 
114  for (Iterator i = begin(); i != end(); ++i) {
115  if (!i->second->isPersistable()) return false;
116  }
117  return true;
118 }
119 
120 std::string ApCorrMap::getPersistenceName() const { return getApCorrMapPersistenceName(); }
121 
122 std::string ApCorrMap::getPythonModule() const { return "lsst.afw.image"; }
123 
124 void ApCorrMap::write(OutputArchiveHandle& handle) const {
125  PersistenceHelper const& keys = PersistenceHelper::get();
126  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
127  for (Iterator i = begin(); i != end(); ++i) {
128  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
129  record->set(keys.name, i->first);
130  record->set(keys.field, handle.put(i->second));
131  }
132  handle.saveCatalog(catalog);
133 }
134 
136  Internal replacement;
137  for (Iterator i = begin(); i != end(); ++i) {
138  replacement[i->first] = (*i->second) * scale;
139  }
140  _internal = replacement;
141  return *this;
142 }
143 }
144 }
145 } // namespace lsst::afw::image
Iterator begin() const
Definition: ApCorrMap.h:55
static std::size_t const MAX_NAME_LENGTH
Maximum number of characters for an aperture correction name (required for persistence).
Definition: ApCorrMap.h:49
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
std::shared_ptr< math::BoundedField > const get(std::string const &name) const
Return the field with the given name, returning an empty pointer when the name is not present...
Definition: ApCorrMap.cc:48
virtual bool isPersistable() const
Whether the map is persistable (true IFF all contained BoundedFields are persistable).
Definition: ApCorrMap.cc:113
Iterator end() const
Definition: ApCorrMap.h:56
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:96
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:47
A thin wrapper around std::map to allow aperture corrections to be attached to Exposures.
Definition: ApCorrMap.h:44
def scale(algorithm, min, max=None, frame=None)
Definition: ds9.py:117
ApCorrMap & operator*=(double const scale)
Scale all fields by a constant.
Definition: ApCorrMap.cc:135
T end(T... args)
STL class.
A base class for image defects.
T make_pair(T... args)
CatalogIterator< typename Internal::const_iterator > const_iterator
Definition: Catalog.h:110
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
T insert(T... args)
T find(T... args)
T size(T... args)
#define LSST_EXCEPT(type,...)
void set(std::string const &name, std::shared_ptr< math::BoundedField > field)
Add or replace an aperture correction.
Definition: ApCorrMap.cc:56
table::Schema schema
Definition: ApCorrMap.cc:70
Internal::const_iterator Iterator
Iterator type returned by begin() and end().
Definition: ApCorrMap.h:53
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
std::shared_ptr< math::BoundedField > const operator[](std::string const &name) const
Return the field with the given name, throwing NotFoundError when the name is not present...
Definition: ApCorrMap.cc:39
table::Key< int > field
Definition: ApCorrMap.cc:72
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:463