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

71 statements  

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

1import os 

2 

3from casatasks import casalog 

4from casatools import table as tbtool 

5from casatools import ms as mstool 

6from .mstools import write_history 

7 

8def conjugatevis(vis,spwlist=[],outputvis="",overwrite=False): 

9 """: 

10 Change the sign of the phases in all visibility columns 

11 

12 Keyword arguments: 

13 vis -- Name of input visibility file 

14 default: none; example='3C273XC1.ms' 

15 spwlist -- Select spectral window 

16 default: [] all spws will be conjugated; example: spw=[1,2] 

17 outputvis -- name of output visibility file 

18 default: 'conjugated_'+vis; example= 'conjugated.ms' 

19 overwrite -- Overwrite the outputvis if it exists 

20 default=False; example: overwrite=True 

21 

22 """ 

23 

24 #Python script 

25 

26 _tb = tbtool() 

27 

28 try: 

29 casalog.origin('conjugatevis') 

30 myddlist = [] 

31 _tb.open(vis+'/SPECTRAL_WINDOW') 

32 maxspw = _tb.nrows()-1 

33 _tb.close() 

34 if (type(spwlist)==type(1)): 

35 spwlist = [spwlist] 

36 elif(spwlist==None or spwlist==[] or spwlist==""): 

37 spwlist = [] 

38 casalog.post("Will conjugate visibilities for all spws.", 'INFO') 

39 if not spwlist==[]: 

40 try: 

41 _tb.open(vis+'/DATA_DESCRIPTION') 

42 for k in spwlist: 

43 if (k<-1 or k>maxspw): 

44 raise RuntimeError("Error: max valid spw id is "+str(maxspw)) 

45 else: 

46 for j in range(0,_tb.nrows()): 

47 if(_tb.getcell("SPECTRAL_WINDOW_ID",j)==k and not (j in myddlist)): 

48 myddlist = myddlist + [j] 

49 #end for k 

50 _tb.close() 

51 casalog.post('DATA_DESC_IDs to process: '+str(myddlist), 'INFO') 

52 except Exception as exc: 

53 raise RuntimeError('Error reading DATA_DESCRIPTION table: {}'.format(exc)) 

54 #endif 

55 outname = 'conjugated_'+vis 

56 if not (outputvis==""): 

57 if((type(outputvis)!=str) or (len(outputvis.split()) < 1)): 

58 raise ValueError('parameter outputvis is invalid') 

59 outname = outputvis 

60 if not overwrite and os.path.exists(outname): 

61 raise RuntimeError('outputvis '+outname+' exists and you did not permit overwrite') 

62 os.system('rm -rf '+outname) 

63 os.system('cp -R '+vis+' '+outname) 

64 _tb.open(outname, nomodify=False) 

65 if _tb.iswritable(): 

66 if(spwlist==[]): 

67 for colname in [ 'DATA', 'CORRECTED_DATA', 'FLOAT_DATA' ]: 

68 if colname in _tb.colnames(): 

69 casalog.post('Conjugating '+str(colname), 'INFO') 

70 for i in range(0,_tb.nrows()): 

71 a = _tb.getcell(colname, i) 

72 a = a.conjugate() 

73 _tb.putcell(colname, i, a) 

74 else: 

75 for colname in [ 'DATA', 'CORRECTED_DATA', 'FLOAT_DATA' ]: 

76 if colname in _tb.colnames(): 

77 casalog.post('Conjugating '+str(colname), 'INFO') 

78 for i in range(0,_tb.nrows()): 

79 if(_tb.getcell("DATA_DESC_ID",i) in myddlist): 

80 a = _tb.getcell(colname, i) 

81 a = a.conjugate() 

82 _tb.putcell(colname, i, a) 

83 #endif 

84 _tb.flush() 

85 _tb.close() 

86 casalog.post('Created '+str(outname), 'INFO') 

87 else: 

88 _tb.close() 

89 casalog.post('Cannot write to output MS '+str(outname), 'WARN') 

90 

91 # Write history to output MS  

92 try: 

93 param_names = conjugatevis.__code__.co_varnames[:conjugatevis.__code__.co_argcount] 

94 local_vars = locals() 

95 param_vals = [local_vars[p] for p in param_names] 

96 

97 write_history(mstool(), outname, 'conjugatevis', param_names, 

98 param_vals, casalog) 

99 

100 except Exception as instance: 

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

102 

103 finally: 

104 _tb.close()