source: trunk/python/selector.py @ 1337

Last change on this file since 1337 was 1337, checked in by mar637, 17 years ago

improvements to the selector. + operator now also 'AND's the query string. Added str function

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