Hide keyboard shortcuts

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# 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 

25import lsst.geom 

26from lsst.log import Log 

27from lsst.afw.fits import ImageWriteOptions 

28from .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) 

36def 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 

83def 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 

107def 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)