LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
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 //
30 //
31 //##====---------------- ----------------====##/
32 
33 #include "boost/format.hpp"
34 #include "boost/algorithm/string/trim.hpp"
35 #include "lsst/pex/exceptions.h"
36 
37 #include "lsst/afw/image/Filter.h"
38 
39 namespace pexEx = lsst::pex::exceptions;
40 
41 namespace lsst { namespace afw { namespace image {
42 
44 
46  std::string const& name,
47  lsst::daf::base::PropertySet const& prop,
48  bool force
49  ) : _name(name), _lambdaEff(-1)
50 {
51  if (prop.exists("lambdaEff")) {
52  _lambdaEff = prop.getAsDouble("lambdaEff");
53  }
54  _insert(force);
55 }
56 
57 
62  lsst::pex::policy::Policy const& pol,
63  bool force
64  ) : _name(name), _lambdaEff(-1)
65 {
66  if (pol.exists("lambdaEff")) {
67  _lambdaEff = pol.getDouble("lambdaEff");
68  }
69  _insert(force);
70 }
71 
76  bool force
77  )
78 {
79  if (!_propertyMap) {
80  _initRegistry();
81  }
82 
83  PropertyMap::iterator keyVal = _propertyMap->find(getName());
84 
85  if (keyVal != _propertyMap->end()) {
86  if (keyVal->second == *this) {
87  return; // OK, a redefinition with identical values
88  }
89 
90  if (!force) {
91  throw LSST_EXCEPT(pexEx::RuntimeError, "Filter " + getName() + " is already defined");
92  }
93  _propertyMap->erase(keyVal);
94  }
95 
96  _propertyMap->insert(std::make_pair(getName(), *this));
97 }
98 
103  ) const
104 {
105  return (_lambdaEff == rhs._lambdaEff);
106 }
107 
108 
113 {
114  if (_propertyMap) {
115  delete _propertyMap;
116  }
117 
119 }
120 
124 FilterProperty const& FilterProperty::lookup(std::string const& name
125  )
126 {
127  if (!_propertyMap) {
128  _initRegistry();
129  }
130 
131  PropertyMap::iterator keyVal = _propertyMap->find(name);
132 
133  if (keyVal == _propertyMap->end()) {
134  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + name);
135  }
136 
137  return keyVal->second;
138 }
139 
140 /************************************************************************************************************/
141 
142 namespace {
143  std::string const unknownFilter = "_unknown_";
144 }
145 
150  bool const force
151  )
152 {
153  std::string const key = "FILTER";
154  if( metadata->exists(key) ) {
155  std::string filterName = boost::algorithm::trim_right_copy(metadata->getAsString(key));
156  _id = _lookup(filterName, force);
157  _name = filterName;
158  }
159 }
160 
161 namespace detail {
168  )
169 {
170  int nstripped = 0;
171 
172  std::string key = "FILTER";
173  if (metadata->exists(key)) {
174  metadata->remove(key);
175  nstripped++;
176  }
177 
178  return nstripped;
179 }
180 }
184 std::vector<std::string> Filter::getNames()
185 {
186  if (!_nameMap) {
187  _initRegistry();
188  }
189 
190  std::vector<std::string> names;
191 
192  for (NameMap::const_iterator ptr = _nameMap->begin(), end = _nameMap->end(); ptr != end; ++ptr) {
193  if (ptr->first != unknownFilter) {
194  names.push_back(ptr->first);
195  }
196  }
197  std::sort(names.begin(), names.end());
198 
199  return names;
200 }
201 
205 bool Filter::operator==(Filter const& rhs) const {
206  return _id != UNKNOWN && _id == rhs._id;
207 }
208 
209 /************************************************************************************************************/
214 {
215  _id0 = UNKNOWN;
216  delete _aliasMap;
217  delete _nameMap;
218  delete _idMap;
219 
220  _aliasMap = new AliasMap;
221  _nameMap = new NameMap;
222  _idMap = new IdMap;
223 
224  define(FilterProperty(unknownFilter, lsst::pex::policy::Policy(), true));
225 }
226 
227 /************************************************************************************************************/
228 
230 
231 Filter::AliasMap *Filter::_aliasMap = NULL; // dynamically allocated as that avoids an intel bug with static
232  // variables in dynamic libraries
233 Filter::NameMap *Filter::_nameMap = NULL; // dynamically allocated as that avoids an intel bug with static
234  // variables in dynamic libraries
235 Filter::IdMap *Filter::_idMap = NULL; // dynamically allocated as that avoids an intel bug with static
236  // variables in dynamic libraries
237 
245 int Filter::define(FilterProperty const& fp, int id, bool force)
246 {
247  if (!_nameMap) {
248  _initRegistry();
249  }
250 
251  std::string const& name = fp.getName();
252  NameMap::iterator keyVal = _nameMap->find(name);
253 
254  if (keyVal != _nameMap->end()) {
255  int oid = keyVal->second;
256 
257  if (id == oid || id == AUTO) {
258  return oid; // OK, same value as before
259  }
260 
261  if (!force) {
262  throw LSST_EXCEPT(pexEx::RuntimeError, "Filter " + name + " is already defined");
263  }
264  _nameMap->erase(keyVal);
265  _idMap->erase(oid);
266  }
267 
268  if (id == AUTO) {
269  id = _id0;
270  ++_id0;
271  }
272 
273  _nameMap->insert(std::make_pair(name, id));
274  _idMap->insert(std::make_pair(id, name));
275 
276  return id;
277 }
278 
282 int Filter::defineAlias(std::string const& oldName,
283  std::string const& newName,
284  bool force
285  )
286 {
287  if (!_nameMap) {
288  _initRegistry();
289  }
290 
291  // Lookup oldName
292  NameMap::iterator keyVal = _nameMap->find(oldName);
293  if (keyVal == _nameMap->end()) {
294  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + oldName);
295  }
296  int const id = keyVal->second;
297 
298  // Lookup oldName in aliasMap
299  AliasMap::iterator aliasKeyVal = _aliasMap->find(newName);
300  if (aliasKeyVal != _aliasMap->end()) {
301  if (aliasKeyVal->second == oldName) {
302  return id; // OK, same value as before
303  }
304 
305  if (!force) {
306  throw LSST_EXCEPT(pexEx::NotFoundError, "Filter " + newName + " is already defined");
307  }
308  _aliasMap->erase(aliasKeyVal);
309  }
310 
311  _aliasMap->insert(std::make_pair(newName, oldName));
312 
313  return id;
314 }
315 
319 int Filter::_lookup(std::string const& name, // Name of filter
320  bool const force // return an invalid ID, but don't throw, if name is unknown
321  )
322 {
323  if (!_nameMap) {
324  _initRegistry();
325  }
326 
327  NameMap::iterator keyVal = _nameMap->find(name);
328 
329  if (keyVal == _nameMap->end()) {
330  AliasMap::iterator aliasKeyVal = _aliasMap->find(name);
331  if (aliasKeyVal != _aliasMap->end()) {
332  return _lookup(aliasKeyVal->second);
333  }
334 
335  if (force) {
336  return UNKNOWN;
337  } else {
338  throw LSST_EXCEPT(pexEx::NotFoundError, "Unable to find filter " + name);
339  }
340  }
341 
342  return keyVal->second;
343 }
344 
348 std::string const& Filter::_lookup(int id)
349 {
350  if (!_idMap) {
351  _initRegistry();
352  }
353 
354  IdMap::iterator keyVal = _idMap->find(id);
355 
356  if (keyVal == _idMap->end()) {
357  throw LSST_EXCEPT(pexEx::NotFoundError, (boost::format("Unable to find filter %d") % id).str());
358  }
359 
360  return keyVal->second;
361 }
366  //
367  // Map name to its ID and back to resolve aliases
368  //
369  int const id = _lookup(_name, true);
370  std::string const& name = (id == UNKNOWN) ? _name : _lookup(id);
371 
372  return FilterProperty::lookup(name);
373 }
374 
375 }}}
std::tr1::unordered_map< std::string const, std::string const > AliasMap
Definition: Filter.h:158
bool exists(const std::string &name) const
Definition: Policy.h:944
table::Key< std::string > name
Definition: ApCorrMap.cc:71
bool operator==(Filter const &rhs) const
Definition: Filter.cc:205
virtual void remove(std::string const &name)
Definition: PropertySet.cc:754
std::string const & _name
Definition: Mask.cc:678
std::tr1::unordered_map< std::string const, FilterProperty > PropertyMap
Definition: Filter.h:92
static NameMap * _nameMap
Definition: Filter.h:172
Include files required for standard LSST Exception handling.
void _insert(bool force=false)
Definition: Filter.cc:75
static void _initRegistry()
Definition: Filter.cc:213
a container for holding hierarchical configuration data in memory.
Definition: Policy.h:169
static int _lookup(std::string const &name, bool const force=false)
Definition: Filter.cc:319
#define PTR(...)
Definition: base.h:41
static PropertyMap * _propertyMap
Definition: Filter.h:100
FilterProperty const & getFilterProperty() const
Definition: Filter.cc:365
table::Key< table::Array< Kernel::Pixel > > image
Definition: FixedKernel.cc:117
std::tr1::unordered_map< unsigned int const, std::string const > IdMap
Definition: Filter.h:160
static int define(FilterProperty const &filterProperty, int id=AUTO, bool force=false)
Definition: Filter.cc:245
static FilterProperty const & lookup(std::string const &name)
Definition: Filter.cc:124
static int defineAlias(std::string const &oldName, std::string const &newName, bool force=false)
Definition: Filter.cc:282
Holds an integer identifier for an LSST filter.
Definition: Filter.h:107
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
FilterProperty(std::string const &name, double lambdaEff, bool force=false)
Definition: Filter.h:60
double getDouble(const std::string &name) const
Definition: Policy.h:617
double getAsDouble(std::string const &name) const
Definition: PropertySet.cc:406
Class for storing generic metadata.
Definition: PropertySet.h:82
int id
Definition: CR.cc:151
std::string getAsString(std::string const &name) const
Definition: PropertySet.cc:444
#define CONST_PTR(...)
Definition: base.h:47
static IdMap * _idMap
Definition: Filter.h:171
static AliasMap * _aliasMap
Definition: Filter.h:170
int stripFilterKeywords(boost::shared_ptr< lsst::daf::base::PropertySet > metadata)
Definition: Filter.cc:167
std::string _name
Definition: Filter.h:167
Filter(std::string const &name, bool const force=false)
Definition: Filter.h:114
bool operator==(FilterProperty const &rhs) const
Definition: Filter.cc:102
std::tr1::unordered_map< std::string const, unsigned int const > NameMap
Definition: Filter.h:159
std::string const & getName() const
Definition: Filter.h:71
bool exists(std::string const &name) const
Definition: PropertySet.cc:190
static std::vector< std::string > getNames()
Definition: Filter.cc:184
Class encapsulating an identifier for an LSST filter.