LSSTApplications  18.1.0
LSSTDataManagementBasePackage
fitsIoWithOptions.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2018 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 __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 . import image
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 = image.PARENT
71  if options.exists("imageOrigin"):
72  originStr = options.getString("imageOrigin")
73  if (originStr == "LOCAL"):
74  origin = image.LOCAL
75  elif (originStr == "PARENT"):
76  origin = image.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 exposureWriteFitsWithOptions(self, dest, options)
Options for writing an image to FITS.
Definition: fits.h:219
def imageReadFitsWithOptions(cls, source, options)
Definition: Log.h:691
def format(config, name=None, writeSourceLine=True, prefix="", verbose=False)
Definition: history.py:168
def imageWriteFitsWithOptions(self, dest, options)
An integer coordinate rectangle.
Definition: Box.h:54