lsst.afw  21.0.0-8-gd9c33f4a0+ec3713772a
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 
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 
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
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 
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 
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 }
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 
363 }
364 } // namespace image
365 } // namespace afw
366 } // namespace lsst
lsst::afw::image
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects.
Definition: imageAlgorithm.dox:1
LSST_ARCHIVE_ASSERT
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
lsst::afw::image::Filter::Filter
Filter(std::string const &name, bool const force=false)
Creates a Filter with the given name.
Definition: Filter.h:149
std::string
STL class.
std::shared_ptr
STL class.
lsst::afw::image::detail::stripFilterKeywords
int stripFilterKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Filter-related keywords from the metadata.
Definition: Filter.cc:132
lsst::afw::image::Filter::define
static int define(FilterProperty const &filterProperty, int id=AUTO, bool force=false)
Define a filter name to have the specified id.
Definition: Filter.cc:256
lsst::afw::image::Filter::write
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: Filter.cc:224
lsst::afw::table::io::OutputArchiveHandle
An object passed to Persistable::write to allow it to persist itself.
Definition: OutputArchive.h:118
std::vector< std::string >
std::unordered_map::find
T find(T... args)
lsst::pex::exceptions::NotFoundError
lsst::afw::image::Filter
Holds an integer identifier for an LSST filter.
Definition: Filter.h:141
lsst::afw::table::io::OutputArchiveHandle::saveCatalog
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Definition: OutputArchive.cc:211
lsst::afw::image::FilterProperty
Describe the properties of a Filter (e.g.
Definition: Filter.h:51
lsst::afw::image::Filter::getPythonModule
std::string getPythonModule() const override
Return the fully-qualified Python module that should be imported to guarantee that its factory is reg...
Definition: Filter.cc:222
lsst::daf::base::PropertySet::exists
bool exists(std::string const &name) const
keys
def keys(self)
CatalogVector.h
lsst::afw::typehandling::Storable
Interface supporting iteration over heterogenous containers.
Definition: Storable.h:58
Filter.h
lsst::afw::image::FilterProperty::FilterProperty
FilterProperty(std::string const &name, double lambdaEff, double lambdaMin=NAN, double lambdaMax=NAN, bool force=false)
Definition: Filter.h:53
std::sort
T sort(T... args)
end
int end
Definition: BoundedField.cc:105
lsst::afw::image::Filter::defineAlias
static int defineAlias(std::string const &oldName, std::string const &newName, bool force=false)
Define an alias for a filter.
Definition: Filter.cc:289
std::vector::push_back
T push_back(T... args)
lsst::afw::table::io::OutputArchiveHandle::makeCatalog
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
Definition: OutputArchive.cc:207
lsst::afw::image::Filter::isPersistable
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: Filter.cc:218
schema
table::Schema schema
Definition: Filter.cc:187
lsst::afw::image::Filter::AUTO
static int const AUTO
Definition: Filter.h:143
lsst::afw::image::FilterProperty::hash_value
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Filter.cc:91
lsst::afw::image::Filter::operator==
bool operator==(Filter const &rhs) const noexcept
Are two filters identical?
Definition: Filter.cc:232
id
table::Key< int > id
Definition: Detector.cc:162
lsst::daf::base::PropertySet::getAsString
std::string getAsString(std::string const &name) const
other
ItemVariant const * other
Definition: Schema.cc:56
lsst::afw::image::Filter::getNames
static std::vector< std::string > getNames()
Return a list of known filters.
Definition: Filter.cc:159
lsst::afw::image::Filter::getFilterProperty
FilterProperty const & getFilterProperty() const
Return a Filter's FilterProperty.
Definition: Filter.cc:355
lsst::afw::image::Filter::hash_value
std::size_t hash_value() const noexcept override
Return a hash of this object.
Definition: Filter.cc:234
Persistable.cc
std::unordered_map::erase
T erase(T... args)
PropertySet.h
lsst::afw::image::FilterProperty::getName
std::string const & getName() const noexcept
Return a filter's name.
Definition: Filter.h:76
lsst
A base class for image defects.
lsst::daf::base::PropertySet::remove
virtual void remove(std::string const &name)
LSST_EXCEPT
#define LSST_EXCEPT(type,...)
lsst::daf::base::PropertySet::getAsDouble
double getAsDouble(std::string const &name) const
exceptions.h
lsst::afw::image::Filter::equals
bool equals(typehandling::Storable const &other) const noexcept override
Compare this object to another Storable.
Definition: Filter.cc:180
std::unordered_map::begin
T begin(T... args)
std
STL namespace.
std::unordered_map::insert
T insert(T... args)
lsst::afw::image::Filter::UNKNOWN
static int const UNKNOWN
Definition: Filter.h:144
key
Key< U > key
Definition: Schema.cc:281
name
table::Key< std::string > name
Definition: Filter.cc:188
lsst::daf::base::PropertySet
lsst::afw::image::Filter::getCanonicalName
std::string const & getCanonicalName() const
Return a filter's canonical name.
Definition: Filter.h:195
InputArchive.h
lsst::pex::exceptions
std::size_t
std::make_pair
T make_pair(T... args)
std::unordered_map::end
T end(T... args)
lsst::afw::table::CatalogT::addNew
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
lsst::afw::image::Filter::getName
std::string const & getName() const noexcept
Return a Filter's name.
Definition: Filter.h:189
lsst::afw::image::Filter::getAliases
std::vector< std::string > getAliases() const
Return all aliases by which this filter is known.
Definition: Filter.cc:146
lsst::afw::image::FilterProperty::lookup
static FilterProperty const & lookup(std::string const &name)
Lookup the properties of a filter "name".
Definition: Filter.cc:101
lsst::afw::table::CatalogT< BaseRecord >
lsst::afw::image::Filter::cloneStorable
std::shared_ptr< typehandling::Storable > cloneStorable() const override
Create a new Filter that is a copy of this one.
Definition: Filter.cc:176
OutputArchive.h
lsst::afw::image::Filter::getPersistenceName
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: Filter.cc:220
lsst::pex::exceptions::RuntimeError
std::hash
lsst::afw::image::FilterProperty::operator==
bool operator==(FilterProperty const &rhs) const noexcept
Return true iff two FilterProperties are identical.
Definition: Filter.cc:87