Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatasks/private/task_imsmooth.py: 86%

83 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-01 07:19 +0000

1########################################################################3 

2# task_imsmooth.py 

3# 

4# Copyright (C) 2008, 2009 

5# Associated Universities, Inc. Washington DC, USA. 

6# 

7# This script is free software; you can redistribute it and/or modify it 

8# under the terms of the GNU Library General Public License as published by 

9# the Free Software Foundation; either version 2 of the License, or (at your 

10# option) any later version. 

11# 

12# This library is distributed in the hope that it will be useful, but WITHOUT 

13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

14# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 

15# License for more details. 

16# 

17# You should have received a copy of the GNU Library General Public License 

18# along with this library; if not, write to the Free Software Foundation, 

19# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 

20# 

21# Correspondence concerning AIPS++ should be adressed as follows: 

22# Internet email: casa-feedback@nrao.edu. 

23# Postal address: AIPS++ Project Office 

24# National Radio Astronomy Observatory 

25# 520 Edgemont Road 

26# Charlottesville, VA 22903-2475 USA 

27# 

28# <summary> 

29# CASA task for smoothing an image, by doing Forier-based convolution 

30# on a CASA image file. 

31# </summary> 

32# 

33# <reviewed reviwer="" date="" tests="" demos=""> 

34# </reviewed 

35# 

36# <author> 

37# Shannon Jaeger (University of Calgary) 

38# </author> 

39# 

40# <etymology> 

41# imsmooth stands for image smoothing 

42# </etymology> 

43# 

44# <synopsis> 

45# task_imsmooth.py is a Python script providing an easy to use task 

46# for smoothing an image.  

47# 

48# The convolv2d function of the image tool is used to do the work, 

49# a description of this function can be found at 

50# http://casa.nrao.edu/docs/casaref/image.convolve2d.html#x27-270001.1.1 

51#  

52# </synopsis>  

53# 

54# <example> 

55# <srcblock> 

56# # The following code snippet find the 1-moments, intensity-weighted 

57# # coordinate, often used for finding velocity fields. 

58# imsmooth( imagename='myimage', kernel='gaussian', outfile='myimage.smooth' ) 

59# 

60# 

61# </example> 

62# 

63# <motivation> 

64# To provide a user-friendly method to smooth images. 

65# </motivation> 

66# 

67# <todo> 

68# </todo> 

69 

70import inspect 

71import os 

72import sys 

73import numpy 

74 

75from casatools import image, regionmanager, quanta 

76from casatasks import casalog 

77from .ialib import write_image_history 

78_qa = quanta() 

79 

80from casatasks.private.callabletask import log_origin_setter 

81 

82 

83@log_origin_setter 

84def imsmooth( 

85 imagename, kernel, major, minor, pa, targetres, kimage, scale, region, 

86 box, chans, stokes, mask, outfile, stretch, overwrite, beam 

87): 

88 

89 ikernel = kernel.startswith('i') 

90 ckernel = kernel.startswith('c') 

91 bkernel = kernel.startswith('b') 

92 gkernel = kernel.startswith('g') 

93 if ( 

94 not ( 

95 gkernel or bkernel or ckernel or ikernel 

96 ) 

97 ): 

98 raise ValueError('Unsupported kernel, ' + kernel) 

99 

100 if (not ikernel and type(beam) == str): 

101 if len(beam) != 0: 

102 err = "beam cannot be a non-empty string" 

103 casalog.post(err, "SEVERE") 

104 raise ValueError(err) 

105 beam = {} 

106 

107 # First check to see if the output file exists. If it 

108 # does then we abort. CASA doesn't allow files to be 

109 # over-written, just a policy. 

110 if ( len( outfile ) < 1 ): 

111 outfile = 'imsmooth_results.im' 

112 casalog.post( "The outfile paramter is empty, consequently the" \ 

113 +" smoothed image will be\nsaved on disk in file, " \ 

114 + outfile, 'WARN') 

115 _myia = image() 

116 _myia.dohistory(False) 

117 retia = image() 

118 _myia.open(imagename) 

119 mycsys = _myia.coordsys() 

120 myrg = regionmanager() 

121 reg = myrg.frombcs( 

122 mycsys.torecord(), _myia.shape(), box, chans, 

123 stokes, "a", region 

124 ) 

125 myrg.done() 

126 mycsys.done() 

127 _myia.done() 

128 # If the values given are integers we assume they are given in 

129 # arcsecs and alter appropriately 

130 if not ikernel: 

131 if isinstance(major, (int, float)): 

132 major=str(major)+'arcsec' 

133 if isinstance(minor, (int, float)): 

134 minor=str(minor)+'arcsec' 

135 if isinstance(pa, (int, float)): 

136 pa=str(pa)+'deg' 

137 

138 try: 

139 outia = None 

140 if ( gkernel or ckernel): 

141 _myia.open(imagename) 

142 if ckernel: 

143 beam = _myia.commonbeam() 

144 # add a small epsilon to avoid convolving with a null beam to reach 

145 # a target resolution that already exists 

146 beam['major'] = _qa.mul(beam['major'], 1 + 1e-10) 

147 beam['minor'] = _qa.mul(beam['minor'], 1 + 1e-10) 

148 major = "" 

149 minor = "" 

150 pa = "" 

151 targetres = True 

152 if (beam and (major or minor or pa)): 

153 raise ValueError("You may specify only beam or the set of major/minor/pa") 

154 if not beam: 

155 if not major: 

156 raise ValueError("Major axis must be specified") 

157 if not minor: 

158 raise ValueError("Minor axis must be specified") 

159 if not pa: 

160 raise ValueError("Position angle must be specified") 

161 

162 outia = _myia.convolve2d( 

163 axes=[0,1], region=reg, major=major, 

164 minor=minor, pa=pa, outfile=outfile, 

165 mask=mask, stretch=stretch, targetres=targetres, 

166 overwrite=overwrite, beam=beam 

167 ) 

168 elif (bkernel ): 

169 if not major or not minor: 

170 raise ValueError("Both major and minor must be specified.") 

171 # BOXCAR KERNEL 

172 # 

173 # Until convolve2d supports boxcar we will need to 

174 # use sepconvolve to do this. 

175 # 

176 # BIG NOTE!!!!! 

177 # According to Gaussian2D documentation the default position 

178 # angle aligns the major axis along the y-axis, which typically 

179 # be lat. So this means that we need to use the major quantity 

180 # on the y axis (or 1) for sepconvolve. 

181 

182 _myia.open( imagename ) 

183 casalog.post( "ia.sepconvolve( axes=[0,1],"+\ 

184 "types=['boxcar','boxcar' ],"+\ 

185 "widths=[ "+str(minor)+", "+str(major)+" ],"+ \ 

186 "region="+str(reg)+",outfile="+outfile+" )",\ 

187 'DEBUG2' ) 

188 #retValue = ia.sepconvolve( axes=[0,1], types=['box','box' ],\ 

189 # widths=[ minor, major ], \ 

190 # region=reg,outfile=outfile ) 

191 outia = _myia.sepconvolve( 

192 axes=[0,1], types=['box','box' ], 

193 widths=[ minor, major ], 

194 region=reg,outfile=outfile, 

195 mask=mask, stretch=stretch, 

196 overwrite=overwrite 

197 ) 

198 elif ikernel: 

199 _myia.open(imagename) 

200 outia = _myia.convolve( 

201 outfile=outfile, kernel=kimage, scale=scale, region=reg, 

202 mask=mask, overwrite=overwrite, stretch=stretch 

203 ) 

204 else: 

205 raise ValueError('Unrecognized kernel type: ' + kernel) 

206 

207 try: 

208 param_names = imsmooth.__code__.co_varnames[:imsmooth.__code__.co_argcount] 

209 param_vals = [eval(p) for p in param_names] 

210 write_image_history( 

211 outia, sys._getframe().f_code.co_name, 

212 param_names, param_vals, casalog 

213 ) 

214 except Exception as instance: 

215 casalog.post("*** Error \'%s\' updating HISTORY" % (instance), 'WARN') 

216 

217 finally: 

218 _myia.done() 

219 if outia: outia.done()