LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
colorterms.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008, 2009, 2010, 2011 LSST Corporation.
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 import numpy as np
23 
24 class Colorterm(object):
25  """!A class to describe colour terms between photometric bands
26  """
27  _colorterms = {} # cached dictionary of dictionaries of Colorterms for devices
28  _activeColorterms = None # The set of Colorterms that are currently in use
29 
30  def __init__(self, primary, secondary, c0, c1=0.0, c2=0.0):
31  """!Construct a Colorterm
32 
33  The transformed magnitude p' is given by
34  p' = primary + c0 + c1*(primary - secondary) + c2*(primary - secondary)**2
35  """
36  self.primary = primary
37  self.secondary = secondary
38  self.c0 = c0
39  self.c1 = c1
40  self.c2 = c2
41 
42  def __str__(self):
43  return "%s %s [%g %g %g]" % (self.primary, self.secondary, self.c0, self.c1, self.c2)
44 
45  @staticmethod
46  def setColorterms(colorterms, device=None):
47  """!Replace or update the cached Colorterms dict
48 
49  @param[in,out] colorterms a dict of device: Colorterm
50  @param[in] device device name, or None;
51  if device is None then colorterms replaces the internal catched dict,
52  else the internal cached dict is updated with device: colorterms[device]
53  """
54  if device:
55  Colorterm._colorterms[device] = colorterms[device]
56  else:
57  Colorterm._colorterms = colorterms
58 
59  Colorterm.setActiveDevice(device, allowRaise=False)
60 
61  @staticmethod
62  def setActiveDevice(device, allowRaise=True):
63  """!Set or clear the default colour terms
64 
65  @param[in] device device name, or None to clear
66  @param[in] allowRaise controls handling an unknown, non-None device:
67  if true raise RuntimeError, else clear the default colorterms
68  """
69  if device is None:
70  Colorterm._activeColorterms = None
71 
72  else:
73  try:
74  Colorterm._activeColorterms = Colorterm._colorterms[device]
75  except KeyError:
76  if allowRaise:
77  raise RuntimeError("No colour terms are available for %s" % device)
78 
79  Colorterm._activeColorterms = None
80 
81  @staticmethod
82  def getColorterm(band):
83  """!Return the Colorterm for the specified band (or None if unknown)
84  """
85  return Colorterm._activeColorterms.get(band) if Colorterm._activeColorterms else None
86 
87  @staticmethod
88  def transformSource(band, source, reverse=False, colorterms=None):
89  """!Transform the magnitudes in <source> to the specified <band> and return it.
90 
91  The <source> must support a get(band) (e.g. source.get("r")) method, as do afw::Source and dicts.
92  Use the colorterms (or the cached set if colorterms is None); if no set is available,
93  return the <band> flux.
94  If reverse is True, return the inverse transformed magnitude
95 
96  @warning reverse is not yet implemented
97  """
98  if not colorterms:
99  colorterms = Colorterm._activeColorterms
100 
101  if not colorterms:
102  return source.get(band)
103 
104  ct = colorterms[band]
105 
106  return Colorterm.transformMags(band, source.get(ct.primary), source.get(ct.secondary),
107  reverse, colorterms)
108 
109  @staticmethod
110  def transformMags(band, primary, secondary, reverse=False, colorterms=None):
111  """!Transform the magnitudes <primary> and <secondary> to the specified <band> and return it.
112 
113  Use the colorterms (or the cached set if colorterms is None); if no set is available,
114  return the <band> flux
115  If reverse is True, return the inverse transformed magnitude
116 
117  @warning reverse is not yet implemented
118  """
119  if not colorterms:
120  colorterms = Colorterm._activeColorterms
121 
122  if not colorterms:
123  return primary
124 
125  ct = colorterms[band]
126 
127  p = primary
128  s = secondary
129 
130  if reverse:
131  raise NotImplemented("reverse photometric transformations are not implemented")
132  else:
133  return p + ct.c0 + (p - s)*(ct.c1 + (p - s)*ct.c2)
134 
135  @staticmethod
136  def propagateFluxErrors(band, primaryFluxErr, secondaryFluxErr, reverse=False, colorterms=None):
137  """!Transform flux errors
138  """
139  if not colorterms:
140  colorterms = Colorterm._activeColorterms
141 
142  if not colorterms:
143  return primaryFluxErr
144 
145  ct = colorterms[band]
146 
147  return np.hypot((1 + ct.c1)*primaryFluxErr, ct.c1*secondaryFluxErr)
148 
def setColorterms
Replace or update the cached Colorterms dict.
Definition: colorterms.py:46
def transformSource
Transform the magnitudes in &lt;source&gt; to the specified &lt;band&gt; and return it.
Definition: colorterms.py:88
def setActiveDevice
Set or clear the default colour terms.
Definition: colorterms.py:62
def getColorterm
Return the Colorterm for the specified band (or None if unknown)
Definition: colorterms.py:82
A class to describe colour terms between photometric bands.
Definition: colorterms.py:24
def __init__
Construct a Colorterm.
Definition: colorterms.py:30