lsst.afw  14.0-49-g535656444+1
utils.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2017 LSST/AURA.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 from __future__ import absolute_import, division, print_function
23 
24 __all__ = ["clipImage", "resetFilters", "defineFilter",
25  "defineFiltersFromPolicy", "CalibNoThrow"]
26 
27 from past.builtins import basestring
28 from builtins import object
29 
30 import lsst.pex.policy as pexPolicy
31 import lsst.afw.detection as afwDetect
32 from .maskedImage import MaskedImage, makeMaskedImage
33 from .mask import Mask
34 from .filter import Filter, FilterProperty
35 from .calib import Calib
36 
37 
38 def clipImage(im, minClip, maxClip):
39  """Clip an image to lie between minClip and maxclip (None to ignore)"""
40  if isinstance(im, MaskedImage):
41  mi = im
42  else:
43  mi = makeMaskedImage(im, Mask(im.getDimensions()))
44 
45  if minClip is not None:
46  ds = afwDetect.FootprintSet(
47  mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False))
48  afwDetect.setImageFromFootprintList(
49  mi.getImage(), ds.getFootprints(), minClip)
50 
51  if maxClip is not None:
52  ds = afwDetect.FootprintSet(mi, afwDetect.Threshold(maxClip))
53  afwDetect.setImageFromFootprintList(
54  mi.getImage(), ds.getFootprints(), maxClip)
55 
56 
58  """Reset registry of filters and filter properties"""
59  Filter.reset()
60  FilterProperty.reset()
61 
62 
63 def defineFilter(name, lambdaEff, alias=[], force=False):
64  """Define a filter and its properties in the filter registry"""
65  prop = FilterProperty(name, lambdaEff, force)
66  Filter.define(prop)
67  if isinstance(alias, basestring):
68  Filter.defineAlias(name, alias)
69  else:
70  for a in alias:
71  Filter.defineAlias(name, a)
72 
73 
74 def defineFiltersFromPolicy(filterPolicy, reset=False):
75  """Process a Policy and define the filters"""
76 
77  if reset:
78  Filter.reset()
79  FilterProperty.reset()
80  #
81  # Process the Policy and define the filters
82  #
83  policyFile = pexPolicy.DefaultPolicyFile(
84  "afw", "FilterDictionary.paf", "policy")
85  defPolicy = pexPolicy.Policy.createPolicy(
86  policyFile, policyFile.getRepositoryPath(), True)
87 
88  filterPolicy.mergeDefaults(defPolicy.getDictionary())
89 
90  for p in filterPolicy.getArray("Filter"):
91  Filter.define(FilterProperty(p.get("name"), p))
92  if p.exists("alias"):
93  for a in p.getArray("alias"):
94  Filter.defineAlias(p.get("name"), a)
95 
96 
97 class CalibNoThrow(object):
98  """A class intended to be used with python's with statement, to return NaNs for negative fluxes
99  instead of raising exceptions (exceptions may be raised for other purposes).
100 
101 E.g.
102  with CalibNoThrow():
103  ax.plot([exposure.getCalib().getMagnitude(a) for a in candAmps], zGood[:,k], 'b+')
104  """
105 
106  def __enter__(self):
107  self._throwOnNegative = Calib.getThrowOnNegativeFlux()
108  Calib.setThrowOnNegativeFlux(False)
109 
110  def __exit__(self, *args):
111  Calib.setThrowOnNegativeFlux(self._throwOnNegative)
def defineFiltersFromPolicy(filterPolicy, reset=False)
Definition: utils.py:74
def defineFilter(name, lambdaEff, alias=[], force=False)
Definition: utils.py:63
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > * makeMaskedImage(typename std::shared_ptr< Image< ImagePixelT >> image, typename std::shared_ptr< Mask< MaskPixelT >> mask=Mask< MaskPixelT >(), typename std::shared_ptr< Image< VariancePixelT >> variance=Image< VariancePixelT >())
A function to return a MaskedImage of the correct type (cf.
Definition: MaskedImage.h:1268
def clipImage(im, minClip, maxClip)
Definition: utils.py:38