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