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
utils.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division
2 #
3 # LSST Data Management System
4 # Copyright 2008, 2009, 2010 LSST Corporation.
5 #
6 # This product includes software developed by the
7 # LSST Project (http://www.lsst.org/).
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the LSST License Statement and
20 # the GNU General Public License along with this program. If not,
21 # see <http://www.lsstcorp.org/LegalNotices/>.
22 #
23 
24 import re
25 import lsst.pex.policy as pexPolicy
26 from lsst.afw.cameraGeom import TAN_PIXELS
27 import lsst.afw.detection as afwDetect
28 from . import imageLib as afwImage
29 
30 def clipImage(im, minClip, maxClip):
31  """Clip an image to lie between minClip and maxclip (None to ignore)"""
32 
33  if re.search("::MaskedImage<", im.__repr__()):
34  mi = im
35  else:
36  mi = afwImage.makeMaskedImage(im, afwImage.MaskU(im.getDimensions()))
37 
38  if minClip is not None:
39  ds = afwDetect.FootprintSet(mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False))
40  afwDetect.setImageFromFootprintList(mi.getImage(), ds.getFootprints(), minClip)
41 
42  if maxClip is not None:
44  afwDetect.setImageFromFootprintList(mi.getImage(), ds.getFootprints(), maxClip)
45 
46 def getDistortedWcs(exposureInfo, log=None):
47  """!Get a WCS from an exposureInfo, with distortion terms if possible
48 
49  If the WCS in the exposure is a pure TAN WCS and distortion information is available
50  in the exposure's Detector, then return a DistortedTanWcs that combines the
51  distortion information with the pure TAN WCS.
52  Otherwise return the WCS in the exposureInfo without modification.
53 
54  This function is intended as a temporary workaround until ISR puts a WCS with distortion information
55  into its exposures.
56 
57  @param[in] exposureInfo exposure information (an lsst.afw.image.ExposureInfo),
58  e.g. from exposure.getInfo()
59  @param[in] log an lsst.pex.logging.Log or None; if specified then a warning is logged if:
60  - the exposureInfo's WCS has no distortion and cannot be cast to a TanWcs
61  - the expousureInfo's detector has no TAN_PIXELS transform (distortion information)
62  @throw RuntimeError if exposureInfo has no WCS.
63  """
64  if not exposureInfo.hasWcs():
65  raise RuntimeError("exposure must have a WCS")
66  wcs = exposureInfo.getWcs()
67  if not wcs.hasDistortion() and exposureInfo.hasDetector():
68  # warn but continue if TAN_PIXELS not present or the initial WCS is not a TanWcs;
69  # other errors indicate a bug that should raise an exception
70  detector = exposureInfo.getDetector()
71  try:
72  pixelsToTanPixels = detector.getTransform(TAN_PIXELS)
73  tanWcs = afwImage.TanWcs.cast(wcs)
74  except Exception as e:
75  if log:
76  log.warn("Could not create a DistortedTanWcs: %s" % (e,))
77  else:
78  wcs = afwImage.DistortedTanWcs(tanWcs, pixelsToTanPixels)
79  return wcs
80 
82  """Reset registry of filters and filter properties"""
83  afwImage.Filter.reset()
84  afwImage.FilterProperty.reset()
85 
86 def defineFilter(name, lambdaEff, alias=[], force=False):
87  """Define a filter and its properties in the filter registry"""
88  prop = afwImage.FilterProperty(name, lambdaEff, force)
89  afwImage.Filter.define(prop)
90  if isinstance(alias, basestring):
91  afwImage.Filter.defineAlias(name, alias)
92  else:
93  for a in alias:
94  afwImage.Filter.defineAlias(name, a)
95 
96 def defineFiltersFromPolicy(filterPolicy, reset=False):
97  """Process a Policy and define the filters"""
98 
99  if reset:
100  afwImage.Filter.reset()
101  afwImage.FilterProperty.reset()
102  #
103  # Process the Policy and define the filters
104  #
105  policyFile = pexPolicy.DefaultPolicyFile("afw", "FilterDictionary.paf", "policy")
106  defPolicy = pexPolicy.Policy.createPolicy(policyFile, policyFile.getRepositoryPath(), True)
107 
108  filterPolicy.mergeDefaults(defPolicy.getDictionary())
109 
110  for p in filterPolicy.getArray("Filter"):
111  afwImage.Filter.define(afwImage.FilterProperty(p.get("name"), p))
112  if p.exists("alias"):
113  for a in p.getArray("alias"):
114  afwImage.Filter.defineAlias(p.get("name"), a)
115 
116 class CalibNoThrow(object):
117  """A class intended to be used with python's with statement, to return NaNs for negative fluxes
118  instead of raising exceptions (exceptions may be raised for other purposes).
119 
120 E.g.
121  with CalibNoThrow():
122  ax.plot([exposure.getCalib().getMagnitude(a) for a in candAmps], zGood[:,k], 'b+')
123  """
124  def __enter__(self):
125  self._throwOnNegative = afwImage.Calib.getThrowOnNegativeFlux()
126  afwImage.Calib.setThrowOnNegativeFlux(False)
127 
128  def __exit__(self, *args):
129  afwImage.Calib.setThrowOnNegativeFlux(self._throwOnNegative)
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > * makeMaskedImage(typename Image< ImagePixelT >::Ptr image, typename Mask< MaskPixelT >::Ptr mask=typename Mask< MaskPixelT >::Ptr(), typename Image< VariancePixelT >::Ptr variance=typename Image< VariancePixelT >::Ptr())
Definition: MaskedImage.h:1067
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:44
def getDistortedWcs
Get a WCS from an exposureInfo, with distortion terms if possible.
Definition: utils.py:46
def defineFiltersFromPolicy
Definition: utils.py:96
ImageT::Pixel setImageFromFootprintList(ImageT *image, std::vector< boost::shared_ptr< Footprint >> const &footprints, typename ImageT::Pixel const value)
Set all image pixels in a set of Footprints to a given value.
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
Combination of a TAN WCS and a distortion model.