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