source: branches/alma/python/selector.py@ 1726

Last change on this file since 1726 was 1693, checked in by Takeshi Nakazato, 15 years ago

New Development: No

JIRA Issue: Yes CAS-1908

Ready to Release: Yes

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes

Module(s): Module Names change impacts.

Description: Describe your changes here...

Changed a tagging as the source type is tagged by using SRCTYPE, not
an extra string in SRCNAME. To do this, I have defined a selection method
by SRCTYPE in STSelector class. I have newly added python_SrcType.cpp
that defines a Python interface of SrcType enums which is defined
in atnf/PKSIO/SrcType.h.

Since I have added new file in the src directory, I have modified src/Makefile
to compile new file.

File size: 7.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
[1603]49 set_polarizations = set_polarisations
50 # for the lazy
51 set_pols = set_polarisations
[932]52
53 def set_ifs(self, ifs=[]):
54 """
55 Set a sequence of IF numbers (0-based).
56 Parameters:
57 ifs: a list of integers. Default [] is to unset the selection.
58 """
59 vec = _to_list(ifs, int)
[1045]60 if isinstance(vec,list):
[932]61 self._setifs(vec)
62 else:
63 raise TypeError('Unknown IFno type. Use lists of integers.')
64
65 def set_scans(self, scans=[]):
66 """
67 Set a sequence of Scan numbers (0-based).
68 Parameters:
69 scans: a list of integers. Default [] is to unset the selection.
70 """
71 vec = _to_list(scans, int)
[1045]72 if isinstance(vec,list):
[932]73 self._setscans(vec)
74 else:
75 raise TypeError('Unknown Scan number type. Use lists of integers.')
76
77 def set_beams(self, beams=[]):
78 """
79 Set a sequence of Beam numbers (0-based).
80 Parameters:
81 beams: a list of integers. Default [] is to unset the selection.
82 """
83 vec = _to_list(beams, int)
[1045]84 if isinstance(vec,list):
[932]85 self._setbeams(vec)
86 else:
87 raise TypeError('Unknown Beam number type. Use lists of integers.')
88
89 def set_cycles(self, cycles=[]):
90 """
91 Set a sequence of IF numbers (0-based).
92 Parameters:
93 cycless: a list of integers. Default [] is to unset the selection.
94 """
95 vec = _to_list(cycles, int)
[1045]96 if isinstance(vec,list):
[932]97 self._setcycles(vec)
98 else:
99 raise TypeError('Unknown Cycle number type. Use lists of integers.')
100
[948]101
[932]102 def set_name(self, name):
103 """
104 Set a selection based on a name. This can be a unix pattern , e.g. "*_R"
105 Parameters:
106 name: a string containing a source name or pattern
107 Examples:
108 # select all reference scans which start with "Orion"
109 selection.set_name("Orion*_R")
110 """
111 if isinstance(name, str):
[952]112 self._setname(name)
[932]113 else:
114 raise TypeError('name must be a string')
[948]115
[932]116 def set_tsys(self, tsysmin=0.0, tsysmax=None):
117 """
118 Select by Tsys range.
119 Parameters:
120 tsysmin: the lower threshold. Default 0.0
121 tsysmax: the upper threshold. Default None.
122 Examples:
123 # select all spectra with Tsys <= 500.0
124 selection.set_tsys(tsysmax=500.0)
[948]125
[932]126 """
[963]127 taql = "SELECT FROM $1 WHERE TSYS[0] >= %f" % (tsysmin)
[932]128 if isinstance(tsysmax, float):
[963]129 taql = taql + " AND TSYS[0] <= %f" % ( tsysmax)
[932]130 self._settaql(taql)
131
132 def set_query(self, query):
133 """
134 Select by Column query. Power users only!
135 Example:
136 # select all off scans with integration times over 60 seconds.
137 selection.set_query("SRCTYPE == 1 AND INTERVAL > 60.0")
138 """
139 taql = "SELECT FROM $1 WHERE " + query
140 self._settaql(taql)
[948]141
142 def set_order(self, order):
143 """
144 Set the order the scantable should be sorted by.
145 Parameters:
[1045]146 order: The list of column names to sort by in order
[948]147 """
148 self._setorder(order)
149
[1639]150 def set_rows(self, rows=[]):
151 """
152 Set a sequence of row numbers (0-based). Power users Only!
153 NOTICE row numbers can be changed easily by sorting,
154 prior selection, etc.
155 Parameters:
156 rows: a list of integers. Default [] is to unset the selection.
157 """
158 vec = _to_list(rows, int)
159 if isinstance(vec,list):
160 self._setrows(vec)
161 else:
162 raise TypeError('Unknown row number type. Use lists of integers.')
163
[1693]164 def set_types(self, types=[]):
165 """
166 Set a sequence of source types.
167 Parameters:
168 types: a list of integers. Default [] is to unset the selection.
169 """
170 vec = _to_list(types, int)
171 if isinstance(vec,list):
172 self._settypes(vec)
173 else:
174 raise TypeError('Unknown row number type. Use lists of integers.')
175
[948]176 def get_scans(self):
177 return list(self._getscans())
178 def get_cycles(self):
179 return list(self._getcycles())
180 def get_beams(self):
181 return list(self._getbeams())
182 def get_ifs(self):
183 return list(self._getifs())
184 def get_pols(self):
185 return list(self._getpols())
186 def get_poltypes(self):
187 return list(self._getpoltypes())
188 def get_order(self):
189 return list(self._getorder())
[1693]190 def get_types(self):
191 return list(self._gettypes())
[1337]192 def get_query(self):
193 prefix = "SELECT FROM $1 WHERE "
194 return self._gettaql().replace(prefix, "")
[1603]195
[948]196 def get_name(self):
197 print "NYI"
198 s = self._gettaql()
[1337]199 return
200 def __str__(self):
201 out = ""
202 d = {"SCANNO": self.get_scans(),
203 "CYCLENO": self.get_cycles(),
204 "BEAMNO": self.get_beams(),
205 "IFNO": self.get_ifs(),
206 "Pol Type": self.get_poltypes(),
207 "POLNO": self.get_pols(),
208 "QUERY": self.get_query(),
209 "Sort Order": self.get_order()
210 }
211 for k,v in d.iteritems():
212 if v:
213 out += "%s: %s\n" % (k, v)
214 if len(out):
215 return out[:-1]
216 else:
217 return out
[948]218 def __add__(self, other):
219 """
220 Merge two selections.
221 """
222 union = selector()
223 gets = [[self._getscans(), other._getscans(), union._setscans],
224 [self._getcycles(), other._getcycles(),union._setcycles],
225 [self._getbeams(), other._getbeams(), union._setbeams],
226 [self._getifs(), other._getifs(), union._setifs],
227 [self._getpols(), other._getpols(), union._setpols]]
228 for v in gets:
229 vec = list(v[0]+v[1])
230 vec.sort()
231 v[2](unique(vec))
[1349]232 q = other.get_query()
233 qs = self.get_query()
234 if len(q) and len(qs):
235 union.set_query(qs +" AND " + q)
236 else:
237 if len(q):
238 union.set_query(q)
239 elif len(qs):
240 union.set_query(qs)
[948]241 return union
Note: See TracBrowser for help on using the repository browser.