| 1 | from asap._asap import selector as _selector | 
|---|
| 2 | from asap import unique, _to_list | 
|---|
| 3 |  | 
|---|
| 4 | class 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 get_scans(self): | 
|---|
| 151 | return list(self._getscans()) | 
|---|
| 152 | def get_cycles(self): | 
|---|
| 153 | return list(self._getcycles()) | 
|---|
| 154 | def get_beams(self): | 
|---|
| 155 | return list(self._getbeams()) | 
|---|
| 156 | def get_ifs(self): | 
|---|
| 157 | return list(self._getifs()) | 
|---|
| 158 | def get_pols(self): | 
|---|
| 159 | return list(self._getpols()) | 
|---|
| 160 | def get_poltypes(self): | 
|---|
| 161 | return list(self._getpoltypes()) | 
|---|
| 162 | def get_order(self): | 
|---|
| 163 | return list(self._getorder()) | 
|---|
| 164 | def get_query(self): | 
|---|
| 165 | prefix = "SELECT FROM $1 WHERE " | 
|---|
| 166 | return self._gettaql().replace(prefix, "") | 
|---|
| 167 |  | 
|---|
| 168 | def get_name(self): | 
|---|
| 169 | print "NYI" | 
|---|
| 170 | s = self._gettaql() | 
|---|
| 171 | return | 
|---|
| 172 | def __str__(self): | 
|---|
| 173 | out = "" | 
|---|
| 174 | d = {"SCANNO": self.get_scans(), | 
|---|
| 175 | "CYCLENO": self.get_cycles(), | 
|---|
| 176 | "BEAMNO": self.get_beams(), | 
|---|
| 177 | "IFNO": self.get_ifs(), | 
|---|
| 178 | "Pol Type": self.get_poltypes(), | 
|---|
| 179 | "POLNO": self.get_pols(), | 
|---|
| 180 | "QUERY": self.get_query(), | 
|---|
| 181 | "Sort Order": self.get_order() | 
|---|
| 182 | } | 
|---|
| 183 | for k,v in d.iteritems(): | 
|---|
| 184 | if v: | 
|---|
| 185 | out += "%s: %s\n" % (k, v) | 
|---|
| 186 | if len(out): | 
|---|
| 187 | return out[:-1] | 
|---|
| 188 | else: | 
|---|
| 189 | return out | 
|---|
| 190 | def __add__(self, other): | 
|---|
| 191 | """ | 
|---|
| 192 | Merge two selections. | 
|---|
| 193 | """ | 
|---|
| 194 | union = selector() | 
|---|
| 195 | gets = [[self._getscans(), other._getscans(), union._setscans], | 
|---|
| 196 | [self._getcycles(), other._getcycles(),union._setcycles], | 
|---|
| 197 | [self._getbeams(), other._getbeams(), union._setbeams], | 
|---|
| 198 | [self._getifs(), other._getifs(), union._setifs], | 
|---|
| 199 | [self._getpols(), other._getpols(), union._setpols]] | 
|---|
| 200 | for v in gets: | 
|---|
| 201 | vec = list(v[0]+v[1]) | 
|---|
| 202 | vec.sort() | 
|---|
| 203 | v[2](unique(vec)) | 
|---|
| 204 | q = other.get_query() | 
|---|
| 205 | qs = self.get_query() | 
|---|
| 206 | if len(q) and len(qs): | 
|---|
| 207 | union.set_query(qs +" AND " + q) | 
|---|
| 208 | else: | 
|---|
| 209 | if len(q): | 
|---|
| 210 | union.set_query(q) | 
|---|
| 211 | elif len(qs): | 
|---|
| 212 | union.set_query(qs) | 
|---|
| 213 | return union | 
|---|