Coverage for python/lsst/afw/image/image/fitsIoWithOptions.py : 14%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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"]
25import lsst.geom
26from lsst.log import Log
27from lsst.afw.fits import ImageWriteOptions
28from . import image
31# This must be added to a class as a *classmethod*, for example:
32#
33# @continueclass
34# class MaskX:
35# readFitsWithOptions = classmethod(imageReadFitsWithOptions)
36def imageReadFitsWithOptions(cls, source, options):
37 """Read an Image, Mask, MaskedImage or Exposure from a FITS file,
38 with options.
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:
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.)
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))
80 return cls(source, bbox=bbox, origin=origin)
83def imageWriteFitsWithOptions(self, dest, options):
84 """Write an Image or Mask to FITS, with options
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)
107def exposureWriteFitsWithOptions(self, dest, options):
108 """Write an Exposure or MaskedImage to FITS, with options
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)