lsst.afw  22.0.0-22-gf1d71818e+0d6c1a9e28
Filter.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 //##====---------------- ----------------====##/
27 //
28 // Implements looking up a filter identifier by name.
29 //
30 //##====---------------- ----------------====##/
31 #include <cmath>
32 #include "boost/format.hpp"
33 #include "boost/algorithm/string/trim.hpp"
34 #include "lsst/pex/exceptions.h"
36 
37 #include "lsst/afw/image/Filter.h"
38 
43 
44 namespace pexEx = lsst::pex::exceptions;
45 
46 namespace lsst {
47 namespace afw {
48 namespace image {
49 
50 FilterProperty::PropertyMap* FilterProperty::_propertyMap = NULL;
51 
53  : _name(name), _lambdaEff(NAN), _lambdaMin(NAN), _lambdaMax(NAN) {
54  if (prop.exists("lambdaEff")) {
55  _lambdaEff = prop.getAsDouble("lambdaEff");
56  }
57  if (prop.exists("lambdaMin")) {
58  _lambdaMin = prop.getAsDouble("lambdaMin");
59  }
60  if (prop.exists("lambdaMax")) {
61  _lambdaMax = prop.getAsDouble("lambdaMax");
62  }
63  _insert(force);
64 }
65 
66 void FilterProperty::_insert(bool force) {
67  if (!_propertyMap) {
68  _initRegistry();
69  }
70 
71  PropertyMap::iterator keyVal = _propertyMap->find(getName());
72 
73  if (keyVal != _propertyMap->end()) {
74  if (keyVal->second == *this) {
75  return; // OK, a redefinition with identical values
76  }
77 
78  if (!force) {
79  throw LSST_EXCEPT(pexEx::RuntimeError, "Filter " + getName() + " is already defined");
80  }
81  _propertyMap->erase(keyVal);
82  }
83 
84  _propertyMap->insert(std::make_pair(getName(), *this));
85 }
86 
87 bool FilterProperty::operator==(FilterProperty const& rhs) const noexcept {
88  return (_lambdaEff == rhs._lambdaEff);
89 }
90 
91 std::size_t FilterProperty::hash_value() const noexcept { return std::hash<double>()(_lambdaEff); };
92 
93 void FilterProperty::_initRegistry() {
94  if (_propertyMap) {
95  delete _propertyMap;
96  }
97 
98  _propertyMap = new PropertyMap;
99 }
100 
101 FilterProperty const& FilterProperty::lookup(std::string const& name) {
102  if (!_propertyMap) {
103  _initRegistry();
104  }
105 
106  PropertyMap::iterator keyVal = _propertyMap->find(name);
107 
108  if (keyVal == _propertyMap->end()) {
109  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + name);
110  }
111 
112  return keyVal->second;
113 }
114 
115 namespace {
116 std::string const unknownFilter = "_unknown_";
117 }
118 
119 int const Filter::AUTO = -1;
120 int const Filter::UNKNOWN = -1;
121 
122 Filter::Filter(std::shared_ptr<lsst::daf::base::PropertySet const> metadata, bool const force) {
123  std::string const key = "FILTER";
124  if (metadata->exists(key)) {
125  std::string filterName = boost::algorithm::trim_right_copy(metadata->getAsString(key));
126  _id = _lookup(filterName, force);
127  _name = filterName;
128  }
129 }
130 
131 namespace detail {
133  int nstripped = 0;
134 
135  std::string key = "FILTER";
136  if (metadata->exists(key)) {
137  metadata->remove(key);
138  nstripped++;
139  }
140 
141  return nstripped;
142 }
143 } // namespace detail
144 
145 // N.b. we cannot declare a std::vector<std::string const&> as there's no way to push the references
146 std::vector<std::string> Filter::getAliases() const {
147  std::vector<std::string> aliases;
148 
149  std::string const& canonicalName = getCanonicalName();
150  for (AliasMap::iterator ptr = _aliasMap->begin(), end = _aliasMap->end(); ptr != end; ++ptr) {
151  if (ptr->second == canonicalName) {
152  aliases.push_back(ptr->first);
153  }
154  }
155 
156  return aliases;
157 }
158 
159 std::vector<std::string> Filter::getNames() {
160  if (!_nameMap) {
161  _initRegistry();
162  }
163 
165 
166  for (NameMap::const_iterator ptr = _nameMap->begin(), end = _nameMap->end(); ptr != end; ++ptr) {
167  if (ptr->first != unknownFilter) {
168  names.push_back(ptr->first);
169  }
170  }
171  std::sort(names.begin(), names.end());
172 
173  return names;
174 }
175 
176 std::shared_ptr<typehandling::Storable> Filter::cloneStorable() const {
177  return std::make_unique<Filter>(*this);
178 }
179 
180 bool Filter::equals(typehandling::Storable const& other) const noexcept {
181  return singleClassEquals(*this, other);
182 }
183 
184 namespace {
185 
186 struct PersistenceHelper {
187  table::Schema schema;
188  table::Key<std::string> name;
189 
190  static PersistenceHelper const& get() {
191  static PersistenceHelper const instance;
192  return instance;
193  }
194 
195 private:
196  PersistenceHelper() : schema(), name(schema.addField<std::string>("name", "name of the filter")) {}
197 };
198 
199 class FilterFactory : public table::io::PersistableFactory {
200 public:
201  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
202  CatalogVector const& catalogs) const override {
203  PersistenceHelper const& keys = PersistenceHelper::get();
204  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
205  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
206  return std::make_shared<Filter>(catalogs.front().begin()->get(keys.name), true);
207  }
208 
209  FilterFactory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
210 };
211 
212 std::string _getPersistenceName() { return "Filter"; }
213 
214 FilterFactory registration(_getPersistenceName());
215 
216 } // namespace
217 
218 bool Filter::isPersistable() const noexcept { return true; }
219 
220 std::string Filter::getPersistenceName() const { return _getPersistenceName(); }
221 
222 std::string Filter::getPythonModule() const { return "lsst.afw.image"; };
223 
224 void Filter::write(OutputArchiveHandle& handle) const {
225  PersistenceHelper const& keys = PersistenceHelper::get();
226  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
227  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
228  record->set(keys.name, getName());
229  handle.saveCatalog(catalog);
230 }
231 
232 bool Filter::operator==(Filter const& rhs) const noexcept { return _id != UNKNOWN && _id == rhs._id; }
233 
234 std::size_t Filter::hash_value() const noexcept { return std::hash<int>()(_id); }
235 
236 void Filter::_initRegistry() {
237  _id0 = UNKNOWN;
238  delete _aliasMap;
239  delete _nameMap;
240  delete _idMap;
241 
242  _aliasMap = new AliasMap;
243  _nameMap = new NameMap;
244  _idMap = new IdMap;
245 
246  define(FilterProperty(unknownFilter, daf::base::PropertySet(), true));
247 }
248 
249 int Filter::_id0 = Filter::UNKNOWN;
250 
251 // dynamically allocated as that avoids an intel bug with static variables in dynamic libraries
252 Filter::AliasMap* Filter::_aliasMap = NULL;
253 Filter::NameMap* Filter::_nameMap = NULL;
254 Filter::IdMap* Filter::_idMap = NULL;
255 
256 int Filter::define(FilterProperty const& fp, int id, bool force) {
257  if (!_nameMap) {
258  _initRegistry();
259  }
260 
261  std::string const& name = fp.getName();
262  NameMap::iterator keyVal = _nameMap->find(name);
263 
264  if (keyVal != _nameMap->end()) {
265  int oid = keyVal->second;
266 
267  if (id == oid || id == AUTO) {
268  return oid; // OK, same value as before
269  }
270 
271  if (!force) {
272  throw LSST_EXCEPT(pexEx::RuntimeError, "Filter " + name + " is already defined");
273  }
274  _nameMap->erase(keyVal);
275  _idMap->erase(oid);
276  }
277 
278  if (id == AUTO) {
279  id = _id0;
280  ++_id0;
281  }
282 
283  _nameMap->insert(std::make_pair(name, id));
284  _idMap->insert(std::make_pair(id, name));
285 
286  return id;
287 }
288 
289 int Filter::defineAlias(std::string const& oldName, std::string const& newName, bool force) {
290  if (!_nameMap) {
291  _initRegistry();
292  }
293 
294  // Lookup oldName
295  NameMap::iterator keyVal = _nameMap->find(oldName);
296  if (keyVal == _nameMap->end()) {
297  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + oldName);
298  }
299  int const id = keyVal->second;
300 
301  // Lookup oldName in aliasMap
302  AliasMap::iterator aliasKeyVal = _aliasMap->find(newName);
303  if (aliasKeyVal != _aliasMap->end()) {
304  if (aliasKeyVal->second == oldName) {
305  return id; // OK, same value as before
306  }
307 
308  if (!force) {
309  throw LSST_EXCEPT(pexEx::NotFoundError, "Filter " + newName + " is already defined");
310  }
311  _aliasMap->erase(aliasKeyVal);
312  }
313 
314  _aliasMap->insert(std::make_pair(newName, oldName));
315 
316  return id;
317 }
318 
319 int Filter::_lookup(std::string const& name, bool const force) {
320  if (!_nameMap) {
321  _initRegistry();
322  }
323 
324  NameMap::iterator keyVal = _nameMap->find(name);
325 
326  if (keyVal == _nameMap->end()) {
327  AliasMap::iterator aliasKeyVal = _aliasMap->find(name);
328  if (aliasKeyVal != _aliasMap->end()) {
329  return _lookup(aliasKeyVal->second);
330  }
331 
332  if (force) {
333  return UNKNOWN;
334  } else {
335  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + name);
336  }
337  }
338 
339  return keyVal->second;
340 }
341 
342 std::string const& Filter::_lookup(int id) {
343  if (!_idMap) {
344  _initRegistry();
345  }
346 
347  IdMap::iterator keyVal = _idMap->find(id);
348 
349  if (keyVal == _idMap->end()) {
350  throw LSST_EXCEPT(pexEx::NotFoundError, (boost::format("Unable to find filter %d") % id).str());
351  }
352 
353  return keyVal->second;
354 }
355 FilterProperty const& Filter::getFilterProperty() const {
356  //
357  // Map name to its ID and back to resolve aliases
358  //
359  int const id = _lookup(_name, true);
360  std::string const& name = (id == UNKNOWN) ? _name : _lookup(id);
361 
362  return FilterProperty::lookup(name);
363 }
364 } // namespace image
365 } // namespace afw
366 } // namespace lsst
int end
table::Key< int > id
Definition: Detector.cc:162
#define LSST_EXCEPT(type,...)
table::Key< std::string > name
Definition: Filter.cc:188
table::Schema schema
Definition: Filter.cc:187
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
T begin(T... args)
virtual void remove(std::string const &name)
bool exists(std::string const &name) const
double getAsDouble(std::string const &name) const
T end(T... args)
T make_pair(T... args)
def keys(self)
int stripFilterKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Filter-related keywords from the metadata.
Definition: Filter.cc:132
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
bool operator==(FilterProperty const &rhs) const noexcept
Return true iff two FilterProperties are identical.
std::string getPythonModule() const override
std::string getPersistenceName() const override
@ UNKNOWN
Rotation angle is unknown.
void write(OutputArchiveHandle &handle) const override
std::size_t hash_value() const noexcept
Return a hash of this object.
std::string const & getName() const noexcept
Return a filter's name.
Definition: Filter.h:78
FilterProperty(FilterProperty const &)=default
CatalogT< BaseRecord > BaseCatalog
Definition: fwd.h:71
A base class for image defects.
STL namespace.
T push_back(T... args)
T sort(T... args)
std::shared_ptr< table::io::Persistable > read(table::io::InputArchive const &archive, table::io::CatalogVector const &catalogs) const override
Definition: warpExposure.cc:0