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

Last change on this file since 1693 was 1693, checked in by Takeshi Nakazato, 14 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
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 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
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())
190    def get_types(self):
191        return list(self._gettypes())
192    def get_query(self):
193        prefix = "SELECT FROM $1 WHERE "
194        return self._gettaql().replace(prefix, "")
195
196    def get_name(self):
197        print "NYI"
198        s = self._gettaql()
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
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))
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)
241        return union
Note: See TracBrowser for help on using the repository browser.