lsst.afw  21.0.0-18-g1421380c6+e734d31160
fitsIoWithOptions.py
Go to the documentation of this file.
1 # This file is part of afw.
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (https://www.lsst.org).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
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 GNU General Public License
20 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 
22 __all__ = ["imageReadFitsWithOptions",
23  "imageWriteFitsWithOptions", "exposureWriteFitsWithOptions"]
24 
25 import lsst.geom
26 from lsst.log import Log
27 from lsst.afw.fits import ImageWriteOptions
28 from .image import ImageOrigin
29 
30 
31 # This must be added to a class as a *classmethod*, for example:
32 #
33 # @continueclass
34 # class MaskX:
35 # readFitsWithOptions = classmethod(imageReadFitsWithOptions)
36 def imageReadFitsWithOptions(cls, source, options):
37  """Read an Image, Mask, MaskedImage or Exposure from a FITS file,
38  with options.
39 
40  Parameters
41  ----------
42  source : `str`
43  Fits file path from which to read image, mask, masked image
44  or exposure.
45  options : `lsst.daf.base.PropertySet`
46  Read options:
47 
48  - llcX: bbox minimum x (int)
49  - llcY: bbox minimum y (int, must be present if llcX is present)
50  - width: bbox width (int, must be present if llcX is present)
51  - height: bbox height (int, must be present if llcX is present)
52  - imageOrigin: one of "LOCAL" or "PARENT" (has no effect unless
53  a bbox is specified by llcX, etc.)
54 
55  Raises
56  ------
57  RuntimeError
58  If options contains an unknown value for "imageOrigin"
59  lsst.pex.exceptions.NotFoundError
60  If options contains "llcX" and is missing any of
61  "llcY", "width", or "height".
62  """
63  bbox = lsst.geom.Box2I()
64  if options.exists("llcX"):
65  llcX = options.getInt("llcX")
66  llcY = options.getInt("llcY")
67  width = options.getInt("width")
68  height = options.getInt("height")
69  bbox = lsst.geom.Box2I(lsst.geom.Point2I(llcX, llcY), lsst.geom.Extent2I(width, height))
70  origin = ImageOrigin.PARENT
71  if options.exists("imageOrigin"):
72  originStr = options.getString("imageOrigin")
73  if (originStr == "LOCAL"):
74  origin = ImageOrigin.LOCAL
75  elif (originStr == "PARENT"):
76  origin = ImageOrigin.PARENT
77  else:
78  raise RuntimeError("Unknown ImageOrigin type {}".format(originStr))
79 
80  return cls(source, bbox=bbox, origin=origin)
81 
82 
83 def imageWriteFitsWithOptions(self, dest, options):
84  """Write an Image or Mask to FITS, with options
85 
86  Parameters
87  ----------
88  dest : `str`
89  Fits file path to which to write the image or mask.
90  options : `lsst.daf.base.PropertySet
91  Write options. The item "image" is read. It must contain an
92  `lsst.daf.base.PropertySet` with data for
93  ``lsst.afw.fits.ImageWriteOptions``.
94  """
95  if options is not None:
96  try:
97  writeOptions = ImageWriteOptions(options.getPropertySet("image"))
98  except Exception as e:
99  log = Log.getLogger("lsst.afw.image")
100  log.warn("Could not parse options; writing with defaults: {}".format(e))
101  else:
102  self.writeFits(dest, writeOptions)
103  return
104  self.writeFits(dest)
105 
106 
107 def exposureWriteFitsWithOptions(self, dest, options):
108  """Write an Exposure or MaskedImage to FITS, with options
109 
110  Parameters
111  ----------
112  dest : `str`
113  Fits file path to which to write the exposure or masked image.
114  options : `lsst.daf.base.PropertySet`
115  Write options. The items "image", "mask" and "variance" are read.
116  Each must be an `lsst.daf.base.PropertySet` with data for
117  ``lsst.afw.fits.ImageWriteOptions``.
118  """
119  if options is not None:
120  try:
121  writeOptionDict = {name + "Options": ImageWriteOptions(options.getPropertySet(name))
122  for name in ("image", "mask", "variance")}
123  except Exception as e:
124  log = Log.getLogger("lsst.afw.image")
125  log.warn("Could not parse options; writing with defaults: {}".format(e))
126  else:
127  self.writeFits(dest, **writeOptionDict)
128  return
129  self.writeFits(dest)
def imageWriteFitsWithOptions(self, dest, options)
def imageReadFitsWithOptions(cls, source, options)
def exposureWriteFitsWithOptions(self, dest, options)
Options for writing an image to FITS.
Definition: fits.h:219