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