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