source: trunk/python/selector.py@ 1462

Last change on this file since 1462 was 1349, checked in by mar637, 18 years ago

added american spelling alias
fixed concatenation of queries.

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