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

100 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-31 17:39 +0000

1import os 

2import numpy 

3 

4from casatools import componentlist, imager, measures, quanta 

5from casatasks import casalog 

6from .setjy_helper import testerrs 

7from . import solar_system_setjy as SSSetjy 

8from casatasks.private.predictcomp_helper import * 

9 

10_qa = quanta( ) 

11 

12def predictcomp(objname=None, standard=None, epoch=None, 

13 minfreq=None, maxfreq=None, nfreqs=None, prefix=None, 

14 antennalist=None, showplot=None, savefig=None, symb=None, 

15 include0amp=None, include0bl=None, blunit=None, bl0flux=None): 

16 """ 

17 Writes a component list named clist to disk and returns a dict of 

18 {'clist': clist, 

19 'objname': objname, 

20 'angdiam': angular diameter in radians (if used in clist), 

21 'standard': standard, 

22 'epoch': epoch, 

23 'freqs': numpy.array of frequencies, in GHz, 

24 'uvrange': numpy.array of baseline lengths, in m, 

25 'amps': numpy.array of predicted visibility amplitudes, in Jy, 

26 'savedfig': False or, if made, the filename of a plot.} 

27 or False on error. 

28 

29 objname: An object supported by standard. 

30 standard: A standard for calculating flux densities, as in setjy. 

31 Default: 'Butler-JPL-Horizons 2010' 

32 epoch: The epoch to use for the calculations. Irrelevant for 

33 extrasolar standards. 

34 minfreq: The minimum frequency to use. 

35 Example: '342.0GHz' 

36 maxfreq: The maximum frequency to use. 

37 Default: minfreq 

38 Example: '346.0GHz' 

39 Example: '', anything <= 0, or None: use minfreq. 

40 nfreqs: The number of frequencies to use. 

41 Default: 1 if minfreq == maxfreq, 

42 2 otherwise. 

43 prefix: The component list will be saved to 

44 prefix + '<objname>_spw0_<minfreq><epoch>.cl' 

45 Default: '' 

46 antennalist: An array configuration file as used by simdata. 

47 If given, a plot of S vs. |u| will be made. 

48 Default: '' (None, just make clist.) 

49 showplot: Whether or not to show the plot on screen. 

50 Subparameter of antennalist. 

51 Default: Necessarily False if antennalist is not specified. 

52 True otherwise. 

53 savefig: Filename for saving a plot of S vs. |u|. 

54 Subparameter of antennalist. 

55 Default: False (necessarily if antennalist is not specified) 

56 Examples: True (save to prefix + '.png') 

57 'myplot.png' (save to myplot.png)  

58 symb: One of matplotlib's codes for plot symbols: .:,o^v<>s+xDd234hH|_ 

59 default: '.' 

60 include0amp: Force the lower limit of the amplitude axis to 0. 

61 Default: False 

62 include0bl: Force the lower limit of the baseline length axis to 0. 

63 blunit: Unit of the baseline length  

64 bl0flux: show zero baseline flux 

65 """ 

66 retval = None 

67 

68 casalog.origin('predictcomp') 

69 # some parameter minimally required 

70 if objname=='': 

71 raise ValueError("Error, objname is undefined") 

72 if minfreq=='': 

73 raise ValueError("Error, minfreq is undefined") 

74 minfreqq = _qa.quantity(minfreq) 

75 minfreqHz = _qa.convert(minfreqq, 'Hz')['value'] 

76 try: 

77 maxfreqq = _qa.quantity(maxfreq) 

78 except Exception as instance: 

79 maxfreqq = minfreqq 

80 frequnit = maxfreqq['unit'] 

81 maxfreqHz = _qa.convert(maxfreqq, 'Hz')['value'] 

82 if maxfreqHz <= 0.0: 

83 maxfreqq = minfreqq 

84 maxfreqHz = minfreqHz 

85 if minfreqHz != maxfreqHz: 

86 if nfreqs < 2: 

87 nfreqs = 2 

88 else: 

89 nfreqs = 1 

90 freqs = numpy.linspace(minfreqHz, maxfreqHz, nfreqs) 

91 

92 myme = measures() 

93 mepoch = myme.epoch('UTC', epoch) 

94 #if not prefix: 

95 ## meanfreq = {'value': 0.5 * (minfreqHz + maxfreqHz), 

96 ## 'unit': frequnit} 

97 ## prefix = "%s%s_%.7g" % (objname, epoch.replace('/', '-'), 

98 ## minfreqq['value']) 

99 ## if minfreqHz != maxfreqHz: 

100 ## prefix += "to" + maxfreq 

101 ## else: 

102 ## prefix += minfreqq['unit'] 

103 ## prefix += "_" 

104 # prefix = '' 

105 

106 # 

107 if not prefix: 

108 if not os.access("./",os.W_OK): 

109 casalog.post("No write access in the current directory, trying to write cl to /tmp...","WARN") 

110 prefix="/tmp/" 

111 if not os.access(prefix, os.W_OK): 

112 casalog.post("No write access to /tmp to write cl file", "SEVERE") 

113 return False 

114 else: 

115 prefixdir=os.path.dirname(prefix) 

116 if prefixdir=='/' and len(prefix)>1: 

117 prefix = prefix+'/' 

118 prefixdir = os.path.dirname(prefix) 

119 if not os.path.exists(prefixdir): 

120 prefixdirs = prefixdir.split('/') 

121 if prefixdirs[0]=="" and len(prefixdirs)>1: 

122 rootdir = "/" + prefixdirs[1] 

123 else: 

124 rootdir = "./" 

125 if os.access(rootdir,os.W_OK): 

126 if prefixdir!='': 

127 os.makedirs(prefixdir) 

128 else: 

129 casalog.post("No write access to "+rootdir+" to write cl file", "SEVERE") 

130 return False 

131 

132 # Get clist 

133 myim = imager() 

134 if hasattr(myim, 'predictcomp'): 

135 casalog.post('local im instance created', 'DEBUG1') 

136 else: 

137 casalog.post('Error creating a local im instance.', 'SEVERE') 

138 return False 

139 # casalog.post("FREQS="+freqs) 

140 # output CL file name is fixed : prefix+"spw0_"+minfreq+mepoch.cl 

141 minfreqGHz = _qa.convert(_qa.quantity(minfreq), 'GHz')['value'] 

142 decimalfreq = minfreqGHz - int(minfreqGHz) 

143 decimalepoch = mepoch['m0']['value'] - int(mepoch['m0']['value']) 

144 if decimalfreq == 0.0: 

145 minfreqGHzStr = str(int(minfreqGHz))+'GHz' 

146 else : 

147 minfreqGHzStr = str(minfreqGHz)+'GHz' 

148 

149 if decimalepoch == 0.0: 

150 epochStr = str(int(mepoch['m0']['value']))+'d' 

151 else: 

152 epochStr=str(mepoch['m0']['value'])+'d' 

153 outfilename = "spw0_"+objname+"_"+minfreqGHzStr+epochStr+'.cl' 

154 outfilename = prefix+outfilename 

155 if (os.path.exists(outfilename) and os.path.isdir(outfilename)) : 

156 

157 shutil.rmtree(outfilename) 

158 casalog.post("Removing the existing componentlist, "+outfilename) 

159 

160 if standard=='Butler-JPL-Horizons 2012': 

161 clist = predictSolarObjectCompList(objname, mepoch, freqs.tolist(), prefix) 

162 else: 

163 clist = myim.predictcomp(objname, standard, mepoch, freqs.tolist(), prefix) 

164 # casalog.post("created componentlist =" +clist) 

165 if os.path.isdir(clist): 

166 # The spw0 is useless here, but it is added by FluxStandard for the sake of setjy. 

167 casalog.post('The component list was saved to ' + clist) 

168 

169 retval = {'clist': clist, 

170 'objname': objname, 

171 'standard': standard, 

172 'epoch': mepoch, 

173 'freqs (GHz)': 1.0e-9 * freqs, 

174 'antennalist': antennalist} 

175 mycl = componentlist() 

176 mycl.open(clist) 

177 comp = mycl.getcomponent(0) 

178 zeroblf=comp['flux']['value'] 

179 if standard=='Butler-JPL-Horizons 2012': 

180 f0=comp['spectrum']['frequency']['m0']['value'] 

181 else: 

182 f0=retval['freqs (GHz)'][0] 

183 casalog.post("Zero baseline flux %s @ %sGHz " % (zeroblf, f0),'INFO') 

184 mycl.close(False) # False prevents the stupid warning. 

185 for k in ('shape', 'spectrum'): 

186 retval[k] = comp[k] 

187 if antennalist: 

188 retval['spectrum']['bl0flux']={} 

189 retval['spectrum']['bl0flux']['value']=zeroblf[0] 

190 retval['spectrum']['bl0flux']['unit']='Jy' 

191 retval['savedfig'] = None 

192 

193 if not bl0flux: 

194 zeroblf=[0.0] 

195 

196 else: 

197 retval['savedfig'] = None 

198 else: 

199 casalog.post("There was an error in making the component list.", 

200 'SEVERE') 

201 

202 return retval