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