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