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

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

New Development: Yes

JIRA Issue: Yes (CAS-1429)

Ready to Release: Yes

Interface Changes: Yes

What Interface Changed:
A new python method, selector.set_rows(rownrs=[]),
and a cpp function,
STSelector::setRows( const std::vector< int >& rows ),
are added.
The cpp function can be called with selector._setrows()
from python scripts.

Test Programs:

# @casa
s=sd.scantable('ORIGINAL_TABLE_NAME', False)
sel=sd.selector()
rownrs=[1,3,5,7]
sel.set_rows(rownrs)
s.set_selection(sel)
s2=s.copy()
s2.save(name='NEW_TABLE_NAME',format='ASAP',overwrite=True)

Put in Release Notes: Yes

Module(s):

Description:

Data selection by a list of row numbers can be done by
the new method selector.set_rows.

File size: 7.0 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    def __init(self):
10        _selector.__init__(self)
11
12    def reset(self):
13        """
14        Unset all selections.
15        """
16        self._reset()
17
18    def is_empty(self):
19        """
20        Has anything been set?
21        """
22        return self._empty()
23
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()
37
38        """
39        vec = _to_list(pols, str) or _to_list(pols, int)
40        if isinstance(vec, list): # is an empty and/or valid vector
41            if len(vec) and isinstance(vec[-1],str):
42                self._setpolstrings(vec)
43                return
44            self._setpols(vec)
45        else:
46            raise TypeError('Unknown pol type. Please use [0,1...] or ["XX","YY"...]')
47   
48    # for the americans
49    set_polarizations = set_polarisations
50    # for the lazy
51    set_pols = set_polarisations
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)
60        if isinstance(vec,list):
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)
72        if isinstance(vec,list):
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)
84        if isinstance(vec,list):
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)
96        if isinstance(vec,list):
97            self._setcycles(vec)
98        else:
99            raise TypeError('Unknown Cycle number type. Use lists of integers.')
100
101
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):
112            self._setname(name)
113        else:
114            raise TypeError('name must be a string')
115
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)
125
126        """
127        taql =  "SELECT FROM $1 WHERE TSYS[0] >= %f" % (tsysmin)
128        if isinstance(tsysmax, float):
129            taql = taql + " AND TSYS[0] <= %f" % ( tsysmax)
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)
141
142    def set_order(self, order):
143        """
144        Set the order the scantable should be sorted by.
145        Parameters:
146            order:    The list of column names to sort by in order
147        """
148        self._setorder(order)
149
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
164    def get_scans(self):
165        return list(self._getscans())
166    def get_cycles(self):
167        return list(self._getcycles())
168    def get_beams(self):
169        return list(self._getbeams())
170    def get_ifs(self):
171        return list(self._getifs())
172    def get_pols(self):
173        return list(self._getpols())
174    def get_poltypes(self):
175        return list(self._getpoltypes())
176    def get_order(self):
177        return list(self._getorder())
178    def get_query(self):
179        prefix = "SELECT FROM $1 WHERE "
180        return self._gettaql().replace(prefix, "")
181
182    def get_name(self):
183        print "NYI"
184        s = self._gettaql()
185        return
186    def __str__(self):
187        out = ""
188        d = {"SCANNO": self.get_scans(),
189             "CYCLENO": self.get_cycles(),
190             "BEAMNO": self.get_beams(),
191             "IFNO": self.get_ifs(),
192             "Pol Type": self.get_poltypes(),
193             "POLNO": self.get_pols(),
194             "QUERY": self.get_query(),
195             "Sort Order": self.get_order()
196             }
197        for k,v in d.iteritems():
198            if v:
199                out += "%s: %s\n" % (k, v)
200        if len(out):
201            return out[:-1]
202        else:
203            return out
204    def __add__(self, other):
205        """
206        Merge two selections.
207        """
208        union = selector()
209        gets = [[self._getscans(), other._getscans(), union._setscans],
210                [self._getcycles(), other._getcycles(),union._setcycles],
211                [self._getbeams(), other._getbeams(), union._setbeams],
212                [self._getifs(), other._getifs(), union._setifs],
213                [self._getpols(), other._getpols(), union._setpols]]
214        for v in gets:
215            vec = list(v[0]+v[1])
216            vec.sort()
217            v[2](unique(vec))
218        q = other.get_query()
219        qs = self.get_query()
220        if len(q) and len(qs):
221            union.set_query(qs +" AND " + q)
222        else:
223            if len(q):
224                union.set_query(q)
225            elif len(qs):
226                union.set_query(qs)
227        return union
Note: See TracBrowser for help on using the repository browser.