LSSTApplications  18.1.0
LSSTDataManagementBasePackage
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 
42 #include "lsst/afw/table/io/Persistable.cc"
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  schema.getCitizen().markPersistent();
198  }
199 };
200 
201 class FilterFactory : public table::io::PersistableFactory {
202 public:
203  std::shared_ptr<table::io::Persistable> read(InputArchive const& archive,
204  CatalogVector const& catalogs) const override {
205  PersistenceHelper const& keys = PersistenceHelper::get();
206  LSST_ARCHIVE_ASSERT(catalogs.size() == 1u);
207  LSST_ARCHIVE_ASSERT(catalogs.front().getSchema() == keys.schema);
208  return std::make_shared<Filter>(catalogs.front().begin()->get(keys.name), true);
209  }
210 
211  FilterFactory(std::string const& name) : afw::table::io::PersistableFactory(name) {}
212 };
213 
214 std::string getPersistenceName() { return "Filter"; }
215 
216 FilterFactory registration(getPersistenceName());
217 
218 } // namespace
219 
220 bool Filter::isPersistable() const noexcept { return true; }
221 
222 std::string Filter::getPersistenceName() const { return getPersistenceName(); }
223 
224 std::string Filter::getPythonModule() const { return "lsst.afw.image"; };
225 
226 void Filter::write(OutputArchiveHandle& handle) const {
227  PersistenceHelper const& keys = PersistenceHelper::get();
228  table::BaseCatalog catalog = handle.makeCatalog(keys.schema);
229  std::shared_ptr<table::BaseRecord> record = catalog.addNew();
230  record->set(keys.name, getName());
231  handle.saveCatalog(catalog);
232 }
233 
234 bool Filter::operator==(Filter const& rhs) const noexcept { return _id != UNKNOWN && _id == rhs._id; }
235 
236 std::size_t Filter::hash_value() const noexcept { return std::hash<int>()(_id); }
237 
238 void Filter::_initRegistry() {
239  _id0 = UNKNOWN;
240  delete _aliasMap;
241  delete _nameMap;
242  delete _idMap;
243 
244  _aliasMap = new AliasMap;
245  _nameMap = new NameMap;
246  _idMap = new IdMap;
247 
248  define(FilterProperty(unknownFilter, daf::base::PropertySet(), true));
249 }
250 
251 int Filter::_id0 = Filter::UNKNOWN;
252 
253 // dynamically allocated as that avoids an intel bug with static variables in dynamic libraries
254 Filter::AliasMap* Filter::_aliasMap = NULL;
255 Filter::NameMap* Filter::_nameMap = NULL;
256 Filter::IdMap* Filter::_idMap = NULL;
257 
258 int Filter::define(FilterProperty const& fp, int id, bool force) {
259  if (!_nameMap) {
260  _initRegistry();
261  }
262 
263  std::string const& name = fp.getName();
264  NameMap::iterator keyVal = _nameMap->find(name);
265 
266  if (keyVal != _nameMap->end()) {
267  int oid = keyVal->second;
268 
269  if (id == oid || id == AUTO) {
270  return oid; // OK, same value as before
271  }
272 
273  if (!force) {
274  throw LSST_EXCEPT(pexEx::RuntimeError, "Filter " + name + " is already defined");
275  }
276  _nameMap->erase(keyVal);
277  _idMap->erase(oid);
278  }
279 
280  if (id == AUTO) {
281  id = _id0;
282  ++_id0;
283  }
284 
285  _nameMap->insert(std::make_pair(name, id));
286  _idMap->insert(std::make_pair(id, name));
287 
288  return id;
289 }
290 
291 int Filter::defineAlias(std::string const& oldName, std::string const& newName, bool force) {
292  if (!_nameMap) {
293  _initRegistry();
294  }
295 
296  // Lookup oldName
297  NameMap::iterator keyVal = _nameMap->find(oldName);
298  if (keyVal == _nameMap->end()) {
299  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + oldName);
300  }
301  int const id = keyVal->second;
302 
303  // Lookup oldName in aliasMap
304  AliasMap::iterator aliasKeyVal = _aliasMap->find(newName);
305  if (aliasKeyVal != _aliasMap->end()) {
306  if (aliasKeyVal->second == oldName) {
307  return id; // OK, same value as before
308  }
309 
310  if (!force) {
311  throw LSST_EXCEPT(pexEx::NotFoundError, "Filter " + newName + " is already defined");
312  }
313  _aliasMap->erase(aliasKeyVal);
314  }
315 
316  _aliasMap->insert(std::make_pair(newName, oldName));
317 
318  return id;
319 }
320 
321 int Filter::_lookup(std::string const& name, bool const force) {
322  if (!_nameMap) {
323  _initRegistry();
324  }
325 
326  NameMap::iterator keyVal = _nameMap->find(name);
327 
328  if (keyVal == _nameMap->end()) {
329  AliasMap::iterator aliasKeyVal = _aliasMap->find(name);
330  if (aliasKeyVal != _aliasMap->end()) {
331  return _lookup(aliasKeyVal->second);
332  }
333 
334  if (force) {
335  return UNKNOWN;
336  } else {
337  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + name);
338  }
339  }
340 
341  return keyVal->second;
342 }
343 
344 std::string const& Filter::_lookup(int id) {
345  if (!_idMap) {
346  _initRegistry();
347  }
348 
349  IdMap::iterator keyVal = _idMap->find(id);
350 
351  if (keyVal == _idMap->end()) {
352  throw LSST_EXCEPT(pexEx::NotFoundError, (boost::format("Unable to find filter %d") % id).str());
353  }
354 
355  return keyVal->second;
356 }
358  //
359  // Map name to its ID and back to resolve aliases
360  //
361  int const id = _lookup(_name, true);
362  std::string const& name = (id == UNKNOWN) ? _name : _lookup(id);
363 
364  return FilterProperty::lookup(name);
365 }
366 } // namespace image
367 } // namespace afw
368 } // namespace lsst
double getAsDouble(std::string const &name) const
Get the last value for any arithmetic property name (possibly hierarchical).
bool operator==(Filter const &rhs) const noexcept
Are two filters identical?
Definition: Filter.cc:234
uint64_t * ptr
Definition: RangeSet.cc:88
std::string const & getName() const noexcept
Return a filter&#39;s name.
Definition: Filter.h:76
std::string getPersistenceName() const override
Return the unique name used to persist this object and look up its factory.
Definition: Filter.cc:222
An object passed to Persistable::write to allow it to persist itself.
std::shared_ptr< typehandling::Storable > cloneStorable() const override
Create a new Filter that is a copy of this one.
Definition: Filter.cc:176
static int const UNKNOWN
Definition: Filter.h:144
Interface supporting iteration over heterogenous containers.
Definition: Storable.h:56
int stripFilterKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Filter-related keywords from the metadata.
Definition: Filter.cc:132
T end(T... args)
table::Key< int > id
Definition: Detector.cc:166
bool isPersistable() const noexcept override
Return true if this particular object can be persisted using afw::table::io.
Definition: Filter.cc:220
Rotation angle is unknown.
STL class.
static FilterProperty const & lookup(std::string const &name)
Lookup the properties of a filter "name".
Definition: Filter.cc:101
bool equals(typehandling::Storable const &other) const noexcept override
Compare this object to another Storable.
Definition: Filter.cc:180
Reports attempts to access elements using an invalid key.
Definition: Runtime.h:151
T push_back(T... args)
A base class for image defects.
bool operator==(FilterProperty const &rhs) const noexcept
Return true iff two FilterProperties are identical.
Definition: Filter.cc:87
#define LSST_ARCHIVE_ASSERT(EXPR)
An assertion macro used to validate the structure of an InputArchive.
Definition: Persistable.h:48
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:224
static int const AUTO
Definition: Filter.h:143
static int define(FilterProperty const &filterProperty, int id=AUTO, bool force=false)
Define a filter name to have the specified id.
Definition: Filter.cc:258
T erase(T... args)
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
table::Schema schema
Definition: Filter.cc:187
T make_pair(T... args)
std::vector< std::string > getAliases() const
Return all aliases by which this filter is known.
Definition: Filter.cc:146
bool exists(std::string const &name) const
Determine if a name (possibly hierarchical) exists.
static int defineAlias(std::string const &oldName, std::string const &newName, bool force=false)
Define an alias for a filter.
Definition: Filter.cc:291
Holds an integer identifier for an LSST filter.
Definition: Filter.h:141
table::Key< std::string > name
Definition: Filter.cc:188
std::size_t hash_value() const noexcept override
Return a hash of this object.
Definition: Filter.cc:236
BaseCatalog makeCatalog(Schema const &schema)
Return a new, empty catalog with the given schema.
T insert(T... args)
T find(T... args)
#define LSST_EXCEPT(type,...)
Create an exception with a given type.
Definition: Exception.h:48
T begin(T... args)
Key< U > key
Definition: Schema.cc:281
Class for storing generic metadata.
Definition: PropertySet.h:68
ItemVariant const * other
Definition: Schema.cc:56
T sort(T... args)
Filter(std::string const &name, bool const force=false)
Creates a Filter with the given name.
Definition: Filter.h:149
FilterProperty const & getFilterProperty() const
Return a Filter&#39;s FilterProperty.
Definition: Filter.cc:357
void saveCatalog(BaseCatalog const &catalog)
Save a catalog in the archive.
Backwards-compatibility support for depersisting the old Calib (FluxMag0/FluxMag0Err) objects...
Describe the properties of a Filter (e.g.
Definition: Filter.h:51
FilterProperty(std::string const &name, double lambdaEff, double lambdaMin=NAN, double lambdaMax=NAN, bool force=false)
Definition: Filter.h:53
int end
static std::vector< std::string > getNames()
Return a list of known filters.
Definition: Filter.cc:159
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Filter.cc:91
Reports errors that are due to events beyond the control of the program.
Definition: Runtime.h:104
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
void write(OutputArchiveHandle &handle) const override
Write the object to one or more catalogs.
Definition: Filter.cc:226