source: branches/newfiller/python/selector.py@ 2644

Last change on this file since 2644 was 1757, checked in by Kana Sugimoto, 15 years ago

New Development: Yes

JIRA Issue: Yes (CAS-2211)

Ready for Test: Yes

Interface Changes: Yes

What Interface Changed: ASAP 3.0.0 interface changes

Test Programs:

Put in Release Notes: Yes

Module(s): all the CASA sd tools and tasks are affected.

Description: Merged ATNF-ASAP 3.0.0 developments to CASA (alma) branch.

Note you also need to update casa/code/atnf.


File size: 8.1 KB
Line 
1from asap._asap import selector as _selector
2from asap import unique, _to_list
3
4class selector(_selector):
5 """
6 A selection object to be applied to scantables to restrict the
7 scantables to specific rows.
8 """
9 fields = ["pols", "ifs", "beams", "scans", "cycles", "name", "query"]
10
11 def __init__(self, *args, **kw):
12 if len(args) == 1:
13 if isinstance(args[0], self.__class__) \
14 or isinstance(args[0], _selector):
15 _selector.__init__(self, args[0])
16 else:
17 raise TypeError("Argument can only be a selector object")
18 else:
19 _selector.__init__(self)
20 for k,v in kw.items():
21 if k in self.fields:
22 func = getattr(self, "set_%s" % k)
23 func(v)
24
25 def reset(self):
26 """
27 Unset all selections.
28 """
29 self._reset()
30
31 def is_empty(self):
32 """
33 Has anything been set?
34 """
35 return self._empty()
36
37 def set_polarisations(self, pols=[]):
38 """
39 Set the polarisations to be selected in the scantable.
40 Parameters:
41 pols: a list of integers of 0-3, or strings, e.g ["I","Q"].
42 Default [] is no selection
43 Example:
44 sel = selector()
45 # These are equivalent if data is 'linear'
46 sel.set_polarisations(["XX","Re(XY)"])
47 sel.set_polarisations([0,2])
48 # reset the polarisation selection
49 sel.set_polarisations()
50
51 """
52 vec = _to_list(pols, str) or _to_list(pols, int)
53 if isinstance(vec, list): # is an empty and/or valid vector
54 if len(vec) and isinstance(vec[-1],str):
55 self._setpolstrings(vec)
56 return
57 self._setpols(vec)
58 else:
59 raise TypeError('Unknown pol type. Please use [0,1...] or ["XX","YY"...]')
60
61 # for the americans
62 set_polarizations = set_polarisations
63 # for the lazy
64 set_pols = set_polarisations
65
66 def set_ifs(self, ifs=[]):
67 """
68 Set a sequence of IF numbers (0-based).
69 Parameters:
70 ifs: a list of integers. Default [] is to unset the selection.
71 """
72 vec = _to_list(ifs, int)
73 if isinstance(vec,list):
74 self._setifs(vec)
75 else:
76 raise TypeError('Unknown IFno type. Use lists of integers.')
77
78 def set_scans(self, scans=[]):
79 """
80 Set a sequence of Scan numbers (0-based).
81 Parameters:
82 scans: a list of integers. Default [] is to unset the selection.
83 """
84 vec = _to_list(scans, int)
85 if isinstance(vec,list):
86 self._setscans(vec)
87 else:
88 raise TypeError('Unknown Scan number type. Use lists of integers.')
89
90 def set_beams(self, beams=[]):
91 """
92 Set a sequence of Beam numbers (0-based).
93 Parameters:
94 beams: a list of integers. Default [] is to unset the selection.
95 """
96 vec = _to_list(beams, int)
97 if isinstance(vec,list):
98 self._setbeams(vec)
99 else:
100 raise TypeError('Unknown Beam number type. Use lists of integers.')
101
102 def set_cycles(self, cycles=[]):
103 """
104 Set a sequence of IF numbers (0-based).
105 Parameters:
106 cycless: a list of integers. Default [] is to unset the selection.
107 """
108 vec = _to_list(cycles, int)
109 if isinstance(vec,list):
110 self._setcycles(vec)
111 else:
112 raise TypeError('Unknown Cycle number type. Use lists of integers.')
113
114
115 def set_name(self, name):
116 """
117 Set a selection based on a name. This can be a unix pattern , e.g. "*_R"
118 Parameters:
119 name: a string containing a source name or pattern
120 Examples:
121 # select all reference scans which start with "Orion"
122 selection.set_name("Orion*_R")
123 """
124 if isinstance(name, str):
125 self._setname(name)
126 else:
127 raise TypeError('name must be a string')
128
129 def set_tsys(self, tsysmin=0.0, tsysmax=None):
130 """
131 Select by Tsys range.
132 Parameters:
133 tsysmin: the lower threshold. Default 0.0
134 tsysmax: the upper threshold. Default None.
135 Examples:
136 # select all spectra with Tsys <= 500.0
137 selection.set_tsys(tsysmax=500.0)
138
139 """
140 taql = "SELECT FROM $1 WHERE TSYS[0] >= %f" % (tsysmin)
141 if isinstance(tsysmax, float):
142 taql = taql + " AND TSYS[0] <= %f" % ( tsysmax)
143 self._settaql(taql)
144
145 def set_query(self, query):
146 """
147 Select by Column query. Power users only!
148 Example:
149 # select all off scans with integration times over 60 seconds.
150 selection.set_query("SRCTYPE == 1 AND INTERVAL > 60.0")
151 """
152 taql = "SELECT FROM $1 WHERE " + query
153 self._settaql(taql)
154
155 def set_order(self, order):
156 """
157 Set the order the scantable should be sorted by.
158 Parameters:
159 order: The list of column names to sort by in order
160 """
161 self._setorder(order)
162
163 def set_rows(self, rows=[]):
164 """
165 Set a sequence of row numbers (0-based). Power users Only!
166 NOTICE row numbers can be changed easily by sorting,
167 prior selection, etc.
168 Parameters:
169 rows: a list of integers. Default [] is to unset the selection.
170 """
171 vec = _to_list(rows, int)
172 if isinstance(vec,list):
173 self._setrows(vec)
174 else:
175 raise TypeError('Unknown row number type. Use lists of integers.')
176
177 def set_types(self, types=[]):
178 """
179 Set a sequence of source types.
180 Parameters:
181 types: a list of integers. Default [] is to unset the selection.
182 """
183 vec = _to_list(types, int)
184 if isinstance(vec,list):
185 self._settypes(vec)
186 else:
187 raise TypeError('Unknown row number type. Use lists of integers.')
188
189 def get_scans(self):
190 return list(self._getscans())
191 def get_cycles(self):
192 return list(self._getcycles())
193 def get_beams(self):
194 return list(self._getbeams())
195 def get_ifs(self):
196 return list(self._getifs())
197 def get_pols(self):
198 return list(self._getpols())
199 def get_poltypes(self):
200 return list(self._getpoltypes())
201 def get_order(self):
202 return list(self._getorder())
203 def get_types(self):
204 return list(self._gettypes())
205 def get_query(self):
206 prefix = "SELECT FROM $1 WHERE "
207 return self._gettaql().replace(prefix, "")
208
209 def get_name(self):
210 print "NYI"
211 s = self._gettaql()
212 return
213 def __str__(self):
214 out = ""
215 d = {"SCANNO": self.get_scans(),
216 "CYCLENO": self.get_cycles(),
217 "BEAMNO": self.get_beams(),
218 "IFNO": self.get_ifs(),
219 "Pol Type": self.get_poltypes(),
220 "POLNO": self.get_pols(),
221 "QUERY": self.get_query(),
222 "Sort Order": self.get_order()
223 }
224 for k,v in d.iteritems():
225 if v:
226 out += "%s: %s\n" % (k, v)
227 if len(out):
228 return out[:-1]
229 else:
230 return out
231 def __add__(self, other):
232 """
233 Merge two selections.
234 """
235 if self.is_empty():
236 return other
237 elif other.is_empty():
238 return self
239 union = selector()
240 gets = [[self._getscans(), other._getscans(), union._setscans],
241 [self._getcycles(), other._getcycles(),union._setcycles],
242 [self._getbeams(), other._getbeams(), union._setbeams],
243 [self._getifs(), other._getifs(), union._setifs],
244 [self._getpols(), other._getpols(), union._setpols]]
245 for v in gets:
246 vec = list(v[0]+v[1])
247 vec.sort()
248 v[2](unique(vec))
249 q = other.get_query()
250 qs = self.get_query()
251 if len(q) and len(qs):
252 union.set_query(qs +" AND " + q)
253 else:
254 if len(q):
255 union.set_query(q)
256 elif len(qs):
257 union.set_query(qs)
258 return union
Note: See TracBrowser for help on using the repository browser.