[1846] | 1 | """This module defines the scantable class.""" |
---|
| 2 | |
---|
[1697] | 3 | import os |
---|
[2751] | 4 | import re |
---|
[2315] | 5 | import tempfile |
---|
[1948] | 6 | import numpy |
---|
[1691] | 7 | try: |
---|
| 8 | from functools import wraps as wraps_dec |
---|
| 9 | except ImportError: |
---|
| 10 | from asap.compatibility import wraps as wraps_dec |
---|
| 11 | |
---|
[1824] | 12 | from asap.env import is_casapy |
---|
[876] | 13 | from asap._asap import Scantable |
---|
[2004] | 14 | from asap._asap import filler, msfiller |
---|
[1824] | 15 | from asap.parameters import rcParams |
---|
[1862] | 16 | from asap.logging import asaplog, asaplog_post_dec |
---|
[1824] | 17 | from asap.selector import selector |
---|
| 18 | from asap.linecatalog import linecatalog |
---|
[1600] | 19 | from asap.coordinate import coordinate |
---|
[1859] | 20 | from asap.utils import _n_bools, mask_not, mask_and, mask_or, page |
---|
[1907] | 21 | from asap.asapfitter import fitter |
---|
[102] | 22 | |
---|
[1689] | 23 | def preserve_selection(func): |
---|
[1691] | 24 | @wraps_dec(func) |
---|
[1689] | 25 | def wrap(obj, *args, **kw): |
---|
| 26 | basesel = obj.get_selection() |
---|
[1857] | 27 | try: |
---|
| 28 | val = func(obj, *args, **kw) |
---|
| 29 | finally: |
---|
| 30 | obj.set_selection(basesel) |
---|
[1689] | 31 | return val |
---|
| 32 | return wrap |
---|
| 33 | |
---|
[1846] | 34 | def is_scantable(filename): |
---|
| 35 | """Is the given file a scantable? |
---|
[1689] | 36 | |
---|
[1846] | 37 | Parameters: |
---|
| 38 | |
---|
| 39 | filename: the name of the file/directory to test |
---|
| 40 | |
---|
| 41 | """ |
---|
[1883] | 42 | if ( os.path.isdir(filename) |
---|
| 43 | and os.path.exists(filename+'/table.info') |
---|
| 44 | and os.path.exists(filename+'/table.dat') ): |
---|
| 45 | f=open(filename+'/table.info') |
---|
| 46 | l=f.readline() |
---|
| 47 | f.close() |
---|
[2751] | 48 | match_pattern = '^Type = Scantable *$' |
---|
| 49 | unmatch_pattern = '^Type = (MeasurementSet|Image|ApplyTable) *$' |
---|
| 50 | #if ( l.find('Scantable') != -1 ): |
---|
| 51 | if re.match(match_pattern,l): |
---|
[1883] | 52 | return True |
---|
[2751] | 53 | #elif ( l.find('Measurement Set') == -1 and |
---|
| 54 | # l.find('Image') == -1): |
---|
[2752] | 55 | elif re.match(unmatch_pattern,l): |
---|
[2687] | 56 | return True |
---|
[1883] | 57 | else: |
---|
| 58 | return False |
---|
| 59 | else: |
---|
| 60 | return False |
---|
| 61 | ## return (os.path.isdir(filename) |
---|
| 62 | ## and not os.path.exists(filename+'/table.f1') |
---|
| 63 | ## and os.path.exists(filename+'/table.info')) |
---|
[1697] | 64 | |
---|
[1883] | 65 | def is_ms(filename): |
---|
| 66 | """Is the given file a MeasurementSet? |
---|
[1697] | 67 | |
---|
[1883] | 68 | Parameters: |
---|
| 69 | |
---|
| 70 | filename: the name of the file/directory to test |
---|
| 71 | |
---|
| 72 | """ |
---|
| 73 | if ( os.path.isdir(filename) |
---|
| 74 | and os.path.exists(filename+'/table.info') |
---|
| 75 | and os.path.exists(filename+'/table.dat') ): |
---|
| 76 | f=open(filename+'/table.info') |
---|
| 77 | l=f.readline() |
---|
| 78 | f.close() |
---|
| 79 | if ( l.find('Measurement Set') != -1 ): |
---|
| 80 | return True |
---|
| 81 | else: |
---|
| 82 | return False |
---|
| 83 | else: |
---|
| 84 | return False |
---|
[2186] | 85 | |
---|
| 86 | def normalise_edge_param(edge): |
---|
| 87 | """\ |
---|
| 88 | Convert a given edge value to a one-dimensional array that can be |
---|
| 89 | given to baseline-fitting/subtraction functions. |
---|
| 90 | The length of the output value will be an even because values for |
---|
| 91 | the both sides of spectra are to be contained for each IF. When |
---|
| 92 | the length is 2, the values will be applied to all IFs. If the length |
---|
| 93 | is larger than 2, it will be 2*ifnos(). |
---|
| 94 | Accepted format of edge include: |
---|
| 95 | * an integer - will be used for both sides of spectra of all IFs. |
---|
| 96 | e.g. 10 is converted to [10,10] |
---|
[2277] | 97 | * an empty list/tuple [] - converted to [0, 0] and used for all IFs. |
---|
[2186] | 98 | * a list/tuple containing an integer - same as the above case. |
---|
| 99 | e.g. [10] is converted to [10,10] |
---|
| 100 | * a list/tuple containing two integers - will be used for all IFs. |
---|
| 101 | e.g. [5,10] is output as it is. no need to convert. |
---|
| 102 | * a list/tuple of lists/tuples containing TWO integers - |
---|
| 103 | each element of edge will be used for each IF. |
---|
[2277] | 104 | e.g. [[5,10],[15,20]] - [5,10] for IF[0] and [15,20] for IF[1]. |
---|
| 105 | |
---|
| 106 | If an element contains the same integer values, the input 'edge' |
---|
| 107 | parameter can be given in a simpler shape in the following cases: |
---|
[2186] | 108 | ** when len(edge)!=2 |
---|
[2277] | 109 | any elements containing the same values can be replaced |
---|
| 110 | to single integers. |
---|
| 111 | e.g. [[15,15]] can be simplified to [15] (or [15,15] or 15 also). |
---|
| 112 | e.g. [[1,1],[2,2],[3,3]] can be simplified to [1,2,3]. |
---|
[2186] | 113 | ** when len(edge)=2 |
---|
| 114 | care is needed for this case: ONLY ONE of the |
---|
| 115 | elements can be a single integer, |
---|
| 116 | e.g. [[5,5],[10,10]] can be simplified to [5,[10,10]] |
---|
[2277] | 117 | or [[5,5],10], but can NOT be simplified to [5,10]. |
---|
[2186] | 118 | when [5,10] given, it is interpreted as |
---|
[2277] | 119 | [[5,10],[5,10],[5,10],...] instead, as shown before. |
---|
[2186] | 120 | """ |
---|
| 121 | from asap import _is_sequence_or_number as _is_valid |
---|
| 122 | if isinstance(edge, list) or isinstance(edge, tuple): |
---|
| 123 | for edgepar in edge: |
---|
| 124 | if not _is_valid(edgepar, int): |
---|
| 125 | raise ValueError, "Each element of the 'edge' tuple has \ |
---|
| 126 | to be a pair of integers or an integer." |
---|
| 127 | if isinstance(edgepar, list) or isinstance(edgepar, tuple): |
---|
| 128 | if len(edgepar) != 2: |
---|
| 129 | raise ValueError, "Each element of the 'edge' tuple has \ |
---|
| 130 | to be a pair of integers or an integer." |
---|
| 131 | else: |
---|
| 132 | if not _is_valid(edge, int): |
---|
| 133 | raise ValueError, "Parameter 'edge' has to be an integer or a \ |
---|
| 134 | pair of integers specified as a tuple. \ |
---|
| 135 | Nested tuples are allowed \ |
---|
| 136 | to make individual selection for different IFs." |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | if isinstance(edge, int): |
---|
| 140 | edge = [ edge, edge ] # e.g. 3 => [3,3] |
---|
| 141 | elif isinstance(edge, list) or isinstance(edge, tuple): |
---|
| 142 | if len(edge) == 0: |
---|
| 143 | edge = [0, 0] # e.g. [] => [0,0] |
---|
| 144 | elif len(edge) == 1: |
---|
| 145 | if isinstance(edge[0], int): |
---|
| 146 | edge = [ edge[0], edge[0] ] # e.g. [1] => [1,1] |
---|
| 147 | |
---|
| 148 | commonedge = True |
---|
| 149 | if len(edge) > 2: commonedge = False |
---|
| 150 | else: |
---|
| 151 | for edgepar in edge: |
---|
| 152 | if isinstance(edgepar, list) or isinstance(edgepar, tuple): |
---|
| 153 | commonedge = False |
---|
| 154 | break |
---|
| 155 | |
---|
| 156 | if commonedge: |
---|
| 157 | if len(edge) > 1: |
---|
| 158 | norm_edge = edge |
---|
| 159 | else: |
---|
| 160 | norm_edge = edge + edge |
---|
| 161 | else: |
---|
| 162 | norm_edge = [] |
---|
| 163 | for edgepar in edge: |
---|
| 164 | if isinstance(edgepar, int): |
---|
| 165 | norm_edge += [edgepar, edgepar] |
---|
| 166 | else: |
---|
| 167 | norm_edge += edgepar |
---|
| 168 | |
---|
| 169 | return norm_edge |
---|
| 170 | |
---|
| 171 | def raise_fitting_failure_exception(e): |
---|
| 172 | msg = "The fit failed, possibly because it didn't converge." |
---|
| 173 | if rcParams["verbose"]: |
---|
| 174 | asaplog.push(str(e)) |
---|
| 175 | asaplog.push(str(msg)) |
---|
| 176 | else: |
---|
| 177 | raise RuntimeError(str(e)+'\n'+msg) |
---|
| 178 | |
---|
[2189] | 179 | def pack_progress_params(showprogress, minnrow): |
---|
| 180 | return str(showprogress).lower() + ',' + str(minnrow) |
---|
| 181 | |
---|
[876] | 182 | class scantable(Scantable): |
---|
[1846] | 183 | """\ |
---|
| 184 | The ASAP container for scans (single-dish data). |
---|
[102] | 185 | """ |
---|
[1819] | 186 | |
---|
[1862] | 187 | @asaplog_post_dec |
---|
[2315] | 188 | def __init__(self, filename, average=None, unit=None, parallactify=None, |
---|
| 189 | **args): |
---|
[1846] | 190 | """\ |
---|
[102] | 191 | Create a scantable from a saved one or make a reference |
---|
[1846] | 192 | |
---|
[102] | 193 | Parameters: |
---|
[1846] | 194 | |
---|
| 195 | filename: the name of an asap table on disk |
---|
| 196 | or |
---|
| 197 | the name of a rpfits/sdfits/ms file |
---|
| 198 | (integrations within scans are auto averaged |
---|
| 199 | and the whole file is read) or |
---|
| 200 | [advanced] a reference to an existing scantable |
---|
| 201 | |
---|
| 202 | average: average all integrations withinb a scan on read. |
---|
| 203 | The default (True) is taken from .asaprc. |
---|
| 204 | |
---|
[484] | 205 | unit: brightness unit; must be consistent with K or Jy. |
---|
[1846] | 206 | Over-rides the default selected by the filler |
---|
| 207 | (input rpfits/sdfits/ms) or replaces the value |
---|
| 208 | in existing scantables |
---|
| 209 | |
---|
[1920] | 210 | antenna: for MeasurementSet input data only: |
---|
[2349] | 211 | Antenna selection. integer (id) or string |
---|
| 212 | (name or id). |
---|
[1846] | 213 | |
---|
[2349] | 214 | parallactify: Indicate that the data had been parallactified. |
---|
| 215 | Default (false) is taken from rc file. |
---|
[1846] | 216 | |
---|
[710] | 217 | """ |
---|
[976] | 218 | if average is None: |
---|
[710] | 219 | average = rcParams['scantable.autoaverage'] |
---|
[1593] | 220 | parallactify = parallactify or rcParams['scantable.parallactify'] |
---|
[1259] | 221 | varlist = vars() |
---|
[876] | 222 | from asap._asap import stmath |
---|
[1819] | 223 | self._math = stmath( rcParams['insitu'] ) |
---|
[876] | 224 | if isinstance(filename, Scantable): |
---|
| 225 | Scantable.__init__(self, filename) |
---|
[181] | 226 | else: |
---|
[1697] | 227 | if isinstance(filename, str): |
---|
[976] | 228 | filename = os.path.expandvars(filename) |
---|
| 229 | filename = os.path.expanduser(filename) |
---|
| 230 | if not os.path.exists(filename): |
---|
| 231 | s = "File '%s' not found." % (filename) |
---|
| 232 | raise IOError(s) |
---|
[1697] | 233 | if is_scantable(filename): |
---|
| 234 | ondisk = rcParams['scantable.storage'] == 'disk' |
---|
| 235 | Scantable.__init__(self, filename, ondisk) |
---|
| 236 | if unit is not None: |
---|
| 237 | self.set_fluxunit(unit) |
---|
[2008] | 238 | if average: |
---|
| 239 | self._assign( self.average_time( scanav=True ) ) |
---|
[1819] | 240 | # do not reset to the default freqframe |
---|
| 241 | #self.set_freqframe(rcParams['scantable.freqframe']) |
---|
[1883] | 242 | elif is_ms(filename): |
---|
[1916] | 243 | # Measurement Set |
---|
| 244 | opts={'ms': {}} |
---|
| 245 | mskeys=['getpt','antenna'] |
---|
| 246 | for key in mskeys: |
---|
| 247 | if key in args.keys(): |
---|
| 248 | opts['ms'][key] = args[key] |
---|
| 249 | self._fill([filename], unit, average, opts) |
---|
[1893] | 250 | elif os.path.isfile(filename): |
---|
[1916] | 251 | self._fill([filename], unit, average) |
---|
[2350] | 252 | # only apply to new data not "copy constructor" |
---|
| 253 | self.parallactify(parallactify) |
---|
[1883] | 254 | else: |
---|
[1819] | 255 | msg = "The given file '%s'is not a valid " \ |
---|
| 256 | "asap table." % (filename) |
---|
[1859] | 257 | raise IOError(msg) |
---|
[1118] | 258 | elif (isinstance(filename, list) or isinstance(filename, tuple)) \ |
---|
[976] | 259 | and isinstance(filename[-1], str): |
---|
[1916] | 260 | self._fill(filename, unit, average) |
---|
[1586] | 261 | self.parallactify(parallactify) |
---|
[1259] | 262 | self._add_history("scantable", varlist) |
---|
[102] | 263 | |
---|
[1862] | 264 | @asaplog_post_dec |
---|
[876] | 265 | def save(self, name=None, format=None, overwrite=False): |
---|
[1846] | 266 | """\ |
---|
[1280] | 267 | Store the scantable on disk. This can be an asap (aips++) Table, |
---|
| 268 | SDFITS or MS2 format. |
---|
[1846] | 269 | |
---|
[116] | 270 | Parameters: |
---|
[1846] | 271 | |
---|
[2431] | 272 | name: the name of the outputfile. For format 'ASCII' |
---|
[1093] | 273 | this is the root file name (data in 'name'.txt |
---|
[497] | 274 | and header in 'name'_header.txt) |
---|
[1855] | 275 | |
---|
[116] | 276 | format: an optional file format. Default is ASAP. |
---|
[1855] | 277 | Allowed are: |
---|
| 278 | |
---|
| 279 | * 'ASAP' (save as ASAP [aips++] Table), |
---|
| 280 | * 'SDFITS' (save as SDFITS file) |
---|
| 281 | * 'ASCII' (saves as ascii text file) |
---|
| 282 | * 'MS2' (saves as an casacore MeasurementSet V2) |
---|
[2315] | 283 | * 'FITS' (save as image FITS - not readable by |
---|
| 284 | class) |
---|
[1855] | 285 | * 'CLASS' (save as FITS readable by CLASS) |
---|
| 286 | |
---|
[411] | 287 | overwrite: If the file should be overwritten if it exists. |
---|
[256] | 288 | The default False is to return with warning |
---|
[411] | 289 | without writing the output. USE WITH CARE. |
---|
[1855] | 290 | |
---|
[1846] | 291 | Example:: |
---|
| 292 | |
---|
[116] | 293 | scan.save('myscan.asap') |
---|
[1118] | 294 | scan.save('myscan.sdfits', 'SDFITS') |
---|
[1846] | 295 | |
---|
[116] | 296 | """ |
---|
[411] | 297 | from os import path |
---|
[1593] | 298 | format = format or rcParams['scantable.save'] |
---|
[256] | 299 | suffix = '.'+format.lower() |
---|
[1118] | 300 | if name is None or name == "": |
---|
[256] | 301 | name = 'scantable'+suffix |
---|
[718] | 302 | msg = "No filename given. Using default name %s..." % name |
---|
| 303 | asaplog.push(msg) |
---|
[411] | 304 | name = path.expandvars(name) |
---|
[256] | 305 | if path.isfile(name) or path.isdir(name): |
---|
| 306 | if not overwrite: |
---|
[718] | 307 | msg = "File %s exists." % name |
---|
[1859] | 308 | raise IOError(msg) |
---|
[451] | 309 | format2 = format.upper() |
---|
| 310 | if format2 == 'ASAP': |
---|
[116] | 311 | self._save(name) |
---|
[2029] | 312 | elif format2 == 'MS2': |
---|
| 313 | msopt = {'ms': {'overwrite': overwrite } } |
---|
| 314 | from asap._asap import mswriter |
---|
| 315 | writer = mswriter( self ) |
---|
| 316 | writer.write( name, msopt ) |
---|
[116] | 317 | else: |
---|
[989] | 318 | from asap._asap import stwriter as stw |
---|
[1118] | 319 | writer = stw(format2) |
---|
| 320 | writer.write(self, name) |
---|
[116] | 321 | return |
---|
| 322 | |
---|
[102] | 323 | def copy(self): |
---|
[1846] | 324 | """Return a copy of this scantable. |
---|
| 325 | |
---|
| 326 | *Note*: |
---|
| 327 | |
---|
[1348] | 328 | This makes a full (deep) copy. scan2 = scan1 makes a reference. |
---|
[1846] | 329 | |
---|
| 330 | Example:: |
---|
| 331 | |
---|
[102] | 332 | copiedscan = scan.copy() |
---|
[1846] | 333 | |
---|
[102] | 334 | """ |
---|
[876] | 335 | sd = scantable(Scantable._copy(self)) |
---|
[113] | 336 | return sd |
---|
| 337 | |
---|
[1093] | 338 | def drop_scan(self, scanid=None): |
---|
[1846] | 339 | """\ |
---|
[1093] | 340 | Return a new scantable where the specified scan number(s) has(have) |
---|
| 341 | been dropped. |
---|
[1846] | 342 | |
---|
[1093] | 343 | Parameters: |
---|
[1846] | 344 | |
---|
[1093] | 345 | scanid: a (list of) scan number(s) |
---|
[1846] | 346 | |
---|
[1093] | 347 | """ |
---|
| 348 | from asap import _is_sequence_or_number as _is_valid |
---|
| 349 | from asap import _to_list |
---|
| 350 | from asap import unique |
---|
| 351 | if not _is_valid(scanid): |
---|
[2315] | 352 | raise RuntimeError( 'Please specify a scanno to drop from the' |
---|
| 353 | ' scantable' ) |
---|
[1859] | 354 | scanid = _to_list(scanid) |
---|
| 355 | allscans = unique([ self.getscan(i) for i in range(self.nrow())]) |
---|
| 356 | for sid in scanid: allscans.remove(sid) |
---|
| 357 | if len(allscans) == 0: |
---|
| 358 | raise ValueError("Can't remove all scans") |
---|
| 359 | sel = selector(scans=allscans) |
---|
| 360 | return self._select_copy(sel) |
---|
[1093] | 361 | |
---|
[1594] | 362 | def _select_copy(self, selection): |
---|
| 363 | orig = self.get_selection() |
---|
| 364 | self.set_selection(orig+selection) |
---|
| 365 | cp = self.copy() |
---|
| 366 | self.set_selection(orig) |
---|
| 367 | return cp |
---|
| 368 | |
---|
[102] | 369 | def get_scan(self, scanid=None): |
---|
[1855] | 370 | """\ |
---|
[102] | 371 | Return a specific scan (by scanno) or collection of scans (by |
---|
| 372 | source name) in a new scantable. |
---|
[1846] | 373 | |
---|
| 374 | *Note*: |
---|
| 375 | |
---|
[1348] | 376 | See scantable.drop_scan() for the inverse operation. |
---|
[1846] | 377 | |
---|
[102] | 378 | Parameters: |
---|
[1846] | 379 | |
---|
[513] | 380 | scanid: a (list of) scanno or a source name, unix-style |
---|
| 381 | patterns are accepted for source name matching, e.g. |
---|
| 382 | '*_R' gets all 'ref scans |
---|
[1846] | 383 | |
---|
| 384 | Example:: |
---|
| 385 | |
---|
[513] | 386 | # get all scans containing the source '323p459' |
---|
| 387 | newscan = scan.get_scan('323p459') |
---|
| 388 | # get all 'off' scans |
---|
| 389 | refscans = scan.get_scan('*_R') |
---|
| 390 | # get a susbset of scans by scanno (as listed in scan.summary()) |
---|
[1118] | 391 | newscan = scan.get_scan([0, 2, 7, 10]) |
---|
[1846] | 392 | |
---|
[102] | 393 | """ |
---|
| 394 | if scanid is None: |
---|
[1859] | 395 | raise RuntimeError( 'Please specify a scan no or name to ' |
---|
| 396 | 'retrieve from the scantable' ) |
---|
[102] | 397 | try: |
---|
[946] | 398 | bsel = self.get_selection() |
---|
| 399 | sel = selector() |
---|
[102] | 400 | if type(scanid) is str: |
---|
[946] | 401 | sel.set_name(scanid) |
---|
[1594] | 402 | return self._select_copy(sel) |
---|
[102] | 403 | elif type(scanid) is int: |
---|
[946] | 404 | sel.set_scans([scanid]) |
---|
[1594] | 405 | return self._select_copy(sel) |
---|
[381] | 406 | elif type(scanid) is list: |
---|
[946] | 407 | sel.set_scans(scanid) |
---|
[1594] | 408 | return self._select_copy(sel) |
---|
[381] | 409 | else: |
---|
[718] | 410 | msg = "Illegal scanid type, use 'int' or 'list' if ints." |
---|
[1859] | 411 | raise TypeError(msg) |
---|
[102] | 412 | except RuntimeError: |
---|
[1859] | 413 | raise |
---|
[102] | 414 | |
---|
| 415 | def __str__(self): |
---|
[2315] | 416 | tempFile = tempfile.NamedTemporaryFile() |
---|
| 417 | Scantable._summary(self, tempFile.name) |
---|
| 418 | tempFile.seek(0) |
---|
| 419 | asaplog.clear() |
---|
| 420 | return tempFile.file.read() |
---|
[102] | 421 | |
---|
[2315] | 422 | @asaplog_post_dec |
---|
[976] | 423 | def summary(self, filename=None): |
---|
[1846] | 424 | """\ |
---|
[102] | 425 | Print a summary of the contents of this scantable. |
---|
[1846] | 426 | |
---|
[102] | 427 | Parameters: |
---|
[1846] | 428 | |
---|
[1931] | 429 | filename: the name of a file to write the putput to |
---|
[102] | 430 | Default - no file output |
---|
[1846] | 431 | |
---|
[102] | 432 | """ |
---|
| 433 | if filename is not None: |
---|
[256] | 434 | if filename is "": |
---|
| 435 | filename = 'scantable_summary.txt' |
---|
[415] | 436 | from os.path import expandvars, isdir |
---|
[411] | 437 | filename = expandvars(filename) |
---|
[2286] | 438 | if isdir(filename): |
---|
[718] | 439 | msg = "Illegal file name '%s'." % (filename) |
---|
[1859] | 440 | raise IOError(msg) |
---|
[2286] | 441 | else: |
---|
| 442 | filename = "" |
---|
| 443 | Scantable._summary(self, filename) |
---|
[710] | 444 | |
---|
[1512] | 445 | def get_spectrum(self, rowno): |
---|
[1471] | 446 | """Return the spectrum for the current row in the scantable as a list. |
---|
[1846] | 447 | |
---|
[1471] | 448 | Parameters: |
---|
[1846] | 449 | |
---|
[1573] | 450 | rowno: the row number to retrieve the spectrum from |
---|
[1846] | 451 | |
---|
[1471] | 452 | """ |
---|
| 453 | return self._getspectrum(rowno) |
---|
[946] | 454 | |
---|
[1471] | 455 | def get_mask(self, rowno): |
---|
| 456 | """Return the mask for the current row in the scantable as a list. |
---|
[1846] | 457 | |
---|
[1471] | 458 | Parameters: |
---|
[1846] | 459 | |
---|
[1573] | 460 | rowno: the row number to retrieve the mask from |
---|
[1846] | 461 | |
---|
[1471] | 462 | """ |
---|
| 463 | return self._getmask(rowno) |
---|
| 464 | |
---|
| 465 | def set_spectrum(self, spec, rowno): |
---|
[1938] | 466 | """Set the spectrum for the current row in the scantable. |
---|
[1846] | 467 | |
---|
[1471] | 468 | Parameters: |
---|
[1846] | 469 | |
---|
[1855] | 470 | spec: the new spectrum |
---|
[1846] | 471 | |
---|
[1855] | 472 | rowno: the row number to set the spectrum for |
---|
| 473 | |
---|
[1471] | 474 | """ |
---|
[2348] | 475 | assert(len(spec) == self.nchan(self.getif(rowno))) |
---|
[1471] | 476 | return self._setspectrum(spec, rowno) |
---|
| 477 | |
---|
[1600] | 478 | def get_coordinate(self, rowno): |
---|
| 479 | """Return the (spectral) coordinate for a a given 'rowno'. |
---|
[1846] | 480 | |
---|
| 481 | *Note*: |
---|
| 482 | |
---|
[1600] | 483 | * This coordinate is only valid until a scantable method modifies |
---|
| 484 | the frequency axis. |
---|
| 485 | * This coordinate does contain the original frequency set-up |
---|
| 486 | NOT the new frame. The conversions however are done using the user |
---|
| 487 | specified frame (e.g. LSRK/TOPO). To get the 'real' coordinate, |
---|
| 488 | use scantable.freq_align first. Without it there is no closure, |
---|
[1846] | 489 | i.e.:: |
---|
[1600] | 490 | |
---|
[1846] | 491 | c = myscan.get_coordinate(0) |
---|
| 492 | c.to_frequency(c.get_reference_pixel()) != c.get_reference_value() |
---|
| 493 | |
---|
[1600] | 494 | Parameters: |
---|
[1846] | 495 | |
---|
[1600] | 496 | rowno: the row number for the spectral coordinate |
---|
| 497 | |
---|
| 498 | """ |
---|
| 499 | return coordinate(Scantable.get_coordinate(self, rowno)) |
---|
| 500 | |
---|
[946] | 501 | def get_selection(self): |
---|
[1846] | 502 | """\ |
---|
[1005] | 503 | Get the selection object currently set on this scantable. |
---|
[1846] | 504 | |
---|
| 505 | Example:: |
---|
| 506 | |
---|
[1005] | 507 | sel = scan.get_selection() |
---|
| 508 | sel.set_ifs(0) # select IF 0 |
---|
| 509 | scan.set_selection(sel) # apply modified selection |
---|
[1846] | 510 | |
---|
[946] | 511 | """ |
---|
| 512 | return selector(self._getselection()) |
---|
| 513 | |
---|
[1576] | 514 | def set_selection(self, selection=None, **kw): |
---|
[1846] | 515 | """\ |
---|
[1005] | 516 | Select a subset of the data. All following operations on this scantable |
---|
| 517 | are only applied to thi selection. |
---|
[1846] | 518 | |
---|
[1005] | 519 | Parameters: |
---|
[1697] | 520 | |
---|
[1846] | 521 | selection: a selector object (default unset the selection), or |
---|
[2431] | 522 | any combination of 'pols', 'ifs', 'beams', 'scans', |
---|
| 523 | 'cycles', 'name', 'query' |
---|
[1697] | 524 | |
---|
[1846] | 525 | Examples:: |
---|
[1697] | 526 | |
---|
[1005] | 527 | sel = selector() # create a selection object |
---|
[1118] | 528 | self.set_scans([0, 3]) # select SCANNO 0 and 3 |
---|
[1005] | 529 | scan.set_selection(sel) # set the selection |
---|
| 530 | scan.summary() # will only print summary of scanno 0 an 3 |
---|
| 531 | scan.set_selection() # unset the selection |
---|
[1697] | 532 | # or the equivalent |
---|
| 533 | scan.set_selection(scans=[0,3]) |
---|
| 534 | scan.summary() # will only print summary of scanno 0 an 3 |
---|
| 535 | scan.set_selection() # unset the selection |
---|
[1846] | 536 | |
---|
[946] | 537 | """ |
---|
[1576] | 538 | if selection is None: |
---|
| 539 | # reset |
---|
| 540 | if len(kw) == 0: |
---|
| 541 | selection = selector() |
---|
| 542 | else: |
---|
| 543 | # try keywords |
---|
| 544 | for k in kw: |
---|
| 545 | if k not in selector.fields: |
---|
[2320] | 546 | raise KeyError("Invalid selection key '%s', " |
---|
| 547 | "valid keys are %s" % (k, |
---|
| 548 | selector.fields)) |
---|
[1576] | 549 | selection = selector(**kw) |
---|
[946] | 550 | self._setselection(selection) |
---|
| 551 | |
---|
[1819] | 552 | def get_row(self, row=0, insitu=None): |
---|
[1846] | 553 | """\ |
---|
[1819] | 554 | Select a row in the scantable. |
---|
| 555 | Return a scantable with single row. |
---|
[1846] | 556 | |
---|
[1819] | 557 | Parameters: |
---|
[1846] | 558 | |
---|
| 559 | row: row no of integration, default is 0. |
---|
| 560 | insitu: if False a new scantable is returned. Otherwise, the |
---|
| 561 | scaling is done in-situ. The default is taken from .asaprc |
---|
| 562 | (False) |
---|
| 563 | |
---|
[1819] | 564 | """ |
---|
[2349] | 565 | if insitu is None: |
---|
| 566 | insitu = rcParams['insitu'] |
---|
[1819] | 567 | if not insitu: |
---|
| 568 | workscan = self.copy() |
---|
| 569 | else: |
---|
| 570 | workscan = self |
---|
| 571 | # Select a row |
---|
[2349] | 572 | sel = selector() |
---|
[1992] | 573 | sel.set_rows([row]) |
---|
[1819] | 574 | workscan.set_selection(sel) |
---|
| 575 | if not workscan.nrow() == 1: |
---|
[2349] | 576 | msg = "Could not identify single row. %d rows selected." \ |
---|
| 577 | % (workscan.nrow()) |
---|
[1819] | 578 | raise RuntimeError(msg) |
---|
| 579 | if insitu: |
---|
| 580 | self._assign(workscan) |
---|
| 581 | else: |
---|
| 582 | return workscan |
---|
| 583 | |
---|
[1862] | 584 | @asaplog_post_dec |
---|
[1907] | 585 | def stats(self, stat='stddev', mask=None, form='3.3f', row=None): |
---|
[1846] | 586 | """\ |
---|
[135] | 587 | Determine the specified statistic of the current beam/if/pol |
---|
[102] | 588 | Takes a 'mask' as an optional parameter to specify which |
---|
| 589 | channels should be excluded. |
---|
[1846] | 590 | |
---|
[102] | 591 | Parameters: |
---|
[1846] | 592 | |
---|
[1819] | 593 | stat: 'min', 'max', 'min_abc', 'max_abc', 'sumsq', 'sum', |
---|
| 594 | 'mean', 'var', 'stddev', 'avdev', 'rms', 'median' |
---|
[1855] | 595 | |
---|
[135] | 596 | mask: an optional mask specifying where the statistic |
---|
[102] | 597 | should be determined. |
---|
[1855] | 598 | |
---|
[1819] | 599 | form: format string to print statistic values |
---|
[1846] | 600 | |
---|
[1907] | 601 | row: row number of spectrum to process. |
---|
| 602 | (default is None: for all rows) |
---|
[1846] | 603 | |
---|
[1907] | 604 | Example: |
---|
[113] | 605 | scan.set_unit('channel') |
---|
[1118] | 606 | msk = scan.create_mask([100, 200], [500, 600]) |
---|
[135] | 607 | scan.stats(stat='mean', mask=m) |
---|
[1846] | 608 | |
---|
[102] | 609 | """ |
---|
[1593] | 610 | mask = mask or [] |
---|
[876] | 611 | if not self._check_ifs(): |
---|
[1118] | 612 | raise ValueError("Cannot apply mask as the IFs have different " |
---|
| 613 | "number of channels. Please use setselection() " |
---|
| 614 | "to select individual IFs") |
---|
[1819] | 615 | rtnabc = False |
---|
| 616 | if stat.lower().endswith('_abc'): rtnabc = True |
---|
| 617 | getchan = False |
---|
| 618 | if stat.lower().startswith('min') or stat.lower().startswith('max'): |
---|
| 619 | chan = self._math._minmaxchan(self, mask, stat) |
---|
| 620 | getchan = True |
---|
| 621 | statvals = [] |
---|
[1907] | 622 | if not rtnabc: |
---|
| 623 | if row == None: |
---|
| 624 | statvals = self._math._stats(self, mask, stat) |
---|
| 625 | else: |
---|
| 626 | statvals = self._math._statsrow(self, mask, stat, int(row)) |
---|
[256] | 627 | |
---|
[1819] | 628 | #def cb(i): |
---|
| 629 | # return statvals[i] |
---|
[256] | 630 | |
---|
[1819] | 631 | #return self._row_callback(cb, stat) |
---|
[102] | 632 | |
---|
[1819] | 633 | label=stat |
---|
| 634 | #callback=cb |
---|
| 635 | out = "" |
---|
| 636 | #outvec = [] |
---|
| 637 | sep = '-'*50 |
---|
[1907] | 638 | |
---|
| 639 | if row == None: |
---|
| 640 | rows = xrange(self.nrow()) |
---|
| 641 | elif isinstance(row, int): |
---|
| 642 | rows = [ row ] |
---|
| 643 | |
---|
| 644 | for i in rows: |
---|
[1819] | 645 | refstr = '' |
---|
| 646 | statunit= '' |
---|
| 647 | if getchan: |
---|
| 648 | qx, qy = self.chan2data(rowno=i, chan=chan[i]) |
---|
| 649 | if rtnabc: |
---|
| 650 | statvals.append(qx['value']) |
---|
| 651 | refstr = ('(value: %'+form) % (qy['value'])+' ['+qy['unit']+'])' |
---|
| 652 | statunit= '['+qx['unit']+']' |
---|
| 653 | else: |
---|
| 654 | refstr = ('(@ %'+form) % (qx['value'])+' ['+qx['unit']+'])' |
---|
| 655 | |
---|
| 656 | tm = self._gettime(i) |
---|
| 657 | src = self._getsourcename(i) |
---|
| 658 | out += 'Scan[%d] (%s) ' % (self.getscan(i), src) |
---|
| 659 | out += 'Time[%s]:\n' % (tm) |
---|
[1907] | 660 | if self.nbeam(-1) > 1: out += ' Beam[%d] ' % (self.getbeam(i)) |
---|
| 661 | if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i)) |
---|
| 662 | if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i)) |
---|
[1819] | 663 | #outvec.append(callback(i)) |
---|
[1907] | 664 | if len(rows) > 1: |
---|
| 665 | # out += ('= %'+form) % (outvec[i]) +' '+refstr+'\n' |
---|
| 666 | out += ('= %'+form) % (statvals[i]) +' '+refstr+'\n' |
---|
| 667 | else: |
---|
| 668 | # out += ('= %'+form) % (outvec[0]) +' '+refstr+'\n' |
---|
| 669 | out += ('= %'+form) % (statvals[0]) +' '+refstr+'\n' |
---|
[1819] | 670 | out += sep+"\n" |
---|
| 671 | |
---|
[1859] | 672 | import os |
---|
| 673 | if os.environ.has_key( 'USER' ): |
---|
| 674 | usr = os.environ['USER'] |
---|
| 675 | else: |
---|
| 676 | import commands |
---|
| 677 | usr = commands.getoutput( 'whoami' ) |
---|
| 678 | tmpfile = '/tmp/tmp_'+usr+'_casapy_asap_scantable_stats' |
---|
| 679 | f = open(tmpfile,'w') |
---|
| 680 | print >> f, sep |
---|
| 681 | print >> f, ' %s %s' % (label, statunit) |
---|
| 682 | print >> f, sep |
---|
| 683 | print >> f, out |
---|
| 684 | f.close() |
---|
| 685 | f = open(tmpfile,'r') |
---|
| 686 | x = f.readlines() |
---|
| 687 | f.close() |
---|
| 688 | asaplog.push(''.join(x), False) |
---|
| 689 | |
---|
[1819] | 690 | return statvals |
---|
| 691 | |
---|
| 692 | def chan2data(self, rowno=0, chan=0): |
---|
[1846] | 693 | """\ |
---|
[1819] | 694 | Returns channel/frequency/velocity and spectral value |
---|
| 695 | at an arbitrary row and channel in the scantable. |
---|
[1846] | 696 | |
---|
[1819] | 697 | Parameters: |
---|
[1846] | 698 | |
---|
[1819] | 699 | rowno: a row number in the scantable. Default is the |
---|
| 700 | first row, i.e. rowno=0 |
---|
[1855] | 701 | |
---|
[1819] | 702 | chan: a channel in the scantable. Default is the first |
---|
| 703 | channel, i.e. pos=0 |
---|
[1846] | 704 | |
---|
[1819] | 705 | """ |
---|
| 706 | if isinstance(rowno, int) and isinstance(chan, int): |
---|
| 707 | qx = {'unit': self.get_unit(), |
---|
| 708 | 'value': self._getabcissa(rowno)[chan]} |
---|
| 709 | qy = {'unit': self.get_fluxunit(), |
---|
| 710 | 'value': self._getspectrum(rowno)[chan]} |
---|
| 711 | return qx, qy |
---|
| 712 | |
---|
[1118] | 713 | def stddev(self, mask=None): |
---|
[1846] | 714 | """\ |
---|
[135] | 715 | Determine the standard deviation of the current beam/if/pol |
---|
| 716 | Takes a 'mask' as an optional parameter to specify which |
---|
| 717 | channels should be excluded. |
---|
[1846] | 718 | |
---|
[135] | 719 | Parameters: |
---|
[1846] | 720 | |
---|
[135] | 721 | mask: an optional mask specifying where the standard |
---|
| 722 | deviation should be determined. |
---|
| 723 | |
---|
[1846] | 724 | Example:: |
---|
| 725 | |
---|
[135] | 726 | scan.set_unit('channel') |
---|
[1118] | 727 | msk = scan.create_mask([100, 200], [500, 600]) |
---|
[135] | 728 | scan.stddev(mask=m) |
---|
[1846] | 729 | |
---|
[135] | 730 | """ |
---|
[1118] | 731 | return self.stats(stat='stddev', mask=mask); |
---|
[135] | 732 | |
---|
[1003] | 733 | |
---|
[1259] | 734 | def get_column_names(self): |
---|
[1846] | 735 | """\ |
---|
[1003] | 736 | Return a list of column names, which can be used for selection. |
---|
| 737 | """ |
---|
[1259] | 738 | return list(Scantable.get_column_names(self)) |
---|
[1003] | 739 | |
---|
[1730] | 740 | def get_tsys(self, row=-1): |
---|
[1846] | 741 | """\ |
---|
[113] | 742 | Return the System temperatures. |
---|
[1846] | 743 | |
---|
| 744 | Parameters: |
---|
| 745 | |
---|
| 746 | row: the rowno to get the information for. (default all rows) |
---|
| 747 | |
---|
[113] | 748 | Returns: |
---|
[1846] | 749 | |
---|
[876] | 750 | a list of Tsys values for the current selection |
---|
[1846] | 751 | |
---|
[113] | 752 | """ |
---|
[1730] | 753 | if row > -1: |
---|
| 754 | return self._get_column(self._gettsys, row) |
---|
[876] | 755 | return self._row_callback(self._gettsys, "Tsys") |
---|
[256] | 756 | |
---|
[2406] | 757 | def get_tsysspectrum(self, row=-1): |
---|
| 758 | """\ |
---|
| 759 | Return the channel dependent system temperatures. |
---|
[1730] | 760 | |
---|
[2406] | 761 | Parameters: |
---|
| 762 | |
---|
| 763 | row: the rowno to get the information for. (default all rows) |
---|
| 764 | |
---|
| 765 | Returns: |
---|
| 766 | |
---|
| 767 | a list of Tsys values for the current selection |
---|
| 768 | |
---|
| 769 | """ |
---|
| 770 | return self._get_column( self._gettsysspectrum, row ) |
---|
| 771 | |
---|
[1730] | 772 | def get_weather(self, row=-1): |
---|
[1846] | 773 | """\ |
---|
| 774 | Return the weather informations. |
---|
| 775 | |
---|
| 776 | Parameters: |
---|
| 777 | |
---|
| 778 | row: the rowno to get the information for. (default all rows) |
---|
| 779 | |
---|
| 780 | Returns: |
---|
| 781 | |
---|
| 782 | a dict or list of of dicts of values for the current selection |
---|
| 783 | |
---|
| 784 | """ |
---|
| 785 | |
---|
[1730] | 786 | values = self._get_column(self._get_weather, row) |
---|
| 787 | if row > -1: |
---|
| 788 | return {'temperature': values[0], |
---|
| 789 | 'pressure': values[1], 'humidity' : values[2], |
---|
| 790 | 'windspeed' : values[3], 'windaz' : values[4] |
---|
| 791 | } |
---|
| 792 | else: |
---|
| 793 | out = [] |
---|
| 794 | for r in values: |
---|
| 795 | |
---|
| 796 | out.append({'temperature': r[0], |
---|
| 797 | 'pressure': r[1], 'humidity' : r[2], |
---|
| 798 | 'windspeed' : r[3], 'windaz' : r[4] |
---|
| 799 | }) |
---|
| 800 | return out |
---|
| 801 | |
---|
[876] | 802 | def _row_callback(self, callback, label): |
---|
| 803 | out = "" |
---|
[1118] | 804 | outvec = [] |
---|
[1590] | 805 | sep = '-'*50 |
---|
[876] | 806 | for i in range(self.nrow()): |
---|
| 807 | tm = self._gettime(i) |
---|
| 808 | src = self._getsourcename(i) |
---|
[1590] | 809 | out += 'Scan[%d] (%s) ' % (self.getscan(i), src) |
---|
[876] | 810 | out += 'Time[%s]:\n' % (tm) |
---|
[1590] | 811 | if self.nbeam(-1) > 1: |
---|
| 812 | out += ' Beam[%d] ' % (self.getbeam(i)) |
---|
| 813 | if self.nif(-1) > 1: out += ' IF[%d] ' % (self.getif(i)) |
---|
| 814 | if self.npol(-1) > 1: out += ' Pol[%d] ' % (self.getpol(i)) |
---|
[876] | 815 | outvec.append(callback(i)) |
---|
| 816 | out += '= %3.3f\n' % (outvec[i]) |
---|
[1590] | 817 | out += sep+'\n' |
---|
[1859] | 818 | |
---|
| 819 | asaplog.push(sep) |
---|
| 820 | asaplog.push(" %s" % (label)) |
---|
| 821 | asaplog.push(sep) |
---|
| 822 | asaplog.push(out) |
---|
[1861] | 823 | asaplog.post() |
---|
[1175] | 824 | return outvec |
---|
[256] | 825 | |
---|
[1947] | 826 | def _get_column(self, callback, row=-1, *args): |
---|
[1070] | 827 | """ |
---|
| 828 | """ |
---|
| 829 | if row == -1: |
---|
[1947] | 830 | return [callback(i, *args) for i in range(self.nrow())] |
---|
[1070] | 831 | else: |
---|
[1819] | 832 | if 0 <= row < self.nrow(): |
---|
[1947] | 833 | return callback(row, *args) |
---|
[256] | 834 | |
---|
[1070] | 835 | |
---|
[1948] | 836 | def get_time(self, row=-1, asdatetime=False, prec=-1): |
---|
[1846] | 837 | """\ |
---|
[113] | 838 | Get a list of time stamps for the observations. |
---|
[1938] | 839 | Return a datetime object or a string (default) for each |
---|
| 840 | integration time stamp in the scantable. |
---|
[1846] | 841 | |
---|
[113] | 842 | Parameters: |
---|
[1846] | 843 | |
---|
[1348] | 844 | row: row no of integration. Default -1 return all rows |
---|
[1855] | 845 | |
---|
[1348] | 846 | asdatetime: return values as datetime objects rather than strings |
---|
[1846] | 847 | |
---|
[2349] | 848 | prec: number of digits shown. Default -1 to automatic |
---|
| 849 | calculation. |
---|
[1948] | 850 | Note this number is equals to the digits of MVTime, |
---|
| 851 | i.e., 0<prec<3: dates with hh:: only, |
---|
| 852 | <5: with hh:mm:, <7 or 0: with hh:mm:ss, |
---|
[1947] | 853 | and 6> : with hh:mm:ss.tt... (prec-6 t's added) |
---|
| 854 | |
---|
[113] | 855 | """ |
---|
[1175] | 856 | from datetime import datetime |
---|
[1948] | 857 | if prec < 0: |
---|
| 858 | # automagically set necessary precision +1 |
---|
[2349] | 859 | prec = 7 - \ |
---|
| 860 | numpy.floor(numpy.log10(numpy.min(self.get_inttime(row)))) |
---|
[1948] | 861 | prec = max(6, int(prec)) |
---|
| 862 | else: |
---|
| 863 | prec = max(0, prec) |
---|
| 864 | if asdatetime: |
---|
| 865 | #precision can be 1 millisecond at max |
---|
| 866 | prec = min(12, prec) |
---|
| 867 | |
---|
[1947] | 868 | times = self._get_column(self._gettime, row, prec) |
---|
[1348] | 869 | if not asdatetime: |
---|
[1392] | 870 | return times |
---|
[1947] | 871 | format = "%Y/%m/%d/%H:%M:%S.%f" |
---|
| 872 | if prec < 7: |
---|
| 873 | nsub = 1 + (((6-prec)/2) % 3) |
---|
| 874 | substr = [".%f","%S","%M"] |
---|
| 875 | for i in range(nsub): |
---|
| 876 | format = format.replace(substr[i],"") |
---|
[1175] | 877 | if isinstance(times, list): |
---|
[1947] | 878 | return [datetime.strptime(i, format) for i in times] |
---|
[1175] | 879 | else: |
---|
[1947] | 880 | return datetime.strptime(times, format) |
---|
[102] | 881 | |
---|
[1348] | 882 | |
---|
| 883 | def get_inttime(self, row=-1): |
---|
[1846] | 884 | """\ |
---|
[1348] | 885 | Get a list of integration times for the observations. |
---|
| 886 | Return a time in seconds for each integration in the scantable. |
---|
[1846] | 887 | |
---|
[1348] | 888 | Parameters: |
---|
[1846] | 889 | |
---|
[1348] | 890 | row: row no of integration. Default -1 return all rows. |
---|
[1846] | 891 | |
---|
[1348] | 892 | """ |
---|
[1573] | 893 | return self._get_column(self._getinttime, row) |
---|
[1348] | 894 | |
---|
[1573] | 895 | |
---|
[714] | 896 | def get_sourcename(self, row=-1): |
---|
[1846] | 897 | """\ |
---|
[794] | 898 | Get a list source names for the observations. |
---|
[714] | 899 | Return a string for each integration in the scantable. |
---|
| 900 | Parameters: |
---|
[1846] | 901 | |
---|
[1348] | 902 | row: row no of integration. Default -1 return all rows. |
---|
[1846] | 903 | |
---|
[714] | 904 | """ |
---|
[1070] | 905 | return self._get_column(self._getsourcename, row) |
---|
[714] | 906 | |
---|
[794] | 907 | def get_elevation(self, row=-1): |
---|
[1846] | 908 | """\ |
---|
[794] | 909 | Get a list of elevations for the observations. |
---|
| 910 | Return a float for each integration in the scantable. |
---|
[1846] | 911 | |
---|
[794] | 912 | Parameters: |
---|
[1846] | 913 | |
---|
[1348] | 914 | row: row no of integration. Default -1 return all rows. |
---|
[1846] | 915 | |
---|
[794] | 916 | """ |
---|
[1070] | 917 | return self._get_column(self._getelevation, row) |
---|
[794] | 918 | |
---|
| 919 | def get_azimuth(self, row=-1): |
---|
[1846] | 920 | """\ |
---|
[794] | 921 | Get a list of azimuths for the observations. |
---|
| 922 | Return a float for each integration in the scantable. |
---|
[1846] | 923 | |
---|
[794] | 924 | Parameters: |
---|
[1348] | 925 | row: row no of integration. Default -1 return all rows. |
---|
[1846] | 926 | |
---|
[794] | 927 | """ |
---|
[1070] | 928 | return self._get_column(self._getazimuth, row) |
---|
[794] | 929 | |
---|
| 930 | def get_parangle(self, row=-1): |
---|
[1846] | 931 | """\ |
---|
[794] | 932 | Get a list of parallactic angles for the observations. |
---|
| 933 | Return a float for each integration in the scantable. |
---|
[1846] | 934 | |
---|
[794] | 935 | Parameters: |
---|
[1846] | 936 | |
---|
[1348] | 937 | row: row no of integration. Default -1 return all rows. |
---|
[1846] | 938 | |
---|
[794] | 939 | """ |
---|
[1070] | 940 | return self._get_column(self._getparangle, row) |
---|
[794] | 941 | |
---|
[1070] | 942 | def get_direction(self, row=-1): |
---|
| 943 | """ |
---|
| 944 | Get a list of Positions on the sky (direction) for the observations. |
---|
[1594] | 945 | Return a string for each integration in the scantable. |
---|
[1855] | 946 | |
---|
[1070] | 947 | Parameters: |
---|
[1855] | 948 | |
---|
[1070] | 949 | row: row no of integration. Default -1 return all rows |
---|
[1855] | 950 | |
---|
[1070] | 951 | """ |
---|
| 952 | return self._get_column(self._getdirection, row) |
---|
| 953 | |
---|
[1391] | 954 | def get_directionval(self, row=-1): |
---|
[1846] | 955 | """\ |
---|
[1391] | 956 | Get a list of Positions on the sky (direction) for the observations. |
---|
| 957 | Return a float for each integration in the scantable. |
---|
[1846] | 958 | |
---|
[1391] | 959 | Parameters: |
---|
[1846] | 960 | |
---|
[1391] | 961 | row: row no of integration. Default -1 return all rows |
---|
[1846] | 962 | |
---|
[1391] | 963 | """ |
---|
| 964 | return self._get_column(self._getdirectionvec, row) |
---|
| 965 | |
---|
[1862] | 966 | @asaplog_post_dec |
---|
[102] | 967 | def set_unit(self, unit='channel'): |
---|
[1846] | 968 | """\ |
---|
[102] | 969 | Set the unit for all following operations on this scantable |
---|
[1846] | 970 | |
---|
[102] | 971 | Parameters: |
---|
[1846] | 972 | |
---|
| 973 | unit: optional unit, default is 'channel'. Use one of '*Hz', |
---|
| 974 | 'km/s', 'channel' or equivalent '' |
---|
| 975 | |
---|
[102] | 976 | """ |
---|
[484] | 977 | varlist = vars() |
---|
[1118] | 978 | if unit in ['', 'pixel', 'channel']: |
---|
[113] | 979 | unit = '' |
---|
| 980 | inf = list(self._getcoordinfo()) |
---|
| 981 | inf[0] = unit |
---|
| 982 | self._setcoordinfo(inf) |
---|
[1118] | 983 | self._add_history("set_unit", varlist) |
---|
[113] | 984 | |
---|
[1862] | 985 | @asaplog_post_dec |
---|
[484] | 986 | def set_instrument(self, instr): |
---|
[1846] | 987 | """\ |
---|
[1348] | 988 | Set the instrument for subsequent processing. |
---|
[1846] | 989 | |
---|
[358] | 990 | Parameters: |
---|
[1846] | 991 | |
---|
[710] | 992 | instr: Select from 'ATPKSMB', 'ATPKSHOH', 'ATMOPRA', |
---|
[407] | 993 | 'DSS-43' (Tid), 'CEDUNA', and 'HOBART' |
---|
[1846] | 994 | |
---|
[358] | 995 | """ |
---|
| 996 | self._setInstrument(instr) |
---|
[1118] | 997 | self._add_history("set_instument", vars()) |
---|
[358] | 998 | |
---|
[1862] | 999 | @asaplog_post_dec |
---|
[1190] | 1000 | def set_feedtype(self, feedtype): |
---|
[1846] | 1001 | """\ |
---|
[1190] | 1002 | Overwrite the feed type, which might not be set correctly. |
---|
[1846] | 1003 | |
---|
[1190] | 1004 | Parameters: |
---|
[1846] | 1005 | |
---|
[1190] | 1006 | feedtype: 'linear' or 'circular' |
---|
[1846] | 1007 | |
---|
[1190] | 1008 | """ |
---|
| 1009 | self._setfeedtype(feedtype) |
---|
| 1010 | self._add_history("set_feedtype", vars()) |
---|
| 1011 | |
---|
[1862] | 1012 | @asaplog_post_dec |
---|
[276] | 1013 | def set_doppler(self, doppler='RADIO'): |
---|
[1846] | 1014 | """\ |
---|
[276] | 1015 | Set the doppler for all following operations on this scantable. |
---|
[1846] | 1016 | |
---|
[276] | 1017 | Parameters: |
---|
[1846] | 1018 | |
---|
[276] | 1019 | doppler: One of 'RADIO', 'OPTICAL', 'Z', 'BETA', 'GAMMA' |
---|
[1846] | 1020 | |
---|
[276] | 1021 | """ |
---|
[484] | 1022 | varlist = vars() |
---|
[276] | 1023 | inf = list(self._getcoordinfo()) |
---|
| 1024 | inf[2] = doppler |
---|
| 1025 | self._setcoordinfo(inf) |
---|
[1118] | 1026 | self._add_history("set_doppler", vars()) |
---|
[710] | 1027 | |
---|
[1862] | 1028 | @asaplog_post_dec |
---|
[226] | 1029 | def set_freqframe(self, frame=None): |
---|
[1846] | 1030 | """\ |
---|
[113] | 1031 | Set the frame type of the Spectral Axis. |
---|
[1846] | 1032 | |
---|
[113] | 1033 | Parameters: |
---|
[1846] | 1034 | |
---|
[591] | 1035 | frame: an optional frame type, default 'LSRK'. Valid frames are: |
---|
[1819] | 1036 | 'TOPO', 'LSRD', 'LSRK', 'BARY', |
---|
[1118] | 1037 | 'GEO', 'GALACTO', 'LGROUP', 'CMB' |
---|
[1846] | 1038 | |
---|
| 1039 | Example:: |
---|
| 1040 | |
---|
[113] | 1041 | scan.set_freqframe('BARY') |
---|
[1846] | 1042 | |
---|
[113] | 1043 | """ |
---|
[1593] | 1044 | frame = frame or rcParams['scantable.freqframe'] |
---|
[484] | 1045 | varlist = vars() |
---|
[1819] | 1046 | # "REST" is not implemented in casacore |
---|
| 1047 | #valid = ['REST', 'TOPO', 'LSRD', 'LSRK', 'BARY', \ |
---|
| 1048 | # 'GEO', 'GALACTO', 'LGROUP', 'CMB'] |
---|
| 1049 | valid = ['TOPO', 'LSRD', 'LSRK', 'BARY', \ |
---|
[1118] | 1050 | 'GEO', 'GALACTO', 'LGROUP', 'CMB'] |
---|
[591] | 1051 | |
---|
[989] | 1052 | if frame in valid: |
---|
[113] | 1053 | inf = list(self._getcoordinfo()) |
---|
| 1054 | inf[1] = frame |
---|
| 1055 | self._setcoordinfo(inf) |
---|
[1118] | 1056 | self._add_history("set_freqframe", varlist) |
---|
[102] | 1057 | else: |
---|
[1118] | 1058 | msg = "Please specify a valid freq type. Valid types are:\n", valid |
---|
[1859] | 1059 | raise TypeError(msg) |
---|
[710] | 1060 | |
---|
[1862] | 1061 | @asaplog_post_dec |
---|
[989] | 1062 | def set_dirframe(self, frame=""): |
---|
[1846] | 1063 | """\ |
---|
[989] | 1064 | Set the frame type of the Direction on the sky. |
---|
[1846] | 1065 | |
---|
[989] | 1066 | Parameters: |
---|
[1846] | 1067 | |
---|
[989] | 1068 | frame: an optional frame type, default ''. Valid frames are: |
---|
| 1069 | 'J2000', 'B1950', 'GALACTIC' |
---|
[1846] | 1070 | |
---|
| 1071 | Example: |
---|
| 1072 | |
---|
[989] | 1073 | scan.set_dirframe('GALACTIC') |
---|
[1846] | 1074 | |
---|
[989] | 1075 | """ |
---|
| 1076 | varlist = vars() |
---|
[1859] | 1077 | Scantable.set_dirframe(self, frame) |
---|
[1118] | 1078 | self._add_history("set_dirframe", varlist) |
---|
[989] | 1079 | |
---|
[113] | 1080 | def get_unit(self): |
---|
[1846] | 1081 | """\ |
---|
[113] | 1082 | Get the default unit set in this scantable |
---|
[1846] | 1083 | |
---|
[113] | 1084 | Returns: |
---|
[1846] | 1085 | |
---|
[113] | 1086 | A unit string |
---|
[1846] | 1087 | |
---|
[113] | 1088 | """ |
---|
| 1089 | inf = self._getcoordinfo() |
---|
| 1090 | unit = inf[0] |
---|
| 1091 | if unit == '': unit = 'channel' |
---|
| 1092 | return unit |
---|
[102] | 1093 | |
---|
[1862] | 1094 | @asaplog_post_dec |
---|
[158] | 1095 | def get_abcissa(self, rowno=0): |
---|
[1846] | 1096 | """\ |
---|
[158] | 1097 | Get the abcissa in the current coordinate setup for the currently |
---|
[113] | 1098 | selected Beam/IF/Pol |
---|
[1846] | 1099 | |
---|
[113] | 1100 | Parameters: |
---|
[1846] | 1101 | |
---|
[226] | 1102 | rowno: an optional row number in the scantable. Default is the |
---|
| 1103 | first row, i.e. rowno=0 |
---|
[1846] | 1104 | |
---|
[113] | 1105 | Returns: |
---|
[1846] | 1106 | |
---|
[1348] | 1107 | The abcissa values and the format string (as a dictionary) |
---|
[1846] | 1108 | |
---|
[113] | 1109 | """ |
---|
[256] | 1110 | abc = self._getabcissa(rowno) |
---|
[710] | 1111 | lbl = self._getabcissalabel(rowno) |
---|
[158] | 1112 | return abc, lbl |
---|
[113] | 1113 | |
---|
[1862] | 1114 | @asaplog_post_dec |
---|
[2322] | 1115 | def flag(self, mask=None, unflag=False, row=-1): |
---|
[1846] | 1116 | """\ |
---|
[1001] | 1117 | Flag the selected data using an optional channel mask. |
---|
[1846] | 1118 | |
---|
[1001] | 1119 | Parameters: |
---|
[1846] | 1120 | |
---|
[1001] | 1121 | mask: an optional channel mask, created with create_mask. Default |
---|
| 1122 | (no mask) is all channels. |
---|
[1855] | 1123 | |
---|
[1819] | 1124 | unflag: if True, unflag the data |
---|
[1846] | 1125 | |
---|
[2322] | 1126 | row: an optional row number in the scantable. |
---|
| 1127 | Default -1 flags all rows |
---|
| 1128 | |
---|
[1001] | 1129 | """ |
---|
| 1130 | varlist = vars() |
---|
[1593] | 1131 | mask = mask or [] |
---|
[1994] | 1132 | self._flag(row, mask, unflag) |
---|
[1001] | 1133 | self._add_history("flag", varlist) |
---|
| 1134 | |
---|
[1862] | 1135 | @asaplog_post_dec |
---|
[2322] | 1136 | def flag_row(self, rows=None, unflag=False): |
---|
[1846] | 1137 | """\ |
---|
[1819] | 1138 | Flag the selected data in row-based manner. |
---|
[1846] | 1139 | |
---|
[1819] | 1140 | Parameters: |
---|
[1846] | 1141 | |
---|
[1843] | 1142 | rows: list of row numbers to be flagged. Default is no row |
---|
[2322] | 1143 | (must be explicitly specified to execute row-based |
---|
| 1144 | flagging). |
---|
[1855] | 1145 | |
---|
[1819] | 1146 | unflag: if True, unflag the data. |
---|
[1846] | 1147 | |
---|
[1819] | 1148 | """ |
---|
| 1149 | varlist = vars() |
---|
[2322] | 1150 | if rows is None: |
---|
| 1151 | rows = [] |
---|
[1859] | 1152 | self._flag_row(rows, unflag) |
---|
[1819] | 1153 | self._add_history("flag_row", varlist) |
---|
| 1154 | |
---|
[1862] | 1155 | @asaplog_post_dec |
---|
[1819] | 1156 | def clip(self, uthres=None, dthres=None, clipoutside=True, unflag=False): |
---|
[1846] | 1157 | """\ |
---|
[1819] | 1158 | Flag the selected data outside a specified range (in channel-base) |
---|
[1846] | 1159 | |
---|
[1819] | 1160 | Parameters: |
---|
[1846] | 1161 | |
---|
[1819] | 1162 | uthres: upper threshold. |
---|
[1855] | 1163 | |
---|
[1819] | 1164 | dthres: lower threshold |
---|
[1846] | 1165 | |
---|
[2322] | 1166 | clipoutside: True for flagging data outside the range |
---|
| 1167 | [dthres:uthres]. |
---|
[1928] | 1168 | False for flagging data inside the range. |
---|
[1855] | 1169 | |
---|
[1846] | 1170 | unflag: if True, unflag the data. |
---|
| 1171 | |
---|
[1819] | 1172 | """ |
---|
| 1173 | varlist = vars() |
---|
[1859] | 1174 | self._clip(uthres, dthres, clipoutside, unflag) |
---|
[1819] | 1175 | self._add_history("clip", varlist) |
---|
| 1176 | |
---|
[1862] | 1177 | @asaplog_post_dec |
---|
[1584] | 1178 | def lag_flag(self, start, end, unit="MHz", insitu=None): |
---|
[1846] | 1179 | """\ |
---|
[1192] | 1180 | Flag the data in 'lag' space by providing a frequency to remove. |
---|
[2177] | 1181 | Flagged data in the scantable get interpolated over the region. |
---|
[1192] | 1182 | No taper is applied. |
---|
[1846] | 1183 | |
---|
[1192] | 1184 | Parameters: |
---|
[1846] | 1185 | |
---|
[1579] | 1186 | start: the start frequency (really a period within the |
---|
| 1187 | bandwidth) or period to remove |
---|
[1855] | 1188 | |
---|
[1579] | 1189 | end: the end frequency or period to remove |
---|
[1855] | 1190 | |
---|
[2431] | 1191 | unit: the frequency unit (default 'MHz') or '' for |
---|
[1579] | 1192 | explicit lag channels |
---|
[1846] | 1193 | |
---|
| 1194 | *Notes*: |
---|
| 1195 | |
---|
[1579] | 1196 | It is recommended to flag edges of the band or strong |
---|
[1348] | 1197 | signals beforehand. |
---|
[1846] | 1198 | |
---|
[1192] | 1199 | """ |
---|
| 1200 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 1201 | self._math._setinsitu(insitu) |
---|
| 1202 | varlist = vars() |
---|
[1579] | 1203 | base = { "GHz": 1000000000., "MHz": 1000000., "kHz": 1000., "Hz": 1.} |
---|
| 1204 | if not (unit == "" or base.has_key(unit)): |
---|
[1192] | 1205 | raise ValueError("%s is not a valid unit." % unit) |
---|
[1859] | 1206 | if unit == "": |
---|
| 1207 | s = scantable(self._math._lag_flag(self, start, end, "lags")) |
---|
| 1208 | else: |
---|
| 1209 | s = scantable(self._math._lag_flag(self, start*base[unit], |
---|
| 1210 | end*base[unit], "frequency")) |
---|
[1192] | 1211 | s._add_history("lag_flag", varlist) |
---|
| 1212 | if insitu: |
---|
| 1213 | self._assign(s) |
---|
| 1214 | else: |
---|
| 1215 | return s |
---|
[1001] | 1216 | |
---|
[1862] | 1217 | @asaplog_post_dec |
---|
[2349] | 1218 | def fft(self, rowno=None, mask=None, getrealimag=False): |
---|
[2177] | 1219 | """\ |
---|
| 1220 | Apply FFT to the spectra. |
---|
| 1221 | Flagged data in the scantable get interpolated over the region. |
---|
| 1222 | |
---|
| 1223 | Parameters: |
---|
[2186] | 1224 | |
---|
| 1225 | rowno: The row number(s) to be processed. int, list |
---|
[2349] | 1226 | and tuple are accepted. By default (None), FFT |
---|
[2186] | 1227 | is applied to the whole data. |
---|
| 1228 | |
---|
| 1229 | mask: Auxiliary channel mask(s). Given as a boolean |
---|
| 1230 | list, it is applied to all specified rows. |
---|
| 1231 | A list of boolean lists can also be used to |
---|
| 1232 | apply different masks. In the latter case, the |
---|
| 1233 | length of 'mask' must be the same as that of |
---|
[2349] | 1234 | 'rowno'. The default is None. |
---|
[2177] | 1235 | |
---|
| 1236 | getrealimag: If True, returns the real and imaginary part |
---|
| 1237 | values of the complex results. |
---|
| 1238 | If False (the default), returns the amplitude |
---|
| 1239 | (absolute value) normalised with Ndata/2 and |
---|
| 1240 | phase (argument, in unit of radian). |
---|
| 1241 | |
---|
| 1242 | Returns: |
---|
| 1243 | |
---|
[2186] | 1244 | A list of dictionaries containing the results for each spectrum. |
---|
| 1245 | Each dictionary contains two values, the real and the imaginary |
---|
| 1246 | parts when getrealimag = True, or the amplitude(absolute value) |
---|
| 1247 | and the phase(argument) when getrealimag = False. The key for |
---|
| 1248 | these values are 'real' and 'imag', or 'ampl' and 'phase', |
---|
[2177] | 1249 | respectively. |
---|
| 1250 | """ |
---|
[2349] | 1251 | if rowno is None: |
---|
| 1252 | rowno = [] |
---|
[2177] | 1253 | if isinstance(rowno, int): |
---|
| 1254 | rowno = [rowno] |
---|
| 1255 | elif not (isinstance(rowno, list) or isinstance(rowno, tuple)): |
---|
[2186] | 1256 | raise TypeError("The row number(s) must be int, list or tuple.") |
---|
| 1257 | if len(rowno) == 0: rowno = [i for i in xrange(self.nrow())] |
---|
| 1258 | |
---|
[2411] | 1259 | usecommonmask = True |
---|
| 1260 | |
---|
| 1261 | if mask is None: |
---|
| 1262 | mask = [] |
---|
| 1263 | if isinstance(mask, list) or isinstance(mask, tuple): |
---|
| 1264 | if len(mask) == 0: |
---|
| 1265 | mask = [[]] |
---|
| 1266 | else: |
---|
| 1267 | if isinstance(mask[0], bool): |
---|
| 1268 | if len(mask) != self.nchan(self.getif(rowno[0])): |
---|
| 1269 | raise ValueError("The spectra and the mask have " |
---|
| 1270 | "different length.") |
---|
| 1271 | mask = [mask] |
---|
| 1272 | elif isinstance(mask[0], list) or isinstance(mask[0], tuple): |
---|
| 1273 | usecommonmask = False |
---|
| 1274 | if len(mask) != len(rowno): |
---|
| 1275 | raise ValueError("When specifying masks for each " |
---|
| 1276 | "spectrum, the numbers of them " |
---|
| 1277 | "must be identical.") |
---|
| 1278 | for i in xrange(mask): |
---|
| 1279 | if len(mask[i]) != self.nchan(self.getif(rowno[i])): |
---|
| 1280 | raise ValueError("The spectra and the mask have " |
---|
| 1281 | "different length.") |
---|
| 1282 | else: |
---|
| 1283 | raise TypeError("The mask must be a boolean list or " |
---|
| 1284 | "a list of boolean list.") |
---|
| 1285 | else: |
---|
[2349] | 1286 | raise TypeError("The mask must be a boolean list or a list of " |
---|
| 1287 | "boolean list.") |
---|
[2186] | 1288 | |
---|
| 1289 | res = [] |
---|
| 1290 | |
---|
| 1291 | imask = 0 |
---|
| 1292 | for whichrow in rowno: |
---|
| 1293 | fspec = self._fft(whichrow, mask[imask], getrealimag) |
---|
| 1294 | nspec = len(fspec) |
---|
[2177] | 1295 | |
---|
[2349] | 1296 | i = 0 |
---|
| 1297 | v1 = [] |
---|
| 1298 | v2 = [] |
---|
| 1299 | reselem = {"real":[],"imag":[]} if getrealimag \ |
---|
| 1300 | else {"ampl":[],"phase":[]} |
---|
[2177] | 1301 | |
---|
[2186] | 1302 | while (i < nspec): |
---|
| 1303 | v1.append(fspec[i]) |
---|
| 1304 | v2.append(fspec[i+1]) |
---|
[2349] | 1305 | i += 2 |
---|
[2186] | 1306 | |
---|
[2177] | 1307 | if getrealimag: |
---|
[2186] | 1308 | reselem["real"] += v1 |
---|
| 1309 | reselem["imag"] += v2 |
---|
[2177] | 1310 | else: |
---|
[2186] | 1311 | reselem["ampl"] += v1 |
---|
| 1312 | reselem["phase"] += v2 |
---|
[2177] | 1313 | |
---|
[2186] | 1314 | res.append(reselem) |
---|
| 1315 | |
---|
[2349] | 1316 | if not usecommonmask: |
---|
| 1317 | imask += 1 |
---|
[2186] | 1318 | |
---|
[2177] | 1319 | return res |
---|
| 1320 | |
---|
| 1321 | @asaplog_post_dec |
---|
[113] | 1322 | def create_mask(self, *args, **kwargs): |
---|
[1846] | 1323 | """\ |
---|
[1118] | 1324 | Compute and return a mask based on [min, max] windows. |
---|
[189] | 1325 | The specified windows are to be INCLUDED, when the mask is |
---|
[113] | 1326 | applied. |
---|
[1846] | 1327 | |
---|
[102] | 1328 | Parameters: |
---|
[1846] | 1329 | |
---|
[1118] | 1330 | [min, max], [min2, max2], ... |
---|
[1024] | 1331 | Pairs of start/end points (inclusive)specifying the regions |
---|
[102] | 1332 | to be masked |
---|
[1855] | 1333 | |
---|
[189] | 1334 | invert: optional argument. If specified as True, |
---|
| 1335 | return an inverted mask, i.e. the regions |
---|
| 1336 | specified are EXCLUDED |
---|
[1855] | 1337 | |
---|
[513] | 1338 | row: create the mask using the specified row for |
---|
| 1339 | unit conversions, default is row=0 |
---|
| 1340 | only necessary if frequency varies over rows. |
---|
[1846] | 1341 | |
---|
| 1342 | Examples:: |
---|
| 1343 | |
---|
[113] | 1344 | scan.set_unit('channel') |
---|
[1846] | 1345 | # a) |
---|
[1118] | 1346 | msk = scan.create_mask([400, 500], [800, 900]) |
---|
[189] | 1347 | # masks everything outside 400 and 500 |
---|
[113] | 1348 | # and 800 and 900 in the unit 'channel' |
---|
| 1349 | |
---|
[1846] | 1350 | # b) |
---|
[1118] | 1351 | msk = scan.create_mask([400, 500], [800, 900], invert=True) |
---|
[189] | 1352 | # masks the regions between 400 and 500 |
---|
[113] | 1353 | # and 800 and 900 in the unit 'channel' |
---|
[1846] | 1354 | |
---|
| 1355 | # c) |
---|
| 1356 | #mask only channel 400 |
---|
[1554] | 1357 | msk = scan.create_mask([400]) |
---|
[1846] | 1358 | |
---|
[102] | 1359 | """ |
---|
[1554] | 1360 | row = kwargs.get("row", 0) |
---|
[513] | 1361 | data = self._getabcissa(row) |
---|
[113] | 1362 | u = self._getcoordinfo()[0] |
---|
[1859] | 1363 | if u == "": |
---|
| 1364 | u = "channel" |
---|
| 1365 | msg = "The current mask window unit is %s" % u |
---|
| 1366 | i = self._check_ifs() |
---|
| 1367 | if not i: |
---|
| 1368 | msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) |
---|
| 1369 | asaplog.push(msg) |
---|
[2348] | 1370 | n = len(data) |
---|
[1295] | 1371 | msk = _n_bools(n, False) |
---|
[710] | 1372 | # test if args is a 'list' or a 'normal *args - UGLY!!! |
---|
| 1373 | |
---|
[2349] | 1374 | ws = (isinstance(args[-1][-1], int) |
---|
| 1375 | or isinstance(args[-1][-1], float)) and args or args[0] |
---|
[710] | 1376 | for window in ws: |
---|
[1554] | 1377 | if len(window) == 1: |
---|
| 1378 | window = [window[0], window[0]] |
---|
| 1379 | if len(window) == 0 or len(window) > 2: |
---|
[2349] | 1380 | raise ValueError("A window needs to be defined as " |
---|
| 1381 | "[start(, end)]") |
---|
[1545] | 1382 | if window[0] > window[1]: |
---|
| 1383 | tmp = window[0] |
---|
| 1384 | window[0] = window[1] |
---|
| 1385 | window[1] = tmp |
---|
[102] | 1386 | for i in range(n): |
---|
[1024] | 1387 | if data[i] >= window[0] and data[i] <= window[1]: |
---|
[1295] | 1388 | msk[i] = True |
---|
[113] | 1389 | if kwargs.has_key('invert'): |
---|
| 1390 | if kwargs.get('invert'): |
---|
[1295] | 1391 | msk = mask_not(msk) |
---|
[102] | 1392 | return msk |
---|
[710] | 1393 | |
---|
[1931] | 1394 | def get_masklist(self, mask=None, row=0, silent=False): |
---|
[1846] | 1395 | """\ |
---|
[1819] | 1396 | Compute and return a list of mask windows, [min, max]. |
---|
[1846] | 1397 | |
---|
[1819] | 1398 | Parameters: |
---|
[1846] | 1399 | |
---|
[1819] | 1400 | mask: channel mask, created with create_mask. |
---|
[1855] | 1401 | |
---|
[1819] | 1402 | row: calcutate the masklist using the specified row |
---|
| 1403 | for unit conversions, default is row=0 |
---|
| 1404 | only necessary if frequency varies over rows. |
---|
[1846] | 1405 | |
---|
[1819] | 1406 | Returns: |
---|
[1846] | 1407 | |
---|
[1819] | 1408 | [min, max], [min2, max2], ... |
---|
| 1409 | Pairs of start/end points (inclusive)specifying |
---|
| 1410 | the masked regions |
---|
[1846] | 1411 | |
---|
[1819] | 1412 | """ |
---|
| 1413 | if not (isinstance(mask,list) or isinstance(mask, tuple)): |
---|
| 1414 | raise TypeError("The mask should be list or tuple.") |
---|
[2427] | 1415 | if len(mask) <= 0: |
---|
| 1416 | raise TypeError("The mask elements should be > 0") |
---|
[2348] | 1417 | data = self._getabcissa(row) |
---|
| 1418 | if len(data) != len(mask): |
---|
[1819] | 1419 | msg = "Number of channels in scantable != number of mask elements" |
---|
| 1420 | raise TypeError(msg) |
---|
| 1421 | u = self._getcoordinfo()[0] |
---|
[1859] | 1422 | if u == "": |
---|
| 1423 | u = "channel" |
---|
| 1424 | msg = "The current mask window unit is %s" % u |
---|
| 1425 | i = self._check_ifs() |
---|
| 1426 | if not i: |
---|
| 1427 | msg += "\nThis mask is only valid for IF=%d" % (self.getif(i)) |
---|
[1931] | 1428 | if not silent: |
---|
| 1429 | asaplog.push(msg) |
---|
[2349] | 1430 | masklist = [] |
---|
[1819] | 1431 | ist, ien = None, None |
---|
| 1432 | ist, ien=self.get_mask_indices(mask) |
---|
| 1433 | if ist is not None and ien is not None: |
---|
| 1434 | for i in xrange(len(ist)): |
---|
| 1435 | range=[data[ist[i]],data[ien[i]]] |
---|
| 1436 | range.sort() |
---|
| 1437 | masklist.append([range[0],range[1]]) |
---|
| 1438 | return masklist |
---|
| 1439 | |
---|
| 1440 | def get_mask_indices(self, mask=None): |
---|
[1846] | 1441 | """\ |
---|
[1819] | 1442 | Compute and Return lists of mask start indices and mask end indices. |
---|
[1855] | 1443 | |
---|
| 1444 | Parameters: |
---|
| 1445 | |
---|
[1819] | 1446 | mask: channel mask, created with create_mask. |
---|
[1846] | 1447 | |
---|
[1819] | 1448 | Returns: |
---|
[1846] | 1449 | |
---|
[1819] | 1450 | List of mask start indices and that of mask end indices, |
---|
| 1451 | i.e., [istart1,istart2,....], [iend1,iend2,....]. |
---|
[1846] | 1452 | |
---|
[1819] | 1453 | """ |
---|
| 1454 | if not (isinstance(mask,list) or isinstance(mask, tuple)): |
---|
| 1455 | raise TypeError("The mask should be list or tuple.") |
---|
[2427] | 1456 | if len(mask) <= 0: |
---|
| 1457 | raise TypeError("The mask elements should be > 0") |
---|
[2349] | 1458 | istart = [] |
---|
| 1459 | iend = [] |
---|
| 1460 | if mask[0]: |
---|
| 1461 | istart.append(0) |
---|
[1819] | 1462 | for i in range(len(mask)-1): |
---|
| 1463 | if not mask[i] and mask[i+1]: |
---|
| 1464 | istart.append(i+1) |
---|
| 1465 | elif mask[i] and not mask[i+1]: |
---|
| 1466 | iend.append(i) |
---|
[2349] | 1467 | if mask[len(mask)-1]: |
---|
| 1468 | iend.append(len(mask)-1) |
---|
[1819] | 1469 | if len(istart) != len(iend): |
---|
| 1470 | raise RuntimeError("Numbers of mask start != mask end.") |
---|
| 1471 | for i in range(len(istart)): |
---|
| 1472 | if istart[i] > iend[i]: |
---|
| 1473 | raise RuntimeError("Mask start index > mask end index") |
---|
| 1474 | break |
---|
| 1475 | return istart,iend |
---|
| 1476 | |
---|
[2013] | 1477 | @asaplog_post_dec |
---|
[2349] | 1478 | def parse_maskexpr(self, maskstring): |
---|
[2013] | 1479 | """ |
---|
| 1480 | Parse CASA type mask selection syntax (IF dependent). |
---|
| 1481 | |
---|
| 1482 | Parameters: |
---|
| 1483 | maskstring : A string mask selection expression. |
---|
| 1484 | A comma separated selections mean different IF - |
---|
| 1485 | channel combinations. IFs and channel selections |
---|
| 1486 | are partitioned by a colon, ':'. |
---|
| 1487 | examples: |
---|
[2015] | 1488 | '' = all IFs (all channels) |
---|
[2013] | 1489 | '<2,4~6,9' = IFs 0,1,4,5,6,9 (all channels) |
---|
| 1490 | '3:3~45;60' = channels 3 to 45 and 60 in IF 3 |
---|
| 1491 | '0~1:2~6,8' = channels 2 to 6 in IFs 0,1, and |
---|
| 1492 | all channels in IF8 |
---|
| 1493 | Returns: |
---|
| 1494 | A dictionary of selected (valid) IF and masklist pairs, |
---|
| 1495 | e.g. {'0': [[50,250],[350,462]], '2': [[100,400],[550,974]]} |
---|
| 1496 | """ |
---|
| 1497 | if not isinstance(maskstring,str): |
---|
| 1498 | asaplog.post() |
---|
[2611] | 1499 | asaplog.push("Mask expression should be a string.") |
---|
[2013] | 1500 | asaplog.post("ERROR") |
---|
| 1501 | |
---|
| 1502 | valid_ifs = self.getifnos() |
---|
| 1503 | frequnit = self.get_unit() |
---|
| 1504 | seldict = {} |
---|
[2015] | 1505 | if maskstring == "": |
---|
| 1506 | maskstring = str(valid_ifs)[1:-1] |
---|
[2611] | 1507 | ## split each selection "IF range[:CHAN range]" |
---|
[2013] | 1508 | sellist = maskstring.split(',') |
---|
| 1509 | for currselstr in sellist: |
---|
| 1510 | selset = currselstr.split(':') |
---|
| 1511 | # spw and mask string (may include ~, < or >) |
---|
[2349] | 1512 | spwmasklist = self._parse_selection(selset[0], typestr='integer', |
---|
[2611] | 1513 | minval=min(valid_ifs), |
---|
[2349] | 1514 | maxval=max(valid_ifs)) |
---|
[2013] | 1515 | for spwlist in spwmasklist: |
---|
| 1516 | selspws = [] |
---|
| 1517 | for ispw in range(spwlist[0],spwlist[1]+1): |
---|
| 1518 | # Put into the list only if ispw exists |
---|
| 1519 | if valid_ifs.count(ispw): |
---|
| 1520 | selspws.append(ispw) |
---|
| 1521 | del spwmasklist, spwlist |
---|
| 1522 | |
---|
| 1523 | # parse frequency mask list |
---|
| 1524 | if len(selset) > 1: |
---|
[2349] | 1525 | freqmasklist = self._parse_selection(selset[1], typestr='float', |
---|
| 1526 | offset=0.) |
---|
[2013] | 1527 | else: |
---|
| 1528 | # want to select the whole spectrum |
---|
| 1529 | freqmasklist = [None] |
---|
| 1530 | |
---|
| 1531 | ## define a dictionary of spw - masklist combination |
---|
| 1532 | for ispw in selspws: |
---|
| 1533 | #print "working on", ispw |
---|
| 1534 | spwstr = str(ispw) |
---|
| 1535 | if len(selspws) == 0: |
---|
| 1536 | # empty spw |
---|
| 1537 | continue |
---|
| 1538 | else: |
---|
| 1539 | ## want to get min and max of the spw and |
---|
| 1540 | ## offset to set for '<' and '>' |
---|
| 1541 | if frequnit == 'channel': |
---|
| 1542 | minfreq = 0 |
---|
| 1543 | maxfreq = self.nchan(ifno=ispw) |
---|
| 1544 | offset = 0.5 |
---|
| 1545 | else: |
---|
| 1546 | ## This is ugly part. need improvement |
---|
| 1547 | for ifrow in xrange(self.nrow()): |
---|
| 1548 | if self.getif(ifrow) == ispw: |
---|
| 1549 | #print "IF",ispw,"found in row =",ifrow |
---|
| 1550 | break |
---|
| 1551 | freqcoord = self.get_coordinate(ifrow) |
---|
| 1552 | freqs = self._getabcissa(ifrow) |
---|
| 1553 | minfreq = min(freqs) |
---|
| 1554 | maxfreq = max(freqs) |
---|
| 1555 | if len(freqs) == 1: |
---|
| 1556 | offset = 0.5 |
---|
| 1557 | elif frequnit.find('Hz') > 0: |
---|
[2349] | 1558 | offset = abs(freqcoord.to_frequency(1, |
---|
| 1559 | unit=frequnit) |
---|
| 1560 | -freqcoord.to_frequency(0, |
---|
| 1561 | unit=frequnit) |
---|
| 1562 | )*0.5 |
---|
[2013] | 1563 | elif frequnit.find('m/s') > 0: |
---|
[2349] | 1564 | offset = abs(freqcoord.to_velocity(1, |
---|
| 1565 | unit=frequnit) |
---|
| 1566 | -freqcoord.to_velocity(0, |
---|
| 1567 | unit=frequnit) |
---|
| 1568 | )*0.5 |
---|
[2013] | 1569 | else: |
---|
| 1570 | asaplog.post() |
---|
| 1571 | asaplog.push("Invalid frequency unit") |
---|
| 1572 | asaplog.post("ERROR") |
---|
| 1573 | del freqs, freqcoord, ifrow |
---|
| 1574 | for freq in freqmasklist: |
---|
| 1575 | selmask = freq or [minfreq, maxfreq] |
---|
| 1576 | if selmask[0] == None: |
---|
| 1577 | ## selection was "<freq[1]". |
---|
| 1578 | if selmask[1] < minfreq: |
---|
| 1579 | ## avoid adding region selection |
---|
| 1580 | selmask = None |
---|
| 1581 | else: |
---|
| 1582 | selmask = [minfreq,selmask[1]-offset] |
---|
| 1583 | elif selmask[1] == None: |
---|
| 1584 | ## selection was ">freq[0]" |
---|
| 1585 | if selmask[0] > maxfreq: |
---|
| 1586 | ## avoid adding region selection |
---|
| 1587 | selmask = None |
---|
| 1588 | else: |
---|
| 1589 | selmask = [selmask[0]+offset,maxfreq] |
---|
| 1590 | if selmask: |
---|
| 1591 | if not seldict.has_key(spwstr): |
---|
| 1592 | # new spw selection |
---|
| 1593 | seldict[spwstr] = [] |
---|
| 1594 | seldict[spwstr] += [selmask] |
---|
| 1595 | del minfreq,maxfreq,offset,freq,selmask |
---|
| 1596 | del spwstr |
---|
| 1597 | del freqmasklist |
---|
| 1598 | del valid_ifs |
---|
| 1599 | if len(seldict) == 0: |
---|
| 1600 | asaplog.post() |
---|
[2349] | 1601 | asaplog.push("No valid selection in the mask expression: " |
---|
| 1602 | +maskstring) |
---|
[2013] | 1603 | asaplog.post("WARN") |
---|
| 1604 | return None |
---|
| 1605 | msg = "Selected masklist:\n" |
---|
| 1606 | for sif, lmask in seldict.iteritems(): |
---|
| 1607 | msg += " IF"+sif+" - "+str(lmask)+"\n" |
---|
| 1608 | asaplog.push(msg) |
---|
| 1609 | return seldict |
---|
| 1610 | |
---|
[2611] | 1611 | @asaplog_post_dec |
---|
| 1612 | def parse_idx_selection(self, mode, selexpr): |
---|
| 1613 | """ |
---|
| 1614 | Parse CASA type mask selection syntax of SCANNO, IFNO, POLNO, |
---|
| 1615 | BEAMNO, and row number |
---|
| 1616 | |
---|
| 1617 | Parameters: |
---|
| 1618 | mode : which column to select. |
---|
| 1619 | ['scan',|'if'|'pol'|'beam'|'row'] |
---|
| 1620 | selexpr : A comma separated selection expression. |
---|
| 1621 | examples: |
---|
| 1622 | '' = all (returns []) |
---|
| 1623 | '<2,4~6,9' = indices less than 2, 4 to 6 and 9 |
---|
| 1624 | (returns [0,1,4,5,6,9]) |
---|
| 1625 | Returns: |
---|
| 1626 | A List of selected indices |
---|
| 1627 | """ |
---|
| 1628 | if selexpr == "": |
---|
| 1629 | return [] |
---|
| 1630 | valid_modes = {'s': 'scan', 'i': 'if', 'p': 'pol', |
---|
| 1631 | 'b': 'beam', 'r': 'row'} |
---|
| 1632 | smode = mode.lower()[0] |
---|
| 1633 | if not (smode in valid_modes.keys()): |
---|
| 1634 | msg = "Invalid mode '%s'. Valid modes are %s" %\ |
---|
| 1635 | (mode, str(valid_modes.values())) |
---|
| 1636 | asaplog.post() |
---|
| 1637 | asaplog.push(msg) |
---|
| 1638 | asaplog.post("ERROR") |
---|
| 1639 | mode = valid_modes[smode] |
---|
| 1640 | minidx = None |
---|
| 1641 | maxidx = None |
---|
| 1642 | if smode == 'r': |
---|
| 1643 | minidx = 0 |
---|
| 1644 | maxidx = self.nrow() |
---|
| 1645 | else: |
---|
| 1646 | idx = getattr(self,"get"+mode+"nos")() |
---|
| 1647 | minidx = min(idx) |
---|
| 1648 | maxidx = max(idx) |
---|
| 1649 | del idx |
---|
| 1650 | sellist = selexpr.split(',') |
---|
| 1651 | idxlist = [] |
---|
| 1652 | for currselstr in sellist: |
---|
| 1653 | # single range (may include ~, < or >) |
---|
| 1654 | currlist = self._parse_selection(currselstr, typestr='integer', |
---|
| 1655 | minval=minidx,maxval=maxidx) |
---|
| 1656 | for thelist in currlist: |
---|
| 1657 | idxlist += range(thelist[0],thelist[1]+1) |
---|
| 1658 | msg = "Selected %s: %s" % (mode.upper()+"NO", str(idxlist)) |
---|
| 1659 | asaplog.push(msg) |
---|
| 1660 | return idxlist |
---|
| 1661 | |
---|
[2349] | 1662 | def _parse_selection(self, selstr, typestr='float', offset=0., |
---|
[2351] | 1663 | minval=None, maxval=None): |
---|
[2013] | 1664 | """ |
---|
| 1665 | Parameters: |
---|
| 1666 | selstr : The Selection string, e.g., '<3;5~7;100~103;9' |
---|
| 1667 | typestr : The type of the values in returned list |
---|
| 1668 | ('integer' or 'float') |
---|
| 1669 | offset : The offset value to subtract from or add to |
---|
| 1670 | the boundary value if the selection string |
---|
[2611] | 1671 | includes '<' or '>' [Valid only for typestr='float'] |
---|
[2013] | 1672 | minval, maxval : The minimum/maximum values to set if the |
---|
| 1673 | selection string includes '<' or '>'. |
---|
| 1674 | The list element is filled with None by default. |
---|
| 1675 | Returns: |
---|
| 1676 | A list of min/max pair of selections. |
---|
| 1677 | Example: |
---|
[2611] | 1678 | _parse_selection('<3;5~7;9',typestr='int',minval=0) |
---|
| 1679 | --> returns [[0,2],[5,7],[9,9]] |
---|
| 1680 | _parse_selection('<3;5~7;9',typestr='float',offset=0.5,minval=0) |
---|
| 1681 | --> returns [[0.,2.5],[5.0,7.0],[9.,9.]] |
---|
[2013] | 1682 | """ |
---|
| 1683 | selgroups = selstr.split(';') |
---|
| 1684 | sellists = [] |
---|
| 1685 | if typestr.lower().startswith('int'): |
---|
| 1686 | formatfunc = int |
---|
[2611] | 1687 | offset = 1 |
---|
[2013] | 1688 | else: |
---|
| 1689 | formatfunc = float |
---|
| 1690 | |
---|
| 1691 | for currsel in selgroups: |
---|
| 1692 | if currsel.find('~') > 0: |
---|
[2611] | 1693 | # val0 <= x <= val1 |
---|
[2013] | 1694 | minsel = formatfunc(currsel.split('~')[0].strip()) |
---|
| 1695 | maxsel = formatfunc(currsel.split('~')[1].strip()) |
---|
[2611] | 1696 | elif currsel.strip().find('<=') > -1: |
---|
| 1697 | bound = currsel.split('<=') |
---|
| 1698 | try: # try "x <= val" |
---|
| 1699 | minsel = minval |
---|
| 1700 | maxsel = formatfunc(bound[1].strip()) |
---|
| 1701 | except ValueError: # now "val <= x" |
---|
| 1702 | minsel = formatfunc(bound[0].strip()) |
---|
| 1703 | maxsel = maxval |
---|
| 1704 | elif currsel.strip().find('>=') > -1: |
---|
| 1705 | bound = currsel.split('>=') |
---|
| 1706 | try: # try "x >= val" |
---|
| 1707 | minsel = formatfunc(bound[1].strip()) |
---|
| 1708 | maxsel = maxval |
---|
| 1709 | except ValueError: # now "val >= x" |
---|
| 1710 | minsel = minval |
---|
| 1711 | maxsel = formatfunc(bound[0].strip()) |
---|
| 1712 | elif currsel.strip().find('<') > -1: |
---|
| 1713 | bound = currsel.split('<') |
---|
| 1714 | try: # try "x < val" |
---|
| 1715 | minsel = minval |
---|
| 1716 | maxsel = formatfunc(bound[1].strip()) \ |
---|
| 1717 | - formatfunc(offset) |
---|
| 1718 | except ValueError: # now "val < x" |
---|
| 1719 | minsel = formatfunc(bound[0].strip()) \ |
---|
[2013] | 1720 | + formatfunc(offset) |
---|
[2611] | 1721 | maxsel = maxval |
---|
| 1722 | elif currsel.strip().find('>') > -1: |
---|
| 1723 | bound = currsel.split('>') |
---|
| 1724 | try: # try "x > val" |
---|
| 1725 | minsel = formatfunc(bound[1].strip()) \ |
---|
| 1726 | + formatfunc(offset) |
---|
| 1727 | maxsel = maxval |
---|
| 1728 | except ValueError: # now "val > x" |
---|
| 1729 | minsel = minval |
---|
| 1730 | maxsel = formatfunc(bound[0].strip()) \ |
---|
| 1731 | - formatfunc(offset) |
---|
[2013] | 1732 | else: |
---|
| 1733 | minsel = formatfunc(currsel) |
---|
| 1734 | maxsel = formatfunc(currsel) |
---|
| 1735 | sellists.append([minsel,maxsel]) |
---|
| 1736 | return sellists |
---|
| 1737 | |
---|
[1819] | 1738 | # def get_restfreqs(self): |
---|
| 1739 | # """ |
---|
| 1740 | # Get the restfrequency(s) stored in this scantable. |
---|
| 1741 | # The return value(s) are always of unit 'Hz' |
---|
| 1742 | # Parameters: |
---|
| 1743 | # none |
---|
| 1744 | # Returns: |
---|
| 1745 | # a list of doubles |
---|
| 1746 | # """ |
---|
| 1747 | # return list(self._getrestfreqs()) |
---|
| 1748 | |
---|
| 1749 | def get_restfreqs(self, ids=None): |
---|
[1846] | 1750 | """\ |
---|
[256] | 1751 | Get the restfrequency(s) stored in this scantable. |
---|
| 1752 | The return value(s) are always of unit 'Hz' |
---|
[1846] | 1753 | |
---|
[256] | 1754 | Parameters: |
---|
[1846] | 1755 | |
---|
[1819] | 1756 | ids: (optional) a list of MOLECULE_ID for that restfrequency(s) to |
---|
| 1757 | be retrieved |
---|
[1846] | 1758 | |
---|
[256] | 1759 | Returns: |
---|
[1846] | 1760 | |
---|
[1819] | 1761 | dictionary containing ids and a list of doubles for each id |
---|
[1846] | 1762 | |
---|
[256] | 1763 | """ |
---|
[1819] | 1764 | if ids is None: |
---|
[2349] | 1765 | rfreqs = {} |
---|
[1819] | 1766 | idlist = self.getmolnos() |
---|
| 1767 | for i in idlist: |
---|
[2349] | 1768 | rfreqs[i] = list(self._getrestfreqs(i)) |
---|
[1819] | 1769 | return rfreqs |
---|
| 1770 | else: |
---|
[2349] | 1771 | if type(ids) == list or type(ids) == tuple: |
---|
| 1772 | rfreqs = {} |
---|
[1819] | 1773 | for i in ids: |
---|
[2349] | 1774 | rfreqs[i] = list(self._getrestfreqs(i)) |
---|
[1819] | 1775 | return rfreqs |
---|
| 1776 | else: |
---|
| 1777 | return list(self._getrestfreqs(ids)) |
---|
[102] | 1778 | |
---|
[2349] | 1779 | @asaplog_post_dec |
---|
[931] | 1780 | def set_restfreqs(self, freqs=None, unit='Hz'): |
---|
[1846] | 1781 | """\ |
---|
[931] | 1782 | Set or replace the restfrequency specified and |
---|
[1938] | 1783 | if the 'freqs' argument holds a scalar, |
---|
[931] | 1784 | then that rest frequency will be applied to all the selected |
---|
| 1785 | data. If the 'freqs' argument holds |
---|
| 1786 | a vector, then it MUST be of equal or smaller length than |
---|
| 1787 | the number of IFs (and the available restfrequencies will be |
---|
| 1788 | replaced by this vector). In this case, *all* data have |
---|
| 1789 | the restfrequency set per IF according |
---|
| 1790 | to the corresponding value you give in the 'freqs' vector. |
---|
[1118] | 1791 | E.g. 'freqs=[1e9, 2e9]' would mean IF 0 gets restfreq 1e9 and |
---|
[931] | 1792 | IF 1 gets restfreq 2e9. |
---|
[1846] | 1793 | |
---|
[1395] | 1794 | You can also specify the frequencies via a linecatalog. |
---|
[1153] | 1795 | |
---|
[931] | 1796 | Parameters: |
---|
[1846] | 1797 | |
---|
[931] | 1798 | freqs: list of rest frequency values or string idenitfiers |
---|
[1855] | 1799 | |
---|
[931] | 1800 | unit: unit for rest frequency (default 'Hz') |
---|
[402] | 1801 | |
---|
[1846] | 1802 | |
---|
| 1803 | Example:: |
---|
| 1804 | |
---|
[1819] | 1805 | # set the given restfrequency for the all currently selected IFs |
---|
[931] | 1806 | scan.set_restfreqs(freqs=1.4e9) |
---|
[1845] | 1807 | # set restfrequencies for the n IFs (n > 1) in the order of the |
---|
| 1808 | # list, i.e |
---|
| 1809 | # IF0 -> 1.4e9, IF1 -> 1.41e9, IF3 -> 1.42e9 |
---|
| 1810 | # len(list_of_restfreqs) == nIF |
---|
| 1811 | # for nIF == 1 the following will set multiple restfrequency for |
---|
| 1812 | # that IF |
---|
[1819] | 1813 | scan.set_restfreqs(freqs=[1.4e9, 1.41e9, 1.42e9]) |
---|
[1845] | 1814 | # set multiple restfrequencies per IF. as a list of lists where |
---|
| 1815 | # the outer list has nIF elements, the inner s arbitrary |
---|
| 1816 | scan.set_restfreqs(freqs=[[1.4e9, 1.41e9], [1.67e9]]) |
---|
[391] | 1817 | |
---|
[1846] | 1818 | *Note*: |
---|
[1845] | 1819 | |
---|
[931] | 1820 | To do more sophisticate Restfrequency setting, e.g. on a |
---|
| 1821 | source and IF basis, use scantable.set_selection() before using |
---|
[1846] | 1822 | this function:: |
---|
[931] | 1823 | |
---|
[1846] | 1824 | # provided your scantable is called scan |
---|
| 1825 | selection = selector() |
---|
[2431] | 1826 | selection.set_name('ORION*') |
---|
[1846] | 1827 | selection.set_ifs([1]) |
---|
| 1828 | scan.set_selection(selection) |
---|
| 1829 | scan.set_restfreqs(freqs=86.6e9) |
---|
| 1830 | |
---|
[931] | 1831 | """ |
---|
| 1832 | varlist = vars() |
---|
[1157] | 1833 | from asap import linecatalog |
---|
| 1834 | # simple value |
---|
[1118] | 1835 | if isinstance(freqs, int) or isinstance(freqs, float): |
---|
[1845] | 1836 | self._setrestfreqs([freqs], [""], unit) |
---|
[1157] | 1837 | # list of values |
---|
[1118] | 1838 | elif isinstance(freqs, list) or isinstance(freqs, tuple): |
---|
[1157] | 1839 | # list values are scalars |
---|
[1118] | 1840 | if isinstance(freqs[-1], int) or isinstance(freqs[-1], float): |
---|
[1845] | 1841 | if len(freqs) == 1: |
---|
| 1842 | self._setrestfreqs(freqs, [""], unit) |
---|
| 1843 | else: |
---|
| 1844 | # allow the 'old' mode of setting mulitple IFs |
---|
| 1845 | savesel = self._getselection() |
---|
[2599] | 1846 | sel = self.get_selection() |
---|
[1845] | 1847 | iflist = self.getifnos() |
---|
| 1848 | if len(freqs)>len(iflist): |
---|
| 1849 | raise ValueError("number of elements in list of list " |
---|
| 1850 | "exeeds the current IF selections") |
---|
| 1851 | iflist = self.getifnos() |
---|
| 1852 | for i, fval in enumerate(freqs): |
---|
| 1853 | sel.set_ifs(iflist[i]) |
---|
| 1854 | self._setselection(sel) |
---|
| 1855 | self._setrestfreqs([fval], [""], unit) |
---|
| 1856 | self._setselection(savesel) |
---|
| 1857 | |
---|
| 1858 | # list values are dict, {'value'=, 'name'=) |
---|
[1157] | 1859 | elif isinstance(freqs[-1], dict): |
---|
[1845] | 1860 | values = [] |
---|
| 1861 | names = [] |
---|
| 1862 | for d in freqs: |
---|
| 1863 | values.append(d["value"]) |
---|
| 1864 | names.append(d["name"]) |
---|
| 1865 | self._setrestfreqs(values, names, unit) |
---|
[1819] | 1866 | elif isinstance(freqs[-1], list) or isinstance(freqs[-1], tuple): |
---|
[1157] | 1867 | savesel = self._getselection() |
---|
[2599] | 1868 | sel = self.get_selection() |
---|
[1322] | 1869 | iflist = self.getifnos() |
---|
[1819] | 1870 | if len(freqs)>len(iflist): |
---|
[1845] | 1871 | raise ValueError("number of elements in list of list exeeds" |
---|
| 1872 | " the current IF selections") |
---|
| 1873 | for i, fval in enumerate(freqs): |
---|
[1322] | 1874 | sel.set_ifs(iflist[i]) |
---|
[1259] | 1875 | self._setselection(sel) |
---|
[1845] | 1876 | self._setrestfreqs(fval, [""], unit) |
---|
[1157] | 1877 | self._setselection(savesel) |
---|
| 1878 | # freqs are to be taken from a linecatalog |
---|
[1153] | 1879 | elif isinstance(freqs, linecatalog): |
---|
| 1880 | savesel = self._getselection() |
---|
[2599] | 1881 | sel = self.get_selection() |
---|
[1153] | 1882 | for i in xrange(freqs.nrow()): |
---|
[1322] | 1883 | sel.set_ifs(iflist[i]) |
---|
[1153] | 1884 | self._setselection(sel) |
---|
[1845] | 1885 | self._setrestfreqs([freqs.get_frequency(i)], |
---|
| 1886 | [freqs.get_name(i)], "MHz") |
---|
[1153] | 1887 | # ensure that we are not iterating past nIF |
---|
| 1888 | if i == self.nif()-1: break |
---|
| 1889 | self._setselection(savesel) |
---|
[931] | 1890 | else: |
---|
| 1891 | return |
---|
| 1892 | self._add_history("set_restfreqs", varlist) |
---|
| 1893 | |
---|
[2349] | 1894 | @asaplog_post_dec |
---|
[1360] | 1895 | def shift_refpix(self, delta): |
---|
[1846] | 1896 | """\ |
---|
[1589] | 1897 | Shift the reference pixel of the Spectra Coordinate by an |
---|
| 1898 | integer amount. |
---|
[1846] | 1899 | |
---|
[1589] | 1900 | Parameters: |
---|
[1846] | 1901 | |
---|
[1589] | 1902 | delta: the amount to shift by |
---|
[1846] | 1903 | |
---|
| 1904 | *Note*: |
---|
| 1905 | |
---|
[1589] | 1906 | Be careful using this with broadband data. |
---|
[1846] | 1907 | |
---|
[1360] | 1908 | """ |
---|
[2349] | 1909 | varlist = vars() |
---|
[1731] | 1910 | Scantable.shift_refpix(self, delta) |
---|
[2349] | 1911 | s._add_history("shift_refpix", varlist) |
---|
[931] | 1912 | |
---|
[1862] | 1913 | @asaplog_post_dec |
---|
[1259] | 1914 | def history(self, filename=None): |
---|
[1846] | 1915 | """\ |
---|
[1259] | 1916 | Print the history. Optionally to a file. |
---|
[1846] | 1917 | |
---|
[1348] | 1918 | Parameters: |
---|
[1846] | 1919 | |
---|
[1928] | 1920 | filename: The name of the file to save the history to. |
---|
[1846] | 1921 | |
---|
[1259] | 1922 | """ |
---|
[484] | 1923 | hist = list(self._gethistory()) |
---|
[794] | 1924 | out = "-"*80 |
---|
[484] | 1925 | for h in hist: |
---|
[489] | 1926 | if h.startswith("---"): |
---|
[1857] | 1927 | out = "\n".join([out, h]) |
---|
[489] | 1928 | else: |
---|
| 1929 | items = h.split("##") |
---|
| 1930 | date = items[0] |
---|
| 1931 | func = items[1] |
---|
| 1932 | items = items[2:] |
---|
[794] | 1933 | out += "\n"+date+"\n" |
---|
| 1934 | out += "Function: %s\n Parameters:" % (func) |
---|
[489] | 1935 | for i in items: |
---|
[1938] | 1936 | if i == '': |
---|
| 1937 | continue |
---|
[489] | 1938 | s = i.split("=") |
---|
[1118] | 1939 | out += "\n %s = %s" % (s[0], s[1]) |
---|
[1857] | 1940 | out = "\n".join([out, "-"*80]) |
---|
[1259] | 1941 | if filename is not None: |
---|
| 1942 | if filename is "": |
---|
| 1943 | filename = 'scantable_history.txt' |
---|
| 1944 | import os |
---|
| 1945 | filename = os.path.expandvars(os.path.expanduser(filename)) |
---|
| 1946 | if not os.path.isdir(filename): |
---|
| 1947 | data = open(filename, 'w') |
---|
| 1948 | data.write(out) |
---|
| 1949 | data.close() |
---|
| 1950 | else: |
---|
| 1951 | msg = "Illegal file name '%s'." % (filename) |
---|
[1859] | 1952 | raise IOError(msg) |
---|
| 1953 | return page(out) |
---|
[2349] | 1954 | |
---|
[513] | 1955 | # |
---|
| 1956 | # Maths business |
---|
| 1957 | # |
---|
[1862] | 1958 | @asaplog_post_dec |
---|
[931] | 1959 | def average_time(self, mask=None, scanav=False, weight='tint', align=False): |
---|
[1846] | 1960 | """\ |
---|
[2349] | 1961 | Return the (time) weighted average of a scan. Scans will be averaged |
---|
| 1962 | only if the source direction (RA/DEC) is within 1' otherwise |
---|
[1846] | 1963 | |
---|
| 1964 | *Note*: |
---|
| 1965 | |
---|
[1070] | 1966 | in channels only - align if necessary |
---|
[1846] | 1967 | |
---|
[513] | 1968 | Parameters: |
---|
[1846] | 1969 | |
---|
[513] | 1970 | mask: an optional mask (only used for 'var' and 'tsys' |
---|
| 1971 | weighting) |
---|
[1855] | 1972 | |
---|
[558] | 1973 | scanav: True averages each scan separately |
---|
| 1974 | False (default) averages all scans together, |
---|
[1855] | 1975 | |
---|
[1099] | 1976 | weight: Weighting scheme. |
---|
| 1977 | 'none' (mean no weight) |
---|
| 1978 | 'var' (1/var(spec) weighted) |
---|
| 1979 | 'tsys' (1/Tsys**2 weighted) |
---|
| 1980 | 'tint' (integration time weighted) |
---|
| 1981 | 'tintsys' (Tint/Tsys**2) |
---|
| 1982 | 'median' ( median averaging) |
---|
[535] | 1983 | The default is 'tint' |
---|
[1855] | 1984 | |
---|
[931] | 1985 | align: align the spectra in velocity before averaging. It takes |
---|
| 1986 | the time of the first spectrum as reference time. |
---|
[1846] | 1987 | |
---|
| 1988 | Example:: |
---|
| 1989 | |
---|
[513] | 1990 | # time average the scantable without using a mask |
---|
[710] | 1991 | newscan = scan.average_time() |
---|
[1846] | 1992 | |
---|
[513] | 1993 | """ |
---|
| 1994 | varlist = vars() |
---|
[1593] | 1995 | weight = weight or 'TINT' |
---|
| 1996 | mask = mask or () |
---|
| 1997 | scanav = (scanav and 'SCAN') or 'NONE' |
---|
[1118] | 1998 | scan = (self, ) |
---|
[1859] | 1999 | |
---|
| 2000 | if align: |
---|
| 2001 | scan = (self.freq_align(insitu=False), ) |
---|
| 2002 | s = None |
---|
| 2003 | if weight.upper() == 'MEDIAN': |
---|
| 2004 | s = scantable(self._math._averagechannel(scan[0], 'MEDIAN', |
---|
| 2005 | scanav)) |
---|
| 2006 | else: |
---|
| 2007 | s = scantable(self._math._average(scan, mask, weight.upper(), |
---|
| 2008 | scanav)) |
---|
[1099] | 2009 | s._add_history("average_time", varlist) |
---|
[513] | 2010 | return s |
---|
[710] | 2011 | |
---|
[1862] | 2012 | @asaplog_post_dec |
---|
[876] | 2013 | def convert_flux(self, jyperk=None, eta=None, d=None, insitu=None): |
---|
[1846] | 2014 | """\ |
---|
[513] | 2015 | Return a scan where all spectra are converted to either |
---|
| 2016 | Jansky or Kelvin depending upon the flux units of the scan table. |
---|
| 2017 | By default the function tries to look the values up internally. |
---|
| 2018 | If it can't find them (or if you want to over-ride), you must |
---|
| 2019 | specify EITHER jyperk OR eta (and D which it will try to look up |
---|
| 2020 | also if you don't set it). jyperk takes precedence if you set both. |
---|
[1846] | 2021 | |
---|
[513] | 2022 | Parameters: |
---|
[1846] | 2023 | |
---|
[513] | 2024 | jyperk: the Jy / K conversion factor |
---|
[1855] | 2025 | |
---|
[513] | 2026 | eta: the aperture efficiency |
---|
[1855] | 2027 | |
---|
[1928] | 2028 | d: the geometric diameter (metres) |
---|
[1855] | 2029 | |
---|
[513] | 2030 | insitu: if False a new scantable is returned. |
---|
| 2031 | Otherwise, the scaling is done in-situ |
---|
| 2032 | The default is taken from .asaprc (False) |
---|
[1846] | 2033 | |
---|
[513] | 2034 | """ |
---|
| 2035 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 2036 | self._math._setinsitu(insitu) |
---|
[513] | 2037 | varlist = vars() |
---|
[1593] | 2038 | jyperk = jyperk or -1.0 |
---|
| 2039 | d = d or -1.0 |
---|
| 2040 | eta = eta or -1.0 |
---|
[876] | 2041 | s = scantable(self._math._convertflux(self, d, eta, jyperk)) |
---|
| 2042 | s._add_history("convert_flux", varlist) |
---|
| 2043 | if insitu: self._assign(s) |
---|
| 2044 | else: return s |
---|
[513] | 2045 | |
---|
[1862] | 2046 | @asaplog_post_dec |
---|
[876] | 2047 | def gain_el(self, poly=None, filename="", method="linear", insitu=None): |
---|
[1846] | 2048 | """\ |
---|
[513] | 2049 | Return a scan after applying a gain-elevation correction. |
---|
| 2050 | The correction can be made via either a polynomial or a |
---|
| 2051 | table-based interpolation (and extrapolation if necessary). |
---|
| 2052 | You specify polynomial coefficients, an ascii table or neither. |
---|
| 2053 | If you specify neither, then a polynomial correction will be made |
---|
| 2054 | with built in coefficients known for certain telescopes (an error |
---|
| 2055 | will occur if the instrument is not known). |
---|
| 2056 | The data and Tsys are *divided* by the scaling factors. |
---|
[1846] | 2057 | |
---|
[513] | 2058 | Parameters: |
---|
[1846] | 2059 | |
---|
[513] | 2060 | poly: Polynomial coefficients (default None) to compute a |
---|
| 2061 | gain-elevation correction as a function of |
---|
| 2062 | elevation (in degrees). |
---|
[1855] | 2063 | |
---|
[513] | 2064 | filename: The name of an ascii file holding correction factors. |
---|
| 2065 | The first row of the ascii file must give the column |
---|
| 2066 | names and these MUST include columns |
---|
[2431] | 2067 | 'ELEVATION' (degrees) and 'FACTOR' (multiply data |
---|
[513] | 2068 | by this) somewhere. |
---|
| 2069 | The second row must give the data type of the |
---|
| 2070 | column. Use 'R' for Real and 'I' for Integer. |
---|
| 2071 | An example file would be |
---|
| 2072 | (actual factors are arbitrary) : |
---|
| 2073 | |
---|
| 2074 | TIME ELEVATION FACTOR |
---|
| 2075 | R R R |
---|
| 2076 | 0.1 0 0.8 |
---|
| 2077 | 0.2 20 0.85 |
---|
| 2078 | 0.3 40 0.9 |
---|
| 2079 | 0.4 60 0.85 |
---|
| 2080 | 0.5 80 0.8 |
---|
| 2081 | 0.6 90 0.75 |
---|
[1855] | 2082 | |
---|
[513] | 2083 | method: Interpolation method when correcting from a table. |
---|
[2431] | 2084 | Values are 'nearest', 'linear' (default), 'cubic' |
---|
| 2085 | and 'spline' |
---|
[1855] | 2086 | |
---|
[513] | 2087 | insitu: if False a new scantable is returned. |
---|
| 2088 | Otherwise, the scaling is done in-situ |
---|
| 2089 | The default is taken from .asaprc (False) |
---|
[1846] | 2090 | |
---|
[513] | 2091 | """ |
---|
| 2092 | |
---|
| 2093 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 2094 | self._math._setinsitu(insitu) |
---|
[513] | 2095 | varlist = vars() |
---|
[1593] | 2096 | poly = poly or () |
---|
[513] | 2097 | from os.path import expandvars |
---|
| 2098 | filename = expandvars(filename) |
---|
[876] | 2099 | s = scantable(self._math._gainel(self, poly, filename, method)) |
---|
| 2100 | s._add_history("gain_el", varlist) |
---|
[1593] | 2101 | if insitu: |
---|
| 2102 | self._assign(s) |
---|
| 2103 | else: |
---|
| 2104 | return s |
---|
[710] | 2105 | |
---|
[1862] | 2106 | @asaplog_post_dec |
---|
[931] | 2107 | def freq_align(self, reftime=None, method='cubic', insitu=None): |
---|
[1846] | 2108 | """\ |
---|
[513] | 2109 | Return a scan where all rows have been aligned in frequency/velocity. |
---|
| 2110 | The alignment frequency frame (e.g. LSRK) is that set by function |
---|
| 2111 | set_freqframe. |
---|
[1846] | 2112 | |
---|
[513] | 2113 | Parameters: |
---|
[1855] | 2114 | |
---|
[513] | 2115 | reftime: reference time to align at. By default, the time of |
---|
| 2116 | the first row of data is used. |
---|
[1855] | 2117 | |
---|
[513] | 2118 | method: Interpolation method for regridding the spectra. |
---|
[2431] | 2119 | Choose from 'nearest', 'linear', 'cubic' (default) |
---|
| 2120 | and 'spline' |
---|
[1855] | 2121 | |
---|
[513] | 2122 | insitu: if False a new scantable is returned. |
---|
| 2123 | Otherwise, the scaling is done in-situ |
---|
| 2124 | The default is taken from .asaprc (False) |
---|
[1846] | 2125 | |
---|
[513] | 2126 | """ |
---|
[931] | 2127 | if insitu is None: insitu = rcParams["insitu"] |
---|
[2429] | 2128 | oldInsitu = self._math._insitu() |
---|
[876] | 2129 | self._math._setinsitu(insitu) |
---|
[513] | 2130 | varlist = vars() |
---|
[1593] | 2131 | reftime = reftime or "" |
---|
[931] | 2132 | s = scantable(self._math._freq_align(self, reftime, method)) |
---|
[876] | 2133 | s._add_history("freq_align", varlist) |
---|
[2429] | 2134 | self._math._setinsitu(oldInsitu) |
---|
[2349] | 2135 | if insitu: |
---|
| 2136 | self._assign(s) |
---|
| 2137 | else: |
---|
| 2138 | return s |
---|
[513] | 2139 | |
---|
[1862] | 2140 | @asaplog_post_dec |
---|
[1725] | 2141 | def opacity(self, tau=None, insitu=None): |
---|
[1846] | 2142 | """\ |
---|
[513] | 2143 | Apply an opacity correction. The data |
---|
| 2144 | and Tsys are multiplied by the correction factor. |
---|
[1846] | 2145 | |
---|
[513] | 2146 | Parameters: |
---|
[1855] | 2147 | |
---|
[1689] | 2148 | tau: (list of) opacity from which the correction factor is |
---|
[513] | 2149 | exp(tau*ZD) |
---|
[1689] | 2150 | where ZD is the zenith-distance. |
---|
| 2151 | If a list is provided, it has to be of length nIF, |
---|
| 2152 | nIF*nPol or 1 and in order of IF/POL, e.g. |
---|
| 2153 | [opif0pol0, opif0pol1, opif1pol0 ...] |
---|
[1725] | 2154 | if tau is `None` the opacities are determined from a |
---|
| 2155 | model. |
---|
[1855] | 2156 | |
---|
[513] | 2157 | insitu: if False a new scantable is returned. |
---|
| 2158 | Otherwise, the scaling is done in-situ |
---|
| 2159 | The default is taken from .asaprc (False) |
---|
[1846] | 2160 | |
---|
[513] | 2161 | """ |
---|
[2349] | 2162 | if insitu is None: |
---|
| 2163 | insitu = rcParams['insitu'] |
---|
[876] | 2164 | self._math._setinsitu(insitu) |
---|
[513] | 2165 | varlist = vars() |
---|
[1689] | 2166 | if not hasattr(tau, "__len__"): |
---|
| 2167 | tau = [tau] |
---|
[876] | 2168 | s = scantable(self._math._opacity(self, tau)) |
---|
| 2169 | s._add_history("opacity", varlist) |
---|
[2349] | 2170 | if insitu: |
---|
| 2171 | self._assign(s) |
---|
| 2172 | else: |
---|
| 2173 | return s |
---|
[513] | 2174 | |
---|
[1862] | 2175 | @asaplog_post_dec |
---|
[513] | 2176 | def bin(self, width=5, insitu=None): |
---|
[1846] | 2177 | """\ |
---|
[513] | 2178 | Return a scan where all spectra have been binned up. |
---|
[1846] | 2179 | |
---|
[1348] | 2180 | Parameters: |
---|
[1846] | 2181 | |
---|
[513] | 2182 | width: The bin width (default=5) in pixels |
---|
[1855] | 2183 | |
---|
[513] | 2184 | insitu: if False a new scantable is returned. |
---|
| 2185 | Otherwise, the scaling is done in-situ |
---|
| 2186 | The default is taken from .asaprc (False) |
---|
[1846] | 2187 | |
---|
[513] | 2188 | """ |
---|
[2349] | 2189 | if insitu is None: |
---|
| 2190 | insitu = rcParams['insitu'] |
---|
[876] | 2191 | self._math._setinsitu(insitu) |
---|
[513] | 2192 | varlist = vars() |
---|
[876] | 2193 | s = scantable(self._math._bin(self, width)) |
---|
[1118] | 2194 | s._add_history("bin", varlist) |
---|
[1589] | 2195 | if insitu: |
---|
| 2196 | self._assign(s) |
---|
| 2197 | else: |
---|
| 2198 | return s |
---|
[513] | 2199 | |
---|
[1862] | 2200 | @asaplog_post_dec |
---|
[2672] | 2201 | def reshape(self, first, last, insitu=None): |
---|
| 2202 | """Resize the band by providing first and last channel. |
---|
| 2203 | This will cut off all channels outside [first, last]. |
---|
| 2204 | """ |
---|
| 2205 | if insitu is None: |
---|
| 2206 | insitu = rcParams['insitu'] |
---|
| 2207 | varlist = vars() |
---|
| 2208 | if last < 0: |
---|
| 2209 | last = self.nchan()-1 + last |
---|
| 2210 | s = None |
---|
| 2211 | if insitu: |
---|
| 2212 | s = self |
---|
| 2213 | else: |
---|
| 2214 | s = self.copy() |
---|
| 2215 | s._reshape(first,last) |
---|
| 2216 | s._add_history("reshape", varlist) |
---|
| 2217 | if not insitu: |
---|
| 2218 | return s |
---|
| 2219 | |
---|
| 2220 | @asaplog_post_dec |
---|
[513] | 2221 | def resample(self, width=5, method='cubic', insitu=None): |
---|
[1846] | 2222 | """\ |
---|
[1348] | 2223 | Return a scan where all spectra have been binned up. |
---|
[1573] | 2224 | |
---|
[1348] | 2225 | Parameters: |
---|
[1846] | 2226 | |
---|
[513] | 2227 | width: The bin width (default=5) in pixels |
---|
[1855] | 2228 | |
---|
[513] | 2229 | method: Interpolation method when correcting from a table. |
---|
[2431] | 2230 | Values are 'nearest', 'linear', 'cubic' (default) |
---|
| 2231 | and 'spline' |
---|
[1855] | 2232 | |
---|
[513] | 2233 | insitu: if False a new scantable is returned. |
---|
| 2234 | Otherwise, the scaling is done in-situ |
---|
| 2235 | The default is taken from .asaprc (False) |
---|
[1846] | 2236 | |
---|
[513] | 2237 | """ |
---|
[2349] | 2238 | if insitu is None: |
---|
| 2239 | insitu = rcParams['insitu'] |
---|
[876] | 2240 | self._math._setinsitu(insitu) |
---|
[513] | 2241 | varlist = vars() |
---|
[876] | 2242 | s = scantable(self._math._resample(self, method, width)) |
---|
[1118] | 2243 | s._add_history("resample", varlist) |
---|
[2349] | 2244 | if insitu: |
---|
| 2245 | self._assign(s) |
---|
| 2246 | else: |
---|
| 2247 | return s |
---|
[513] | 2248 | |
---|
[1862] | 2249 | @asaplog_post_dec |
---|
[946] | 2250 | def average_pol(self, mask=None, weight='none'): |
---|
[1846] | 2251 | """\ |
---|
[946] | 2252 | Average the Polarisations together. |
---|
[1846] | 2253 | |
---|
[946] | 2254 | Parameters: |
---|
[1846] | 2255 | |
---|
[946] | 2256 | mask: An optional mask defining the region, where the |
---|
| 2257 | averaging will be applied. The output will have all |
---|
| 2258 | specified points masked. |
---|
[1855] | 2259 | |
---|
[946] | 2260 | weight: Weighting scheme. 'none' (default), 'var' (1/var(spec) |
---|
| 2261 | weighted), or 'tsys' (1/Tsys**2 weighted) |
---|
[1846] | 2262 | |
---|
[946] | 2263 | """ |
---|
| 2264 | varlist = vars() |
---|
[1593] | 2265 | mask = mask or () |
---|
[1010] | 2266 | s = scantable(self._math._averagepol(self, mask, weight.upper())) |
---|
[1118] | 2267 | s._add_history("average_pol", varlist) |
---|
[992] | 2268 | return s |
---|
[513] | 2269 | |
---|
[1862] | 2270 | @asaplog_post_dec |
---|
[1145] | 2271 | def average_beam(self, mask=None, weight='none'): |
---|
[1846] | 2272 | """\ |
---|
[1145] | 2273 | Average the Beams together. |
---|
[1846] | 2274 | |
---|
[1145] | 2275 | Parameters: |
---|
| 2276 | mask: An optional mask defining the region, where the |
---|
| 2277 | averaging will be applied. The output will have all |
---|
| 2278 | specified points masked. |
---|
[1855] | 2279 | |
---|
[1145] | 2280 | weight: Weighting scheme. 'none' (default), 'var' (1/var(spec) |
---|
| 2281 | weighted), or 'tsys' (1/Tsys**2 weighted) |
---|
[1846] | 2282 | |
---|
[1145] | 2283 | """ |
---|
| 2284 | varlist = vars() |
---|
[1593] | 2285 | mask = mask or () |
---|
[1145] | 2286 | s = scantable(self._math._averagebeams(self, mask, weight.upper())) |
---|
| 2287 | s._add_history("average_beam", varlist) |
---|
| 2288 | return s |
---|
| 2289 | |
---|
[1586] | 2290 | def parallactify(self, pflag): |
---|
[1846] | 2291 | """\ |
---|
[1843] | 2292 | Set a flag to indicate whether this data should be treated as having |
---|
[1617] | 2293 | been 'parallactified' (total phase == 0.0) |
---|
[1846] | 2294 | |
---|
[1617] | 2295 | Parameters: |
---|
[1855] | 2296 | |
---|
[1843] | 2297 | pflag: Bool indicating whether to turn this on (True) or |
---|
[1617] | 2298 | off (False) |
---|
[1846] | 2299 | |
---|
[1617] | 2300 | """ |
---|
[1586] | 2301 | varlist = vars() |
---|
| 2302 | self._parallactify(pflag) |
---|
| 2303 | self._add_history("parallactify", varlist) |
---|
| 2304 | |
---|
[1862] | 2305 | @asaplog_post_dec |
---|
[992] | 2306 | def convert_pol(self, poltype=None): |
---|
[1846] | 2307 | """\ |
---|
[992] | 2308 | Convert the data to a different polarisation type. |
---|
[1565] | 2309 | Note that you will need cross-polarisation terms for most conversions. |
---|
[1846] | 2310 | |
---|
[992] | 2311 | Parameters: |
---|
[1855] | 2312 | |
---|
[992] | 2313 | poltype: The new polarisation type. Valid types are: |
---|
[2431] | 2314 | 'linear', 'circular', 'stokes' and 'linpol' |
---|
[1846] | 2315 | |
---|
[992] | 2316 | """ |
---|
| 2317 | varlist = vars() |
---|
[1859] | 2318 | s = scantable(self._math._convertpol(self, poltype)) |
---|
[1118] | 2319 | s._add_history("convert_pol", varlist) |
---|
[992] | 2320 | return s |
---|
| 2321 | |
---|
[1862] | 2322 | @asaplog_post_dec |
---|
[2269] | 2323 | def smooth(self, kernel="hanning", width=5.0, order=2, plot=False, |
---|
| 2324 | insitu=None): |
---|
[1846] | 2325 | """\ |
---|
[513] | 2326 | Smooth the spectrum by the specified kernel (conserving flux). |
---|
[1846] | 2327 | |
---|
[513] | 2328 | Parameters: |
---|
[1846] | 2329 | |
---|
[513] | 2330 | kernel: The type of smoothing kernel. Select from |
---|
[1574] | 2331 | 'hanning' (default), 'gaussian', 'boxcar', 'rmedian' |
---|
| 2332 | or 'poly' |
---|
[1855] | 2333 | |
---|
[513] | 2334 | width: The width of the kernel in pixels. For hanning this is |
---|
| 2335 | ignored otherwise it defauls to 5 pixels. |
---|
| 2336 | For 'gaussian' it is the Full Width Half |
---|
| 2337 | Maximum. For 'boxcar' it is the full width. |
---|
[1574] | 2338 | For 'rmedian' and 'poly' it is the half width. |
---|
[1855] | 2339 | |
---|
[1574] | 2340 | order: Optional parameter for 'poly' kernel (default is 2), to |
---|
| 2341 | specify the order of the polnomial. Ignored by all other |
---|
| 2342 | kernels. |
---|
[1855] | 2343 | |
---|
[1819] | 2344 | plot: plot the original and the smoothed spectra. |
---|
| 2345 | In this each indivual fit has to be approved, by |
---|
| 2346 | typing 'y' or 'n' |
---|
[1855] | 2347 | |
---|
[513] | 2348 | insitu: if False a new scantable is returned. |
---|
| 2349 | Otherwise, the scaling is done in-situ |
---|
| 2350 | The default is taken from .asaprc (False) |
---|
[1846] | 2351 | |
---|
[513] | 2352 | """ |
---|
| 2353 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 2354 | self._math._setinsitu(insitu) |
---|
[513] | 2355 | varlist = vars() |
---|
[1819] | 2356 | |
---|
| 2357 | if plot: orgscan = self.copy() |
---|
| 2358 | |
---|
[1574] | 2359 | s = scantable(self._math._smooth(self, kernel.lower(), width, order)) |
---|
[876] | 2360 | s._add_history("smooth", varlist) |
---|
[1819] | 2361 | |
---|
[2610] | 2362 | action = 'H' |
---|
[1819] | 2363 | if plot: |
---|
[2150] | 2364 | from asap.asapplotter import new_asaplot |
---|
| 2365 | theplot = new_asaplot(rcParams['plotter.gui']) |
---|
[2535] | 2366 | from matplotlib import rc as rcp |
---|
| 2367 | rcp('lines', linewidth=1) |
---|
[2150] | 2368 | theplot.set_panels() |
---|
[1819] | 2369 | ylab=s._get_ordinate_label() |
---|
[2150] | 2370 | #theplot.palette(0,["#777777","red"]) |
---|
[1819] | 2371 | for r in xrange(s.nrow()): |
---|
| 2372 | xsm=s._getabcissa(r) |
---|
| 2373 | ysm=s._getspectrum(r) |
---|
| 2374 | xorg=orgscan._getabcissa(r) |
---|
| 2375 | yorg=orgscan._getspectrum(r) |
---|
[2610] | 2376 | if action != "N": #skip plotting if rejecting all |
---|
| 2377 | theplot.clear() |
---|
| 2378 | theplot.hold() |
---|
| 2379 | theplot.set_axes('ylabel',ylab) |
---|
| 2380 | theplot.set_axes('xlabel',s._getabcissalabel(r)) |
---|
| 2381 | theplot.set_axes('title',s._getsourcename(r)) |
---|
| 2382 | theplot.set_line(label='Original',color="#777777") |
---|
| 2383 | theplot.plot(xorg,yorg) |
---|
| 2384 | theplot.set_line(label='Smoothed',color="red") |
---|
| 2385 | theplot.plot(xsm,ysm) |
---|
| 2386 | ### Ugly part for legend |
---|
| 2387 | for i in [0,1]: |
---|
| 2388 | theplot.subplots[0]['lines'].append( |
---|
| 2389 | [theplot.subplots[0]['axes'].lines[i]] |
---|
| 2390 | ) |
---|
| 2391 | theplot.release() |
---|
| 2392 | ### Ugly part for legend |
---|
| 2393 | theplot.subplots[0]['lines']=[] |
---|
| 2394 | res = self._get_verify_action("Accept smoothing?",action) |
---|
| 2395 | #print "IF%d, POL%d: got result = %s" %(s.getif(r),s.getpol(r),res) |
---|
| 2396 | if r == 0: action = None |
---|
| 2397 | #res = raw_input("Accept smoothing ([y]/n): ") |
---|
[1819] | 2398 | if res.upper() == 'N': |
---|
[2610] | 2399 | # reject for the current rows |
---|
[1819] | 2400 | s._setspectrum(yorg, r) |
---|
[2610] | 2401 | elif res.upper() == 'R': |
---|
| 2402 | # reject all the following rows |
---|
| 2403 | action = "N" |
---|
| 2404 | s._setspectrum(yorg, r) |
---|
| 2405 | elif res.upper() == 'A': |
---|
| 2406 | # accept all the following rows |
---|
| 2407 | break |
---|
[2150] | 2408 | theplot.quit() |
---|
| 2409 | del theplot |
---|
[1819] | 2410 | del orgscan |
---|
| 2411 | |
---|
[876] | 2412 | if insitu: self._assign(s) |
---|
| 2413 | else: return s |
---|
[513] | 2414 | |
---|
[2186] | 2415 | @asaplog_post_dec |
---|
[2435] | 2416 | def regrid_channel(self, width=5, plot=False, insitu=None): |
---|
| 2417 | """\ |
---|
| 2418 | Regrid the spectra by the specified channel width |
---|
| 2419 | |
---|
| 2420 | Parameters: |
---|
| 2421 | |
---|
| 2422 | width: The channel width (float) of regridded spectra |
---|
| 2423 | in the current spectral unit. |
---|
| 2424 | |
---|
| 2425 | plot: [NOT IMPLEMENTED YET] |
---|
| 2426 | plot the original and the regridded spectra. |
---|
| 2427 | In this each indivual fit has to be approved, by |
---|
| 2428 | typing 'y' or 'n' |
---|
| 2429 | |
---|
| 2430 | insitu: if False a new scantable is returned. |
---|
| 2431 | Otherwise, the scaling is done in-situ |
---|
| 2432 | The default is taken from .asaprc (False) |
---|
| 2433 | |
---|
| 2434 | """ |
---|
| 2435 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 2436 | varlist = vars() |
---|
| 2437 | |
---|
| 2438 | if plot: |
---|
| 2439 | asaplog.post() |
---|
| 2440 | asaplog.push("Verification plot is not implemtnetd yet.") |
---|
| 2441 | asaplog.post("WARN") |
---|
| 2442 | |
---|
| 2443 | s = self.copy() |
---|
| 2444 | s._regrid_specchan(width) |
---|
| 2445 | |
---|
| 2446 | s._add_history("regrid_channel", varlist) |
---|
| 2447 | |
---|
| 2448 | # if plot: |
---|
| 2449 | # from asap.asapplotter import new_asaplot |
---|
| 2450 | # theplot = new_asaplot(rcParams['plotter.gui']) |
---|
[2535] | 2451 | # from matplotlib import rc as rcp |
---|
| 2452 | # rcp('lines', linewidth=1) |
---|
[2435] | 2453 | # theplot.set_panels() |
---|
| 2454 | # ylab=s._get_ordinate_label() |
---|
| 2455 | # #theplot.palette(0,["#777777","red"]) |
---|
| 2456 | # for r in xrange(s.nrow()): |
---|
| 2457 | # xsm=s._getabcissa(r) |
---|
| 2458 | # ysm=s._getspectrum(r) |
---|
| 2459 | # xorg=orgscan._getabcissa(r) |
---|
| 2460 | # yorg=orgscan._getspectrum(r) |
---|
| 2461 | # theplot.clear() |
---|
| 2462 | # theplot.hold() |
---|
| 2463 | # theplot.set_axes('ylabel',ylab) |
---|
| 2464 | # theplot.set_axes('xlabel',s._getabcissalabel(r)) |
---|
| 2465 | # theplot.set_axes('title',s._getsourcename(r)) |
---|
| 2466 | # theplot.set_line(label='Original',color="#777777") |
---|
| 2467 | # theplot.plot(xorg,yorg) |
---|
| 2468 | # theplot.set_line(label='Smoothed',color="red") |
---|
| 2469 | # theplot.plot(xsm,ysm) |
---|
| 2470 | # ### Ugly part for legend |
---|
| 2471 | # for i in [0,1]: |
---|
| 2472 | # theplot.subplots[0]['lines'].append( |
---|
| 2473 | # [theplot.subplots[0]['axes'].lines[i]] |
---|
| 2474 | # ) |
---|
| 2475 | # theplot.release() |
---|
| 2476 | # ### Ugly part for legend |
---|
| 2477 | # theplot.subplots[0]['lines']=[] |
---|
| 2478 | # res = raw_input("Accept smoothing ([y]/n): ") |
---|
| 2479 | # if res.upper() == 'N': |
---|
| 2480 | # s._setspectrum(yorg, r) |
---|
| 2481 | # theplot.quit() |
---|
| 2482 | # del theplot |
---|
| 2483 | # del orgscan |
---|
| 2484 | |
---|
| 2485 | if insitu: self._assign(s) |
---|
| 2486 | else: return s |
---|
| 2487 | |
---|
| 2488 | @asaplog_post_dec |
---|
[2186] | 2489 | def _parse_wn(self, wn): |
---|
| 2490 | if isinstance(wn, list) or isinstance(wn, tuple): |
---|
| 2491 | return wn |
---|
| 2492 | elif isinstance(wn, int): |
---|
| 2493 | return [ wn ] |
---|
| 2494 | elif isinstance(wn, str): |
---|
[2277] | 2495 | if '-' in wn: # case 'a-b' : return [a,a+1,...,b-1,b] |
---|
[2186] | 2496 | val = wn.split('-') |
---|
| 2497 | val = [int(val[0]), int(val[1])] |
---|
| 2498 | val.sort() |
---|
| 2499 | res = [i for i in xrange(val[0], val[1]+1)] |
---|
[2277] | 2500 | elif wn[:2] == '<=' or wn[:2] == '=<': # cases '<=a','=<a' : return [0,1,...,a-1,a] |
---|
[2186] | 2501 | val = int(wn[2:])+1 |
---|
| 2502 | res = [i for i in xrange(val)] |
---|
[2277] | 2503 | elif wn[-2:] == '>=' or wn[-2:] == '=>': # cases 'a>=','a=>' : return [0,1,...,a-1,a] |
---|
[2186] | 2504 | val = int(wn[:-2])+1 |
---|
| 2505 | res = [i for i in xrange(val)] |
---|
[2277] | 2506 | elif wn[0] == '<': # case '<a' : return [0,1,...,a-2,a-1] |
---|
[2186] | 2507 | val = int(wn[1:]) |
---|
| 2508 | res = [i for i in xrange(val)] |
---|
[2277] | 2509 | elif wn[-1] == '>': # case 'a>' : return [0,1,...,a-2,a-1] |
---|
[2186] | 2510 | val = int(wn[:-1]) |
---|
| 2511 | res = [i for i in xrange(val)] |
---|
[2411] | 2512 | elif wn[:2] == '>=' or wn[:2] == '=>': # cases '>=a','=>a' : return [a,-999], which is |
---|
| 2513 | # then interpreted in C++ |
---|
| 2514 | # side as [a,a+1,...,a_nyq] |
---|
| 2515 | # (CAS-3759) |
---|
[2186] | 2516 | val = int(wn[2:]) |
---|
[2411] | 2517 | res = [val, -999] |
---|
| 2518 | #res = [i for i in xrange(val, self.nchan()/2+1)] |
---|
| 2519 | elif wn[-2:] == '<=' or wn[-2:] == '=<': # cases 'a<=','a=<' : return [a,-999], which is |
---|
| 2520 | # then interpreted in C++ |
---|
| 2521 | # side as [a,a+1,...,a_nyq] |
---|
| 2522 | # (CAS-3759) |
---|
[2186] | 2523 | val = int(wn[:-2]) |
---|
[2411] | 2524 | res = [val, -999] |
---|
| 2525 | #res = [i for i in xrange(val, self.nchan()/2+1)] |
---|
| 2526 | elif wn[0] == '>': # case '>a' : return [a+1,-999], which is |
---|
| 2527 | # then interpreted in C++ |
---|
| 2528 | # side as [a+1,a+2,...,a_nyq] |
---|
| 2529 | # (CAS-3759) |
---|
[2186] | 2530 | val = int(wn[1:])+1 |
---|
[2411] | 2531 | res = [val, -999] |
---|
| 2532 | #res = [i for i in xrange(val, self.nchan()/2+1)] |
---|
| 2533 | elif wn[-1] == '<': # case 'a<' : return [a+1,-999], which is |
---|
| 2534 | # then interpreted in C++ |
---|
| 2535 | # side as [a+1,a+2,...,a_nyq] |
---|
| 2536 | # (CAS-3759) |
---|
[2186] | 2537 | val = int(wn[:-1])+1 |
---|
[2411] | 2538 | res = [val, -999] |
---|
| 2539 | #res = [i for i in xrange(val, self.nchan()/2+1)] |
---|
[2012] | 2540 | |
---|
[2186] | 2541 | return res |
---|
| 2542 | else: |
---|
| 2543 | msg = 'wrong value given for addwn/rejwn' |
---|
| 2544 | raise RuntimeError(msg) |
---|
| 2545 | |
---|
[2713] | 2546 | @asaplog_post_dec |
---|
| 2547 | def calc_aic(self, value=None, blfunc=None, order=None, mask=None, |
---|
| 2548 | whichrow=None, uselinefinder=None, edge=None, |
---|
| 2549 | threshold=None, chan_avg_limit=None): |
---|
| 2550 | """\ |
---|
| 2551 | Calculates and returns model selection criteria for a specified |
---|
| 2552 | baseline model and a given spectrum data. |
---|
| 2553 | Available values include Akaike Information Criterion (AIC), the |
---|
| 2554 | corrected Akaike Information Criterion (AICc) by Sugiura(1978), |
---|
| 2555 | Bayesian Information Criterion (BIC) and the Generalised Cross |
---|
| 2556 | Validation (GCV). |
---|
[2186] | 2557 | |
---|
[2713] | 2558 | Parameters: |
---|
| 2559 | value: name of model selection criteria to calculate. |
---|
| 2560 | available ones include 'aic', 'aicc', 'bic' and |
---|
| 2561 | 'gcv'. default is 'aicc'. |
---|
| 2562 | blfunc: baseline function name. available ones include |
---|
| 2563 | 'chebyshev', 'cspline' and 'sinusoid'. |
---|
| 2564 | default is 'chebyshev'. |
---|
| 2565 | order: parameter for basline function. actually stands for |
---|
| 2566 | order of polynomial (order) for 'chebyshev', |
---|
| 2567 | number of spline pieces (npiece) for 'cspline' and |
---|
| 2568 | maximum wave number for 'sinusoid', respectively. |
---|
| 2569 | default is 5 (which is also the default order value |
---|
| 2570 | for [auto_]chebyshev_baseline()). |
---|
| 2571 | mask: an optional mask. default is []. |
---|
| 2572 | whichrow: row number. default is 0 (the first row) |
---|
| 2573 | uselinefinder: use sd.linefinder() to flag out line regions |
---|
| 2574 | default is True. |
---|
| 2575 | edge: an optional number of channel to drop at |
---|
| 2576 | the edge of spectrum. If only one value is |
---|
| 2577 | specified, the same number will be dropped |
---|
| 2578 | from both sides of the spectrum. Default |
---|
| 2579 | is to keep all channels. Nested tuples |
---|
| 2580 | represent individual edge selection for |
---|
| 2581 | different IFs (a number of spectral channels |
---|
| 2582 | can be different) |
---|
| 2583 | default is (0, 0). |
---|
| 2584 | threshold: the threshold used by line finder. It is |
---|
| 2585 | better to keep it large as only strong lines |
---|
| 2586 | affect the baseline solution. |
---|
| 2587 | default is 3. |
---|
| 2588 | chan_avg_limit: a maximum number of consequtive spectral |
---|
| 2589 | channels to average during the search of |
---|
| 2590 | weak and broad lines. The default is no |
---|
| 2591 | averaging (and no search for weak lines). |
---|
| 2592 | If such lines can affect the fitted baseline |
---|
| 2593 | (e.g. a high order polynomial is fitted), |
---|
| 2594 | increase this parameter (usually values up |
---|
| 2595 | to 8 are reasonable). Most users of this |
---|
| 2596 | method should find the default value sufficient. |
---|
| 2597 | default is 1. |
---|
| 2598 | |
---|
| 2599 | Example: |
---|
| 2600 | aic = scan.calc_aic(blfunc='chebyshev', order=5, whichrow=0) |
---|
| 2601 | """ |
---|
| 2602 | |
---|
| 2603 | try: |
---|
| 2604 | varlist = vars() |
---|
| 2605 | |
---|
| 2606 | if value is None: value = 'aicc' |
---|
| 2607 | if blfunc is None: blfunc = 'chebyshev' |
---|
| 2608 | if order is None: order = 5 |
---|
| 2609 | if mask is None: mask = [] |
---|
| 2610 | if whichrow is None: whichrow = 0 |
---|
| 2611 | if uselinefinder is None: uselinefinder = True |
---|
| 2612 | if edge is None: edge = (0, 0) |
---|
| 2613 | if threshold is None: threshold = 3 |
---|
| 2614 | if chan_avg_limit is None: chan_avg_limit = 1 |
---|
| 2615 | |
---|
| 2616 | return self._calc_aic(value, blfunc, order, mask, |
---|
| 2617 | whichrow, uselinefinder, edge, |
---|
| 2618 | threshold, chan_avg_limit) |
---|
| 2619 | |
---|
| 2620 | except RuntimeError, e: |
---|
| 2621 | raise_fitting_failure_exception(e) |
---|
| 2622 | |
---|
[1862] | 2623 | @asaplog_post_dec |
---|
[2277] | 2624 | def sinusoid_baseline(self, insitu=None, mask=None, applyfft=None, |
---|
[2269] | 2625 | fftmethod=None, fftthresh=None, |
---|
| 2626 | addwn=None, rejwn=None, clipthresh=None, |
---|
| 2627 | clipniter=None, plot=None, |
---|
| 2628 | getresidual=None, showprogress=None, |
---|
[2641] | 2629 | minnrow=None, outlog=None, blfile=None, csvformat=None): |
---|
[2047] | 2630 | """\ |
---|
[2349] | 2631 | Return a scan which has been baselined (all rows) with sinusoidal |
---|
| 2632 | functions. |
---|
| 2633 | |
---|
[2047] | 2634 | Parameters: |
---|
[2186] | 2635 | insitu: if False a new scantable is returned. |
---|
[2081] | 2636 | Otherwise, the scaling is done in-situ |
---|
| 2637 | The default is taken from .asaprc (False) |
---|
[2186] | 2638 | mask: an optional mask |
---|
| 2639 | applyfft: if True use some method, such as FFT, to find |
---|
| 2640 | strongest sinusoidal components in the wavenumber |
---|
| 2641 | domain to be used for baseline fitting. |
---|
| 2642 | default is True. |
---|
| 2643 | fftmethod: method to find the strong sinusoidal components. |
---|
| 2644 | now only 'fft' is available and it is the default. |
---|
| 2645 | fftthresh: the threshold to select wave numbers to be used for |
---|
| 2646 | fitting from the distribution of amplitudes in the |
---|
| 2647 | wavenumber domain. |
---|
| 2648 | both float and string values accepted. |
---|
| 2649 | given a float value, the unit is set to sigma. |
---|
| 2650 | for string values, allowed formats include: |
---|
[2349] | 2651 | 'xsigma' or 'x' (= x-sigma level. e.g., |
---|
| 2652 | '3sigma'), or |
---|
[2186] | 2653 | 'topx' (= the x strongest ones, e.g. 'top5'). |
---|
| 2654 | default is 3.0 (unit: sigma). |
---|
| 2655 | addwn: the additional wave numbers to be used for fitting. |
---|
| 2656 | list or integer value is accepted to specify every |
---|
| 2657 | wave numbers. also string value can be used in case |
---|
| 2658 | you need to specify wave numbers in a certain range, |
---|
| 2659 | e.g., 'a-b' (= a, a+1, a+2, ..., b-1, b), |
---|
| 2660 | '<a' (= 0,1,...,a-2,a-1), |
---|
| 2661 | '>=a' (= a, a+1, ... up to the maximum wave |
---|
| 2662 | number corresponding to the Nyquist |
---|
| 2663 | frequency for the case of FFT). |
---|
[2411] | 2664 | default is [0]. |
---|
[2186] | 2665 | rejwn: the wave numbers NOT to be used for fitting. |
---|
| 2666 | can be set just as addwn but has higher priority: |
---|
| 2667 | wave numbers which are specified both in addwn |
---|
| 2668 | and rejwn will NOT be used. default is []. |
---|
[2081] | 2669 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma) |
---|
[2349] | 2670 | clipniter: maximum number of iteration of 'clipthresh'-sigma |
---|
| 2671 | clipping (default is 0) |
---|
[2081] | 2672 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE *** |
---|
| 2673 | plot the fit and the residual. In this each |
---|
| 2674 | indivual fit has to be approved, by typing 'y' |
---|
| 2675 | or 'n' |
---|
| 2676 | getresidual: if False, returns best-fit values instead of |
---|
| 2677 | residual. (default is True) |
---|
[2189] | 2678 | showprogress: show progress status for large data. |
---|
| 2679 | default is True. |
---|
| 2680 | minnrow: minimum number of input spectra to show. |
---|
| 2681 | default is 1000. |
---|
[2081] | 2682 | outlog: Output the coefficients of the best-fit |
---|
| 2683 | function to logger (default is False) |
---|
| 2684 | blfile: Name of a text file in which the best-fit |
---|
| 2685 | parameter values to be written |
---|
[2186] | 2686 | (default is '': no file/logger output) |
---|
[2641] | 2687 | csvformat: if True blfile is csv-formatted, default is False. |
---|
[2047] | 2688 | |
---|
| 2689 | Example: |
---|
[2349] | 2690 | # return a scan baselined by a combination of sinusoidal curves |
---|
| 2691 | # having wave numbers in spectral window up to 10, |
---|
[2047] | 2692 | # also with 3-sigma clipping, iteration up to 4 times |
---|
[2186] | 2693 | bscan = scan.sinusoid_baseline(addwn='<=10',clipthresh=3.0,clipniter=4) |
---|
[2081] | 2694 | |
---|
| 2695 | Note: |
---|
| 2696 | The best-fit parameter values output in logger and/or blfile are now |
---|
| 2697 | based on specunit of 'channel'. |
---|
[2047] | 2698 | """ |
---|
| 2699 | |
---|
[2186] | 2700 | try: |
---|
| 2701 | varlist = vars() |
---|
[2047] | 2702 | |
---|
[2186] | 2703 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 2704 | if insitu: |
---|
| 2705 | workscan = self |
---|
| 2706 | else: |
---|
| 2707 | workscan = self.copy() |
---|
| 2708 | |
---|
[2410] | 2709 | #if mask is None: mask = [True for i in xrange(workscan.nchan())] |
---|
| 2710 | if mask is None: mask = [] |
---|
[2186] | 2711 | if applyfft is None: applyfft = True |
---|
| 2712 | if fftmethod is None: fftmethod = 'fft' |
---|
| 2713 | if fftthresh is None: fftthresh = 3.0 |
---|
[2411] | 2714 | if addwn is None: addwn = [0] |
---|
[2186] | 2715 | if rejwn is None: rejwn = [] |
---|
| 2716 | if clipthresh is None: clipthresh = 3.0 |
---|
| 2717 | if clipniter is None: clipniter = 0 |
---|
| 2718 | if plot is None: plot = False |
---|
| 2719 | if getresidual is None: getresidual = True |
---|
[2189] | 2720 | if showprogress is None: showprogress = True |
---|
| 2721 | if minnrow is None: minnrow = 1000 |
---|
[2186] | 2722 | if outlog is None: outlog = False |
---|
| 2723 | if blfile is None: blfile = '' |
---|
[2641] | 2724 | if csvformat is None: csvformat = False |
---|
[2047] | 2725 | |
---|
[2641] | 2726 | if csvformat: |
---|
| 2727 | scsvformat = "T" |
---|
| 2728 | else: |
---|
| 2729 | scsvformat = "F" |
---|
| 2730 | |
---|
[2081] | 2731 | #CURRENTLY, PLOT=true is UNAVAILABLE UNTIL sinusoidal fitting is implemented as a fitter method. |
---|
[2349] | 2732 | workscan._sinusoid_baseline(mask, applyfft, fftmethod.lower(), |
---|
| 2733 | str(fftthresh).lower(), |
---|
| 2734 | workscan._parse_wn(addwn), |
---|
[2643] | 2735 | workscan._parse_wn(rejwn), |
---|
| 2736 | clipthresh, clipniter, |
---|
| 2737 | getresidual, |
---|
[2349] | 2738 | pack_progress_params(showprogress, |
---|
[2641] | 2739 | minnrow), |
---|
| 2740 | outlog, scsvformat+blfile) |
---|
[2186] | 2741 | workscan._add_history('sinusoid_baseline', varlist) |
---|
[2047] | 2742 | |
---|
| 2743 | if insitu: |
---|
| 2744 | self._assign(workscan) |
---|
| 2745 | else: |
---|
| 2746 | return workscan |
---|
| 2747 | |
---|
| 2748 | except RuntimeError, e: |
---|
[2186] | 2749 | raise_fitting_failure_exception(e) |
---|
[2047] | 2750 | |
---|
| 2751 | |
---|
[2186] | 2752 | @asaplog_post_dec |
---|
[2349] | 2753 | def auto_sinusoid_baseline(self, insitu=None, mask=None, applyfft=None, |
---|
| 2754 | fftmethod=None, fftthresh=None, |
---|
| 2755 | addwn=None, rejwn=None, clipthresh=None, |
---|
| 2756 | clipniter=None, edge=None, threshold=None, |
---|
| 2757 | chan_avg_limit=None, plot=None, |
---|
| 2758 | getresidual=None, showprogress=None, |
---|
[2641] | 2759 | minnrow=None, outlog=None, blfile=None, csvformat=None): |
---|
[2047] | 2760 | """\ |
---|
[2349] | 2761 | Return a scan which has been baselined (all rows) with sinusoidal |
---|
| 2762 | functions. |
---|
[2047] | 2763 | Spectral lines are detected first using linefinder and masked out |
---|
| 2764 | to avoid them affecting the baseline solution. |
---|
| 2765 | |
---|
| 2766 | Parameters: |
---|
[2189] | 2767 | insitu: if False a new scantable is returned. |
---|
| 2768 | Otherwise, the scaling is done in-situ |
---|
| 2769 | The default is taken from .asaprc (False) |
---|
| 2770 | mask: an optional mask retreived from scantable |
---|
| 2771 | applyfft: if True use some method, such as FFT, to find |
---|
| 2772 | strongest sinusoidal components in the wavenumber |
---|
| 2773 | domain to be used for baseline fitting. |
---|
| 2774 | default is True. |
---|
| 2775 | fftmethod: method to find the strong sinusoidal components. |
---|
| 2776 | now only 'fft' is available and it is the default. |
---|
| 2777 | fftthresh: the threshold to select wave numbers to be used for |
---|
| 2778 | fitting from the distribution of amplitudes in the |
---|
| 2779 | wavenumber domain. |
---|
| 2780 | both float and string values accepted. |
---|
| 2781 | given a float value, the unit is set to sigma. |
---|
| 2782 | for string values, allowed formats include: |
---|
[2349] | 2783 | 'xsigma' or 'x' (= x-sigma level. e.g., |
---|
| 2784 | '3sigma'), or |
---|
[2189] | 2785 | 'topx' (= the x strongest ones, e.g. 'top5'). |
---|
| 2786 | default is 3.0 (unit: sigma). |
---|
| 2787 | addwn: the additional wave numbers to be used for fitting. |
---|
| 2788 | list or integer value is accepted to specify every |
---|
| 2789 | wave numbers. also string value can be used in case |
---|
| 2790 | you need to specify wave numbers in a certain range, |
---|
| 2791 | e.g., 'a-b' (= a, a+1, a+2, ..., b-1, b), |
---|
| 2792 | '<a' (= 0,1,...,a-2,a-1), |
---|
| 2793 | '>=a' (= a, a+1, ... up to the maximum wave |
---|
| 2794 | number corresponding to the Nyquist |
---|
| 2795 | frequency for the case of FFT). |
---|
[2411] | 2796 | default is [0]. |
---|
[2189] | 2797 | rejwn: the wave numbers NOT to be used for fitting. |
---|
| 2798 | can be set just as addwn but has higher priority: |
---|
| 2799 | wave numbers which are specified both in addwn |
---|
| 2800 | and rejwn will NOT be used. default is []. |
---|
| 2801 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma) |
---|
[2349] | 2802 | clipniter: maximum number of iteration of 'clipthresh'-sigma |
---|
| 2803 | clipping (default is 0) |
---|
[2189] | 2804 | edge: an optional number of channel to drop at |
---|
| 2805 | the edge of spectrum. If only one value is |
---|
| 2806 | specified, the same number will be dropped |
---|
| 2807 | from both sides of the spectrum. Default |
---|
| 2808 | is to keep all channels. Nested tuples |
---|
| 2809 | represent individual edge selection for |
---|
| 2810 | different IFs (a number of spectral channels |
---|
| 2811 | can be different) |
---|
| 2812 | threshold: the threshold used by line finder. It is |
---|
| 2813 | better to keep it large as only strong lines |
---|
| 2814 | affect the baseline solution. |
---|
| 2815 | chan_avg_limit: a maximum number of consequtive spectral |
---|
| 2816 | channels to average during the search of |
---|
| 2817 | weak and broad lines. The default is no |
---|
| 2818 | averaging (and no search for weak lines). |
---|
| 2819 | If such lines can affect the fitted baseline |
---|
| 2820 | (e.g. a high order polynomial is fitted), |
---|
| 2821 | increase this parameter (usually values up |
---|
| 2822 | to 8 are reasonable). Most users of this |
---|
| 2823 | method should find the default value sufficient. |
---|
| 2824 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE *** |
---|
| 2825 | plot the fit and the residual. In this each |
---|
| 2826 | indivual fit has to be approved, by typing 'y' |
---|
| 2827 | or 'n' |
---|
| 2828 | getresidual: if False, returns best-fit values instead of |
---|
| 2829 | residual. (default is True) |
---|
| 2830 | showprogress: show progress status for large data. |
---|
| 2831 | default is True. |
---|
| 2832 | minnrow: minimum number of input spectra to show. |
---|
| 2833 | default is 1000. |
---|
| 2834 | outlog: Output the coefficients of the best-fit |
---|
| 2835 | function to logger (default is False) |
---|
| 2836 | blfile: Name of a text file in which the best-fit |
---|
| 2837 | parameter values to be written |
---|
| 2838 | (default is "": no file/logger output) |
---|
[2641] | 2839 | csvformat: if True blfile is csv-formatted, default is False. |
---|
[2047] | 2840 | |
---|
| 2841 | Example: |
---|
[2186] | 2842 | bscan = scan.auto_sinusoid_baseline(addwn='<=10', insitu=False) |
---|
[2081] | 2843 | |
---|
| 2844 | Note: |
---|
| 2845 | The best-fit parameter values output in logger and/or blfile are now |
---|
| 2846 | based on specunit of 'channel'. |
---|
[2047] | 2847 | """ |
---|
| 2848 | |
---|
[2186] | 2849 | try: |
---|
| 2850 | varlist = vars() |
---|
[2047] | 2851 | |
---|
[2186] | 2852 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 2853 | if insitu: |
---|
| 2854 | workscan = self |
---|
[2047] | 2855 | else: |
---|
[2186] | 2856 | workscan = self.copy() |
---|
| 2857 | |
---|
[2410] | 2858 | #if mask is None: mask = [True for i in xrange(workscan.nchan())] |
---|
| 2859 | if mask is None: mask = [] |
---|
[2186] | 2860 | if applyfft is None: applyfft = True |
---|
| 2861 | if fftmethod is None: fftmethod = 'fft' |
---|
| 2862 | if fftthresh is None: fftthresh = 3.0 |
---|
[2411] | 2863 | if addwn is None: addwn = [0] |
---|
[2186] | 2864 | if rejwn is None: rejwn = [] |
---|
| 2865 | if clipthresh is None: clipthresh = 3.0 |
---|
| 2866 | if clipniter is None: clipniter = 0 |
---|
| 2867 | if edge is None: edge = (0,0) |
---|
| 2868 | if threshold is None: threshold = 3 |
---|
| 2869 | if chan_avg_limit is None: chan_avg_limit = 1 |
---|
| 2870 | if plot is None: plot = False |
---|
| 2871 | if getresidual is None: getresidual = True |
---|
[2189] | 2872 | if showprogress is None: showprogress = True |
---|
| 2873 | if minnrow is None: minnrow = 1000 |
---|
[2186] | 2874 | if outlog is None: outlog = False |
---|
| 2875 | if blfile is None: blfile = '' |
---|
[2641] | 2876 | if csvformat is None: csvformat = False |
---|
[2047] | 2877 | |
---|
[2641] | 2878 | if csvformat: |
---|
| 2879 | scsvformat = "T" |
---|
| 2880 | else: |
---|
| 2881 | scsvformat = "F" |
---|
| 2882 | |
---|
[2277] | 2883 | #CURRENTLY, PLOT=true is UNAVAILABLE UNTIL sinusoidal fitting is implemented as a fitter method. |
---|
[2349] | 2884 | workscan._auto_sinusoid_baseline(mask, applyfft, |
---|
| 2885 | fftmethod.lower(), |
---|
| 2886 | str(fftthresh).lower(), |
---|
| 2887 | workscan._parse_wn(addwn), |
---|
| 2888 | workscan._parse_wn(rejwn), |
---|
| 2889 | clipthresh, clipniter, |
---|
| 2890 | normalise_edge_param(edge), |
---|
| 2891 | threshold, chan_avg_limit, |
---|
| 2892 | getresidual, |
---|
| 2893 | pack_progress_params(showprogress, |
---|
| 2894 | minnrow), |
---|
[2641] | 2895 | outlog, scsvformat+blfile) |
---|
[2047] | 2896 | workscan._add_history("auto_sinusoid_baseline", varlist) |
---|
| 2897 | |
---|
| 2898 | if insitu: |
---|
| 2899 | self._assign(workscan) |
---|
| 2900 | else: |
---|
| 2901 | return workscan |
---|
| 2902 | |
---|
| 2903 | except RuntimeError, e: |
---|
[2186] | 2904 | raise_fitting_failure_exception(e) |
---|
[2047] | 2905 | |
---|
| 2906 | @asaplog_post_dec |
---|
[2349] | 2907 | def cspline_baseline(self, insitu=None, mask=None, npiece=None, |
---|
| 2908 | clipthresh=None, clipniter=None, plot=None, |
---|
| 2909 | getresidual=None, showprogress=None, minnrow=None, |
---|
[2641] | 2910 | outlog=None, blfile=None, csvformat=None): |
---|
[1846] | 2911 | """\ |
---|
[2349] | 2912 | Return a scan which has been baselined (all rows) by cubic spline |
---|
| 2913 | function (piecewise cubic polynomial). |
---|
| 2914 | |
---|
[513] | 2915 | Parameters: |
---|
[2189] | 2916 | insitu: If False a new scantable is returned. |
---|
| 2917 | Otherwise, the scaling is done in-situ |
---|
| 2918 | The default is taken from .asaprc (False) |
---|
| 2919 | mask: An optional mask |
---|
| 2920 | npiece: Number of pieces. (default is 2) |
---|
| 2921 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma) |
---|
[2349] | 2922 | clipniter: maximum number of iteration of 'clipthresh'-sigma |
---|
| 2923 | clipping (default is 0) |
---|
[2189] | 2924 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE *** |
---|
| 2925 | plot the fit and the residual. In this each |
---|
| 2926 | indivual fit has to be approved, by typing 'y' |
---|
| 2927 | or 'n' |
---|
| 2928 | getresidual: if False, returns best-fit values instead of |
---|
| 2929 | residual. (default is True) |
---|
| 2930 | showprogress: show progress status for large data. |
---|
| 2931 | default is True. |
---|
| 2932 | minnrow: minimum number of input spectra to show. |
---|
| 2933 | default is 1000. |
---|
| 2934 | outlog: Output the coefficients of the best-fit |
---|
| 2935 | function to logger (default is False) |
---|
| 2936 | blfile: Name of a text file in which the best-fit |
---|
| 2937 | parameter values to be written |
---|
| 2938 | (default is "": no file/logger output) |
---|
[2641] | 2939 | csvformat: if True blfile is csv-formatted, default is False. |
---|
[1846] | 2940 | |
---|
[2012] | 2941 | Example: |
---|
[2349] | 2942 | # return a scan baselined by a cubic spline consisting of 2 pieces |
---|
| 2943 | # (i.e., 1 internal knot), |
---|
[2012] | 2944 | # also with 3-sigma clipping, iteration up to 4 times |
---|
| 2945 | bscan = scan.cspline_baseline(npiece=2,clipthresh=3.0,clipniter=4) |
---|
[2081] | 2946 | |
---|
| 2947 | Note: |
---|
| 2948 | The best-fit parameter values output in logger and/or blfile are now |
---|
| 2949 | based on specunit of 'channel'. |
---|
[2012] | 2950 | """ |
---|
| 2951 | |
---|
[2186] | 2952 | try: |
---|
| 2953 | varlist = vars() |
---|
| 2954 | |
---|
| 2955 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 2956 | if insitu: |
---|
| 2957 | workscan = self |
---|
| 2958 | else: |
---|
| 2959 | workscan = self.copy() |
---|
[1855] | 2960 | |
---|
[2410] | 2961 | #if mask is None: mask = [True for i in xrange(workscan.nchan())] |
---|
| 2962 | if mask is None: mask = [] |
---|
[2189] | 2963 | if npiece is None: npiece = 2 |
---|
| 2964 | if clipthresh is None: clipthresh = 3.0 |
---|
| 2965 | if clipniter is None: clipniter = 0 |
---|
| 2966 | if plot is None: plot = False |
---|
| 2967 | if getresidual is None: getresidual = True |
---|
| 2968 | if showprogress is None: showprogress = True |
---|
| 2969 | if minnrow is None: minnrow = 1000 |
---|
| 2970 | if outlog is None: outlog = False |
---|
| 2971 | if blfile is None: blfile = '' |
---|
[2641] | 2972 | if csvformat is None: csvformat = False |
---|
[1855] | 2973 | |
---|
[2641] | 2974 | if csvformat: |
---|
| 2975 | scsvformat = "T" |
---|
| 2976 | else: |
---|
| 2977 | scsvformat = "F" |
---|
| 2978 | |
---|
[2012] | 2979 | #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method. |
---|
[2349] | 2980 | workscan._cspline_baseline(mask, npiece, clipthresh, clipniter, |
---|
| 2981 | getresidual, |
---|
| 2982 | pack_progress_params(showprogress, |
---|
[2641] | 2983 | minnrow), |
---|
| 2984 | outlog, scsvformat+blfile) |
---|
[2012] | 2985 | workscan._add_history("cspline_baseline", varlist) |
---|
| 2986 | |
---|
| 2987 | if insitu: |
---|
| 2988 | self._assign(workscan) |
---|
| 2989 | else: |
---|
| 2990 | return workscan |
---|
| 2991 | |
---|
| 2992 | except RuntimeError, e: |
---|
[2186] | 2993 | raise_fitting_failure_exception(e) |
---|
[1855] | 2994 | |
---|
[2186] | 2995 | @asaplog_post_dec |
---|
[2349] | 2996 | def auto_cspline_baseline(self, insitu=None, mask=None, npiece=None, |
---|
| 2997 | clipthresh=None, clipniter=None, |
---|
| 2998 | edge=None, threshold=None, chan_avg_limit=None, |
---|
| 2999 | getresidual=None, plot=None, |
---|
| 3000 | showprogress=None, minnrow=None, outlog=None, |
---|
[2641] | 3001 | blfile=None, csvformat=None): |
---|
[2012] | 3002 | """\ |
---|
| 3003 | Return a scan which has been baselined (all rows) by cubic spline |
---|
| 3004 | function (piecewise cubic polynomial). |
---|
| 3005 | Spectral lines are detected first using linefinder and masked out |
---|
| 3006 | to avoid them affecting the baseline solution. |
---|
| 3007 | |
---|
| 3008 | Parameters: |
---|
[2189] | 3009 | insitu: if False a new scantable is returned. |
---|
| 3010 | Otherwise, the scaling is done in-situ |
---|
| 3011 | The default is taken from .asaprc (False) |
---|
| 3012 | mask: an optional mask retreived from scantable |
---|
| 3013 | npiece: Number of pieces. (default is 2) |
---|
| 3014 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma) |
---|
[2349] | 3015 | clipniter: maximum number of iteration of 'clipthresh'-sigma |
---|
| 3016 | clipping (default is 0) |
---|
[2189] | 3017 | edge: an optional number of channel to drop at |
---|
| 3018 | the edge of spectrum. If only one value is |
---|
| 3019 | specified, the same number will be dropped |
---|
| 3020 | from both sides of the spectrum. Default |
---|
| 3021 | is to keep all channels. Nested tuples |
---|
| 3022 | represent individual edge selection for |
---|
| 3023 | different IFs (a number of spectral channels |
---|
| 3024 | can be different) |
---|
| 3025 | threshold: the threshold used by line finder. It is |
---|
| 3026 | better to keep it large as only strong lines |
---|
| 3027 | affect the baseline solution. |
---|
| 3028 | chan_avg_limit: a maximum number of consequtive spectral |
---|
| 3029 | channels to average during the search of |
---|
| 3030 | weak and broad lines. The default is no |
---|
| 3031 | averaging (and no search for weak lines). |
---|
| 3032 | If such lines can affect the fitted baseline |
---|
| 3033 | (e.g. a high order polynomial is fitted), |
---|
| 3034 | increase this parameter (usually values up |
---|
| 3035 | to 8 are reasonable). Most users of this |
---|
| 3036 | method should find the default value sufficient. |
---|
| 3037 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE *** |
---|
| 3038 | plot the fit and the residual. In this each |
---|
| 3039 | indivual fit has to be approved, by typing 'y' |
---|
| 3040 | or 'n' |
---|
| 3041 | getresidual: if False, returns best-fit values instead of |
---|
| 3042 | residual. (default is True) |
---|
| 3043 | showprogress: show progress status for large data. |
---|
| 3044 | default is True. |
---|
| 3045 | minnrow: minimum number of input spectra to show. |
---|
| 3046 | default is 1000. |
---|
| 3047 | outlog: Output the coefficients of the best-fit |
---|
| 3048 | function to logger (default is False) |
---|
| 3049 | blfile: Name of a text file in which the best-fit |
---|
| 3050 | parameter values to be written |
---|
| 3051 | (default is "": no file/logger output) |
---|
[2641] | 3052 | csvformat: if True blfile is csv-formatted, default is False. |
---|
[1846] | 3053 | |
---|
[1907] | 3054 | Example: |
---|
[2012] | 3055 | bscan = scan.auto_cspline_baseline(npiece=3, insitu=False) |
---|
[2081] | 3056 | |
---|
| 3057 | Note: |
---|
| 3058 | The best-fit parameter values output in logger and/or blfile are now |
---|
| 3059 | based on specunit of 'channel'. |
---|
[2012] | 3060 | """ |
---|
[1846] | 3061 | |
---|
[2186] | 3062 | try: |
---|
| 3063 | varlist = vars() |
---|
[2012] | 3064 | |
---|
[2186] | 3065 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 3066 | if insitu: |
---|
| 3067 | workscan = self |
---|
[1391] | 3068 | else: |
---|
[2186] | 3069 | workscan = self.copy() |
---|
| 3070 | |
---|
[2410] | 3071 | #if mask is None: mask = [True for i in xrange(workscan.nchan())] |
---|
| 3072 | if mask is None: mask = [] |
---|
[2186] | 3073 | if npiece is None: npiece = 2 |
---|
| 3074 | if clipthresh is None: clipthresh = 3.0 |
---|
| 3075 | if clipniter is None: clipniter = 0 |
---|
| 3076 | if edge is None: edge = (0, 0) |
---|
| 3077 | if threshold is None: threshold = 3 |
---|
| 3078 | if chan_avg_limit is None: chan_avg_limit = 1 |
---|
| 3079 | if plot is None: plot = False |
---|
| 3080 | if getresidual is None: getresidual = True |
---|
[2189] | 3081 | if showprogress is None: showprogress = True |
---|
| 3082 | if minnrow is None: minnrow = 1000 |
---|
[2186] | 3083 | if outlog is None: outlog = False |
---|
| 3084 | if blfile is None: blfile = '' |
---|
[2641] | 3085 | if csvformat is None: csvformat = False |
---|
[1819] | 3086 | |
---|
[2641] | 3087 | if csvformat: |
---|
| 3088 | scsvformat = "T" |
---|
| 3089 | else: |
---|
| 3090 | scsvformat = "F" |
---|
| 3091 | |
---|
[2277] | 3092 | #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method. |
---|
[2269] | 3093 | workscan._auto_cspline_baseline(mask, npiece, clipthresh, |
---|
| 3094 | clipniter, |
---|
| 3095 | normalise_edge_param(edge), |
---|
| 3096 | threshold, |
---|
| 3097 | chan_avg_limit, getresidual, |
---|
| 3098 | pack_progress_params(showprogress, |
---|
| 3099 | minnrow), |
---|
[2641] | 3100 | outlog, scsvformat+blfile) |
---|
[2012] | 3101 | workscan._add_history("auto_cspline_baseline", varlist) |
---|
[1907] | 3102 | |
---|
[1856] | 3103 | if insitu: |
---|
| 3104 | self._assign(workscan) |
---|
| 3105 | else: |
---|
| 3106 | return workscan |
---|
[2012] | 3107 | |
---|
| 3108 | except RuntimeError, e: |
---|
[2186] | 3109 | raise_fitting_failure_exception(e) |
---|
[513] | 3110 | |
---|
[1931] | 3111 | @asaplog_post_dec |
---|
[2645] | 3112 | def chebyshev_baseline(self, insitu=None, mask=None, order=None, |
---|
| 3113 | clipthresh=None, clipniter=None, plot=None, |
---|
| 3114 | getresidual=None, showprogress=None, minnrow=None, |
---|
| 3115 | outlog=None, blfile=None, csvformat=None): |
---|
| 3116 | """\ |
---|
| 3117 | Return a scan which has been baselined (all rows) by Chebyshev polynomials. |
---|
| 3118 | |
---|
| 3119 | Parameters: |
---|
| 3120 | insitu: If False a new scantable is returned. |
---|
| 3121 | Otherwise, the scaling is done in-situ |
---|
| 3122 | The default is taken from .asaprc (False) |
---|
| 3123 | mask: An optional mask |
---|
| 3124 | order: the maximum order of Chebyshev polynomial (default is 5) |
---|
| 3125 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma) |
---|
| 3126 | clipniter: maximum number of iteration of 'clipthresh'-sigma |
---|
| 3127 | clipping (default is 0) |
---|
| 3128 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE *** |
---|
| 3129 | plot the fit and the residual. In this each |
---|
| 3130 | indivual fit has to be approved, by typing 'y' |
---|
| 3131 | or 'n' |
---|
| 3132 | getresidual: if False, returns best-fit values instead of |
---|
| 3133 | residual. (default is True) |
---|
| 3134 | showprogress: show progress status for large data. |
---|
| 3135 | default is True. |
---|
| 3136 | minnrow: minimum number of input spectra to show. |
---|
| 3137 | default is 1000. |
---|
| 3138 | outlog: Output the coefficients of the best-fit |
---|
| 3139 | function to logger (default is False) |
---|
| 3140 | blfile: Name of a text file in which the best-fit |
---|
| 3141 | parameter values to be written |
---|
| 3142 | (default is "": no file/logger output) |
---|
| 3143 | csvformat: if True blfile is csv-formatted, default is False. |
---|
| 3144 | |
---|
| 3145 | Example: |
---|
| 3146 | # return a scan baselined by a cubic spline consisting of 2 pieces |
---|
| 3147 | # (i.e., 1 internal knot), |
---|
| 3148 | # also with 3-sigma clipping, iteration up to 4 times |
---|
| 3149 | bscan = scan.cspline_baseline(npiece=2,clipthresh=3.0,clipniter=4) |
---|
| 3150 | |
---|
| 3151 | Note: |
---|
| 3152 | The best-fit parameter values output in logger and/or blfile are now |
---|
| 3153 | based on specunit of 'channel'. |
---|
| 3154 | """ |
---|
| 3155 | |
---|
| 3156 | try: |
---|
| 3157 | varlist = vars() |
---|
| 3158 | |
---|
| 3159 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 3160 | if insitu: |
---|
| 3161 | workscan = self |
---|
| 3162 | else: |
---|
| 3163 | workscan = self.copy() |
---|
| 3164 | |
---|
| 3165 | #if mask is None: mask = [True for i in xrange(workscan.nchan())] |
---|
| 3166 | if mask is None: mask = [] |
---|
| 3167 | if order is None: order = 5 |
---|
| 3168 | if clipthresh is None: clipthresh = 3.0 |
---|
| 3169 | if clipniter is None: clipniter = 0 |
---|
| 3170 | if plot is None: plot = False |
---|
| 3171 | if getresidual is None: getresidual = True |
---|
| 3172 | if showprogress is None: showprogress = True |
---|
| 3173 | if minnrow is None: minnrow = 1000 |
---|
| 3174 | if outlog is None: outlog = False |
---|
| 3175 | if blfile is None: blfile = '' |
---|
| 3176 | if csvformat is None: csvformat = False |
---|
| 3177 | |
---|
| 3178 | if csvformat: |
---|
| 3179 | scsvformat = "T" |
---|
| 3180 | else: |
---|
| 3181 | scsvformat = "F" |
---|
| 3182 | |
---|
| 3183 | #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method. |
---|
| 3184 | workscan._chebyshev_baseline(mask, order, clipthresh, clipniter, |
---|
| 3185 | getresidual, |
---|
| 3186 | pack_progress_params(showprogress, |
---|
| 3187 | minnrow), |
---|
| 3188 | outlog, scsvformat+blfile) |
---|
| 3189 | workscan._add_history("chebyshev_baseline", varlist) |
---|
| 3190 | |
---|
| 3191 | if insitu: |
---|
| 3192 | self._assign(workscan) |
---|
| 3193 | else: |
---|
| 3194 | return workscan |
---|
| 3195 | |
---|
| 3196 | except RuntimeError, e: |
---|
| 3197 | raise_fitting_failure_exception(e) |
---|
| 3198 | |
---|
| 3199 | @asaplog_post_dec |
---|
| 3200 | def auto_chebyshev_baseline(self, insitu=None, mask=None, order=None, |
---|
| 3201 | clipthresh=None, clipniter=None, |
---|
| 3202 | edge=None, threshold=None, chan_avg_limit=None, |
---|
| 3203 | getresidual=None, plot=None, |
---|
| 3204 | showprogress=None, minnrow=None, outlog=None, |
---|
| 3205 | blfile=None, csvformat=None): |
---|
| 3206 | """\ |
---|
| 3207 | Return a scan which has been baselined (all rows) by Chebyshev polynomials. |
---|
| 3208 | Spectral lines are detected first using linefinder and masked out |
---|
| 3209 | to avoid them affecting the baseline solution. |
---|
| 3210 | |
---|
| 3211 | Parameters: |
---|
| 3212 | insitu: if False a new scantable is returned. |
---|
| 3213 | Otherwise, the scaling is done in-situ |
---|
| 3214 | The default is taken from .asaprc (False) |
---|
| 3215 | mask: an optional mask retreived from scantable |
---|
| 3216 | order: the maximum order of Chebyshev polynomial (default is 5) |
---|
| 3217 | clipthresh: Clipping threshold. (default is 3.0, unit: sigma) |
---|
| 3218 | clipniter: maximum number of iteration of 'clipthresh'-sigma |
---|
| 3219 | clipping (default is 0) |
---|
| 3220 | edge: an optional number of channel to drop at |
---|
| 3221 | the edge of spectrum. If only one value is |
---|
| 3222 | specified, the same number will be dropped |
---|
| 3223 | from both sides of the spectrum. Default |
---|
| 3224 | is to keep all channels. Nested tuples |
---|
| 3225 | represent individual edge selection for |
---|
| 3226 | different IFs (a number of spectral channels |
---|
| 3227 | can be different) |
---|
| 3228 | threshold: the threshold used by line finder. It is |
---|
| 3229 | better to keep it large as only strong lines |
---|
| 3230 | affect the baseline solution. |
---|
| 3231 | chan_avg_limit: a maximum number of consequtive spectral |
---|
| 3232 | channels to average during the search of |
---|
| 3233 | weak and broad lines. The default is no |
---|
| 3234 | averaging (and no search for weak lines). |
---|
| 3235 | If such lines can affect the fitted baseline |
---|
| 3236 | (e.g. a high order polynomial is fitted), |
---|
| 3237 | increase this parameter (usually values up |
---|
| 3238 | to 8 are reasonable). Most users of this |
---|
| 3239 | method should find the default value sufficient. |
---|
| 3240 | plot: *** CURRENTLY UNAVAILABLE, ALWAYS FALSE *** |
---|
| 3241 | plot the fit and the residual. In this each |
---|
| 3242 | indivual fit has to be approved, by typing 'y' |
---|
| 3243 | or 'n' |
---|
| 3244 | getresidual: if False, returns best-fit values instead of |
---|
| 3245 | residual. (default is True) |
---|
| 3246 | showprogress: show progress status for large data. |
---|
| 3247 | default is True. |
---|
| 3248 | minnrow: minimum number of input spectra to show. |
---|
| 3249 | default is 1000. |
---|
| 3250 | outlog: Output the coefficients of the best-fit |
---|
| 3251 | function to logger (default is False) |
---|
| 3252 | blfile: Name of a text file in which the best-fit |
---|
| 3253 | parameter values to be written |
---|
| 3254 | (default is "": no file/logger output) |
---|
| 3255 | csvformat: if True blfile is csv-formatted, default is False. |
---|
| 3256 | |
---|
| 3257 | Example: |
---|
| 3258 | bscan = scan.auto_cspline_baseline(npiece=3, insitu=False) |
---|
| 3259 | |
---|
| 3260 | Note: |
---|
| 3261 | The best-fit parameter values output in logger and/or blfile are now |
---|
| 3262 | based on specunit of 'channel'. |
---|
| 3263 | """ |
---|
| 3264 | |
---|
| 3265 | try: |
---|
| 3266 | varlist = vars() |
---|
| 3267 | |
---|
| 3268 | if insitu is None: insitu = rcParams['insitu'] |
---|
| 3269 | if insitu: |
---|
| 3270 | workscan = self |
---|
| 3271 | else: |
---|
| 3272 | workscan = self.copy() |
---|
| 3273 | |
---|
| 3274 | #if mask is None: mask = [True for i in xrange(workscan.nchan())] |
---|
| 3275 | if mask is None: mask = [] |
---|
| 3276 | if order is None: order = 5 |
---|
| 3277 | if clipthresh is None: clipthresh = 3.0 |
---|
| 3278 | if clipniter is None: clipniter = 0 |
---|
| 3279 | if edge is None: edge = (0, 0) |
---|
| 3280 | if threshold is None: threshold = 3 |
---|
| 3281 | if chan_avg_limit is None: chan_avg_limit = 1 |
---|
| 3282 | if plot is None: plot = False |
---|
| 3283 | if getresidual is None: getresidual = True |
---|
| 3284 | if showprogress is None: showprogress = True |
---|
| 3285 | if minnrow is None: minnrow = 1000 |
---|
| 3286 | if outlog is None: outlog = False |
---|
| 3287 | if blfile is None: blfile = '' |
---|
| 3288 | if csvformat is None: csvformat = False |
---|
| 3289 | |
---|
| 3290 | if csvformat: |
---|
| 3291 | scsvformat = "T" |
---|
| 3292 | else: |
---|
| 3293 | scsvformat = "F" |
---|
| 3294 | |
---|
| 3295 | #CURRENTLY, PLOT=true UNAVAILABLE UNTIL cubic spline fitting is implemented as a fitter method. |
---|
| 3296 | workscan._auto_chebyshev_baseline(mask, order, clipthresh, |
---|
| 3297 | clipniter, |
---|
| 3298 | normalise_edge_param(edge), |
---|
| 3299 | threshold, |
---|
| 3300 | chan_avg_limit, getresidual, |
---|
| 3301 | pack_progress_params(showprogress, |
---|
| 3302 | minnrow), |
---|
| 3303 | outlog, scsvformat+blfile) |
---|
| 3304 | workscan._add_history("auto_chebyshev_baseline", varlist) |
---|
| 3305 | |
---|
| 3306 | if insitu: |
---|
| 3307 | self._assign(workscan) |
---|
| 3308 | else: |
---|
| 3309 | return workscan |
---|
| 3310 | |
---|
| 3311 | except RuntimeError, e: |
---|
| 3312 | raise_fitting_failure_exception(e) |
---|
| 3313 | |
---|
| 3314 | @asaplog_post_dec |
---|
[2269] | 3315 | def poly_baseline(self, mask=None, order=None, insitu=None, plot=None, |
---|
| 3316 | getresidual=None, showprogress=None, minnrow=None, |
---|
[2641] | 3317 | outlog=None, blfile=None, csvformat=None): |
---|
[1907] | 3318 | """\ |
---|
| 3319 | Return a scan which has been baselined (all rows) by a polynomial. |
---|
| 3320 | Parameters: |
---|
[2189] | 3321 | insitu: if False a new scantable is returned. |
---|
| 3322 | Otherwise, the scaling is done in-situ |
---|
| 3323 | The default is taken from .asaprc (False) |
---|
| 3324 | mask: an optional mask |
---|
| 3325 | order: the order of the polynomial (default is 0) |
---|
| 3326 | plot: plot the fit and the residual. In this each |
---|
| 3327 | indivual fit has to be approved, by typing 'y' |
---|
| 3328 | or 'n' |
---|
| 3329 | getresidual: if False, returns best-fit values instead of |
---|
| 3330 | residual. (default is True) |
---|
| 3331 | showprogress: show progress status for large data. |
---|
| 3332 | default is True. |
---|
| 3333 | minnrow: minimum number of input spectra to show. |
---|
| 3334 | default is 1000. |
---|
| 3335 | outlog: Output the coefficients of the best-fit |
---|
| 3336 | function to logger (default is False) |
---|
| 3337 | blfile: Name of a text file in which the best-fit |
---|
| 3338 | parameter values to be written |
---|
| 3339 | (default is "": no file/logger output) |
---|
[2641] | 3340 | csvformat: if True blfile is csv-formatted, default is False. |
---|
[2012] | 3341 | |
---|
[1907] | 3342 | Example: |
---|
| 3343 | # return a scan baselined by a third order polynomial, |
---|
| 3344 | # not using a mask |
---|
| 3345 | bscan = scan.poly_baseline(order=3) |
---|
| 3346 | """ |
---|
[1931] | 3347 | |
---|
[2186] | 3348 | try: |
---|
| 3349 | varlist = vars() |
---|
[1931] | 3350 | |
---|
[2269] | 3351 | if insitu is None: |
---|
| 3352 | insitu = rcParams["insitu"] |
---|
[2186] | 3353 | if insitu: |
---|
| 3354 | workscan = self |
---|
| 3355 | else: |
---|
| 3356 | workscan = self.copy() |
---|
[1907] | 3357 | |
---|
[2410] | 3358 | #if mask is None: mask = [True for i in \ |
---|
| 3359 | # xrange(workscan.nchan())] |
---|
| 3360 | if mask is None: mask = [] |
---|
[2189] | 3361 | if order is None: order = 0 |
---|
| 3362 | if plot is None: plot = False |
---|
| 3363 | if getresidual is None: getresidual = True |
---|
| 3364 | if showprogress is None: showprogress = True |
---|
| 3365 | if minnrow is None: minnrow = 1000 |
---|
| 3366 | if outlog is None: outlog = False |
---|
| 3367 | if blfile is None: blfile = "" |
---|
[2641] | 3368 | if csvformat is None: csvformat = False |
---|
[1907] | 3369 | |
---|
[2641] | 3370 | if csvformat: |
---|
| 3371 | scsvformat = "T" |
---|
| 3372 | else: |
---|
| 3373 | scsvformat = "F" |
---|
| 3374 | |
---|
[2012] | 3375 | if plot: |
---|
[2269] | 3376 | outblfile = (blfile != "") and \ |
---|
[2349] | 3377 | os.path.exists(os.path.expanduser( |
---|
| 3378 | os.path.expandvars(blfile)) |
---|
| 3379 | ) |
---|
[2269] | 3380 | if outblfile: |
---|
| 3381 | blf = open(blfile, "a") |
---|
[2012] | 3382 | |
---|
[1907] | 3383 | f = fitter() |
---|
| 3384 | f.set_function(lpoly=order) |
---|
[2186] | 3385 | |
---|
| 3386 | rows = xrange(workscan.nrow()) |
---|
| 3387 | #if len(rows) > 0: workscan._init_blinfo() |
---|
[2610] | 3388 | |
---|
| 3389 | action = "H" |
---|
[1907] | 3390 | for r in rows: |
---|
| 3391 | f.x = workscan._getabcissa(r) |
---|
| 3392 | f.y = workscan._getspectrum(r) |
---|
[2541] | 3393 | if mask: |
---|
| 3394 | f.mask = mask_and(mask, workscan._getmask(r)) # (CAS-1434) |
---|
| 3395 | else: # mask=None |
---|
| 3396 | f.mask = workscan._getmask(r) |
---|
| 3397 | |
---|
[1907] | 3398 | f.data = None |
---|
| 3399 | f.fit() |
---|
[2541] | 3400 | |
---|
[2610] | 3401 | if action != "Y": # skip plotting when accepting all |
---|
| 3402 | f.plot(residual=True) |
---|
| 3403 | #accept_fit = raw_input("Accept fit ( [y]/n ): ") |
---|
| 3404 | #if accept_fit.upper() == "N": |
---|
| 3405 | # #workscan._append_blinfo(None, None, None) |
---|
| 3406 | # continue |
---|
| 3407 | accept_fit = self._get_verify_action("Accept fit?",action) |
---|
| 3408 | if r == 0: action = None |
---|
[1907] | 3409 | if accept_fit.upper() == "N": |
---|
| 3410 | continue |
---|
[2610] | 3411 | elif accept_fit.upper() == "R": |
---|
| 3412 | break |
---|
| 3413 | elif accept_fit.upper() == "A": |
---|
| 3414 | action = "Y" |
---|
[2012] | 3415 | |
---|
| 3416 | blpars = f.get_parameters() |
---|
| 3417 | masklist = workscan.get_masklist(f.mask, row=r, silent=True) |
---|
| 3418 | #workscan._append_blinfo(blpars, masklist, f.mask) |
---|
[2269] | 3419 | workscan._setspectrum((f.fitter.getresidual() |
---|
| 3420 | if getresidual else f.fitter.getfit()), r) |
---|
[1907] | 3421 | |
---|
[2012] | 3422 | if outblfile: |
---|
| 3423 | rms = workscan.get_rms(f.mask, r) |
---|
[2269] | 3424 | dataout = \ |
---|
| 3425 | workscan.format_blparams_row(blpars["params"], |
---|
| 3426 | blpars["fixed"], |
---|
| 3427 | rms, str(masklist), |
---|
[2641] | 3428 | r, True, csvformat) |
---|
[2012] | 3429 | blf.write(dataout) |
---|
| 3430 | |
---|
[1907] | 3431 | f._p.unmap() |
---|
| 3432 | f._p = None |
---|
[2012] | 3433 | |
---|
[2349] | 3434 | if outblfile: |
---|
| 3435 | blf.close() |
---|
[1907] | 3436 | else: |
---|
[2269] | 3437 | workscan._poly_baseline(mask, order, getresidual, |
---|
| 3438 | pack_progress_params(showprogress, |
---|
| 3439 | minnrow), |
---|
[2641] | 3440 | outlog, scsvformat+blfile) |
---|
[1907] | 3441 | |
---|
| 3442 | workscan._add_history("poly_baseline", varlist) |
---|
| 3443 | |
---|
| 3444 | if insitu: |
---|
| 3445 | self._assign(workscan) |
---|
| 3446 | else: |
---|
| 3447 | return workscan |
---|
| 3448 | |
---|
[1919] | 3449 | except RuntimeError, e: |
---|
[2186] | 3450 | raise_fitting_failure_exception(e) |
---|
[1907] | 3451 | |
---|
[2186] | 3452 | @asaplog_post_dec |
---|
[2269] | 3453 | def auto_poly_baseline(self, mask=None, order=None, edge=None, |
---|
| 3454 | threshold=None, chan_avg_limit=None, |
---|
| 3455 | plot=None, insitu=None, |
---|
| 3456 | getresidual=None, showprogress=None, |
---|
[2641] | 3457 | minnrow=None, outlog=None, blfile=None, csvformat=None): |
---|
[1846] | 3458 | """\ |
---|
[1931] | 3459 | Return a scan which has been baselined (all rows) by a polynomial. |
---|
[880] | 3460 | Spectral lines are detected first using linefinder and masked out |
---|
| 3461 | to avoid them affecting the baseline solution. |
---|
| 3462 | |
---|
| 3463 | Parameters: |
---|
[2189] | 3464 | insitu: if False a new scantable is returned. |
---|
| 3465 | Otherwise, the scaling is done in-situ |
---|
| 3466 | The default is taken from .asaprc (False) |
---|
| 3467 | mask: an optional mask retreived from scantable |
---|
| 3468 | order: the order of the polynomial (default is 0) |
---|
| 3469 | edge: an optional number of channel to drop at |
---|
| 3470 | the edge of spectrum. If only one value is |
---|
| 3471 | specified, the same number will be dropped |
---|
| 3472 | from both sides of the spectrum. Default |
---|
| 3473 | is to keep all channels. Nested tuples |
---|
| 3474 | represent individual edge selection for |
---|
| 3475 | different IFs (a number of spectral channels |
---|
| 3476 | can be different) |
---|
| 3477 | threshold: the threshold used by line finder. It is |
---|
| 3478 | better to keep it large as only strong lines |
---|
| 3479 | affect the baseline solution. |
---|
| 3480 | chan_avg_limit: a maximum number of consequtive spectral |
---|
| 3481 | channels to average during the search of |
---|
| 3482 | weak and broad lines. The default is no |
---|
| 3483 | averaging (and no search for weak lines). |
---|
| 3484 | If such lines can affect the fitted baseline |
---|
| 3485 | (e.g. a high order polynomial is fitted), |
---|
| 3486 | increase this parameter (usually values up |
---|
| 3487 | to 8 are reasonable). Most users of this |
---|
| 3488 | method should find the default value sufficient. |
---|
| 3489 | plot: plot the fit and the residual. In this each |
---|
| 3490 | indivual fit has to be approved, by typing 'y' |
---|
| 3491 | or 'n' |
---|
| 3492 | getresidual: if False, returns best-fit values instead of |
---|
| 3493 | residual. (default is True) |
---|
| 3494 | showprogress: show progress status for large data. |
---|
| 3495 | default is True. |
---|
| 3496 | minnrow: minimum number of input spectra to show. |
---|
| 3497 | default is 1000. |
---|
| 3498 | outlog: Output the coefficients of the best-fit |
---|
| 3499 | function to logger (default is False) |
---|
| 3500 | blfile: Name of a text file in which the best-fit |
---|
| 3501 | parameter values to be written |
---|
| 3502 | (default is "": no file/logger output) |
---|
[2641] | 3503 | csvformat: if True blfile is csv-formatted, default is False. |
---|
[1846] | 3504 | |
---|
[2012] | 3505 | Example: |
---|
| 3506 | bscan = scan.auto_poly_baseline(order=7, insitu=False) |
---|
| 3507 | """ |
---|
[880] | 3508 | |
---|
[2186] | 3509 | try: |
---|
| 3510 | varlist = vars() |
---|
[1846] | 3511 | |
---|
[2269] | 3512 | if insitu is None: |
---|
| 3513 | insitu = rcParams['insitu'] |
---|
[2186] | 3514 | if insitu: |
---|
| 3515 | workscan = self |
---|
| 3516 | else: |
---|
| 3517 | workscan = self.copy() |
---|
[1846] | 3518 | |
---|
[2410] | 3519 | #if mask is None: mask = [True for i in xrange(workscan.nchan())] |
---|
| 3520 | if mask is None: mask = [] |
---|
[2186] | 3521 | if order is None: order = 0 |
---|
| 3522 | if edge is None: edge = (0, 0) |
---|
| 3523 | if threshold is None: threshold = 3 |
---|
| 3524 | if chan_avg_limit is None: chan_avg_limit = 1 |
---|
| 3525 | if plot is None: plot = False |
---|
| 3526 | if getresidual is None: getresidual = True |
---|
[2189] | 3527 | if showprogress is None: showprogress = True |
---|
| 3528 | if minnrow is None: minnrow = 1000 |
---|
[2186] | 3529 | if outlog is None: outlog = False |
---|
| 3530 | if blfile is None: blfile = '' |
---|
[2641] | 3531 | if csvformat is None: csvformat = False |
---|
[1846] | 3532 | |
---|
[2641] | 3533 | if csvformat: |
---|
| 3534 | scsvformat = "T" |
---|
| 3535 | else: |
---|
| 3536 | scsvformat = "F" |
---|
| 3537 | |
---|
[2186] | 3538 | edge = normalise_edge_param(edge) |
---|
[880] | 3539 | |
---|
[2012] | 3540 | if plot: |
---|
[2269] | 3541 | outblfile = (blfile != "") and \ |
---|
| 3542 | os.path.exists(os.path.expanduser(os.path.expandvars(blfile))) |
---|
[2012] | 3543 | if outblfile: blf = open(blfile, "a") |
---|
| 3544 | |
---|
[2186] | 3545 | from asap.asaplinefind import linefinder |
---|
[2012] | 3546 | fl = linefinder() |
---|
[2269] | 3547 | fl.set_options(threshold=threshold, avg_limit=chan_avg_limit) |
---|
[2012] | 3548 | fl.set_scan(workscan) |
---|
[2186] | 3549 | |
---|
[2012] | 3550 | f = fitter() |
---|
| 3551 | f.set_function(lpoly=order) |
---|
[880] | 3552 | |
---|
[2186] | 3553 | rows = xrange(workscan.nrow()) |
---|
| 3554 | #if len(rows) > 0: workscan._init_blinfo() |
---|
[2610] | 3555 | |
---|
| 3556 | action = "H" |
---|
[2012] | 3557 | for r in rows: |
---|
[2186] | 3558 | idx = 2*workscan.getif(r) |
---|
[2541] | 3559 | if mask: |
---|
| 3560 | msk = mask_and(mask, workscan._getmask(r)) # (CAS-1434) |
---|
| 3561 | else: # mask=None |
---|
| 3562 | msk = workscan._getmask(r) |
---|
| 3563 | fl.find_lines(r, msk, edge[idx:idx+2]) |
---|
[907] | 3564 | |
---|
[2012] | 3565 | f.x = workscan._getabcissa(r) |
---|
| 3566 | f.y = workscan._getspectrum(r) |
---|
| 3567 | f.mask = fl.get_mask() |
---|
| 3568 | f.data = None |
---|
| 3569 | f.fit() |
---|
| 3570 | |
---|
[2610] | 3571 | if action != "Y": # skip plotting when accepting all |
---|
| 3572 | f.plot(residual=True) |
---|
| 3573 | #accept_fit = raw_input("Accept fit ( [y]/n ): ") |
---|
| 3574 | accept_fit = self._get_verify_action("Accept fit?",action) |
---|
| 3575 | if r == 0: action = None |
---|
[2012] | 3576 | if accept_fit.upper() == "N": |
---|
| 3577 | #workscan._append_blinfo(None, None, None) |
---|
| 3578 | continue |
---|
[2610] | 3579 | elif accept_fit.upper() == "R": |
---|
| 3580 | break |
---|
| 3581 | elif accept_fit.upper() == "A": |
---|
| 3582 | action = "Y" |
---|
[2012] | 3583 | |
---|
| 3584 | blpars = f.get_parameters() |
---|
| 3585 | masklist = workscan.get_masklist(f.mask, row=r, silent=True) |
---|
| 3586 | #workscan._append_blinfo(blpars, masklist, f.mask) |
---|
[2349] | 3587 | workscan._setspectrum( |
---|
| 3588 | (f.fitter.getresidual() if getresidual |
---|
| 3589 | else f.fitter.getfit()), r |
---|
| 3590 | ) |
---|
[2012] | 3591 | |
---|
| 3592 | if outblfile: |
---|
| 3593 | rms = workscan.get_rms(f.mask, r) |
---|
[2269] | 3594 | dataout = \ |
---|
| 3595 | workscan.format_blparams_row(blpars["params"], |
---|
| 3596 | blpars["fixed"], |
---|
| 3597 | rms, str(masklist), |
---|
[2641] | 3598 | r, True, csvformat) |
---|
[2012] | 3599 | blf.write(dataout) |
---|
| 3600 | |
---|
| 3601 | f._p.unmap() |
---|
| 3602 | f._p = None |
---|
| 3603 | |
---|
| 3604 | if outblfile: blf.close() |
---|
| 3605 | else: |
---|
[2269] | 3606 | workscan._auto_poly_baseline(mask, order, edge, threshold, |
---|
| 3607 | chan_avg_limit, getresidual, |
---|
| 3608 | pack_progress_params(showprogress, |
---|
| 3609 | minnrow), |
---|
[2641] | 3610 | outlog, scsvformat+blfile) |
---|
[2012] | 3611 | |
---|
| 3612 | workscan._add_history("auto_poly_baseline", varlist) |
---|
| 3613 | |
---|
| 3614 | if insitu: |
---|
| 3615 | self._assign(workscan) |
---|
| 3616 | else: |
---|
| 3617 | return workscan |
---|
| 3618 | |
---|
| 3619 | except RuntimeError, e: |
---|
[2186] | 3620 | raise_fitting_failure_exception(e) |
---|
[2012] | 3621 | |
---|
| 3622 | def _init_blinfo(self): |
---|
| 3623 | """\ |
---|
| 3624 | Initialise the following three auxiliary members: |
---|
| 3625 | blpars : parameters of the best-fit baseline, |
---|
| 3626 | masklists : mask data (edge positions of masked channels) and |
---|
| 3627 | actualmask : mask data (in boolean list), |
---|
| 3628 | to keep for use later (including output to logger/text files). |
---|
| 3629 | Used by poly_baseline() and auto_poly_baseline() in case of |
---|
| 3630 | 'plot=True'. |
---|
| 3631 | """ |
---|
| 3632 | self.blpars = [] |
---|
| 3633 | self.masklists = [] |
---|
| 3634 | self.actualmask = [] |
---|
| 3635 | return |
---|
[880] | 3636 | |
---|
[2012] | 3637 | def _append_blinfo(self, data_blpars, data_masklists, data_actualmask): |
---|
| 3638 | """\ |
---|
| 3639 | Append baseline-fitting related info to blpars, masklist and |
---|
| 3640 | actualmask. |
---|
| 3641 | """ |
---|
| 3642 | self.blpars.append(data_blpars) |
---|
| 3643 | self.masklists.append(data_masklists) |
---|
| 3644 | self.actualmask.append(data_actualmask) |
---|
| 3645 | return |
---|
| 3646 | |
---|
[1862] | 3647 | @asaplog_post_dec |
---|
[914] | 3648 | def rotate_linpolphase(self, angle): |
---|
[1846] | 3649 | """\ |
---|
[914] | 3650 | Rotate the phase of the complex polarization O=Q+iU correlation. |
---|
| 3651 | This is always done in situ in the raw data. So if you call this |
---|
| 3652 | function more than once then each call rotates the phase further. |
---|
[1846] | 3653 | |
---|
[914] | 3654 | Parameters: |
---|
[1846] | 3655 | |
---|
[914] | 3656 | angle: The angle (degrees) to rotate (add) by. |
---|
[1846] | 3657 | |
---|
| 3658 | Example:: |
---|
| 3659 | |
---|
[914] | 3660 | scan.rotate_linpolphase(2.3) |
---|
[1846] | 3661 | |
---|
[914] | 3662 | """ |
---|
| 3663 | varlist = vars() |
---|
[936] | 3664 | self._math._rotate_linpolphase(self, angle) |
---|
[914] | 3665 | self._add_history("rotate_linpolphase", varlist) |
---|
| 3666 | return |
---|
[710] | 3667 | |
---|
[1862] | 3668 | @asaplog_post_dec |
---|
[914] | 3669 | def rotate_xyphase(self, angle): |
---|
[1846] | 3670 | """\ |
---|
[914] | 3671 | Rotate the phase of the XY correlation. This is always done in situ |
---|
| 3672 | in the data. So if you call this function more than once |
---|
| 3673 | then each call rotates the phase further. |
---|
[1846] | 3674 | |
---|
[914] | 3675 | Parameters: |
---|
[1846] | 3676 | |
---|
[914] | 3677 | angle: The angle (degrees) to rotate (add) by. |
---|
[1846] | 3678 | |
---|
| 3679 | Example:: |
---|
| 3680 | |
---|
[914] | 3681 | scan.rotate_xyphase(2.3) |
---|
[1846] | 3682 | |
---|
[914] | 3683 | """ |
---|
| 3684 | varlist = vars() |
---|
[936] | 3685 | self._math._rotate_xyphase(self, angle) |
---|
[914] | 3686 | self._add_history("rotate_xyphase", varlist) |
---|
| 3687 | return |
---|
| 3688 | |
---|
[1862] | 3689 | @asaplog_post_dec |
---|
[914] | 3690 | def swap_linears(self): |
---|
[1846] | 3691 | """\ |
---|
[1573] | 3692 | Swap the linear polarisations XX and YY, or better the first two |
---|
[1348] | 3693 | polarisations as this also works for ciculars. |
---|
[914] | 3694 | """ |
---|
| 3695 | varlist = vars() |
---|
[936] | 3696 | self._math._swap_linears(self) |
---|
[914] | 3697 | self._add_history("swap_linears", varlist) |
---|
| 3698 | return |
---|
| 3699 | |
---|
[1862] | 3700 | @asaplog_post_dec |
---|
[914] | 3701 | def invert_phase(self): |
---|
[1846] | 3702 | """\ |
---|
[914] | 3703 | Invert the phase of the complex polarisation |
---|
| 3704 | """ |
---|
| 3705 | varlist = vars() |
---|
[936] | 3706 | self._math._invert_phase(self) |
---|
[914] | 3707 | self._add_history("invert_phase", varlist) |
---|
| 3708 | return |
---|
| 3709 | |
---|
[1862] | 3710 | @asaplog_post_dec |
---|
[876] | 3711 | def add(self, offset, insitu=None): |
---|
[1846] | 3712 | """\ |
---|
[513] | 3713 | Return a scan where all spectra have the offset added |
---|
[1846] | 3714 | |
---|
[513] | 3715 | Parameters: |
---|
[1846] | 3716 | |
---|
[513] | 3717 | offset: the offset |
---|
[1855] | 3718 | |
---|
[513] | 3719 | insitu: if False a new scantable is returned. |
---|
| 3720 | Otherwise, the scaling is done in-situ |
---|
| 3721 | The default is taken from .asaprc (False) |
---|
[1846] | 3722 | |
---|
[513] | 3723 | """ |
---|
| 3724 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 3725 | self._math._setinsitu(insitu) |
---|
[513] | 3726 | varlist = vars() |
---|
[876] | 3727 | s = scantable(self._math._unaryop(self, offset, "ADD", False)) |
---|
[1118] | 3728 | s._add_history("add", varlist) |
---|
[876] | 3729 | if insitu: |
---|
| 3730 | self._assign(s) |
---|
| 3731 | else: |
---|
[513] | 3732 | return s |
---|
| 3733 | |
---|
[1862] | 3734 | @asaplog_post_dec |
---|
[1308] | 3735 | def scale(self, factor, tsys=True, insitu=None): |
---|
[1846] | 3736 | """\ |
---|
| 3737 | |
---|
[1938] | 3738 | Return a scan where all spectra are scaled by the given 'factor' |
---|
[1846] | 3739 | |
---|
[513] | 3740 | Parameters: |
---|
[1846] | 3741 | |
---|
[1819] | 3742 | factor: the scaling factor (float or 1D float list) |
---|
[1855] | 3743 | |
---|
[513] | 3744 | insitu: if False a new scantable is returned. |
---|
| 3745 | Otherwise, the scaling is done in-situ |
---|
| 3746 | The default is taken from .asaprc (False) |
---|
[1855] | 3747 | |
---|
[513] | 3748 | tsys: if True (default) then apply the operation to Tsys |
---|
| 3749 | as well as the data |
---|
[1846] | 3750 | |
---|
[513] | 3751 | """ |
---|
| 3752 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 3753 | self._math._setinsitu(insitu) |
---|
[513] | 3754 | varlist = vars() |
---|
[1819] | 3755 | s = None |
---|
| 3756 | import numpy |
---|
| 3757 | if isinstance(factor, list) or isinstance(factor, numpy.ndarray): |
---|
[2320] | 3758 | if isinstance(factor[0], list) or isinstance(factor[0], |
---|
| 3759 | numpy.ndarray): |
---|
[1819] | 3760 | from asapmath import _array2dOp |
---|
[2320] | 3761 | s = _array2dOp( self, factor, "MUL", tsys, insitu ) |
---|
[1819] | 3762 | else: |
---|
[2320] | 3763 | s = scantable( self._math._arrayop( self, factor, |
---|
| 3764 | "MUL", tsys ) ) |
---|
[1819] | 3765 | else: |
---|
[2320] | 3766 | s = scantable(self._math._unaryop(self, factor, "MUL", tsys)) |
---|
[1118] | 3767 | s._add_history("scale", varlist) |
---|
[876] | 3768 | if insitu: |
---|
| 3769 | self._assign(s) |
---|
| 3770 | else: |
---|
[513] | 3771 | return s |
---|
| 3772 | |
---|
[2349] | 3773 | @preserve_selection |
---|
| 3774 | def set_sourcetype(self, match, matchtype="pattern", |
---|
[1504] | 3775 | sourcetype="reference"): |
---|
[1846] | 3776 | """\ |
---|
[1502] | 3777 | Set the type of the source to be an source or reference scan |
---|
[1846] | 3778 | using the provided pattern. |
---|
| 3779 | |
---|
[1502] | 3780 | Parameters: |
---|
[1846] | 3781 | |
---|
[1504] | 3782 | match: a Unix style pattern, regular expression or selector |
---|
[1855] | 3783 | |
---|
[1504] | 3784 | matchtype: 'pattern' (default) UNIX style pattern or |
---|
| 3785 | 'regex' regular expression |
---|
[1855] | 3786 | |
---|
[1502] | 3787 | sourcetype: the type of the source to use (source/reference) |
---|
[1846] | 3788 | |
---|
[1502] | 3789 | """ |
---|
| 3790 | varlist = vars() |
---|
| 3791 | stype = -1 |
---|
[2480] | 3792 | if sourcetype.lower().startswith("r") or sourcetype.lower() == "off": |
---|
[1502] | 3793 | stype = 1 |
---|
[2480] | 3794 | elif sourcetype.lower().startswith("s") or sourcetype.lower() == "on": |
---|
[1502] | 3795 | stype = 0 |
---|
[1504] | 3796 | else: |
---|
[2480] | 3797 | raise ValueError("Illegal sourcetype use s(ource)/on or r(eference)/off") |
---|
[1504] | 3798 | if matchtype.lower().startswith("p"): |
---|
| 3799 | matchtype = "pattern" |
---|
| 3800 | elif matchtype.lower().startswith("r"): |
---|
| 3801 | matchtype = "regex" |
---|
| 3802 | else: |
---|
| 3803 | raise ValueError("Illegal matchtype, use p(attern) or r(egex)") |
---|
[1502] | 3804 | sel = selector() |
---|
| 3805 | if isinstance(match, selector): |
---|
| 3806 | sel = match |
---|
| 3807 | else: |
---|
[2480] | 3808 | sel.set_query("SRCNAME=%s('%s')" % (matchtype, match)) |
---|
| 3809 | self.set_selection(sel) |
---|
[1502] | 3810 | self._setsourcetype(stype) |
---|
[1573] | 3811 | self._add_history("set_sourcetype", varlist) |
---|
[1502] | 3812 | |
---|
[1862] | 3813 | @asaplog_post_dec |
---|
[1857] | 3814 | @preserve_selection |
---|
[1819] | 3815 | def auto_quotient(self, preserve=True, mode='paired', verify=False): |
---|
[1846] | 3816 | """\ |
---|
[670] | 3817 | This function allows to build quotients automatically. |
---|
[1819] | 3818 | It assumes the observation to have the same number of |
---|
[670] | 3819 | "ons" and "offs" |
---|
[1846] | 3820 | |
---|
[670] | 3821 | Parameters: |
---|
[1846] | 3822 | |
---|
[710] | 3823 | preserve: you can preserve (default) the continuum or |
---|
| 3824 | remove it. The equations used are |
---|
[1857] | 3825 | |
---|
[670] | 3826 | preserve: Output = Toff * (on/off) - Toff |
---|
[1857] | 3827 | |
---|
[1070] | 3828 | remove: Output = Toff * (on/off) - Ton |
---|
[1855] | 3829 | |
---|
[1573] | 3830 | mode: the on/off detection mode |
---|
[1348] | 3831 | 'paired' (default) |
---|
| 3832 | identifies 'off' scans by the |
---|
| 3833 | trailing '_R' (Mopra/Parkes) or |
---|
| 3834 | '_e'/'_w' (Tid) and matches |
---|
| 3835 | on/off pairs from the observing pattern |
---|
[1502] | 3836 | 'time' |
---|
| 3837 | finds the closest off in time |
---|
[1348] | 3838 | |
---|
[1857] | 3839 | .. todo:: verify argument is not implemented |
---|
| 3840 | |
---|
[670] | 3841 | """ |
---|
[1857] | 3842 | varlist = vars() |
---|
[1348] | 3843 | modes = ["time", "paired"] |
---|
[670] | 3844 | if not mode in modes: |
---|
[876] | 3845 | msg = "please provide valid mode. Valid modes are %s" % (modes) |
---|
| 3846 | raise ValueError(msg) |
---|
[1348] | 3847 | s = None |
---|
| 3848 | if mode.lower() == "paired": |
---|
[1857] | 3849 | sel = self.get_selection() |
---|
[1875] | 3850 | sel.set_query("SRCTYPE==psoff") |
---|
[1356] | 3851 | self.set_selection(sel) |
---|
[1348] | 3852 | offs = self.copy() |
---|
[1875] | 3853 | sel.set_query("SRCTYPE==pson") |
---|
[1356] | 3854 | self.set_selection(sel) |
---|
[1348] | 3855 | ons = self.copy() |
---|
| 3856 | s = scantable(self._math._quotient(ons, offs, preserve)) |
---|
| 3857 | elif mode.lower() == "time": |
---|
| 3858 | s = scantable(self._math._auto_quotient(self, mode, preserve)) |
---|
[1118] | 3859 | s._add_history("auto_quotient", varlist) |
---|
[876] | 3860 | return s |
---|
[710] | 3861 | |
---|
[1862] | 3862 | @asaplog_post_dec |
---|
[1145] | 3863 | def mx_quotient(self, mask = None, weight='median', preserve=True): |
---|
[1846] | 3864 | """\ |
---|
[1143] | 3865 | Form a quotient using "off" beams when observing in "MX" mode. |
---|
[1846] | 3866 | |
---|
[1143] | 3867 | Parameters: |
---|
[1846] | 3868 | |
---|
[1145] | 3869 | mask: an optional mask to be used when weight == 'stddev' |
---|
[1855] | 3870 | |
---|
[1143] | 3871 | weight: How to average the off beams. Default is 'median'. |
---|
[1855] | 3872 | |
---|
[1145] | 3873 | preserve: you can preserve (default) the continuum or |
---|
[1855] | 3874 | remove it. The equations used are: |
---|
[1846] | 3875 | |
---|
[1855] | 3876 | preserve: Output = Toff * (on/off) - Toff |
---|
| 3877 | |
---|
| 3878 | remove: Output = Toff * (on/off) - Ton |
---|
| 3879 | |
---|
[1217] | 3880 | """ |
---|
[1593] | 3881 | mask = mask or () |
---|
[1141] | 3882 | varlist = vars() |
---|
| 3883 | on = scantable(self._math._mx_extract(self, 'on')) |
---|
[1143] | 3884 | preoff = scantable(self._math._mx_extract(self, 'off')) |
---|
| 3885 | off = preoff.average_time(mask=mask, weight=weight, scanav=False) |
---|
[1217] | 3886 | from asapmath import quotient |
---|
[1145] | 3887 | q = quotient(on, off, preserve) |
---|
[1143] | 3888 | q._add_history("mx_quotient", varlist) |
---|
[1217] | 3889 | return q |
---|
[513] | 3890 | |
---|
[1862] | 3891 | @asaplog_post_dec |
---|
[718] | 3892 | def freq_switch(self, insitu=None): |
---|
[1846] | 3893 | """\ |
---|
[718] | 3894 | Apply frequency switching to the data. |
---|
[1846] | 3895 | |
---|
[718] | 3896 | Parameters: |
---|
[1846] | 3897 | |
---|
[718] | 3898 | insitu: if False a new scantable is returned. |
---|
| 3899 | Otherwise, the swictching is done in-situ |
---|
| 3900 | The default is taken from .asaprc (False) |
---|
[1846] | 3901 | |
---|
[718] | 3902 | """ |
---|
| 3903 | if insitu is None: insitu = rcParams['insitu'] |
---|
[876] | 3904 | self._math._setinsitu(insitu) |
---|
[718] | 3905 | varlist = vars() |
---|
[876] | 3906 | s = scantable(self._math._freqswitch(self)) |
---|
[1118] | 3907 | s._add_history("freq_switch", varlist) |
---|
[1856] | 3908 | if insitu: |
---|
| 3909 | self._assign(s) |
---|
| 3910 | else: |
---|
| 3911 | return s |
---|
[718] | 3912 | |
---|
[1862] | 3913 | @asaplog_post_dec |
---|
[780] | 3914 | def recalc_azel(self): |
---|
[1846] | 3915 | """Recalculate the azimuth and elevation for each position.""" |
---|
[780] | 3916 | varlist = vars() |
---|
[876] | 3917 | self._recalcazel() |
---|
[780] | 3918 | self._add_history("recalc_azel", varlist) |
---|
| 3919 | return |
---|
| 3920 | |
---|
[1862] | 3921 | @asaplog_post_dec |
---|
[513] | 3922 | def __add__(self, other): |
---|
[2574] | 3923 | """ |
---|
| 3924 | implicit on all axes and on Tsys |
---|
| 3925 | """ |
---|
[513] | 3926 | varlist = vars() |
---|
[2574] | 3927 | s = self.__op( other, "ADD" ) |
---|
[513] | 3928 | s._add_history("operator +", varlist) |
---|
| 3929 | return s |
---|
| 3930 | |
---|
[1862] | 3931 | @asaplog_post_dec |
---|
[513] | 3932 | def __sub__(self, other): |
---|
| 3933 | """ |
---|
| 3934 | implicit on all axes and on Tsys |
---|
| 3935 | """ |
---|
| 3936 | varlist = vars() |
---|
[2574] | 3937 | s = self.__op( other, "SUB" ) |
---|
[513] | 3938 | s._add_history("operator -", varlist) |
---|
| 3939 | return s |
---|
[710] | 3940 | |
---|
[1862] | 3941 | @asaplog_post_dec |
---|
[513] | 3942 | def __mul__(self, other): |
---|
| 3943 | """ |
---|
| 3944 | implicit on all axes and on Tsys |
---|
| 3945 | """ |
---|
| 3946 | varlist = vars() |
---|
[2574] | 3947 | s = self.__op( other, "MUL" ) ; |
---|
[513] | 3948 | s._add_history("operator *", varlist) |
---|
| 3949 | return s |
---|
| 3950 | |
---|
[710] | 3951 | |
---|
[1862] | 3952 | @asaplog_post_dec |
---|
[513] | 3953 | def __div__(self, other): |
---|
| 3954 | """ |
---|
| 3955 | implicit on all axes and on Tsys |
---|
| 3956 | """ |
---|
| 3957 | varlist = vars() |
---|
[2574] | 3958 | s = self.__op( other, "DIV" ) |
---|
| 3959 | s._add_history("operator /", varlist) |
---|
| 3960 | return s |
---|
| 3961 | |
---|
| 3962 | @asaplog_post_dec |
---|
| 3963 | def __op( self, other, mode ): |
---|
[513] | 3964 | s = None |
---|
| 3965 | if isinstance(other, scantable): |
---|
[2574] | 3966 | s = scantable(self._math._binaryop(self, other, mode)) |
---|
[513] | 3967 | elif isinstance(other, float): |
---|
| 3968 | if other == 0.0: |
---|
[718] | 3969 | raise ZeroDivisionError("Dividing by zero is not recommended") |
---|
[2574] | 3970 | s = scantable(self._math._unaryop(self, other, mode, False)) |
---|
[2144] | 3971 | elif isinstance(other, list) or isinstance(other, numpy.ndarray): |
---|
[2349] | 3972 | if isinstance(other[0], list) \ |
---|
| 3973 | or isinstance(other[0], numpy.ndarray): |
---|
[2144] | 3974 | from asapmath import _array2dOp |
---|
[2574] | 3975 | s = _array2dOp( self, other, mode, False ) |
---|
[2144] | 3976 | else: |
---|
[2574] | 3977 | s = scantable( self._math._arrayop( self, other, |
---|
| 3978 | mode, False ) ) |
---|
[513] | 3979 | else: |
---|
[718] | 3980 | raise TypeError("Other input is not a scantable or float value") |
---|
[513] | 3981 | return s |
---|
| 3982 | |
---|
[1862] | 3983 | @asaplog_post_dec |
---|
[530] | 3984 | def get_fit(self, row=0): |
---|
[1846] | 3985 | """\ |
---|
[530] | 3986 | Print or return the stored fits for a row in the scantable |
---|
[1846] | 3987 | |
---|
[530] | 3988 | Parameters: |
---|
[1846] | 3989 | |
---|
[530] | 3990 | row: the row which the fit has been applied to. |
---|
[1846] | 3991 | |
---|
[530] | 3992 | """ |
---|
| 3993 | if row > self.nrow(): |
---|
| 3994 | return |
---|
[976] | 3995 | from asap.asapfit import asapfit |
---|
[530] | 3996 | fit = asapfit(self._getfit(row)) |
---|
[1859] | 3997 | asaplog.push( '%s' %(fit) ) |
---|
| 3998 | return fit.as_dict() |
---|
[530] | 3999 | |
---|
[2349] | 4000 | @preserve_selection |
---|
[1483] | 4001 | def flag_nans(self): |
---|
[1846] | 4002 | """\ |
---|
[1483] | 4003 | Utility function to flag NaN values in the scantable. |
---|
| 4004 | """ |
---|
| 4005 | import numpy |
---|
| 4006 | basesel = self.get_selection() |
---|
| 4007 | for i in range(self.nrow()): |
---|
[1589] | 4008 | sel = self.get_row_selector(i) |
---|
| 4009 | self.set_selection(basesel+sel) |
---|
[1483] | 4010 | nans = numpy.isnan(self._getspectrum(0)) |
---|
| 4011 | if numpy.any(nans): |
---|
| 4012 | bnans = [ bool(v) for v in nans] |
---|
| 4013 | self.flag(bnans) |
---|
| 4014 | |
---|
[1588] | 4015 | def get_row_selector(self, rowno): |
---|
[1992] | 4016 | return selector(rows=[rowno]) |
---|
[1573] | 4017 | |
---|
[484] | 4018 | def _add_history(self, funcname, parameters): |
---|
[1435] | 4019 | if not rcParams['scantable.history']: |
---|
| 4020 | return |
---|
[484] | 4021 | # create date |
---|
| 4022 | sep = "##" |
---|
| 4023 | from datetime import datetime |
---|
| 4024 | dstr = datetime.now().strftime('%Y/%m/%d %H:%M:%S') |
---|
| 4025 | hist = dstr+sep |
---|
| 4026 | hist += funcname+sep#cdate+sep |
---|
[2349] | 4027 | if parameters.has_key('self'): |
---|
| 4028 | del parameters['self'] |
---|
[1118] | 4029 | for k, v in parameters.iteritems(): |
---|
[484] | 4030 | if type(v) is dict: |
---|
[1118] | 4031 | for k2, v2 in v.iteritems(): |
---|
[484] | 4032 | hist += k2 |
---|
| 4033 | hist += "=" |
---|
[1118] | 4034 | if isinstance(v2, scantable): |
---|
[484] | 4035 | hist += 'scantable' |
---|
| 4036 | elif k2 == 'mask': |
---|
[1118] | 4037 | if isinstance(v2, list) or isinstance(v2, tuple): |
---|
[513] | 4038 | hist += str(self._zip_mask(v2)) |
---|
| 4039 | else: |
---|
| 4040 | hist += str(v2) |
---|
[484] | 4041 | else: |
---|
[513] | 4042 | hist += str(v2) |
---|
[484] | 4043 | else: |
---|
| 4044 | hist += k |
---|
| 4045 | hist += "=" |
---|
[1118] | 4046 | if isinstance(v, scantable): |
---|
[484] | 4047 | hist += 'scantable' |
---|
| 4048 | elif k == 'mask': |
---|
[1118] | 4049 | if isinstance(v, list) or isinstance(v, tuple): |
---|
[513] | 4050 | hist += str(self._zip_mask(v)) |
---|
| 4051 | else: |
---|
| 4052 | hist += str(v) |
---|
[484] | 4053 | else: |
---|
| 4054 | hist += str(v) |
---|
| 4055 | hist += sep |
---|
| 4056 | hist = hist[:-2] # remove trailing '##' |
---|
| 4057 | self._addhistory(hist) |
---|
| 4058 | |
---|
[710] | 4059 | |
---|
[484] | 4060 | def _zip_mask(self, mask): |
---|
| 4061 | mask = list(mask) |
---|
| 4062 | i = 0 |
---|
| 4063 | segments = [] |
---|
| 4064 | while mask[i:].count(1): |
---|
| 4065 | i += mask[i:].index(1) |
---|
| 4066 | if mask[i:].count(0): |
---|
| 4067 | j = i + mask[i:].index(0) |
---|
| 4068 | else: |
---|
[710] | 4069 | j = len(mask) |
---|
[1118] | 4070 | segments.append([i, j]) |
---|
[710] | 4071 | i = j |
---|
[484] | 4072 | return segments |
---|
[714] | 4073 | |
---|
[626] | 4074 | def _get_ordinate_label(self): |
---|
| 4075 | fu = "("+self.get_fluxunit()+")" |
---|
| 4076 | import re |
---|
| 4077 | lbl = "Intensity" |
---|
[1118] | 4078 | if re.match(".K.", fu): |
---|
[626] | 4079 | lbl = "Brightness Temperature "+ fu |
---|
[1118] | 4080 | elif re.match(".Jy.", fu): |
---|
[626] | 4081 | lbl = "Flux density "+ fu |
---|
| 4082 | return lbl |
---|
[710] | 4083 | |
---|
[876] | 4084 | def _check_ifs(self): |
---|
[2349] | 4085 | # return len(set([self.nchan(i) for i in self.getifnos()])) == 1 |
---|
[1986] | 4086 | nchans = [self.nchan(i) for i in self.getifnos()] |
---|
[2004] | 4087 | nchans = filter(lambda t: t > 0, nchans) |
---|
[876] | 4088 | return (sum(nchans)/len(nchans) == nchans[0]) |
---|
[976] | 4089 | |
---|
[1862] | 4090 | @asaplog_post_dec |
---|
[1916] | 4091 | def _fill(self, names, unit, average, opts={}): |
---|
[976] | 4092 | first = True |
---|
| 4093 | fullnames = [] |
---|
| 4094 | for name in names: |
---|
| 4095 | name = os.path.expandvars(name) |
---|
| 4096 | name = os.path.expanduser(name) |
---|
| 4097 | if not os.path.exists(name): |
---|
| 4098 | msg = "File '%s' does not exists" % (name) |
---|
| 4099 | raise IOError(msg) |
---|
| 4100 | fullnames.append(name) |
---|
| 4101 | if average: |
---|
| 4102 | asaplog.push('Auto averaging integrations') |
---|
[1079] | 4103 | stype = int(rcParams['scantable.storage'].lower() == 'disk') |
---|
[976] | 4104 | for name in fullnames: |
---|
[1073] | 4105 | tbl = Scantable(stype) |
---|
[2004] | 4106 | if is_ms( name ): |
---|
| 4107 | r = msfiller( tbl ) |
---|
| 4108 | else: |
---|
| 4109 | r = filler( tbl ) |
---|
[976] | 4110 | msg = "Importing %s..." % (name) |
---|
[1118] | 4111 | asaplog.push(msg, False) |
---|
[2349] | 4112 | r.open(name, opts) |
---|
[2480] | 4113 | rx = rcParams['scantable.reference'] |
---|
| 4114 | r.setreferenceexpr(rx) |
---|
[1843] | 4115 | r.fill() |
---|
[976] | 4116 | if average: |
---|
[1118] | 4117 | tbl = self._math._average((tbl, ), (), 'NONE', 'SCAN') |
---|
[976] | 4118 | if not first: |
---|
| 4119 | tbl = self._math._merge([self, tbl]) |
---|
| 4120 | Scantable.__init__(self, tbl) |
---|
[1843] | 4121 | r.close() |
---|
[1118] | 4122 | del r, tbl |
---|
[976] | 4123 | first = False |
---|
[1861] | 4124 | #flush log |
---|
| 4125 | asaplog.post() |
---|
[976] | 4126 | if unit is not None: |
---|
| 4127 | self.set_fluxunit(unit) |
---|
[1824] | 4128 | if not is_casapy(): |
---|
| 4129 | self.set_freqframe(rcParams['scantable.freqframe']) |
---|
[976] | 4130 | |
---|
[2610] | 4131 | def _get_verify_action( self, msg, action=None ): |
---|
| 4132 | valid_act = ['Y', 'N', 'A', 'R'] |
---|
| 4133 | if not action or not isinstance(action, str): |
---|
| 4134 | action = raw_input("%s [Y/n/a/r] (h for help): " % msg) |
---|
| 4135 | if action == '': |
---|
| 4136 | return "Y" |
---|
| 4137 | elif (action.upper()[0] in valid_act): |
---|
| 4138 | return action.upper()[0] |
---|
| 4139 | elif (action.upper()[0] in ['H','?']): |
---|
| 4140 | print "Available actions of verification [Y|n|a|r]" |
---|
| 4141 | print " Y : Yes for current data (default)" |
---|
| 4142 | print " N : No for current data" |
---|
| 4143 | print " A : Accept all in the following and exit from verification" |
---|
| 4144 | print " R : Reject all in the following and exit from verification" |
---|
| 4145 | print " H or ?: help (show this message)" |
---|
| 4146 | return self._get_verify_action(msg) |
---|
| 4147 | else: |
---|
| 4148 | return 'Y' |
---|
[2012] | 4149 | |
---|
[1402] | 4150 | def __getitem__(self, key): |
---|
| 4151 | if key < 0: |
---|
| 4152 | key += self.nrow() |
---|
| 4153 | if key >= self.nrow(): |
---|
| 4154 | raise IndexError("Row index out of range.") |
---|
| 4155 | return self._getspectrum(key) |
---|
| 4156 | |
---|
| 4157 | def __setitem__(self, key, value): |
---|
| 4158 | if key < 0: |
---|
| 4159 | key += self.nrow() |
---|
| 4160 | if key >= self.nrow(): |
---|
| 4161 | raise IndexError("Row index out of range.") |
---|
| 4162 | if not hasattr(value, "__len__") or \ |
---|
| 4163 | len(value) > self.nchan(self.getif(key)): |
---|
| 4164 | raise ValueError("Spectrum length doesn't match.") |
---|
| 4165 | return self._setspectrum(value, key) |
---|
| 4166 | |
---|
| 4167 | def __len__(self): |
---|
| 4168 | return self.nrow() |
---|
| 4169 | |
---|
| 4170 | def __iter__(self): |
---|
| 4171 | for i in range(len(self)): |
---|
| 4172 | yield self[i] |
---|