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

113 statements  

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

1import os 

2 

3from casatasks import casalog 

4from casatools import sdm, table, quanta, ms 

5 

6_ms = ms() 

7_tb = table() 

8_qa = quanta() 

9 

10def exportasdm(vis=None, asdm=None, datacolumn=None, archiveid=None, rangeid=None, 

11 subscanduration=None, sbduration=None, apcorrected=None, 

12 verbose=None): 

13 """ Convert a CASA visibility file (MS) into an ALMA or EVLA Science Data Model. 

14  

15 Keyword arguments: 

16 vis -- MS name, 

17 default: none 

18 

19 asdm -- Name of output ASDM file (directory), 

20 default: none; example: asdm='ExecBlock3' 

21 

22 datacolumn -- specifies which of the MS data columns (DATA, 

23 CORRECTED_DATA, or MODEL_DATA) should be used as the 

24 visibilities in the ASDM, default: DATA 

25 

26 archiveid -- the X0 in uid://X0/X1/X<running> 

27 default: "S0" 

28 

29 rangeid -- the X1 in uid://X0/X1/X<running> 

30 default: "X1" 

31 

32 subscanduration -- maximum duration of a subscan in the output ASDM 

33 default: "24h" 

34 

35 sbduration -- maximum duration of a scheduling block in the output ASDM 

36 default: "2700s" 

37 

38 apcorrected -- If true, the data in column datacolumn should be regarded 

39 as having atmospheric phase correction, default: False 

40 

41 verbose -- produce log output, default: True 

42 

43 """ 

44 #Python script 

45 

46 casalog.origin('exportasdm') 

47 parsummary = 'vis=\"'+str(vis)+'\", asdm=\"'+str(asdm)+'\", datacolumn=\"'+str(datacolumn)+'\",' 

48 casalog.post(parsummary) 

49 parsummary = 'archiveid=\"'+str(archiveid)+'\", rangeid=\"'+str(rangeid)+'\", subscanduration=\"'+str(subscanduration)+'\",' 

50 casalog.post(parsummary) 

51 parsummary = 'sbduration=\"'+str(sbduration)+'\", apcorrected='+str(apcorrected)+', verbose='+str(verbose)+',' 

52 casalog.post(parsummary) 

53 

54 if not (type(vis)==str) or not (os.path.exists(vis)): 

55 raise ValueError('Visibility data set not found - please verify the name') 

56 

57 if (asdm == ""): 

58 raise ValueError("Must provide output data set name in parameter asdm.") 

59 

60 if os.path.exists(asdm): 

61 raise ValueError("Output ASDM %s already exists - will not overwrite." % asdm) 

62 

63 # determine sb and subscan duration 

64 ssdur_secs = 24.*3600 # default is one day, i.e. there will be only one subscan per scan 

65 if not(subscanduration==""): 

66 if (_qa.canonical(subscanduration)['unit'].find('s') < 0): 

67 raise TypeError("subscanduration is not a valid time quantity: %s" % subscanduration) 

68 else: 

69 ssdur_secs = _qa.canonical(subscanduration)['value'] 

70 

71 sbdur_secs = 2700. # default is 45 minutes 

72 if not(sbduration==""): 

73 if (_qa.canonical(sbduration)['unit'].find('s') < 0): 

74 raise TypeError("sbduration is not a valid time quantity: %s" % sbduration) 

75 else: 

76 sbdur_secs = _qa.canonical(sbduration)['value'] 

77 

78 # create timesorted copy of the input ms 

79 tsortvis = vis+'-tsorted' 

80 os.system('rm -rf '+tsortvis) 

81 _ms.open(vis) 

82 _ms.timesort(tsortvis) 

83 _ms.close() 

84 

85 # Prepare for actual exportasdm 

86 casalog.post("Checking timesorted MS for potential problems ... ") 

87 _tb.open(tsortvis) 

88 a = _tb.getcol('PROCESSOR_ID') 

89 a0 = a[0] 

90 candoit = True 

91 for i in range(0,len(a)-1): 

92 if(a[i]!=a[i+1]): 

93 candoit = False 

94 break 

95 _tb.close() 

96 

97 if candoit: 

98 casalog.post(" Checking if PROCESSOR and MAIN need modifications ...") 

99 _tb.open(tsortvis+'/PROCESSOR') 

100 nprocrows = _tb.nrows() 

101 _tb.close() 

102 if ((nprocrows>0) and (a0>-1)): 

103 _tb.open(tsortvis+'/PROCESSOR') 

104 therow = _tb.nrows()-1 

105 mode0 = _tb.getcell('MODE_ID',a0) 

106 _tb.close() 

107 offset = 1 

108 if nprocrows>1: 

109 casalog.post(" Modifying PROCESSOR subtable ...") 

110 while (nprocrows>1 and therow>0): 

111 _tb.open(tsortvis+'/PROCESSOR', nomodify=False) 

112 therow = _tb.nrows()-offset 

113 if(_tb.getcell('MODE_ID',therow)!=mode0): 

114 _tb.removerows([therow]) 

115 else: 

116 offset += 1 

117 nprocrows = _tb.nrows() 

118 casalog.post("... done.") 

119 

120 casalog.post(" Modifying processor ids in main table ...") 

121 a = a - a # set all precessor ids to zero 

122 _tb.open(tsortvis, nomodify=False) 

123 _tb.putcol('PROCESSOR_ID', a) 

124 _tb.close() 

125 casalog.post(" ... done.") 

126 else: 

127 casalog.post(" No modifications to proc id in PROCESSOR and MAIN necessary.") 

128 casalog.post(" Checking if SYSCAL needs modifications ...") 

129 if(os.path.exists(tsortvis+'/SYSCAL')): 

130 for cname in ['TANT_SPECTRUM', 

131 'TSYS_SPECTRUM', 

132 'TANT_TSYS_SPECTRUM', 

133 'TCAL_SPECTRUM', 

134 'TRX_SPECTRUM', 

135 'TSKY_SPECTRUM', 

136 'PHASE_DIFF_SPECTRUM']: 

137 _tb.open(tsortvis+'/SYSCAL', nomodify=False) 

138 if(cname in _tb.colnames()): 

139 cdesc = _tb.getcoldesc(cname) 

140 if 'ndim' in cdesc and (cdesc['ndim']==-1): 

141 _tb.removecols([cname]) 

142 casalog.post(' Removed empty array column '+cname+' from table SYSCAL.') 

143 _tb.close() 

144 

145 casalog.post(" Checking if OBSERVATION needs modifications ...") 

146 _tb.open(tsortvis+'/OBSERVATION') 

147 nobsrows = _tb.nrows() 

148 _tb.close() 

149 if(nobsrows>0): 

150 _tb.open(tsortvis+'/OBSERVATION', nomodify=False) 

151 cdesc = _tb.getcoldesc('LOG') 

152 if 'ndim' in cdesc and (cdesc['ndim']>0): 

153 b = _tb.getvarcol('LOG') 

154 if not (type(b['r1'])==bool): 

155 kys = b.keys() 

156 modified = False 

157 b2 = [] 

158 for i in range(0,len(kys)): 

159 k = 'r'+str(i+1) 

160 if (b[k][0] == [''])[0]: 

161 b[k][0] = ["-"] 

162 modified = True 

163 b2.append([b[k][0][0]]) 

164 if modified: 

165 _tb.putcol('LOG',b2) 

166 casalog.post(" Modified log column in OBSERVATION table.") 

167 _tb.close() 

168 casalog.post("Done.") 

169 else: 

170 raise RuntimeError("More than one processor id in use in the main table. Cannot proceed.") 

171 

172 # sdm tool 

173 _sdm = sdm(asdm) 

174 rval = _sdm.fromms(tsortvis, datacolumn, archiveid, rangeid, ssdur_secs, sbdur_secs, apcorrected, verbose) 

175 # this line is independent of CASA version, but is here so that the CASA5 version can do additional error reporting after cleaning up this temporary MS 

176 os.system('rm -rf '+tsortvis) 

177 if not rval: 

178 raise RuntimeError('The sdm tool method fromms failed')