LSSTApplications  11.0-22-g33de520,13.0+4,13.0+72,13.0-1-g46ffeb4+33,13.0-1-g47a359c+13,13.0-10-gbb93d41+29,13.0-12-g0251d74+22,13.0-12-gaf0c0ec+7,13.0-13-gd4b2922+21,13.0-14-g9415442+37,13.0-18-gc4ad422+6,13.0-2-g167564e+9,13.0-2-g50559bf,13.0-22-g3839dbb+22,13.0-26-g0f127ff+4,13.0-3-g3542790+8,13.0-3-g520d906+1,13.0-31-g48013df,13.0-4-g4231ded+8,13.0-42-g52e9227+2,13.0-5-g2a40766+1,13.0-52-g022e0bf+6,13.0-6-g08b5043,13.0-6-geef1ef2+6,13.0-8-gb7ca535,13.0-94-ga1c4440,master-gada5ecbbff+5,master-gf6b1fd7af3+2
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", "getDistortedWcs", "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 from lsst.afw.cameraGeom import TAN_PIXELS
32 import lsst.afw.detection as afwDetect
33 from .maskedImage import MaskedImage, makeMaskedImage
34 from .mask import Mask
35 from .tanWcs import TanWcs
36 from .distortedTanWcs import DistortedTanWcs
37 from .filter import Filter, FilterProperty
38 from .calib import Calib
39 
40 
41 def clipImage(im, minClip, maxClip):
42  """Clip an image to lie between minClip and maxclip (None to ignore)"""
43  if isinstance(im, MaskedImage):
44  mi = im
45  else:
46  mi = makeMaskedImage(im, Mask(im.getDimensions()))
47 
48  if minClip is not None:
50  mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False))
51  afwDetect.setImageFromFootprintList(
52  mi.getImage(), ds.getFootprints(), minClip)
53 
54  if maxClip is not None:
56  afwDetect.setImageFromFootprintList(
57  mi.getImage(), ds.getFootprints(), maxClip)
58 
59 
60 def getDistortedWcs(exposureInfo, log=None):
61  """!Get a WCS from an exposureInfo, with distortion terms if possible
62 
63  If the WCS in the exposure is a pure TAN WCS and distortion information is available
64  in the exposure's Detector, then return a DistortedTanWcs that combines the
65  distortion information with the pure TAN WCS.
66  Otherwise return the WCS in the exposureInfo without modification.
67 
68  This function is intended as a temporary workaround until ISR puts a WCS with distortion information
69  into its exposures.
70 
71  @param[in] exposureInfo exposure information (an lsst.afw.image.ExposureInfo),
72  e.g. from exposure.getInfo()
73  @param[in] log an lsst.log.Log or None; if specified then a warning is logged if:
74  - the exposureInfo's WCS has no distortion and cannot be cast to a TanWcs
75  - the expousureInfo's detector has no TAN_PIXELS transform (distortion information)
76  @throw RuntimeError if exposureInfo has no WCS.
77  """
78  if not exposureInfo.hasWcs():
79  raise RuntimeError("exposure must have a WCS")
80  wcs = exposureInfo.getWcs()
81  if not wcs.hasDistortion() and exposureInfo.hasDetector():
82  # warn and return original Wcs the initial WCS is not a TanWcs or TAN_PIXELS not present;
83  # other errors indicate a bug that should raise an exception
84  if not isinstance(wcs, TanWcs):
85  if log:
86  log.warn("Could not create a DistortedTanWcs:"
87  "exposure's Wcs is a %r isntead of a TanWcs" % (wcs,))
88  return wcs
89 
90  detector = exposureInfo.getDetector()
91  if not detector.hasTransform(TAN_PIXELS):
92  if log:
93  log.warn(
94  "Could not create a DistortedTanWcs: exposure has no Detector")
95  return wcs
96 
97  pixelsToTanPixels = detector.getTransform(TAN_PIXELS)
98  return DistortedTanWcs(wcs, pixelsToTanPixels)
99  return wcs
100 
101 
103  """Reset registry of filters and filter properties"""
104  Filter.reset()
105  FilterProperty.reset()
106 
107 
108 def defineFilter(name, lambdaEff, alias=[], force=False):
109  """Define a filter and its properties in the filter registry"""
110  prop = FilterProperty(name, lambdaEff, force)
111  Filter.define(prop)
112  if isinstance(alias, basestring):
113  Filter.defineAlias(name, alias)
114  else:
115  for a in alias:
116  Filter.defineAlias(name, a)
117 
118 
119 def defineFiltersFromPolicy(filterPolicy, reset=False):
120  """Process a Policy and define the filters"""
121 
122  if reset:
123  Filter.reset()
124  FilterProperty.reset()
125  #
126  # Process the Policy and define the filters
127  #
128  policyFile = pexPolicy.DefaultPolicyFile(
129  "afw", "FilterDictionary.paf", "policy")
130  defPolicy = pexPolicy.Policy.createPolicy(
131  policyFile, policyFile.getRepositoryPath(), True)
132 
133  filterPolicy.mergeDefaults(defPolicy.getDictionary())
134 
135  for p in filterPolicy.getArray("Filter"):
136  Filter.define(FilterProperty(p.get("name"), p))
137  if p.exists("alias"):
138  for a in p.getArray("alias"):
139  Filter.defineAlias(p.get("name"), a)
140 
141 
142 class CalibNoThrow(object):
143  """A class intended to be used with python's with statement, to return NaNs for negative fluxes
144  instead of raising exceptions (exceptions may be raised for other purposes).
145 
146 E.g.
147  with CalibNoThrow():
148  ax.plot([exposure.getCalib().getMagnitude(a) for a in candAmps], zGood[:,k], 'b+')
149  """
150 
151  def __enter__(self):
152  self._throwOnNegative = Calib.getThrowOnNegativeFlux()
153  Calib.setThrowOnNegativeFlux(False)
154 
155  def __exit__(self, *args):
156  Calib.setThrowOnNegativeFlux(self._throwOnNegative)
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:1186
def getDistortedWcs
Get a WCS from an exposureInfo, with distortion terms if possible.
Definition: utils.py:60
def defineFiltersFromPolicy
Definition: utils.py:119
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
Describe the properties of a Filter (e.g.
Definition: Filter.h:55