[876] | 1 | from asap._asap import Scantable |
---|
[226] | 2 | from asap import rcParams |
---|
[1118] | 3 | from asap import print_log |
---|
| 4 | from asap import asaplog |
---|
[946] | 5 | from asap import selector |
---|
[1153] | 6 | from asap import linecatalog |
---|
[1295] | 7 | from asap import _n_bools, mask_not, mask_and, mask_or |
---|
[102] | 8 | |
---|
[876] | 9 | class scantable(Scantable): |
---|
[102] | 10 | """ |
---|
| 11 | The ASAP container for scans |
---|
| 12 | """ |
---|
[710] | 13 | |
---|
[1496] | 14 | def __init__(self, filename, average=None, unit=None, getpt=None): |
---|
[102] | 15 | """ |
---|
| 16 | Create a scantable from a saved one or make a reference |
---|
| 17 | Parameters: |
---|
[181] | 18 | filename: the name of an asap table on disk |
---|
| 19 | or |
---|
| 20 | the name of a rpfits/sdfits/ms file |
---|
| 21 | (integrations within scans are auto averaged |
---|
| 22 | and the whole file is read) |
---|
| 23 | or |
---|
| 24 | [advanced] a reference to an existing |
---|
[102] | 25 | scantable |
---|
[484] | 26 | average: average all integrations withinb a scan on read. |
---|
| 27 | The default (True) is taken from .asaprc. |
---|
| 28 | unit: brightness unit; must be consistent with K or Jy. |
---|
[340] | 29 | Over-rides the default selected by the reader |
---|
| 30 | (input rpfits/sdfits/ms) or replaces the value |
---|
| 31 | in existing scantables |
---|
[710] | 32 | """ |
---|
[976] | 33 | if average is None: |
---|
[710] | 34 | average = rcParams['scantable.autoaverage'] |
---|
[1496] | 35 | if getpt is None: |
---|
| 36 | getpt = False |
---|
[1259] | 37 | varlist = vars() |
---|
[876] | 38 | from asap._asap import stmath |
---|
| 39 | self._math = stmath() |
---|
| 40 | if isinstance(filename, Scantable): |
---|
| 41 | Scantable.__init__(self, filename) |
---|
[181] | 42 | else: |
---|
[1118] | 43 | if isinstance(filename, str): |
---|
[976] | 44 | import os.path |
---|
| 45 | filename = os.path.expandvars(filename) |
---|
| 46 | filename = os.path.expanduser(filename) |
---|
| 47 | if not os.path.exists(filename): |
---|
| 48 | s = "File '%s' not found." % (filename) |
---|
[718] | 49 | if rcParams['verbose']: |
---|
[976] | 50 | asaplog.push(s) |
---|
| 51 | print asaplog.pop().strip() |
---|
[718] | 52 | return |
---|
[976] | 53 | raise IOError(s) |
---|
[1115] | 54 | if os.path.isdir(filename) \ |
---|
[1118] | 55 | and not os.path.exists(filename+'/table.f1'): |
---|
[976] | 56 | # crude check if asap table |
---|
| 57 | if os.path.exists(filename+'/table.info'): |
---|
[1118] | 58 | ondisk = rcParams['scantable.storage'] == 'disk' |
---|
| 59 | Scantable.__init__(self, filename, ondisk) |
---|
[976] | 60 | if unit is not None: |
---|
| 61 | self.set_fluxunit(unit) |
---|
[1496] | 62 | # do not reset to the default freqframe |
---|
| 63 | #self.set_freqframe(rcParams['scantable.freqframe']) |
---|
[718] | 64 | else: |
---|
[1118] | 65 | msg = "The given file '%s'is not a valid " \ |
---|
| 66 | "asap table." % (filename) |
---|
[976] | 67 | if rcParams['verbose']: |
---|
| 68 | print msg |
---|
| 69 | return |
---|
| 70 | else: |
---|
| 71 | raise IOError(msg) |
---|
[226] | 72 | else: |
---|
[1496] | 73 | self._fill([filename], unit, average, getpt) |
---|
[1118] | 74 | elif (isinstance(filename, list) or isinstance(filename, tuple)) \ |
---|
[976] | 75 | and isinstance(filename[-1], str): |
---|
[1496] | 76 | self._fill(filename, unit, average, getpt) |
---|
[1259] | 77 | self._add_history("scantable", varlist) |
---|
[714] | 78 | print_log() |
---|
[102] | 79 | |
---|
[876] | 80 | def save(self, name=None, format=None, overwrite=False): |
---|
[116] | 81 | """ |
---|
[1280] | 82 | Store the scantable on disk. This can be an asap (aips++) Table, |
---|
| 83 | SDFITS or MS2 format. |
---|
[116] | 84 | Parameters: |
---|
[1093] | 85 | name: the name of the outputfile. For format "ASCII" |
---|
| 86 | this is the root file name (data in 'name'.txt |
---|
[497] | 87 | and header in 'name'_header.txt) |
---|
[116] | 88 | format: an optional file format. Default is ASAP. |
---|
[280] | 89 | Allowed are - 'ASAP' (save as ASAP [aips++] Table), |
---|
[194] | 90 | 'SDFITS' (save as SDFITS file) |
---|
[200] | 91 | 'ASCII' (saves as ascii text file) |
---|
[226] | 92 | 'MS2' (saves as an aips++ |
---|
| 93 | MeasurementSet V2) |
---|
[411] | 94 | overwrite: If the file should be overwritten if it exists. |
---|
[256] | 95 | The default False is to return with warning |
---|
[411] | 96 | without writing the output. USE WITH CARE. |
---|
[116] | 97 | Example: |
---|
| 98 | scan.save('myscan.asap') |
---|
[1118] | 99 | scan.save('myscan.sdfits', 'SDFITS') |
---|
[116] | 100 | """ |
---|
[411] | 101 | from os import path |
---|
[226] | 102 | if format is None: format = rcParams['scantable.save'] |
---|
[256] | 103 | suffix = '.'+format.lower() |
---|
[1118] | 104 | if name is None or name == "": |
---|
[256] | 105 | name = 'scantable'+suffix |
---|
[718] | 106 | msg = "No filename given. Using default name %s..." % name |
---|
| 107 | asaplog.push(msg) |
---|
[411] | 108 | name = path.expandvars(name) |
---|
[256] | 109 | if path.isfile(name) or path.isdir(name): |
---|
| 110 | if not overwrite: |
---|
[718] | 111 | msg = "File %s exists." % name |
---|
| 112 | if rcParams['verbose']: |
---|
| 113 | print msg |
---|
| 114 | return |
---|
| 115 | else: |
---|
| 116 | raise IOError(msg) |
---|
[451] | 117 | format2 = format.upper() |
---|
| 118 | if format2 == 'ASAP': |
---|
[116] | 119 | self._save(name) |
---|
| 120 | else: |
---|
[989] | 121 | from asap._asap import stwriter as stw |
---|
[1118] | 122 | writer = stw(format2) |
---|
| 123 | writer.write(self, name) |
---|
[718] | 124 | print_log() |
---|
[116] | 125 | return |
---|
| 126 | |
---|
[102] | 127 | def copy(self): |
---|
| 128 | """ |
---|
| 129 | Return a copy of this scantable. |
---|
[1348] | 130 | Note: |
---|
| 131 | This makes a full (deep) copy. scan2 = scan1 makes a reference. |
---|
[102] | 132 | Parameters: |
---|
[113] | 133 | none |
---|
[102] | 134 | Example: |
---|
| 135 | copiedscan = scan.copy() |
---|
| 136 | """ |
---|
[876] | 137 | sd = scantable(Scantable._copy(self)) |
---|
[113] | 138 | return sd |
---|
| 139 | |
---|
[1093] | 140 | def drop_scan(self, scanid=None): |
---|
| 141 | """ |
---|
| 142 | Return a new scantable where the specified scan number(s) has(have) |
---|
| 143 | been dropped. |
---|
| 144 | Parameters: |
---|
| 145 | scanid: a (list of) scan number(s) |
---|
| 146 | """ |
---|
| 147 | from asap import _is_sequence_or_number as _is_valid |
---|
| 148 | from asap import _to_list |
---|
| 149 | from asap import unique |
---|
| 150 | if not _is_valid(scanid): |
---|
| 151 | if rcParams['verbose']: |
---|
| 152 | print "Please specify a scanno to drop from the scantable" |
---|
| 153 | return |
---|
| 154 | else: |
---|
| 155 | raise RuntimeError("No scan given") |
---|
| 156 | try: |
---|
| 157 | scanid = _to_list(scanid) |
---|
| 158 | allscans = unique([ self.getscan(i) for i in range(self.nrow())]) |
---|
| 159 | for sid in scanid: allscans.remove(sid) |
---|
[1118] | 160 | if len(allscans) == 0: |
---|
| 161 | raise ValueError("Can't remove all scans") |
---|
[1093] | 162 | except ValueError: |
---|
| 163 | if rcParams['verbose']: |
---|
| 164 | print "Couldn't find any match." |
---|
| 165 | return |
---|
| 166 | else: raise |
---|
| 167 | try: |
---|
| 168 | bsel = self.get_selection() |
---|
| 169 | sel = selector() |
---|
| 170 | sel.set_scans(allscans) |
---|
| 171 | self.set_selection(bsel+sel) |
---|
| 172 | scopy = self._copy() |
---|
| 173 | self.set_selection(bsel) |
---|
| 174 | return scantable(scopy) |
---|
| 175 | except RuntimeError: |
---|
[1118] | 176 | if rcParams['verbose']: |
---|
| 177 | print "Couldn't find any match." |
---|
| 178 | else: |
---|
| 179 | raise |
---|
[1093] | 180 | |
---|
| 181 | |
---|
[102] | 182 | def get_scan(self, scanid=None): |
---|
| 183 | """ |
---|
| 184 | Return a specific scan (by scanno) or collection of scans (by |
---|
| 185 | source name) in a new scantable. |
---|
[1348] | 186 | Note: |
---|
| 187 | See scantable.drop_scan() for the inverse operation. |
---|
[102] | 188 | Parameters: |
---|
[513] | 189 | scanid: a (list of) scanno or a source name, unix-style |
---|
| 190 | patterns are accepted for source name matching, e.g. |
---|
| 191 | '*_R' gets all 'ref scans |
---|
[102] | 192 | Example: |
---|
[513] | 193 | # get all scans containing the source '323p459' |
---|
| 194 | newscan = scan.get_scan('323p459') |
---|
| 195 | # get all 'off' scans |
---|
| 196 | refscans = scan.get_scan('*_R') |
---|
| 197 | # get a susbset of scans by scanno (as listed in scan.summary()) |
---|
[1118] | 198 | newscan = scan.get_scan([0, 2, 7, 10]) |
---|
[102] | 199 | """ |
---|
| 200 | if scanid is None: |
---|
[718] | 201 | if rcParams['verbose']: |
---|
[1118] | 202 | print "Please specify a scan no or name to " \ |
---|
| 203 | "retrieve from the scantable" |
---|
[718] | 204 | return |
---|
| 205 | else: |
---|
| 206 | raise RuntimeError("No scan given") |
---|
| 207 | |
---|
[102] | 208 | try: |
---|
[946] | 209 | bsel = self.get_selection() |
---|
| 210 | sel = selector() |
---|
[102] | 211 | if type(scanid) is str: |
---|
[946] | 212 | sel.set_name(scanid) |
---|
| 213 | self.set_selection(bsel+sel) |
---|
[876] | 214 | scopy = self._copy() |
---|
[946] | 215 | self.set_selection(bsel) |
---|
[876] | 216 | return scantable(scopy) |
---|
[102] | 217 | elif type(scanid) is int: |
---|
[946] | 218 | sel.set_scans([scanid]) |
---|
| 219 | self.set_selection(bsel+sel) |
---|
[876] | 220 | scopy = self._copy() |
---|
[946] | 221 | self.set_selection(bsel) |
---|
[876] | 222 | return scantable(scopy) |
---|
[381] | 223 | elif type(scanid) is list: |
---|
[946] | 224 | sel.set_scans(scanid) |
---|
| 225 | self.set_selection(sel) |
---|
[876] | 226 | scopy = self._copy() |
---|
[946] | 227 | self.set_selection(bsel) |
---|
[876] | 228 | return scantable(scopy) |
---|
[381] | 229 | else: |
---|
[718] | 230 | msg = "Illegal scanid type, use 'int' or 'list' if ints." |
---|
| 231 | if rcParams['verbose']: |
---|
| 232 | print msg |
---|
| 233 | else: |
---|
| 234 | raise TypeError(msg) |
---|
[102] | 235 | except RuntimeError: |
---|
[718] | 236 | if rcParams['verbose']: print "Couldn't find any match." |
---|
| 237 | else: raise |
---|
[102] | 238 | |
---|
| 239 | def __str__(self): |
---|
[1118] | 240 | return Scantable._summary(self, True) |
---|
[102] | 241 | |
---|
[976] | 242 | def summary(self, filename=None): |
---|
[102] | 243 | """ |
---|
| 244 | Print a summary of the contents of this scantable. |
---|
| 245 | Parameters: |
---|
| 246 | filename: the name of a file to write the putput to |
---|
| 247 | Default - no file output |
---|
[381] | 248 | verbose: print extra info such as the frequency table |
---|
| 249 | The default (False) is taken from .asaprc |
---|
[102] | 250 | """ |
---|
[976] | 251 | info = Scantable._summary(self, True) |
---|
| 252 | #if verbose is None: verbose = rcParams['scantable.verbosesummary'] |
---|
[102] | 253 | if filename is not None: |
---|
[256] | 254 | if filename is "": |
---|
| 255 | filename = 'scantable_summary.txt' |
---|
[415] | 256 | from os.path import expandvars, isdir |
---|
[411] | 257 | filename = expandvars(filename) |
---|
[415] | 258 | if not isdir(filename): |
---|
[413] | 259 | data = open(filename, 'w') |
---|
| 260 | data.write(info) |
---|
| 261 | data.close() |
---|
| 262 | else: |
---|
[718] | 263 | msg = "Illegal file name '%s'." % (filename) |
---|
| 264 | if rcParams['verbose']: |
---|
| 265 | print msg |
---|
| 266 | else: |
---|
| 267 | raise IOError(msg) |
---|
| 268 | if rcParams['verbose']: |
---|
[794] | 269 | try: |
---|
| 270 | from IPython.genutils import page as pager |
---|
| 271 | except ImportError: |
---|
| 272 | from pydoc import pager |
---|
| 273 | pager(info) |
---|
[718] | 274 | else: |
---|
| 275 | return info |
---|
[710] | 276 | |
---|
[946] | 277 | |
---|
| 278 | def get_selection(self): |
---|
| 279 | """ |
---|
[1005] | 280 | Get the selection object currently set on this scantable. |
---|
| 281 | Parameters: |
---|
| 282 | none |
---|
| 283 | Example: |
---|
| 284 | sel = scan.get_selection() |
---|
| 285 | sel.set_ifs(0) # select IF 0 |
---|
| 286 | scan.set_selection(sel) # apply modified selection |
---|
[946] | 287 | """ |
---|
| 288 | return selector(self._getselection()) |
---|
| 289 | |
---|
[1005] | 290 | def set_selection(self, selection=selector()): |
---|
[946] | 291 | """ |
---|
[1005] | 292 | Select a subset of the data. All following operations on this scantable |
---|
| 293 | are only applied to thi selection. |
---|
| 294 | Parameters: |
---|
| 295 | selection: a selector object (default unset the selection) |
---|
| 296 | Examples: |
---|
| 297 | sel = selector() # create a selection object |
---|
[1118] | 298 | self.set_scans([0, 3]) # select SCANNO 0 and 3 |
---|
[1005] | 299 | scan.set_selection(sel) # set the selection |
---|
| 300 | scan.summary() # will only print summary of scanno 0 an 3 |
---|
| 301 | scan.set_selection() # unset the selection |
---|
[946] | 302 | """ |
---|
| 303 | self._setselection(selection) |
---|
| 304 | |
---|
[1446] | 305 | def get_row(self, row=0, insitu=None): |
---|
| 306 | """ |
---|
| 307 | Select a row in the scantable. |
---|
| 308 | Return a scantable with single row. |
---|
| 309 | Parameters: |
---|
| 310 | row: row no of integration, default is 0. |
---|
| 311 | insitu: if False a new scantable is returned. |
---|
| 312 | Otherwise, the scaling is done in-situ |
---|
| 313 | The default is taken from .asaprc (False) |
---|
| 314 | """ |
---|
| 315 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 316 | if not insitu: |
---|
| 317 | workscan = self.copy() |
---|
| 318 | else: |
---|
| 319 | workscan = self |
---|
| 320 | # Select a row |
---|
| 321 | sel=selector() |
---|
| 322 | sel.set_scans([workscan.getscan(row)]) |
---|
| 323 | sel.set_cycles([workscan.getcycle(row)]) |
---|
| 324 | sel.set_beams([workscan.getbeam(row)]) |
---|
| 325 | sel.set_ifs([workscan.getif(row)]) |
---|
| 326 | sel.set_polarisations([workscan.getpol(row)]) |
---|
| 327 | sel.set_name(workscan._getsourcename(row)) |
---|
| 328 | workscan.set_selection(sel) |
---|
| 329 | if not workscan.nrow() == 1: |
---|
| 330 | msg = "Cloud not identify single row. %d rows selected."%(workscan.nrow()) |
---|
| 331 | raise RuntimeError(msg) |
---|
| 332 | del sel |
---|
| 333 | if insitu: |
---|
| 334 | self._assign(workscan) |
---|
| 335 | else: |
---|
| 336 | return workscan |
---|
| 337 | |
---|
[876] | 338 | def stats(self, stat='stddev', mask=None): |
---|
[102] | 339 | """ |
---|
[135] | 340 | Determine the specified statistic of the current beam/if/pol |
---|
[102] | 341 | Takes a 'mask' as an optional parameter to specify which |
---|
| 342 | channels should be excluded. |
---|
| 343 | Parameters: |
---|
[135] | 344 | stat: 'min', 'max', 'sumsq', 'sum', 'mean' |
---|
| 345 | 'var', 'stddev', 'avdev', 'rms', 'median' |
---|
| 346 | mask: an optional mask specifying where the statistic |
---|
[102] | 347 | should be determined. |
---|
| 348 | Example: |
---|
[113] | 349 | scan.set_unit('channel') |
---|
[1118] | 350 | msk = scan.create_mask([100, 200], [500, 600]) |
---|
[135] | 351 | scan.stats(stat='mean', mask=m) |
---|
[102] | 352 | """ |
---|
| 353 | if mask == None: |
---|
[876] | 354 | mask = [] |
---|
[1118] | 355 | axes = ['Beam', 'IF', 'Pol', 'Time'] |
---|
[876] | 356 | if not self._check_ifs(): |
---|
[1118] | 357 | raise ValueError("Cannot apply mask as the IFs have different " |
---|
| 358 | "number of channels. Please use setselection() " |
---|
| 359 | "to select individual IFs") |
---|
[256] | 360 | |
---|
[876] | 361 | statvals = self._math._stats(self, mask, stat) |
---|
| 362 | out = '' |
---|
| 363 | axes = [] |
---|
| 364 | for i in range(self.nrow()): |
---|
| 365 | axis = [] |
---|
| 366 | axis.append(self.getscan(i)) |
---|
| 367 | axis.append(self.getbeam(i)) |
---|
| 368 | axis.append(self.getif(i)) |
---|
| 369 | axis.append(self.getpol(i)) |
---|
| 370 | axis.append(self.getcycle(i)) |
---|
| 371 | axes.append(axis) |
---|
| 372 | tm = self._gettime(i) |
---|
| 373 | src = self._getsourcename(i) |
---|
| 374 | out += 'Scan[%d] (%s) ' % (axis[0], src) |
---|
| 375 | out += 'Time[%s]:\n' % (tm) |
---|
| 376 | if self.nbeam(-1) > 1: out += ' Beam[%d] ' % (axis[1]) |
---|
| 377 | if self.nif(-1) > 1: out += ' IF[%d] ' % (axis[2]) |
---|
| 378 | if self.npol(-1) > 1: out += ' Pol[%d] ' % (axis[3]) |
---|
| 379 | out += '= %3.3f\n' % (statvals[i]) |
---|
| 380 | out += "--------------------------------------------------\n" |
---|
[256] | 381 | |
---|
[876] | 382 | if rcParams['verbose']: |
---|
| 383 | print "--------------------------------------------------" |
---|
[1118] | 384 | print " ", stat |
---|
[876] | 385 | print "--------------------------------------------------" |
---|
| 386 | print out |
---|
[1295] | 387 | #else: |
---|
| 388 | #retval = { 'axesnames': ['scanno', 'beamno', 'ifno', 'polno', 'cycleno'], |
---|
| 389 | # 'axes' : axes, |
---|
| 390 | # 'data': statvals} |
---|
| 391 | return statvals |
---|
[102] | 392 | |
---|
[1118] | 393 | def stddev(self, mask=None): |
---|
[135] | 394 | """ |
---|
| 395 | Determine the standard deviation of the current beam/if/pol |
---|
| 396 | Takes a 'mask' as an optional parameter to specify which |
---|
| 397 | channels should be excluded. |
---|
| 398 | Parameters: |
---|
| 399 | mask: an optional mask specifying where the standard |
---|
| 400 | deviation should be determined. |
---|
| 401 | |
---|
| 402 | Example: |
---|
| 403 | scan.set_unit('channel') |
---|
[1118] | 404 | msk = scan.create_mask([100, 200], [500, 600]) |
---|
[135] | 405 | scan.stddev(mask=m) |
---|
| 406 | """ |
---|
[1118] | 407 | return self.stats(stat='stddev', mask=mask); |
---|
[135] | 408 | |
---|
[1003] | 409 | |
---|
[1259] | 410 | def get_column_names(self): |
---|
[1003] | 411 | """ |
---|
| 412 | Return a list of column names, which can be used for selection. |
---|
| 413 | """ |
---|
[1259] | 414 | return list(Scantable.get_column_names(self)) |
---|
[1003] | 415 | |
---|
[876] | 416 | def get_tsys(self): |
---|
[113] | 417 | """ |
---|
| 418 | Return the System temperatures. |
---|
| 419 | Returns: |
---|
[876] | 420 | a list of Tsys values for the current selection |
---|
[113] | 421 | """ |
---|
[256] | 422 | |
---|
[876] | 423 | return self._row_callback(self._gettsys, "Tsys") |
---|
[256] | 424 | |
---|
[876] | 425 | def _row_callback(self, callback, label): |
---|
| 426 | axes = [] |
---|
[1118] | 427 | axesnames = ['scanno', 'beamno', 'ifno', 'polno', 'cycleno'] |
---|
[876] | 428 | out = "" |
---|
[1118] | 429 | outvec = [] |
---|
[876] | 430 | for i in range(self.nrow()): |
---|
| 431 | axis = [] |
---|
| 432 | axis.append(self.getscan(i)) |
---|
| 433 | axis.append(self.getbeam(i)) |
---|
| 434 | axis.append(self.getif(i)) |
---|
| 435 | axis.append(self.getpol(i)) |
---|
| 436 | axis.append(self.getcycle(i)) |
---|
| 437 | axes.append(axis) |
---|
| 438 | tm = self._gettime(i) |
---|
| 439 | src = self._getsourcename(i) |
---|
| 440 | out += 'Scan[%d] (%s) ' % (axis[0], src) |
---|
| 441 | out += 'Time[%s]:\n' % (tm) |
---|
| 442 | if self.nbeam(-1) > 1: out += ' Beam[%d] ' % (axis[1]) |
---|
| 443 | if self.nif(-1) > 1: out += ' IF[%d] ' % (axis[2]) |
---|
| 444 | if self.npol(-1) > 1: out += ' Pol[%d] ' % (axis[3]) |
---|
| 445 | outvec.append(callback(i)) |
---|
| 446 | out += '= %3.3f\n' % (outvec[i]) |
---|
| 447 | out += "--------------------------------------------------\n" |
---|
| 448 | if rcParams['verbose']: |
---|
| 449 | print "--------------------------------------------------" |
---|
| 450 | print " %s" % (label) |
---|
| 451 | print "--------------------------------------------------" |
---|
| 452 | print out |
---|
[1175] | 453 | # disabled because the vector seems more useful |
---|
| 454 | #retval = {'axesnames': axesnames, 'axes': axes, 'data': outvec} |
---|
| 455 | return outvec |
---|
[256] | 456 | |
---|
[1070] | 457 | def _get_column(self, callback, row=-1): |
---|
| 458 | """ |
---|
| 459 | """ |
---|
| 460 | if row == -1: |
---|
| 461 | return [callback(i) for i in range(self.nrow())] |
---|
| 462 | else: |
---|
| 463 | if 0 <= row < self.nrow(): |
---|
| 464 | return callback(row) |
---|
[256] | 465 | |
---|
[1070] | 466 | |
---|
[1348] | 467 | def get_time(self, row=-1, asdatetime=False): |
---|
[113] | 468 | """ |
---|
| 469 | Get a list of time stamps for the observations. |
---|
[1348] | 470 | Return a datetime object for each integration time stamp in the scantable. |
---|
[113] | 471 | Parameters: |
---|
[1348] | 472 | row: row no of integration. Default -1 return all rows |
---|
| 473 | asdatetime: return values as datetime objects rather than strings |
---|
[113] | 474 | Example: |
---|
| 475 | none |
---|
| 476 | """ |
---|
[1175] | 477 | from time import strptime |
---|
| 478 | from datetime import datetime |
---|
[1457] | 479 | times = self._get_column(self._gettime, row) |
---|
[1348] | 480 | if not asdatetime: |
---|
[1457] | 481 | return times |
---|
[1175] | 482 | format = "%Y/%m/%d/%H:%M:%S" |
---|
| 483 | if isinstance(times, list): |
---|
| 484 | return [datetime(*strptime(i, format)[:6]) for i in times] |
---|
| 485 | else: |
---|
| 486 | return datetime(*strptime(times, format)[:6]) |
---|
[102] | 487 | |
---|
[1348] | 488 | |
---|
| 489 | def get_inttime(self, row=-1): |
---|
| 490 | """ |
---|
| 491 | Get a list of integration times for the observations. |
---|
| 492 | Return a time in seconds for each integration in the scantable. |
---|
| 493 | Parameters: |
---|
| 494 | row: row no of integration. Default -1 return all rows. |
---|
| 495 | Example: |
---|
| 496 | none |
---|
| 497 | """ |
---|
| 498 | return self._get_column(self._getinttime, row) |
---|
| 499 | |
---|
| 500 | |
---|
[714] | 501 | def get_sourcename(self, row=-1): |
---|
| 502 | """ |
---|
[794] | 503 | Get a list source names for the observations. |
---|
[714] | 504 | Return a string for each integration in the scantable. |
---|
| 505 | Parameters: |
---|
[1348] | 506 | row: row no of integration. Default -1 return all rows. |
---|
[714] | 507 | Example: |
---|
| 508 | none |
---|
| 509 | """ |
---|
[1070] | 510 | return self._get_column(self._getsourcename, row) |
---|
[714] | 511 | |
---|
[794] | 512 | def get_elevation(self, row=-1): |
---|
| 513 | """ |
---|
| 514 | Get a list of elevations for the observations. |
---|
| 515 | Return a float for each integration in the scantable. |
---|
| 516 | Parameters: |
---|
[1348] | 517 | row: row no of integration. Default -1 return all rows. |
---|
[794] | 518 | Example: |
---|
| 519 | none |
---|
| 520 | """ |
---|
[1070] | 521 | return self._get_column(self._getelevation, row) |
---|
[794] | 522 | |
---|
| 523 | def get_azimuth(self, row=-1): |
---|
| 524 | """ |
---|
| 525 | Get a list of azimuths for the observations. |
---|
| 526 | Return a float for each integration in the scantable. |
---|
| 527 | Parameters: |
---|
[1348] | 528 | row: row no of integration. Default -1 return all rows. |
---|
[794] | 529 | Example: |
---|
| 530 | none |
---|
| 531 | """ |
---|
[1070] | 532 | return self._get_column(self._getazimuth, row) |
---|
[794] | 533 | |
---|
| 534 | def get_parangle(self, row=-1): |
---|
| 535 | """ |
---|
| 536 | Get a list of parallactic angles for the observations. |
---|
| 537 | Return a float for each integration in the scantable. |
---|
| 538 | Parameters: |
---|
[1348] | 539 | row: row no of integration. Default -1 return all rows. |
---|
[794] | 540 | Example: |
---|
| 541 | none |
---|
| 542 | """ |
---|
[1070] | 543 | return self._get_column(self._getparangle, row) |
---|
[794] | 544 | |
---|
[1070] | 545 | def get_direction(self, row=-1): |
---|
| 546 | """ |
---|
| 547 | Get a list of Positions on the sky (direction) for the observations. |
---|
| 548 | Return a float for each integration in the scantable. |
---|
| 549 | Parameters: |
---|
| 550 | row: row no of integration. Default -1 return all rows |
---|
| 551 | Example: |
---|
| 552 | none |
---|
| 553 | """ |
---|
| 554 | return self._get_column(self._getdirection, row) |
---|
| 555 | |
---|
[1389] | 556 | def get_directionval(self, row=-1): |
---|
| 557 | """ |
---|
| 558 | Get a list of Positions on the sky (direction) for the observations. |
---|
| 559 | Return a float for each integration in the scantable. |
---|
| 560 | Parameters: |
---|
| 561 | row: row no of integration. Default -1 return all rows |
---|
| 562 | Example: |
---|
| 563 | none |
---|
| 564 | """ |
---|
| 565 | return self._get_column(self._getdirectionvec, row) |
---|
| 566 | |
---|
[102] | 567 | def set_unit(self, unit='channel'): |
---|
| 568 | """ |
---|
| 569 | Set the unit for all following operations on this scantable |
---|
| 570 | Parameters: |
---|
| 571 | unit: optional unit, default is 'channel' |
---|
[1118] | 572 | one of '*Hz', 'km/s', 'channel', '' |
---|
[102] | 573 | """ |
---|
[484] | 574 | varlist = vars() |
---|
[1118] | 575 | if unit in ['', 'pixel', 'channel']: |
---|
[113] | 576 | unit = '' |
---|
| 577 | inf = list(self._getcoordinfo()) |
---|
| 578 | inf[0] = unit |
---|
| 579 | self._setcoordinfo(inf) |
---|
[1118] | 580 | self._add_history("set_unit", varlist) |
---|
[113] | 581 | |
---|
[484] | 582 | def set_instrument(self, instr): |
---|
[358] | 583 | """ |
---|
[1348] | 584 | Set the instrument for subsequent processing. |
---|
[358] | 585 | Parameters: |
---|
[710] | 586 | instr: Select from 'ATPKSMB', 'ATPKSHOH', 'ATMOPRA', |
---|
[407] | 587 | 'DSS-43' (Tid), 'CEDUNA', and 'HOBART' |
---|
[358] | 588 | """ |
---|
| 589 | self._setInstrument(instr) |
---|
[1118] | 590 | self._add_history("set_instument", vars()) |
---|
[718] | 591 | print_log() |
---|
[358] | 592 | |
---|
[1190] | 593 | def set_feedtype(self, feedtype): |
---|
| 594 | """ |
---|
| 595 | Overwrite the feed type, which might not be set correctly. |
---|
| 596 | Parameters: |
---|
| 597 | feedtype: 'linear' or 'circular' |
---|
| 598 | """ |
---|
| 599 | self._setfeedtype(feedtype) |
---|
| 600 | self._add_history("set_feedtype", vars()) |
---|
| 601 | print_log() |
---|
| 602 | |
---|
[276] | 603 | def set_doppler(self, doppler='RADIO'): |
---|
| 604 | """ |
---|
| 605 | Set the doppler for all following operations on this scantable. |
---|
| 606 | Parameters: |
---|
| 607 | doppler: One of 'RADIO', 'OPTICAL', 'Z', 'BETA', 'GAMMA' |
---|
| 608 | """ |
---|
[484] | 609 | varlist = vars() |
---|
[276] | 610 | inf = list(self._getcoordinfo()) |
---|
| 611 | inf[2] = doppler |
---|
| 612 | self._setcoordinfo(inf) |
---|
[1118] | 613 | self._add_history("set_doppler", vars()) |
---|
[718] | 614 | print_log() |
---|
[710] | 615 | |
---|
[226] | 616 | def set_freqframe(self, frame=None): |
---|
[113] | 617 | """ |
---|
| 618 | Set the frame type of the Spectral Axis. |
---|
| 619 | Parameters: |
---|
[591] | 620 | frame: an optional frame type, default 'LSRK'. Valid frames are: |
---|
[1118] | 621 | 'REST', 'TOPO', 'LSRD', 'LSRK', 'BARY', |
---|
| 622 | 'GEO', 'GALACTO', 'LGROUP', 'CMB' |
---|
[113] | 623 | Examples: |
---|
| 624 | scan.set_freqframe('BARY') |
---|
| 625 | """ |
---|
[484] | 626 | if frame is None: frame = rcParams['scantable.freqframe'] |
---|
| 627 | varlist = vars() |
---|
[1118] | 628 | valid = ['REST', 'TOPO', 'LSRD', 'LSRK', 'BARY', \ |
---|
| 629 | 'GEO', 'GALACTO', 'LGROUP', 'CMB'] |
---|
[591] | 630 | |
---|
[989] | 631 | if frame in valid: |
---|
[113] | 632 | inf = list(self._getcoordinfo()) |
---|
| 633 | inf[1] = frame |
---|
| 634 | self._setcoordinfo(inf) |
---|
[1118] | 635 | self._add_history("set_freqframe", varlist) |
---|
[102] | 636 | else: |
---|
[1118] | 637 | msg = "Please specify a valid freq type. Valid types are:\n", valid |
---|
[718] | 638 | if rcParams['verbose']: |
---|
| 639 | print msg |
---|
| 640 | else: |
---|
| 641 | raise TypeError(msg) |
---|
| 642 | print_log() |
---|
[710] | 643 | |
---|
[989] | 644 | def set_dirframe(self, frame=""): |
---|
| 645 | """ |
---|
| 646 | Set the frame type of the Direction on the sky. |
---|
| 647 | Parameters: |
---|
| 648 | frame: an optional frame type, default ''. Valid frames are: |
---|
| 649 | 'J2000', 'B1950', 'GALACTIC' |
---|
| 650 | Examples: |
---|
| 651 | scan.set_dirframe('GALACTIC') |
---|
| 652 | """ |
---|
| 653 | varlist = vars() |
---|
| 654 | try: |
---|
| 655 | Scantable.set_dirframe(self, frame) |
---|
[1118] | 656 | except RuntimeError, msg: |
---|
[989] | 657 | if rcParams['verbose']: |
---|
| 658 | print msg |
---|
| 659 | else: |
---|
| 660 | raise |
---|
[1118] | 661 | self._add_history("set_dirframe", varlist) |
---|
[989] | 662 | |
---|
[113] | 663 | def get_unit(self): |
---|
| 664 | """ |
---|
| 665 | Get the default unit set in this scantable |
---|
| 666 | Returns: |
---|
| 667 | A unit string |
---|
| 668 | """ |
---|
| 669 | inf = self._getcoordinfo() |
---|
| 670 | unit = inf[0] |
---|
| 671 | if unit == '': unit = 'channel' |
---|
| 672 | return unit |
---|
[102] | 673 | |
---|
[158] | 674 | def get_abcissa(self, rowno=0): |
---|
[102] | 675 | """ |
---|
[158] | 676 | Get the abcissa in the current coordinate setup for the currently |
---|
[113] | 677 | selected Beam/IF/Pol |
---|
| 678 | Parameters: |
---|
[226] | 679 | rowno: an optional row number in the scantable. Default is the |
---|
| 680 | first row, i.e. rowno=0 |
---|
[113] | 681 | Returns: |
---|
[1348] | 682 | The abcissa values and the format string (as a dictionary) |
---|
[113] | 683 | """ |
---|
[256] | 684 | abc = self._getabcissa(rowno) |
---|
[710] | 685 | lbl = self._getabcissalabel(rowno) |
---|
[718] | 686 | print_log() |
---|
[158] | 687 | return abc, lbl |
---|
[113] | 688 | |
---|
[1401] | 689 | def flag(self, mask=None, unflag=False): |
---|
[1001] | 690 | """ |
---|
| 691 | Flag the selected data using an optional channel mask. |
---|
| 692 | Parameters: |
---|
| 693 | mask: an optional channel mask, created with create_mask. Default |
---|
| 694 | (no mask) is all channels. |
---|
[1401] | 695 | unflag: if True, unflag the data |
---|
[1001] | 696 | """ |
---|
| 697 | varlist = vars() |
---|
[1118] | 698 | if mask is None: |
---|
| 699 | mask = [] |
---|
[1001] | 700 | try: |
---|
[1401] | 701 | self._flag(mask, unflag) |
---|
[1118] | 702 | except RuntimeError, msg: |
---|
[1001] | 703 | if rcParams['verbose']: |
---|
| 704 | print msg |
---|
| 705 | return |
---|
| 706 | else: raise |
---|
| 707 | self._add_history("flag", varlist) |
---|
| 708 | |
---|
[1203] | 709 | def lag_flag(self, frequency, width=0.0, unit="GHz", insitu=None): |
---|
[1192] | 710 | """ |
---|
| 711 | Flag the data in 'lag' space by providing a frequency to remove. |
---|
| 712 | Flagged data in the scantable gets set to 0.0 before the fft. |
---|
| 713 | No taper is applied. |
---|
| 714 | Parameters: |
---|
[1348] | 715 | frequency: the frequency (really a period within the bandwidth) |
---|
| 716 | to remove |
---|
| 717 | width: the width of the frequency to remove, to remove a |
---|
| 718 | range of frequencies aroung the centre. |
---|
[1203] | 719 | unit: the frequency unit (default "GHz") |
---|
| 720 | Notes: |
---|
[1348] | 721 | It is recommended to flag edges of the band or strong |
---|
| 722 | signals beforehand. |
---|
[1192] | 723 | """ |
---|
| 724 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 725 | self._math._setinsitu(insitu) |
---|
| 726 | varlist = vars() |
---|
[1370] | 727 | base = { "GHz": 1000000000., "MHz": 1000000., "kHz": 1000., "Hz": 1. } |
---|
[1192] | 728 | if not base.has_key(unit): |
---|
| 729 | raise ValueError("%s is not a valid unit." % unit) |
---|
| 730 | try: |
---|
[1200] | 731 | s = scantable(self._math._lag_flag(self, frequency*base[unit], |
---|
| 732 | width*base[unit])) |
---|
[1192] | 733 | except RuntimeError, msg: |
---|
| 734 | if rcParams['verbose']: |
---|
| 735 | print msg |
---|
| 736 | return |
---|
| 737 | else: raise |
---|
| 738 | s._add_history("lag_flag", varlist) |
---|
| 739 | print_log() |
---|
| 740 | if insitu: |
---|
| 741 | self._assign(s) |
---|
| 742 | else: |
---|
| 743 | return s |
---|
[1001] | 744 | |
---|
[1192] | 745 | |
---|
[113] | 746 | def create_mask(self, *args, **kwargs): |
---|
| 747 | """ |
---|
[1118] | 748 | Compute and return a mask based on [min, max] windows. |
---|
[189] | 749 | The specified windows are to be INCLUDED, when the mask is |
---|
[113] | 750 | applied. |
---|
[102] | 751 | Parameters: |
---|
[1118] | 752 | [min, max], [min2, max2], ... |
---|
[1024] | 753 | Pairs of start/end points (inclusive)specifying the regions |
---|
[102] | 754 | to be masked |
---|
[189] | 755 | invert: optional argument. If specified as True, |
---|
| 756 | return an inverted mask, i.e. the regions |
---|
| 757 | specified are EXCLUDED |
---|
[513] | 758 | row: create the mask using the specified row for |
---|
| 759 | unit conversions, default is row=0 |
---|
| 760 | only necessary if frequency varies over rows. |
---|
[102] | 761 | Example: |
---|
[113] | 762 | scan.set_unit('channel') |
---|
| 763 | a) |
---|
[1118] | 764 | msk = scan.create_mask([400, 500], [800, 900]) |
---|
[189] | 765 | # masks everything outside 400 and 500 |
---|
[113] | 766 | # and 800 and 900 in the unit 'channel' |
---|
| 767 | |
---|
| 768 | b) |
---|
[1118] | 769 | msk = scan.create_mask([400, 500], [800, 900], invert=True) |
---|
[189] | 770 | # masks the regions between 400 and 500 |
---|
[113] | 771 | # and 800 and 900 in the unit 'channel' |
---|
[1024] | 772 | c) |
---|
| 773 | mask only channel 400 |
---|
[1118] | 774 | msk = scan.create_mask([400, 400]) |
---|
[102] | 775 | """ |
---|
[513] | 776 | row = 0 |
---|
| 777 | if kwargs.has_key("row"): |
---|
| 778 | row = kwargs.get("row") |
---|
| 779 | data = self._getabcissa(row) |
---|
[113] | 780 | u = self._getcoordinfo()[0] |
---|
[718] | 781 | if rcParams['verbose']: |
---|
[113] | 782 | if u == "": u = "channel" |
---|
[718] | 783 | msg = "The current mask window unit is %s" % u |
---|
[1118] | 784 | i = self._check_ifs() |
---|
| 785 | if not i: |
---|
[876] | 786 | msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) |
---|
[718] | 787 | asaplog.push(msg) |
---|
[102] | 788 | n = self.nchan() |
---|
[1295] | 789 | msk = _n_bools(n, False) |
---|
[710] | 790 | # test if args is a 'list' or a 'normal *args - UGLY!!! |
---|
| 791 | |
---|
[1118] | 792 | ws = (isinstance(args[-1][-1], int) or isinstance(args[-1][-1], float)) \ |
---|
| 793 | and args or args[0] |
---|
[710] | 794 | for window in ws: |
---|
[102] | 795 | if (len(window) != 2 or window[0] > window[1] ): |
---|
[1118] | 796 | raise TypeError("A window needs to be defined as [min, max]") |
---|
[102] | 797 | for i in range(n): |
---|
[1024] | 798 | if data[i] >= window[0] and data[i] <= window[1]: |
---|
[1295] | 799 | msk[i] = True |
---|
[113] | 800 | if kwargs.has_key('invert'): |
---|
| 801 | if kwargs.get('invert'): |
---|
[1295] | 802 | msk = mask_not(msk) |
---|
[718] | 803 | print_log() |
---|
[102] | 804 | return msk |
---|
[710] | 805 | |
---|
[1446] | 806 | def get_masklist(self, mask=None, row=0): |
---|
[256] | 807 | """ |
---|
[1446] | 808 | Compute and return a list of mask windows, [min, max]. |
---|
| 809 | Parameters: |
---|
| 810 | mask: channel mask, created with create_mask. |
---|
| 811 | row: calcutate the masklist using the specified row |
---|
| 812 | for unit conversions, default is row=0 |
---|
| 813 | only necessary if frequency varies over rows. |
---|
| 814 | Returns: |
---|
| 815 | [min, max], [min2, max2], ... |
---|
| 816 | Pairs of start/end points (inclusive)specifying |
---|
| 817 | the masked regions |
---|
| 818 | """ |
---|
| 819 | if not (isinstance(mask,list) or isinstance(mask, tuple)): |
---|
| 820 | raise TypeError("The mask should be list or tuple.") |
---|
| 821 | if len(mask) < 2: |
---|
| 822 | raise TypeError("The mask elements should be > 1") |
---|
| 823 | if self.nchan() != len(mask): |
---|
| 824 | msg = "Number of channels in scantable != number of mask elements" |
---|
| 825 | raise TypeError(msg) |
---|
| 826 | data = self._getabcissa(row) |
---|
| 827 | u = self._getcoordinfo()[0] |
---|
| 828 | if rcParams['verbose']: |
---|
| 829 | if u == "": u = "channel" |
---|
| 830 | msg = "The current mask window unit is %s" % u |
---|
| 831 | i = self._check_ifs() |
---|
| 832 | if not i: |
---|
| 833 | msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) |
---|
| 834 | asaplog.push(msg) |
---|
| 835 | masklist=[] |
---|
| 836 | ist, ien = None, None |
---|
| 837 | ist, ien=self.get_mask_indices(mask) |
---|
| 838 | if ist is not None and ien is not None: |
---|
| 839 | for i in xrange(len(ist)): |
---|
| 840 | range=[data[ist[i]],data[ien[i]]] |
---|
| 841 | range.sort() |
---|
| 842 | masklist.append([range[0],range[1]]) |
---|
| 843 | return masklist |
---|
| 844 | |
---|
| 845 | def get_mask_indices(self, mask=None): |
---|
| 846 | """ |
---|
| 847 | Compute and Return lists of mask start indices and mask end indices. |
---|
| 848 | Parameters: |
---|
| 849 | mask: channel mask, created with create_mask. |
---|
| 850 | Returns: |
---|
| 851 | List of mask start indices and that of mask end indices, |
---|
| 852 | i.e., [istart1,istart2,....], [iend1,iend2,....]. |
---|
| 853 | """ |
---|
| 854 | if not (isinstance(mask,list) or isinstance(mask, tuple)): |
---|
| 855 | raise TypeError("The mask should be list or tuple.") |
---|
| 856 | if len(mask) < 2: |
---|
| 857 | raise TypeError("The mask elements should be > 1") |
---|
| 858 | istart=[] |
---|
| 859 | iend=[] |
---|
| 860 | if mask[0]: istart.append(0) |
---|
| 861 | for i in range(len(mask)-1): |
---|
| 862 | if not mask[i] and mask[i+1]: |
---|
| 863 | istart.append(i+1) |
---|
| 864 | elif mask[i] and not mask[i+1]: |
---|
| 865 | iend.append(i) |
---|
| 866 | if mask[len(mask)-1]: iend.append(len(mask)-1) |
---|
| 867 | if len(istart) != len(iend): |
---|
| 868 | raise RuntimeError("Numbers of mask start != mask end.") |
---|
| 869 | for i in range(len(istart)): |
---|
| 870 | if istart[i] > iend[i]: |
---|
| 871 | raise RuntimeError("Mask start index > mask end index") |
---|
| 872 | break |
---|
| 873 | return istart,iend |
---|
| 874 | |
---|
| 875 | # def get_restfreqs(self): |
---|
| 876 | # """ |
---|
| 877 | # Get the restfrequency(s) stored in this scantable. |
---|
| 878 | # The return value(s) are always of unit 'Hz' |
---|
| 879 | # Parameters: |
---|
| 880 | # none |
---|
| 881 | # Returns: |
---|
| 882 | # a list of doubles |
---|
| 883 | # """ |
---|
| 884 | # return list(self._getrestfreqs()) |
---|
| 885 | |
---|
| 886 | def get_restfreqs(self, ids=None): |
---|
| 887 | """ |
---|
[256] | 888 | Get the restfrequency(s) stored in this scantable. |
---|
| 889 | The return value(s) are always of unit 'Hz' |
---|
| 890 | Parameters: |
---|
[1446] | 891 | ids: (optional) a list of MOLECULE_ID for that restfrequency(s) to |
---|
| 892 | be retrieved |
---|
[256] | 893 | Returns: |
---|
[1446] | 894 | dictionary containing ids and a list of doubles for each id |
---|
[256] | 895 | """ |
---|
[1446] | 896 | if ids is None: |
---|
| 897 | rfreqs={} |
---|
| 898 | idlist = self.getmolnos() |
---|
| 899 | for i in idlist: |
---|
| 900 | rfreqs[i]=list(self._getrestfreqs(i)) |
---|
| 901 | return rfreqs |
---|
| 902 | else: |
---|
| 903 | if type(ids)==list or type(ids)==tuple: |
---|
| 904 | rfreqs={} |
---|
| 905 | for i in ids: |
---|
| 906 | rfreqs[i]=list(self._getrestfreqs(i)) |
---|
| 907 | return rfreqs |
---|
| 908 | else: |
---|
| 909 | return list(self._getrestfreqs(ids)) |
---|
| 910 | #return list(self._getrestfreqs(ids)) |
---|
[102] | 911 | |
---|
[931] | 912 | def set_restfreqs(self, freqs=None, unit='Hz'): |
---|
| 913 | """ |
---|
[1446] | 914 | ********NEED TO BE UPDATED begin************ |
---|
[931] | 915 | Set or replace the restfrequency specified and |
---|
| 916 | If the 'freqs' argument holds a scalar, |
---|
| 917 | then that rest frequency will be applied to all the selected |
---|
| 918 | data. If the 'freqs' argument holds |
---|
| 919 | a vector, then it MUST be of equal or smaller length than |
---|
| 920 | the number of IFs (and the available restfrequencies will be |
---|
| 921 | replaced by this vector). In this case, *all* data have |
---|
| 922 | the restfrequency set per IF according |
---|
| 923 | to the corresponding value you give in the 'freqs' vector. |
---|
[1118] | 924 | E.g. 'freqs=[1e9, 2e9]' would mean IF 0 gets restfreq 1e9 and |
---|
[931] | 925 | IF 1 gets restfreq 2e9. |
---|
[1446] | 926 | ********NEED TO BE UPDATED end************ |
---|
[1153] | 927 | You can also specify the frequencies via a linecatalog/ |
---|
| 928 | |
---|
[931] | 929 | Parameters: |
---|
| 930 | freqs: list of rest frequency values or string idenitfiers |
---|
| 931 | unit: unit for rest frequency (default 'Hz') |
---|
[402] | 932 | |
---|
[931] | 933 | Example: |
---|
[1446] | 934 | # set the given restfrequency for the all currently selected IFs |
---|
[931] | 935 | scan.set_restfreqs(freqs=1.4e9) |
---|
[1446] | 936 | # set multiple restfrequencies to all the selected data |
---|
| 937 | scan.set_restfreqs(freqs=[1.4e9, 1.41e9, 1.42e9]) |
---|
| 938 | # If the number of IFs in the data is >= 2 the IF0 gets the first |
---|
| 939 | # value IF1 the second... NOTE that freqs needs to be |
---|
| 940 | # specified in list of list (e.g. [[],[],...] ). |
---|
| 941 | scan.set_restfreqs(freqs=[[1.4e9],[1.67e9]]) |
---|
[931] | 942 | #set the given restfrequency for the whole table (by name) |
---|
| 943 | scan.set_restfreqs(freqs="OH1667") |
---|
[391] | 944 | |
---|
[931] | 945 | Note: |
---|
| 946 | To do more sophisticate Restfrequency setting, e.g. on a |
---|
| 947 | source and IF basis, use scantable.set_selection() before using |
---|
| 948 | this function. |
---|
| 949 | # provide your scantable is call scan |
---|
| 950 | selection = selector() |
---|
| 951 | selection.set_name("ORION*") |
---|
| 952 | selection.set_ifs([1]) |
---|
| 953 | scan.set_selection(selection) |
---|
| 954 | scan.set_restfreqs(freqs=86.6e9) |
---|
| 955 | |
---|
| 956 | """ |
---|
| 957 | varlist = vars() |
---|
[1157] | 958 | from asap import linecatalog |
---|
| 959 | # simple value |
---|
[1118] | 960 | if isinstance(freqs, int) or isinstance(freqs, float): |
---|
[1446] | 961 | # TT mod |
---|
| 962 | #self._setrestfreqs(freqs, "",unit) |
---|
| 963 | self._setrestfreqs([freqs], [""],unit) |
---|
[1157] | 964 | # list of values |
---|
[1118] | 965 | elif isinstance(freqs, list) or isinstance(freqs, tuple): |
---|
[1157] | 966 | # list values are scalars |
---|
[1118] | 967 | if isinstance(freqs[-1], int) or isinstance(freqs[-1], float): |
---|
[1446] | 968 | self._setrestfreqs(freqs, [""],unit) |
---|
[1157] | 969 | # list values are tuples, (value, name) |
---|
| 970 | elif isinstance(freqs[-1], dict): |
---|
[1446] | 971 | #sel = selector() |
---|
| 972 | #savesel = self._getselection() |
---|
| 973 | #iflist = self.getifnos() |
---|
| 974 | #for i in xrange(len(freqs)): |
---|
| 975 | # sel.set_ifs(iflist[i]) |
---|
| 976 | # self._setselection(sel) |
---|
| 977 | # self._setrestfreqs(freqs[i], "",unit) |
---|
| 978 | #self._setselection(savesel) |
---|
| 979 | self._setrestfreqs(freqs["value"], |
---|
| 980 | freqs["name"], "MHz") |
---|
| 981 | elif isinstance(freqs[-1], list) or isinstance(freqs[-1], tuple): |
---|
[1157] | 982 | sel = selector() |
---|
| 983 | savesel = self._getselection() |
---|
[1322] | 984 | iflist = self.getifnos() |
---|
[1446] | 985 | if len(freqs)>len(iflist): |
---|
| 986 | raise ValueError("number of elements in list of list exeeds the current IF selections") |
---|
[1157] | 987 | for i in xrange(len(freqs)): |
---|
[1322] | 988 | sel.set_ifs(iflist[i]) |
---|
[1259] | 989 | self._setselection(sel) |
---|
[1157] | 990 | self._setrestfreqs(freqs[i]["value"], |
---|
| 991 | freqs[i]["name"], "MHz") |
---|
| 992 | self._setselection(savesel) |
---|
| 993 | # freqs are to be taken from a linecatalog |
---|
[1153] | 994 | elif isinstance(freqs, linecatalog): |
---|
| 995 | sel = selector() |
---|
| 996 | savesel = self._getselection() |
---|
| 997 | for i in xrange(freqs.nrow()): |
---|
[1322] | 998 | sel.set_ifs(iflist[i]) |
---|
[1153] | 999 | self._setselection(sel) |
---|
| 1000 | self._setrestfreqs(freqs.get_frequency(i), |
---|
| 1001 | freqs.get_name(i), "MHz") |
---|
| 1002 | # ensure that we are not iterating past nIF |
---|
| 1003 | if i == self.nif()-1: break |
---|
| 1004 | self._setselection(savesel) |
---|
[931] | 1005 | else: |
---|
| 1006 | return |
---|
| 1007 | self._add_history("set_restfreqs", varlist) |
---|
| 1008 | |
---|
[1360] | 1009 | def shift_refpix(self, delta): |
---|
| 1010 | """ |
---|
| 1011 | Shift the reference pixel of the Spectra Coordinate by an |
---|
| 1012 | integer amount. |
---|
| 1013 | Parameters: |
---|
| 1014 | delta: the amount to shift by |
---|
| 1015 | Note: |
---|
| 1016 | Be careful using this with broadband data. |
---|
| 1017 | """ |
---|
| 1018 | Scantable.shift(self, delta) |
---|
[931] | 1019 | |
---|
[1259] | 1020 | def history(self, filename=None): |
---|
| 1021 | """ |
---|
| 1022 | Print the history. Optionally to a file. |
---|
[1348] | 1023 | Parameters: |
---|
| 1024 | filename: The name of the file to save the history to. |
---|
[1259] | 1025 | """ |
---|
[484] | 1026 | hist = list(self._gethistory()) |
---|
[794] | 1027 | out = "-"*80 |
---|
[484] | 1028 | for h in hist: |
---|
[489] | 1029 | if h.startswith("---"): |
---|
[794] | 1030 | out += "\n"+h |
---|
[489] | 1031 | else: |
---|
| 1032 | items = h.split("##") |
---|
| 1033 | date = items[0] |
---|
| 1034 | func = items[1] |
---|
| 1035 | items = items[2:] |
---|
[794] | 1036 | out += "\n"+date+"\n" |
---|
| 1037 | out += "Function: %s\n Parameters:" % (func) |
---|
[489] | 1038 | for i in items: |
---|
| 1039 | s = i.split("=") |
---|
[1118] | 1040 | out += "\n %s = %s" % (s[0], s[1]) |
---|
[794] | 1041 | out += "\n"+"-"*80 |
---|
[1259] | 1042 | if filename is not None: |
---|
| 1043 | if filename is "": |
---|
| 1044 | filename = 'scantable_history.txt' |
---|
| 1045 | import os |
---|
| 1046 | filename = os.path.expandvars(os.path.expanduser(filename)) |
---|
| 1047 | if not os.path.isdir(filename): |
---|
| 1048 | data = open(filename, 'w') |
---|
| 1049 | data.write(out) |
---|
| 1050 | data.close() |
---|
| 1051 | else: |
---|
| 1052 | msg = "Illegal file name '%s'." % (filename) |
---|
| 1053 | if rcParams['verbose']: |
---|
| 1054 | print msg |
---|
| 1055 | else: |
---|
| 1056 | raise IOError(msg) |
---|
| 1057 | if rcParams['verbose']: |
---|
| 1058 | try: |
---|
| 1059 | from IPython.genutils import page as pager |
---|
| 1060 | except ImportError: |
---|
| 1061 | from pydoc import pager |
---|
| 1062 | pager(out) |
---|
| 1063 | else: |
---|
| 1064 | return out |
---|
[484] | 1065 | return |
---|
[513] | 1066 | # |
---|
| 1067 | # Maths business |
---|
| 1068 | # |
---|
| 1069 | |
---|
[931] | 1070 | def average_time(self, mask=None, scanav=False, weight='tint', align=False): |
---|
[513] | 1071 | """ |
---|
[1070] | 1072 | Return the (time) weighted average of a scan. |
---|
[513] | 1073 | Note: |
---|
[1070] | 1074 | in channels only - align if necessary |
---|
[513] | 1075 | Parameters: |
---|
| 1076 | mask: an optional mask (only used for 'var' and 'tsys' |
---|
| 1077 | weighting) |
---|
[558] | 1078 | scanav: True averages each scan separately |
---|
| 1079 | False (default) averages all scans together, |
---|
[1099] | 1080 | weight: Weighting scheme. |
---|
| 1081 | 'none' (mean no weight) |
---|
| 1082 | 'var' (1/var(spec) weighted) |
---|
| 1083 | 'tsys' (1/Tsys**2 weighted) |
---|
| 1084 | 'tint' (integration time weighted) |
---|
| 1085 | 'tintsys' (Tint/Tsys**2) |
---|
| 1086 | 'median' ( median averaging) |
---|
[535] | 1087 | The default is 'tint' |
---|
[931] | 1088 | align: align the spectra in velocity before averaging. It takes |
---|
| 1089 | the time of the first spectrum as reference time. |
---|
[513] | 1090 | Example: |
---|
| 1091 | # time average the scantable without using a mask |
---|
[710] | 1092 | newscan = scan.average_time() |
---|
[513] | 1093 | """ |
---|
| 1094 | varlist = vars() |
---|
[976] | 1095 | if weight is None: weight = 'TINT' |
---|
[513] | 1096 | if mask is None: mask = () |
---|
[1099] | 1097 | if scanav: scanav = "SCAN" |
---|
| 1098 | else: scanav = "NONE" |
---|
[1118] | 1099 | scan = (self, ) |
---|
[989] | 1100 | try: |
---|
[1118] | 1101 | if align: |
---|
| 1102 | scan = (self.freq_align(insitu=False), ) |
---|
| 1103 | s = None |
---|
| 1104 | if weight.upper() == 'MEDIAN': |
---|
| 1105 | s = scantable(self._math._averagechannel(scan[0], 'MEDIAN', |
---|
| 1106 | scanav)) |
---|
| 1107 | else: |
---|
| 1108 | s = scantable(self._math._average(scan, mask, weight.upper(), |
---|
| 1109 | scanav)) |
---|
| 1110 | except RuntimeError, msg: |
---|
[989] | 1111 | if rcParams['verbose']: |
---|
| 1112 | print msg |
---|
| 1113 | return |
---|
| 1114 | else: raise |
---|
[1099] | 1115 | s._add_history("average_time", varlist) |
---|
[718] | 1116 | print_log() |
---|
[513] | 1117 | return s |
---|
[710] | 1118 | |
---|
[876] | 1119 | def convert_flux(self, jyperk=None, eta=None, d=None, insitu=None): |
---|
[513] | 1120 | """ |
---|
| 1121 | Return a scan where all spectra are converted to either |
---|
| 1122 | Jansky or Kelvin depending upon the flux units of the scan table. |
---|
| 1123 | By default the function tries to look the values up internally. |
---|
| 1124 | If it can't find them (or if you want to over-ride), you must |
---|
| 1125 | specify EITHER jyperk OR eta (and D which it will try to look up |
---|
| 1126 | also if you don't set it). jyperk takes precedence if you set both. |
---|
| 1127 | Parameters: |
---|
| 1128 | jyperk: the Jy / K conversion factor |
---|
| 1129 | eta: the aperture efficiency |
---|
| 1130 | d: the geomtric diameter (metres) |
---|
| 1131 | insitu: if False a new scantable is returned. |
---|
| 1132 | Otherwise, the scaling is done in-situ |
---|
| 1133 | The default is taken from .asaprc (False) |
---|
| 1134 | """ |
---|
| 1135 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 1136 | self._math._setinsitu(insitu) |
---|
[513] | 1137 | varlist = vars() |
---|
| 1138 | if jyperk is None: jyperk = -1.0 |
---|
| 1139 | if d is None: d = -1.0 |
---|
| 1140 | if eta is None: eta = -1.0 |
---|
[876] | 1141 | s = scantable(self._math._convertflux(self, d, eta, jyperk)) |
---|
| 1142 | s._add_history("convert_flux", varlist) |
---|
| 1143 | print_log() |
---|
| 1144 | if insitu: self._assign(s) |
---|
| 1145 | else: return s |
---|
[513] | 1146 | |
---|
[876] | 1147 | def gain_el(self, poly=None, filename="", method="linear", insitu=None): |
---|
[513] | 1148 | """ |
---|
| 1149 | Return a scan after applying a gain-elevation correction. |
---|
| 1150 | The correction can be made via either a polynomial or a |
---|
| 1151 | table-based interpolation (and extrapolation if necessary). |
---|
| 1152 | You specify polynomial coefficients, an ascii table or neither. |
---|
| 1153 | If you specify neither, then a polynomial correction will be made |
---|
| 1154 | with built in coefficients known for certain telescopes (an error |
---|
| 1155 | will occur if the instrument is not known). |
---|
| 1156 | The data and Tsys are *divided* by the scaling factors. |
---|
| 1157 | Parameters: |
---|
| 1158 | poly: Polynomial coefficients (default None) to compute a |
---|
| 1159 | gain-elevation correction as a function of |
---|
| 1160 | elevation (in degrees). |
---|
| 1161 | filename: The name of an ascii file holding correction factors. |
---|
| 1162 | The first row of the ascii file must give the column |
---|
| 1163 | names and these MUST include columns |
---|
| 1164 | "ELEVATION" (degrees) and "FACTOR" (multiply data |
---|
| 1165 | by this) somewhere. |
---|
| 1166 | The second row must give the data type of the |
---|
| 1167 | column. Use 'R' for Real and 'I' for Integer. |
---|
| 1168 | An example file would be |
---|
| 1169 | (actual factors are arbitrary) : |
---|
| 1170 | |
---|
| 1171 | TIME ELEVATION FACTOR |
---|
| 1172 | R R R |
---|
| 1173 | 0.1 0 0.8 |
---|
| 1174 | 0.2 20 0.85 |
---|
| 1175 | 0.3 40 0.9 |
---|
| 1176 | 0.4 60 0.85 |
---|
| 1177 | 0.5 80 0.8 |
---|
| 1178 | 0.6 90 0.75 |
---|
| 1179 | method: Interpolation method when correcting from a table. |
---|
| 1180 | Values are "nearest", "linear" (default), "cubic" |
---|
| 1181 | and "spline" |
---|
| 1182 | insitu: if False a new scantable is returned. |
---|
| 1183 | Otherwise, the scaling is done in-situ |
---|
| 1184 | The default is taken from .asaprc (False) |
---|
| 1185 | """ |
---|
| 1186 | |
---|
| 1187 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 1188 | self._math._setinsitu(insitu) |
---|
[513] | 1189 | varlist = vars() |
---|
| 1190 | if poly is None: |
---|
[1118] | 1191 | poly = () |
---|
[513] | 1192 | from os.path import expandvars |
---|
| 1193 | filename = expandvars(filename) |
---|
[876] | 1194 | s = scantable(self._math._gainel(self, poly, filename, method)) |
---|
| 1195 | s._add_history("gain_el", varlist) |
---|
| 1196 | print_log() |
---|
| 1197 | if insitu: self._assign(s) |
---|
| 1198 | else: return s |
---|
[710] | 1199 | |
---|
[931] | 1200 | def freq_align(self, reftime=None, method='cubic', insitu=None): |
---|
[513] | 1201 | """ |
---|
| 1202 | Return a scan where all rows have been aligned in frequency/velocity. |
---|
| 1203 | The alignment frequency frame (e.g. LSRK) is that set by function |
---|
| 1204 | set_freqframe. |
---|
| 1205 | Parameters: |
---|
| 1206 | reftime: reference time to align at. By default, the time of |
---|
| 1207 | the first row of data is used. |
---|
| 1208 | method: Interpolation method for regridding the spectra. |
---|
| 1209 | Choose from "nearest", "linear", "cubic" (default) |
---|
| 1210 | and "spline" |
---|
| 1211 | insitu: if False a new scantable is returned. |
---|
| 1212 | Otherwise, the scaling is done in-situ |
---|
| 1213 | The default is taken from .asaprc (False) |
---|
| 1214 | """ |
---|
[931] | 1215 | if insitu is None: insitu = rcParams["insitu"] |
---|
[876] | 1216 | self._math._setinsitu(insitu) |
---|
[513] | 1217 | varlist = vars() |
---|
[931] | 1218 | if reftime is None: reftime = "" |
---|
| 1219 | s = scantable(self._math._freq_align(self, reftime, method)) |
---|
[876] | 1220 | s._add_history("freq_align", varlist) |
---|
| 1221 | print_log() |
---|
| 1222 | if insitu: self._assign(s) |
---|
| 1223 | else: return s |
---|
[513] | 1224 | |
---|
[876] | 1225 | def opacity(self, tau, insitu=None): |
---|
[513] | 1226 | """ |
---|
| 1227 | Apply an opacity correction. The data |
---|
| 1228 | and Tsys are multiplied by the correction factor. |
---|
| 1229 | Parameters: |
---|
| 1230 | tau: Opacity from which the correction factor is |
---|
| 1231 | exp(tau*ZD) |
---|
| 1232 | where ZD is the zenith-distance |
---|
| 1233 | insitu: if False a new scantable is returned. |
---|
| 1234 | Otherwise, the scaling is done in-situ |
---|
| 1235 | The default is taken from .asaprc (False) |
---|
| 1236 | """ |
---|
| 1237 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 1238 | self._math._setinsitu(insitu) |
---|
[513] | 1239 | varlist = vars() |
---|
[876] | 1240 | s = scantable(self._math._opacity(self, tau)) |
---|
| 1241 | s._add_history("opacity", varlist) |
---|
| 1242 | print_log() |
---|
| 1243 | if insitu: self._assign(s) |
---|
| 1244 | else: return s |
---|
[513] | 1245 | |
---|
| 1246 | def bin(self, width=5, insitu=None): |
---|
| 1247 | """ |
---|
| 1248 | Return a scan where all spectra have been binned up. |
---|
[1348] | 1249 | Parameters: |
---|
[513] | 1250 | width: The bin width (default=5) in pixels |
---|
| 1251 | insitu: if False a new scantable is returned. |
---|
| 1252 | Otherwise, the scaling is done in-situ |
---|
| 1253 | The default is taken from .asaprc (False) |
---|
| 1254 | """ |
---|
| 1255 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 1256 | self._math._setinsitu(insitu) |
---|
[513] | 1257 | varlist = vars() |
---|
[876] | 1258 | s = scantable(self._math._bin(self, width)) |
---|
[1118] | 1259 | s._add_history("bin", varlist) |
---|
[876] | 1260 | print_log() |
---|
| 1261 | if insitu: self._assign(s) |
---|
| 1262 | else: return s |
---|
[513] | 1263 | |
---|
[710] | 1264 | |
---|
[513] | 1265 | def resample(self, width=5, method='cubic', insitu=None): |
---|
| 1266 | """ |
---|
[1348] | 1267 | Return a scan where all spectra have been binned up. |
---|
| 1268 | |
---|
| 1269 | Parameters: |
---|
[513] | 1270 | width: The bin width (default=5) in pixels |
---|
| 1271 | method: Interpolation method when correcting from a table. |
---|
| 1272 | Values are "nearest", "linear", "cubic" (default) |
---|
| 1273 | and "spline" |
---|
| 1274 | insitu: if False a new scantable is returned. |
---|
| 1275 | Otherwise, the scaling is done in-situ |
---|
| 1276 | The default is taken from .asaprc (False) |
---|
| 1277 | """ |
---|
| 1278 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 1279 | self._math._setinsitu(insitu) |
---|
[513] | 1280 | varlist = vars() |
---|
[876] | 1281 | s = scantable(self._math._resample(self, method, width)) |
---|
[1118] | 1282 | s._add_history("resample", varlist) |
---|
[876] | 1283 | print_log() |
---|
| 1284 | if insitu: self._assign(s) |
---|
| 1285 | else: return s |
---|
[513] | 1286 | |
---|
| 1287 | |
---|
[946] | 1288 | def average_pol(self, mask=None, weight='none'): |
---|
| 1289 | """ |
---|
| 1290 | Average the Polarisations together. |
---|
| 1291 | Parameters: |
---|
| 1292 | mask: An optional mask defining the region, where the |
---|
| 1293 | averaging will be applied. The output will have all |
---|
| 1294 | specified points masked. |
---|
| 1295 | weight: Weighting scheme. 'none' (default), 'var' (1/var(spec) |
---|
| 1296 | weighted), or 'tsys' (1/Tsys**2 weighted) |
---|
| 1297 | """ |
---|
| 1298 | varlist = vars() |
---|
| 1299 | if mask is None: |
---|
| 1300 | mask = () |
---|
[1010] | 1301 | s = scantable(self._math._averagepol(self, mask, weight.upper())) |
---|
[1118] | 1302 | s._add_history("average_pol", varlist) |
---|
[946] | 1303 | print_log() |
---|
[992] | 1304 | return s |
---|
[513] | 1305 | |
---|
[1145] | 1306 | def average_beam(self, mask=None, weight='none'): |
---|
| 1307 | """ |
---|
| 1308 | Average the Beams together. |
---|
| 1309 | Parameters: |
---|
| 1310 | mask: An optional mask defining the region, where the |
---|
| 1311 | averaging will be applied. The output will have all |
---|
| 1312 | specified points masked. |
---|
| 1313 | weight: Weighting scheme. 'none' (default), 'var' (1/var(spec) |
---|
| 1314 | weighted), or 'tsys' (1/Tsys**2 weighted) |
---|
| 1315 | """ |
---|
| 1316 | varlist = vars() |
---|
| 1317 | if mask is None: |
---|
| 1318 | mask = () |
---|
| 1319 | s = scantable(self._math._averagebeams(self, mask, weight.upper())) |
---|
| 1320 | s._add_history("average_beam", varlist) |
---|
| 1321 | print_log() |
---|
| 1322 | return s |
---|
| 1323 | |
---|
[992] | 1324 | def convert_pol(self, poltype=None): |
---|
| 1325 | """ |
---|
| 1326 | Convert the data to a different polarisation type. |
---|
| 1327 | Parameters: |
---|
| 1328 | poltype: The new polarisation type. Valid types are: |
---|
| 1329 | "linear", "stokes" and "circular" |
---|
| 1330 | """ |
---|
| 1331 | varlist = vars() |
---|
| 1332 | try: |
---|
| 1333 | s = scantable(self._math._convertpol(self, poltype)) |
---|
[1118] | 1334 | except RuntimeError, msg: |
---|
[992] | 1335 | if rcParams['verbose']: |
---|
[1118] | 1336 | print msg |
---|
| 1337 | return |
---|
[992] | 1338 | else: |
---|
| 1339 | raise |
---|
[1118] | 1340 | s._add_history("convert_pol", varlist) |
---|
[992] | 1341 | print_log() |
---|
| 1342 | return s |
---|
| 1343 | |
---|
[876] | 1344 | def smooth(self, kernel="hanning", width=5.0, insitu=None): |
---|
[513] | 1345 | """ |
---|
| 1346 | Smooth the spectrum by the specified kernel (conserving flux). |
---|
| 1347 | Parameters: |
---|
| 1348 | kernel: The type of smoothing kernel. Select from |
---|
[1373] | 1349 | 'hanning' (default), 'gaussian', 'boxcar' and |
---|
| 1350 | 'rmedian' |
---|
[513] | 1351 | width: The width of the kernel in pixels. For hanning this is |
---|
| 1352 | ignored otherwise it defauls to 5 pixels. |
---|
| 1353 | For 'gaussian' it is the Full Width Half |
---|
| 1354 | Maximum. For 'boxcar' it is the full width. |
---|
[1373] | 1355 | For 'rmedian' it is the half width. |
---|
[513] | 1356 | insitu: if False a new scantable is returned. |
---|
| 1357 | Otherwise, the scaling is done in-situ |
---|
| 1358 | The default is taken from .asaprc (False) |
---|
| 1359 | Example: |
---|
| 1360 | none |
---|
| 1361 | """ |
---|
| 1362 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 1363 | self._math._setinsitu(insitu) |
---|
[513] | 1364 | varlist = vars() |
---|
[1118] | 1365 | s = scantable(self._math._smooth(self, kernel.lower(), width)) |
---|
[876] | 1366 | s._add_history("smooth", varlist) |
---|
| 1367 | print_log() |
---|
| 1368 | if insitu: self._assign(s) |
---|
| 1369 | else: return s |
---|
[513] | 1370 | |
---|
[876] | 1371 | |
---|
[1389] | 1372 | def poly_baseline(self, mask=None, order=0, plot=False, uselin=False, insitu=None): |
---|
[513] | 1373 | """ |
---|
| 1374 | Return a scan which has been baselined (all rows) by a polynomial. |
---|
| 1375 | Parameters: |
---|
[794] | 1376 | mask: an optional mask |
---|
| 1377 | order: the order of the polynomial (default is 0) |
---|
[1061] | 1378 | plot: plot the fit and the residual. In this each |
---|
| 1379 | indivual fit has to be approved, by typing 'y' |
---|
| 1380 | or 'n' |
---|
[1389] | 1381 | uselin: use linear polynomial fit |
---|
[794] | 1382 | insitu: if False a new scantable is returned. |
---|
| 1383 | Otherwise, the scaling is done in-situ |
---|
| 1384 | The default is taken from .asaprc (False) |
---|
[513] | 1385 | Example: |
---|
| 1386 | # return a scan baselined by a third order polynomial, |
---|
| 1387 | # not using a mask |
---|
| 1388 | bscan = scan.poly_baseline(order=3) |
---|
[579] | 1389 | """ |
---|
[513] | 1390 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 1391 | varlist = vars() |
---|
| 1392 | if mask is None: |
---|
[1295] | 1393 | mask = [True for i in xrange(self.nchan(-1))] |
---|
[513] | 1394 | from asap.asapfitter import fitter |
---|
[1217] | 1395 | try: |
---|
| 1396 | f = fitter() |
---|
| 1397 | f.set_scan(self, mask) |
---|
[1389] | 1398 | if uselin: |
---|
| 1399 | f.set_function(lpoly=order) |
---|
| 1400 | else: |
---|
| 1401 | f.set_function(poly=order) |
---|
[1217] | 1402 | s = f.auto_fit(insitu, plot=plot) |
---|
[1446] | 1403 | # Save parameters of baseline fits as a class attribute. |
---|
| 1404 | # NOTICE: It does not reflect changes in scantable! |
---|
| 1405 | self.blpars = f.blpars |
---|
[1217] | 1406 | s._add_history("poly_baseline", varlist) |
---|
| 1407 | print_log() |
---|
| 1408 | if insitu: self._assign(s) |
---|
| 1409 | else: return s |
---|
| 1410 | except RuntimeError: |
---|
| 1411 | msg = "The fit failed, possibly because it didn't converge." |
---|
| 1412 | if rcParams['verbose']: |
---|
| 1413 | print msg |
---|
| 1414 | return |
---|
| 1415 | else: |
---|
| 1416 | raise RuntimeError(msg) |
---|
[513] | 1417 | |
---|
[1217] | 1418 | |
---|
[1118] | 1419 | def auto_poly_baseline(self, mask=[], edge=(0, 0), order=0, |
---|
[1280] | 1420 | threshold=3, chan_avg_limit=1, plot=False, |
---|
| 1421 | insitu=None): |
---|
[880] | 1422 | """ |
---|
| 1423 | Return a scan which has been baselined (all rows) by a polynomial. |
---|
| 1424 | Spectral lines are detected first using linefinder and masked out |
---|
| 1425 | to avoid them affecting the baseline solution. |
---|
| 1426 | |
---|
| 1427 | Parameters: |
---|
| 1428 | mask: an optional mask retreived from scantable |
---|
| 1429 | edge: an optional number of channel to drop at |
---|
| 1430 | the edge of spectrum. If only one value is |
---|
| 1431 | specified, the same number will be dropped from |
---|
| 1432 | both sides of the spectrum. Default is to keep |
---|
[907] | 1433 | all channels. Nested tuples represent individual |
---|
[976] | 1434 | edge selection for different IFs (a number of spectral |
---|
| 1435 | channels can be different) |
---|
[880] | 1436 | order: the order of the polynomial (default is 0) |
---|
| 1437 | threshold: the threshold used by line finder. It is better to |
---|
| 1438 | keep it large as only strong lines affect the |
---|
| 1439 | baseline solution. |
---|
[1280] | 1440 | chan_avg_limit: |
---|
| 1441 | a maximum number of consequtive spectral channels to |
---|
| 1442 | average during the search of weak and broad lines. |
---|
| 1443 | The default is no averaging (and no search for weak |
---|
| 1444 | lines). If such lines can affect the fitted baseline |
---|
| 1445 | (e.g. a high order polynomial is fitted), increase this |
---|
| 1446 | parameter (usually values up to 8 are reasonable). Most |
---|
| 1447 | users of this method should find the default value |
---|
| 1448 | sufficient. |
---|
[1061] | 1449 | plot: plot the fit and the residual. In this each |
---|
| 1450 | indivual fit has to be approved, by typing 'y' |
---|
| 1451 | or 'n' |
---|
[880] | 1452 | insitu: if False a new scantable is returned. |
---|
| 1453 | Otherwise, the scaling is done in-situ |
---|
| 1454 | The default is taken from .asaprc (False) |
---|
| 1455 | |
---|
| 1456 | Example: |
---|
| 1457 | scan2=scan.auto_poly_baseline(order=7) |
---|
| 1458 | """ |
---|
| 1459 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 1460 | varlist = vars() |
---|
| 1461 | from asap.asapfitter import fitter |
---|
| 1462 | from asap.asaplinefind import linefinder |
---|
| 1463 | from asap import _is_sequence_or_number as _is_valid |
---|
| 1464 | |
---|
[976] | 1465 | # check whether edge is set up for each IF individually |
---|
[1118] | 1466 | individualedge = False; |
---|
| 1467 | if len(edge) > 1: |
---|
| 1468 | if isinstance(edge[0], list) or isinstance(edge[0], tuple): |
---|
| 1469 | individualedge = True; |
---|
[907] | 1470 | |
---|
[1118] | 1471 | if not _is_valid(edge, int) and not individualedge: |
---|
[909] | 1472 | raise ValueError, "Parameter 'edge' has to be an integer or a \ |
---|
[907] | 1473 | pair of integers specified as a tuple. Nested tuples are allowed \ |
---|
| 1474 | to make individual selection for different IFs." |
---|
[919] | 1475 | |
---|
[1118] | 1476 | curedge = (0, 0) |
---|
| 1477 | if individualedge: |
---|
| 1478 | for edgepar in edge: |
---|
| 1479 | if not _is_valid(edgepar, int): |
---|
| 1480 | raise ValueError, "Each element of the 'edge' tuple has \ |
---|
| 1481 | to be a pair of integers or an integer." |
---|
[907] | 1482 | else: |
---|
[1118] | 1483 | curedge = edge; |
---|
[880] | 1484 | |
---|
| 1485 | # setup fitter |
---|
| 1486 | f = fitter() |
---|
| 1487 | f.set_function(poly=order) |
---|
| 1488 | |
---|
| 1489 | # setup line finder |
---|
[1118] | 1490 | fl = linefinder() |
---|
[1268] | 1491 | fl.set_options(threshold=threshold,avg_limit=chan_avg_limit) |
---|
[880] | 1492 | |
---|
| 1493 | if not insitu: |
---|
[1118] | 1494 | workscan = self.copy() |
---|
[880] | 1495 | else: |
---|
[1118] | 1496 | workscan = self |
---|
[880] | 1497 | |
---|
[907] | 1498 | fl.set_scan(workscan) |
---|
| 1499 | |
---|
[1118] | 1500 | rows = range(workscan.nrow()) |
---|
[1446] | 1501 | # Save parameters of baseline fits & masklists as a class attribute. |
---|
| 1502 | # NOTICE: It does not reflect changes in scantable! |
---|
| 1503 | if len(rows) > 0: |
---|
| 1504 | self.blpars=[] |
---|
| 1505 | self.masklists=[] |
---|
[880] | 1506 | asaplog.push("Processing:") |
---|
| 1507 | for r in rows: |
---|
[1118] | 1508 | msg = " Scan[%d] Beam[%d] IF[%d] Pol[%d] Cycle[%d]" % \ |
---|
| 1509 | (workscan.getscan(r), workscan.getbeam(r), workscan.getif(r), \ |
---|
| 1510 | workscan.getpol(r), workscan.getcycle(r)) |
---|
[880] | 1511 | asaplog.push(msg, False) |
---|
[907] | 1512 | |
---|
[976] | 1513 | # figure out edge parameter |
---|
[1118] | 1514 | if individualedge: |
---|
| 1515 | if len(edge) >= workscan.getif(r): |
---|
| 1516 | raise RuntimeError, "Number of edge elements appear to " \ |
---|
| 1517 | "be less than the number of IFs" |
---|
| 1518 | curedge = edge[workscan.getif(r)] |
---|
[919] | 1519 | |
---|
[976] | 1520 | # setup line finder |
---|
[1118] | 1521 | fl.find_lines(r, mask, curedge) |
---|
[1446] | 1522 | outmask=fl.get_mask() |
---|
[880] | 1523 | f.set_scan(workscan, fl.get_mask()) |
---|
| 1524 | f.x = workscan._getabcissa(r) |
---|
| 1525 | f.y = workscan._getspectrum(r) |
---|
| 1526 | f.data = None |
---|
| 1527 | f.fit() |
---|
[1446] | 1528 | |
---|
| 1529 | # Show mask list |
---|
| 1530 | masklist=workscan.get_masklist(fl.get_mask(),row=r) |
---|
| 1531 | msg = "mask range: "+str(masklist) |
---|
| 1532 | asaplog.push(msg, False) |
---|
| 1533 | |
---|
| 1534 | fpar = f.get_parameters() |
---|
[1061] | 1535 | if plot: |
---|
| 1536 | f.plot(residual=True) |
---|
| 1537 | x = raw_input("Accept fit ( [y]/n ): ") |
---|
| 1538 | if x.upper() == 'N': |
---|
[1446] | 1539 | self.blpars.append(None) |
---|
| 1540 | self.masklists.append(None) |
---|
[1061] | 1541 | continue |
---|
[880] | 1542 | workscan._setspectrum(f.fitter.getresidual(), r) |
---|
[1446] | 1543 | self.blpars.append(fpar) |
---|
| 1544 | self.masklists.append(masklist) |
---|
[1061] | 1545 | if plot: |
---|
| 1546 | f._p.unmap() |
---|
| 1547 | f._p = None |
---|
| 1548 | workscan._add_history("auto_poly_baseline", varlist) |
---|
[880] | 1549 | if insitu: |
---|
| 1550 | self._assign(workscan) |
---|
| 1551 | else: |
---|
| 1552 | return workscan |
---|
| 1553 | |
---|
[914] | 1554 | def rotate_linpolphase(self, angle): |
---|
| 1555 | """ |
---|
| 1556 | Rotate the phase of the complex polarization O=Q+iU correlation. |
---|
| 1557 | This is always done in situ in the raw data. So if you call this |
---|
| 1558 | function more than once then each call rotates the phase further. |
---|
| 1559 | Parameters: |
---|
| 1560 | angle: The angle (degrees) to rotate (add) by. |
---|
| 1561 | Examples: |
---|
| 1562 | scan.rotate_linpolphase(2.3) |
---|
| 1563 | """ |
---|
| 1564 | varlist = vars() |
---|
[936] | 1565 | self._math._rotate_linpolphase(self, angle) |
---|
[914] | 1566 | self._add_history("rotate_linpolphase", varlist) |
---|
| 1567 | print_log() |
---|
| 1568 | return |
---|
[710] | 1569 | |
---|
[513] | 1570 | |
---|
[914] | 1571 | def rotate_xyphase(self, angle): |
---|
| 1572 | """ |
---|
| 1573 | Rotate the phase of the XY correlation. This is always done in situ |
---|
| 1574 | in the data. So if you call this function more than once |
---|
| 1575 | then each call rotates the phase further. |
---|
| 1576 | Parameters: |
---|
| 1577 | angle: The angle (degrees) to rotate (add) by. |
---|
| 1578 | Examples: |
---|
| 1579 | scan.rotate_xyphase(2.3) |
---|
| 1580 | """ |
---|
| 1581 | varlist = vars() |
---|
[936] | 1582 | self._math._rotate_xyphase(self, angle) |
---|
[914] | 1583 | self._add_history("rotate_xyphase", varlist) |
---|
| 1584 | print_log() |
---|
| 1585 | return |
---|
| 1586 | |
---|
| 1587 | def swap_linears(self): |
---|
| 1588 | """ |
---|
[1348] | 1589 | Swap the linear polarisations XX and YY, or better the first two |
---|
| 1590 | polarisations as this also works for ciculars. |
---|
[914] | 1591 | """ |
---|
| 1592 | varlist = vars() |
---|
[936] | 1593 | self._math._swap_linears(self) |
---|
[914] | 1594 | self._add_history("swap_linears", varlist) |
---|
| 1595 | print_log() |
---|
| 1596 | return |
---|
| 1597 | |
---|
| 1598 | def invert_phase(self): |
---|
| 1599 | """ |
---|
| 1600 | Invert the phase of the complex polarisation |
---|
| 1601 | """ |
---|
| 1602 | varlist = vars() |
---|
[936] | 1603 | self._math._invert_phase(self) |
---|
[914] | 1604 | self._add_history("invert_phase", varlist) |
---|
| 1605 | print_log() |
---|
| 1606 | return |
---|
| 1607 | |
---|
[876] | 1608 | def add(self, offset, insitu=None): |
---|
[513] | 1609 | """ |
---|
| 1610 | Return a scan where all spectra have the offset added |
---|
| 1611 | Parameters: |
---|
| 1612 | offset: the offset |
---|
| 1613 | insitu: if False a new scantable is returned. |
---|
| 1614 | Otherwise, the scaling is done in-situ |
---|
| 1615 | The default is taken from .asaprc (False) |
---|
| 1616 | """ |
---|
| 1617 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 1618 | self._math._setinsitu(insitu) |
---|
[513] | 1619 | varlist = vars() |
---|
[876] | 1620 | s = scantable(self._math._unaryop(self, offset, "ADD", False)) |
---|
[1118] | 1621 | s._add_history("add", varlist) |
---|
[876] | 1622 | print_log() |
---|
| 1623 | if insitu: |
---|
| 1624 | self._assign(s) |
---|
| 1625 | else: |
---|
[513] | 1626 | return s |
---|
| 1627 | |
---|
[1308] | 1628 | def scale(self, factor, tsys=True, insitu=None): |
---|
[513] | 1629 | """ |
---|
| 1630 | Return a scan where all spectra are scaled by the give 'factor' |
---|
| 1631 | Parameters: |
---|
| 1632 | factor: the scaling factor |
---|
| 1633 | insitu: if False a new scantable is returned. |
---|
| 1634 | Otherwise, the scaling is done in-situ |
---|
| 1635 | The default is taken from .asaprc (False) |
---|
| 1636 | tsys: if True (default) then apply the operation to Tsys |
---|
| 1637 | as well as the data |
---|
| 1638 | """ |
---|
| 1639 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 1640 | self._math._setinsitu(insitu) |
---|
[513] | 1641 | varlist = vars() |
---|
[876] | 1642 | s = scantable(self._math._unaryop(self, factor, "MUL", tsys)) |
---|
[1118] | 1643 | s._add_history("scale", varlist) |
---|
[876] | 1644 | print_log() |
---|
| 1645 | if insitu: |
---|
| 1646 | self._assign(s) |
---|
| 1647 | else: |
---|
[513] | 1648 | return s |
---|
| 1649 | |
---|
[1348] | 1650 | def auto_quotient(self, preserve=True, mode='paired'): |
---|
[670] | 1651 | """ |
---|
| 1652 | This function allows to build quotients automatically. |
---|
| 1653 | It assumes the observation to have the same numer of |
---|
| 1654 | "ons" and "offs" |
---|
| 1655 | Parameters: |
---|
[710] | 1656 | preserve: you can preserve (default) the continuum or |
---|
| 1657 | remove it. The equations used are |
---|
[670] | 1658 | preserve: Output = Toff * (on/off) - Toff |
---|
[1070] | 1659 | remove: Output = Toff * (on/off) - Ton |
---|
[1348] | 1660 | mode: the on/off detection mode |
---|
| 1661 | 'paired' (default) |
---|
| 1662 | identifies 'off' scans by the |
---|
| 1663 | trailing '_R' (Mopra/Parkes) or |
---|
| 1664 | '_e'/'_w' (Tid) and matches |
---|
| 1665 | on/off pairs from the observing pattern |
---|
| 1666 | 'time' |
---|
| 1667 | finds the closest off in time |
---|
| 1668 | |
---|
[670] | 1669 | """ |
---|
[1348] | 1670 | modes = ["time", "paired"] |
---|
[670] | 1671 | if not mode in modes: |
---|
[876] | 1672 | msg = "please provide valid mode. Valid modes are %s" % (modes) |
---|
| 1673 | raise ValueError(msg) |
---|
| 1674 | varlist = vars() |
---|
[1348] | 1675 | s = None |
---|
| 1676 | if mode.lower() == "paired": |
---|
| 1677 | basesel = self.get_selection() |
---|
[1356] | 1678 | sel = selector()+basesel |
---|
| 1679 | sel.set_query("SRCTYPE==1") |
---|
| 1680 | self.set_selection(sel) |
---|
[1348] | 1681 | offs = self.copy() |
---|
| 1682 | sel.set_query("SRCTYPE==0") |
---|
[1356] | 1683 | self.set_selection(sel) |
---|
[1348] | 1684 | ons = self.copy() |
---|
| 1685 | s = scantable(self._math._quotient(ons, offs, preserve)) |
---|
| 1686 | self.set_selection(basesel) |
---|
| 1687 | elif mode.lower() == "time": |
---|
| 1688 | s = scantable(self._math._auto_quotient(self, mode, preserve)) |
---|
[1118] | 1689 | s._add_history("auto_quotient", varlist) |
---|
[876] | 1690 | print_log() |
---|
| 1691 | return s |
---|
[710] | 1692 | |
---|
[1145] | 1693 | def mx_quotient(self, mask = None, weight='median', preserve=True): |
---|
[1141] | 1694 | """ |
---|
[1143] | 1695 | Form a quotient using "off" beams when observing in "MX" mode. |
---|
| 1696 | Parameters: |
---|
[1145] | 1697 | mask: an optional mask to be used when weight == 'stddev' |
---|
[1143] | 1698 | weight: How to average the off beams. Default is 'median'. |
---|
[1145] | 1699 | preserve: you can preserve (default) the continuum or |
---|
| 1700 | remove it. The equations used are |
---|
| 1701 | preserve: Output = Toff * (on/off) - Toff |
---|
| 1702 | remove: Output = Toff * (on/off) - Ton |
---|
[1217] | 1703 | """ |
---|
[1143] | 1704 | if mask is None: mask = () |
---|
[1141] | 1705 | varlist = vars() |
---|
| 1706 | on = scantable(self._math._mx_extract(self, 'on')) |
---|
[1143] | 1707 | preoff = scantable(self._math._mx_extract(self, 'off')) |
---|
| 1708 | off = preoff.average_time(mask=mask, weight=weight, scanav=False) |
---|
[1217] | 1709 | from asapmath import quotient |
---|
[1145] | 1710 | q = quotient(on, off, preserve) |
---|
[1143] | 1711 | q._add_history("mx_quotient", varlist) |
---|
[1145] | 1712 | print_log() |
---|
[1217] | 1713 | return q |
---|
[513] | 1714 | |
---|
[718] | 1715 | def freq_switch(self, insitu=None): |
---|
| 1716 | """ |
---|
| 1717 | Apply frequency switching to the data. |
---|
| 1718 | Parameters: |
---|
| 1719 | insitu: if False a new scantable is returned. |
---|
| 1720 | Otherwise, the swictching is done in-situ |
---|
| 1721 | The default is taken from .asaprc (False) |
---|
| 1722 | Example: |
---|
| 1723 | none |
---|
| 1724 | """ |
---|
| 1725 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 1726 | self._math._setinsitu(insitu) |
---|
[718] | 1727 | varlist = vars() |
---|
[876] | 1728 | s = scantable(self._math._freqswitch(self)) |
---|
[1118] | 1729 | s._add_history("freq_switch", varlist) |
---|
[876] | 1730 | print_log() |
---|
| 1731 | if insitu: self._assign(s) |
---|
| 1732 | else: return s |
---|
[718] | 1733 | |
---|
[780] | 1734 | def recalc_azel(self): |
---|
| 1735 | """ |
---|
| 1736 | Recalculate the azimuth and elevation for each position. |
---|
| 1737 | Parameters: |
---|
| 1738 | none |
---|
| 1739 | Example: |
---|
| 1740 | """ |
---|
| 1741 | varlist = vars() |
---|
[876] | 1742 | self._recalcazel() |
---|
[780] | 1743 | self._add_history("recalc_azel", varlist) |
---|
| 1744 | print_log() |
---|
| 1745 | return |
---|
| 1746 | |
---|
[513] | 1747 | def __add__(self, other): |
---|
| 1748 | varlist = vars() |
---|
| 1749 | s = None |
---|
| 1750 | if isinstance(other, scantable): |
---|
[1308] | 1751 | s = scantable(self._math._binaryop(self, other, "ADD")) |
---|
[513] | 1752 | elif isinstance(other, float): |
---|
[876] | 1753 | s = scantable(self._math._unaryop(self, other, "ADD", False)) |
---|
[513] | 1754 | else: |
---|
[718] | 1755 | raise TypeError("Other input is not a scantable or float value") |
---|
[513] | 1756 | s._add_history("operator +", varlist) |
---|
[718] | 1757 | print_log() |
---|
[513] | 1758 | return s |
---|
| 1759 | |
---|
| 1760 | def __sub__(self, other): |
---|
| 1761 | """ |
---|
| 1762 | implicit on all axes and on Tsys |
---|
| 1763 | """ |
---|
| 1764 | varlist = vars() |
---|
| 1765 | s = None |
---|
| 1766 | if isinstance(other, scantable): |
---|
[1308] | 1767 | s = scantable(self._math._binaryop(self, other, "SUB")) |
---|
[513] | 1768 | elif isinstance(other, float): |
---|
[876] | 1769 | s = scantable(self._math._unaryop(self, other, "SUB", False)) |
---|
[513] | 1770 | else: |
---|
[718] | 1771 | raise TypeError("Other input is not a scantable or float value") |
---|
[513] | 1772 | s._add_history("operator -", varlist) |
---|
[718] | 1773 | print_log() |
---|
[513] | 1774 | return s |
---|
[710] | 1775 | |
---|
[513] | 1776 | def __mul__(self, other): |
---|
| 1777 | """ |
---|
| 1778 | implicit on all axes and on Tsys |
---|
| 1779 | """ |
---|
| 1780 | varlist = vars() |
---|
| 1781 | s = None |
---|
| 1782 | if isinstance(other, scantable): |
---|
[1308] | 1783 | s = scantable(self._math._binaryop(self, other, "MUL")) |
---|
[513] | 1784 | elif isinstance(other, float): |
---|
[876] | 1785 | s = scantable(self._math._unaryop(self, other, "MUL", False)) |
---|
[513] | 1786 | else: |
---|
[718] | 1787 | raise TypeError("Other input is not a scantable or float value") |
---|
[513] | 1788 | s._add_history("operator *", varlist) |
---|
[718] | 1789 | print_log() |
---|
[513] | 1790 | return s |
---|
| 1791 | |
---|
[710] | 1792 | |
---|
[513] | 1793 | def __div__(self, other): |
---|
| 1794 | """ |
---|
| 1795 | implicit on all axes and on Tsys |
---|
| 1796 | """ |
---|
| 1797 | varlist = vars() |
---|
| 1798 | s = None |
---|
| 1799 | if isinstance(other, scantable): |
---|
[1308] | 1800 | s = scantable(self._math._binaryop(self, other, "DIV")) |
---|
[513] | 1801 | elif isinstance(other, float): |
---|
| 1802 | if other == 0.0: |
---|
[718] | 1803 | raise ZeroDivisionError("Dividing by zero is not recommended") |
---|
[876] | 1804 | s = scantable(self._math._unaryop(self, other, "DIV", False)) |
---|
[513] | 1805 | else: |
---|
[718] | 1806 | raise TypeError("Other input is not a scantable or float value") |
---|
[513] | 1807 | s._add_history("operator /", varlist) |
---|
[718] | 1808 | print_log() |
---|
[513] | 1809 | return s |
---|
| 1810 | |
---|
[530] | 1811 | def get_fit(self, row=0): |
---|
| 1812 | """ |
---|
| 1813 | Print or return the stored fits for a row in the scantable |
---|
| 1814 | Parameters: |
---|
| 1815 | row: the row which the fit has been applied to. |
---|
| 1816 | """ |
---|
| 1817 | if row > self.nrow(): |
---|
| 1818 | return |
---|
[976] | 1819 | from asap.asapfit import asapfit |
---|
[530] | 1820 | fit = asapfit(self._getfit(row)) |
---|
[718] | 1821 | if rcParams['verbose']: |
---|
[530] | 1822 | print fit |
---|
| 1823 | return |
---|
| 1824 | else: |
---|
| 1825 | return fit.as_dict() |
---|
| 1826 | |
---|
[484] | 1827 | def _add_history(self, funcname, parameters): |
---|
| 1828 | # create date |
---|
| 1829 | sep = "##" |
---|
| 1830 | from datetime import datetime |
---|
| 1831 | dstr = datetime.now().strftime('%Y/%m/%d %H:%M:%S') |
---|
| 1832 | hist = dstr+sep |
---|
| 1833 | hist += funcname+sep#cdate+sep |
---|
| 1834 | if parameters.has_key('self'): del parameters['self'] |
---|
[1118] | 1835 | for k, v in parameters.iteritems(): |
---|
[484] | 1836 | if type(v) is dict: |
---|
[1118] | 1837 | for k2, v2 in v.iteritems(): |
---|
[484] | 1838 | hist += k2 |
---|
| 1839 | hist += "=" |
---|
[1118] | 1840 | if isinstance(v2, scantable): |
---|
[484] | 1841 | hist += 'scantable' |
---|
| 1842 | elif k2 == 'mask': |
---|
[1118] | 1843 | if isinstance(v2, list) or isinstance(v2, tuple): |
---|
[513] | 1844 | hist += str(self._zip_mask(v2)) |
---|
| 1845 | else: |
---|
| 1846 | hist += str(v2) |
---|
[484] | 1847 | else: |
---|
[513] | 1848 | hist += str(v2) |
---|
[484] | 1849 | else: |
---|
| 1850 | hist += k |
---|
| 1851 | hist += "=" |
---|
[1118] | 1852 | if isinstance(v, scantable): |
---|
[484] | 1853 | hist += 'scantable' |
---|
| 1854 | elif k == 'mask': |
---|
[1118] | 1855 | if isinstance(v, list) or isinstance(v, tuple): |
---|
[513] | 1856 | hist += str(self._zip_mask(v)) |
---|
| 1857 | else: |
---|
| 1858 | hist += str(v) |
---|
[484] | 1859 | else: |
---|
| 1860 | hist += str(v) |
---|
| 1861 | hist += sep |
---|
| 1862 | hist = hist[:-2] # remove trailing '##' |
---|
| 1863 | self._addhistory(hist) |
---|
| 1864 | |
---|
[710] | 1865 | |
---|
[484] | 1866 | def _zip_mask(self, mask): |
---|
| 1867 | mask = list(mask) |
---|
| 1868 | i = 0 |
---|
| 1869 | segments = [] |
---|
| 1870 | while mask[i:].count(1): |
---|
| 1871 | i += mask[i:].index(1) |
---|
| 1872 | if mask[i:].count(0): |
---|
| 1873 | j = i + mask[i:].index(0) |
---|
| 1874 | else: |
---|
[710] | 1875 | j = len(mask) |
---|
[1118] | 1876 | segments.append([i, j]) |
---|
[710] | 1877 | i = j |
---|
[484] | 1878 | return segments |
---|
[714] | 1879 | |
---|
[626] | 1880 | def _get_ordinate_label(self): |
---|
| 1881 | fu = "("+self.get_fluxunit()+")" |
---|
| 1882 | import re |
---|
| 1883 | lbl = "Intensity" |
---|
[1118] | 1884 | if re.match(".K.", fu): |
---|
[626] | 1885 | lbl = "Brightness Temperature "+ fu |
---|
[1118] | 1886 | elif re.match(".Jy.", fu): |
---|
[626] | 1887 | lbl = "Flux density "+ fu |
---|
| 1888 | return lbl |
---|
[710] | 1889 | |
---|
[876] | 1890 | def _check_ifs(self): |
---|
| 1891 | nchans = [self.nchan(i) for i in range(self.nif(-1))] |
---|
[889] | 1892 | nchans = filter(lambda t: t > 0, nchans) |
---|
[876] | 1893 | return (sum(nchans)/len(nchans) == nchans[0]) |
---|
[976] | 1894 | |
---|
[1496] | 1895 | def _fill(self, names, unit, average, getpt): |
---|
[976] | 1896 | import os |
---|
| 1897 | from asap._asap import stfiller |
---|
| 1898 | first = True |
---|
| 1899 | fullnames = [] |
---|
| 1900 | for name in names: |
---|
| 1901 | name = os.path.expandvars(name) |
---|
| 1902 | name = os.path.expanduser(name) |
---|
| 1903 | if not os.path.exists(name): |
---|
| 1904 | msg = "File '%s' does not exists" % (name) |
---|
| 1905 | if rcParams['verbose']: |
---|
| 1906 | asaplog.push(msg) |
---|
| 1907 | print asaplog.pop().strip() |
---|
| 1908 | return |
---|
| 1909 | raise IOError(msg) |
---|
| 1910 | fullnames.append(name) |
---|
| 1911 | if average: |
---|
| 1912 | asaplog.push('Auto averaging integrations') |
---|
[1079] | 1913 | stype = int(rcParams['scantable.storage'].lower() == 'disk') |
---|
[976] | 1914 | for name in fullnames: |
---|
[1073] | 1915 | tbl = Scantable(stype) |
---|
| 1916 | r = stfiller(tbl) |
---|
[976] | 1917 | msg = "Importing %s..." % (name) |
---|
[1118] | 1918 | asaplog.push(msg, False) |
---|
[976] | 1919 | print_log() |
---|
[1496] | 1920 | r._open(name, -1, -1, getpt) |
---|
[976] | 1921 | r._read() |
---|
[1073] | 1922 | #tbl = r._getdata() |
---|
[976] | 1923 | if average: |
---|
[1118] | 1924 | tbl = self._math._average((tbl, ), (), 'NONE', 'SCAN') |
---|
[976] | 1925 | #tbl = tbl2 |
---|
| 1926 | if not first: |
---|
| 1927 | tbl = self._math._merge([self, tbl]) |
---|
| 1928 | #tbl = tbl2 |
---|
| 1929 | Scantable.__init__(self, tbl) |
---|
| 1930 | r._close() |
---|
[1118] | 1931 | del r, tbl |
---|
[976] | 1932 | first = False |
---|
| 1933 | if unit is not None: |
---|
| 1934 | self.set_fluxunit(unit) |
---|
[1446] | 1935 | #self.set_freqframe(rcParams['scantable.freqframe']) |
---|
[976] | 1936 | |
---|