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