[102] | 1 | from asap._asap import sdtable
|
---|
[226] | 2 | from asap import rcParams
|
---|
[189] | 3 | from numarray import ones,zeros
|
---|
[102] | 4 | import sys
|
---|
| 5 |
|
---|
| 6 | class scantable(sdtable):
|
---|
| 7 | """
|
---|
| 8 | The ASAP container for scans
|
---|
| 9 | """
|
---|
[113] | 10 |
|
---|
[340] | 11 | def __init__(self, filename, unit=None):
|
---|
[102] | 12 | """
|
---|
| 13 | Create a scantable from a saved one or make a reference
|
---|
| 14 | Parameters:
|
---|
[181] | 15 | filename: the name of an asap table on disk
|
---|
| 16 | or
|
---|
| 17 | the name of a rpfits/sdfits/ms file
|
---|
| 18 | (integrations within scans are auto averaged
|
---|
| 19 | and the whole file is read)
|
---|
| 20 | or
|
---|
| 21 | [advanced] a reference to an existing
|
---|
[102] | 22 | scantable
|
---|
[340] | 23 | unit: brightness unit; must be consistent with K or Jy.
|
---|
| 24 | Over-rides the default selected by the reader
|
---|
| 25 | (input rpfits/sdfits/ms) or replaces the value
|
---|
| 26 | in existing scantables
|
---|
[102] | 27 | """
|
---|
[226] | 28 | self._vb = rcParams['verbose']
|
---|
[113] | 29 | self._p = None
|
---|
[181] | 30 | from os import stat as st
|
---|
| 31 | import stat
|
---|
| 32 | if isinstance(filename,sdtable):
|
---|
| 33 | sdtable.__init__(self, filename)
|
---|
[340] | 34 | if unit is not None:
|
---|
[346] | 35 | self.set_fluxunit(unit)
|
---|
[181] | 36 | else:
|
---|
[411] | 37 | import os.path
|
---|
| 38 | if not os.path.exists(filename):
|
---|
| 39 | print "File '%s' not found." % (filename)
|
---|
[226] | 40 | return
|
---|
[411] | 41 | filename = os.path.expandvars(filename)
|
---|
| 42 | if os.path.isdir(filename):
|
---|
[181] | 43 | # crude check if asap table
|
---|
[411] | 44 | if os.path.exists(filename+'/table.info'):
|
---|
[181] | 45 | sdtable.__init__(self, filename)
|
---|
[340] | 46 | if unit is not None:
|
---|
| 47 | self.set_fluxunit(unit)
|
---|
[181] | 48 | else:
|
---|
[411] | 49 | print "The given file '%s'is not a valid asap table." % (filename)
|
---|
[226] | 50 | return
|
---|
| 51 | else:
|
---|
| 52 | autoav = rcParams['scantable.autoaverage']
|
---|
| 53 |
|
---|
[181] | 54 | from asap._asap import sdreader
|
---|
[340] | 55 | ifSel = -1
|
---|
| 56 | beamSel = -1
|
---|
[345] | 57 | r = sdreader(filename,ifSel,beamSel)
|
---|
[181] | 58 | print 'Importing data...'
|
---|
[411] | 59 | r._read([-1])
|
---|
| 60 | tbl = r._getdata()
|
---|
[346] | 61 | if unit is not None:
|
---|
| 62 | tbl.set_fluxunit(unit)
|
---|
[226] | 63 | if autoav:
|
---|
| 64 | from asap._asap import average
|
---|
| 65 | tmp = tuple([tbl])
|
---|
| 66 | print 'Auto averaging integrations...'
|
---|
| 67 | tbl2 = average(tmp,(),True,'none')
|
---|
| 68 | sdtable.__init__(self,tbl2)
|
---|
| 69 | del r,tbl
|
---|
| 70 | else:
|
---|
| 71 | sdtable.__init__(self,tbl)
|
---|
[102] | 72 |
|
---|
[445] | 73 | def save(self, name=None, format=None, stokes=True, overwrite=False):
|
---|
[116] | 74 | """
|
---|
[280] | 75 | Store the scantable on disk. This can be an asap (aips++) Table, SDFITS,
|
---|
| 76 | Image FITS or MS2 format.
|
---|
[116] | 77 | Parameters:
|
---|
[196] | 78 | name: the name of the outputfile. For format="FITS" this
|
---|
[280] | 79 | is the directory file name into which all the files will
|
---|
| 80 | be written (default is 'asap_FITS')
|
---|
[116] | 81 | format: an optional file format. Default is ASAP.
|
---|
[280] | 82 | Allowed are - 'ASAP' (save as ASAP [aips++] Table),
|
---|
[194] | 83 | 'SDFITS' (save as SDFITS file)
|
---|
| 84 | 'FITS' (saves each row as a FITS Image)
|
---|
[200] | 85 | 'ASCII' (saves as ascii text file)
|
---|
[226] | 86 | 'MS2' (saves as an aips++
|
---|
| 87 | MeasurementSet V2)
|
---|
[445] | 88 | stokes: Convert to Stokes parameters (only available
|
---|
| 89 | currently with FITS and ASCII formats.
|
---|
[411] | 90 | overwrite: If the file should be overwritten if it exists.
|
---|
[256] | 91 | The default False is to return with warning
|
---|
[411] | 92 | without writing the output. USE WITH CARE.
|
---|
[116] | 93 | Example:
|
---|
| 94 | scan.save('myscan.asap')
|
---|
| 95 | scan.save('myscan.sdfits','SDFITS')
|
---|
| 96 | """
|
---|
[411] | 97 | from os import path
|
---|
[226] | 98 | if format is None: format = rcParams['scantable.save']
|
---|
[256] | 99 | suffix = '.'+format.lower()
|
---|
| 100 | if name is None or name =="":
|
---|
| 101 | name = 'scantable'+suffix
|
---|
[283] | 102 | print "No filename given. Using default name %s..." % name
|
---|
[411] | 103 | name = path.expandvars(name)
|
---|
[256] | 104 | if path.isfile(name) or path.isdir(name):
|
---|
| 105 | if not overwrite:
|
---|
| 106 | print "File %s already exists." % name
|
---|
| 107 | return
|
---|
[116] | 108 | if format == 'ASAP':
|
---|
| 109 | self._save(name)
|
---|
| 110 | else:
|
---|
| 111 | from asap._asap import sdwriter as _sw
|
---|
[194] | 112 | w = _sw(format)
|
---|
[445] | 113 | w.write(self, name, stokes)
|
---|
[116] | 114 | return
|
---|
| 115 |
|
---|
[102] | 116 | def copy(self):
|
---|
| 117 | """
|
---|
| 118 | Return a copy of this scantable.
|
---|
| 119 | Parameters:
|
---|
[113] | 120 | none
|
---|
[102] | 121 | Example:
|
---|
| 122 | copiedscan = scan.copy()
|
---|
| 123 | """
|
---|
[113] | 124 | sd = scantable(sdtable._copy(self))
|
---|
| 125 | return sd
|
---|
| 126 |
|
---|
[102] | 127 | def get_scan(self, scanid=None):
|
---|
| 128 | """
|
---|
| 129 | Return a specific scan (by scanno) or collection of scans (by
|
---|
| 130 | source name) in a new scantable.
|
---|
| 131 | Parameters:
|
---|
| 132 | scanid: a scanno or a source name
|
---|
| 133 | Example:
|
---|
[113] | 134 | scan.get_scan('323p459')
|
---|
[102] | 135 | # gets all scans containing the source '323p459'
|
---|
| 136 | """
|
---|
| 137 | if scanid is None:
|
---|
| 138 | print "Please specify a scan no or name to retrieve from the scantable"
|
---|
| 139 | try:
|
---|
| 140 | if type(scanid) is str:
|
---|
[113] | 141 | s = sdtable._getsource(self,scanid)
|
---|
[102] | 142 | return scantable(s)
|
---|
| 143 | elif type(scanid) is int:
|
---|
[381] | 144 | s = sdtable._getscan(self,[scanid])
|
---|
| 145 | return scantable(s)
|
---|
| 146 | elif type(scanid) is list:
|
---|
[113] | 147 | s = sdtable._getscan(self,scanid)
|
---|
[102] | 148 | return scantable(s)
|
---|
[381] | 149 | else:
|
---|
| 150 | print "Illegal scanid type, use 'int' or 'list' if ints."
|
---|
[102] | 151 | except RuntimeError:
|
---|
| 152 | print "Couldn't find any match."
|
---|
| 153 |
|
---|
| 154 | def __str__(self):
|
---|
[381] | 155 | return sdtable._summary(self,True)
|
---|
[102] | 156 |
|
---|
[381] | 157 | def summary(self,filename=None, verbose=None):
|
---|
[102] | 158 | """
|
---|
| 159 | Print a summary of the contents of this scantable.
|
---|
| 160 | Parameters:
|
---|
| 161 | filename: the name of a file to write the putput to
|
---|
| 162 | Default - no file output
|
---|
[381] | 163 | verbose: print extra info such as the frequency table
|
---|
| 164 | The default (False) is taken from .asaprc
|
---|
[102] | 165 | """
|
---|
[381] | 166 | info = sdtable._summary(self, verbose)
|
---|
| 167 | if verbose is None: verbose = rcParams['scantable.verbosesummary']
|
---|
[102] | 168 | if filename is not None:
|
---|
[256] | 169 | if filename is "":
|
---|
| 170 | filename = 'scantable_summary.txt'
|
---|
[415] | 171 | from os.path import expandvars, isdir
|
---|
[411] | 172 | filename = expandvars(filename)
|
---|
[415] | 173 | if not isdir(filename):
|
---|
[413] | 174 | data = open(filename, 'w')
|
---|
| 175 | data.write(info)
|
---|
| 176 | data.close()
|
---|
| 177 | else:
|
---|
| 178 | print "Illegal file name '%s'." % (filename)
|
---|
[102] | 179 | print info
|
---|
| 180 |
|
---|
[256] | 181 | def set_cursor(self, thebeam=0,theif=0,thepol=0):
|
---|
[102] | 182 | """
|
---|
| 183 | Set the spectrum for individual operations.
|
---|
| 184 | Parameters:
|
---|
| 185 | thebeam,theif,thepol: a number
|
---|
| 186 | Example:
|
---|
[256] | 187 | scan.set_cursor(0,0,1)
|
---|
[135] | 188 | pol1sig = scan.stats(all=False) # returns std dev for beam=0
|
---|
[181] | 189 | # if=0, pol=1
|
---|
[102] | 190 | """
|
---|
| 191 | self.setbeam(thebeam)
|
---|
| 192 | self.setpol(thepol)
|
---|
| 193 | self.setif(theif)
|
---|
| 194 | return
|
---|
| 195 |
|
---|
[256] | 196 | def get_cursor(self):
|
---|
[113] | 197 | """
|
---|
| 198 | Return/print a the current 'cursor' into the Beam/IF/Pol cube.
|
---|
| 199 | Parameters:
|
---|
| 200 | none
|
---|
| 201 | Returns:
|
---|
| 202 | a list of values (currentBeam,currentIF,currentPol)
|
---|
| 203 | Example:
|
---|
| 204 | none
|
---|
| 205 | """
|
---|
[102] | 206 | i = self.getbeam()
|
---|
| 207 | j = self.getif()
|
---|
| 208 | k = self.getpol()
|
---|
[113] | 209 | if self._vb:
|
---|
[181] | 210 | print "--------------------------------------------------"
|
---|
[256] | 211 | print " Cursor position"
|
---|
[181] | 212 | print "--------------------------------------------------"
|
---|
[226] | 213 | out = 'Beam=%d IF=%d Pol=%d ' % (i,j,k)
|
---|
[113] | 214 | print out
|
---|
[102] | 215 | return i,j,k
|
---|
| 216 |
|
---|
[436] | 217 | def stats(self, stat='stddev', mask=None, allaxes=None):
|
---|
[102] | 218 | """
|
---|
[135] | 219 | Determine the specified statistic of the current beam/if/pol
|
---|
[102] | 220 | Takes a 'mask' as an optional parameter to specify which
|
---|
| 221 | channels should be excluded.
|
---|
| 222 | Parameters:
|
---|
[135] | 223 | stat: 'min', 'max', 'sumsq', 'sum', 'mean'
|
---|
| 224 | 'var', 'stddev', 'avdev', 'rms', 'median'
|
---|
| 225 | mask: an optional mask specifying where the statistic
|
---|
[102] | 226 | should be determined.
|
---|
[436] | 227 | allaxes: if True apply to all spectra. Otherwise
|
---|
| 228 | apply only to the selected (beam/pol/if)spectra only.
|
---|
| 229 | The default is taken from .asaprc (True if none)
|
---|
[102] | 230 | Example:
|
---|
[113] | 231 | scan.set_unit('channel')
|
---|
[102] | 232 | msk = scan.create_mask([100,200],[500,600])
|
---|
[135] | 233 | scan.stats(stat='mean', mask=m)
|
---|
[102] | 234 | """
|
---|
[436] | 235 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
[135] | 236 | from asap._asap import stats as _stats
|
---|
[256] | 237 | from numarray import array,zeros,Float
|
---|
[102] | 238 | if mask == None:
|
---|
| 239 | mask = ones(self.nchan())
|
---|
[256] | 240 | axes = ['Beam','IF','Pol','Time']
|
---|
| 241 |
|
---|
[436] | 242 | beamSel,IFSel,polSel = (self.getbeam(),self.getif(),self.getpol())
|
---|
| 243 | if allaxes:
|
---|
[256] | 244 | n = self.nbeam()*self.nif()*self.npol()*self.nrow()
|
---|
| 245 | shp = [self.nbeam(),self.nif(),self.npol(),self.nrow()]
|
---|
| 246 | arr = array(zeros(n),shape=shp,type=Float)
|
---|
| 247 |
|
---|
| 248 | for i in range(self.nbeam()):
|
---|
| 249 | self.setbeam(i)
|
---|
| 250 | for j in range(self.nif()):
|
---|
| 251 | self.setif(j)
|
---|
| 252 | for k in range(self.npol()):
|
---|
| 253 | self.setpol(k)
|
---|
| 254 | arr[i,j,k,:] = _stats(self,mask,stat,-1)
|
---|
| 255 | retval = {'axes': axes, 'data': arr, 'cursor':None}
|
---|
| 256 | tm = [self._gettime(val) for val in range(self.nrow())]
|
---|
[181] | 257 | if self._vb:
|
---|
[256] | 258 | self._print_values(retval,stat,tm)
|
---|
[436] | 259 | self.setbeam(beamSel)
|
---|
| 260 | self.setif(IFSel)
|
---|
| 261 | self.setpol(polSel)
|
---|
[256] | 262 | return retval
|
---|
[102] | 263 |
|
---|
| 264 | else:
|
---|
[241] | 265 | statval = _stats(self,mask,stat,-1)
|
---|
[181] | 266 | out = ''
|
---|
| 267 | for l in range(self.nrow()):
|
---|
| 268 | tm = self._gettime(l)
|
---|
| 269 | out += 'Time[%s]:\n' % (tm)
|
---|
[436] | 270 | if self.nbeam() > 1: out += ' Beam[%d] ' % (beamSel)
|
---|
| 271 | if self.nif() > 1: out += ' IF[%d] ' % (IFSel)
|
---|
| 272 | if self.npol() > 1: out += ' Pol[%d] ' % (polSel)
|
---|
[181] | 273 | out += '= %3.3f\n' % (statval[l])
|
---|
| 274 | out += "--------------------------------------------------\n"
|
---|
[226] | 275 |
|
---|
[181] | 276 | if self._vb:
|
---|
| 277 | print "--------------------------------------------------"
|
---|
| 278 | print " ",stat
|
---|
| 279 | print "--------------------------------------------------"
|
---|
| 280 | print out
|
---|
[256] | 281 | retval = {'axes': axes, 'data': array(statval), 'cursor':(i,j,k)}
|
---|
| 282 | return retval
|
---|
[102] | 283 |
|
---|
[436] | 284 | def stddev(self,mask=None, allaxes=None):
|
---|
[135] | 285 | """
|
---|
| 286 | Determine the standard deviation of the current beam/if/pol
|
---|
| 287 | Takes a 'mask' as an optional parameter to specify which
|
---|
| 288 | channels should be excluded.
|
---|
| 289 | Parameters:
|
---|
| 290 | mask: an optional mask specifying where the standard
|
---|
| 291 | deviation should be determined.
|
---|
[436] | 292 | allaxes: optional flag to show all or a cursor selected
|
---|
[226] | 293 | spectrum of Beam/IF/Pol. Default is all or taken
|
---|
| 294 | from .asaprc
|
---|
[135] | 295 |
|
---|
| 296 | Example:
|
---|
| 297 | scan.set_unit('channel')
|
---|
| 298 | msk = scan.create_mask([100,200],[500,600])
|
---|
| 299 | scan.stddev(mask=m)
|
---|
| 300 | """
|
---|
[436] | 301 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
| 302 | return self.stats(stat='stddev',mask=mask, allaxes=allaxes);
|
---|
[135] | 303 |
|
---|
[436] | 304 | def get_tsys(self, allaxes=None):
|
---|
[113] | 305 | """
|
---|
| 306 | Return the System temperatures.
|
---|
| 307 | Parameters:
|
---|
[436] | 308 | allaxes: if True apply to all spectra. Otherwise
|
---|
| 309 | apply only to the selected (beam/pol/if)spectra only.
|
---|
| 310 | The default is taken from .asaprc (True if none)
|
---|
[113] | 311 | Returns:
|
---|
| 312 | a list of Tsys values.
|
---|
| 313 | """
|
---|
[436] | 314 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
[256] | 315 | from numarray import array,zeros,Float
|
---|
| 316 | axes = ['Beam','IF','Pol','Time']
|
---|
| 317 |
|
---|
[436] | 318 | if allaxes:
|
---|
[256] | 319 | n = self.nbeam()*self.nif()*self.npol()*self.nrow()
|
---|
| 320 | shp = [self.nbeam(),self.nif(),self.npol(),self.nrow()]
|
---|
| 321 | arr = array(zeros(n),shape=shp,type=Float)
|
---|
| 322 |
|
---|
| 323 | for i in range(self.nbeam()):
|
---|
| 324 | self.setbeam(i)
|
---|
| 325 | for j in range(self.nif()):
|
---|
| 326 | self.setif(j)
|
---|
| 327 | for k in range(self.npol()):
|
---|
| 328 | self.setpol(k)
|
---|
| 329 | arr[i,j,k,:] = self._gettsys()
|
---|
| 330 | retval = {'axes': axes, 'data': arr, 'cursor':None}
|
---|
| 331 | tm = [self._gettime(val) for val in range(self.nrow())]
|
---|
[181] | 332 | if self._vb:
|
---|
[256] | 333 | self._print_values(retval,'Tsys',tm)
|
---|
| 334 | return retval
|
---|
| 335 |
|
---|
[102] | 336 | else:
|
---|
[256] | 337 | i,j,k = (self.getbeam(),self.getif(),self.getpol())
|
---|
| 338 | statval = self._gettsys()
|
---|
[181] | 339 | out = ''
|
---|
| 340 | for l in range(self.nrow()):
|
---|
| 341 | tm = self._gettime(l)
|
---|
| 342 | out += 'Time[%s]:\n' % (tm)
|
---|
| 343 | if self.nbeam() > 1: out += ' Beam[%d] ' % (i)
|
---|
| 344 | if self.nif() > 1: out += ' IF[%d] ' % (j)
|
---|
| 345 | if self.npol() > 1: out += ' Pol[%d] ' % (k)
|
---|
[256] | 346 | out += '= %3.3f\n' % (statval[l])
|
---|
| 347 | out += "--------------------------------------------------\n"
|
---|
| 348 |
|
---|
[181] | 349 | if self._vb:
|
---|
| 350 | print "--------------------------------------------------"
|
---|
[256] | 351 | print " TSys"
|
---|
[181] | 352 | print "--------------------------------------------------"
|
---|
| 353 | print out
|
---|
[256] | 354 | retval = {'axes': axes, 'data': array(statval), 'cursor':(i,j,k)}
|
---|
| 355 | return retval
|
---|
[113] | 356 |
|
---|
[407] | 357 | def get_time(self, row=-1):
|
---|
[113] | 358 | """
|
---|
| 359 | Get a list of time stamps for the observations.
|
---|
[181] | 360 | Return a string for each integration in the scantable.
|
---|
[113] | 361 | Parameters:
|
---|
[407] | 362 | row: row no of integration. Default -1 return all rows
|
---|
[113] | 363 | Example:
|
---|
| 364 | none
|
---|
| 365 | """
|
---|
| 366 | out = []
|
---|
[407] | 367 | if row == -1:
|
---|
| 368 | for i in range(self.nrow()):
|
---|
| 369 | out.append(self._gettime(i))
|
---|
| 370 | return out
|
---|
| 371 | else:
|
---|
| 372 | if row < self.nrow():
|
---|
| 373 | return self._gettime(row)
|
---|
[102] | 374 |
|
---|
| 375 | def set_unit(self, unit='channel'):
|
---|
| 376 | """
|
---|
| 377 | Set the unit for all following operations on this scantable
|
---|
| 378 | Parameters:
|
---|
| 379 | unit: optional unit, default is 'channel'
|
---|
[113] | 380 | one of '*Hz','km/s','channel', ''
|
---|
[102] | 381 | """
|
---|
[113] | 382 |
|
---|
| 383 | if unit in ['','pixel', 'channel']:
|
---|
| 384 | unit = ''
|
---|
| 385 | inf = list(self._getcoordinfo())
|
---|
| 386 | inf[0] = unit
|
---|
| 387 | self._setcoordinfo(inf)
|
---|
| 388 | if self._p: self.plot()
|
---|
| 389 |
|
---|
[358] | 390 | def set_instrument (self, instr):
|
---|
| 391 | """
|
---|
| 392 | Set the instrument for subsequent processing
|
---|
| 393 | Parameters:
|
---|
[407] | 394 | instr: Select from 'ATPKSMB', 'ATPKSHOH', 'ATMOPRA',
|
---|
| 395 | 'DSS-43' (Tid), 'CEDUNA', and 'HOBART'
|
---|
[358] | 396 | """
|
---|
| 397 | self._setInstrument(instr)
|
---|
| 398 |
|
---|
[276] | 399 | def set_doppler(self, doppler='RADIO'):
|
---|
| 400 | """
|
---|
| 401 | Set the doppler for all following operations on this scantable.
|
---|
| 402 | Parameters:
|
---|
| 403 | doppler: One of 'RADIO', 'OPTICAL', 'Z', 'BETA', 'GAMMA'
|
---|
| 404 | """
|
---|
| 405 |
|
---|
| 406 | inf = list(self._getcoordinfo())
|
---|
| 407 | inf[2] = doppler
|
---|
| 408 | self._setcoordinfo(inf)
|
---|
| 409 | if self._p: self.plot()
|
---|
[432] | 410 |
|
---|
[226] | 411 | def set_freqframe(self, frame=None):
|
---|
[113] | 412 | """
|
---|
| 413 | Set the frame type of the Spectral Axis.
|
---|
| 414 | Parameters:
|
---|
| 415 | frame: an optional frame type, default 'LSRK'.
|
---|
| 416 | Examples:
|
---|
| 417 | scan.set_freqframe('BARY')
|
---|
| 418 | """
|
---|
[226] | 419 | if not frame: frame = rcParams['scantable.freqframe']
|
---|
[113] | 420 | valid = ['REST','TOPO','LSRD','LSRK','BARY', \
|
---|
| 421 | 'GEO','GALACTO','LGROUP','CMB']
|
---|
| 422 | if frame in valid:
|
---|
| 423 | inf = list(self._getcoordinfo())
|
---|
| 424 | inf[1] = frame
|
---|
| 425 | self._setcoordinfo(inf)
|
---|
[102] | 426 | else:
|
---|
[113] | 427 | print "Please specify a valid freq type. Valid types are:\n",valid
|
---|
| 428 |
|
---|
| 429 | def get_unit(self):
|
---|
| 430 | """
|
---|
| 431 | Get the default unit set in this scantable
|
---|
| 432 | Parameters:
|
---|
| 433 | Returns:
|
---|
| 434 | A unit string
|
---|
| 435 | """
|
---|
| 436 | inf = self._getcoordinfo()
|
---|
| 437 | unit = inf[0]
|
---|
| 438 | if unit == '': unit = 'channel'
|
---|
| 439 | return unit
|
---|
[102] | 440 |
|
---|
[158] | 441 | def get_abcissa(self, rowno=0):
|
---|
[102] | 442 | """
|
---|
[158] | 443 | Get the abcissa in the current coordinate setup for the currently
|
---|
[113] | 444 | selected Beam/IF/Pol
|
---|
| 445 | Parameters:
|
---|
[226] | 446 | rowno: an optional row number in the scantable. Default is the
|
---|
| 447 | first row, i.e. rowno=0
|
---|
[113] | 448 | Returns:
|
---|
[407] | 449 | The abcissa values and it's format string (as a dictionary)
|
---|
[113] | 450 | """
|
---|
[256] | 451 | abc = self._getabcissa(rowno)
|
---|
[407] | 452 | lbl = self._getabcissalabel(rowno)
|
---|
[158] | 453 | return abc, lbl
|
---|
[407] | 454 | #return {'abcissa':abc,'label':lbl}
|
---|
[113] | 455 |
|
---|
| 456 | def create_mask(self, *args, **kwargs):
|
---|
| 457 | """
|
---|
[102] | 458 | Compute and return a mask based on [min,max] windows.
|
---|
[189] | 459 | The specified windows are to be INCLUDED, when the mask is
|
---|
[113] | 460 | applied.
|
---|
[102] | 461 | Parameters:
|
---|
| 462 | [min,max],[min2,max2],...
|
---|
| 463 | Pairs of start/end points specifying the regions
|
---|
| 464 | to be masked
|
---|
[189] | 465 | invert: optional argument. If specified as True,
|
---|
| 466 | return an inverted mask, i.e. the regions
|
---|
| 467 | specified are EXCLUDED
|
---|
[102] | 468 | Example:
|
---|
[113] | 469 | scan.set_unit('channel')
|
---|
| 470 |
|
---|
| 471 | a)
|
---|
[102] | 472 | msk = scan.set_mask([400,500],[800,900])
|
---|
[189] | 473 | # masks everything outside 400 and 500
|
---|
[113] | 474 | # and 800 and 900 in the unit 'channel'
|
---|
| 475 |
|
---|
| 476 | b)
|
---|
| 477 | msk = scan.set_mask([400,500],[800,900], invert=True)
|
---|
[189] | 478 | # masks the regions between 400 and 500
|
---|
[113] | 479 | # and 800 and 900 in the unit 'channel'
|
---|
| 480 |
|
---|
[102] | 481 | """
|
---|
[113] | 482 | u = self._getcoordinfo()[0]
|
---|
| 483 | if self._vb:
|
---|
| 484 | if u == "": u = "channel"
|
---|
| 485 | print "The current mask window unit is", u
|
---|
[102] | 486 | n = self.nchan()
|
---|
[256] | 487 | data = self._getabcissa()
|
---|
[189] | 488 | msk = zeros(n)
|
---|
[102] | 489 | for window in args:
|
---|
| 490 | if (len(window) != 2 or window[0] > window[1] ):
|
---|
| 491 | print "A window needs to be defined as [min,max]"
|
---|
| 492 | return
|
---|
| 493 | for i in range(n):
|
---|
[113] | 494 | if data[i] >= window[0] and data[i] < window[1]:
|
---|
[189] | 495 | msk[i] = 1
|
---|
[113] | 496 | if kwargs.has_key('invert'):
|
---|
| 497 | if kwargs.get('invert'):
|
---|
| 498 | from numarray import logical_not
|
---|
| 499 | msk = logical_not(msk)
|
---|
[102] | 500 | return msk
|
---|
[113] | 501 |
|
---|
[256] | 502 | def get_restfreqs(self):
|
---|
| 503 | """
|
---|
| 504 | Get the restfrequency(s) stored in this scantable.
|
---|
| 505 | The return value(s) are always of unit 'Hz'
|
---|
| 506 | Parameters:
|
---|
| 507 | none
|
---|
| 508 | Returns:
|
---|
| 509 | a list of doubles
|
---|
| 510 | """
|
---|
| 511 | return list(self._getrestfreqs())
|
---|
[102] | 512 |
|
---|
[402] | 513 | def lines(self):
|
---|
[391] | 514 | """
|
---|
[402] | 515 | Print the list of known spectral lines
|
---|
| 516 | """
|
---|
| 517 | sdtable._lines(self)
|
---|
| 518 |
|
---|
| 519 | def set_restfreqs(self, freqs=None, unit='Hz', lines=None, source=None, theif=None):
|
---|
| 520 | """
|
---|
[393] | 521 | Select the restfrequency for the specified source and IF OR
|
---|
| 522 | replace for all IFs. If the 'freqs' argument holds a scalar,
|
---|
| 523 | then that rest frequency will be applied to the selected
|
---|
| 524 | data (and added to the list of available rest frequencies).
|
---|
| 525 | In this way, you can set a rest frequency for each
|
---|
| 526 | source and IF combination. If the 'freqs' argument holds
|
---|
| 527 | a vector, then it MUST be of length the number of IFs
|
---|
| 528 | (and the available restfrequencies will be replaced by
|
---|
| 529 | this vector). In this case, *all* data ('source' and
|
---|
| 530 | 'theif' are ignored) have the restfrequency set per IF according
|
---|
| 531 | to the corresponding value you give in the 'freqs' vector.
|
---|
| 532 | E.g. 'freqs=[1e9,2e9]' would mean IF 0 gets restfreq 1e9 and
|
---|
| 533 | IF 1 gets restfreq 2e9.
|
---|
[402] | 534 |
|
---|
| 535 | You can also specify the frequencies via known line names
|
---|
| 536 | in the argument 'lines'. Use 'freqs' or 'lines'. 'freqs'
|
---|
| 537 | takes precedence. See the list of known names via function
|
---|
| 538 | scantable.lines()
|
---|
[391] | 539 | Parameters:
|
---|
[402] | 540 | freqs: list of rest frequencies
|
---|
[393] | 541 | unit: unit for rest frequency (default 'Hz')
|
---|
[402] | 542 | lines: list of known spectral lines names (alternative to freqs).
|
---|
| 543 | See possible list via scantable.lines()
|
---|
[391] | 544 | source: Source name (blank means all)
|
---|
| 545 | theif: IF (-1 means all)
|
---|
| 546 | Example:
|
---|
[393] | 547 | scan.set_restfreqs(freqs=1.4e9, source='NGC253', theif=2)
|
---|
| 548 | scan.set_restfreqs(freqs=[1.4e9,1.67e9])
|
---|
[391] | 549 | """
|
---|
| 550 | if source is None:
|
---|
| 551 | source = ""
|
---|
| 552 | if theif is None:
|
---|
| 553 | theif = -1
|
---|
[393] | 554 | t = type(freqs)
|
---|
| 555 | if t is int or t is float:
|
---|
| 556 | freqs = [freqs]
|
---|
[402] | 557 | if freqs is None:
|
---|
| 558 | freqs = []
|
---|
| 559 | t = type(lines)
|
---|
| 560 | if t is str:
|
---|
| 561 | lines = [lines]
|
---|
| 562 | if lines is None:
|
---|
| 563 | lines = []
|
---|
| 564 | sdtable._setrestfreqs(self, freqs, unit, lines, source, theif)
|
---|
[391] | 565 | return
|
---|
| 566 |
|
---|
| 567 |
|
---|
[102] | 568 | def flag_spectrum(self, thebeam, theif, thepol):
|
---|
| 569 | """
|
---|
| 570 | This flags a selected spectrum in the scan 'for good'.
|
---|
| 571 | USE WITH CARE - not reversible.
|
---|
| 572 | Use masks for non-permanent exclusion of channels.
|
---|
| 573 | Parameters:
|
---|
| 574 | thebeam,theif,thepol: all have to be explicitly
|
---|
| 575 | specified
|
---|
| 576 | Example:
|
---|
| 577 | scan.flag_spectrum(0,0,1)
|
---|
| 578 | flags the spectrum for Beam=0, IF=0, Pol=1
|
---|
| 579 | """
|
---|
[407] | 580 | if (thebeam < self.nbeam() and
|
---|
| 581 | theif < self.nif() and
|
---|
| 582 | thepol < self.npol()):
|
---|
| 583 | sdtable.setbeam(self, thebeam)
|
---|
| 584 | sdtable.setif(self, theif)
|
---|
| 585 | sdtable.setpol(self, thepol)
|
---|
| 586 | sdtable._flag(self)
|
---|
[102] | 587 | else:
|
---|
| 588 | print "Please specify a valid (Beam/IF/Pol)"
|
---|
| 589 | return
|
---|
[113] | 590 |
|
---|
[436] | 591 | def rotate_xyphase (self, angle, allaxes=None):
|
---|
[432] | 592 | """
|
---|
| 593 | Rotate the phase of the XY correlation. This is done in situ
|
---|
| 594 | in the data. So if you call this function more than once
|
---|
| 595 | then each call rotates the phase further.
|
---|
| 596 | Parameters:
|
---|
| 597 | angle: The angle (degrees) to rotate (add) by.
|
---|
[436] | 598 | allaxes: If True apply to all spectra. Otherwise
|
---|
| 599 | apply only to the selected (beam/pol/if)spectra only.
|
---|
| 600 | The default is taken from .asaprc (True if none)
|
---|
[432] | 601 | Examples:
|
---|
| 602 | scan.rotate_xyphase(2.3)
|
---|
| 603 | """
|
---|
[436] | 604 | if allaxes is None: allaxes = rcParams['scantable.allaxes']
|
---|
| 605 | sdtable._rotate_xyphase(self, angle, allaxes)
|
---|
[432] | 606 |
|
---|
[113] | 607 | def plot(self, what='spectrum',col='Pol', panel=None):
|
---|
| 608 | """
|
---|
| 609 | Plot the spectra contained in the scan. Alternatively you can also
|
---|
| 610 | Plot Tsys vs Time
|
---|
| 611 | Parameters:
|
---|
| 612 | what: a choice of 'spectrum' (default) or 'tsys'
|
---|
[135] | 613 | col: which out of Beams/IFs/Pols should be colour stacked
|
---|
[113] | 614 | panel: set up multiple panels, currently not working.
|
---|
| 615 | """
|
---|
[283] | 616 | print "Warning! Not fully functional. Use plotter.plot() instead"
|
---|
| 617 |
|
---|
[113] | 618 | validcol = {'Beam':self.nbeam(),'IF':self.nif(),'Pol':self.npol()}
|
---|
[124] | 619 |
|
---|
[113] | 620 | validyax = ['spectrum','tsys']
|
---|
[189] | 621 | from asap.asaplot import ASAPlot
|
---|
[113] | 622 | if not self._p:
|
---|
| 623 | self._p = ASAPlot()
|
---|
[189] | 624 | #print "Plotting not enabled"
|
---|
| 625 | #return
|
---|
| 626 | if self._p.is_dead:
|
---|
| 627 | del self._p
|
---|
| 628 | self._p = ASAPlot()
|
---|
[113] | 629 | npan = 1
|
---|
| 630 | x = None
|
---|
| 631 | if what == 'tsys':
|
---|
| 632 | n = self.nrow()
|
---|
| 633 | if n < 2:
|
---|
| 634 | print "Only one integration. Can't plot."
|
---|
| 635 | return
|
---|
[124] | 636 | self._p.hold()
|
---|
| 637 | self._p.clear()
|
---|
[113] | 638 | if panel == 'Time':
|
---|
| 639 | npan = self.nrow()
|
---|
[124] | 640 | self._p.set_panels(rows=npan)
|
---|
[113] | 641 | xlab,ylab,tlab = None,None,None
|
---|
[226] | 642 | self._vb = False
|
---|
[256] | 643 | sel = self.get_cursor()
|
---|
[113] | 644 | for i in range(npan):
|
---|
[226] | 645 | if npan > 1:
|
---|
| 646 | self._p.subplot(i)
|
---|
[113] | 647 | for j in range(validcol[col]):
|
---|
| 648 | x = None
|
---|
| 649 | y = None
|
---|
| 650 | m = None
|
---|
| 651 | tlab = self._getsourcename(i)
|
---|
[124] | 652 | import re
|
---|
| 653 | tlab = re.sub('_S','',tlab)
|
---|
[113] | 654 | if col == 'Beam':
|
---|
| 655 | self.setbeam(j)
|
---|
| 656 | elif col == 'IF':
|
---|
| 657 | self.setif(j)
|
---|
| 658 | elif col == 'Pol':
|
---|
| 659 | self.setpol(j)
|
---|
| 660 | if what == 'tsys':
|
---|
| 661 | x = range(self.nrow())
|
---|
| 662 | xlab = 'Time [pixel]'
|
---|
| 663 | m = list(ones(len(x)))
|
---|
| 664 | y = []
|
---|
| 665 | ylab = r'$T_{sys}$'
|
---|
| 666 | for k in range(len(x)):
|
---|
| 667 | y.append(self._gettsys(k))
|
---|
| 668 | else:
|
---|
[158] | 669 | x,xlab = self.get_abcissa(i)
|
---|
[256] | 670 | y = self._getspectrum(i)
|
---|
[113] | 671 | ylab = r'Flux'
|
---|
[256] | 672 | m = self._getmask(i)
|
---|
[113] | 673 | llab = col+' '+str(j)
|
---|
| 674 | self._p.set_line(label=llab)
|
---|
| 675 | self._p.plot(x,y,m)
|
---|
[124] | 676 | self._p.set_axes('xlabel',xlab)
|
---|
| 677 | self._p.set_axes('ylabel',ylab)
|
---|
| 678 | self._p.set_axes('title',tlab)
|
---|
[113] | 679 | self._p.release()
|
---|
[256] | 680 | self.set_cursor(sel[0],sel[1],sel[2])
|
---|
[226] | 681 | self._vb = rcParams['verbose']
|
---|
[113] | 682 | return
|
---|
[256] | 683 |
|
---|
| 684 | print out
|
---|
| 685 |
|
---|
| 686 | def _print_values(self, dat, label='', timestamps=[]):
|
---|
| 687 | d = dat['data']
|
---|
| 688 | a = dat['axes']
|
---|
| 689 | shp = d.getshape()
|
---|
| 690 | out = ''
|
---|
| 691 | for i in range(shp[3]):
|
---|
| 692 | out += '%s [%s]:\n' % (a[3],timestamps[i])
|
---|
| 693 | t = d[:,:,:,i]
|
---|
| 694 | for j in range(shp[0]):
|
---|
| 695 | if shp[0] > 1: out += ' %s[%d] ' % (a[0],j)
|
---|
| 696 | for k in range(shp[1]):
|
---|
| 697 | if shp[1] > 1: out += ' %s[%d] ' % (a[1],k)
|
---|
| 698 | for l in range(shp[2]):
|
---|
| 699 | if shp[2] > 1: out += ' %s[%d] ' % (a[2],l)
|
---|
| 700 | out += '= %3.3f\n' % (t[j,k,l])
|
---|
| 701 | out += "--------------------------------------------------\n"
|
---|
| 702 | print "--------------------------------------------------"
|
---|
| 703 | print " ", label
|
---|
| 704 | print "--------------------------------------------------"
|
---|
| 705 | print out
|
---|