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