LSSTApplications  11.0-24-g0a022a1,14.0+64,15.0,15.0+1,15.0-1-g14e9bfd,15.0-1-g1eca518,15.0-1-g499c38d,15.0-1-g60afb23,15.0-1-g6668b0b,15.0-1-g788a293,15.0-1-g82223af,15.0-1-ga91101e,15.0-1-gae1598d,15.0-1-gc45031d,15.0-1-gd076f1f,15.0-1-gf4f1c34,15.0-1-gfe1617d,15.0-16-g953e39cab,15.0-2-g2010ef9,15.0-2-g33d94b3,15.0-2-g5218728,15.0-2-g947dc0d,15.0-3-g9103c06,15.0-3-ga03b4ca,15.0-3-ga659d1f3,15.0-3-ga695220+2,15.0-3-gaec6799,15.0-3-gb7a597c,15.0-3-gd5b9ff95,15.0-4-g0478fed+2,15.0-4-g45f767a,15.0-4-gff20472+2,15.0-6-ge2d9597
LSSTDataManagementBasePackage
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 import numpy as np
30 
31 import lsst.pex.policy as pexPolicy
32 import lsst.afw.detection as afwDetect
33 from .maskedImage import MaskedImage, makeMaskedImage
34 from .mask import Mask
35 from .filter import Filter, FilterProperty
36 from .calib import Calib
37 
38 
39 def clipImage(im, minClip, maxClip):
40  """Clip an image to lie between minClip and maxclip (None to ignore)"""
41  if isinstance(im, MaskedImage):
42  mi = im
43  else:
44  mi = makeMaskedImage(im, Mask(im.getDimensions()))
45 
46  if minClip is not None:
48  mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False))
49  afwDetect.setImageFromFootprintList(
50  mi.getImage(), ds.getFootprints(), minClip)
51 
52  if maxClip is not None:
54  afwDetect.setImageFromFootprintList(
55  mi.getImage(), ds.getFootprints(), maxClip)
56 
57 
59  """Reset registry of filters and filter properties"""
60  Filter.reset()
61  FilterProperty.reset()
62 
63 
64 def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False):
65  """Define a filter and its properties in the filter registry"""
66  prop = FilterProperty(name, lambdaEff, lambdaMin, lambdaMax, force)
67  Filter.define(prop)
68  if isinstance(alias, basestring):
69  Filter.defineAlias(name, alias)
70  else:
71  for a in alias:
72  Filter.defineAlias(name, a)
73 
74 
75 def defineFiltersFromPolicy(filterPolicy, reset=False):
76  """Process a Policy and define the filters"""
77 
78  if reset:
79  Filter.reset()
80  FilterProperty.reset()
81  #
82  # Process the Policy and define the filters
83  #
84  policyFile = pexPolicy.DefaultPolicyFile(
85  "afw", "FilterDictionary.paf", "policy")
86  defPolicy = pexPolicy.Policy.createPolicy(
87  policyFile, policyFile.getRepositoryPath(), True)
88 
89  filterPolicy.mergeDefaults(defPolicy.getDictionary())
90 
91  for p in filterPolicy.getArray("Filter"):
92  Filter.define(FilterProperty(p.get("name"), p))
93  if p.exists("alias"):
94  for a in p.getArray("alias"):
95  Filter.defineAlias(p.get("name"), a)
96 
97 
99  """A class intended to be used with python's with statement, to return NaNs for negative fluxes
100  instead of raising exceptions (exceptions may be raised for other purposes).
101 
102 E.g.
103  with CalibNoThrow():
104  ax.plot([exposure.getCalib().getMagnitude(a) for a in candAmps], zGood[:,k], 'b+')
105  """
106 
107  def __enter__(self):
108  self._throwOnNegative = Calib.getThrowOnNegativeFlux()
109  Calib.setThrowOnNegativeFlux(False)
110 
111  def __exit__(self, *args):
112  Calib.setThrowOnNegativeFlux(self._throwOnNegative)
def defineFiltersFromPolicy(filterPolicy, reset=False)
Definition: utils.py:75
a representation of a default Policy file that is stored as a file in the installation directory of a...
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:43
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 defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False)
Definition: utils.py:64
def clipImage(im, minClip, maxClip)
Definition: utils.py:39
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53