Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatasks/private/task_split.py: 11%
153 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-31 19:10 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-31 19:10 +0000
1import os
2import re
3import string
4import time
5import shutil
6import numpy as np
8from . import flaghelper as fh
9from .update_spw import update_spwchan
10from .parallel.parallel_data_helper import ParallelDataHelper
11from .mstools import write_history
12from casatasks import casalog
13from casatools import quanta
14from casatools import table as tbtool
15from casatools import ms as mstool
16from casatools import mstransformer as mttool
18_qa = quanta( )
21def split(vis,
22 outputvis,
23 keepmms,
24 field,
25 spw,
26 scan,
27 antenna,
28 correlation,
29 timerange,
30 intent,
31 array,
32 uvrange,
33 observation,
34 feed,
35 datacolumn,
36 keepflags,
37 width,
38 timebin,
39 combine
40 ):
42 """Create a visibility subset from an existing visibility set"""
44 casalog.origin('split')
46 # Initialize the helper class
47 pdh = ParallelDataHelper("split", locals())
49 # Validate input and output parameters
50 pdh.setupIO()
52 # Input vis is an MMS
53 if pdh.isMMSAndNotServer(vis) and keepmms:
55 retval = pdh.validateInputParams()
56 if not retval['status']:
57 raise Exception('Unable to continue with MMS processing')
59 pdh.setupCluster('split')
61 # Execute the jobs
62 pdh.go()
63 return
66 try:
67 mtlocal = mttool()
68 # Gather all the parameters in a dictionary.
69 config = {}
71 if keepflags:
72 taqlstr = ''
73 else:
74 taqlstr = "NOT (FLAG_ROW OR ALL(FLAG))"
76 if type(correlation) == list:
77 correlation = ', '.join(correlation)
78 correlation = correlation.upper()
80 config = pdh.setupParameters(inputms=vis, outputms=outputvis, field=str(field),
81 spw=str(spw), array=str(array), scan=str(scan), antenna=str(antenna), correlation=correlation,
82 uvrange=uvrange,timerange=timerange, intent=intent, observation=str(observation),
83 feed=str(feed), taql=taqlstr)
85 config['datacolumn'] = datacolumn
87 # Channel averaging
88 chanaverage = False
89 chanbin = width
91 # String type
92 if isinstance(width, str):
93 if width.isdigit():
94 chanbin = int(width)
95 else:
96 casalog.post('Parameter width is invalid. Using 1 as default', 'WARN')
97 chanbin = width = 1
99 if chanbin > 1:
100 chanaverage = True
102 # List type
103 elif isinstance(width, list):
104 if isinstance(width[0], str):
105 if width[0].isdigit():
106 chanbin = map(int,width)
107 else:
108 casalog.post('Parameter width is invalid. Using 1 as default', 'WARN')
109 chanbin = width = 1
111 # If any chanbin in list is > 1, chanaverage=True
112 testbin = [i for i in chanbin if i > 1]
113 if len(testbin) > 0:
114 chanaverage = True
116 # Any other type
117 if not isinstance(chanbin,str) and not isinstance(chanbin,list):
118 casalog.post('Original type(width) is %s'%type(chanbin),'DEBUG')
119 if chanbin > 1:
120 chanaverage = True
122 if chanaverage:
123 casalog.post('Parse channel averaging parameters')
124 config['chanaverage'] = True
125 # verify that the number of spws is the same of the number of chanbin
126 pdh.validateChanBin()
127 # convert numpy types, until CAS-6493 is not fixed
128 chanbin = fh.evaluateNumpyType(chanbin)
129 casalog.post('Converted type(width) is %s'%type(chanbin),'DEBUG')
130 config['chanbin'] = chanbin
132 # Time averaging
133 timeaverage = False
134 tb = _qa.convert(_qa.quantity(timebin), 's')['value']
135 if tb > 0:
136 timeaverage = True
138 if timeaverage:
139 casalog.post('Parse time averaging parameters')
140 config['timeaverage'] = True
141 config['timebin'] = timebin
142 config['timespan'] = combine
143 config['maxuvwdistance'] = 0.0
145 # Configure the tool
146 casalog.post('%s'%config, 'DEBUG1')
147 mtlocal.config(config)
149 # Open the MS, select the data and configure the output
150 mtlocal.open()
152 # Run the tool
153 mtlocal.run()
155 mtlocal.done()
157 finally:
158 mtlocal.done()
160 # Local copy of ms tool
161 mslocal = mstool()
163 # Update the FLAG_CMD sub-table to reflect any spw/channels selection
164 # If the spw selection is by name or FLAG_CMD contains spw with names, skip the updating
166 if ((spw != '') and (spw != '*')) or chanaverage == True:
167 isopen = False
169 try:
170 mytb = tbtool()
171 mytb.open(outputvis + '/FLAG_CMD', nomodify=False)
172 isopen = True
173 nflgcmds = mytb.nrows()
175 if nflgcmds > 0:
176 updateFlagCmd = False
178 # If spw selection is by name in FLAG_CMD, do not update, CAS-7751
179 mycmd = mytb.getcell('COMMAND', 0)
180 cmdlist = mycmd.split()
181 for cmd in cmdlist:
182 # Match only spw indices, not names
183 if cmd.__contains__('spw'):
184 cmd = cmd.strip("spw=")
185 spwstr = re.search('^[^a-zA-Z]+$', cmd)
186 if spwstr != None and spwstr.string.__len__() > 0:
187 updateFlagCmd = True
188 break
191 if updateFlagCmd:
192 mademod = False
193 cmds = mytb.getcol('COMMAND')
194 widths = {}
195 if hasattr(chanbin, 'has_key'):
196 widths = chanbin
197 else:
198 if hasattr(chanbin, '__iter__') and len(chanbin) > 1:
199 for i in range(len(chanbin)):
200 widths[i] = chanbin[i]
201 elif chanbin != 1:
202 numspw = len(mslocal.msseltoindex(vis=vis,
203 spw='*')['spw'])
204 if hasattr(chanbin, '__iter__'):
205 w = chanbin[0]
206 else:
207 w = chanbin
208 for i in range(numspw):
209 widths[i] = w
210 for rownum in range(nflgcmds):
211 # Matches a bare number or a string quoted any way.
212 spwmatch = re.search(r'spw\s*=\s*(\S+)', cmds[rownum])
213 if spwmatch:
214 sch1 = spwmatch.groups()[0]
215 sch1 = re.sub(r"[\'\"]", '', sch1) # Dequote
216 # Provide a default in case the split selection excludes
217 # cmds[rownum]. update_spwchan() will throw an exception
218 # in that case.
219 cmd = ''
220 try:
221 sch2 = update_spwchan(vis, spw, sch1, truncate=True,
222 widths=widths)
223 if sch2:
224 repl = ''
225 if sch2 != '*':
226 repl = "spw='" + sch2 + "'"
227 cmd = cmds[rownum].replace(spwmatch.group(), repl)
228 except Exception as e:
229 casalog.post(
230 "Error %s updating row %d of FLAG_CMD" % (e,rownum), 'WARN')
231 casalog.post('sch1 = ' + sch1, 'DEBUG1')
232 casalog.post('cmd = ' + cmd, 'DEBUG1')
233 if cmd != cmds[rownum]:
234 mademod = True
235 cmds[rownum] = cmd
236 if mademod:
237 casalog.post('Updating FLAG_CMD', 'INFO')
238 mytb.putcol('COMMAND', cmds)
240 else:
241 casalog.post('FLAG_CMD table contains spw selection by name. Will not update it!','DEBUG')
243 finally:
244 if isopen:
245 mytb.close()
246 mytb = None
248 # Write history to output MS, not the input ms.
249 try:
250 param_names = split.__code__.co_varnames[:split.__code__.co_argcount]
251 local_vars = locals( )
252 param_vals = [local_vars[p] for p in param_names]
253 write_history(mslocal, outputvis, 'split', param_names,
254 param_vals, casalog)
255 except Exception as instance:
256 casalog.post("*** Error \'%s\' updating HISTORY" % (instance),'WARN')
258 mslocal = None