LSSTApplications  20.0.0
LSSTDataManagementBasePackage
Classes | Functions
lsst.obs.base.utils Namespace Reference

Classes

class  InitialSkyWcsError
 

Functions

def createInitialSkyWcs (visitInfo, detector, flipX=False)
 
def bboxFromIraf (irafBBoxStr)
 
def getInstrument (instrumentName, registry=None)
 
def setDottedAttr (item, name, value)
 
def setDottedAttrs (item, attrs)
 

Function Documentation

◆ bboxFromIraf()

def lsst.obs.base.utils.bboxFromIraf (   irafBBoxStr)
Return a Box2I corresponding to an IRAF-style BBOX

[x0:x1,y0:y1] where x0 and x1 are the one-indexed start and end columns, and correspondingly
y0 and y1 are the start and end rows.

Definition at line 85 of file utils.py.

85 def bboxFromIraf(irafBBoxStr):
86  """Return a Box2I corresponding to an IRAF-style BBOX
87 
88  [x0:x1,y0:y1] where x0 and x1 are the one-indexed start and end columns, and correspondingly
89  y0 and y1 are the start and end rows.
90  """
91 
92  mat = re.search(r"^\[([-\d]+):([-\d]+),([-\d]+):([-\d]+)\]$", irafBBoxStr)
93  if not mat:
94  raise RuntimeError("Unable to parse IRAF-style bbox \"%s\"" % irafBBoxStr)
95  x0, x1, y0, y1 = [int(_) for _ in mat.groups()]
96 
97  return geom.BoxI(geom.PointI(x0 - 1, y0 - 1), geom.PointI(x1 - 1, y1 - 1))
98 
99 

◆ createInitialSkyWcs()

def lsst.obs.base.utils.createInitialSkyWcs (   visitInfo,
  detector,
  flipX = False 
)
Create a SkyWcs from the telescope boresight and detector geometry.

A typical usecase for this is to create the initial WCS for a newly-read
raw exposure.


Parameters
----------
visitInfo : `lsst.afw.image.VisitInfo`
    Where to get the telescope boresight and rotator angle from.
detector : `lsst.afw.cameraGeom.Detector`
    Where to get the camera geomtry from.
flipX : `bool`, optional
    If False, +X is along W, if True +X is along E.

Returns
-------
skyWcs : `lsst.afw.geom.SkyWcs`
    The new composed WCS.

Raises
------
InitialSkyWcsError
    Raised if there is an error generating the SkyWcs, chained from the
    lower-level exception if available.

Definition at line 44 of file utils.py.

44 def createInitialSkyWcs(visitInfo, detector, flipX=False):
45  """Create a SkyWcs from the telescope boresight and detector geometry.
46 
47  A typical usecase for this is to create the initial WCS for a newly-read
48  raw exposure.
49 
50 
51  Parameters
52  ----------
53  visitInfo : `lsst.afw.image.VisitInfo`
54  Where to get the telescope boresight and rotator angle from.
55  detector : `lsst.afw.cameraGeom.Detector`
56  Where to get the camera geomtry from.
57  flipX : `bool`, optional
58  If False, +X is along W, if True +X is along E.
59 
60  Returns
61  -------
62  skyWcs : `lsst.afw.geom.SkyWcs`
63  The new composed WCS.
64 
65  Raises
66  ------
67  InitialSkyWcsError
68  Raised if there is an error generating the SkyWcs, chained from the
69  lower-level exception if available.
70  """
71  if visitInfo.getRotType() != RotType.SKY:
72  msg = (f"Cannot create SkyWcs from camera geometry: rotator angle defined using "
73  f"RotType={visitInfo.getRotType()} instead of SKY.")
74  raise InitialSkyWcsError(msg)
75  orientation = visitInfo.getBoresightRotAngle()
76  boresight = visitInfo.getBoresightRaDec()
77  try:
78  pixelsToFieldAngle = detector.getTransform(detector.makeCameraSys(PIXELS),
79  detector.makeCameraSys(FIELD_ANGLE))
81  raise InitialSkyWcsError("Cannot compute PIXELS to FIELD_ANGLE Transform.") from e
82  return makeSkyWcs(pixelsToFieldAngle, orientation, flipX, boresight)
83 
84 

◆ getInstrument()

def lsst.obs.base.utils.getInstrument (   instrumentName,
  registry = None 
)
Return an instance of a named instrument.

If the instrument name not is qualified (does not contain a '.') and a
butler registry is provided, this will attempt to load the instrument using
Instrument.fromName. Otherwise the instrument will be imported and
instantiated.

Parameters
----------
instrumentName : string
    The name or fully-qualified class name of an instrument.
registry : `lsst.daf.butler.Registry`, optional
    Butler registry to query to find information about the instrument, by
    default None

Returns
-------
Instrument subclass instance
    The instantiated instrument.

Raises
------
RuntimeError
    If the instrument can not be imported, instantiated, or obtained from
    the registry.
TypeError
    If the instrument is not a subclass of lsst.obs.base.Instrument.

Definition at line 100 of file utils.py.

100 def getInstrument(instrumentName, registry=None):
101  """Return an instance of a named instrument.
102 
103  If the instrument name not is qualified (does not contain a '.') and a
104  butler registry is provided, this will attempt to load the instrument using
105  Instrument.fromName. Otherwise the instrument will be imported and
106  instantiated.
107 
108  Parameters
109  ----------
110  instrumentName : string
111  The name or fully-qualified class name of an instrument.
112  registry : `lsst.daf.butler.Registry`, optional
113  Butler registry to query to find information about the instrument, by
114  default None
115 
116  Returns
117  -------
118  Instrument subclass instance
119  The instantiated instrument.
120 
121  Raises
122  ------
123  RuntimeError
124  If the instrument can not be imported, instantiated, or obtained from
125  the registry.
126  TypeError
127  If the instrument is not a subclass of lsst.obs.base.Instrument.
128  """
129  if "." not in instrumentName and registry is not None:
130  try:
131  instr = Instrument.fromName(instrumentName, registry)
132  except Exception as err:
133  raise RuntimeError(
134  f"Could not get instrument from name: {instrumentName}. Failed with exception: {err}")
135  else:
136  try:
137  instr = doImport(instrumentName)
138  except Exception as err:
139  raise RuntimeError(f"Could not import instrument: {instrumentName}. Failed with exception: {err}")
140  instr = instr()
141  if not isinstance(instr, Instrument):
142  raise TypeError(f"{instrumentName} is not an Instrument subclass.")
143  return instr
144 
145 
146 # TODO remove the impl in pipe_base? (NB this combines setDottedAtr AND the
147 # handling in ConfigValueAction.__call__)

◆ setDottedAttr()

def lsst.obs.base.utils.setDottedAttr (   item,
  name,
  value 
)
Set an instance attribute (like `setattr` but accepting hierarchical
names such as ``foo.bar.baz``) If the attribute can not be set as a string,
will attempt to set the attribute with the result of eval'ing the value.

Parameters
----------
item : obj
    Object whose attribute is to be set.
name : `str`
    Name of attribute to set.
value : obj
    New value for the attribute.

Notes
-----
For example if name is ``foo.bar.baz`` then ``item.foo.bar.baz``
is set to the specified value.

Raises
------
AttributeError
    If the item does not have a field specified by name that can be set.
RuntimeError
    If the value can not be set as a string or rendered by eval, or if
    there is an error setting the attribute with the rendered value.

Definition at line 148 of file utils.py.

148 def setDottedAttr(item, name, value):
149  """Set an instance attribute (like `setattr` but accepting hierarchical
150  names such as ``foo.bar.baz``) If the attribute can not be set as a string,
151  will attempt to set the attribute with the result of eval'ing the value.
152 
153  Parameters
154  ----------
155  item : obj
156  Object whose attribute is to be set.
157  name : `str`
158  Name of attribute to set.
159  value : obj
160  New value for the attribute.
161 
162  Notes
163  -----
164  For example if name is ``foo.bar.baz`` then ``item.foo.bar.baz``
165  is set to the specified value.
166 
167  Raises
168  ------
169  AttributeError
170  If the item does not have a field specified by name that can be set.
171  RuntimeError
172  If the value can not be set as a string or rendered by eval, or if
173  there is an error setting the attribute with the rendered value.
174  """
175  subitem = item
176  subnameList = name.split(".")
177  for subname in subnameList[:-1]:
178  subitem = getattr(subitem, subname)
179  try:
180  setattr(subitem, subnameList[-1], value)
181  except AttributeError:
182  raise AttributeError(f"No field: {name!r}")
183  except Exception:
184  try:
185  v = eval(value, {})
186  except Exception:
187  raise RuntimeError(f"Cannot render {value!r} as a value for {name!r}")
188  try:
189  setattr(subitem, subnameList[-1], v)
190  except Exception as e:
191  raise RuntimeError(f"Cannot set config. {name}={value!r}: {e}")
192 
193 

◆ setDottedAttrs()

def lsst.obs.base.utils.setDottedAttrs (   item,
  attrs 
)

Definition at line 194 of file utils.py.

194 def setDottedAttrs(item, attrs):
195  for name, value in attrs:
196  setDottedAttr(item, name, value)
lsst.obs.base.utils.getInstrument
def getInstrument(instrumentName, registry=None)
Definition: utils.py:100
lsst.obs.base.utils.setDottedAttrs
def setDottedAttrs(item, attrs)
Definition: utils.py:194
lsst::daf::persistence.utils.doImport
def doImport(pythonType)
Definition: utils.py:104
lsst.obs.base.utils.bboxFromIraf
def bboxFromIraf(irafBBoxStr)
Definition: utils.py:85
lsst::pex::exceptions::InvalidParameterError
Reports invalid arguments.
Definition: Runtime.h:66
lsst::geom::Point< int, 2 >
lsst::geom::Box2I
An integer coordinate rectangle.
Definition: Box.h:55
lsst::afw::geom::makeSkyWcs
std::shared_ptr< SkyWcs > makeSkyWcs(daf::base::PropertySet &metadata, bool strip=false)
Construct a SkyWcs from FITS keywords.
Definition: SkyWcs.cc:526
lsst.obs.base.utils.createInitialSkyWcs
def createInitialSkyWcs(visitInfo, detector, flipX=False)
Definition: utils.py:44
lsst.obs.base.utils.setDottedAttr
def setDottedAttr(item, name, value)
Definition: utils.py:148