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