Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatasks/private/task_fluxscale.py: 87%
100 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-01 07:19 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-01 07:19 +0000
1import os
3from casatools import calibrater, ms
4from casatasks import casalog
5from .mstools import write_history
7def writeResultsHistory(myms, vis, mycasalog, indict):
8 """
9 write returned output of fluxscale to HISTORY subtable of the parent ms
10 """
11 isOpen = False
12 try:
13 myms.open(vis)
14 isOpen = True
16 mainkeys = indict.keys()
18 spwids = indict['spwID']
19 freqs = indict['freq']
20 msg0 = "Fluxscale results *****"
21 myms.writehistory(message=msg0, origin='fluxscale')
22 for ky in mainkeys:
23 try:
24 fieldid = int(ky)
25 except:
26 fieldid = None
28 if fieldid!=None:
29 fdict = indict[ky]
30 fname = fdict['fieldName']
31 fitF = fdict['fitFluxd']
32 fitFerr = fdict['fitFluxdErr']
33 fitRefFreq = fdict['fitRefFreq']
34 spix = fdict['spidx']
35 spixerr = fdict['spidxerr']
36 msg1 = fname+"(field id="+ky+") "
37 myms.writehistory(message=msg1, origin='fluxscale')
38 for ispw in spwids:
39 strspw = str(ispw)
40 spwfdict = fdict[strspw]
41 flux = spwfdict['fluxd']
42 fluxerr = spwfdict['fluxdErr']
43 freq = freqs[ispw]
44 fvalbase = 1.0
45 funit=''
46 if freq>1.e9:
47 fvalbase = 1.e9
48 funit = 'GHz'
49 elif freq>1.e6:
50 fvalbase = 1.e6
51 funit = 'MHz'
52 elif freq>1.e3:
53 fvalbase = 1.e3
54 funit = 'kHz'
55 else:
56 if freq > 0.0:
57 funit = 'Hz'
58 freq = freq/fvalbase
59 if funit!='':
60 funitlast=funit
61 fvalbaselast=fvalbase
62 if freq < 0.0:
63 msg2 = " Spw "+strspw+" insufficient data, flux density is not determined."
64 else:
65 msg2 = " Spw "+strspw+"(freq = {:7.3f}".format(freq)+funit+") Flux density = {:10.6f}".format(flux[0])+\
66 "+/-{:10.6f}".format(fluxerr[0])+" Jy"
67 myms.writehistory(message=msg2, origin='fluxscale')
68 if len(spwids) > 1:
69 msg3 = " Spectral index fitting coefficients [zero-point, alpha, beta] with errors :"
70 msg3a = "["
71 msg3b = "["
72 nspix = len(spix)
73 for ispix in range(nspix):
74 msg3a += "{:5.3f}".format(spix[ispix])
75 msg3b += "{:5.3f}".format(spixerr[ispix])
76 if ispix!=nspix-1:
77 msg3a += ","
78 msg3b += ","
79 msg3a += "]"
80 msg3b += "]"
81 msg3 += msg3a + "+/-" + msg3b
82 fitreffreq = fitRefFreq/fvalbaselast
83 msg4 = " Fitted flux density = {:10.6f}".format(fitF)+"+/-{:10.6f}".format(fitFerr)+\
84 " Jy (reference freq = {:7.3f}".format(fitreffreq)+funitlast+")"
85 myms.writehistory(message=msg3, origin='fluxscale')
86 myms.writehistory(message=msg4, origin='fluxscale')
88 except Exception as instance:
89 mycasalog.post("*** Error \'%s\' updating fluxscale results in HISTORY of %s" % (instance, vis), 'WARN' )
91 finally:
92 if isOpen:
93 myms.close()
95def fluxscale(vis=None, caltable=None, fluxtable=None, reference=None, transfer=None, listfile=None, append=None,
96 refspwmap=None, gainthreshold=None, antenna=None, timerange=None, scan=None, incremental=None,
97 fitorder=None, display=None):
98 """Bootstrap the flux density scale from standard calibrators:
100 After running gaincal on standard flux density calibrators (with or
101 without a model), and other calibrators with unknown flux densities,
102 fluxscale will determine the flux density of the unknowns calibrators
103 that are most consistent with the standard calibrator antenna gains.
105 Keyword arguments:
106 vis -- Name of input visibility file
107 default: none; example: vis='ngc5921.ms'
108 caltable -- Name of input calibration table
109 default: none; example: caltable='ngc5921.gcal'
110 This cal table was obtained from task gaincal.
111 fluxtable -- Name of output, flux-scaled calibration table
112 default: none; example: fluxtable='ngc5921.gcal2'
113 The gains in this table have been adjusted by the
114 derived flux density each calibrator. The MODEL_DATA
115 column has NOT been updated for the flux density of the
116 calibrator. Use setjy to do this if it is a point source.
117 reference -- Reference field name(s)
118 The names of the fields with a known flux densities or
119 visibilties that have been placed in the MODEL column
120 by setjy or ft for a model not in the CASA system.
121 The syntax is similar to field. Hence field index or
122 names can be used.
123 default: none; example: reference='1328+307'
124 transfer -- Transfer field name(s)
125 The names of the fields with unknown flux densities.
126 These should be point-like calibrator sources
127 The syntax is similar to field. Hence source index or
128 names can be used.
129 default: '' = all sources in caltable that are not specified
130 as reference sources. Do not include unknown target sources
131 example: transfer='1445+099, 3C84'; transfer = '0,4'
133 NOTE: All fields in reference and transfer must have solutions
134 in the caltable.
136 listfile -- Fit listfile name
137 The list file contains the flux density, flux density error,
138 S/N, and number of solutions (all antennas and feeds) for each
139 spectral window. NOTE: The nominal spectral window frequencies
140 will be included in the future.
141 default: '' = no fit listfile will be created.
143 append -- Append fluxscaled solutions to the fluxtable.
144 default: False; (will overwrite if already existing)
145 example: append=True
146 refspwmap -- Vector of spectral windows enablings scaling across
147 spectral windows
148 default: [-1]==> none.
149 Example with 4 spectral windows:
150 if the reference fields were observed only in spw=1 & 3,
151 and the transfer fields were observed in all 4 spws (0,1,2,3),
152 specify refspwmap=[1,1,3,3].
153 This will ensure that transfer fields observed in spws 0,1,2,3
154 will be referenced to reference field solutions only in
155 spw 1 or 3.
157 gainthreshold -- % deviation threshold from the median gain to be used flux scaling derivation
159 antenna -- Select antennas to be used to derive flux scaling
161 timerange -- Select timerage to be used to derive flux scaling with given antenna selection
163 scan -- Select scans to be used to derived flux scaling with given antenna selection
165 incremental -- Create an incremental caltable containing only the gain correction
166 factors.
167 default: False; (older behavior, produces flux scaled gain table)
169 fitorder -- the order of spectral fitting when solutions for multiple spws are available
170 default: 1
172 display -- display statistics (histogram) of derived correction factors
173 default: false
174 """
176 try:
177 casalog.origin('fluxscale')
179 mycb = calibrater()
180 # check the input param
181 if fluxtable=="":
182 casalog.post("Missing fluxtable name.","SEVERRE")
183 raise ValueError("Missing fluxtable name.")
184 else:
185 if os.path.exists(fluxtable) and not append:
186 casalog.post("fluxtable %s exists." % fluxtable, "SEVERE")
187 raise ValueError("fluxtable %s exists. Please specify a different name. Or set append=True, to append the results to the table." % fluxtable)
189 mycb.open(filename=vis,compress=False,addcorr=False,addmodel=False)
190 output = mycb.fluxscale(tablein=caltable,tableout=fluxtable,reference=reference,
191 transfer=transfer,listfile=listfile,append=append,
192 refspwmap=refspwmap,gainthreshold=gainthreshold,antenna=antenna,
193 timerange=timerange,scan=scan,
194 incremental=incremental,fitorder=fitorder,display=display)
196 #write history
197 try:
198 param_names = fluxscale.__code__.co_varnames[:fluxscale.__code__.co_argcount]
199 local_vars = locals( )
200 param_vals = [local_vars[p] for p in param_names]
202 write_history(ms(), vis, 'fluxscale', param_names,
203 param_vals, casalog)
204 writeResultsHistory(ms(), vis, casalog, output)
205 except Exception as instance:
206 casalog.post( "*** Error \'%s\' updating HISTORY" % (instance), 'WARN' )
208 finally:
209 mycb.close()
211 return output