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

90 statements  

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

1import os 

2import numpy as np 

3 

4from casatasks import casalog 

5from casatools import ms 

6from . import partitionhelper as ph 

7from . import flaghelper as fh 

8 

9 

10def listpartition(vis=None, createdict=None, listfile=None): 

11 

12 """List information about an MMS data set in the logger: 

13 

14 Keyword arguments: 

15 vis -- Name of multi-MS or normal MS. 

16 default: none. example: vis='uidA002.mms' 

17 createdict -- Create and return a dictionary with information about 

18 the sub-MSs. 

19 default: False 

20 listfile -- save the output to a file 

21 default: ''. Example: listfile="mylist.txt" 

22  

23 The task also returns a dictionary with scan summary information 

24 for each sub-MS.  

25  

26 

27 """ 

28 

29 casalog.origin('listpartition') 

30 

31 mslocal = ms() 

32 ffout = None 

33 

34 try: 

35 if (type(vis) == str) & os.path.exists(vis): 

36 mslocal.open(thems=vis) 

37 else: 

38 raise Exception('Visibility data set not found - please verify the name') 

39 

40 # Check output filename existence  

41 if listfile != '': 

42 if (type(listfile) == str) & os.path.exists(listfile): 

43 raise Exception('Output file \'%s\' already exists'%listfile) 

44 

45 casalog.post('Will save output to \'%s\''%listfile) 

46 ffout = open(listfile, 'w') 

47 

48 # Is it a multi-MS? 

49 ismms = mslocal.ismultims() 

50 

51 # List of MSs to process 

52 mslist = [] 

53 

54 # It is a multi-MS 

55 if ismms: 

56 mslist = mslocal.getreferencedtables() 

57 mslist.sort() 

58 sname = 'Sub-MS' 

59 

60 # Get the AxisType of the MMS 

61 axis = ph.axisType(vis) 

62 if axis == '': 

63 axis = 'unknown' 

64 

65 casalog.post('This is a Multi-MS with separation axis = '+axis) 

66 

67 else: 

68 mslist.append(vis) 

69 sname = 'MS' 

70 

71 # Close top MS 

72 mslocal.close() 

73 

74 # Get a consolidated dictionary with scan, spw, channel information 

75 # of the list of subMSs. It adds the nrows of all sub-scans of a scan. 

76 outdict = {} 

77 outdict = ph.getScanSpwSummary(mslist) 

78 

79 # Now loop through the dictionary to print the information 

80 if outdict.keys() == []: 

81 casalog.post('Error in processing dictionaries','ERROR') 

82 

83 indices = list(outdict.keys()) 

84 indices.sort() 

85 

86 counter = 0 

87 for index in indices: 

88 

89 # Get data 

90 MS = outdict[index]['MS'] 

91 SIZE = outdict[index]['size'] 

92 SCAN = outdict[index]['scanId'] 

93 

94 # Sort scans for more optimal printing 

95 # Print information per scan 

96 firstscan = True 

97 skeys = list(SCAN.keys()) 

98 skeys.sort() 

99 for myscan in skeys: 

100 SPW = outdict[index]['scanId'][myscan]['spwIds'] 

101 NCHAN = outdict[index]['scanId'][myscan]['nchans'] 

102 NROWS = outdict[index]['scanId'][myscan]['nrows'] 

103 

104 # Get maximum widths 

105 smxw = getWidth(outdict, 'spw') 

106 cmxw = getWidth(outdict, 'channel') 

107 

108 # Create format 

109 fhdr = '%-'+str(len(MS)+2)+'s' + '%-6s' + '%-'+str(smxw+2)+'s' + \ 

110 '%-'+str(cmxw+2)+'s' + '%-8s' + '%-6s' 

111 

112 

113 # Print header 

114 text = '' 

115 if counter == 0: 

116 text = text + fhdr % (sname,'Scan','Spw','Nchan','Nrows','Size') 

117 text = text + '\n' 

118 counter += 1 

119 

120 # Print first scan 

121 if firstscan: 

122 text = text + fhdr % (MS, myscan, SPW, NCHAN, NROWS, SIZE) 

123 else: 

124 text = text + fhdr % ('', myscan, SPW, NCHAN, NROWS, '') 

125 

126 firstscan = False 

127 

128 # Print to a file 

129 if ffout is not None: 

130 ffout.write(text + '\n') 

131 else: 

132 # Print to the logger 

133 casalog.post(text) 

134 

135 # Return the scan dictionary 

136 if createdict: 

137 return outdict 

138 else: 

139 return {} 

140 

141 finally: 

142 if ffout is not None: 

143 ffout.close( ) 

144 

145def getWidth(adict, par): 

146 

147 width = 0 

148 for aa in adict.keys(): 

149 scans = adict[aa]['scanId'].keys() 

150 for bb in scans: 

151 if par == 'spw': 

152 spws = adict[aa]['scanId'][bb]['spwIds'] 

153 mystr = str(spws) 

154 length = len(mystr) 

155 if length > width: 

156 width = length 

157 

158 elif par == 'channel': 

159 chans = adict[aa]['scanId'][bb]['nchans'] 

160 mystr = str(chans) 

161 length = len(mystr) 

162 if length > width: 

163 width = length 

164 if width < 5: 

165 width = 5 

166 

167 return width 

168