lsst.afw  17.0.1-19-gee9b810f8+2
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 
39 namespace pexEx = lsst::pex::exceptions;
40 
41 namespace lsst {
42 namespace afw {
43 namespace image {
44 
45 FilterProperty::PropertyMap* FilterProperty::_propertyMap = NULL;
46 
48  : _name(name), _lambdaEff(NAN), _lambdaMin(NAN), _lambdaMax(NAN) {
49  if (prop.exists("lambdaEff")) {
50  _lambdaEff = prop.getAsDouble("lambdaEff");
51  }
52  if (prop.exists("lambdaMin")) {
53  _lambdaMin = prop.getAsDouble("lambdaMin");
54  }
55  if (prop.exists("lambdaMax")) {
56  _lambdaMax = prop.getAsDouble("lambdaMax");
57  }
58  _insert(force);
59 }
60 
61 void FilterProperty::_insert(bool force) {
62  if (!_propertyMap) {
63  _initRegistry();
64  }
65 
66  PropertyMap::iterator keyVal = _propertyMap->find(getName());
67 
68  if (keyVal != _propertyMap->end()) {
69  if (keyVal->second == *this) {
70  return; // OK, a redefinition with identical values
71  }
72 
73  if (!force) {
74  throw LSST_EXCEPT(pexEx::RuntimeError, "Filter " + getName() + " is already defined");
75  }
76  _propertyMap->erase(keyVal);
77  }
78 
79  _propertyMap->insert(std::make_pair(getName(), *this));
80 }
81 
82 bool FilterProperty::operator==(FilterProperty const& rhs) const noexcept {
83  return (_lambdaEff == rhs._lambdaEff);
84 }
85 
86 std::size_t FilterProperty::hash_value() const noexcept { return std::hash<double>()(_lambdaEff); };
87 
88 void FilterProperty::_initRegistry() {
89  if (_propertyMap) {
90  delete _propertyMap;
91  }
92 
93  _propertyMap = new PropertyMap;
94 }
95 
97  if (!_propertyMap) {
98  _initRegistry();
99  }
100 
101  PropertyMap::iterator keyVal = _propertyMap->find(name);
102 
103  if (keyVal == _propertyMap->end()) {
104  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + name);
105  }
106 
107  return keyVal->second;
108 }
109 
110 namespace {
111 std::string const unknownFilter = "_unknown_";
112 }
113 
114 int const Filter::AUTO = -1;
115 int const Filter::UNKNOWN = -1;
116 
118  std::string const key = "FILTER";
119  if (metadata->exists(key)) {
120  std::string filterName = boost::algorithm::trim_right_copy(metadata->getAsString(key));
121  _id = _lookup(filterName, force);
122  _name = filterName;
123  }
124 }
125 
126 namespace detail {
128  int nstripped = 0;
129 
130  std::string key = "FILTER";
131  if (metadata->exists(key)) {
132  metadata->remove(key);
133  nstripped++;
134  }
135 
136  return nstripped;
137 }
138 } // namespace detail
139 
140 // N.b. we cannot declare a std::vector<std::string const&> as there's no way to push the references
142  std::vector<std::string> aliases;
143 
144  std::string const& canonicalName = getCanonicalName();
145  for (AliasMap::iterator ptr = _aliasMap->begin(), end = _aliasMap->end(); ptr != end; ++ptr) {
146  if (ptr->second == canonicalName) {
147  aliases.push_back(ptr->first);
148  }
149  }
150 
151  return aliases;
152 }
153 
155  if (!_nameMap) {
156  _initRegistry();
157  }
158 
160 
161  for (NameMap::const_iterator ptr = _nameMap->begin(), end = _nameMap->end(); ptr != end; ++ptr) {
162  if (ptr->first != unknownFilter) {
163  names.push_back(ptr->first);
164  }
165  }
166  std::sort(names.begin(), names.end());
167 
168  return names;
169 }
170 
171 bool Filter::operator==(Filter const& rhs) const noexcept { return _id != UNKNOWN && _id == rhs._id; }
172 
173 std::size_t Filter::hash_value() const noexcept { return std::hash<int>()(_id); }
174 
175 void Filter::_initRegistry() {
176  _id0 = UNKNOWN;
177  delete _aliasMap;
178  delete _nameMap;
179  delete _idMap;
180 
181  _aliasMap = new AliasMap;
182  _nameMap = new NameMap;
183  _idMap = new IdMap;
184 
185  define(FilterProperty(unknownFilter, daf::base::PropertySet(), true));
186 }
187 
188 int Filter::_id0 = Filter::UNKNOWN;
189 
190 // dynamically allocated as that avoids an intel bug with static variables in dynamic libraries
191 Filter::AliasMap* Filter::_aliasMap = NULL;
192 Filter::NameMap* Filter::_nameMap = NULL;
193 Filter::IdMap* Filter::_idMap = NULL;
194 
195 int Filter::define(FilterProperty const& fp, int id, bool force) {
196  if (!_nameMap) {
197  _initRegistry();
198  }
199 
200  std::string const& name = fp.getName();
201  NameMap::iterator keyVal = _nameMap->find(name);
202 
203  if (keyVal != _nameMap->end()) {
204  int oid = keyVal->second;
205 
206  if (id == oid || id == AUTO) {
207  return oid; // OK, same value as before
208  }
209 
210  if (!force) {
211  throw LSST_EXCEPT(pexEx::RuntimeError, "Filter " + name + " is already defined");
212  }
213  _nameMap->erase(keyVal);
214  _idMap->erase(oid);
215  }
216 
217  if (id == AUTO) {
218  id = _id0;
219  ++_id0;
220  }
221 
222  _nameMap->insert(std::make_pair(name, id));
223  _idMap->insert(std::make_pair(id, name));
224 
225  return id;
226 }
227 
228 int Filter::defineAlias(std::string const& oldName, std::string const& newName, bool force) {
229  if (!_nameMap) {
230  _initRegistry();
231  }
232 
233  // Lookup oldName
234  NameMap::iterator keyVal = _nameMap->find(oldName);
235  if (keyVal == _nameMap->end()) {
236  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + oldName);
237  }
238  int const id = keyVal->second;
239 
240  // Lookup oldName in aliasMap
241  AliasMap::iterator aliasKeyVal = _aliasMap->find(newName);
242  if (aliasKeyVal != _aliasMap->end()) {
243  if (aliasKeyVal->second == oldName) {
244  return id; // OK, same value as before
245  }
246 
247  if (!force) {
248  throw LSST_EXCEPT(pexEx::NotFoundError, "Filter " + newName + " is already defined");
249  }
250  _aliasMap->erase(aliasKeyVal);
251  }
252 
253  _aliasMap->insert(std::make_pair(newName, oldName));
254 
255  return id;
256 }
257 
258 int Filter::_lookup(std::string const& name, bool const force) {
259  if (!_nameMap) {
260  _initRegistry();
261  }
262 
263  NameMap::iterator keyVal = _nameMap->find(name);
264 
265  if (keyVal == _nameMap->end()) {
266  AliasMap::iterator aliasKeyVal = _aliasMap->find(name);
267  if (aliasKeyVal != _aliasMap->end()) {
268  return _lookup(aliasKeyVal->second);
269  }
270 
271  if (force) {
272  return UNKNOWN;
273  } else {
274  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + name);
275  }
276  }
277 
278  return keyVal->second;
279 }
280 
281 std::string const& Filter::_lookup(int id) {
282  if (!_idMap) {
283  _initRegistry();
284  }
285 
286  IdMap::iterator keyVal = _idMap->find(id);
287 
288  if (keyVal == _idMap->end()) {
289  throw LSST_EXCEPT(pexEx::NotFoundError, (boost::format("Unable to find filter %d") % id).str());
290  }
291 
292  return keyVal->second;
293 }
295  //
296  // Map name to its ID and back to resolve aliases
297  //
298  int const id = _lookup(_name, true);
299  std::string const& name = (id == UNKNOWN) ? _name : _lookup(id);
300 
301  return FilterProperty::lookup(name);
302 }
303 } // namespace image
304 } // namespace afw
305 } // namespace lsst
double getAsDouble(std::string const &name) const
bool operator==(Filter const &rhs) const noexcept
Are two filters identical?
Definition: Filter.cc:171
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Filter.cc:173
std::string const & getName() const noexcept
Return a filter&#39;s name.
Definition: Filter.h:76
static int const UNKNOWN
Definition: Filter.h:144
int stripFilterKeywords(std::shared_ptr< lsst::daf::base::PropertySet > metadata)
Remove Filter-related keywords from the metadata.
Definition: Filter.cc:127
T end(T... args)
Rotation angle is unknown.
STL class.
static FilterProperty const & lookup(std::string const &name)
Lookup the properties of a filter "name".
Definition: Filter.cc:96
T push_back(T... args)
A base class for image defects.
int end
bool operator==(FilterProperty const &rhs) const noexcept
Return true iff two FilterProperties are identical.
Definition: Filter.cc:82
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:195
T erase(T... args)
Key< U > key
Definition: Schema.cc:281
T make_pair(T... args)
std::vector< std::string > getAliases() const
Return all aliases by which this filter is known.
Definition: Filter.cc:141
bool exists(std::string const &name) const
table::Key< int > id
Definition: Detector.cc:166
static int defineAlias(std::string const &oldName, std::string const &newName, bool force=false)
Define an alias for a filter.
Definition: Filter.cc:228
Holds an integer identifier for an LSST filter.
Definition: Filter.h:141
T insert(T... args)
T find(T... args)
#define LSST_EXCEPT(type,...)
T begin(T... args)
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:294
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
static std::vector< std::string > getNames()
Return a list of known filters.
Definition: Filter.cc:154
std::size_t hash_value() const noexcept
Return a hash of this object.
Definition: Filter.cc:86