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